Skip to content

Commit

Permalink
refactor: move bigint utils to src/optimizer/util.ts (#1468)
Browse files Browse the repository at this point in the history
  • Loading branch information
i582 authored Jan 21, 2025
1 parent 2b9bde4 commit 78092d7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 31 deletions.
28 changes: 0 additions & 28 deletions src/ast/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,31 +191,3 @@ export function checkIsName(ast: A.AstExpression): boolean {
export function checkIsBoolean(ast: A.AstExpression, b: boolean): boolean {
return ast.kind === "boolean" ? ast.value == b : false;
}

// bigint arithmetic

// precondition: the divisor is not zero
// rounds the division result towards negative infinity
export function divFloor(a: bigint, b: bigint): bigint {
const almostSameSign = a > 0n === b > 0n;
if (almostSameSign) {
return a / b;
}
return a / b + (a % b === 0n ? 0n : -1n);
}

export function abs(a: bigint): bigint {
return a < 0n ? -a : a;
}

export function sign(a: bigint): bigint {
if (a === 0n) return 0n;
else return a < 0n ? -1n : 1n;
}

// precondition: the divisor is not zero
// rounds the result towards negative infinity
// Uses the fact that a / b * b + a % b == a, for all b != 0.
export function modFloor(a: bigint, b: bigint): bigint {
return a - divFloor(a, b) * b;
}
3 changes: 1 addition & 2 deletions src/optimizer/associative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import * as A from "../ast/ast";
import * as iM from "./interpreter";
import { ExpressionTransformer, Rule } from "./types";
import {
abs,
checkIsBinaryOpNode,
checkIsBinaryOp_With_RightValue,
checkIsBinaryOp_With_LeftValue,
sign,
AstUtil,
} from "../ast/util";
import { abs, sign } from "./util";

type TransformData = {
simplifiedExpression: A.AstExpression;
Expand Down
3 changes: 2 additions & 1 deletion src/optimizer/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
throwConstEvalError,
throwInternalCompilerError,
} from "../error/errors";
import { AstUtil, divFloor, getAstUtil, modFloor } from "../ast/util";
import { AstUtil, getAstUtil } from "../ast/util";
import {
getStaticConstant,
getStaticFunction,
Expand All @@ -24,6 +24,7 @@ import { TypeRef, showValue } from "../types/types";
import { sha256_sync } from "@ton/crypto";
import { defaultParser, getParser, Parser } from "../grammar/grammar";
import { dummySrcInfo, SrcInfo } from "../grammar";
import { divFloor, modFloor } from "./util";

// TVM integers are signed 257-bit integers
const minTvmInt: bigint = -(2n ** 256n);
Expand Down
25 changes: 25 additions & 0 deletions src/optimizer/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// precondition: the divisor is not zero
// rounds the division result towards negative infinity
export function divFloor(a: bigint, b: bigint): bigint {
const almostSameSign = a > 0n === b > 0n;
if (almostSameSign) {
return a / b;
}
return a / b + (a % b === 0n ? 0n : -1n);
}

export function abs(a: bigint): bigint {
return a < 0n ? -a : a;
}

export function sign(a: bigint): bigint {
if (a === 0n) return 0n;
else return a < 0n ? -1n : 1n;
}

// precondition: the divisor is not zero
// rounds the result towards negative infinity
// Uses the fact that a / b * b + a % b == a, for all b != 0.
export function modFloor(a: bigint, b: bigint): bigint {
return a - divFloor(a, b) * b;
}

0 comments on commit 78092d7

Please sign in to comment.