diff --git a/uuid4.c b/uuid4.c new file mode 100644 index 0000000..b474305 --- /dev/null +++ b/uuid4.c @@ -0,0 +1,43 @@ +#include "intern/wapp/src/common/wapp_common.h" +#include "intern/wapp/src/prng/wapp_prng.h" +#include +#include + +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); +}