Skip to content

Commit 0b5d07d

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

3 files changed

Lines changed: 48 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+=-DALIGN_ALLOC_BOOST -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 *aligned_alloc(size_t alignment, size_t size) {
6+
return boost::alignment::aligned_alloc(alignment, size);
7+
}
8+
9+
10+
void aligned_free(void *p) {
11+
boost::alignment::aligned_free(p);
12+
}
13+

utils.h

Lines changed: 9 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 ALIGN_ALLOC_BOOST
23+
void *aligned_alloc(size_t alignment, size_t size);
24+
void 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))
@@ -104,9 +109,13 @@ static inline void *alloc_simd(size_t n) {
104109
static inline void free_simd(void *p) {
105110
#ifdef _WIN32
106111
_aligned_free(p);
112+
#else
113+
#if defined ALIGN_ALLOC_BOOST
114+
aligned_free(p);
107115
#else
108116
free(p);
109117
#endif
118+
#endif
110119
}
111120

112121
#ifdef __cplusplus

0 commit comments

Comments
 (0)