authorgravatar for liljaanton2001@gmail.comantlilja <liljaanton2001@gmail.com> 2020-06-17 18:18:45+02:00
committergravatar for liljaanton2001@gmail.comantlilja <liljaanton2001@gmail.com> 2020-06-17 18:18:45+02:00
logeb7fad28f8f11b985c8ee6fbeb4a68345a9b3a5e
tree078b19ddf21e6d3901ff614d87a03b48c2a5315c
parent1157ee130732449811294d70021aaafa588d3048

Improve f128 standard library support

* Add functions: floor128, ceil128, trunc128 and round128 * Add corresponding tests

5 files changed, 178 insertions(+), 0 deletions(-)

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}