Skip to content

Commit 5578a0c

Browse files
KenoStefanKarpinski
authored andcommitted
Use unchecked conversion in bitcounting operations. NFC. (#34398)
LLVM's intrinsics for bitcounting return an integer of the same size as the input integer. On the julia side, we then trunc back to `Int` (since we're guaranteed that the answers will always be at most `log2(x)`). However, the code was calling for a checked conversion, rather than an unchecked one. This doesn't matter too much as LLVM does know about the result range of these intrinsics, but it made me sad to generate all these error checks in the first place, so use the unchecked variants instead.
1 parent a7cd97a commit 5578a0c

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

base/int.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ julia> count_ones(7)
337337
3
338338
```
339339
"""
340-
count_ones(x::BitInteger) = Int(ctpop_int(x))
340+
count_ones(x::BitInteger) = ctpop_int(x) % Int
341341

342342
"""
343343
leading_zeros(x::Integer) -> Integer
@@ -350,7 +350,7 @@ julia> leading_zeros(Int32(1))
350350
31
351351
```
352352
"""
353-
leading_zeros(x::BitInteger) = Int(ctlz_int(x))
353+
leading_zeros(x::BitInteger) = ctlz_int(x) % Int
354354

355355
"""
356356
trailing_zeros(x::Integer) -> Integer
@@ -363,7 +363,7 @@ julia> trailing_zeros(2)
363363
1
364364
```
365365
"""
366-
trailing_zeros(x::BitInteger) = Int(cttz_int(x))
366+
trailing_zeros(x::BitInteger) = cttz_int(x) % Int
367367

368368
"""
369369
count_zeros(x::Integer) -> Integer

0 commit comments

Comments
 (0)