Skip to content

Commit 693d8e9

Browse files
committed
Compile using aligned_alloc() provided by Boost.
Useful for MacOS < 10.15 (Catalina).
1 parent aa97982 commit 693d8e9

3 files changed

Lines changed: 52 additions & 2 deletions

File tree

Makefile

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ DEBUG?=0
1616
PROFILE?=0
1717
SAVE_ASM?=0
1818
WINDOWS?=0
19+
MACOS?=0
20+
BOOST?=0
1921

2022
# VARIABLES
2123
CFLAGS+=-std=c11 -pedantic
2224
CFLAGS+=-msse2 -mfpmath=sse
2325
CFLAGS+=-g
26+
CXXFLAGS+=-msse2 -mfpmath=sse -std=c++17
27+
CXXFLAGS+=-g
2428
WARN_FLAGS+=-Wall -Wextra -Winline -Wshadow
2529
NO_WARN_FLAGS+=-w
2630
ifeq ($(CC),)
@@ -70,6 +74,19 @@ CFLAGS+=-mstackrealign # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=48659
7074
RES+=icon.rc.o
7175
endif
7276

77+
ifeq ($(MACOS),1)
78+
HOMEBREW:=$(shell brew --prefix)
79+
CPPFLAGS+=-I$(HOMEBREW)/include
80+
LDFLAGS+=-L$(HOMEBREW)/lib
81+
82+
ifeq ($(BOOST),1)
83+
HOMEBREW_BOOST:=$(shell brew --prefix boost)
84+
CPPFLAGS+=-DBOOST_ALIGNED_ALLOC -I$(HOMEBREW_BOOST)/include
85+
LDFLAGS+=-L$(HOMEBREW_BOOST)/lib
86+
OBJS+=boost_aligned_alloc.o
87+
endif
88+
endif
89+
7390
ifeq ($(SAVE_ASM),1)
7491
CFLAGS+=-save-temps -masm=intel -fverbose-asm
7592
endif
@@ -82,15 +99,22 @@ LDFLAGS+=$(BFLAGS)
8299
all: jpeg2png$(EXE)
83100

84101
jpeg2png$(EXE): $(OBJS) $(RES) Makefile
102+
ifeq ($(BOOST),0)
85103
$(CC) $(OBJS) $(RES) -o $@ $(LDFLAGS) $(LIBS)
104+
else
105+
$(CXX) $(OBJS) $(RES) -o $@ $(CXXFLAGS) $(LDFLAGS) $(LIBS)
106+
endif
86107

87108
-include $(OBJS:.o=.d)
88109

89110
gopt/gopt.o: gopt/gopt.c gopt/gopt.h Makefile
90-
$(CC) $< -c -o $@ $(CFLAGS) $(NO_WARN_FLAGS)
111+
$(CC) $< -c -o $@ $(CFLAGS) $(CPPFLAGS) $(NO_WARN_FLAGS)
91112

92113
%.o: %.c Makefile
93-
$(CC) -MP -MMD $< -c -o $@ $(CFLAGS) $(WARN_FLAGS)
114+
$(CC) -MP -MMD $< -c -o $@ $(CFLAGS) $(CPPFLAGS) $(WARN_FLAGS)
115+
116+
%.o: %.cpp Makefile
117+
$(CXX) -MP -MMD $< -c -o $@ $(CXXFLAGS) $(CPPFLAGS) $(WARN_FLAGS)
94118

95119
%.rc.o: %.rc Makefile
96120
$(WINDRES) $< $@

boost_aligned_alloc.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <boost/align.hpp>
2+
#include <boost/align/aligned_alloc.hpp>
3+
#include "utils.h"
4+
5+
void *boost_aligned_alloc(size_t alignment, size_t size) {
6+
return boost::alignment::aligned_alloc(alignment, size);
7+
}
8+
9+
10+
void boost_aligned_free(void *p) {
11+
boost::alignment::aligned_free(p);
12+
}
13+

utils.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ clock_t start_timer(const char *name);
1919
void stop_timer(clock_t t, const char *n);
2020
void compare(const char *name, unsigned w, unsigned h, float *bnew, float *bold);
2121

22+
#ifdef BOOST_ALIGNED_ALLOC
23+
void *boost_aligned_alloc(size_t alignment, size_t size);
24+
void boost_aligned_free(void *p);
25+
#endif
26+
2227
// Convenience macros
2328
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
2429
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
@@ -93,8 +98,12 @@ static inline float sqf(float x) {
9398
static inline void *alloc_simd(size_t n) {
9499
#if defined(_WIN32)
95100
void *p = _aligned_malloc(n, 16);
101+
#else
102+
#if defined BOOST_ALIGNED_ALLOC
103+
void *p = boost_aligned_alloc(16, n);
96104
#else
97105
void *p = aligned_alloc(16, n);
106+
#endif
98107
#endif
99108
if(!p) { die("allocation error"); }
100109
ASSUME_ALIGNED(p);
@@ -104,9 +113,13 @@ static inline void *alloc_simd(size_t n) {
104113
static inline void free_simd(void *p) {
105114
#ifdef _WIN32
106115
_aligned_free(p);
116+
#else
117+
#if defined BOOST_ALIGNED_ALLOC
118+
boost_aligned_free(p);
107119
#else
108120
free(p);
109121
#endif
122+
#endif
110123
}
111124

112125
#ifdef __cplusplus

0 commit comments

Comments
 (0)