Skip to content

Instantly share code, notes, and snippets.

@kdrnic
kdrnic / hash.c
Created April 30, 2019 00:37
hashtable implementation in C uses open addressing, linear probing also includes a FNV1/DJB2 hashing comparison
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <time.h>
#include <assert.h>
#include <string.h>
#include <limits.h>
#include <inttypes.h>
uint32_t fnv1(const char *str)
@kdrnic
kdrnic / hash.c
Created April 30, 2019 00:37
hashtable implementation in C uses open addressing, linear probing also includes a FNV1/DJB2 hashing comparison
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <time.h>
#include <assert.h>
#include <string.h>
#include <limits.h>
#include <inttypes.h>
uint32_t fnv1(const char *str)
@kdrnic
kdrnic / hash.c
Created April 30, 2019 00:36
hashtable implementation in C uses open addressing, linear probing also includes a FNV1/DJB2 hashing comparison
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <time.h>
#include <assert.h>
#include <string.h>
#include <limits.h>
#include <inttypes.h>
uint32_t fnv1(const char *str)
uint64_t rdtsc()
{
uint64_t ret;
asm volatile ("rdtsc" : "=A"(ret));
return ret;
}
@kdrnic
kdrnic / millerrabin.c
Created April 29, 2019 15:52
implement and verify the Miller-Rabin primality test
#include <stdio.h>
#include <stdlib.h>
int modular_pow(int base, int exponent, int modulus)
{
if(modulus == 1) return 0;
int result = 1;
base %= modulus;
while(exponent > 0){
if(exponent & 1){
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <time.h>
#include "arr.h"
arr_int func(void)
{
@kdrnic
kdrnic / arr.h
Last active April 29, 2019 07:12
A GNU C dynamic array implementation, inspired by Javascript arrays, and desired to be safe if possible
#ifndef ARR_H
#define ARR_H
#ifndef assert
#define assert()
#endif
#define arr_alloc malloc
#define arr_calloc(a,b) arr_alloc((a) * (b))
int mesh_to_obbs(const mesh *me, struct vec3 (*boxes)[5], int *set_)
{
int i, j, k, l;
int *set = set_;
int nset = 0;
if(!set){
set = calloc(me->num_subs, sizeof(*set));
memset(set, 0, me->num_subs * sizeof(*set));
}
float time_s_max = -FLT_MAX, time_e_min = FLT_MAX;
//[...]
for(i = 0; i < 4 + 3 * tri->num_verts; i++){
float box_min, box_max, tri_min, tri_max;
//contact interval: time of first and last contact
float time_s, time_e;
//speed of box on axis
float dot = vec3_dot(axes[i], move);
@kdrnic
kdrnic / raycast.c
Last active February 4, 2019 23:11
typedef struct vec2
{
fixed x, y;
} vec2;
typedef struct s_rayquery
{
BITMAP *map;
vec2 start;
vec2 end;