Compare commits

..

4 commits

Author SHA1 Message Date
Imbus
b07255ab1d Ignore .so 2025-08-18 11:23:08 +02:00
Imbus
c40c698ece Lua loadable c library example (.so) 2025-08-18 11:23:04 +02:00
Imbus
d089e583cd Shortcase on single line 2025-08-18 11:19:46 +02:00
Imbus
e0ea973df4 Lookup3 2025-08-18 11:19:37 +02:00
6 changed files with 115 additions and 0 deletions

View file

@ -15,3 +15,4 @@ AlignConsecutiveDeclarations:
AlignFunctionPointers: false AlignFunctionPointers: false
PadOperators: false PadOperators: false
AlignConsecutiveMacros: true AlignConsecutiveMacros: true
AllowShortCaseLabelsOnASingleLine: true

1
.gitignore vendored
View file

@ -1,4 +1,5 @@
*.o *.o
*.so
.cache .cache
.json .json
*.elf *.elf

63
lookup3.c Normal file
View file

@ -0,0 +1,63 @@
#include <stdint.h>
#include <stdlib.h>
#define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
// clang-format off
#define mix(a,b,c) \
{ \
a -= c; a ^= rot(c, 4); c += b; \
b -= a; b ^= rot(a, 6); a += c; \
c -= b; c ^= rot(b, 8); b += a; \
a -= c; a ^= rot(c,16); c += b; \
b -= a; b ^= rot(a,19); a += c; \
c -= b; c ^= rot(b, 4); b += a; \
}
#define final(a,b,c) \
{ \
c ^= b; c -= rot(b,14); \
a ^= c; a -= rot(c,11); \
b ^= a; b -= rot(a,25); \
c ^= b; c -= rot(b,16); \
a ^= c; a -= rot(c,4); \
b ^= a; b -= rot(a,14); \
c ^= b; c -= rot(b,24); \
}
// clang-format on
uint32_t lookup3(const void *key, size_t length, uint32_t initval) {
uint32_t a, b, c;
const uint8_t *k;
const uint32_t *data32Bit;
data32Bit = key;
a = b = c = 0xdeadbeef + (((uint32_t)length) << 2) + initval;
while (length > 12) {
a += *(data32Bit++);
b += *(data32Bit++);
c += *(data32Bit++);
mix(a, b, c);
length -= 12;
}
k = (const uint8_t *)data32Bit;
switch (length) {
case 12: c += ((uint32_t)k[11]) << 24;
case 11: c += ((uint32_t)k[10]) << 16;
case 10: c += ((uint32_t)k[9]) << 8;
case 9: c += k[8];
case 8: b += ((uint32_t)k[7]) << 24;
case 7: b += ((uint32_t)k[6]) << 16;
case 6: b += ((uint32_t)k[5]) << 8;
case 5: b += k[4];
case 4: a += ((uint32_t)k[3]) << 24;
case 3: a += ((uint32_t)k[2]) << 16;
case 2: a += ((uint32_t)k[1]) << 8;
case 1: a += k[0]; break;
case 0: return c;
}
final(a, b, c);
return c;
}

21
lua_lib/Makefile Normal file
View file

@ -0,0 +1,21 @@
CC ?= gcc
CFLAGS ?= -Wall -O2 -fPIC
LDFLAGS ?=
TARGET = lookup3.so
SRC = lua_lookup3.c
OBJS = lua_lookup3.o lookup3.o
all: $(TARGET)
$(TARGET): $(OBJS)
$(CC) -shared -o $@ $^ $(LDFLAGS)
lua_lookup3.o: $(SRC)
$(CC) $(CFLAGS) -c -o $@ $<
lookup3.o: ../lookup3.c
$(CC) $(CFLAGS) -c -o $@ $<
clean:
rm -f $(TARGET) $(OBJS)

2
lua_lib/driver.lua Normal file
View file

@ -0,0 +1,2 @@
local myhash = require "lookup3"
print(myhash.lookup3("hello", 0) % 100)

27
lua_lib/lua_lookup3.c Normal file
View file

@ -0,0 +1,27 @@
#include <lua.h>
#include <lauxlib.h>
#include <stdint.h>
uint32_t lookup3(const void *key, size_t length, uint32_t initval);
// Lua wrapper
static int l_lookup3(lua_State *L) {
size_t len;
const char *str = luaL_checklstring(L, 1, &len); // get string + length
uint32_t initval = (uint32_t)luaL_checkinteger(L, 2); // get initval
uint32_t hash = lookup3(str, len, initval);
lua_pushinteger(L, hash); // return the hash
return 1; // number of return values
}
// Module open function
int luaopen_lookup3(lua_State *L) {
lua_newtable(L);
lua_pushcfunction(L, l_lookup3);
lua_setfield(L, -2, "lookup3");
return 1; // return the table
}