Skip to content

Commit

Permalink
Handle mod with values < 1 && > 0
Browse files Browse the repository at this point in the history
Fixes #32
  • Loading branch information
captbaritone committed May 16, 2021
1 parent 3f7cb81 commit dfb9a2c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/compiler/src/encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export const op = {
i32_mul: 0x6c,
// i32_div_s: 0x6d,
// i32_div_u: 0x6e,
// i32_rem_s: 0x6f,
i32_rem_s: 0x6f,
// i32_rem_u: 0x70,
i32_and: 0x71,
i32_or: 0x72,
Expand Down Expand Up @@ -160,7 +160,7 @@ export const op = {
// f64_copysign: 0xa6,

i32_trunc_f64_s: 0xaa,
// i32_trunc_f64_u: 0xab,
i32_trunc_f64_u: 0xab,
i64_trunc_s_f64: 0xb0,
f64_convert_i64_s: 0xb9,
f64_convert_i32_s: 0xb7,
Expand Down
16 changes: 9 additions & 7 deletions packages/compiler/src/wasmFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,20 @@ export const localFuncMap: { [functionName: string]: FunctionDefinition } = {
mod: {
args: [VAL_TYPE.f64, VAL_TYPE.f64],
returns: [VAL_TYPE.f64],
localVariables: [VAL_TYPE.i32],
// TODO: Simplify all this type coersion
binary: [
...op.local_get(1),
...op.f64_const(0),
op.f64_ne,
op.i32_trunc_f64_s,
...op.local_tee(2),
...op.i32_const(0),
op.i32_ne,
...op.if(BLOCK.f64),
...op.local_get(0),
op.i64_trunc_s_f64,
...op.local_get(1),
op.i64_trunc_s_f64,
op.i64_rem_s,
op.f64_convert_i64_s,
op.i32_trunc_f64_s,
...op.local_get(2),
op.i32_rem_s,
op.f64_convert_i32_s,
op.else,
...op.f64_const(0),
op.end,
Expand Down
1 change: 1 addition & 0 deletions packages/compiler/tools/testCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const testCases: [string, string, number][] = [
["Divide", "g = 10 / 10;", 1],
["Mod", "g = 5 % 2;", 1],
["Mod zero", "g = 5 % 0;", 0],
["Mod almost zero", "g = 5 % 0.1;", 0],
["Bitwise and", "g = 3 & 5;", 1],
["Bitwise or", "g = 3 | 5;", 7],
["To the power", "g = 5 ^ 2;", 25],
Expand Down

0 comments on commit dfb9a2c

Please sign in to comment.