diff --git a/Makefile b/Makefile index 6e2460d..5d60550 100644 --- a/Makefile +++ b/Makefile @@ -9,9 +9,6 @@ CFLAGS ?= -O2 -s -std=c11 -Wall -Wextra -march=native CPPFLAGS += -D_POSIX_C_SOURCE=200809L -D_DEFAULT_SOURCE LDFLAGS ?= LDLIBS += -lpthread -# The matcher uses POSIX memmem(), which glibc accelerates with SIMD on -# whatever CPU you run on. -mavx2 is harmless but unnecessary; drop it for -# maximum portability (e.g. make MARCH=""). MARCH ?= BINARY := gg diff --git a/gg.c b/gg.c index 1b61b4c..8d95690 100644 --- a/gg.c +++ b/gg.c @@ -6,6 +6,9 @@ * Created by Luxferre in 2026, released into the public domain * */ +#if defined(__APPLE__) && !defined(_DARWIN_C_SOURCE) +#define _DARWIN_C_SOURCE +#endif #ifndef _POSIX_C_SOURCE #define _POSIX_C_SOURCE 200809L #endif @@ -29,7 +32,7 @@ /* sysctlbyname() is the portable way to query CPU count on every BSD family * (FreeBSD, OpenBSD, NetBSD, DragonFly) and on macOS/Darwin. */ -#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__APPLE__) +#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__APPLE__) #include #endif @@ -52,7 +55,7 @@ * all provide it, but only expose the prototype outside strict conformance * mode, so declare it ourselves when the system has not already. Any libc that * genuinely lacks it (rare) gets a small portable fallback. */ -#if defined(__GLIBC__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__APPLE__) +#if defined(__GLIBC__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__APPLE__) #ifndef memmem void *memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen); @@ -60,13 +63,13 @@ void *memmem(const void *haystack, size_t haystacklen, #else static void *gg_memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) { - if (needlelen == 0) return (void *)haystack; - if (haystacklen < needlelen) return NULL; + if(needlelen == 0) return (void *)haystack; + if(haystacklen < needlelen) return NULL; const unsigned char *h = (const unsigned char *)haystack; const unsigned char *n = (const unsigned char *)needle; const unsigned char *end = h + haystacklen - needlelen; - for (; h <= end; h++) - if (memcmp(h, n, needlelen) == 0) return (void *)h; + for(; h <= end; h++) + if(memcmp(h, n, needlelen) == 0) return (void *)h; return NULL; } #define memmem gg_memmem @@ -76,15 +79,15 @@ static void *gg_memmem(const void *haystack, size_t haystacklen, static long gg_nproc(void) { #if defined(_SC_NPROCESSORS_ONLN) long n = sysconf(_SC_NPROCESSORS_ONLN); - if (n > 0) return n; + if(n > 0) return n; #elif defined(_SC_NPROCESSORS_CONF) long n = sysconf(_SC_NPROCESSORS_CONF); - if (n > 0) return n; + if(n > 0) return n; #endif #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || defined(__APPLE__) int ncpu = 1; size_t len = sizeof(ncpu); - if (sysctlbyname("hw.ncpu", &ncpu, &len, NULL, 0) == 0 && ncpu > 0) + if(sysctlbyname("hw.ncpu", &ncpu, &len, NULL, 0) == 0 && ncpu > 0) return (long)ncpu; #endif return 1;