Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing compilation on Windows #51

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions cutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void pstrcpy(char *buf, int buf_size, const char *str)
char *pstrcat(char *buf, int buf_size, const char *s)
{
int len;
len = strlen(buf);
len = (int)strlen(buf);
if (len < buf_size)
pstrcpy(buf + len, buf_size - len, s);
return buf;
Expand Down Expand Up @@ -166,7 +166,7 @@ int dbuf_putstr(DynBuf *s, const char *str)
return dbuf_put(s, (const uint8_t *)str, strlen(str));
}

int __attribute__((format(printf, 2, 3))) dbuf_printf(DynBuf *s,
int util_format(printf, 2, 3) dbuf_printf(DynBuf *s,
const char *fmt, ...)
{
va_list ap;
Expand Down Expand Up @@ -235,7 +235,7 @@ int unicode_to_utf8(uint8_t *buf, unsigned int c)
}
*q++ = (c & 0x3f) | 0x80;
}
return q - buf;
return (int)(q - buf);
}

static const unsigned int utf8_min_code[5] = {
Expand All @@ -250,7 +250,8 @@ static const unsigned char utf8_first_code_mask[5] = {
be >= 1. The maximum length for a UTF8 byte sequence is 6 bytes. */
int unicode_from_utf8(const uint8_t *p, int max_len, const uint8_t **pp)
{
int l, c, b, i;
int l, b, i;
uint8_t c;

c = *p++;
if (c < 0x80) {
Expand Down
201 changes: 187 additions & 14 deletions cutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,160 @@
/* set if CPU is big endian */
#undef WORDS_BIGENDIAN

#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#define force_inline inline __attribute__((always_inline))
#define no_inline __attribute__((noinline))
#define __maybe_unused __attribute__((unused))
#if defined(_WIN32)

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <intrin.h>
#include <winsock.h>

#include "win32/stdatomic.h"
#define _Atomic(x) x

#include <pthread.h> /* needed for timespec */

#define likely(x) (x)
#define unlikely(x) (x)
#define force_inline __forceinline
#define no_inline __declspec(noinline)
#define __maybe_unused
#define util_packed(_declaration) __pragma(pack(push, 1)) _declaration __pragma(pack(pop))
#define util_format(...)

#define js_popen _popen
#define js_pclose _pclose
#else

#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#define force_inline inline __attribute__((always_inline))
#define no_inline __attribute__((noinline))
#define __maybe_unused __attribute__((unused))
#define util_packed(_declaration) _declaration __attribute__((packed))
#define util_format(...) util_format(__VA_ARGS__))

#define js_popen popen
#define js_pclose pclose
#endif

#if !defined(_SSIZE_T_DEFINED)
#if defined(_WIN64)
typedef unsigned __int64 ssize_t;
#elif defined(_WIN32)
typedef _W64 unsigned int ssize_t;
#endif
#define _SSIZE_T_DEFINED
#endif

#if defined(_WIN32)
static int js_gettimeofday(struct timeval* tv, void* tz)
{
(void)tz;

// Note: some broken versions only have 8 trailing zero's, the correct epoch has 9 trailing zero's
// This magic number is the number of 100 nanosecond intervals since January 1, 1601 (UTC)
// until 00:00:00 January 1, 1970
static const uint64_t EPOCH = ((uint64_t)116444736000000000ULL);

SYSTEMTIME system_time;
FILETIME file_time;
uint64_t time;

GetSystemTime(&system_time);
SystemTimeToFileTime(&system_time, &file_time);

time = ((uint64_t)file_time.dwLowDateTime);
time += ((uint64_t)file_time.dwHighDateTime) << 32;

tv->tv_sec = (long)((time - EPOCH) / 10000000L);
tv->tv_usec = (long)(system_time.wMilliseconds * 1000);

return 0;
}

static int js_clock_getrealtime(struct timespec* ts)
{
/* https://stackoverflow.com/a/5404467 */

LARGE_INTEGER t;
FILETIME f;
double microseconds;
static LARGE_INTEGER offset;
static double frequencyToMicroseconds;
static int initialized = 0;
static BOOL usePerformanceCounter = 0;

if (!initialized)
{
LARGE_INTEGER performanceFrequency;
initialized = 1;
usePerformanceCounter = QueryPerformanceFrequency(&performanceFrequency);
if (usePerformanceCounter)
{
QueryPerformanceCounter(&offset);
frequencyToMicroseconds = (double)performanceFrequency.QuadPart / 1000000.;
}
else
{
SYSTEMTIME s;
s.wYear = 1970;
s.wMonth = 1;
s.wDay = 1;
s.wHour = 0;
s.wMinute = 0;
s.wSecond = 0;
s.wMilliseconds = 0;
SystemTimeToFileTime(&s, &f);

offset.QuadPart = f.dwHighDateTime;
offset.QuadPart <<= 32;
offset.QuadPart |= f.dwLowDateTime;

frequencyToMicroseconds = 10.0;
}
}

if (usePerformanceCounter)
{
QueryPerformanceCounter(&t);
}
else
{
GetSystemTimeAsFileTime(&f);
t.QuadPart = f.dwHighDateTime;
t.QuadPart <<= 32;
t.QuadPart |= f.dwLowDateTime;
}

t.QuadPart -= offset.QuadPart;
microseconds = (double)t.QuadPart / frequencyToMicroseconds;
t.QuadPart = microseconds;

ts->tv_sec = t.QuadPart / 1000000;
ts->tv_nsec = t.QuadPart % 1000000;

return 0;
}

static int js_clock_getmonotonic(struct timespec* ts)
{
return js_clock_getrealtime(ts);
}
#else
#define js_gettimeofday gettimeofday

static int js_clock_getrealtime(struct timespec* ts)
{
return clock_gettime(CLOCK_REALTIME, ts);
}

static int js_clock_getmonotonic(struct timespec* ts)
{
return clock_gettime(CLOCK_MONOTONIC, ts);
}

#define js_popen popen
#endif

#define xglue(x, y) x ## y
#define glue(x, y) xglue(x, y)
Expand Down Expand Up @@ -112,40 +261,64 @@ static inline int64_t min_int64(int64_t a, int64_t b)
}

/* WARNING: undefined if a = 0 */
static inline int clz32(unsigned int a)
static force_inline int clz32(unsigned int a)
{
#ifdef _WIN32
uint32_t leading_zero = 0;
/* failing to bit scan is undefined, returning 32 instead */
return (_BitScanReverse(&leading_zero, a)) ? (31 - leading_zero) : 32;
#else
return __builtin_clz(a);
#endif
}

/* WARNING: undefined if a = 0 */
static inline int clz64(uint64_t a)
static force_inline int clz64(uint64_t a)
{
#ifdef _WIN32
uint32_t leading_zero = 0;
/* failing to bit scan is undefined, returning 64 instead */
return (_BitScanReverse64(&leading_zero, a)) ? (63 - leading_zero) : 32;
#else
return __builtin_clzll(a);
#endif
}

/* WARNING: undefined if a = 0 */
static inline int ctz32(unsigned int a)
{
#ifdef _WIN32
uint32_t trailing_zero = 0;
/* failing to bit scan is undefined, returning 32 instead */
return (_BitScanForward(&trailing_zero, a)) ? trailing_zero : 32;
#else
return __builtin_ctz(a);
#endif
}

/* WARNING: undefined if a = 0 */
static inline int ctz64(uint64_t a)
{
#ifdef _WIN32
uint32_t trailing_zero = 0;
/* failing to bit scan is undefined, returning 64 instead */
return (_BitScanForward64(&trailing_zero, a)) ? trailing_zero : 64;
#else
return __builtin_ctzll(a);
#endif
}

struct __attribute__((packed)) packed_u64 {
util_packed(struct packed_u64 {
uint64_t v;
};
});

struct __attribute__((packed)) packed_u32 {
util_packed(struct packed_u32 {
uint32_t v;
};
});

struct __attribute__((packed)) packed_u16 {
util_packed(struct packed_u16 {
uint16_t v;
};
});

static inline uint64_t get_u64(const uint8_t *tab)
{
Expand Down Expand Up @@ -262,7 +435,7 @@ static inline int dbuf_put_u64(DynBuf *s, uint64_t val)
{
return dbuf_put(s, (uint8_t *)&val, 8);
}
int __attribute__((format(printf, 2, 3))) dbuf_printf(DynBuf *s,
int util_format(printf, 2, 3) dbuf_printf(DynBuf *s,
const char *fmt, ...);
void dbuf_free(DynBuf *s);
static inline BOOL dbuf_error(DynBuf *s) {
Expand Down
32 changes: 16 additions & 16 deletions libbf.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ static inline limb_t get_bit(const limb_t *tab, limb_t len, slimb_t pos)
{
slimb_t i;
i = pos >> LIMB_LOG2_BITS;
if (i < 0 || i >= len)
if (i < 0 || i >= (slimb_t)len)
return 0;
return (tab[i] >> (pos & (LIMB_BITS - 1))) & 1;
}
Expand Down Expand Up @@ -395,7 +395,7 @@ static inline limb_t scan_bit_nz(const bf_t *r, slimb_t bit_pos)
static int bf_get_rnd_add(int *pret, const bf_t *r, limb_t l,
slimb_t prec, int rnd_mode)
{
int add_one, inexact;
limb_t add_one, inexact;
limb_t bit1, bit0;

if (rnd_mode == BF_RNDF) {
Expand All @@ -420,7 +420,7 @@ static int bf_get_rnd_add(int *pret, const bf_t *r, limb_t l,
} else {
/* round to even */
add_one =
get_bit(r->tab, l, l * LIMB_BITS - 1 - (prec - 1));
get_bit(r->tab, l, (slimb_t)(l * LIMB_BITS - 1 - (prec - 1)));
}
}
break;
Expand All @@ -442,7 +442,7 @@ static int bf_get_rnd_add(int *pret, const bf_t *r, limb_t l,

if (inexact)
*pret |= BF_ST_INEXACT;
return add_one;
return (int)add_one;
}

static int bf_set_overflow(bf_t *r, int sign, limb_t prec, bf_flags_t flags)
Expand Down Expand Up @@ -5356,38 +5356,38 @@ int bf_acos(bf_t *r, const bf_t *a, limb_t prec, bf_flags_t flags)
/* Note: we assume __int128 is available */
#define muldq(r1, r0, a, b) \
do { \
unsigned __int128 __t; \
__t = (unsigned __int128)(a) * (unsigned __int128)(b); \
dlimb_t __t; \
__t = (dlimb_t)(a) * (dlimb_t)(b); \
r0 = __t; \
r1 = __t >> 64; \
} while (0)

#define divdq(q, r, a1, a0, b) \
do { \
unsigned __int128 __t; \
dlimb_t __t; \
limb_t __b = (b); \
__t = ((unsigned __int128)(a1) << 64) | (a0); \
q = __t / __b; \
r = __t % __b; \
__t = ((dlimb_t)(a1) << 64) | (a0); \
q = __t / __b; \
r = __t % __b; \
} while (0)

#else

#define muldq(r1, r0, a, b) \
do { \
uint64_t __t; \
__t = (uint64_t)(a) * (uint64_t)(b); \
uint64_t __t; \
__t = (uint64_t)(a) * (uint64_t)(b); \
r0 = __t; \
r1 = __t >> 32; \
} while (0)

#define divdq(q, r, a1, a0, b) \
do { \
uint64_t __t; \
uint64_t __t; \
limb_t __b = (b); \
__t = ((uint64_t)(a1) << 32) | (a0); \
q = __t / __b; \
r = __t % __b; \
__t = ((uint64_t)(a1) << 32) | (a0); \
q = __t / __b; \
r = __t % __b; \
} while (0)

#endif /* LIMB_BITS != 64 */
Expand Down
Loading