Skip to content

Commit 52cc15a

Browse files
authored
feat(stdlib)!: Replace Float32 arithmatic/comparison functions with operators (#1954)
1 parent 3c0ea18 commit 52cc15a

File tree

5 files changed

+144
-95
lines changed

5 files changed

+144
-95
lines changed

compiler/test/stdlib/float32.test.gr

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ module Float32Test
22

33
include "float32"
44
from Float32 use *
5+
from Pervasives use { (-) as numberSub }
56

67
// Constants Test
78
// smoke test:
8-
assert gt(infinity, 100000000.0f)
9+
assert infinity > 100000000.0f
910
// test infinity-specific semantics:
1011
assert toNumber(infinity) == toNumber(infinity)
11-
assert toNumber(infinity) == toNumber(infinity) - 1
12+
assert toNumber(infinity) == numberSub(toNumber(infinity), 1)
1213
assert nan != nan
1314

1415
assert pi == 3.1415927f
@@ -25,15 +26,15 @@ assert fromNumber(0) == 0.0f
2526
assert toNumber(555.0f) == 555
2627
assert toNumber(0.0f) == 0
2728

28-
assert gt(5.0f, 4.0f)
29-
assert gte(5.0f, 5.0f)
30-
assert lt(5.0f, 17.0f)
31-
assert lte(5.0f, 5.0f)
32-
assert !gt(5.0f, 5.0f)
33-
assert !gte(5.0f, 22.0f)
34-
assert !lt(5.0f, -17.0f)
35-
assert !lte(5.0f, 4.0f)
36-
29+
assert 5.0f > 4.0f
30+
assert 5.0f >= 5.0f
31+
assert 5.0f < 17.0f
32+
assert 5.0f <= 5.0f
33+
assert !(5.0f > 5.0f)
34+
assert !(5.0f >= 22.0f)
35+
assert !(5.0f < -17.0f)
36+
assert !(5.0f <= 4.0f)
37+
from Pervasives use { (<), (>) }
3738
assert compare(1.0f, 2.0f) < 0
3839
assert compare(3.0f, 2.0f) > 0
3940
assert compare(1.0f, 1.0f) == 0

compiler/test/suites/numbers.re

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,7 @@ describe("numbers", ({test, testSkip}) => {
9191
);
9292
assertRun("number_syntax13", "print(17179869184 - 1024)", "17179868160\n");
9393
// equality checks
94-
assertRun(
95-
"nan_equality1",
96-
{|include "float32"; print(Float32.div(0.0f, 0.0f) == Float32.div(0.0f, 0.0f))|},
97-
"false\n",
98-
);
94+
assertRun("nan_equality1", {|print(NaNf == NaNf)|}, "false\n");
9995
assertRun(
10096
"nan_equality2",
10197
{|include "float64"; print(Float64.div(0.0d, 0.0d) == Float64.div(0.0d, 0.0d))|},

compiler/test/suites/strings.re

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -265,21 +265,9 @@ bar", 1))|},
265265
"let x = \"\\u{110000}\"",
266266
"Illegal unicode code point",
267267
);
268-
assertRun(
269-
"string_float1",
270-
{|include "float32"; from Float32 use *; print(div(0.0f, 0.0f))|},
271-
"NaN\n",
272-
);
273-
assertRun(
274-
"string_float2",
275-
{|include "float32"; from Float32 use *; print(div(1.0f, 0.0f))|},
276-
"Infinity\n",
277-
);
278-
assertRun(
279-
"string_float3",
280-
{|include "float32"; from Float32 use *; print(div(-1.0f, 0.0f))|},
281-
"-Infinity\n",
282-
);
268+
assertRun("string_float1", {|print(NaNf)|}, "NaN\n");
269+
assertRun("string_float2", {|print(Infinityf)|}, "Infinity\n");
270+
assertRun("string_float3", {|print(-Infinityf)|}, "-Infinity\n");
283271
assertRun(
284272
"string_float4",
285273
{|include "float64"; from Float64 use *; print(div(0.0d, 0.0d))|},

stdlib/float32.gr

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,11 @@ provide { fromNumber, toNumber }
7979
* @param y: The second operand
8080
* @returns The sum of the two operands
8181
*
82-
* @since v0.2.0
82+
* @since v0.6.0
83+
* @history v0.2.0: Originally named `add`
8384
*/
8485
@unsafe
85-
provide let add = (x: Float32, y: Float32) => {
86+
provide let (+) = (x: Float32, y: Float32) => {
8687
let xv = WasmF32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
8788
let yv = WasmF32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
8889
let ptr = newFloat32(xv + yv)
@@ -96,10 +97,11 @@ provide let add = (x: Float32, y: Float32) => {
9697
* @param y: The second operand
9798
* @returns The difference of the two operands
9899
*
99-
* @since v0.2.0
100+
* @since v0.6.0
101+
* @history v0.2.0: Originally named `sub`
100102
*/
101103
@unsafe
102-
provide let sub = (x: Float32, y: Float32) => {
104+
provide let (-) = (x: Float32, y: Float32) => {
103105
let xv = WasmF32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
104106
let yv = WasmF32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
105107
let ptr = newFloat32(xv - yv)
@@ -113,10 +115,11 @@ provide let sub = (x: Float32, y: Float32) => {
113115
* @param y: The second operand
114116
* @returns The product of the two operands
115117
*
116-
* @since v0.2.0
118+
* @since v0.6.0
119+
* @history v0.2.0: Originally named `mul`
117120
*/
118121
@unsafe
119-
provide let mul = (x: Float32, y: Float32) => {
122+
provide let (*) = (x: Float32, y: Float32) => {
120123
let xv = WasmF32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
121124
let yv = WasmF32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
122125
let ptr = newFloat32(xv * yv)
@@ -130,10 +133,11 @@ provide let mul = (x: Float32, y: Float32) => {
130133
* @param y: The second operand
131134
* @returns The quotient of the two operands
132135
*
133-
* @since v0.2.0
136+
* @since v0.6.0
137+
* @history v0.2.0: Originally named `div`
134138
*/
135139
@unsafe
136-
provide let div = (x: Float32, y: Float32) => {
140+
provide let (/) = (x: Float32, y: Float32) => {
137141
let xv = WasmF32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
138142
let yv = WasmF32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
139143
let ptr = newFloat32(xv / yv)
@@ -147,10 +151,11 @@ provide let div = (x: Float32, y: Float32) => {
147151
* @param y: The second value
148152
* @returns `true` if the first value is less than the second value or `false` otherwise
149153
*
150-
* @since v0.2.0
154+
* @since v0.6.0
155+
* @history v0.2.0: Originally named `lt`
151156
*/
152157
@unsafe
153-
provide let lt = (x: Float32, y: Float32) => {
158+
provide let (<) = (x: Float32, y: Float32) => {
154159
let xv = WasmF32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
155160
let yv = WasmF32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
156161
xv < yv
@@ -163,10 +168,11 @@ provide let lt = (x: Float32, y: Float32) => {
163168
* @param y: The second value
164169
* @returns `true` if the first value is greater than the second value or `false` otherwise
165170
*
166-
* @since v0.2.0
171+
* @since v0.6.0
172+
* @history v0.2.0: Originally named `gt`
167173
*/
168174
@unsafe
169-
provide let gt = (x: Float32, y: Float32) => {
175+
provide let (>) = (x: Float32, y: Float32) => {
170176
let xv = WasmF32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
171177
let yv = WasmF32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
172178
xv > yv
@@ -179,10 +185,11 @@ provide let gt = (x: Float32, y: Float32) => {
179185
* @param y: The second value
180186
* @returns `true` if the first value is less than or equal to the second value or `false` otherwise
181187
*
182-
* @since v0.2.0
188+
* @since v0.6.0
189+
* @history v0.2.0: Originally named `lte`
183190
*/
184191
@unsafe
185-
provide let lte = (x: Float32, y: Float32) => {
192+
provide let (<=) = (x: Float32, y: Float32) => {
186193
let xv = WasmF32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
187194
let yv = WasmF32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
188195
xv <= yv
@@ -195,10 +202,11 @@ provide let lte = (x: Float32, y: Float32) => {
195202
* @param y: The second value
196203
* @returns `true` if the first value is greater than or equal to the second value or `false` otherwise
197204
*
198-
* @since v0.2.0
205+
* @since v0.6.0
206+
* @history v0.2.0: Originally named `gte`
199207
*/
200208
@unsafe
201-
provide let gte = (x: Float32, y: Float32) => {
209+
provide let (>=) = (x: Float32, y: Float32) => {
202210
let xv = WasmF32.load(WasmI32.fromGrain(x), _VALUE_OFFSET)
203211
let yv = WasmF32.load(WasmI32.fromGrain(y), _VALUE_OFFSET)
204212
xv >= yv

0 commit comments

Comments
 (0)