From 75abeca4a9d606bee89a41475f2f451187fec127 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 4 Feb 2023 18:17:39 -0800 Subject: More compliance with C11. --- mempool/include/mempool.h | 22 +++++++++++----------- random/src/normal.c | 10 ++++++---- timer/include/timer.h | 4 ++++ timer/src/timer.c | 9 ++++----- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/mempool/include/mempool.h b/mempool/include/mempool.h index f2b20b9..a0b3a33 100644 --- a/mempool/include/mempool.h +++ b/mempool/include/mempool.h @@ -37,7 +37,7 @@ /// Return the ith block. /// The block must have been allocated. #define mempool_get_block(POOL, INDEX) \ - ((typeof((POOL)->blocks[0])*)mempool_get_block_(&(POOL)->pool, INDEX)) + ((__typeof__((POOL)->blocks[0])*)mempool_get_block_(&(POOL)->pool, INDEX)) /// Get the index to the given block. #define mempool_get_block_index(POOL, BLOCK_PTR) \ @@ -48,16 +48,16 @@ /// The caller can use 'i' as the index of the current block. /// /// It is valid to mempool_free() the object at each step of the iteration. -#define mempool_foreach(POOL, ITER, BODY) \ - for (size_t i = 0; \ - i < (sizeof((POOL)->blocks) / sizeof(typeof((POOL)->blocks[0]))); \ - ++i) { \ - if (!(POOL)->block_info[i].used) { \ - continue; \ - } \ - typeof((POOL)->blocks[0])* ITER = &(POOL)->blocks[i]; \ - (void)ITER; \ - BODY; \ +#define mempool_foreach(POOL, ITER, BODY) \ + for (size_t i = 0; \ + i < (sizeof((POOL)->blocks) / sizeof(__typeof__((POOL)->blocks[0]))); \ + ++i) { \ + if (!(POOL)->block_info[i].used) { \ + continue; \ + } \ + __typeof__((POOL)->blocks[0])* ITER = &(POOL)->blocks[i]; \ + (void)ITER; \ + BODY; \ } typedef struct BlockInfo { diff --git a/random/src/normal.c b/random/src/normal.c index d4bcac1..5978274 100644 --- a/random/src/normal.c +++ b/random/src/normal.c @@ -2,16 +2,18 @@ #include +#define PI 3.14159265359 + // Generate two samples in the standard normal distribution using the // Box-Muller transform. // https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform void normal2(double u, double v, double* z0, double* z1) { const double r = sqrt(-2 * log(u)); - const double x = 2 * M_PI * v; - *z0 = r * cos(x); - *z1 = r * sin(x); + const double x = 2 * PI * v; + *z0 = r * cos(x); + *z1 = r * sin(x); } double normal_transform(double z, double mu, double sigma) { - return z*sigma + mu; + return z * sigma + mu; } diff --git a/timer/include/timer.h b/timer/include/timer.h index a8a3f8b..b65f8d4 100644 --- a/timer/include/timer.h +++ b/timer/include/timer.h @@ -6,6 +6,10 @@ #ifdef _WIN32 typedef uint64_t time_point; #else +// Need to macro to make CLOCK_REALTIME available when compiling with ISO C11. +// The constant is only needed in the source file, but the header file needs to +// include time.h too. +#define __USE_POSIX199309 #include typedef struct timespec time_point; #endif diff --git a/timer/src/timer.c b/timer/src/timer.c index 0c33e51..da3485b 100644 --- a/timer/src/timer.c +++ b/timer/src/timer.c @@ -1,16 +1,15 @@ #include "timer.h" #include - #ifdef _WIN32 -static const int64_t microseconds = 1000000; +#define WIN32_LEAN_AND_MEAN +#include #endif -static const int64_t nanoseconds = 1000000000; #ifdef _WIN32 -#define WIN32_LEAN_AND_MEAN -#include +static const int64_t microseconds = 1000000; #endif +static const int64_t nanoseconds = 1000000000; #ifdef _WIN32 static double seconds_per_count; -- cgit v1.2.3