Skip to content

Commit

Permalink
+fixed float conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
AjaniBilby committed Oct 6, 2024
1 parent 2e5e9f0 commit 407a04c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 79 deletions.
28 changes: 14 additions & 14 deletions source/compiler/codegen/expression/infix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ function CoerceToFloat(ctx: Context, type: IntrinsicType, goal: IntrinsicType) {
}

if (type.bitcode === WasmTypes.Intrinsic.i32) {
if (type.signed) ctx.block.push(Instruction.i32.trunc_f32_s());
else ctx.block.push(Instruction.i32.trunc_f32_u());
if (type.signed) ctx.block.push(Instruction.f32.convert_i32_s());
else ctx.block.push(Instruction.f32.convert_i32_u());
} else {
if (type.signed) ctx.block.push(Instruction.i32.trunc_f64_s());
else ctx.block.push(Instruction.i32.trunc_f64_u());
if (type.signed) ctx.block.push(Instruction.f64.convert_i64_s());
else ctx.block.push(Instruction.f64.convert_i64_u());
}

return goal;
Expand All @@ -134,38 +134,38 @@ function CoerceToInt(ctx: Context, type: IntrinsicType, goal: IntrinsicType) {
if (type === f32) {
if (goal.bitcode === WasmTypes.Intrinsic.i32) {
if (goal.signed) {
ctx.block.push(Instruction.f32.convert_i32_s());
ctx.block.push(Instruction.i32.trunc_sat_f32_s());
type = i32;
} else {
ctx.block.push(Instruction.f32.convert_i32_u());
ctx.block.push(Instruction.i32.trunc_sat_f32_u());
type = u32;
}
} else {
if (goal.signed) {
ctx.block.push(Instruction.f32.convert_i64_s());
ctx.block.push(Instruction.i64.trunc_sat_f32_s());
type = i64;
} else {
ctx.block.push(Instruction.f32.convert_i64_u());
ctx.block.push(Instruction.i64.trunc_sat_f32_u());
type = u64;
}
}
} else if (type === f64) {
if (goal.bitcode === WasmTypes.Intrinsic.i32) {
if (goal.signed) {
ctx.block.push(Instruction.f64.convert_i32_s());
ctx.block.push(Instruction.i32.trunc_sat_f64_s());
goal = i32;
} else {
ctx.block.push(Instruction.f64.convert_i32_u());
type = u32;
ctx.block.push(Instruction.i32.trunc_sat_f64_u());
type = i32;
}
} else {
if (goal.signed) {
ctx.block.push(Instruction.f64.convert_i64_s());
ctx.block.push(Instruction.i64.trunc_sat_f64_s());
type = i64;
}
else {
ctx.block.push(Instruction.f64.convert_i64_u());
type = u64;
ctx.block.push(Instruction.i64.trunc_sat_f64_u());
type = i64;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions source/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ function CompileTests(files: Set<string>, mainPck: Package) {
index.push(test);
testSuccess++;
pass++;
} catch (e) {
} catch (e: unknown) {
testFail++;
test.evict();
errs.push(e);
errs.push(e as Error);
ok = false;
}
}
Expand Down
19 changes: 19 additions & 0 deletions tests/conversion.test.sa
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,24 @@ test "Integer Saturation" {
// if (int(17179869184) as i32) != maxI32() { return false; };
// if (int(120) as i32) != 120 { return false; };

return true;
}

test "Float Saturation" {
if (300.0 as u8) != 255 { return false; };
if ( 16.0 as u8) != 16 { return false; };
if (200.0 as i8) != 127 { return false; };
if (120.0 as i8) != 120 { return false; };

if (-300.0 as u8) != 0 { return false; };
if ( -16.0 as u8) != 0 { return false; };
if (-200.0 as i8) != -128 { return false; };
if (-120.0 as i8) != -120 { return false; };

if (66000.0 as u16) != 65535 { return false; };
if (65534.0 as u16) != 65534 { return false; };
if (34767.0 as i16) != 32767 { return false; };
if (32000.0 as i16) != 32000 { return false; };

return true;
}
63 changes: 0 additions & 63 deletions tests/e2e/compiler/conversion.test.ts

This file was deleted.

0 comments on commit 407a04c

Please sign in to comment.