Skip to content

Commit 76ed20a

Browse files
committed
min/max
1 parent 2d5f14b commit 76ed20a

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

common/numbers.rv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export {
22
Number, Real, Float32, Float64, Complex, real, imag
33
==, !=, !, +, -, *, /, ^, >, <, >=, <=, &, |, abs2, div, rem
4-
zero?, zero, one, round
4+
zero?, zero, one, round, min, max
55
}
66

77
fn matchTrait(tag"common.core.Float32", x: pack(tag"common.core.Float32", _)) { Some(x) }

common/promote.rv

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,23 @@ fn (a: Real) <= (b: Real) {
6363
[a, b] = promote(a, b)
6464
return a <= b
6565
}
66+
67+
fn min(a: Real, b: Real) {
68+
if tag(a) == tag(b) {
69+
[a, b] = promote(a, b)
70+
if a < b { a } else { b }
71+
} else {
72+
[a, b] = promote(a, b)
73+
min(a, b)
74+
}
75+
}
76+
77+
fn max(a: Real, b: Real) {
78+
if tag(a) == tag(b) {
79+
[a, b] = promote(a, b)
80+
if a > b { a } else { b }
81+
} else {
82+
[a, b] = promote(a, b)
83+
max(a, b)
84+
}
85+
}

common/wasm/numeric.rv

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ fn (a: Float32) == (b: Float32) {
4444
Bool(wasm f32.eq(a: f32, b: f32): i32)
4545
}
4646

47+
fn min(a: Float64, b: Float64) {
48+
wasm f64.min(a: f64, b: f64): f64
49+
}
50+
51+
fn max(a: Float64, b: Float64) {
52+
wasm f64.max(a: f64, b: f64): f64
53+
}
54+
4755
fn i64trunc(x: Float64) {
4856
Int64(wasm i64.trunc_f64_s(x: f64): i64)
4957
}

0 commit comments

Comments
 (0)