[RELEASE] u22-v03

This version is submitted to U22 breau.
This commit is contained in:
2020-09-14 00:00:00 +00:00
parent 360595de37
commit 84c3a02b9a
357 changed files with 29223 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
add_library(chaos
abchash.c
xorshift.c
)
target_link_libraries(chaos
math
)

32
util/chaos/abchash.c Normal file
View File

@@ -0,0 +1,32 @@
#include "./abchash.h"
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#define CHAOS_ABCHASH_GET_CODE(c) ( \
('0' <= c && c <= '9')? c-'0': \
('a' <= c && c <= 'z')? c-'a'+10: \
(c == '_') ? 36: UINT8_MAX)
#define CHAOS_ABCHASH_MAX_CODE 36
uint64_t chaos_abchash(const char* str, size_t len) {
assert(chaos_abchash_validate(str, len));
uint64_t ans = 0;
for (size_t i = 0; i < len; ++i) {
ans = ans*(CHAOS_ABCHASH_MAX_CODE+1) + CHAOS_ABCHASH_GET_CODE(str[i]);
}
return ans;
}
bool chaos_abchash_validate(const char* str, size_t len) {
for (size_t i = 0; i < len; ++i) {
if (CHAOS_ABCHASH_GET_CODE(str[i]) > CHAOS_ABCHASH_MAX_CODE) {
return false;
}
}
return len > 0;
}

19
util/chaos/abchash.h Normal file
View File

@@ -0,0 +1,19 @@
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
/* DON'T EXPECT THIS ALGORITHM TO BE ONE-WAY */
uint64_t
chaos_abchash(
const char* str,
size_t len
);
bool
chaos_abchash_validate(
const char* str,
size_t len
);

19
util/chaos/xorshift.c Normal file
View File

@@ -0,0 +1,19 @@
#include "./xorshift.h"
#include <stddef.h>
#include <stdint.h>
uint64_t chaos_xorshift(uint64_t seed) {
seed = seed ^ (seed << 13);
seed = seed ^ (seed >> 7);
return seed ^ (seed << 17);
}
float chaos_xorshift_fract(uint64_t seed, uint64_t* next_seed) {
static const uint64_t period = 10000;
seed = chaos_xorshift(seed);
if (next_seed != NULL) *next_seed = seed;
return seed%period*1.f/period;
}

14
util/chaos/xorshift.h Normal file
View File

@@ -0,0 +1,14 @@
#pragma once
#include <stdint.h>
uint64_t
chaos_xorshift(
uint64_t seed
);
float
chaos_xorshift_fract(
uint64_t seed,
uint64_t* next_seed /* NULLABLE */
);