| author | |
| committer | |
| log | eb7fad28f8f11b985c8ee6fbeb4a68345a9b3a5e |
| tree | 078b19ddf21e6d3901ff614d87a03b48c2a5315c |
| parent | 1157ee130732449811294d70021aaafa588d3048 |
* Add functions: floor128, ceil128, trunc128 and round128
* Add corresponding tests5 files changed, 178 insertions(+), 0 deletions(-)
lib/std/math.zig+5| ... | @@ -122,6 +122,11 @@ pub fn forceEval(value: var) void { | ... | @@ -122,6 +122,11 @@ pub fn forceEval(value: var) void { |
| 122 | const p = @ptrCast(*volatile f64, &x); | 122 | const p = @ptrCast(*volatile f64, &x); |
| 123 | p.* = x; | 123 | p.* = x; |
| 124 | }, | 124 | }, |
| 125 | f128 => { | ||
| 126 | var x: f128 = undefined; | ||
| 127 | const p = @ptrCast(*volatile f128, &x); | ||
| 128 | p.* = x; | ||
| 129 | }, | ||
| 125 | else => { | 130 | else => { |
| 126 | @compileError("forceEval not implemented for " ++ @typeName(T)); | 131 | @compileError("forceEval not implemented for " ++ @typeName(T)); |
| 127 | }, | 132 | }, |
lib/std/math/ceil.zig+43| ... | @@ -20,6 +20,7 @@ pub fn ceil(x: var) @TypeOf(x) { | ... | @@ -20,6 +20,7 @@ pub fn ceil(x: var) @TypeOf(x) { |
| 20 | return switch (T) { | 20 | return switch (T) { |
| 21 | f32 => ceil32(x), | 21 | f32 => ceil32(x), |
| 22 | f64 => ceil64(x), | 22 | f64 => ceil64(x), |
| 23 | f128 => ceil128(x), | ||
| 23 | else => @compileError("ceil not implemented for " ++ @typeName(T)), | 24 | else => @compileError("ceil not implemented for " ++ @typeName(T)), |
| 24 | }; | 25 | }; |
| 25 | } | 26 | } |
| ... | @@ -86,9 +87,37 @@ fn ceil64(x: f64) f64 { | ... | @@ -86,9 +87,37 @@ fn ceil64(x: f64) f64 { |
| 86 | } | 87 | } |
| 87 | } | 88 | } |
| 88 | 89 | ||
| 90 | fn ceil128(x: f128) f128 { | ||
| 91 | const u = @bitCast(u128, x); | ||
| 92 | const e = (u >> 112) & 0x7FFF; | ||
| 93 | var y: f128 = undefined; | ||
| 94 | |||
| 95 | if (e >= 0x3FFF + 112 or x == 0) return x; | ||
| 96 | |||
| 97 | if (u >> 127 != 0) { | ||
| 98 | y = x - math.f128_toint + math.f128_toint - x; | ||
| 99 | } else { | ||
| 100 | y = x + math.f128_toint - math.f128_toint - x; | ||
| 101 | } | ||
| 102 | |||
| 103 | if (e <= 0x3FFF - 1) { | ||
| 104 | math.forceEval(y); | ||
| 105 | if (u >> 127 != 0) { | ||
| 106 | return -0.0; | ||
| 107 | } else { | ||
| 108 | return 1.0; | ||
| 109 | } | ||
| 110 | } else if (y < 0) { | ||
| 111 | return x + y + 1; | ||
| 112 | } else { | ||
| 113 | return x + y; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 89 | test "math.ceil" { | 117 | test "math.ceil" { |
| 90 | expect(ceil(@as(f32, 0.0)) == ceil32(0.0)); | 118 | expect(ceil(@as(f32, 0.0)) == ceil32(0.0)); |
| 91 | expect(ceil(@as(f64, 0.0)) == ceil64(0.0)); | 119 | expect(ceil(@as(f64, 0.0)) == ceil64(0.0)); |
| 120 | expect(ceil(@as(f128, 0.0)) == ceil128(0.0)); | ||
| 92 | } | 121 | } |
| 93 | 122 | ||
| 94 | test "math.ceil32" { | 123 | test "math.ceil32" { |
| ... | @@ -103,6 +132,12 @@ test "math.ceil64" { | ... | @@ -103,6 +132,12 @@ test "math.ceil64" { |
| 103 | expect(ceil64(0.2) == 1.0); | 132 | expect(ceil64(0.2) == 1.0); |
| 104 | } | 133 | } |
| 105 | 134 | ||
| 135 | test "math.ceil128" { | ||
| 136 | expect(ceil128(1.3) == 2.0); | ||
| 137 | expect(ceil128(-1.3) == -1.0); | ||
| 138 | expect(ceil128(0.2) == 1.0); | ||
| 139 | } | ||
| 140 | |||
| 106 | test "math.ceil32.special" { | 141 | test "math.ceil32.special" { |
| 107 | expect(ceil32(0.0) == 0.0); | 142 | expect(ceil32(0.0) == 0.0); |
| 108 | expect(ceil32(-0.0) == -0.0); | 143 | expect(ceil32(-0.0) == -0.0); |
| ... | @@ -118,3 +153,11 @@ test "math.ceil64.special" { | ... | @@ -118,3 +153,11 @@ test "math.ceil64.special" { |
| 118 | expect(math.isNegativeInf(ceil64(-math.inf(f64)))); | 153 | expect(math.isNegativeInf(ceil64(-math.inf(f64)))); |
| 119 | expect(math.isNan(ceil64(math.nan(f64)))); | 154 | expect(math.isNan(ceil64(math.nan(f64)))); |
| 120 | } | 155 | } |
| 156 | |||
| 157 | test "math.ceil128.special" { | ||
| 158 | expect(ceil128(0.0) == 0.0); | ||
| 159 | expect(ceil128(-0.0) == -0.0); | ||
| 160 | expect(math.isPositiveInf(ceil128(math.inf(f128)))); | ||
| 161 | expect(math.isNegativeInf(ceil128(-math.inf(f128)))); | ||
| 162 | expect(math.isNan(ceil128(math.nan(f128)))); | ||
| 163 | } |
lib/std/math/floor.zig+43| ... | @@ -21,6 +21,7 @@ pub fn floor(x: var) @TypeOf(x) { | ... | @@ -21,6 +21,7 @@ pub fn floor(x: var) @TypeOf(x) { |
| 21 | f16 => floor16(x), | 21 | f16 => floor16(x), |
| 22 | f32 => floor32(x), | 22 | f32 => floor32(x), |
| 23 | f64 => floor64(x), | 23 | f64 => floor64(x), |
| 24 | f128 => floor128(x), | ||
| 24 | else => @compileError("floor not implemented for " ++ @typeName(T)), | 25 | else => @compileError("floor not implemented for " ++ @typeName(T)), |
| 25 | }; | 26 | }; |
| 26 | } | 27 | } |
| ... | @@ -122,10 +123,38 @@ fn floor64(x: f64) f64 { | ... | @@ -122,10 +123,38 @@ fn floor64(x: f64) f64 { |
| 122 | } | 123 | } |
| 123 | } | 124 | } |
| 124 | 125 | ||
| 126 | fn floor128(x: f128) f128 { | ||
| 127 | const u = @bitCast(u128, x); | ||
| 128 | const e = (u >> 112) & 0x7FFF; | ||
| 129 | var y: f128 = undefined; | ||
| 130 | |||
| 131 | if (e >= 0x3FFF + 112 or x == 0) return x; | ||
| 132 | |||
| 133 | if (u >> 127 != 0) { | ||
| 134 | y = x - math.f128_toint + math.f128_toint - x; | ||
| 135 | } else { | ||
| 136 | y = x + math.f128_toint - math.f128_toint - x; | ||
| 137 | } | ||
| 138 | |||
| 139 | if (e <= 0x3FFF - 1) { | ||
| 140 | math.forceEval(y); | ||
| 141 | if (u >> 127 != 0) { | ||
| 142 | return -1.0; | ||
| 143 | } else { | ||
| 144 | return 0.0; | ||
| 145 | } | ||
| 146 | } else if (y > 0) { | ||
| 147 | return x + y - 1; | ||
| 148 | } else { | ||
| 149 | return x + y; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 125 | test "math.floor" { | 153 | test "math.floor" { |
| 126 | expect(floor(@as(f16, 1.3)) == floor16(1.3)); | 154 | expect(floor(@as(f16, 1.3)) == floor16(1.3)); |
| 127 | expect(floor(@as(f32, 1.3)) == floor32(1.3)); | 155 | expect(floor(@as(f32, 1.3)) == floor32(1.3)); |
| 128 | expect(floor(@as(f64, 1.3)) == floor64(1.3)); | 156 | expect(floor(@as(f64, 1.3)) == floor64(1.3)); |
| 157 | expect(floor(@as(f128, 1.3)) == floor128(1.3)); | ||
| 129 | } | 158 | } |
| 130 | 159 | ||
| 131 | test "math.floor16" { | 160 | test "math.floor16" { |
| ... | @@ -146,6 +175,12 @@ test "math.floor64" { | ... | @@ -146,6 +175,12 @@ test "math.floor64" { |
| 146 | expect(floor64(0.2) == 0.0); | 175 | expect(floor64(0.2) == 0.0); |
| 147 | } | 176 | } |
| 148 | 177 | ||
| 178 | test "math.floor128" { | ||
| 179 | expect(floor128(1.3) == 1.0); | ||
| 180 | expect(floor128(-1.3) == -2.0); | ||
| 181 | expect(floor128(0.2) == 0.0); | ||
| 182 | } | ||
| 183 | |||
| 149 | test "math.floor16.special" { | 184 | test "math.floor16.special" { |
| 150 | expect(floor16(0.0) == 0.0); | 185 | expect(floor16(0.0) == 0.0); |
| 151 | expect(floor16(-0.0) == -0.0); | 186 | expect(floor16(-0.0) == -0.0); |
| ... | @@ -169,3 +204,11 @@ test "math.floor64.special" { | ... | @@ -169,3 +204,11 @@ test "math.floor64.special" { |
| 169 | expect(math.isNegativeInf(floor64(-math.inf(f64)))); | 204 | expect(math.isNegativeInf(floor64(-math.inf(f64)))); |
| 170 | expect(math.isNan(floor64(math.nan(f64)))); | 205 | expect(math.isNan(floor64(math.nan(f64)))); |
| 171 | } | 206 | } |
| 207 | |||
| 208 | test "math.floor128.special" { | ||
| 209 | expect(floor128(0.0) == 0.0); | ||
| 210 | expect(floor128(-0.0) == -0.0); | ||
| 211 | expect(math.isPositiveInf(floor128(math.inf(f128)))); | ||
| 212 | expect(math.isNegativeInf(floor128(-math.inf(f128)))); | ||
| 213 | expect(math.isNan(floor128(math.nan(f128)))); | ||
| 214 | } |
lib/std/math/round.zig+50| ... | @@ -20,6 +20,7 @@ pub fn round(x: var) @TypeOf(x) { | ... | @@ -20,6 +20,7 @@ pub fn round(x: var) @TypeOf(x) { |
| 20 | return switch (T) { | 20 | return switch (T) { |
| 21 | f32 => round32(x), | 21 | f32 => round32(x), |
| 22 | f64 => round64(x), | 22 | f64 => round64(x), |
| 23 | f128 => round128(x), | ||
| 23 | else => @compileError("round not implemented for " ++ @typeName(T)), | 24 | else => @compileError("round not implemented for " ++ @typeName(T)), |
| 24 | }; | 25 | }; |
| 25 | } | 26 | } |
| ... | @@ -90,9 +91,43 @@ fn round64(x_: f64) f64 { | ... | @@ -90,9 +91,43 @@ fn round64(x_: f64) f64 { |
| 90 | } | 91 | } |
| 91 | } | 92 | } |
| 92 | 93 | ||
| 94 | fn round128(x_: f128) f128 { | ||
| 95 | var x = x_; | ||
| 96 | const u = @bitCast(u128, x); | ||
| 97 | const e = (u >> 112) & 0x7FFF; | ||
| 98 | var y: f128 = undefined; | ||
| 99 | |||
| 100 | if (e >= 0x3FFF + 112) { | ||
| 101 | return x; | ||
| 102 | } | ||
| 103 | if (u >> 127 != 0) { | ||
| 104 | x = -x; | ||
| 105 | } | ||
| 106 | if (e < 0x3FFF - 1) { | ||
| 107 | math.forceEval(x + math.f64_toint); | ||
| 108 | return 0 * @bitCast(f128, u); | ||
| 109 | } | ||
| 110 | |||
| 111 | y = x + math.f128_toint - math.f128_toint - x; | ||
| 112 | if (y > 0.5) { | ||
| 113 | y = y + x - 1; | ||
| 114 | } else if (y <= -0.5) { | ||
| 115 | y = y + x + 1; | ||
| 116 | } else { | ||
| 117 | y = y + x; | ||
| 118 | } | ||
| 119 | |||
| 120 | if (u >> 127 != 0) { | ||
| 121 | return -y; | ||
| 122 | } else { | ||
| 123 | return y; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 93 | test "math.round" { | 127 | test "math.round" { |
| 94 | expect(round(@as(f32, 1.3)) == round32(1.3)); | 128 | expect(round(@as(f32, 1.3)) == round32(1.3)); |
| 95 | expect(round(@as(f64, 1.3)) == round64(1.3)); | 129 | expect(round(@as(f64, 1.3)) == round64(1.3)); |
| 130 | expect(round(@as(f128, 1.3)) == round128(1.3)); | ||
| 96 | } | 131 | } |
| 97 | 132 | ||
| 98 | test "math.round32" { | 133 | test "math.round32" { |
| ... | @@ -109,6 +144,13 @@ test "math.round64" { | ... | @@ -109,6 +144,13 @@ test "math.round64" { |
| 109 | expect(round64(1.8) == 2.0); | 144 | expect(round64(1.8) == 2.0); |
| 110 | } | 145 | } |
| 111 | 146 | ||
| 147 | test "math.round128" { | ||
| 148 | expect(round128(1.3) == 1.0); | ||
| 149 | expect(round128(-1.3) == -1.0); | ||
| 150 | expect(round128(0.2) == 0.0); | ||
| 151 | expect(round128(1.8) == 2.0); | ||
| 152 | } | ||
| 153 | |||
| 112 | test "math.round32.special" { | 154 | test "math.round32.special" { |
| 113 | expect(round32(0.0) == 0.0); | 155 | expect(round32(0.0) == 0.0); |
| 114 | expect(round32(-0.0) == -0.0); | 156 | expect(round32(-0.0) == -0.0); |
| ... | @@ -124,3 +166,11 @@ test "math.round64.special" { | ... | @@ -124,3 +166,11 @@ test "math.round64.special" { |
| 124 | expect(math.isNegativeInf(round64(-math.inf(f64)))); | 166 | expect(math.isNegativeInf(round64(-math.inf(f64)))); |
| 125 | expect(math.isNan(round64(math.nan(f64)))); | 167 | expect(math.isNan(round64(math.nan(f64)))); |
| 126 | } | 168 | } |
| 169 | |||
| 170 | test "math.round128.special" { | ||
| 171 | expect(round128(0.0) == 0.0); | ||
| 172 | expect(round128(-0.0) == -0.0); | ||
| 173 | expect(math.isPositiveInf(round128(math.inf(f128)))); | ||
| 174 | expect(math.isNegativeInf(round128(-math.inf(f128)))); | ||
| 175 | expect(math.isNan(round128(math.nan(f128)))); | ||
| 176 | } |
lib/std/math/trunc.zig+37| ... | @@ -20,6 +20,7 @@ pub fn trunc(x: var) @TypeOf(x) { | ... | @@ -20,6 +20,7 @@ pub fn trunc(x: var) @TypeOf(x) { |
| 20 | return switch (T) { | 20 | return switch (T) { |
| 21 | f32 => trunc32(x), | 21 | f32 => trunc32(x), |
| 22 | f64 => trunc64(x), | 22 | f64 => trunc64(x), |
| 23 | f128 => trunc128(x), | ||
| 23 | else => @compileError("trunc not implemented for " ++ @typeName(T)), | 24 | else => @compileError("trunc not implemented for " ++ @typeName(T)), |
| 24 | }; | 25 | }; |
| 25 | } | 26 | } |
| ... | @@ -66,9 +67,31 @@ fn trunc64(x: f64) f64 { | ... | @@ -66,9 +67,31 @@ fn trunc64(x: f64) f64 { |
| 66 | } | 67 | } |
| 67 | } | 68 | } |
| 68 | 69 | ||
| 70 | fn trunc128(x: f128) f128 { | ||
| 71 | const u = @bitCast(u128, x); | ||
| 72 | var e = @intCast(i32, ((u >> 112) & 0x7FFF)) - 0x3FFF + 16; | ||
| 73 | var m: u128 = undefined; | ||
| 74 | |||
| 75 | if (e >= 112 + 16) { | ||
| 76 | return x; | ||
| 77 | } | ||
| 78 | if (e < 16) { | ||
| 79 | e = 1; | ||
| 80 | } | ||
| 81 | |||
| 82 | m = @as(u128, maxInt(u128)) >> @intCast(u7, e); | ||
| 83 | if (u & m == 0) { | ||
| 84 | return x; | ||
| 85 | } else { | ||
| 86 | math.forceEval(x + 0x1p120); | ||
| 87 | return @bitCast(f128, u & ~m); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 69 | test "math.trunc" { | 91 | test "math.trunc" { |
| 70 | expect(trunc(@as(f32, 1.3)) == trunc32(1.3)); | 92 | expect(trunc(@as(f32, 1.3)) == trunc32(1.3)); |
| 71 | expect(trunc(@as(f64, 1.3)) == trunc64(1.3)); | 93 | expect(trunc(@as(f64, 1.3)) == trunc64(1.3)); |
| 94 | expect(trunc(@as(f128, 1.3)) == trunc128(1.3)); | ||
| 72 | } | 95 | } |
| 73 | 96 | ||
| 74 | test "math.trunc32" { | 97 | test "math.trunc32" { |
| ... | @@ -83,6 +106,12 @@ test "math.trunc64" { | ... | @@ -83,6 +106,12 @@ test "math.trunc64" { |
| 83 | expect(trunc64(0.2) == 0.0); | 106 | expect(trunc64(0.2) == 0.0); |
| 84 | } | 107 | } |
| 85 | 108 | ||
| 109 | test "math.trunc128" { | ||
| 110 | expect(trunc128(1.3) == 1.0); | ||
| 111 | expect(trunc128(-1.3) == -1.0); | ||
| 112 | expect(trunc128(0.2) == 0.0); | ||
| 113 | } | ||
| 114 | |||
| 86 | test "math.trunc32.special" { | 115 | test "math.trunc32.special" { |
| 87 | expect(trunc32(0.0) == 0.0); // 0x3F800000 | 116 | expect(trunc32(0.0) == 0.0); // 0x3F800000 |
| 88 | expect(trunc32(-0.0) == -0.0); | 117 | expect(trunc32(-0.0) == -0.0); |
| ... | @@ -98,3 +127,11 @@ test "math.trunc64.special" { | ... | @@ -98,3 +127,11 @@ test "math.trunc64.special" { |
| 98 | expect(math.isNegativeInf(trunc64(-math.inf(f64)))); | 127 | expect(math.isNegativeInf(trunc64(-math.inf(f64)))); |
| 99 | expect(math.isNan(trunc64(math.nan(f64)))); | 128 | expect(math.isNan(trunc64(math.nan(f64)))); |
| 100 | } | 129 | } |
| 130 | |||
| 131 | test "math.trunc128.special" { | ||
| 132 | expect(trunc128(0.0) == 0.0); | ||
| 133 | expect(trunc128(-0.0) == -0.0); | ||
| 134 | expect(math.isPositiveInf(trunc128(math.inf(f128)))); | ||
| 135 | expect(math.isNegativeInf(trunc128(-math.inf(f128)))); | ||
| 136 | expect(math.isNan(trunc128(math.nan(f128)))); | ||
| 137 | } |