From 639fca0157a08e07805847e64862388f63223b40 Mon Sep 17 00:00:00 2001 From: Leif Scholz Date: Sun, 19 Apr 2026 14:42:58 +0200 Subject: [PATCH] Fix: Enable MSVC build on Windows Replaces compiler-specific function calls with a compatible MSVC implementation. Removes unsupported '-std=gnu99' compile argument to allow clean compilation on Windows. --- setup.py | 2 +- src/pidng/liblj92/lj92.c | 21 ++++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/setup.py b/setup.py index 49ee496..0562fca 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ long_description = fh.read() ljpeg92 = Extension('ljpegCompress', sources=[ - "src/pidng/bitunpack.c", "src/pidng/liblj92/lj92.c"], extra_compile_args=['-std=gnu99'], extra_link_args=[]) + "src/pidng/bitunpack.c", "src/pidng/liblj92/lj92.c"], extra_compile_args=[], extra_link_args=[]) setup( name="pidng", diff --git a/src/pidng/liblj92/lj92.c b/src/pidng/liblj92/lj92.c index b46a68f..6908ab5 100644 --- a/src/pidng/liblj92/lj92.c +++ b/src/pidng/liblj92/lj92.c @@ -35,14 +35,21 @@ typedef uint32_t u32; //#define SLOW_HUFF //#define DEBUG -#if defined(__clang__) - #define _clz __builtin_clz -#elif defined(__GNUC__) || defined(__GNUG__) - #define _clz __builtin_clz -#elif defined(_MSC_VER) - #define _clz __lzcnt +#if defined(_MSC_VER) +#include +static inline int __msvc_clz(unsigned int x) { + unsigned long leading_zero; + if (_BitScanReverse(&leading_zero, x)) { + return 31 - leading_zero; + } else { + return 32; + } +} +#define _clz __msvc_clz +#elif defined(__GNUC__) || defined(__clang__) +#define _clz __builtin_clz #else - #error Could not find builtin clz function +#error Could not find builtin clz function #endif typedef struct _ljp {