74 lines
2.5 KiB
Plaintext
74 lines
2.5 KiB
Plaintext
#! @file bits.sakura
|
|||
|
|
#! @author John Christman (sorakatadzuma@gmail.com)
|
||
|
|
#! @copyright Malunal Studios, LLC.
|
||
|
|
#! @license MIT
|
||
|
|
#!
|
||
|
|
#! Contains counting and reversing the bits and bytes of an integer, which no
|
||
|
|
#! operator writes, and which text, memory, and vectors are searched with.
|
||
|
|
#!
|
||
|
|
#! The compiler gives the operations of a whole word they are built on: the
|
||
|
|
#! virtual machine runs its own, and an executable the instructions `bsr`,
|
||
|
|
#! `bsf`, and `bswap`, and a sequence counting ones, since SSE2 alone has no
|
||
|
|
#! `popcnt`.
|
||
|
|
|
||
|
|
module core;
|
||
|
|
|
||
|
|
|
||
|
|
#! How many bits of an integer are 1, as its type holds it, so -1 of an `i8` has
|
||
|
|
#! 8.
|
||
|
|
#! @tparam T The type of integer.
|
||
|
|
#! @param x The integer.
|
||
|
|
#! @returns How many.
|
||
|
|
pub def count_ones<T>(x: T): u32 where T: integer {
|
||
|
|
return ones_in(x as u64 & bits_mask::<T>()) as u32;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! How many 0 bits lead an integer, from its top bit, as its type holds it.
|
||
|
|
#! @tparam T The type of integer.
|
||
|
|
#! @param x The integer.
|
||
|
|
#! @returns How many: the width of the type where it is zero, and 0 where it is
|
||
|
|
#! below zero.
|
||
|
|
pub def leading_zeros<T>(x: T): u32 where T: integer {
|
||
|
|
return (zeros_leading(x as u64 & bits_mask::<T>()) - (64 - size_of::<T>() * 8)) as u32;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! How many 0 bits trail an integer, from its bottom bit.
|
||
|
|
#! @tparam T The type of integer.
|
||
|
|
#! @param x The integer.
|
||
|
|
#! @returns How many: the width of the type where it is zero.
|
||
|
|
@inline
|
||
|
|
pub def trailing_zeros<T>(x: T): u32 where T: integer {
|
||
|
|
if const size_of::<T>() == 8 {
|
||
|
|
return zeros_trailing(x as u64) as u32;
|
||
|
|
}
|
||
|
|
return zeros_trailing(x as u64 | (1u << (size_of::<T>() * 8))) as u32;
|
||
|
|
}
|
||
|
|
|
||
|
|
#! An integer with its bytes in the reverse order, as converting between little
|
||
|
|
#! and big endian does.
|
||
|
|
#! @tparam T The type of integer.
|
||
|
|
#! @param x The integer.
|
||
|
|
#! @returns The integer reversed.
|
||
|
|
pub def swap_bytes<T>(x: T): T where T: integer {
|
||
|
|
return (bytes_reversed(x as u64) >> (64 - size_of::<T>() * 8)) as T;
|
||
|
|
}
|
||
|
|
|
||
|
|
# The bits of a word a type of integer holds.
|
||
|
|
def bits_mask<T>(): u64 where T: integer {
|
||
|
|
if const size_of::<T>() == 8 {
|
||
|
|
return 0xFFFFFFFFFFFFFFFFu;
|
||
|
|
}
|
||
|
|
return (1u << (size_of::<T>() * 8)) - 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
# How many bits of a word are 1.
|
||
|
|
@intrinsic def ones_in(word: u64): u64;
|
||
|
|
|
||
|
|
# How many 0 bits lead a word from its top: 64 where it is zero.
|
||
|
|
@intrinsic def zeros_leading(word: u64): u64;
|
||
|
|
|
||
|
|
# How many 0 bits trail a word from its bottom: 64 where it is zero.
|
||
|
|
@intrinsic def zeros_trailing(word: u64): u64;
|
||
|
|
|
||
|
|
# A word with its bytes in the reverse order.
|
||
|
|
@intrinsic def bytes_reversed(word: u64): u64;
|