Skip to content

Instantly share code, notes, and snippets.

@MrSmith33
Last active April 27, 2022 18:26
Show Gist options
  • Save MrSmith33/4fa2b44263d8388b4c123eb30520fade to your computer and use it in GitHub Desktop.
Save MrSmith33/4fa2b44263d8388b4c123eb30520fade to your computer and use it in GitHub Desktop.
import std.array;
import std.format;
import std.string;
import std.file : write;
void main()
{
string dir = `D:/sources/my_sources/tiny_jit/test_work_dir`;
foreach(s; [10,100,1000,10_000,100_000,1000_000]) {
//make_source_c(dir, s);
//make_source_d(dir, s);
//make_source_vox(dir, s);
//make_source_go(dir, s);
//make_source_rust(dir, s);
//make_source_java(dir, s);
//make_source_cs(dir, s);
//make_source_vox_big_func(dir, s);
//make_source_vox_some_func(dir, s);
//make_source_vox_fannkuch(dir, s);
}
make_source_vox_fannkuch(dir, 80000);
//make_source_vox_let(dir, 1);
}
int funcCounter;
void make_source_c(string dir, int size)
{
auto result = appender!string;
result.reserve(10_000_000);
result ~= "#define i32 int\n#define u8 char\n";
result ~= "void print(i32 n){}\n";
foreach(i; 0..size) result.formattedWrite(fib, i);
result ~= mainSourceC;
string filename = format(`%s/c/a%s.c`, dir, size);
write(filename, result.data);
}
void make_source_d(string dir, int size)
{
auto result = appender!string;
result.reserve(10_000_000);
result ~= "alias i32 = int;\nalias u8 = char;\n";
result ~= "void print(i32 n){}\n";
foreach(i; 0..size) result.formattedWrite(fib, i);
result ~= mainSourceD;
string filename = format(`%s/d/a%s.d`, dir, size);
write(filename, result.data);
}
void make_source_go(string dir, int size)
{
auto result = appender!string;
result.reserve(10_000_000);
result ~= format("package main\n");
result ~= "func print(n int){}\n";
string fib = `func fibonacci%s() {
lo := 0;
hi := 1;
for hi < 10000 {
tmp := hi;
hi = hi + lo;
lo = tmp;
print(lo);
}
}
`;
foreach(i; 0..size) result.formattedWrite(fib, i);
result ~= mainSourceGo;
string filename = format(`%s/go/a%s.go`, dir, size);
write(filename, result.data);
}
void make_source_rust(string dir, int size)
{
auto result = appender!string;
result.reserve(10_000_000);
result ~= "#![allow(unused)]\nfn print(n : i32){}\n";
string fib = `fn fibonacci%s() {
let mut lo = 0;
let mut hi = 1;
while hi < 10000 {
let tmp = hi;
hi = hi + lo;
lo = tmp;
print(lo);
}
}
`;
foreach(i; 0..size) result.formattedWrite(fib, i);
result ~= mainSourceRust;
string filename = format(`%s/rust/a%s.rs`, dir, size);
write(filename, result.data);
}
void make_source_java(string dir, int size)
{
auto result = appender!string;
result.reserve(10_000_000);
result ~= format("public class a%s {\n", size);
result ~= "\tpublic static void main(String[] args) {}\n";
result ~= "\tpublic static void print(int n) {}\n";
string fib = ` public static void fibonacci%s() {
int lo = 0;
int hi = 1;
while (hi < 10000) {
int tmp = hi;
hi = hi + lo;
lo = tmp;
print(lo);
}
}
`;
foreach(i; 0..size) result.formattedWrite(fib, i);
result ~= "}\n";
string filename = format(`%s/java/a%s.java`, dir, size);
write(filename, result.data);
}
void make_source_cs(string dir, int size)
{
auto result = appender!string;
result.reserve(10_000_000);
result ~= format("public class a%s {\n", size);
result ~= "\tpublic static void Main(string[] args) {}\n";
result ~= "\tpublic static void print(int n) {}\n";
string fib = ` public static void fibonacci%s() {
int lo = 0;
int hi = 1;
while (hi < 10000) {
int tmp = hi;
hi = hi + lo;
lo = tmp;
print(lo);
}
}
`;
foreach(i; 0..size) result.formattedWrite(fib, i);
result ~= "}\n";
string filename = format(`%s/cs/a%s.cs`, dir, size);
write(filename, result.data);
}
void make_source_vox(string dir, int size)
{
auto result = appender!string;
result.reserve(10_000_000);
result ~= "void print(i32 n){}\n";
foreach(i; 0..size) result.formattedWrite(fib, i);
result ~= mainSourceC;
string filename = format(`%s/fib%s.vx`, dir, size);
write(filename, result.data);
}
void make_source_vox_big_func(string dir, int size)
{
auto result = appender!string;
result.reserve(10_000_000);
result ~= "i32 func(i32 a, i32 b, i32 c, i32 d) {\n";
foreach(i; 0..size)
result.formattedWrite("\ta = b * c + d;\n");
result ~= "\treturn a;\n";
result ~= "}\n";
result ~= mainSourceC;
string filename = format(`%s/big_func%s.vx`, dir, size);
write(filename, result.data);
}
void make_source_vox_some_func(string dir, int size)
{
auto result = appender!string;
result.reserve(10_000_000);
string code = "i32 someFunc%s(i32 a, i32 b, i32 c, i32 d) {
a = b * c + d;
return a;
}\n";
foreach(i; 0..size)
result.formattedWrite(code, i);
result ~= mainSourceC;
string filename = format(`%s/some_func%s.vx`, dir, size);
write(filename, result.data);
}
void make_source_vox_fannkuch(string dir, int size)
{
auto result = appender!string;
result.reserve(10_000_000);
result ~= "i32 main(void* hInstance, void* hPrevInstance, u8* lpCmdLine, i32 nCmdShow) {\n";
foreach(i; 0..size)
result.formattedWrite("\tfannkuch%s(5);\n", i);
result ~= "\treturn 0;\n";
result ~= "}\n";
result ~= "struct Result { i32 sum; i32 maxflips; }\n";
foreach(i; 0..size)
result.formattedWrite(fannkuch_vx_fun, i);
string filename = format(`%s/fannkuch%s.vx`, dir, size);
write(filename, result.data);
}
void make_source_vox_let(string dir, int size)
{
auto result = appender!string;
result ~= "i32 main() {\n";
foreach(i; 0..size)
result.formattedWrite("\ti32 _%s = 1;\n", i);
result ~= "\treturn 0;\n";
result ~= "}\n";
string filename = format(`%s/let%s.vx`, dir, size);
write(filename, result.data);
}
int someFunc0(int a, int b, int c, int d) {
a = b * c + d;
return a;
}
int make_funcs(int level, ref Appender!string result)
{
if (level == 0)
{
int func = ++funcCounter;
result.formattedWrite(funcCode0, func);
return func;
}
else
{
int func1 = make_funcs(level - 1, result);
int func2 = make_funcs(level - 1, result);
int func = ++funcCounter;
result.formattedWrite(funcCode1, func, func1, func2);
return func;
}
}
string mainSourceD =
"i32 WinMain(void* hInstance, void* hPrevInstance, u8* lpCmdLine, i32 nCmdShow) {
return 0;
}
";
string mainSourceC =
"i32 main(void* hInstance, void* hPrevInstance, u8* lpCmdLine, i32 nCmdShow) {
return 0;
}
";
string mainSourceGo =
"func main() {
}";
string mainSourceRust =
"fn main() {
}";
string funcCode0 = "i32 func%s() { return 1; }\n";
string funcCode1 = "i32 func%s() { return func%s() + func%s(); }\n";
string fib = "void fibonacci%s() {
i32 lo = 0;
i32 hi = 1;
while (hi < 10000) {
i32 tmp = hi;
hi = hi + lo;
lo = tmp;
print(lo);
}
}
";
string fannkuch_vx_fun = "
Result fannkuch%s(i32 n) {
i32 signx;
i32 maxflips;
i32 sum;
i32 i;
i32 j;
i32 k;
i32 q1;
i32 flips;
i32 qq;
i32 t;
i32 sx;
i32 tt;
i32[100] p;
i32[100] q;
i32[100] s;
signx = 1;
maxflips = 0;
sum = 0;
for (i=1; i<=n; ++i) {
p[i-1] = i;
q[i-1] = i;
s[i-1] = i;
}
while (true) {
q1 = p[1-1];
if (q1 != 1) {
for (i=2; i<=n; ++i) {
q[i-1] = p[i-1];
}
flips = 1;
while (true) {
qq = q[q1-1];
if (qq == 1) {
sum += signx*flips;
if (flips > maxflips) {
maxflips = flips;
}
break;
}
q[q1-1] = q1;
if (q1 >= 4) {
i = 2;
j = q1-1;
while (true) {
{
i32 temp = q[i-1];
q[i-1] = q[j-1];
q[j-1] = temp;
}
++i;
--j;
if (i >= j) break;
}
}
q1 = qq;
++flips;
}
}
if (signx == 1) {
{
i32 temp = p[2-1];
p[2-1] = p[1-1];
p[1-1] = temp;
}
signx = -1;
}
else {
{
i32 temp = p[2-1];
p[2-1] = p[3-1];
p[3-1] = temp;
}
signx = 1;
for (i=3; i<=n; ++i) {
sx = s[i-1];
if (sx != 1) {
s[i-1] = sx-1;
break;
}
if (i == n) {
return Result(sum, maxflips);
}
s[i-1] = i;
tt = p[1-1];
for (j=1; j<=i; ++j) {
p[j-1] = p[j+1-1];
}
p[i+1-1] = tt;
}
}
}
}";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment