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 › std/ › mem.zig › readPackedIntBig
readPackedIntBig
mem.readPackedIntBig
fn readPackedIntBig (comptime T : type , bytes : []const u8 , bit_offset : usize ) T
File
Code
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 ;
// 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 );
}
}