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 standard library.
std › mem › readPackedIntBig Private Function readPackedIntBig Doc Source Private Function std.mem.readPackedIntBig
fn readPackedIntBig (comptime T : type , bytes : []const u8 , bit_offset : usize ) T Params T : type bytes : []const u8 bit_offset : usize File mem.zig: 1972
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999 fn readPackedIntBig (comptime T : type , bytes : []const u8 , bit_offset : usize ) T {
const uN = @Int (.unsigned , @bitSizeOf (T ));
const Log2N = std .math .Log2Int (T );
const bit_count = @as (usize , @bitSizeOf (T ));
const bit_shift = @as (u3 , @intCast (bit_offset % 8 ));
const byte_count = @divCeil (@as (usize , bit_shift ) + bit_count , 8 );
const load_size = @divCeil (bit_count , 8 );
const load_tail_bits = @as (u3 , @intCast ((load_size * 8 ) - bit_count ));
const LoadInt = @Int (.unsigned , load_size * 8 );
if (bit_count == 0 )
return 0 ;
// Read by loading a LoadInt, and then follow it up with a 1-byte read
// of the tail if bit_offset pushed us over a byte boundary.
const end = bytes .len - (bit_offset / 8 );
const read_bytes = bytes [(end - byte_count )..end ];
const val = @as (uN , @truncate (readInt (LoadInt , bytes [(end - load_size )..end ][0 ..load_size ], .big ) >> bit_shift ));
if (bit_shift > load_tail_bits ) {
const tail_bits = @as (Log2N , @intCast (bit_shift - load_tail_bits ));
const tail_byte = if (bit_count < 8 ) @as (uN , @truncate (read_bytes [0 ])) else @as (uN , read_bytes [0 ]);
return @bitCast (val | (tail_byte << (@as (Log2N , @truncate (bit_count )) -% tail_bits )));
} else {
return @bitCast (val );
}
}