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 › readPackedIntLittle Private Function readPackedIntLittle Doc Source Private Function std.mem.readPackedIntLittle
fn readPackedIntLittle (comptime T : type , bytes : []const u8 , bit_offset : usize ) T Params T : type bytes : []const u8 bit_offset : usize File mem.zig: 1944
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970 fn readPackedIntLittle (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 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 read_bytes = bytes [bit_offset / 8 ..];
const val : uN = @truncate (readInt (LoadInt , read_bytes [0 ..load_size ], .little ) >> bit_shift );
if (bit_shift > load_tail_bits ) {
const tail_bits = @as (Log2N , @intCast (bit_shift - load_tail_bits ));
const tail_byte = read_bytes [load_size ];
const tail_truncated = if (bit_count < 8 ) @as (uN , @truncate (tail_byte )) else @as (uN , tail_byte );
return @bitCast (val | (tail_truncated << (@as (Log2N , @truncate (bit_count )) -% tail_bits )));
} else {
return @bitCast (val );
}
}