aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2023-02-04 18:17:39 -0800
committer3gg <3gg@shellblade.net>2023-02-04 18:17:39 -0800
commit75abeca4a9d606bee89a41475f2f451187fec127 (patch)
tree348c29304d2500f87ebce81b5ce843793d53ee68
parent90183beeb914590a2ea4eff0242b813562d2d641 (diff)
More compliance with C11.
-rw-r--r--mempool/include/mempool.h22
-rw-r--r--random/src/normal.c10
-rw-r--r--timer/include/timer.h4
-rw-r--r--timer/src/timer.c9
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 @@
37/// Return the ith block. 37/// Return the ith block.
38/// The block must have been allocated. 38/// The block must have been allocated.
39#define mempool_get_block(POOL, INDEX) \ 39#define mempool_get_block(POOL, INDEX) \
40 ((typeof((POOL)->blocks[0])*)mempool_get_block_(&(POOL)->pool, INDEX)) 40 ((__typeof__((POOL)->blocks[0])*)mempool_get_block_(&(POOL)->pool, INDEX))
41 41
42/// Get the index to the given block. 42/// Get the index to the given block.
43#define mempool_get_block_index(POOL, BLOCK_PTR) \ 43#define mempool_get_block_index(POOL, BLOCK_PTR) \
@@ -48,16 +48,16 @@
48/// The caller can use 'i' as the index of the current block. 48/// The caller can use 'i' as the index of the current block.
49/// 49///
50/// It is valid to mempool_free() the object at each step of the iteration. 50/// It is valid to mempool_free() the object at each step of the iteration.
51#define mempool_foreach(POOL, ITER, BODY) \ 51#define mempool_foreach(POOL, ITER, BODY) \
52 for (size_t i = 0; \ 52 for (size_t i = 0; \
53 i < (sizeof((POOL)->blocks) / sizeof(typeof((POOL)->blocks[0]))); \ 53 i < (sizeof((POOL)->blocks) / sizeof(__typeof__((POOL)->blocks[0]))); \
54 ++i) { \ 54 ++i) { \
55 if (!(POOL)->block_info[i].used) { \ 55 if (!(POOL)->block_info[i].used) { \
56 continue; \ 56 continue; \
57 } \ 57 } \
58 typeof((POOL)->blocks[0])* ITER = &(POOL)->blocks[i]; \ 58 __typeof__((POOL)->blocks[0])* ITER = &(POOL)->blocks[i]; \
59 (void)ITER; \ 59 (void)ITER; \
60 BODY; \ 60 BODY; \
61 } 61 }
62 62
63typedef struct BlockInfo { 63typedef 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 @@
2 2
3#include <math.h> 3#include <math.h>
4 4
5#define PI 3.14159265359
6
5// Generate two samples in the standard normal distribution using the 7// Generate two samples in the standard normal distribution using the
6// Box-Muller transform. 8// Box-Muller transform.
7// https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform 9// https://en.wikipedia.org/wiki/Box%E2%80%93Muller_transform
8void normal2(double u, double v, double* z0, double* z1) { 10void normal2(double u, double v, double* z0, double* z1) {
9 const double r = sqrt(-2 * log(u)); 11 const double r = sqrt(-2 * log(u));
10 const double x = 2 * M_PI * v; 12 const double x = 2 * PI * v;
11 *z0 = r * cos(x); 13 *z0 = r * cos(x);
12 *z1 = r * sin(x); 14 *z1 = r * sin(x);
13} 15}
14 16
15double normal_transform(double z, double mu, double sigma) { 17double normal_transform(double z, double mu, double sigma) {
16 return z*sigma + mu; 18 return z * sigma + mu;
17} 19}
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 @@
6#ifdef _WIN32 6#ifdef _WIN32
7typedef uint64_t time_point; 7typedef uint64_t time_point;
8#else 8#else
9// Need to macro to make CLOCK_REALTIME available when compiling with ISO C11.
10// The constant is only needed in the source file, but the header file needs to
11// include time.h too.
12#define __USE_POSIX199309
9#include <time.h> 13#include <time.h>
10typedef struct timespec time_point; 14typedef struct timespec time_point;
11#endif 15#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 @@
1#include "timer.h" 1#include "timer.h"
2 2
3#include <stdlib.h> 3#include <stdlib.h>
4
5#ifdef _WIN32 4#ifdef _WIN32
6static const int64_t microseconds = 1000000; 5#define WIN32_LEAN_AND_MEAN
6#include <Windows.h>
7#endif 7#endif
8static const int64_t nanoseconds = 1000000000;
9 8
10#ifdef _WIN32 9#ifdef _WIN32
11#define WIN32_LEAN_AND_MEAN 10static const int64_t microseconds = 1000000;
12#include <Windows.h>
13#endif 11#endif
12static const int64_t nanoseconds = 1000000000;
14 13
15#ifdef _WIN32 14#ifdef _WIN32
16static double seconds_per_count; 15static double seconds_per_count;