Mass reformat
This commit is contained in:
parent
4d27def35e
commit
df11e34235
27 changed files with 121 additions and 136 deletions
|
|
@ -30,12 +30,8 @@ typedef struct __attribute__((__packed__)) {
|
||||||
|
|
||||||
void print_command(CommandMessage *m) {
|
void print_command(CommandMessage *m) {
|
||||||
switch (m->type) {
|
switch (m->type) {
|
||||||
case CMD_SET_LEVEL:
|
case CMD_SET_LEVEL: printf("Type: SetLevel\nPayload: %d\n", m->data.set_level.level); break;
|
||||||
printf("Type: SetLevel\nPayload: %d\n", m->data.set_level.level);
|
case CMD_SET_LIGHTS: printf("Type: SetLights\nPayload: %d\n", m->data.set_lights.on); break;
|
||||||
break;
|
|
||||||
case CMD_SET_LIGHTS:
|
|
||||||
printf("Type: SetLights\nPayload: %d\n", m->data.set_lights.on);
|
|
||||||
break;
|
|
||||||
case CMD_SET_DISTANCE_FACTOR:
|
case CMD_SET_DISTANCE_FACTOR:
|
||||||
printf("Type: SetDistanceFactor\nPayload: %.2f\n", m->data.set_distance.factor);
|
printf("Type: SetDistanceFactor\nPayload: %.2f\n", m->data.set_distance.factor);
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
11
break.c
11
break.c
|
|
@ -1,8 +1,8 @@
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/mman.h> // mmap and friends
|
#include <sys/mman.h> // mmap and friends
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdbool.h>
|
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
/*
|
/*
|
||||||
|
|
@ -12,8 +12,7 @@ int main(void) {
|
||||||
* See: "man 3 getauxval" or
|
* See: "man 3 getauxval" or
|
||||||
* https://www.man7.org/linux/man-pages/man3/getauxval.3.html
|
* https://www.man7.org/linux/man-pages/man3/getauxval.3.html
|
||||||
*/
|
*/
|
||||||
long page_size =
|
long page_size = sysconf(_SC_PAGESIZE); // or _SC_PAGE_SIZE (POSIX allows both)
|
||||||
sysconf(_SC_PAGESIZE); // or _SC_PAGE_SIZE (POSIX allows both)
|
|
||||||
if (page_size == -1) {
|
if (page_size == -1) {
|
||||||
perror("sysconf");
|
perror("sysconf");
|
||||||
return 1;
|
return 1;
|
||||||
|
|
@ -45,11 +44,9 @@ int main(void) {
|
||||||
* (which must be greater than 0).
|
* (which must be greater than 0).
|
||||||
*/
|
*/
|
||||||
|
|
||||||
uint8_t *first_mmap = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
|
uint8_t *first_mmap = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
|
||||||
MAP_PRIVATE | MAP_ANON, -1, 0);
|
|
||||||
|
|
||||||
uint8_t *second_mmap = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
|
uint8_t *second_mmap = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
|
||||||
MAP_PRIVATE | MAP_ANON, -1, 0);
|
|
||||||
|
|
||||||
printf("First mmap: %p\n", first_mmap);
|
printf("First mmap: %p\n", first_mmap);
|
||||||
printf("Second mmap: %p\n", second_mmap);
|
printf("Second mmap: %p\n", second_mmap);
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ typedef struct block {
|
||||||
// int is_free;
|
// int is_free;
|
||||||
} block_t;
|
} block_t;
|
||||||
|
|
||||||
static char *heap;
|
static char *heap;
|
||||||
static block_t *free_lists[MAX_ORDER - MIN_ORDER + 1];
|
static block_t *free_lists[MAX_ORDER - MIN_ORDER + 1];
|
||||||
|
|
||||||
void buddy_init() {
|
void buddy_init() {
|
||||||
|
|
@ -59,7 +59,7 @@ void *buddy_alloc(size_t size) {
|
||||||
|
|
||||||
size_t split_size = ORDER_TO_SZ(i);
|
size_t split_size = ORDER_TO_SZ(i);
|
||||||
// FIXME: Char???
|
// FIXME: Char???
|
||||||
char *middle = ((char *)bigger + split_size);
|
char *middle = ((char *)bigger + split_size);
|
||||||
block_t *buddy = (block_t *)middle;
|
block_t *buddy = (block_t *)middle;
|
||||||
// block_t *buddy = (block_t *)((char *)bigger + split_size);
|
// block_t *buddy = (block_t *)((char *)bigger + split_size);
|
||||||
buddy->next = NULL;
|
buddy->next = NULL;
|
||||||
|
|
|
||||||
|
|
@ -13,16 +13,16 @@ void buf_append_u32(uint8_t *buf, uint32_t value, size_t *index);
|
||||||
void buf_append_i64(uint8_t *buf, int64_t value, size_t *index);
|
void buf_append_i64(uint8_t *buf, int64_t value, size_t *index);
|
||||||
void buf_append_u64(uint8_t *buf, uint64_t value, size_t *index);
|
void buf_append_u64(uint8_t *buf, uint64_t value, size_t *index);
|
||||||
|
|
||||||
int8_t buf_read_i8(const uint8_t *buf, size_t *index);
|
int8_t buf_read_i8(const uint8_t *buf, size_t *index);
|
||||||
uint8_t buf_read_u8(const uint8_t *buf, size_t *index);
|
uint8_t buf_read_u8(const uint8_t *buf, size_t *index);
|
||||||
|
|
||||||
int16_t buf_read_i16(const uint8_t *buf, size_t *index);
|
int16_t buf_read_i16(const uint8_t *buf, size_t *index);
|
||||||
uint16_t buf_read_u16(const uint8_t *buf, size_t *index);
|
uint16_t buf_read_u16(const uint8_t *buf, size_t *index);
|
||||||
|
|
||||||
int32_t buf_read_i32(const uint8_t *buf, size_t *index);
|
int32_t buf_read_i32(const uint8_t *buf, size_t *index);
|
||||||
uint32_t buf_read_u32(const uint8_t *buf, size_t *index);
|
uint32_t buf_read_u32(const uint8_t *buf, size_t *index);
|
||||||
|
|
||||||
int64_t buf_read_i64(const uint8_t *buf, size_t *index);
|
int64_t buf_read_i64(const uint8_t *buf, size_t *index);
|
||||||
uint64_t buf_read_u64(const uint8_t *buf, size_t *index);
|
uint64_t buf_read_u64(const uint8_t *buf, size_t *index);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
||||||
35
calc.c
35
calc.c
|
|
@ -2,24 +2,16 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
typedef enum {
|
typedef enum { TOKEN_INT, TOKEN_PLUS, TOKEN_MUL, TOKEN_DIV, TOKEN_LPAREN, TOKEN_RPAREN, TOKEN_EOF } TokenType;
|
||||||
TOKEN_INT,
|
|
||||||
TOKEN_PLUS,
|
|
||||||
TOKEN_MUL,
|
|
||||||
TOKEN_DIV,
|
|
||||||
TOKEN_LPAREN,
|
|
||||||
TOKEN_RPAREN,
|
|
||||||
TOKEN_EOF
|
|
||||||
} TokenType;
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
TokenType type;
|
TokenType type;
|
||||||
int value;
|
int value;
|
||||||
} Token;
|
} Token;
|
||||||
|
|
||||||
const char *input;
|
const char *input;
|
||||||
size_t pos;
|
size_t pos;
|
||||||
Token current_token;
|
Token current_token;
|
||||||
|
|
||||||
void skip_whitespace() {
|
void skip_whitespace() {
|
||||||
while (isspace(input[pos])) pos++;
|
while (isspace(input[pos])) pos++;
|
||||||
|
|
@ -43,19 +35,12 @@ Token get_next_token() {
|
||||||
|
|
||||||
pos++;
|
pos++;
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
case '+':
|
case '+': return (Token){TOKEN_PLUS, 0};
|
||||||
return (Token){TOKEN_PLUS, 0};
|
case '*': return (Token){TOKEN_MUL, 0};
|
||||||
case '*':
|
case '/': return (Token){TOKEN_DIV, 0};
|
||||||
return (Token){TOKEN_MUL, 0};
|
case '(': return (Token){TOKEN_LPAREN, 0};
|
||||||
case '/':
|
case ')': return (Token){TOKEN_RPAREN, 0};
|
||||||
return (Token){TOKEN_DIV, 0};
|
default: fprintf(stderr, "Unexpected character: %c\n", ch); exit(1);
|
||||||
case '(':
|
|
||||||
return (Token){TOKEN_LPAREN, 0};
|
|
||||||
case ')':
|
|
||||||
return (Token){TOKEN_RPAREN, 0};
|
|
||||||
default:
|
|
||||||
fprintf(stderr, "Unexpected character: %c\n", ch);
|
|
||||||
exit(1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,8 +14,12 @@ typedef struct {
|
||||||
int (*get)();
|
int (*get)();
|
||||||
} Test;
|
} Test;
|
||||||
|
|
||||||
void my_put(int a) { global = a; }
|
void my_put(int a) {
|
||||||
int my_get() { return global; }
|
global = a;
|
||||||
|
}
|
||||||
|
int my_get() {
|
||||||
|
return global;
|
||||||
|
}
|
||||||
|
|
||||||
void executor(Test *t) {
|
void executor(Test *t) {
|
||||||
t->put(10);
|
t->put(10);
|
||||||
|
|
|
||||||
2
djb2.c
2
djb2.c
|
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
unsigned long djb2(const char *str) {
|
unsigned long djb2(const char *str) {
|
||||||
unsigned long h = 5381;
|
unsigned long h = 5381;
|
||||||
int c;
|
int c;
|
||||||
|
|
||||||
while ((c = *str++)) h = ((h << 5) + h) + c; // h = h * 33 + c;
|
while ((c = *str++)) h = ((h << 5) + h) + c; // h = h * 33 + c;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -74,6 +74,10 @@ void *dynbuf_get(Dynbuf *v, size_t index) {
|
||||||
return (char *)v->data + index * v->elem_size;
|
return (char *)v->data + index * v->elem_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline size_t dynbuf_size(const Dynbuf *v) { return v->length; }
|
inline size_t dynbuf_size(const Dynbuf *v) {
|
||||||
|
return v->length;
|
||||||
|
}
|
||||||
|
|
||||||
inline size_t dynbuf_capacity(const Dynbuf *v) { return v->capacity; }
|
inline size_t dynbuf_capacity(const Dynbuf *v) {
|
||||||
|
return v->capacity;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@
|
||||||
* Holds homogenous data. The underlying array will expand automatically.
|
* Holds homogenous data. The underlying array will expand automatically.
|
||||||
*/
|
*/
|
||||||
typedef struct {
|
typedef struct {
|
||||||
void *data;
|
void *data;
|
||||||
size_t elem_size;
|
size_t elem_size;
|
||||||
size_t length;
|
size_t length;
|
||||||
size_t capacity;
|
size_t capacity;
|
||||||
|
|
|
||||||
5
fibmat.c
5
fibmat.c
|
|
@ -1,5 +1,5 @@
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Computes the n-th Fibonacci number using matrix exponentiation.
|
* @brief Computes the n-th Fibonacci number using matrix exponentiation.
|
||||||
|
|
@ -48,7 +48,8 @@ static FibMatrix matrix_power(FibMatrix base, uint32_t n) {
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t fibonacci_matrix(uint32_t n) {
|
uint64_t fibonacci_matrix(uint32_t n) {
|
||||||
if (n == 0) return 0;
|
if (n == 0)
|
||||||
|
return 0;
|
||||||
FibMatrix base = {1, 1, 1, 0};
|
FibMatrix base = {1, 1, 1, 0};
|
||||||
FibMatrix result = matrix_power(base, n - 1);
|
FibMatrix result = matrix_power(base, n - 1);
|
||||||
return result.a;
|
return result.a;
|
||||||
|
|
|
||||||
24
gba_header.c
24
gba_header.c
|
|
@ -5,18 +5,18 @@
|
||||||
|
|
||||||
// Structure to hold GBA ROM header data
|
// Structure to hold GBA ROM header data
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint32_t entry_point; // 0x08000000 - Entry Point
|
uint32_t entry_point; // 0x08000000 - Entry Point
|
||||||
uint8_t nintendo_logo[156]; // 0x08000004 - Nintendo Logo
|
uint8_t nintendo_logo[156]; // 0x08000004 - Nintendo Logo
|
||||||
char game_title[12]; // 0x080000A0 - Game Title
|
char game_title[12]; // 0x080000A0 - Game Title
|
||||||
char game_code[4]; // 0x080000AC - Game Code (e.g., "AGB-XXXX")
|
char game_code[4]; // 0x080000AC - Game Code (e.g., "AGB-XXXX")
|
||||||
char maker_code[2]; // 0x080000B0 - Maker Code
|
char maker_code[2]; // 0x080000B0 - Maker Code
|
||||||
uint8_t fixed_value; // 0x080000B2 - Always 0x96
|
uint8_t fixed_value; // 0x080000B2 - Always 0x96
|
||||||
uint8_t main_unit_code; // 0x080000B3 - Usually 0x00
|
uint8_t main_unit_code; // 0x080000B3 - Usually 0x00
|
||||||
uint8_t device_type; // 0x080000B4 - Usually 0x00
|
uint8_t device_type; // 0x080000B4 - Usually 0x00
|
||||||
uint8_t reserved1[7]; // 0x080000B5 - Reserved
|
uint8_t reserved1[7]; // 0x080000B5 - Reserved
|
||||||
uint8_t software_version; // 0x080000BC - Software Version
|
uint8_t software_version; // 0x080000BC - Software Version
|
||||||
uint8_t complement_check; // 0x080000BD - Header Checksum
|
uint8_t complement_check; // 0x080000BD - Header Checksum
|
||||||
uint8_t reserved2[2]; // 0x080000BE - Reserved
|
uint8_t reserved2[2]; // 0x080000BE - Reserved
|
||||||
} GBAHeader;
|
} GBAHeader;
|
||||||
|
|
||||||
// Function to read and display the ROM header
|
// Function to read and display the ROM header
|
||||||
|
|
|
||||||
|
|
@ -3,19 +3,19 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
typedef struct hashmap_entry {
|
typedef struct hashmap_entry {
|
||||||
char *key;
|
char *key;
|
||||||
void *value;
|
void *value;
|
||||||
struct hashmap_entry *next;
|
struct hashmap_entry *next;
|
||||||
} hashmap_entry_t;
|
} hashmap_entry_t;
|
||||||
|
|
||||||
struct hashmap {
|
struct hashmap {
|
||||||
hashmap_entry_t **buckets;
|
hashmap_entry_t **buckets;
|
||||||
size_t bucket_count;
|
size_t bucket_count;
|
||||||
};
|
};
|
||||||
|
|
||||||
static unsigned long hash(const char *str) {
|
static unsigned long hash(const char *str) {
|
||||||
unsigned long hash = 5381;
|
unsigned long hash = 5381;
|
||||||
int c;
|
int c;
|
||||||
while ((c = *str++)) hash = ((hash << 5) + hash) + c;
|
while ((c = *str++)) hash = ((hash << 5) + hash) + c;
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
|
|
@ -43,7 +43,7 @@ void hashmap_destroy(hashmap_t *map) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void hashmap_put(hashmap_t *map, const char *key, void *value) {
|
void hashmap_put(hashmap_t *map, const char *key, void *value) {
|
||||||
size_t index = (hash(key) % map->bucket_count);
|
size_t index = (hash(key) % map->bucket_count);
|
||||||
hashmap_entry_t *entry = map->buckets[index];
|
hashmap_entry_t *entry = map->buckets[index];
|
||||||
|
|
||||||
while (entry) {
|
while (entry) {
|
||||||
|
|
@ -63,7 +63,7 @@ void hashmap_put(hashmap_t *map, const char *key, void *value) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void *hashmap_get(hashmap_t *map, const char *key) {
|
void *hashmap_get(hashmap_t *map, const char *key) {
|
||||||
size_t index = (hash(key) % map->bucket_count);
|
size_t index = (hash(key) % map->bucket_count);
|
||||||
hashmap_entry_t *entry = map->buckets[index];
|
hashmap_entry_t *entry = map->buckets[index];
|
||||||
|
|
||||||
while (entry) {
|
while (entry) {
|
||||||
|
|
@ -81,7 +81,7 @@ void hashmap_remove(hashmap_t *map, const char *key) {
|
||||||
size_t index = (hash(key) % map->bucket_count);
|
size_t index = (hash(key) % map->bucket_count);
|
||||||
|
|
||||||
hashmap_entry_t **prev = &map->buckets[index];
|
hashmap_entry_t **prev = &map->buckets[index];
|
||||||
hashmap_entry_t *entry = *prev;
|
hashmap_entry_t *entry = *prev;
|
||||||
|
|
||||||
while (entry) {
|
while (entry) {
|
||||||
if (strcmp(entry->key, key) == 0) {
|
if (strcmp(entry->key, key) == 0) {
|
||||||
|
|
|
||||||
|
|
@ -246,8 +246,7 @@ float mat3_det(const Mat3 *m) {
|
||||||
float m21 = MAT3_AT(m, 2, 1);
|
float m21 = MAT3_AT(m, 2, 1);
|
||||||
float m22 = MAT3_AT(m, 2, 2);
|
float m22 = MAT3_AT(m, 2, 2);
|
||||||
|
|
||||||
return m00 * (m11 * m22 - m12 * m21) - m01 * (m10 * m22 - m12 * m20) +
|
return m00 * (m11 * m22 - m12 * m21) - m01 * (m10 * m22 - m12 * m20) + m02 * (m10 * m21 - m11 * m20);
|
||||||
m02 * (m10 * m21 - m11 * m20);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inline float vec3_dot(const Vec3 *a, const Vec3 *b) {
|
inline float vec3_dot(const Vec3 *a, const Vec3 *b) {
|
||||||
|
|
@ -255,9 +254,7 @@ inline float vec3_dot(const Vec3 *a, const Vec3 *b) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Vec3 vec3_cross(const Vec3 *a, const Vec3 *b) {
|
Vec3 vec3_cross(const Vec3 *a, const Vec3 *b) {
|
||||||
Vec3 res = {.x = a->y * b->z - a->z * b->y,
|
Vec3 res = {.x = a->y * b->z - a->z * b->y, .y = a->x * b->z - a->z * b->x, .z = a->x * b->y - a->y * b->x};
|
||||||
.y = a->x * b->z - a->z * b->x,
|
|
||||||
.z = a->x * b->y - a->y * b->x};
|
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
@ -325,14 +322,10 @@ Vec2 vec2_scale(const Vec2 *a, const float scalar) {
|
||||||
|
|
||||||
Mat2 mat2_mul(const Mat2 *m1, const Mat2 *m2) {
|
Mat2 mat2_mul(const Mat2 *m1, const Mat2 *m2) {
|
||||||
Mat2 m3 = {.arr = {
|
Mat2 m3 = {.arr = {
|
||||||
MAT2_AT(m1, 0, 0) * MAT2_AT(m2, 0, 0) +
|
MAT2_AT(m1, 0, 0) * MAT2_AT(m2, 0, 0) + MAT2_AT(m1, 0, 1) * MAT2_AT(m2, 1, 0),
|
||||||
MAT2_AT(m1, 0, 1) * MAT2_AT(m2, 1, 0),
|
MAT2_AT(m1, 1, 0) * MAT2_AT(m2, 0, 0) + MAT2_AT(m1, 1, 1) * MAT2_AT(m2, 1, 0),
|
||||||
MAT2_AT(m1, 1, 0) * MAT2_AT(m2, 0, 0) +
|
MAT2_AT(m1, 0, 0) * MAT2_AT(m2, 0, 1) + MAT2_AT(m1, 0, 1) * MAT2_AT(m2, 1, 1),
|
||||||
MAT2_AT(m1, 1, 1) * MAT2_AT(m2, 1, 0),
|
MAT2_AT(m1, 1, 0) * MAT2_AT(m2, 0, 1) + MAT2_AT(m1, 1, 1) * MAT2_AT(m2, 1, 1),
|
||||||
MAT2_AT(m1, 0, 0) * MAT2_AT(m2, 0, 1) +
|
|
||||||
MAT2_AT(m1, 0, 1) * MAT2_AT(m2, 1, 1),
|
|
||||||
MAT2_AT(m1, 1, 0) * MAT2_AT(m2, 0, 1) +
|
|
||||||
MAT2_AT(m1, 1, 1) * MAT2_AT(m2, 1, 1),
|
|
||||||
}};
|
}};
|
||||||
|
|
||||||
return m3;
|
return m3;
|
||||||
|
|
@ -385,8 +378,7 @@ bool vec2_approx_eq(const Vec2 *a, const Vec2 *b, float epsilon) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool vec3_approx_eq(const Vec3 *a, const Vec3 *b, float epsilon) {
|
bool vec3_approx_eq(const Vec3 *a, const Vec3 *b, float epsilon) {
|
||||||
return (fabsf(a->x - b->x) <= epsilon) && (fabsf(a->y - b->y) <= epsilon) &&
|
return (fabsf(a->x - b->x) <= epsilon) && (fabsf(a->y - b->y) <= epsilon) && (fabsf(a->z - b->z) <= epsilon);
|
||||||
(fabsf(a->z - b->z) <= epsilon);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Mat2 mat2_adj(const Mat2 *m) {
|
Mat2 mat2_adj(const Mat2 *m) {
|
||||||
|
|
@ -438,7 +430,7 @@ int main(void) {
|
||||||
assert(mat3_approx_eq(&m3, &m, 0.01));
|
assert(mat3_approx_eq(&m3, &m, 0.01));
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
Mat3 m = {{1, 0, 0, 0, 1, 0, 0, 0, 1}};
|
Mat3 m = {{1, 0, 0, 0, 1, 0, 0, 0, 1}};
|
||||||
float d = mat3_det(&m);
|
float d = mat3_det(&m);
|
||||||
assert(fabs(d - 1.0) <= 0.01);
|
assert(fabs(d - 1.0) <= 0.01);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,19 @@
|
||||||
#include <lua.h>
|
|
||||||
#include <lauxlib.h>
|
#include <lauxlib.h>
|
||||||
|
#include <lua.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
uint32_t lookup3(const void *key, size_t length, uint32_t initval);
|
uint32_t lookup3(const void *key, size_t length, uint32_t initval);
|
||||||
|
|
||||||
// Lua wrapper
|
// Lua wrapper
|
||||||
static int l_lookup3(lua_State *L) {
|
static int l_lookup3(lua_State *L) {
|
||||||
size_t len;
|
size_t len;
|
||||||
const char *str = luaL_checklstring(L, 1, &len); // get string + length
|
const char *str = luaL_checklstring(L, 1, &len); // get string + length
|
||||||
uint32_t initval = (uint32_t)luaL_checkinteger(L, 2); // get initval
|
uint32_t initval = (uint32_t)luaL_checkinteger(L, 2); // get initval
|
||||||
|
|
||||||
uint32_t hash = lookup3(str, len, initval);
|
uint32_t hash = lookup3(str, len, initval);
|
||||||
|
|
||||||
lua_pushinteger(L, hash); // return the hash
|
lua_pushinteger(L, hash); // return the hash
|
||||||
return 1; // number of return values
|
return 1; // number of return values
|
||||||
}
|
}
|
||||||
|
|
||||||
// Module open function
|
// Module open function
|
||||||
|
|
@ -23,5 +23,5 @@ int luaopen_lookup3(lua_State *L) {
|
||||||
lua_pushcfunction(L, l_lookup3);
|
lua_pushcfunction(L, l_lookup3);
|
||||||
lua_setfield(L, -2, "lookup3");
|
lua_setfield(L, -2, "lookup3");
|
||||||
|
|
||||||
return 1; // return the table
|
return 1; // return the table
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,13 +3,13 @@
|
||||||
// Macro for register access (volatile pointer dereferencing, not used here)
|
// Macro for register access (volatile pointer dereferencing, not used here)
|
||||||
#define REG32(addr) (*(volatile uint32_t *)(addr))
|
#define REG32(addr) (*(volatile uint32_t *)(addr))
|
||||||
#define REG16(addr) (*(volatile uint16_t *)(addr))
|
#define REG16(addr) (*(volatile uint16_t *)(addr))
|
||||||
#define REG8(addr) (*(volatile uint8_t *)(addr))
|
#define REG8(addr) (*(volatile uint8_t *)(addr))
|
||||||
|
|
||||||
typedef struct MMIO_device {
|
typedef struct MMIO_device {
|
||||||
volatile uint16_t hello;
|
volatile uint16_t hello;
|
||||||
volatile uint8_t __RESERVED1[2];
|
volatile uint8_t __RESERVED1[2];
|
||||||
volatile uint16_t status;
|
volatile uint16_t status;
|
||||||
volatile uint8_t __RESERVED2[8];
|
volatile uint8_t __RESERVED2[8];
|
||||||
volatile uint32_t control;
|
volatile uint32_t control;
|
||||||
// ...and so on
|
// ...and so on
|
||||||
} __attribute__((aligned(4))) MMIO_device;
|
} __attribute__((aligned(4))) MMIO_device;
|
||||||
|
|
@ -20,8 +20,8 @@ typedef struct MMIO_device {
|
||||||
/* Specific device, often defined as an offset from a base */
|
/* Specific device, often defined as an offset from a base */
|
||||||
#define MY_MMIO (MEM_PERIPH_BASE + 0x10)
|
#define MY_MMIO (MEM_PERIPH_BASE + 0x10)
|
||||||
|
|
||||||
#define DVC_CTLR_ENA (0x3 << 1) /* Device Control Register Enable */
|
#define DVC_CTLR_ENA (0x3 << 1) /* Device Control Register Enable */
|
||||||
#define DVC_CTLR_WRITE (0x1) /* Device Control Register Write */
|
#define DVC_CTLR_WRITE (0x1) /* Device Control Register Write */
|
||||||
|
|
||||||
volatile MMIO_device *mmio1 = (volatile MMIO_device *)MY_MMIO;
|
volatile MMIO_device *mmio1 = (volatile MMIO_device *)MY_MMIO;
|
||||||
|
|
||||||
|
|
|
||||||
24
murmur3.c
24
murmur3.c
|
|
@ -1,15 +1,15 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
uint32_t murmur3_32(const uint8_t* key, size_t len, uint32_t seed) {
|
uint32_t murmur3_32(const uint8_t *key, size_t len, uint32_t seed) {
|
||||||
uint32_t h = seed;
|
uint32_t h = seed;
|
||||||
uint32_t c1 = 0xcc9e2d51;
|
uint32_t c1 = 0xcc9e2d51;
|
||||||
uint32_t c2 = 0x1b873593;
|
uint32_t c2 = 0x1b873593;
|
||||||
uint32_t k;
|
uint32_t k;
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
for (i = 0; i + 4 <= len; i += 4) {
|
for (i = 0; i + 4 <= len; i += 4) {
|
||||||
k = *(uint32_t*)(key + i);
|
k = *(uint32_t *)(key + i);
|
||||||
k *= c1;
|
k *= c1;
|
||||||
k = (k << 15) | (k >> (32 - 15));
|
k = (k << 15) | (k >> (32 - 15));
|
||||||
k *= c2;
|
k *= c2;
|
||||||
|
|
@ -22,12 +22,16 @@ uint32_t murmur3_32(const uint8_t* key, size_t len, uint32_t seed) {
|
||||||
// Tail handling
|
// Tail handling
|
||||||
k = 0;
|
k = 0;
|
||||||
switch (len & 3) {
|
switch (len & 3) {
|
||||||
case 3: k ^= key[i + 2] << 16;
|
case 3: k ^= key[i + 2] << 16;
|
||||||
/* fall through */
|
/* fall through */
|
||||||
case 2: k ^= key[i + 1] << 8;
|
case 2: k ^= key[i + 1] << 8;
|
||||||
/* fall through */
|
/* fall through */
|
||||||
case 1: k ^= key[i + 0];
|
case 1:
|
||||||
k *= c1; k = (k << 15) | (k >> (32 - 15)); k *= c2; h ^= k;
|
k ^= key[i + 0];
|
||||||
|
k *= c1;
|
||||||
|
k = (k << 15) | (k >> (32 - 15));
|
||||||
|
k *= c2;
|
||||||
|
h ^= k;
|
||||||
}
|
}
|
||||||
|
|
||||||
h ^= len;
|
h ^= len;
|
||||||
|
|
@ -42,7 +46,7 @@ uint32_t murmur3_32(const uint8_t* key, size_t len, uint32_t seed) {
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
const char *key = "hello world";
|
const char *key = "hello world";
|
||||||
uint32_t hash = murmur3_32((const uint8_t *)key, 11, 42); // seed = 42
|
uint32_t hash = murmur3_32((const uint8_t *)key, 11, 42); // seed = 42
|
||||||
printf("MurmurHash3 of \"%s\" is 0x%X\n", key, hash);
|
printf("MurmurHash3 of \"%s\" is 0x%X\n", key, hash);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
9
prand.c
9
prand.c
|
|
@ -3,9 +3,8 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#define BUILD_SEED \
|
#define BUILD_SEED \
|
||||||
((uint64_t)(__TIME__[0]) * (uint64_t)(__TIME__[1]) * \
|
((uint64_t)(__TIME__[0]) * (uint64_t)(__TIME__[1]) * (uint64_t)(__TIME__[3]) * (uint64_t)(__TIME__[4]) * \
|
||||||
(uint64_t)(__TIME__[3]) * (uint64_t)(__TIME__[4]) * \
|
|
||||||
(uint64_t)(__TIME__[6]) * (uint64_t)(__TIME__[7]))
|
(uint64_t)(__TIME__[6]) * (uint64_t)(__TIME__[7]))
|
||||||
|
|
||||||
#define PRNG_SAVE_INTERVAL 50 // Save every 1000 calls to prand()
|
#define PRNG_SAVE_INTERVAL 50 // Save every 1000 calls to prand()
|
||||||
|
|
@ -29,7 +28,9 @@ void sprand(uint64_t s) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void rand_reseed() { seed = BUILD_SEED; }
|
void rand_reseed() {
|
||||||
|
seed = BUILD_SEED;
|
||||||
|
}
|
||||||
|
|
||||||
#define PRAND_MAIN
|
#define PRAND_MAIN
|
||||||
#ifdef PRAND_MAIN
|
#ifdef PRAND_MAIN
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
struct timespec ts = {.tv_nsec = 30, .tv_sec = 0};
|
struct timespec ts = {.tv_nsec = 30, .tv_sec = 0};
|
||||||
volatile int dummy = 0;
|
volatile int dummy = 0;
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
struct timespec start, end;
|
struct timespec start, end;
|
||||||
|
|
|
||||||
4
sin.c
4
sin.c
|
|
@ -11,7 +11,9 @@ double factorial(int n) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
double abs_double(double x) { return x < 0 ? -x : x; }
|
double abs_double(double x) {
|
||||||
|
return x < 0 ? -x : x;
|
||||||
|
}
|
||||||
|
|
||||||
// SICP-style iterative approximation for sin(x)
|
// SICP-style iterative approximation for sin(x)
|
||||||
double sin_iter(double x) {
|
double sin_iter(double x) {
|
||||||
|
|
|
||||||
8
sock.c
8
sock.c
|
|
@ -29,8 +29,7 @@ int main(void) {
|
||||||
};
|
};
|
||||||
|
|
||||||
// Bind socket
|
// Bind socket
|
||||||
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) <
|
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
|
||||||
0) {
|
|
||||||
perror("bind failed");
|
perror("bind failed");
|
||||||
close(server_fd);
|
close(server_fd);
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
|
|
@ -47,9 +46,8 @@ int main(void) {
|
||||||
|
|
||||||
// Accept one client connection
|
// Accept one client connection
|
||||||
struct sockaddr_in client_addr;
|
struct sockaddr_in client_addr;
|
||||||
socklen_t client_len = sizeof(client_addr);
|
socklen_t client_len = sizeof(client_addr);
|
||||||
int client_fd =
|
int client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_len);
|
||||||
accept(server_fd, (struct sockaddr *)&client_addr, &client_len);
|
|
||||||
if (client_fd < 0) {
|
if (client_fd < 0) {
|
||||||
perror("accept failed");
|
perror("accept failed");
|
||||||
close(server_fd);
|
close(server_fd);
|
||||||
|
|
|
||||||
|
|
@ -8,9 +8,9 @@
|
||||||
#define SOCKET_PATH "/tmp/demosocket"
|
#define SOCKET_PATH "/tmp/demosocket"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int sock_fd;
|
int sock_fd;
|
||||||
struct sockaddr_un addr;
|
struct sockaddr_un addr;
|
||||||
char buffer[100];
|
char buffer[100];
|
||||||
|
|
||||||
sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
||||||
if (sock_fd < 0) {
|
if (sock_fd < 0) {
|
||||||
|
|
@ -22,8 +22,7 @@ int main() {
|
||||||
addr.sun_family = AF_UNIX;
|
addr.sun_family = AF_UNIX;
|
||||||
strncpy(addr.sun_path, SOCKET_PATH, sizeof(addr.sun_path) - 1);
|
strncpy(addr.sun_path, SOCKET_PATH, sizeof(addr.sun_path) - 1);
|
||||||
|
|
||||||
if (connect(sock_fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) <
|
if (connect(sock_fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) {
|
||||||
0) {
|
|
||||||
perror("connect");
|
perror("connect");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,13 +7,13 @@
|
||||||
|
|
||||||
/* Unix sockets need a path */
|
/* Unix sockets need a path */
|
||||||
#define SOCK_PATH "/tmp/demosocket"
|
#define SOCK_PATH "/tmp/demosocket"
|
||||||
#define BUF_SIZE 128
|
#define BUF_SIZE 128
|
||||||
|
|
||||||
char buf[BUF_SIZE];
|
char buf[BUF_SIZE];
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
struct sockaddr_un addr;
|
struct sockaddr_un addr;
|
||||||
int server_fd, client_fd;
|
int server_fd, client_fd;
|
||||||
|
|
||||||
// Unlink this in case its still around
|
// Unlink this in case its still around
|
||||||
unlink(SOCK_PATH);
|
unlink(SOCK_PATH);
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,17 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/un.h>
|
#include <sys/un.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#define SOCKET_PATH "/tmp/demosocket"
|
#define SOCKET_PATH "/tmp/demosocket"
|
||||||
#define BUF_SIZE 128
|
#define BUF_SIZE 128
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
int server_fd, client_fd;
|
int server_fd, client_fd;
|
||||||
struct sockaddr_un addr;
|
struct sockaddr_un addr;
|
||||||
char buf[BUF_SIZE];
|
char buf[BUF_SIZE];
|
||||||
|
|
||||||
// Clean up any leftover socket file
|
// Clean up any leftover socket file
|
||||||
unlink(SOCKET_PATH);
|
unlink(SOCKET_PATH);
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@
|
||||||
int main(void) {
|
int main(void) {
|
||||||
printf("%s\n", sqlite3_libversion());
|
printf("%s\n", sqlite3_libversion());
|
||||||
|
|
||||||
sqlite3 *db;
|
sqlite3 *db;
|
||||||
sqlite3_stmt *res;
|
sqlite3_stmt *res;
|
||||||
|
|
||||||
int rc = sqlite3_open(":memory:", &db);
|
int rc = sqlite3_open(":memory:", &db);
|
||||||
|
|
|
||||||
2
time.c
2
time.c
|
|
@ -3,7 +3,7 @@
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
char *out = malloc(200);
|
char *out = malloc(200);
|
||||||
struct tm *tmp;
|
struct tm *tmp;
|
||||||
|
|
||||||
time_t t = time(NULL);
|
time_t t = time(NULL);
|
||||||
|
|
|
||||||
|
|
@ -3,14 +3,14 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
struct treeset_node {
|
struct treeset_node {
|
||||||
void *data;
|
void *data;
|
||||||
struct treeset_node *left;
|
struct treeset_node *left;
|
||||||
struct treeset_node *right;
|
struct treeset_node *right;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct treeset {
|
struct treeset {
|
||||||
treeset_node_t *root;
|
treeset_node_t *root;
|
||||||
size_t node_count;
|
size_t node_count;
|
||||||
};
|
};
|
||||||
|
|
||||||
void ts_node_destroy(treeset_node_t *n) {
|
void ts_node_destroy(treeset_node_t *n) {
|
||||||
|
|
@ -22,4 +22,6 @@ void ts_node_destroy(treeset_node_t *n) {
|
||||||
free(n);
|
free(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
void treeset_destroy(treeset_t *set) { ts_node_destroy(set->root); }
|
void treeset_destroy(treeset_t *set) {
|
||||||
|
ts_node_destroy(set->root);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
typedef struct treeset treeset_t;
|
typedef struct treeset treeset_t;
|
||||||
typedef struct treeset_node treeset_node_t;
|
typedef struct treeset_node treeset_node_t;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue