| author | |
| committer | |
| log | 5ea0f589c92018b4596ebbbd5e0ce3b71467585c |
| tree | 10cbcb71acd74dcdc3e43d944cb07141639d3d9e |
| parent | caaa26c9f0db92c463a826f15c8392162be2e037 |
| parent | eb7fad28f8f11b985c8ee6fbeb4a68345a9b3a5e |
| signature |
Improve support for f128 and comptime_float operations10 files changed, 351 insertions(+), 5 deletions(-)
CMakeLists.txt+1| ... | ... | @@ -288,6 +288,7 @@ set(ZIG_SOURCES |
| 288 | 288 | "${CMAKE_SOURCE_DIR}/src/target.cpp" |
| 289 | 289 | "${CMAKE_SOURCE_DIR}/src/tokenizer.cpp" |
| 290 | 290 | "${CMAKE_SOURCE_DIR}/src/util.cpp" |
| 291 | "${CMAKE_SOURCE_DIR}/src/softfloat_ext.cpp" | |
| 291 | 292 | "${ZIG_SOURCES_MEM_PROFILE}" |
| 292 | 293 | ) |
| 293 | 294 | set(OPTIMIZED_C_SOURCES |
lib/std/math.zig+5| ... | ... | @@ -122,6 +122,11 @@ pub fn forceEval(value: var) void { |
| 122 | 122 | const p = @ptrCast(*volatile f64, &x); |
| 123 | 123 | p.* = x; |
| 124 | 124 | }, |
| 125 | f128 => { | |
| 126 | var x: f128 = undefined; | |
| 127 | const p = @ptrCast(*volatile f128, &x); | |
| 128 | p.* = x; | |
| 129 | }, | |
| 125 | 130 | else => { |
| 126 | 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 | 20 | return switch (T) { |
| 21 | 21 | f32 => ceil32(x), |
| 22 | 22 | f64 => ceil64(x), |
| 23 | f128 => ceil128(x), | |
| 23 | 24 | else => @compileError("ceil not implemented for " ++ @typeName(T)), |
| 24 | 25 | }; |
| 25 | 26 | } |
| ... | ... | @@ -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 | 117 | test "math.ceil" { |
| 90 | 118 | expect(ceil(@as(f32, 0.0)) == ceil32(0.0)); |
| 91 | 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 | 123 | test "math.ceil32" { |
| ... | ... | @@ -103,6 +132,12 @@ test "math.ceil64" { |
| 103 | 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 | 141 | test "math.ceil32.special" { |
| 107 | 142 | expect(ceil32(0.0) == 0.0); |
| 108 | 143 | expect(ceil32(-0.0) == -0.0); |
| ... | ... | @@ -118,3 +153,11 @@ test "math.ceil64.special" { |
| 118 | 153 | expect(math.isNegativeInf(ceil64(-math.inf(f64)))); |
| 119 | 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 | 21 | f16 => floor16(x), |
| 22 | 22 | f32 => floor32(x), |
| 23 | 23 | f64 => floor64(x), |
| 24 | f128 => floor128(x), | |
| 24 | 25 | else => @compileError("floor not implemented for " ++ @typeName(T)), |
| 25 | 26 | }; |
| 26 | 27 | } |
| ... | ... | @@ -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 | 153 | test "math.floor" { |
| 126 | 154 | expect(floor(@as(f16, 1.3)) == floor16(1.3)); |
| 127 | 155 | expect(floor(@as(f32, 1.3)) == floor32(1.3)); |
| 128 | 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 | 160 | test "math.floor16" { |
| ... | ... | @@ -146,6 +175,12 @@ test "math.floor64" { |
| 146 | 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 | 184 | test "math.floor16.special" { |
| 150 | 185 | expect(floor16(0.0) == 0.0); |
| 151 | 186 | expect(floor16(-0.0) == -0.0); |
| ... | ... | @@ -169,3 +204,11 @@ test "math.floor64.special" { |
| 169 | 204 | expect(math.isNegativeInf(floor64(-math.inf(f64)))); |
| 170 | 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 | 20 | return switch (T) { |
| 21 | 21 | f32 => round32(x), |
| 22 | 22 | f64 => round64(x), |
| 23 | f128 => round128(x), | |
| 23 | 24 | else => @compileError("round not implemented for " ++ @typeName(T)), |
| 24 | 25 | }; |
| 25 | 26 | } |
| ... | ... | @@ -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 | 127 | test "math.round" { |
| 94 | 128 | expect(round(@as(f32, 1.3)) == round32(1.3)); |
| 95 | 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 | 133 | test "math.round32" { |
| ... | ... | @@ -109,6 +144,13 @@ test "math.round64" { |
| 109 | 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 | 154 | test "math.round32.special" { |
| 113 | 155 | expect(round32(0.0) == 0.0); |
| 114 | 156 | expect(round32(-0.0) == -0.0); |
| ... | ... | @@ -124,3 +166,11 @@ test "math.round64.special" { |
| 124 | 166 | expect(math.isNegativeInf(round64(-math.inf(f64)))); |
| 125 | 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 | 20 | return switch (T) { |
| 21 | 21 | f32 => trunc32(x), |
| 22 | 22 | f64 => trunc64(x), |
| 23 | f128 => trunc128(x), | |
| 23 | 24 | else => @compileError("trunc not implemented for " ++ @typeName(T)), |
| 24 | 25 | }; |
| 25 | 26 | } |
| ... | ... | @@ -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 | 91 | test "math.trunc" { |
| 70 | 92 | expect(trunc(@as(f32, 1.3)) == trunc32(1.3)); |
| 71 | 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 | 97 | test "math.trunc32" { |
| ... | ... | @@ -83,6 +106,12 @@ test "math.trunc64" { |
| 83 | 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 | 115 | test "math.trunc32.special" { |
| 87 | 116 | expect(trunc32(0.0) == 0.0); // 0x3F800000 |
| 88 | 117 | expect(trunc32(-0.0) == -0.0); |
| ... | ... | @@ -98,3 +127,11 @@ test "math.trunc64.special" { |
| 98 | 127 | expect(math.isNegativeInf(trunc64(-math.inf(f64)))); |
| 99 | 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 | } |
src/ir.cpp+16-5| ... | ... | @@ -13,6 +13,7 @@ |
| 13 | 13 | #include "os.hpp" |
| 14 | 14 | #include "range_set.hpp" |
| 15 | 15 | #include "softfloat.hpp" |
| 16 | #include "softfloat_ext.hpp" | |
| 16 | 17 | #include "util.hpp" |
| 17 | 18 | #include "mem_list.hpp" |
| 18 | 19 | #include "all_types.hpp" |
| ... | ... | @@ -30278,6 +30279,21 @@ static ErrorMsg *ir_eval_float_op(IrAnalyze *ira, IrInst* source_instr, BuiltinF |
| 30278 | 30279 | case BuiltinFnIdSqrt: |
| 30279 | 30280 | f128M_sqrt(in, out); |
| 30280 | 30281 | break; |
| 30282 | case BuiltinFnIdFabs: | |
| 30283 | f128M_abs(in, out); | |
| 30284 | break; | |
| 30285 | case BuiltinFnIdFloor: | |
| 30286 | f128M_roundToInt(in, softfloat_round_min, false, out); | |
| 30287 | break; | |
| 30288 | case BuiltinFnIdCeil: | |
| 30289 | f128M_roundToInt(in, softfloat_round_max, false, out); | |
| 30290 | break; | |
| 30291 | case BuiltinFnIdTrunc: | |
| 30292 | f128M_trunc(in, out); | |
| 30293 | break; | |
| 30294 | case BuiltinFnIdRound: | |
| 30295 | f128M_roundToInt(in, softfloat_round_near_maxMag, false, out); | |
| 30296 | break; | |
| 30281 | 30297 | case BuiltinFnIdNearbyInt: |
| 30282 | 30298 | case BuiltinFnIdSin: |
| 30283 | 30299 | case BuiltinFnIdCos: |
| ... | ... | @@ -30286,11 +30302,6 @@ static ErrorMsg *ir_eval_float_op(IrAnalyze *ira, IrInst* source_instr, BuiltinF |
| 30286 | 30302 | case BuiltinFnIdLog: |
| 30287 | 30303 | case BuiltinFnIdLog10: |
| 30288 | 30304 | case BuiltinFnIdLog2: |
| 30289 | case BuiltinFnIdFabs: | |
| 30290 | case BuiltinFnIdFloor: | |
| 30291 | case BuiltinFnIdCeil: | |
| 30292 | case BuiltinFnIdTrunc: | |
| 30293 | case BuiltinFnIdRound: | |
| 30294 | 30305 | return ir_add_error(ira, source_instr, |
| 30295 | 30306 | buf_sprintf("compiler bug: TODO: implement '%s' for type '%s'. See https://github.com/ziglang/zig/issues/4026", |
| 30296 | 30307 | float_op_to_name(fop), buf_ptr(&float_type->name))); |
src/softfloat_ext.cpp created+25| ... | ... | @@ -0,0 +1,25 @@ |
| 1 | #include "softfloat_ext.hpp" | |
| 2 | ||
| 3 | extern "C" { | |
| 4 | #include "softfloat.h" | |
| 5 | } | |
| 6 | ||
| 7 | void f128M_abs(const float128_t *aPtr, float128_t *zPtr) { | |
| 8 | float128_t zero_float; | |
| 9 | ui32_to_f128M(0, &zero_float); | |
| 10 | if (f128M_lt(aPtr, &zero_float)) { | |
| 11 | f128M_sub(&zero_float, aPtr, zPtr); | |
| 12 | } else { | |
| 13 | *zPtr = *aPtr; | |
| 14 | } | |
| 15 | } | |
| 16 | ||
| 17 | void f128M_trunc(const float128_t *aPtr, float128_t *zPtr) { | |
| 18 | float128_t zero_float; | |
| 19 | ui32_to_f128M(0, &zero_float); | |
| 20 | if (f128M_lt(aPtr, &zero_float)) { | |
| 21 | f128M_roundToInt(aPtr, softfloat_round_max, false, zPtr); | |
| 22 | } else { | |
| 23 | f128M_roundToInt(aPtr, softfloat_round_min, false, zPtr); | |
| 24 | } | |
| 25 | } | |
| \ No newline at end of file |
src/softfloat_ext.hpp created+9| ... | ... | @@ -0,0 +1,9 @@ |
| 1 | #ifndef ZIG_SOFTFLOAT_EXT_HPP | |
| 2 | #define ZIG_SOFTFLOAT_EXT_HPP | |
| 3 | ||
| 4 | #include "softfloat_types.h" | |
| 5 | ||
| 6 | void f128M_abs(const float128_t *aPtr, float128_t *zPtr); | |
| 7 | void f128M_trunc(const float128_t *aPtr, float128_t *zPtr); | |
| 8 | ||
| 9 | #endif | |
| \ No newline at end of file |
test/stage1/behavior/math.zig+122| ... | ... | @@ -634,6 +634,128 @@ fn testSqrt(comptime T: type, x: T) void { |
| 634 | 634 | expect(@sqrt(x * x) == x); |
| 635 | 635 | } |
| 636 | 636 | |
| 637 | test "@fabs" { | |
| 638 | testFabs(f128, 12.0); | |
| 639 | comptime testFabs(f128, 12.0); | |
| 640 | testFabs(f64, 12.0); | |
| 641 | comptime testFabs(f64, 12.0); | |
| 642 | testFabs(f32, 12.0); | |
| 643 | comptime testFabs(f32, 12.0); | |
| 644 | testFabs(f16, 12.0); | |
| 645 | comptime testFabs(f16, 12.0); | |
| 646 | ||
| 647 | const x = 14.0; | |
| 648 | const y = -x; | |
| 649 | const z = @fabs(y); | |
| 650 | comptime expectEqual(x, z); | |
| 651 | } | |
| 652 | ||
| 653 | fn testFabs(comptime T: type, x: T) void { | |
| 654 | const y = -x; | |
| 655 | const z = @fabs(y); | |
| 656 | expectEqual(x, z); | |
| 657 | } | |
| 658 | ||
| 659 | test "@floor" { | |
| 660 | // FIXME: Generates a floorl function call | |
| 661 | // testFloor(f128, 12.0); | |
| 662 | comptime testFloor(f128, 12.0); | |
| 663 | testFloor(f64, 12.0); | |
| 664 | comptime testFloor(f64, 12.0); | |
| 665 | testFloor(f32, 12.0); | |
| 666 | comptime testFloor(f32, 12.0); | |
| 667 | testFloor(f16, 12.0); | |
| 668 | comptime testFloor(f16, 12.0); | |
| 669 | ||
| 670 | const x = 14.0; | |
| 671 | const y = x + 0.7; | |
| 672 | const z = @floor(y); | |
| 673 | comptime expectEqual(x, z); | |
| 674 | } | |
| 675 | ||
| 676 | fn testFloor(comptime T: type, x: T) void { | |
| 677 | const y = x + 0.6; | |
| 678 | const z = @floor(y); | |
| 679 | expectEqual(x, z); | |
| 680 | } | |
| 681 | ||
| 682 | test "@ceil" { | |
| 683 | // FIXME: Generates a ceill function call | |
| 684 | //testCeil(f128, 12.0); | |
| 685 | comptime testCeil(f128, 12.0); | |
| 686 | testCeil(f64, 12.0); | |
| 687 | comptime testCeil(f64, 12.0); | |
| 688 | testCeil(f32, 12.0); | |
| 689 | comptime testCeil(f32, 12.0); | |
| 690 | testCeil(f16, 12.0); | |
| 691 | comptime testCeil(f16, 12.0); | |
| 692 | ||
| 693 | const x = 14.0; | |
| 694 | const y = x - 0.7; | |
| 695 | const z = @ceil(y); | |
| 696 | comptime expectEqual(x, z); | |
| 697 | } | |
| 698 | ||
| 699 | fn testCeil(comptime T: type, x: T) void { | |
| 700 | const y = x - 0.8; | |
| 701 | const z = @ceil(y); | |
| 702 | expectEqual(x, z); | |
| 703 | } | |
| 704 | ||
| 705 | test "@trunc" { | |
| 706 | // FIXME: Generates a truncl function call | |
| 707 | //testTrunc(f128, 12.0); | |
| 708 | comptime testTrunc(f128, 12.0); | |
| 709 | testTrunc(f64, 12.0); | |
| 710 | comptime testTrunc(f64, 12.0); | |
| 711 | testTrunc(f32, 12.0); | |
| 712 | comptime testTrunc(f32, 12.0); | |
| 713 | testTrunc(f16, 12.0); | |
| 714 | comptime testTrunc(f16, 12.0); | |
| 715 | ||
| 716 | const x = 14.0; | |
| 717 | const y = x + 0.7; | |
| 718 | const z = @trunc(y); | |
| 719 | comptime expectEqual(x, z); | |
| 720 | } | |
| 721 | ||
| 722 | fn testTrunc(comptime T: type, x: T) void { | |
| 723 | { | |
| 724 | const y = x + 0.8; | |
| 725 | const z = @trunc(y); | |
| 726 | expectEqual(x, z); | |
| 727 | } | |
| 728 | ||
| 729 | { | |
| 730 | const y = -x - 0.8; | |
| 731 | const z = @trunc(y); | |
| 732 | expectEqual(-x, z); | |
| 733 | } | |
| 734 | } | |
| 735 | ||
| 736 | test "@round" { | |
| 737 | // FIXME: Generates a roundl function call | |
| 738 | //testRound(f128, 12.0); | |
| 739 | comptime testRound(f128, 12.0); | |
| 740 | testRound(f64, 12.0); | |
| 741 | comptime testRound(f64, 12.0); | |
| 742 | testRound(f32, 12.0); | |
| 743 | comptime testRound(f32, 12.0); | |
| 744 | testRound(f16, 12.0); | |
| 745 | comptime testRound(f16, 12.0); | |
| 746 | ||
| 747 | const x = 14.0; | |
| 748 | const y = x + 0.4; | |
| 749 | const z = @round(y); | |
| 750 | comptime expectEqual(x, z); | |
| 751 | } | |
| 752 | ||
| 753 | fn testRound(comptime T: type, x: T) void { | |
| 754 | const y = x - 0.5; | |
| 755 | const z = @round(y); | |
| 756 | expectEqual(x, z); | |
| 757 | } | |
| 758 | ||
| 637 | 759 | test "comptime_int param and return" { |
| 638 | 760 | const a = comptimeAdd(35361831660712422535336160538497375248, 101752735581729509668353361206450473702); |
| 639 | 761 | expect(a == 137114567242441932203689521744947848950); |