Zig 0.17.0-dev (Split by item)
This is an example of documentation generated by
ZigDoc , an alternative to Zig's built-in
Auto Doc feature. See also
examples in other modes/formats . The project being documented here (as the example) is the Zig library itself.
Zig › compiler_rt/ › int_from_float.zig › intFromFloat
intFromFloat
int_from_float.intFromFloat
pub inline fn intFromFloat (comptime I : type , a : anytype ) I
File
Code
pub inline fn intFromFloat (comptime I : type , a : anytype ) I {
const F = @TypeOf (a );
const float_bits = @typeInfo (F ).float .bits ;
const int_bits = @typeInfo (I ).int .bits ;
const rep_t = @Int (.unsigned , float_bits );
const sig_bits = math .floatMantissaBits (F );
const exp_bits = math .floatExponentBits (F );
const fractional_bits = math .floatFractionalBits (F );
const implicit_bit = if (F != f80 ) (@as (rep_t , 1 ) << sig_bits ) else 0 ;
const max_exp = (1 << (exp_bits - 1 ));
const exp_bias = max_exp - 1 ;
const sig_mask = (@as (rep_t , 1 ) << sig_bits ) - 1 ;
const a_rep : rep_t = @bitCast (a );
const negative = (a_rep >> (float_bits - 1 )) != 0 ;
const exponent = @as (i32 , @intCast ((a_rep << 1 ) >> (sig_bits + 1 ))) - exp_bias ;
const significand : rep_t = (a_rep & sig_mask ) | implicit_bit ;
if (exponent < 0 ) return 0 ;
switch (@typeInfo (I ).int .signedness ) {
.unsigned => {
if (negative ) return 0 ;
if (@as (c_uint , @intCast (exponent )) >= @min (int_bits , max_exp )) return math .maxInt (I );
},
.signed => if (@as (c_uint , @intCast (exponent )) >= @min (int_bits - 1 , max_exp )) {
return if (negative ) math .minInt (I ) else math .maxInt (I );
},
}
// Otherwise, shift left.
var result : I = undefined ;
if (exponent < fractional_bits ) {
result = @intCast (significand >> @intCast (fractional_bits - exponent ));
} else {
result = @as (I , @intCast (significand )) << @intCast (exponent - fractional_bits );
}
if ((@typeInfo (I ).int .signedness == .signed ) and negative )
return ~result +% 1 ;
return result ;
}