Skip to content

Commit d9a3a3a

Browse files
committed
strconv,math.bits: eliminate bounds checking in commonly used routines
1 parent 9179038 commit d9a3a3a

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

vlib/math/bits/bits.v

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@ pub fn leading_zeros_64(x u64) int {
4545

4646
// --- TrailingZeros ---
4747
// trailing_zeros_8 returns the number of trailing zero bits in x; the result is 8 for x == 0.
48+
@[direct_array_access]
4849
pub fn trailing_zeros_8(x u8) int {
4950
return int(ntz_8_tab[x])
5051
}
5152

5253
// trailing_zeros_16 returns the number of trailing zero bits in x; the result is 16 for x == 0.
54+
@[direct_array_access]
5355
pub fn trailing_zeros_16(x u16) int {
5456
if x == 0 {
5557
return 16
@@ -59,6 +61,7 @@ pub fn trailing_zeros_16(x u16) int {
5961
}
6062

6163
// trailing_zeros_32 returns the number of trailing zero bits in x; the result is 32 for x == 0.
64+
@[direct_array_access]
6265
pub fn trailing_zeros_32(x u32) int {
6366
if x == 0 {
6467
return 32
@@ -68,6 +71,7 @@ pub fn trailing_zeros_32(x u32) int {
6871
}
6972

7073
// trailing_zeros_64 returns the number of trailing zero bits in x; the result is 64 for x == 0.
74+
@[direct_array_access]
7175
pub fn trailing_zeros_64(x u64) int {
7276
if x == 0 {
7377
return 64
@@ -88,16 +92,19 @@ pub fn trailing_zeros_64(x u64) int {
8892

8993
// --- OnesCount ---
9094
// ones_count_8 returns the number of one bits ("population count") in x.
95+
@[direct_array_access]
9196
pub fn ones_count_8(x u8) int {
9297
return int(pop_8_tab[x])
9398
}
9499

95100
// ones_count_16 returns the number of one bits ("population count") in x.
101+
@[direct_array_access]
96102
pub fn ones_count_16(x u16) int {
97103
return int(pop_8_tab[x >> 8] + pop_8_tab[x & u16(0xff)])
98104
}
99105

100106
// ones_count_32 returns the number of one bits ("population count") in x.
107+
@[direct_array_access]
101108
pub fn ones_count_32(x u32) int {
102109
return int(pop_8_tab[x >> 24] + pop_8_tab[x >> 16 & 0xff] + pop_8_tab[x >> 8 & 0xff] +
103110
pop_8_tab[x & u32(0xff)])
@@ -181,13 +188,13 @@ pub fn rotate_left_64(x u64, k int) u64 {
181188

182189
// --- Reverse ---
183190
// reverse_8 returns the value of x with its bits in reversed order.
184-
@[inline]
191+
@[direct_array_access; inline]
185192
pub fn reverse_8(x u8) u8 {
186193
return rev_8_tab[x]
187194
}
188195

189196
// reverse_16 returns the value of x with its bits in reversed order.
190-
@[inline]
197+
@[direct_array_access; inline]
191198
pub fn reverse_16(x u16) u16 {
192199
return u16(rev_8_tab[x >> 8]) | (u16(rev_8_tab[x & u16(0xff)]) << 8)
193200
}
@@ -240,11 +247,13 @@ pub fn reverse_bytes_64(x u64) u64 {
240247

241248
// --- Len ---
242249
// len_8 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
250+
@[direct_array_access]
243251
pub fn len_8(x u8) int {
244252
return int(len_8_tab[x])
245253
}
246254

247255
// len_16 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
256+
@[direct_array_access]
248257
pub fn len_16(x u16) int {
249258
mut y := x
250259
mut n := 0
@@ -256,6 +265,7 @@ pub fn len_16(x u16) int {
256265
}
257266

258267
// len_32 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
268+
@[direct_array_access]
259269
pub fn len_32(x u32) int {
260270
mut y := x
261271
mut n := 0
@@ -271,6 +281,7 @@ pub fn len_32(x u32) int {
271281
}
272282

273283
// len_64 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
284+
@[direct_array_access]
274285
pub fn len_64(x u64) int {
275286
mut y := x
276287
mut n := 0

vlib/strconv/utilities.v

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,15 @@ fn mul_shift_32(m u32, mul u64, ishift int) u32 {
6767
return u32(shifted_sum)
6868
}
6969

70+
@[direct_array_access; inline]
7071
fn mul_pow5_invdiv_pow2(m u32, q u32, j int) u32 {
72+
assert1(q < pow5_inv_split_32.len, 'q < pow5_inv_split_32.len')
7173
return mul_shift_32(m, pow5_inv_split_32[q], j)
7274
}
7375

76+
@[direct_array_access; inline]
7477
fn mul_pow5_div_pow2(m u32, i u32, j int) u32 {
78+
assert1(i < pow5_split_32.len, 'i < pow5_split_32.len')
7579
return mul_shift_32(m, pow5_split_32[i], j)
7680
}
7781

0 commit comments

Comments
 (0)