Skip to content

Commit ca3406c

Browse files
Fuuzetsutreeowl
authored andcommitted
Produce better code from maskW
``` complement (m-1) `xor` m == -m `xor` m ``` Latter produces slightly better assembly. ```asm movq %rdi, %rax negq %rax xorq %rdi, %rax ``` Indeed if we give ```cpp uint64_t f(uint64_t m_aQU2) { return ((m_aQU2 - 1) ^ 18446744073709551615UL) ^ m_aQU2; } ``` to clang and compile with optimisation, that's the ASM it spits out. Translating it back to Haskell gives the solution from this branch. Without same optimisation, even in Haskell it saves an instruction. ```hs maskW :: Word -> Word maskW m = complement (m-1) `xor` m {- movq %rax,%rbx xorq $-1,%rbx decq %rax xorq %rbx,%rax -} ``` ```hs maskMine :: Word -> Word maskMine m = (-m) `xor` m {- movq %rax,%rbx negq %rbx xorq %rax,%rbx -} ```
1 parent 2420e11 commit ca3406c

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

Data/IntMap/Internal.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,7 +1117,7 @@ withoutKeys t1@(Bin p1 m1 _ _) (IntSet.Tip p2 bm2) =
11171117
let minbit = bitmapOf p1
11181118
lt_minbit = minbit - 1
11191119
maxbit = bitmapOf (p1 .|. (m1 .|. (m1 - 1)))
1120-
gt_maxbit = maxbit `xor` complement (maxbit - 1)
1120+
gt_maxbit = (-maxbit) `xor` maxbit
11211121
-- TODO(wrengr): should we manually inline/unroll 'updatePrefix'
11221122
-- and 'withoutBM' here, in order to avoid redundant case analyses?
11231123
in updatePrefix p2 t1 $ withoutBM (bm2 .|. lt_minbit .|. gt_maxbit)
@@ -3336,7 +3336,7 @@ mask i m
33363336
-- bit @m@.
33373337
maskW :: Nat -> Nat -> Prefix
33383338
maskW i m
3339-
= intFromNat (i .&. (complement (m-1) `xor` m))
3339+
= intFromNat (i .&. ((-m) `xor` m))
33403340
{-# INLINE maskW #-}
33413341

33423342
-- | Does the left switching bit specify a shorter prefix?

0 commit comments

Comments
 (0)