Skip to content

Commit aadb0e9

Browse files
authored
maths.bits: fix ambiguous expressions, remove special case in the checker (#24815)
1 parent 65a5e96 commit aadb0e9

2 files changed

Lines changed: 17 additions & 19 deletions

File tree

‎vlib/math/bits/bits.v‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ pub fn ones_count_16(x u16) int {
106106
// ones_count_32 returns the number of one bits ("population count") in x.
107107
@[direct_array_access]
108108
pub fn ones_count_32(x u32) int {
109-
return int(pop_8_tab[x >> 24] + pop_8_tab[x >> 16 & 0xff] + pop_8_tab[x >> 8 & 0xff] +
109+
return int(pop_8_tab[x >> 24] + pop_8_tab[(x >> 16) & 0xff] + pop_8_tab[(x >> 8) & 0xff] +
110110
pop_8_tab[x & u32(0xff)])
111111
}
112112

@@ -131,8 +131,8 @@ pub fn ones_count_64(x u64) int {
131131
// Per "Hacker's Delight", the first line can be simplified
132132
// more, but it saves at best one instruction, so we leave
133133
// it alone for clarity.
134-
mut y := (x >> u64(1) & (m0 & max_u64)) + (x & (m0 & max_u64))
135-
y = (y >> u64(2) & (m1 & max_u64)) + (y & (m1 & max_u64))
134+
mut y := ((x >> u64(1)) & (m0 & max_u64)) + (x & (m0 & max_u64))
135+
y = ((y >> u64(2)) & (m1 & max_u64)) + (y & (m1 & max_u64))
136136
y = ((y >> 4) + y) & (m2 & max_u64)
137137
y += y >> 8
138138
y += y >> 16
@@ -202,18 +202,18 @@ pub fn reverse_16(x u16) u16 {
202202
// reverse_32 returns the value of x with its bits in reversed order.
203203
@[inline]
204204
pub fn reverse_32(x u32) u32 {
205-
mut y := ((x >> u32(1) & (m0 & max_u32)) | ((x & (m0 & max_u32)) << 1))
206-
y = ((y >> u32(2) & (m1 & max_u32)) | ((y & (m1 & max_u32)) << u32(2)))
207-
y = ((y >> u32(4) & (m2 & max_u32)) | ((y & (m2 & max_u32)) << u32(4)))
205+
mut y := (((x >> u32(1)) & (m0 & max_u32)) | ((x & (m0 & max_u32)) << 1))
206+
y = (((y >> u32(2)) & (m1 & max_u32)) | ((y & (m1 & max_u32)) << u32(2)))
207+
y = (((y >> u32(4)) & (m2 & max_u32)) | ((y & (m2 & max_u32)) << u32(4)))
208208
return reverse_bytes_32(u32(y))
209209
}
210210

211211
// reverse_64 returns the value of x with its bits in reversed order.
212212
@[inline]
213213
pub fn reverse_64(x u64) u64 {
214-
mut y := ((x >> u64(1) & (m0 & max_u64)) | ((x & (m0 & max_u64)) << 1))
215-
y = ((y >> u64(2) & (m1 & max_u64)) | ((y & (m1 & max_u64)) << 2))
216-
y = ((y >> u64(4) & (m2 & max_u64)) | ((y & (m2 & max_u64)) << 4))
214+
mut y := (((x >> u64(1)) & (m0 & max_u64)) | ((x & (m0 & max_u64)) << 1))
215+
y = (((y >> u64(2)) & (m1 & max_u64)) | ((y & (m1 & max_u64)) << 2))
216+
y = (((y >> u64(4)) & (m2 & max_u64)) | ((y & (m2 & max_u64)) << 4))
217217
return reverse_bytes_64(y)
218218
}
219219

@@ -231,7 +231,7 @@ pub fn reverse_bytes_16(x u16) u16 {
231231
// This function's execution time does not depend on the inputs.
232232
@[inline]
233233
pub fn reverse_bytes_32(x u32) u32 {
234-
y := ((x >> u32(8) & (m3 & max_u32)) | ((x & (m3 & max_u32)) << u32(8)))
234+
y := (((x >> u32(8)) & (m3 & max_u32)) | ((x & (m3 & max_u32)) << u32(8)))
235235
return u32((y >> 16) | (y << 16))
236236
}
237237

@@ -240,8 +240,8 @@ pub fn reverse_bytes_32(x u32) u32 {
240240
// This function's execution time does not depend on the inputs.
241241
@[inline]
242242
pub fn reverse_bytes_64(x u64) u64 {
243-
mut y := ((x >> u64(8) & (m3 & max_u64)) | ((x & (m3 & max_u64)) << u64(8)))
244-
y = ((y >> u64(16) & (m4 & max_u64)) | ((y & (m4 & max_u64)) << u64(16)))
243+
mut y := (((x >> u64(8)) & (m3 & max_u64)) | ((x & (m3 & max_u64)) << u64(8)))
244+
y = (((y >> u64(16)) & (m4 & max_u64)) | ((y & (m4 & max_u64)) << u64(16)))
245245
return (y >> 32) | (y << 32)
246246
}
247247

‎vlib/v/checker/infix.v‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -806,13 +806,11 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
806806
}
807807
// Do an ambiguous expression check for << >> and &, since they all have the same precedence (unlike in C)
808808
if !c.is_builtin_mod && node.op in [.amp, .left_shift, .right_shift] {
809-
if !c.mod.starts_with('math') { // TODO fix all warnings in math
810-
if mut node.left is ast.InfixExpr {
811-
if node.left.op != node.op && node.left.op in [.amp, .left_shift, .right_shift] {
812-
// for example: `(a << b) & c` instead of `a << b & c`
813-
c.note('ambiguous expression. use `()` to ensure correct order of operations',
814-
node.pos)
815-
}
809+
if mut node.left is ast.InfixExpr {
810+
if node.left.op != node.op && node.left.op in [.amp, .left_shift, .right_shift] {
811+
// for example: `(a << b) & c` instead of `a << b & c`
812+
c.note('ambiguous expression. use `()` to ensure correct order of operations',
813+
node.pos)
816814
}
817815
}
818816
}

0 commit comments

Comments
 (0)