Skip to content

Commit 7264603

Browse files
unamedkrclaude
andcommitted
MSVC fixes round 2: address PierreChatelier feedback (#10)
1. -Wall -Wpedantic: wrapped in if(NOT MSVC) for all targets 2. dirent.h: minimal FindFirstFile-based opendir/readdir/closedir for MSVC 3. PTHREAD_MUTEX_INITIALIZER: switch to SRWLOCK + SRWLOCK_INIT (CRITICAL_SECTION has no static initializer equivalent) 4. Windows.h: added #include in tq_transformer.c for LARGE_INTEGER 35/35 tests pass, zero warnings on macOS. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 251a02d commit 7264603

5 files changed

Lines changed: 55 additions & 9 deletions

File tree

CMakeLists.txt

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,13 @@ if(TQ_BUILD_ROCM)
203203
endif()
204204
endif()
205205

206-
# Compiler warnings
207-
target_compile_options(turboquant PRIVATE
208-
-Wall -Wextra -Wpedantic -Wno-unused-parameter
209-
)
210-
target_compile_options(turboquant_shared PRIVATE
211-
-Wall -Wextra -Wpedantic -Wno-unused-parameter
212-
)
206+
# Compiler warnings (skip on MSVC — handled separately above)
207+
if(NOT MSVC)
208+
target_compile_options(turboquant PRIVATE
209+
-Wall -Wextra -Wpedantic -Wno-unused-parameter)
210+
target_compile_options(turboquant_shared PRIVATE
211+
-Wall -Wextra -Wpedantic -Wno-unused-parameter)
212+
endif()
213213

214214
# Tests
215215
if(TQ_BUILD_TESTS)
@@ -289,7 +289,9 @@ if(TQ_BUILD_SERVER)
289289
if(NOT MSVC)
290290
target_link_libraries(quant-server m)
291291
endif()
292-
target_compile_options(quant-server PRIVATE
293-
-Wall -Wextra -Wpedantic -Wno-unused-parameter)
292+
if(NOT MSVC)
293+
target_compile_options(quant-server PRIVATE
294+
-Wall -Wextra -Wpedantic -Wno-unused-parameter)
295+
endif()
294296
message(STATUS "quant.cpp: HTTP server target enabled (quant-server)")
295297
endif()

src/engine/tq_generate.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@
1313
#include <string.h>
1414
#include <math.h>
1515
#include <stdio.h>
16+
#ifdef _WIN32
17+
#include <windows.h>
18+
#define pthread_mutex_t SRWLOCK
19+
#define PTHREAD_MUTEX_INITIALIZER SRWLOCK_INIT
20+
#define pthread_mutex_lock(m) AcquireSRWLockExclusive(m)
21+
#define pthread_mutex_unlock(m) ReleaseSRWLockExclusive(m)
22+
#else
1623
#include <pthread.h>
24+
#endif
1725

1826
/* ============================================================
1927
* Argmax sampling: return token with highest logit

src/engine/tq_model.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,26 @@
3131
#define R_OK 4
3232
#endif
3333
#define access _access
34+
/* Minimal dirent.h for MSVC (FindFirstFile-based) */
35+
#ifndef _DIRENT_H_WIN32
36+
#define _DIRENT_H_WIN32
37+
struct dirent { char d_name[260]; };
38+
typedef struct { HANDLE h; WIN32_FIND_DATAA fd; int first; } DIR;
39+
static DIR* opendir(const char* p) {
40+
char buf[270]; snprintf(buf, sizeof(buf), "%s\\*", p);
41+
DIR* d = (DIR*)malloc(sizeof(DIR));
42+
d->h = FindFirstFileA(buf, &d->fd); d->first = 1;
43+
if (d->h == INVALID_HANDLE_VALUE) { free(d); return NULL; }
44+
return d;
45+
}
46+
static struct dirent* readdir(DIR* d) {
47+
static struct dirent e;
48+
if (d->first) { d->first = 0; } else if (!FindNextFileA(d->h, &d->fd)) return NULL;
49+
strncpy(e.d_name, d->fd.cFileName, 259); e.d_name[259] = 0;
50+
return &e;
51+
}
52+
static void closedir(DIR* d) { FindClose(d->h); free(d); }
53+
#endif
3454
#else
3555
#include <sys/mman.h>
3656
#include <sys/stat.h>

src/engine/tq_ops.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ typedef CONDITION_VARIABLE pthread_cond_t;
2828
#define pthread_cond_destroy(c) ((void)0)
2929
/* __thread → __declspec(thread) */
3030
#define __thread __declspec(thread)
31+
/* PTHREAD_MUTEX_INITIALIZER: SRWLOCK can be zero-initialized */
32+
typedef SRWLOCK pthread_mutex_srw_t;
33+
#undef pthread_mutex_t
34+
#define pthread_mutex_t SRWLOCK
35+
#undef pthread_mutex_init
36+
#define pthread_mutex_init(m, a) InitializeSRWLock(m)
37+
#undef pthread_mutex_lock
38+
#define pthread_mutex_lock(m) AcquireSRWLockExclusive(m)
39+
#undef pthread_mutex_unlock
40+
#define pthread_mutex_unlock(m) ReleaseSRWLockExclusive(m)
41+
#undef pthread_mutex_destroy
42+
#define pthread_mutex_destroy(m) ((void)0)
43+
#define PTHREAD_MUTEX_INITIALIZER SRWLOCK_INIT
3144
#else
3245
#include <pthread.h>
3346
#endif

src/engine/tq_transformer.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
#ifdef __APPLE__
3131
#include <unistd.h> /* getpagesize, posix_memalign */
3232
#endif
33+
#ifdef _WIN32
34+
#include <windows.h> /* QueryPerformanceCounter, LARGE_INTEGER */
35+
#endif
3336

3437
/* Unified Q2/1-bit matmul dispatch.
3538
* When model->use_1bit_weights, Q2 fields contain sign bits + norms,

0 commit comments

Comments
 (0)