Skip to content

Commit

Permalink
Add float repr bit extraction functions
Browse files Browse the repository at this point in the history
  • Loading branch information
tiehuis authored and andrewrk committed Jun 21, 2018
1 parent eb6a8e6 commit f50c0c6
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion std/math/index.zig
Expand Up @@ -183,6 +183,32 @@ test "math" {
_ = @import("big/index.zig");
}

pub fn floatMantissaBits(comptime T: type) comptime_int {
assert(@typeId(T) == builtin.TypeId.Float);

return switch (T.bit_count) {
16 => 10,
32 => 23,
64 => 52,
80 => 64,
128 => 112,
else => @compileError("unknown floating point type " ++ @typeName(T)),
};
}

pub fn floatExponentBits(comptime T: type) comptime_int {
assert(@typeId(T) == builtin.TypeId.Float);

return switch (T.bit_count) {
16 => 5,
32 => 8,
64 => 11,
80 => 15,
128 => 15,
else => @compileError("unknown floating point type " ++ @typeName(T)),
};
}

pub fn min(x: var, y: var) @typeOf(x + y) {
return if (x < y) x else y;
}
Expand Down Expand Up @@ -607,4 +633,3 @@ pub fn lossyCast(comptime T: type, value: var) T {
else => @compileError("bad type"),
}
}

0 comments on commit f50c0c6

Please sign in to comment.