Skip to content

Instantly share code, notes, and snippets.

@WheretIB
Created September 22, 2019 08:31
Show Gist options
  • Save WheretIB/ed326c59375de76515383553e5a9f30d to your computer and use it in GitHub Desktop.
Save WheretIB/ed326c59375de76515383553e5a9f30d to your computer and use it in GitHub Desktop.
nullc tests
import std.io;
import std.math;
import std.time;
double PI = 3.141592653589793;
double SOLAR_MASS = 4 * PI * PI;
double DAYS_PER_YEAR = 365.24;
class Body {
double x, y, z, vx, vy, vz, mass;
void Body(double x, y, z, vx, vy, vz, mass)
{
this.x = x;
this.y = y;
this.z = z;
this.vx = vx;
this.vy = vy;
this.vz = vz;
this.mass = mass;
}
}
Body ref Jupiter(){
return new Body(
4.84143144246472090e+00,
-1.16032004402742839e+00,
-1.03622044471123109e-01,
1.66007664274403694e-03 * DAYS_PER_YEAR,
7.69901118419740425e-03 * DAYS_PER_YEAR,
-6.90460016972063023e-05 * DAYS_PER_YEAR,
9.54791938424326609e-04 * SOLAR_MASS
);
}
Body ref Saturn(){
return new Body(
8.34336671824457987e+00,
4.12479856412430479e+00,
-4.03523417114321381e-01,
-2.76742510726862411e-03 * DAYS_PER_YEAR,
4.99852801234917238e-03 * DAYS_PER_YEAR,
2.30417297573763929e-05 * DAYS_PER_YEAR,
2.85885980666130812e-04 * SOLAR_MASS
);
}
Body ref Uranus(){
return new Body(
1.28943695621391310e+01,
-1.51111514016986312e+01,
-2.23307578892655734e-01,
2.96460137564761618e-03 * DAYS_PER_YEAR,
2.37847173959480950e-03 * DAYS_PER_YEAR,
-2.96589568540237556e-05 * DAYS_PER_YEAR,
4.36624404335156298e-05 * SOLAR_MASS
);
}
Body ref Neptune(){
return new Body(
1.53796971148509165e+01,
-2.59193146099879641e+01,
1.79258772950371181e-01,
2.68067772490389322e-03 * DAYS_PER_YEAR,
1.62824170038242295e-03 * DAYS_PER_YEAR,
-9.51592254519715870e-05 * DAYS_PER_YEAR,
5.15138902046611451e-05 * SOLAR_MASS
);
}
Body ref Sun(){
return new Body(
0,
0,
0,
0,
0,
0,
SOLAR_MASS
);
}
class NBodySystem {
Body ref[] bodies;
void NBodySystem()
{
bodies = {
Sun(),
Jupiter(),
Saturn(),
Uranus(),
Neptune()
};
auto px = 0.0;
auto py = 0.0;
auto pz = 0.0;
auto size = this.bodies.size;
for (auto i=0; i<size; i++){
auto b = this.bodies[i];
auto m = b.mass;
px += b.vx * m;
py += b.vy * m;
pz += b.vz * m;
}
bodies[0].vx = -px / SOLAR_MASS;
bodies[0].vy = -py / SOLAR_MASS;
bodies[0].vz = -pz / SOLAR_MASS;
}
void advance(double dt)
{
double dx, dy, dz, distance, mag;
int size = this.bodies.size;
for (auto i=0; i<size; i++) {
for (auto j=i+1; j < size; j++) {
auto bi = this.bodies[i];
auto bj = this.bodies[j];
dx = bi.x - bj.x;
dy = bi.y - bj.y;
dz = bi.z - bj.z;
distance = sqrt(dx*dx + dy*dy + dz*dz);
mag = dt / (distance * distance * distance);
bi.vx -= dx * bj.mass * mag;
bi.vy -= dy * bj.mass * mag;
bi.vz -= dz * bj.mass * mag;
bj.vx += dx * bi.mass * mag;
bj.vy += dy * bi.mass * mag;
bj.vz += dz * bi.mass * mag;
}
}
for (auto i=0; i<size; i++) {
auto body = this.bodies[i];
body.x += dt * body.vx;
body.y += dt * body.vy;
body.z += dt * body.vz;
}
}
}
auto n = 1 * 200000;//+process.argv[2];
auto nbodies = new NBodySystem();
auto t0 = clock();
for (auto i=0; i<n; i++){ nbodies.advance(0.01); }
auto duration = clock() - t0;
Print(duration);
Print("\n");
return duration;
import img.canvas;
//import win.window;
import std.time;
import std.io;
import std.math;
float time = 160200;
//time = clock();
int size = 1000;
float ambient = 0.1;
float diffusion = 0.7;
float specular = 0.6;
float power = 12;
int width = size;
int height = size;
class Material
{
int[513] lmap;
void Material(int col, float amb, float dif)
{
int i, r = col >> 16, g = (col >> 8) & 255, b = col & 255;
float a;
for(i = 0; i < 512; i++)
{
a = ((i < 256) ? amb : ((((i - 256) * (dif - amb)) * 0.00390625) + amb)) * 2;
if (a<1) lmap[i] = (int(r * a) << 16) | (int(g * a) << 8) | (int(b * a) << 0);
else lmap[i] = (int(255 - (255 - r) * (2 - a)) << 16) | (int(255 - (255 - g) * (2 - a)) << 8) | (int((255 - (255 - b) * (2 - a))) << 0);
}
}
}
class Sphere
{
float x, y, z, r2;
Material ref mat;
float cx, cy, cz, cr, omg, pha;
void Sphere(float cx, cy, cz, cr, omg, pha, r, Material ref mat)
{
this.cx = cx;
this.cy = cy;
this.cz = cz;
this.cr = cr;
this.omg = omg;
this.pha = pha;
this.r2 = r * r;
this.mat = mat;
}
void update()
{
float ang = time * omg + pha;
x = cos(ang)*cr+cx;
y = cy;
z = sin(ang)*cr+cz;
}
}
class SphereNode
{
Sphere ref sphere;
SphereNode ref next;
}
auto SphereNode:init(Sphere ref sphere)
{
this.sphere = sphere;
return this;
}
auto SphereNode:add(Sphere ref sphere)
{
next = new SphereNode;
next.init(sphere);
return next;
}
float focusZ = size;
float floorY = 100;
Canvas screen = Canvas(width, height);
float[] initialDir = new float[size*size*3];
SphereNode ref firstSphere, lastSphere;
float3 ldir = float3(0, 0, 0);
float3 light = float3(100,100,100);
int[513] smap;
int[1024] fcol;
auto refs = {0, 1, 2, 3, 3};
auto reff = {0xffffff, 0x7f7f7f, 0x3f3f3f, 0x1f1f1f, 0x1f1f1f};
float mx, my, camX, camY, camZ, tcamX, tcamY, tcamZ;
float[] pixels = screen.GetData();
void setup()
{
int i, j, idx;
float l;
int s;
float hs = (size - 1) * 0.5;
for({j=0;idx=0;}; j<size; j++)
{
for (i=0; i<size; i++)
{
l = 1/sqrt((i - hs)*(i - hs) + (j - hs)*(j - hs) + focusZ*focusZ);
initialDir[idx] = (i-hs) * l; idx++;
initialDir[idx] = (j-hs) * l; idx++;
initialDir[idx] = focusZ * l; idx++;
}
}
for (i=0; i<512; i++)
{
s = (i<256) ? 64 : int((((i-256) * 0.00390625) ** power) * (power + 2) * specular * 0.15915494309189534 * 192 + 64);
if (s > 255) s = 255;
smap[i] = 0x10101 * s;
s = (i<256) ? (255-i) : (i-256);
fcol[i] = 0x10101 * (s - (s>>3) + 31);
fcol[i+512] = 0x10101 * ((s>>2) - (s>>5) + 31);
}
firstSphere = new SphereNode;
firstSphere.init(new Sphere(100, 40, 600, 200, 0.3, 1.0, 60, new Material(0x8080ff,ambient,diffusion)))
.add(new Sphere( 0, 50, 300, 100, 0.8, 0.8, 50, new Material(0x80ff80,ambient,diffusion)))
.add(new Sphere( 50, 60, 200, 200, 0.6, 2.0, 40, new Material(0xff8080,ambient,diffusion)))
.add(new Sphere(-50, 70, 500, 300, 0.4, 1.4, 30, new Material(0xc0c080,ambient,diffusion)))
.add(new Sphere(-90, 30, 600, 400, 0.2, 1.5, 70, new Material(0xc080c0,ambient,diffusion)))
.add(new Sphere( 70, 80, 400, 100, 0.7, 1.2, 20, new Material(0x80c0c0,ambient,diffusion)));
camX = camY = camZ = tcamX = tcamY = tcamZ = 0;
}
void draw()
{
int i, j, k, l, idx;
float t, tmin, n;
Sphere ref s;
int ln, pixel;
Sphere ref hit;
int a, kmax;
float ox, oy, oz, dx, dy, dz, nx, ny, nz, dsx, dsy, dsz, B, C, D;
light.x = cos(time*0.6)*100;
light.y = sin(time*1.1)*25+100;
light.z = sin(time*0.9)*100-100;
ldir.x = -light.x;
ldir.y = -light.y;
ldir.z = -light.z;
ldir.normalize();
tcamX = mx * 400;
tcamY = my * 150 - 50;
tcamZ = my * 400 - 200;
camX += (tcamX - camX) * 0.02;
camY += (tcamY - camY) * 0.02;
camZ += (tcamZ - camZ) * 0.02;
auto sphereNode = firstSphere;
while (sphereNode)
{
sphereNode.sphere.update();
sphereNode = sphereNode.next;
}
int pos = 0;
for ({j=0;idx=0;}; j<size; ++j)
{
for (i=0; i<size; ++i)
{
ox = camX;
oy = camY;
oz = camZ;
dx = initialDir[idx]; idx++;
dy = initialDir[idx]; idx++;
dz = initialDir[idx]; idx++;
pixel = 0;
for (l=1; l<5; l++)
{
tmin = 99999;
hit = nullptr;
sphereNode = firstSphere;
while(sphereNode)
{
s = sphereNode.sphere;
dsx = ox - s.x;
dsy = oy - s.y;
dsz = oz - s.z;
B = dsx * dx + dsy * dy + dsz * dz;
C = dsx * dsx + dsy * dsy + dsz * dsz - s.r2;
D = B * B - C;
if (D > 0) {
t = - B - sqrt(D);
if ((t > 0) && (t < tmin)) {
tmin = t;
hit = s;
}
}
sphereNode = sphereNode.next;
}
if (hit)
{
ox += dx * tmin;
oy += dy * tmin;
oz += dz * tmin;
nx = ox - hit.x;
ny = oy - hit.y;
nz = oz - hit.z;
n = 1 / sqrt(nx*nx + ny*ny + nz*nz);
nx *= n;
ny *= n;
nz *= n;
n = -(nx*dx + ny*dy + nz*dz) * 2;
dx += nx * n;
dy += ny * n;
dz += nz * n;
ln = int((ldir.x * nx + ldir.y * ny + ldir.z * nz) * 255) + 256;
a = hit.mat.lmap[ln];
a = a >> refs[l];
a = a & reff[l];
pixel += a;
}
else
{
if (dy < 0)
{
ln = int((ldir.x * dx + ldir.y * dy + ldir.z * dz) * 255) + 256;
a = smap[ln];
ln = l - 1;
a = a >> refs[ln];
a = a & reff[ln];
pixel += a;
break;
}
else
{
tmin = (floorY-oy)/dy;
ox += dx * tmin;
oy += dy * tmin;
oz += dz * tmin;
dy = -dy;
ln = dy * 256 + ((((int(ox+oz)>>7)+(int(ox-oz)>>7))&1)<<9) + 256;
a = fcol[ln];
a = a >> refs[l];
a = a & reff[l];
pixel += a;
}
}
}
pixels[pos * 4 + 0] = ((pixel >> 16) & 0xff) / 255.0;
pixels[pos * 4 + 1] = ((pixel >> 8) & 0xff) / 255.0;
pixels[pos * 4 + 2] = ((pixel) & 0xff) / 255.0;
++pos;
}
}
}
setup();
auto t0 = clock();
draw();
auto duration = clock() - t0;
/*
Window main = Window("nullc", 0, 0, width, height);
main.DrawCanvas(&screen, -1, -1);
while(1)
main.Update();
main.Close();
*/
Print(duration);
Print("\n");
return duration;
import std.time;
import std.io;
int[8] sha256(char[] message, int messageSize)
{
//Initialize hash values:
//(first 32 bits of the fractional parts of the square roots of the first 8 primes 2..19):
int h0 = 0x6a09e667;
int h1 = 0xbb67ae85;
int h2 = 0x3c6ef372;
int h3 = 0xa54ff53a;
int h4 = 0x510e527f;
int h5 = 0x9b05688c;
int h6 = 0x1f83d9ab;
int h7 = 0x5be0cd19;
//Initialize array of round constants:
//(first 32 bits of the fractional parts of the cube roots of the first 64 primes 2..311):
int[64] k = {
int(0x428a2f98), int(0x71374491), int(0xb5c0fbcf), int(0xe9b5dba5), int(0x3956c25b), int(0x59f111f1), int(0x923f82a4), int(0xab1c5ed5),
int(0xd807aa98), int(0x12835b01), int(0x243185be), int(0x550c7dc3), int(0x72be5d74), int(0x80deb1fe), int(0x9bdc06a7), int(0xc19bf174),
int(0xe49b69c1), int(0xefbe4786), int(0x0fc19dc6), int(0x240ca1cc), int(0x2de92c6f), int(0x4a7484aa), int(0x5cb0a9dc), int(0x76f988da),
int(0x983e5152), int(0xa831c66d), int(0xb00327c8), int(0xbf597fc7), int(0xc6e00bf3), int(0xd5a79147), int(0x06ca6351), int(0x14292967),
int(0x27b70a85), int(0x2e1b2138), int(0x4d2c6dfc), int(0x53380d13), int(0x650a7354), int(0x766a0abb), int(0x81c2c92e), int(0x92722c85),
int(0xa2bfe8a1), int(0xa81a664b), int(0xc24b8b70), int(0xc76c51a3), int(0xd192e819), int(0xd6990624), int(0xf40e3585), int(0x106aa070),
int(0x19a4c116), int(0x1e376c08), int(0x2748774c), int(0x34b0bcb5), int(0x391c0cb3), int(0x4ed8aa4a), int(0x5b9cca4f), int(0x682e6ff3),
int(0x748f82ee), int(0x78a5636f), int(0x84c87814), int(0x8cc70208), int(0x90befffa), int(0xa4506ceb), int(0xbef9a3f7), int(0xc67178f2) };
/*Pre-processing (Padding):
begin with the original message of length L bits
append a single '1' bit
append K '0' bits, where K is the minimum number >= 0 such that L + 1 + K + 64 is a multiple of 512
append L as a 64-bit big-endian integer, making the total post-processed length a multiple of 512 bits
Process the message in successive 512-bit chunks:
break message into 512-bit chunks*/
for (int chunk = 0; chunk < messageSize / 64; chunk++)
{
int[64] w;
//(The initial values in w[0..63] don't matter, so many implementations zero them here)
for (int i = 0; i < 16; i++)
{
w[i] = (message[i * 4 + 0] << 24) | (message[i * 4 + 1] << 16) | (message[i * 4 + 2] << 8) | message[i * 4 + 3];
}
//Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array:
for (int i = 16; i < 64; i++)
{
int s0 = ((w[i-15] >> 7) | (w[i-15] << (32 - 7))) ^ ((w[i-15] >> 18) | (w[i-15] << (32 - 18))) ^ (w[i-15] >> 3);
int s1 = ((w[i-2] >> 17) | (w[i-2] << (32 - 17))) ^ ((w[i- 2] >> 19) | (w[i- 2] << (32 - 19))) ^ (w[i- 2] >> 10);
w[i] = w[i-16] + s0 + w[i-7] + s1;
}
//Initialize working variables to current hash value:
int a = h0;
int b = h1;
int c = h2;
int d = h3;
int e = h4;
int f = h5;
int g = h6;
int h = h7;
//Compression function main loop:
for (int i = 0; i < 64; i++)
{
int S1 = ((e >> 6) | (e << (32 - 6))) ^ ((e >> 11) | (e << (32 - 11))) ^ ((e >> 25) | (e << (32 - 25)));
int ch = (e & f) ^ ((~e) & g);
int temp1 = h + S1 + ch + k[i] + w[i];
int S0 = ((a >> 2) | (a << (32 - 2))) ^ ((a >> 13) | (a << (32 - 13))) ^ ((a >> 22) | (a << (32 - 22)));
int maj = (a & b) ^ (a & c) ^ (b & c);
int temp2 = S0 + maj;
h = g;
g = f;
f = e;
e = d + temp1;
d = c;
c = b;
b = a;
a = temp1 + temp2;
}
//Add the compressed chunk to the current hash value:
h0 = h0 + a;
h1 = h1 + b;
h2 = h2 + c;
h3 = h3 + d;
h4 = h4 + e;
h5 = h5 + f;
h6 = h6 + g;
h7 = h7 + h;
}
return { h0, h1, h2, h3, h4, h5, h6, h7 };
}
char[1024] message = 13;
int messageSize = 1024;
int[8] res;
auto t0 = clock();
for (int iters = 0; iters < 1024; iters++)
{
res = sha256(message, messageSize);
}
auto duration = clock() - t0;
Print(duration);
Print("\n");
return duration;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment