Skip to content

Commit

Permalink
Document preconditions
Browse files Browse the repository at this point in the history
  • Loading branch information
meooow25 committed Oct 29, 2024
1 parent 07098bf commit b9dc73d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions containers/src/Data/IntSet/Internal/IntTreeCommons.hs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ mask i m = i .&. ((-m) `xor` m)
{-# INLINE mask #-}

-- | The first switching bit where the two prefixes disagree.
--
-- Precondition for defined behavior: p1 /= p2
branchMask :: Int -> Int -> Int
branchMask p1 p2 = iHighestBitMask (p1 `xor` p2)
{-# INLINE branchMask #-}
Expand Down
6 changes: 6 additions & 0 deletions containers/src/Utils/Containers/Internal/BitUtil.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,15 @@ import GHC.Exts (Int(..), uncheckedIShiftRL#)
#endif

-- | An @Int@ where only the highest bit is set.
--
-- Precondition for defined behavior: w /= 0
iHighestBitMask :: Int -> Int
iHighestBitMask w = unsafeShiftL 1 (wordSize - 1 - countLeadingZeros w)
{-# INLINE iHighestBitMask #-}

-- Right and left logical shifts.
--
-- Precondition for defined behavior: 0 <= shift amount < wordSize
shiftRL, shiftLL :: Word -> Int -> Word
shiftRL = unsafeShiftR
shiftLL = unsafeShiftL
Expand All @@ -58,6 +62,8 @@ wordSize :: Int
wordSize = finiteBitSize (0 :: Word)

-- Right logical shift.
--
-- Precondition for defined behavior: 0 <= shift amount < wordSize
iShiftRL :: Int -> Int -> Int
#ifdef __GLASGOW_HASKELL__
iShiftRL (I# x#) (I# sh#) = I# (uncheckedIShiftRL# x# sh#)
Expand Down

0 comments on commit b9dc73d

Please sign in to comment.