Skip to content

Commit

Permalink
feat(stdlib): Add Float32/Float64 constants for infinity/nan (#720)
Browse files Browse the repository at this point in the history
Co-authored-by: Philip Blair <[email protected]>
  • Loading branch information
phated and peblair committed Jun 13, 2021
1 parent 1dc7f56 commit 4ff3b9f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
7 changes: 7 additions & 0 deletions compiler/test/stdlib/float32.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@ assert !gt(5.0f, 5.0f)
assert !gte(5.0f, 22.0f)
assert !lt(5.0f, -17.0f)
assert !lte(5.0f, 4.0f)

// smoke test:
assert gt(infinity, 100000000.f)
// test infinity-specific semantics:
assert toNumber(infinity) == toNumber(infinity)
assert toNumber(infinity) == (toNumber(infinity) - 1)
assert nan != nan
6 changes: 6 additions & 0 deletions compiler/test/stdlib/float64.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ assert !gt(5.0d, 5.0d)
assert !gte(5.0d, 22.0d)
assert !lt(5.0d, -17.0d)
assert !lte(5.0d, 4.0d)

// smoke test:
assert gt(infinity, 100000000.d)
// test infinity-specific semantics:
assert toNumber(infinity) == (toNumber(infinity) - 1)
assert nan != nan
18 changes: 18 additions & 0 deletions stdlib/float32.gr
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,21 @@ export let gte = (x: Float32, y: Float32) => {
let y = WasmF32.load(WasmI32.fromGrain(y), 8n)
WasmF32.ge(x, y)
}

// floating-point constants:

@disableGC
let makeInfinity = () => {
let ptr = newFloat32(WasmF32.reinterpretI32(0b01111111100000000000000000000000n))
WasmI32.toGrain(ptr) : Float32
}

export let infinity = makeInfinity()

@disableGC
let makeNaN = () => {
let ptr = newFloat32(WasmF32.reinterpretI32(0b01111111100000000000000000000001n))
WasmI32.toGrain(ptr) : Float32
}

export let nan = makeNaN()
18 changes: 18 additions & 0 deletions stdlib/float64.gr
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,21 @@ export let gte = (x: Float64, y: Float64) => {
let y = WasmF64.load(WasmI32.fromGrain(y), 8n)
WasmF64.ge(x, y)
}

// floating-point constants:

@disableGC
let makeInfinity = () => {
let ptr = newFloat64(WasmF64.reinterpretI64(0b0111111111110000000000000000000000000000000000000000000000000000N))
WasmI32.toGrain(ptr) : Float64
}

export let infinity = makeInfinity()

@disableGC
let makeNaN = () => {
let ptr = newFloat64(WasmF64.reinterpretI64(0b0111111111110000000000000000000000000000000000000000000000000001N))
WasmI32.toGrain(ptr) : Float64
}

export let nan = makeNaN()

0 comments on commit 4ff3b9f

Please sign in to comment.