Files
sites/fsd.vxserver.dev/include/snippet.lua
2025-11-29 08:35:09 -05:00

78 lines
1.3 KiB
Lua

local M = {}
local snippet = {
[[
#include <stdio.h>
int main()
{
long x = 470020878965;
puts((void*)&x); // le representation
return 0;
}
]],
[[
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
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)++;
double d = x - *mean;
*mean += d / *n;
*m2 += d * (x - *mean);
}
]],
[[
#define ever (;;)
for ever {
...
}
]],
[[
void *xmalloc(size_t size)
{
void *ret = malloc(size);
if (!ret)
die("memory exhausted");
return ret;
}
]],
}
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]
end
return M