Skip to content

Commit f50c0c6

Browse files
tiehuisandrewrk
authored andcommittedJun 21, 2018
Add float repr bit extraction functions
·
0.15.20.3.0
1 parent eb6a8e6 commit f50c0c6

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed
 

‎std/math/index.zig‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,32 @@ test "math" {
183183
_ = @import("big/index.zig");
184184
}
185185

186+
pub 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+
199+
pub 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+
186212
pub fn min(x: var, y: var) @typeOf(x + y) {
187213
return if (x < y) x else y;
188214
}
@@ -607,4 +633,3 @@ pub fn lossyCast(comptime T: type, value: var) T {
607633
else => @compileError("bad type"),
608634
}
609635
}
610-

0 commit comments

Comments
 (0)
Please sign in to comment.