authorgravatar for marc@tiehu.isMarc Tiehuis <marc@tiehu.is> 2018-06-21 17:13:53+12:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-06-21 01:45:12-04:00
logf50c0c664f3bd7a56c0274ffecae28e2b41c8d20
tree55eed8ceab3ce0a97715eba808e65685ccc84f85
parenteb6a8e6a3bba061c4a7fb18f53976e1fb683c3d4

Add float repr bit extraction functions


1 files changed, 26 insertions(+), 1 deletions(-)

std/math/index.zig+26-1
...@@ -183,6 +183,32 @@ test "math" {...@@ -183,6 +183,32 @@ test "math" {
183 _ = @import("big/index.zig");183 _ = @import("big/index.zig");
184}184}
185185
186pub fn floatMantissaBits(comptime T: type) comptime_int {
187 assert(@typeId(T) == builtin.TypeId.Float);
188
189 return switch (T.bit_count) {
190 16 => 10,
191 32 => 23,
192 64 => 52,
193 80 => 64,
194 128 => 112,
195 else => @compileError("unknown floating point type " ++ @typeName(T)),
196 };
197}
198
199pub fn floatExponentBits(comptime T: type) comptime_int {
200 assert(@typeId(T) == builtin.TypeId.Float);
201
202 return switch (T.bit_count) {
203 16 => 5,
204 32 => 8,
205 64 => 11,
206 80 => 15,
207 128 => 15,
208 else => @compileError("unknown floating point type " ++ @typeName(T)),
209 };
210}
211
186pub fn min(x: var, y: var) @typeOf(x + y) {212pub fn min(x: var, y: var) @typeOf(x + y) {
187 return if (x < y) x else y;213 return if (x < y) x else y;
188}214}
...@@ -607,4 +633,3 @@ pub fn lossyCast(comptime T: type, value: var) T {...@@ -607,4 +633,3 @@ pub fn lossyCast(comptime T: type, value: var) T {
607 else => @compileError("bad type"),633 else => @compileError("bad type"),
608 }634 }
609}635}
610