commit b87169064aeae8c8a18bcb1a853584df858ac500
parent 52a7d06be038285b2b797245c397d4ad4c2ca5f4
Author: Sebastian <sebastian@sebsite.pw>
Date: Sun, 1 Dec 2024 20:25:22 -0500
math: merge bit_size functions
The size of the integer type has no effect on the result, and all
supported types can promote to u64, so we only need one function which
takes in a u64 argument.
This is a breaking change.
Signed-off-by: Sebastian <sebastian@sebsite.pw>
Diffstat:
4 files changed, 20 insertions(+), 62 deletions(-)
diff --git a/encoding/asn1/decoder.ha b/encoding/asn1/decoder.ha
@@ -3,7 +3,7 @@
use bytes;
use io;
-use math::{bit_size_u8};
+use math::{bit_size};
use os;
use strings;
use time::chrono;
@@ -300,7 +300,7 @@ fn parse_longtag(p: *decoder, max: size) (u32 | error) = {
let b = scan_byte(p)?;
const part = b & 0x7f;
- nbits += if (tag == 0) bit_size_u8(part) else 7;
+ nbits += if (tag == 0) bit_size(part) else 7;
if (nbits > maxbits) {
// overflows u32
return invalid;
diff --git a/encoding/asn1/encoder.ha b/encoding/asn1/encoder.ha
@@ -5,7 +5,7 @@ use bytes;
use endian;
use errors;
use io;
-use math::{bit_size_u8,bit_size_u32};
+use math::{bit_size};
use memio;
use strings;
use types;
@@ -101,7 +101,7 @@ fn write_id(e: *encoder, c: class, t: u32, cons: bool) (void | overflow) = {
write(e, [head | 0x1f])?;
- const bsz = bit_size_u32(t);
+ const bsz = bit_size(t);
const n = ((bsz + 6) / 7) - 1;
for (let i = 0z; i < n; i += 1) {
write(e, [0x80 | (t >> ((n - i) * 7)): u8])?;
@@ -193,7 +193,7 @@ fn pop_bt(e: *encoder) (size, datasz) = {
return x;
};
-fn lensz(l: datasz) u8 = if (l < 128) 1: u8 else (1 + (bit_size_u32(l) + 7) / 8);
+fn lensz(l: datasz) u8 = if (l < 128) 1: u8 else (1 + (bit_size(l) + 7) / 8);
fn encode_dsz(sz: size) []u8 = {
static let buf: [size(datasz) + 1]u8 = [0...];
diff --git a/math/uints.ha b/math/uints.ha
@@ -107,36 +107,7 @@ const DEBRUIJN64TAB: [64]u8 = [
def UINT_SIZE: u8 = (size(uint): u8) * 8;
// Returns the minimum number of bits required to represent x.
-export fn bit_size_u8(x: u8) u8 = {
- return LEN8TAB[x];
-};
-
-// Returns the minimum number of bits required to represent x.
-export fn bit_size_u16(x: u16) u8 = {
- let res = 0u8;
- if (x >= 1u16 << 8) {
- x >>= 8;
- res += 8;
- };
- return res + LEN8TAB[x];
-};
-
-// Returns the minimum number of bits required to represent x.
-export fn bit_size_u32(x: u32) u8 = {
- let res = 0u8;
- if (x >= 1u32 << 16) {
- x >>= 16;
- res += 16;
- };
- if (x >= 1u32 << 8) {
- x >>= 8;
- res += 8;
- };
- return res + LEN8TAB[x];
-};
-
-// Returns the minimum number of bits required to represent x.
-export fn bit_size_u64(x: u64) u8 = {
+export fn bit_size(x: u64) u8 = {
let res = 0u8;
if (x >= 1u64 << 32) {
x >>= 32;
@@ -153,48 +124,35 @@ export fn bit_size_u64(x: u64) u8 = {
return res + LEN8TAB[x];
};
-// Returns the minimum number of bits required to represent x.
-export fn bit_size_u(x: uint) u8 = {
- if (UINT_SIZE == 32) {
- return bit_size_u32(x: u32);
- };
- return bit_size_u64(x: u64);
+@test fn bit_size() void = {
+ assert(bit_size(0) == 0);
+ assert(bit_size(1) == 1);
+ assert(bit_size(2) == 2);
+ assert(bit_size(5) == 3);
+ assert(bit_size(31111) == 15);
+ assert(bit_size(536870911) == 29);
+ assert(bit_size(8589934591) == 33);
};
-@test fn bit_size_u() void = {
- assert(bit_size_u8(0) == 0);
- assert(bit_size_u8(2) == 2);
- assert(bit_size_u8(5) == 3);
- assert(bit_size_u16(5) == 3);
- assert(bit_size_u32(5) == 3);
- assert(bit_size_u64(5) == 3);
- assert(bit_size_u16(31111) == 15);
- assert(bit_size_u32(536870911) == 29);
- assert(bit_size_u64(8589934591) == 33);
- assert(bit_size_u(0) == 0);
- assert(bit_size_u(1) == 1);
-};
-
-
// Returns the number of leading zero bits in x
// The result is UINT_SIZE for x == 0.
-export fn leading_zeros_u(x: uint) u8 = UINT_SIZE - bit_size_u(x);
+export fn leading_zeros_u(x: uint) u8 = UINT_SIZE - bit_size(x);
// Returns the number of leading zero bits in x
// The result is 8 for x == 0.
-export fn leading_zeros_u8(x: u8) u8 = 8 - bit_size_u8(x);
+export fn leading_zeros_u8(x: u8) u8 = 8 - bit_size(x);
// Returns the number of leading zero bits in x
// The result is 16 for x == 0.
-export fn leading_zeros_u16(x: u16) u8 = 16 - bit_size_u16(x);
+export fn leading_zeros_u16(x: u16) u8 = 16 - bit_size(x);
// Returns the number of leading zero bits in x
// The result is 32 for x == 0.
-export fn leading_zeros_u32(x: u32) u8 = 32 - bit_size_u32(x);
+export fn leading_zeros_u32(x: u32) u8 = 32 - bit_size(x);
// Returns the number of leading zero bits in x
// The result is 64 for x == 0.
-export fn leading_zeros_u64(x: u64) u8 = 64 - bit_size_u64(x);
+export fn leading_zeros_u64(x: u64) u8 = 64 - bit_size(x);
@test fn leading_zeros_u() void = {
assert(leading_zeros_u(0) == UINT_SIZE);
diff --git a/sort/sort.ha b/sort/sort.ha
@@ -88,7 +88,7 @@ type run = struct {
fn powersort(items: []opaque, itemsz: size, cmp: *cmpfunc) void = {
// npowers = floor(log2(n)) + 1
- const npowers = math::bit_size_u64(len(items)) + 1;
+ const npowers = math::bit_size(len(items)) + 1;
const runs: []run = alloc([run { start = EMPTY, ... }...], npowers + 1);
defer free(runs);
let top = 0u8;