Skip to content

Commit

Permalink
feat: add sqrt function
Browse files Browse the repository at this point in the history
  • Loading branch information
Gusarich committed Nov 18, 2024
1 parent 1fcfc65 commit c88e882
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 1 deletion.
114 changes: 114 additions & 0 deletions src/generator/writers/__snapshots__/writeSerialization.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1741,6 +1741,44 @@ return result;",
"name": "__tact_pow",
"signature": "int __tact_pow(int base, int exp)",
},
{
"code": {
"code": "UBITSIZE",
"kind": "asm",
"shuffle": "",
},
"comment": null,
"context": "stdlib",
"depends": Set {},
"flags": Set {},
"name": "__tact_log2_floor_unsafe",
"signature": "int __tact_log2_floor_unsafe(int num)",
},
{
"code": {
"code": "if (num == 0) { return 0; }
int s = __tact_log2_floor_unsafe(num);
int x = (s == 1 ? (num - 1) / 2 + 1 : 1 << ((s + 1) / 2));

do {
int q = (muldivc(num, 1, x) - x) / 2;
x += q;
} until (q == 0);

return x;",
"kind": "generic",
},
"comment": null,
"context": "stdlib",
"depends": Set {
"__tact_log2_floor_unsafe",
},
"flags": Set {
"inline",
},
"name": "__tact_sqrt",
"signature": "int __tact_sqrt(int num)",
},
{
"code": {
"code": "var (r, ok) = __tact_dict_get(d, kl, k);
Expand Down Expand Up @@ -6167,6 +6205,44 @@ return result;",
"name": "__tact_pow",
"signature": "int __tact_pow(int base, int exp)",
},
{
"code": {
"code": "UBITSIZE",
"kind": "asm",
"shuffle": "",
},
"comment": null,
"context": "stdlib",
"depends": Set {},
"flags": Set {},
"name": "__tact_log2_floor_unsafe",
"signature": "int __tact_log2_floor_unsafe(int num)",
},
{
"code": {
"code": "if (num == 0) { return 0; }
int s = __tact_log2_floor_unsafe(num);
int x = (s == 1 ? (num - 1) / 2 + 1 : 1 << ((s + 1) / 2));

do {
int q = (muldivc(num, 1, x) - x) / 2;
x += q;
} until (q == 0);

return x;",
"kind": "generic",
},
"comment": null,
"context": "stdlib",
"depends": Set {
"__tact_log2_floor_unsafe",
},
"flags": Set {
"inline",
},
"name": "__tact_sqrt",
"signature": "int __tact_sqrt(int num)",
},
{
"code": {
"code": "var (r, ok) = __tact_dict_get(d, kl, k);
Expand Down Expand Up @@ -10593,6 +10669,44 @@ return result;",
"name": "__tact_pow",
"signature": "int __tact_pow(int base, int exp)",
},
{
"code": {
"code": "UBITSIZE",
"kind": "asm",
"shuffle": "",
},
"comment": null,
"context": "stdlib",
"depends": Set {},
"flags": Set {},
"name": "__tact_log2_floor_unsafe",
"signature": "int __tact_log2_floor_unsafe(int num)",
},
{
"code": {
"code": "if (num == 0) { return 0; }
int s = __tact_log2_floor_unsafe(num);
int x = (s == 1 ? (num - 1) / 2 + 1 : 1 << ((s + 1) / 2));

do {
int q = (muldivc(num, 1, x) - x) / 2;
x += q;
} until (q == 0);

return x;",
"kind": "generic",
},
"comment": null,
"context": "stdlib",
"depends": Set {
"__tact_log2_floor_unsafe",
},
"flags": Set {
"inline",
},
"name": "__tact_sqrt",
"signature": "int __tact_sqrt(int num)",
},
{
"code": {
"code": "var (r, ok) = __tact_dict_get(d, kl, k);
Expand Down
26 changes: 26 additions & 0 deletions src/generator/writers/writeStdlib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,32 @@ export function writeStdlib(ctx: WriterContext): void {
});
});

ctx.fun(`__tact_log2_floor_unsafe`, () => {
ctx.signature(`int __tact_log2_floor_unsafe(int num)`);
ctx.context("stdlib");
ctx.asm("", "UBITSIZE");
});

ctx.fun(`__tact_sqrt`, () => {
ctx.signature(`int __tact_sqrt(int num)`);
ctx.flag("inline");
ctx.context("stdlib");
ctx.body(() => {
ctx.write(`
if (num == 0) { return 0; }
int s = ${ctx.used("__tact_log2_floor_unsafe")}(num);
int x = (s == 1 ? (num - 1) / 2 + 1 : 1 << ((s + 1) / 2));
do {
int q = (muldivc(num, 1, x) - x) / 2;
x += q;
} until (q == 0);
return x;
`);
});
});

// generate dict operations for all combinations of key/value types
for (const key of keyTypes) {
for (const val of valTypes) {
Expand Down
3 changes: 2 additions & 1 deletion src/imports/stdlib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ files['std/math.tact'] =
'bmFtZShfX3RhY3RfcG93KQpuYXRpdmUgcG93KGJhc2U6IEludCwgZXhwOiBJbnQpOiBJbnQ7Cgphc20gZnVuIHBvdzIoZXhwOiBJbnQpOiBJbnQgeyBQT1cyIH0KCmFz' +
'bSBmdW4gc2lnbih4OiBJbnQpOiBJbnQgeyBTR04gfQoKLy8gZmxvb3IoeCp5LzJeeikKYXNtIGZ1biBtdWxyc2hpZnQoeDogSW50LCB5OiBJbnQsIHo6IEludCk6IElu' +
'dCB7IE1VTFJTSElGVCB9CgovLyByb3VuZCh4KnkvMl56KQphc20gZnVuIG11bHJzaGlmdF9yb3VuZCh4OiBJbnQsIHk6IEludCwgejogSW50KTogSW50IHsgTVVMUlNI' +
'SUZUUiB9CgovLyBjZWlsKHgqeS8yXnopCmFzbSBmdW4gbXVscnNoaWZ0X2NlaWwoeDogSW50LCB5OiBJbnQsIHo6IEludCk6IEludCB7IE1VTFJTSElGVEMgfQo=';
'SUZUUiB9CgovLyBjZWlsKHgqeS8yXnopCmFzbSBmdW4gbXVscnNoaWZ0X2NlaWwoeDogSW50LCB5OiBJbnQsIHo6IEludCk6IEludCB7IE1VTFJTSElGVEMgfQoKQG5h' +
'bWUoX190YWN0X3NxcnQpCm5hdGl2ZSBzcXJ0KG51bTogSW50KTogSW50Owo=';
files['std/primitives.tact'] =
'cHJpbWl0aXZlIEludDsKcHJpbWl0aXZlIEJvb2w7CnByaW1pdGl2ZSBCdWlsZGVyOwpwcmltaXRpdmUgU2xpY2U7CnByaW1pdGl2ZSBDZWxsOwpwcmltaXRpdmUgQWRk' +
'cmVzczsKcHJpbWl0aXZlIFN0cmluZzsKcHJpbWl0aXZlIFN0cmluZ0J1aWxkZXI7';
Expand Down
3 changes: 3 additions & 0 deletions stdlib/std/math.tact
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,6 @@ asm fun mulrshift_round(x: Int, y: Int, z: Int): Int { MULRSHIFTR }

// ceil(x*y/2^z)
asm fun mulrshift_ceil(x: Int, y: Int, z: Int): Int { MULRSHIFTC }

@name(__tact_sqrt)
native sqrt(num: Int): Int;

0 comments on commit c88e882

Please sign in to comment.