2727
2828import macros
2929import std/ private/ since
30- from std/ private/ bitops_utils import forwardImpl, toUnsigned
30+ from std/ private/ bitops_utils import forwardImpl, castToUnsigned
3131
3232func bitnot * [T: SomeInteger ](x: T): T {.magic : " BitnotI" .}
3333 # # Computes the `bitwise complement` of the integer `x`.
@@ -72,7 +72,7 @@ func bitsliced*[T: SomeInteger](v: T; slice: Slice[int]): T {.inline, since: (1,
7272
7373 let
7474 upmost = sizeof (T) * 8 - 1
75- uv = when v is SomeUnsignedInt : v else : v.toUnsigned
75+ uv = v.castToUnsigned
7676 (uv shl (upmost - slice.b) shr (upmost - slice.b + slice.a)).T
7777
7878proc bitslice * [T: SomeInteger ](v: var T; slice: Slice [int ]) {.inline , since : (1 , 3 ).} =
@@ -84,7 +84,7 @@ proc bitslice*[T: SomeInteger](v: var T; slice: Slice[int]) {.inline, since: (1,
8484
8585 let
8686 upmost = sizeof (T) * 8 - 1
87- uv = when v is SomeUnsignedInt : v else : v.toUnsigned
87+ uv = v.castToUnsigned
8888 v = (uv shl (upmost - slice.b) shr (upmost - slice.b + slice.a)).T
8989
9090func toMask * [T: SomeInteger ](slice: Slice [int ]): T {.inline , since : (1 , 3 ).} =
@@ -95,10 +95,7 @@ func toMask*[T: SomeInteger](slice: Slice[int]): T {.inline, since: (1, 3).} =
9595
9696 let
9797 upmost = sizeof (T) * 8 - 1
98- bitmask = when T is SomeUnsignedInt :
99- bitnot (0 .T)
100- else :
101- bitnot (0 .T).toUnsigned
98+ bitmask = bitnot (0 .T).castToUnsigned
10299 (bitmask shl (upmost - slice.b + slice.a) shr (upmost - slice.b)).T
103100
104101proc masked * [T: SomeInteger ](v, mask :T): T {.inline , since : (1 , 3 ).} =
@@ -505,8 +502,7 @@ func parityBits*(x: SomeInteger): int {.inline.} =
505502
506503 # Can be used a base if creating ASM version.
507504 # https://stackoverflow.com/questions/21617970/how-to-check-if-value-has-even-parity-of-bits-or-odd
508- when x is SomeSignedInt :
509- let x = x.toUnsigned
505+ let x = x.castToUnsigned
510506 when nimvm :
511507 result = forwardImpl (parityImpl, x)
512508 else :
@@ -529,8 +525,7 @@ func firstSetBit*(x: SomeInteger): int {.inline.} =
529525 doAssert firstSetBit (0b 0000_1111 'u8 ) == 1
530526
531527 # GCC builtin 'builtin_ffs' already handle zero input.
532- when x is SomeSignedInt :
533- let x = x.toUnsigned
528+ let x = x.castToUnsigned
534529 when nimvm :
535530 when noUndefined:
536531 if x == 0 :
@@ -572,8 +567,7 @@ func fastLog2*(x: SomeInteger): int {.inline.} =
572567 doAssert fastLog2 (0b 0000_1000 'u8 ) == 3
573568 doAssert fastLog2 (0b 0000_1111 'u8 ) == 3
574569
575- when x is SomeSignedInt :
576- let x = x.toUnsigned
570+ let x = x.castToUnsigned
577571 when noUndefined:
578572 if x == 0 :
579573 return - 1
@@ -615,8 +609,7 @@ func countLeadingZeroBits*(x: SomeInteger): int {.inline.} =
615609 doAssert countLeadingZeroBits (0b 0000_1000 'u8 ) == 4
616610 doAssert countLeadingZeroBits (0b 0000_1111 'u8 ) == 4
617611
618- when x is SomeSignedInt :
619- let x = x.toUnsigned
612+ let x = x.castToUnsigned
620613 when noUndefined:
621614 if x == 0 :
622615 return 0
@@ -644,8 +637,7 @@ func countTrailingZeroBits*(x: SomeInteger): int {.inline.} =
644637 doAssert countTrailingZeroBits (0b 0000_1000 'u8 ) == 3
645638 doAssert countTrailingZeroBits (0b 0000_1111 'u8 ) == 0
646639
647- when x is SomeSignedInt :
648- let x = x.toUnsigned
640+ let x = x.castToUnsigned
649641 when noUndefined:
650642 if x == 0 :
651643 return 0
0 commit comments