This commit is contained in:
2025-12-02 21:10:34 -05:00
parent 12c4d3c46e
commit 76eb4809e2
15 changed files with 327 additions and 103 deletions

View File

@@ -1,42 +1,38 @@
local M = {}
local snippet = {
[[
[[
#include <stdio.h>
int main()
int main(void)
{
long x = 470020878965;
puts((void*)&x); // le representation
puts((void *)&x);
return 0;
}
]],
[[
float Q_rsqrt( float number )
[[
float Q_rsqrt(float number)
{
long i;
float x2, y;
const float threehalfs = 1.5F;
x2 = number * 0.5F;
y = number;
i = * ( long * ) &y; // evil floating point bit level hacking
i = 0x5f3759df - ( i >> 1 ); // what the fuck?
y = * ( float * ) &i;
y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration
// y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed
y = number;
i = *(long *)&y;
i = 0x5f3759df - (i >> 1);
y = *(float *)&i;
y = y * (threehalfs - (x2 * y * y));
return y;
}
]],
[[
[[
#include <stdio.h>
int main(int argc, char **argv)
{
return printf("%s: Comma operator\n", argv[0]), 2;
}
]],
[[
[[
void welford(double x, double *n, double *mean, double *m2)
{
(*n)++;
@@ -45,33 +41,128 @@ void welford(double x, double *n, double *mean, double *m2)
*m2 += d * (x - *mean);
}
]],
[[
[[
#define ever (;;)
for ever {
...
;
}
]],
[[
[[
void *xmalloc(size_t size)
{
void *ret = malloc(size);
if (!ret)
die("memory exhausted");
return ret;
void *r = malloc(size);
if (!r)
abort();
return r;
}
]],
[[
#include <stdint.h>
uint32_t rotl32(uint32_t x, uint32_t n)
{
return (x << n) | (x >> (32 - n));
}
]],
[[
#include <stddef.h>
void memzero(void *p, size_t n)
{
unsigned char *t = p;
while (n--)
*t++ = 0;
}
]],
[[
#include <stdio.h>
int main(void)
{
int a = 0;
if (a++ == 0 && a++ == 1 && a++ == 2)
printf("chain ok\n");
return 0;
}
]],
[[
static unsigned long next = 1;
int lrand(void)
{
next = next * 1103515245 + 12345;
return (unsigned)(next / 65536) & 32767;
}
]],
[[
#!/bin/sh
set -eu
printf '%s\n' "$1"
]],
[[
#!/bin/sh
if [ "$#" -eq 0 ]; then
exit 1
fi
printf '%s\n' "$@"
]],
[[
#!/bin/sh
x=0
while :; do
x=$((x + 1))
[ "$x" -gt 3 ] && break
done
printf '%s\n' "$x"
]],
[[
#!/bin/sh
exec 3>&1
printf 'ok\n' >&3
]],
[[
#!/bin/sh
f() {
printf '%s\n' "$1"
}
f "${1:-none}"
]],
[[
#!/bin/sh
for i in a b c; do
printf '%s\n' "$i"
done
]],
[[
#!/bin/sh
x=$(printf '%s' foo)
printf '%s\n' "$x"
]],
[[
#!/bin/sh
read x
printf '%s\n' "$x"
]],
[[
#!/bin/sh
PATH=/bin:/usr/bin
export PATH
printf '%s\n' "$PATH"
]],
[[
#!/bin/sh
trap 'printf trap\n' HUP INT TERM
sleep 1
]]
}
M.daily_random = function()
local day = tonumber(os.date("%d"))
local n = #snippet
local a = 1664525
local c = 1013904223
local m = 2^32
local v = (a * day + c) % m
local r = v / m
return snippet[math.floor(r * n) + 1]
local t = os.date("*t")
local seed = t.day + t.month * 31 + t.year * 997
local a = 1103515245
local c = 12345
local m = 2 ^ 31
seed = (a * seed + c) % m
local r = seed / m
local n = #snippet
return snippet[math.floor(r * n) + 1]
end
return M