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/ › udivmod.zig › divwide_generic
divwide_generic
udivmod.divwide_generic
fn divwide_generic (comptime T : type , _u1 : T , _u0 : T , v_ : T , r : *T ) T
File
Code
fn divwide_generic (comptime T : type , _u1 : T , _u0 : T , v_ : T , r : *T ) T {
const HalfT = HalveInt (T , false ).HalfT ;
@setRuntimeSafety (compiler_rt .test_safety );
var v = v_ ;
const b = @as (T , 1 ) << (@bitSizeOf (T ) / 2 );
var un64 : T = undefined ;
var un10 : T = undefined ;
const s : Log2Int (T ) = @intCast (@clz (v ));
if (s > 0 ) {
v <<= s ;
un64 = (_u1 << s ) | (_u0 >> @intCast ((@bitSizeOf (T ) - @as (T , @intCast (s )))));
un10 = _u0 << s ;
} else {
un64 = _u1 ;
un10 = _u0 ;
}
const vn1 = v >> (@bitSizeOf (T ) / 2 );
const vn0 = v & std .math .maxInt (HalfT );
const un1 = un10 >> (@bitSizeOf (T ) / 2 );
const un0 = un10 & std .math .maxInt (HalfT );
var q1 = un64 / vn1 ;
var rhat = un64 -% q1 *% vn1 ;
while (q1 >= b or q1 * vn0 > b * rhat + un1 ) {
q1 -= 1 ;
rhat += vn1 ;
if (rhat >= b ) break ;
}
const un21 = un64 *% b +% un1 -% q1 *% v ;
var q0 = un21 / vn1 ;
rhat = un21 -% q0 *% vn1 ;
while (q0 >= b or q0 * vn0 > b * rhat + un0 ) {
q0 -= 1 ;
rhat += vn1 ;
if (rhat >= b ) break ;
}
r .* = (un21 *% b +% un0 -% q0 *% v ) >> s ;
return q1 *% b +% q0 ;
}