Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(stdlib)!: Replace Float32 arithmatic/comparison functions with operators #1954

Merged
merged 3 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions compiler/test/stdlib/float32.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ module Float32Test

include "float32"
from Float32 use *
from Pervasives use { (-) as numberSub }

// Constants Test
// smoke test:
assert gt(infinity, 100000000.0f)
assert infinity > 100000000.0f
// test infinity-specific semantics:
assert toNumber(infinity) == toNumber(infinity)
assert toNumber(infinity) == toNumber(infinity) - 1
assert toNumber(infinity) == numberSub(toNumber(infinity), 1)
assert nan != nan

assert pi == 3.1415927f
Expand All @@ -25,15 +26,15 @@ assert fromNumber(0) == 0.0f
assert toNumber(555.0f) == 555
assert toNumber(0.0f) == 0

assert gt(5.0f, 4.0f)
assert gte(5.0f, 5.0f)
assert lt(5.0f, 17.0f)
assert lte(5.0f, 5.0f)
assert !gt(5.0f, 5.0f)
assert !gte(5.0f, 22.0f)
assert !lt(5.0f, -17.0f)
assert !lte(5.0f, 4.0f)

assert 5.0f > 4.0f
assert 5.0f >= 5.0f
assert 5.0f < 17.0f
assert 5.0f <= 5.0f
assert !(5.0f > 5.0f)
assert !(5.0f >= 22.0f)
assert !(5.0f < -17.0f)
assert !(5.0f <= 4.0f)
from Pervasives use { (<), (>) }
assert compare(1.0f, 2.0f) < 0
assert compare(3.0f, 2.0f) > 0
assert compare(1.0f, 1.0f) == 0
Expand Down
6 changes: 1 addition & 5 deletions compiler/test/suites/numbers.re
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,7 @@ describe("numbers", ({test, testSkip}) => {
);
assertRun("number_syntax13", "print(17179869184 - 1024)", "17179868160\n");
// equality checks
assertRun(
"nan_equality1",
{|include "float32"; print(Float32.div(0.0f, 0.0f) == Float32.div(0.0f, 0.0f))|},
"false\n",
);
assertRun("nan_equality1", {|print(NaNf == NaNf)|}, "false\n");
assertRun(
"nan_equality2",
{|include "float64"; print(Float64.div(0.0d, 0.0d) == Float64.div(0.0d, 0.0d))|},
Expand Down
18 changes: 3 additions & 15 deletions compiler/test/suites/strings.re
Original file line number Diff line number Diff line change
Expand Up @@ -265,21 +265,9 @@ bar", 1))|},
"let x = \"\\u{110000}\"",
"Illegal unicode code point",
);
assertRun(
"string_float1",
{|include "float32"; from Float32 use *; print(div(0.0f, 0.0f))|},
"NaN\n",
);
assertRun(
"string_float2",
{|include "float32"; from Float32 use *; print(div(1.0f, 0.0f))|},
"Infinity\n",
);
assertRun(
"string_float3",
{|include "float32"; from Float32 use *; print(div(-1.0f, 0.0f))|},
"-Infinity\n",
);
assertRun("string_float1", {|print(NaNf)|}, "NaN\n");
assertRun("string_float2", {|print(Infinityf)|}, "Infinity\n");
assertRun("string_float3", {|print(-Infinityf)|}, "-Infinity\n");
assertRun(
"string_float4",
{|include "float64"; from Float64 use *; print(div(0.0d, 0.0d))|},
Expand Down
40 changes: 24 additions & 16 deletions stdlib/float32.gr
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@ provide { fromNumber, toNumber }
* @param y: The second operand
* @returns The sum of the two operands
*
* @since v0.2.0
* @since v0.6.0
* @history v0.2.0: Originally named `add`
*/
@unsafe
provide let add = (x: Float32, y: Float32) => {
provide let (+) = (x: Float32, y: Float32) => {
let xv = WasmF32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
let yv = WasmF32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
let ptr = newFloat32(xv + yv)
Expand All @@ -96,10 +97,11 @@ provide let add = (x: Float32, y: Float32) => {
* @param y: The second operand
* @returns The difference of the two operands
*
* @since v0.2.0
* @since v0.6.0
* @history v0.2.0: Originally named `sub`
*/
@unsafe
provide let sub = (x: Float32, y: Float32) => {
provide let (-) = (x: Float32, y: Float32) => {
let xv = WasmF32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
let yv = WasmF32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
let ptr = newFloat32(xv - yv)
Expand All @@ -113,10 +115,11 @@ provide let sub = (x: Float32, y: Float32) => {
* @param y: The second operand
* @returns The product of the two operands
*
* @since v0.2.0
* @since v0.6.0
* @history v0.2.0: Originally named `mul`
*/
@unsafe
provide let mul = (x: Float32, y: Float32) => {
provide let (*) = (x: Float32, y: Float32) => {
let xv = WasmF32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
let yv = WasmF32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
let ptr = newFloat32(xv * yv)
Expand All @@ -130,10 +133,11 @@ provide let mul = (x: Float32, y: Float32) => {
* @param y: The second operand
* @returns The quotient of the two operands
*
* @since v0.2.0
* @since v0.6.0
* @history v0.2.0: Originally named `div`
*/
@unsafe
provide let div = (x: Float32, y: Float32) => {
provide let (/) = (x: Float32, y: Float32) => {
let xv = WasmF32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
let yv = WasmF32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
let ptr = newFloat32(xv / yv)
Expand All @@ -147,10 +151,11 @@ provide let div = (x: Float32, y: Float32) => {
* @param y: The second value
* @returns `true` if the first value is less than the second value or `false` otherwise
*
* @since v0.2.0
* @since v0.6.0
* @history v0.2.0: Originally named `lt`
*/
@unsafe
provide let lt = (x: Float32, y: Float32) => {
provide let (<) = (x: Float32, y: Float32) => {
let xv = WasmF32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
let yv = WasmF32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
xv < yv
Expand All @@ -163,10 +168,11 @@ provide let lt = (x: Float32, y: Float32) => {
* @param y: The second value
* @returns `true` if the first value is greater than the second value or `false` otherwise
*
* @since v0.2.0
* @since v0.6.0
* @history v0.2.0: Originally named `gt`
*/
@unsafe
provide let gt = (x: Float32, y: Float32) => {
provide let (>) = (x: Float32, y: Float32) => {
let xv = WasmF32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
let yv = WasmF32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
xv > yv
Expand All @@ -179,10 +185,11 @@ provide let gt = (x: Float32, y: Float32) => {
* @param y: The second value
* @returns `true` if the first value is less than or equal to the second value or `false` otherwise
*
* @since v0.2.0
* @since v0.6.0
* @history v0.2.0: Originally named `lte`
*/
@unsafe
provide let lte = (x: Float32, y: Float32) => {
provide let (<=) = (x: Float32, y: Float32) => {
let xv = WasmF32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
let yv = WasmF32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
xv <= yv
Expand All @@ -195,10 +202,11 @@ provide let lte = (x: Float32, y: Float32) => {
* @param y: The second value
* @returns `true` if the first value is greater than or equal to the second value or `false` otherwise
*
* @since v0.2.0
* @since v0.6.0
* @history v0.2.0: Originally named `gte`
*/
@unsafe
provide let gte = (x: Float32, y: Float32) => {
provide let (>=) = (x: Float32, y: Float32) => {
let xv = WasmF32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
let yv = WasmF32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
xv >= yv
Expand Down
Loading
Loading