authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-06-18 20:32:43-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-06-18 20:32:43-04:00
log5ea0f589c92018b4596ebbbd5e0ce3b71467585c
tree10cbcb71acd74dcdc3e43d944cb07141639d3d9e
parentcaaa26c9f0db92c463a826f15c8392162be2e037
parenteb7fad28f8f11b985c8ee6fbeb4a68345a9b3a5e
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #5625 from antlilja/master

Improve support for f128 and comptime_float operations

10 files changed, 351 insertions(+), 5 deletions(-)

CMakeLists.txt+1
......@@ -288,6 +288,7 @@ set(ZIG_SOURCES
288288 "${CMAKE_SOURCE_DIR}/src/target.cpp"
289289 "${CMAKE_SOURCE_DIR}/src/tokenizer.cpp"
290290 "${CMAKE_SOURCE_DIR}/src/util.cpp"
291 "${CMAKE_SOURCE_DIR}/src/softfloat_ext.cpp"
291292 "${ZIG_SOURCES_MEM_PROFILE}"
292293)
293294set(OPTIMIZED_C_SOURCES
lib/std/math.zig+5
......@@ -122,6 +122,11 @@ pub fn forceEval(value: var) void {
122122 const p = @ptrCast(*volatile f64, &x);
123123 p.* = x;
124124 },
125 f128 => {
126 var x: f128 = undefined;
127 const p = @ptrCast(*volatile f128, &x);
128 p.* = x;
129 },
125130 else => {
126131 @compileError("forceEval not implemented for " ++ @typeName(T));
127132 },
lib/std/math/ceil.zig+43
......@@ -20,6 +20,7 @@ pub fn ceil(x: var) @TypeOf(x) {
2020 return switch (T) {
2121 f32 => ceil32(x),
2222 f64 => ceil64(x),
23 f128 => ceil128(x),
2324 else => @compileError("ceil not implemented for " ++ @typeName(T)),
2425 };
2526}
......@@ -86,9 +87,37 @@ fn ceil64(x: f64) f64 {
8687 }
8788}
8889
90fn 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
89117test "math.ceil" {
90118 expect(ceil(@as(f32, 0.0)) == ceil32(0.0));
91119 expect(ceil(@as(f64, 0.0)) == ceil64(0.0));
120 expect(ceil(@as(f128, 0.0)) == ceil128(0.0));
92121}
93122
94123test "math.ceil32" {
......@@ -103,6 +132,12 @@ test "math.ceil64" {
103132 expect(ceil64(0.2) == 1.0);
104133}
105134
135test "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
106141test "math.ceil32.special" {
107142 expect(ceil32(0.0) == 0.0);
108143 expect(ceil32(-0.0) == -0.0);
......@@ -118,3 +153,11 @@ test "math.ceil64.special" {
118153 expect(math.isNegativeInf(ceil64(-math.inf(f64))));
119154 expect(math.isNan(ceil64(math.nan(f64))));
120155}
156
157test "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) {
2121 f16 => floor16(x),
2222 f32 => floor32(x),
2323 f64 => floor64(x),
24 f128 => floor128(x),
2425 else => @compileError("floor not implemented for " ++ @typeName(T)),
2526 };
2627}
......@@ -122,10 +123,38 @@ fn floor64(x: f64) f64 {
122123 }
123124}
124125
126fn 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
125153test "math.floor" {
126154 expect(floor(@as(f16, 1.3)) == floor16(1.3));
127155 expect(floor(@as(f32, 1.3)) == floor32(1.3));
128156 expect(floor(@as(f64, 1.3)) == floor64(1.3));
157 expect(floor(@as(f128, 1.3)) == floor128(1.3));
129158}
130159
131160test "math.floor16" {
......@@ -146,6 +175,12 @@ test "math.floor64" {
146175 expect(floor64(0.2) == 0.0);
147176}
148177
178test "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
149184test "math.floor16.special" {
150185 expect(floor16(0.0) == 0.0);
151186 expect(floor16(-0.0) == -0.0);
......@@ -169,3 +204,11 @@ test "math.floor64.special" {
169204 expect(math.isNegativeInf(floor64(-math.inf(f64))));
170205 expect(math.isNan(floor64(math.nan(f64))));
171206}
207
208test "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) {
2020 return switch (T) {
2121 f32 => round32(x),
2222 f64 => round64(x),
23 f128 => round128(x),
2324 else => @compileError("round not implemented for " ++ @typeName(T)),
2425 };
2526}
......@@ -90,9 +91,43 @@ fn round64(x_: f64) f64 {
9091 }
9192}
9293
94fn 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
93127test "math.round" {
94128 expect(round(@as(f32, 1.3)) == round32(1.3));
95129 expect(round(@as(f64, 1.3)) == round64(1.3));
130 expect(round(@as(f128, 1.3)) == round128(1.3));
96131}
97132
98133test "math.round32" {
......@@ -109,6 +144,13 @@ test "math.round64" {
109144 expect(round64(1.8) == 2.0);
110145}
111146
147test "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
112154test "math.round32.special" {
113155 expect(round32(0.0) == 0.0);
114156 expect(round32(-0.0) == -0.0);
......@@ -124,3 +166,11 @@ test "math.round64.special" {
124166 expect(math.isNegativeInf(round64(-math.inf(f64))));
125167 expect(math.isNan(round64(math.nan(f64))));
126168}
169
170test "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) {
2020 return switch (T) {
2121 f32 => trunc32(x),
2222 f64 => trunc64(x),
23 f128 => trunc128(x),
2324 else => @compileError("trunc not implemented for " ++ @typeName(T)),
2425 };
2526}
......@@ -66,9 +67,31 @@ fn trunc64(x: f64) f64 {
6667 }
6768}
6869
70fn 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
6991test "math.trunc" {
7092 expect(trunc(@as(f32, 1.3)) == trunc32(1.3));
7193 expect(trunc(@as(f64, 1.3)) == trunc64(1.3));
94 expect(trunc(@as(f128, 1.3)) == trunc128(1.3));
7295}
7396
7497test "math.trunc32" {
......@@ -83,6 +106,12 @@ test "math.trunc64" {
83106 expect(trunc64(0.2) == 0.0);
84107}
85108
109test "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
86115test "math.trunc32.special" {
87116 expect(trunc32(0.0) == 0.0); // 0x3F800000
88117 expect(trunc32(-0.0) == -0.0);
......@@ -98,3 +127,11 @@ test "math.trunc64.special" {
98127 expect(math.isNegativeInf(trunc64(-math.inf(f64))));
99128 expect(math.isNan(trunc64(math.nan(f64))));
100129}
130
131test "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 @@
1313#include "os.hpp"
1414#include "range_set.hpp"
1515#include "softfloat.hpp"
16#include "softfloat_ext.hpp"
1617#include "util.hpp"
1718#include "mem_list.hpp"
1819#include "all_types.hpp"
......@@ -30278,6 +30279,21 @@ static ErrorMsg *ir_eval_float_op(IrAnalyze *ira, IrInst* source_instr, BuiltinF
3027830279 case BuiltinFnIdSqrt:
3027930280 f128M_sqrt(in, out);
3028030281 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;
3028130297 case BuiltinFnIdNearbyInt:
3028230298 case BuiltinFnIdSin:
3028330299 case BuiltinFnIdCos:
......@@ -30286,11 +30302,6 @@ static ErrorMsg *ir_eval_float_op(IrAnalyze *ira, IrInst* source_instr, BuiltinF
3028630302 case BuiltinFnIdLog:
3028730303 case BuiltinFnIdLog10:
3028830304 case BuiltinFnIdLog2:
30289 case BuiltinFnIdFabs:
30290 case BuiltinFnIdFloor:
30291 case BuiltinFnIdCeil:
30292 case BuiltinFnIdTrunc:
30293 case BuiltinFnIdRound:
3029430305 return ir_add_error(ira, source_instr,
3029530306 buf_sprintf("compiler bug: TODO: implement '%s' for type '%s'. See https://github.com/ziglang/zig/issues/4026",
3029630307 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
3extern "C" {
4 #include "softfloat.h"
5}
6
7void 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
17void 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
6void f128M_abs(const float128_t *aPtr, float128_t *zPtr);
7void 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 {
634634 expect(@sqrt(x * x) == x);
635635}
636636
637test "@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
653fn testFabs(comptime T: type, x: T) void {
654 const y = -x;
655 const z = @fabs(y);
656 expectEqual(x, z);
657}
658
659test "@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
676fn testFloor(comptime T: type, x: T) void {
677 const y = x + 0.6;
678 const z = @floor(y);
679 expectEqual(x, z);
680}
681
682test "@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
699fn testCeil(comptime T: type, x: T) void {
700 const y = x - 0.8;
701 const z = @ceil(y);
702 expectEqual(x, z);
703}
704
705test "@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
722fn 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
736test "@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
753fn testRound(comptime T: type, x: T) void {
754 const y = x - 0.5;
755 const z = @round(y);
756 expectEqual(x, z);
757}
758
637759test "comptime_int param and return" {
638760 const a = comptimeAdd(35361831660712422535336160538497375248, 101752735581729509668353361206450473702);
639761 expect(a == 137114567242441932203689521744947848950);