Add test uuid4 implementation

This commit is contained in:
Abdelrahman Said 2025-03-16 23:49:50 +00:00
parent 73fc039709
commit 9080e04d5a

43
uuid4.c Normal file
View File

@ -0,0 +1,43 @@
#include "intern/wapp/src/common/wapp_common.h"
#include "intern/wapp/src/prng/wapp_prng.h"
#include <inttypes.h>
#include <stdio.h>
typedef struct uuid4 UUID4;
struct uuid4 {
u64 high;
u64 low;
};
UUID4 gen_uuid4(XOR256State *state);
void print_uuid4(UUID4 uuid);
int main(void) {
XOR256State state = wapp_init_xor_256_state();
for (int i = 0; i < 100000; ++i) {
UUID4 uuid = gen_uuid4(&state);
print_uuid4(uuid);
}
return 0;
}
UUID4 gen_uuid4(XOR256State *state) {
persistent u64 and_mask = 0xffffffffffff0fff;
persistent u64 or_mask = 0x0000000000004000;
UUID4 uuid = { .high = wapp_xorshift_256_generate(state), .low = wapp_xorshift_256_generate(state) };
uuid.high &= and_mask;
uuid.high |= or_mask;
return uuid;
}
void print_uuid4(UUID4 uuid) {
u64 a = uuid.high >> 32;
u64 b = (uuid.high << 32) >> 48;
u64 c = (uuid.high << 48) >> 48;
u64 d = uuid.low >> 48;
u64 e = (uuid.low << 16) >> 16;
printf("%.8llx-%.4llx-%.4llx-%.4llx-%.12llx\n", a, b, c, d, e);
}