[project @ 2001-12-13 11:12:27 by simonmar]
authorsimonmar <unknown>
Thu, 13 Dec 2001 11:12:27 +0000 (11:12 +0000)
committersimonmar <unknown>
Thu, 13 Dec 2001 11:12:27 +0000 (11:12 +0000)
Add shifty wrappers:

   shiftL#, shiftR# :: Word# -> Int# -> Word#
   iShiftL#, iShiftRA#, iShiftRL# :: Int# -> Int# -> Int#

ghc/lib/std/PrelBase.lhs

index 5ff8a80..cb2b702 100644 (file)
@@ -1,5 +1,5 @@
 % -----------------------------------------------------------------------------
-% $Id: PrelBase.lhs,v 1.56 2001/12/07 11:34:48 sewardj Exp $
+% $Id: PrelBase.lhs,v 1.57 2001/12/13 11:12:27 simonmar Exp $
 %
 % (c) The University of Glasgow, 1992-2000
 %
@@ -677,6 +677,33 @@ gtInt, geInt, eqInt, neInt, ltInt, leInt :: Int -> Int -> Bool
 "x# <=# x#" forall x#. x# <=# x# = True
   #-}
 
+-- Wrappers for the shift operations.  The uncheckedShift# family are
+-- undefined when the amount being shifted by is greater than the size
+-- in bits of Int#, so these wrappers perform a check and return
+-- either zero or -1 appropriately.
+--
+-- Note that these wrappers still produce undefined results when the
+-- second argument (the shift amount) is negative.
+
+shiftL#, shiftR# :: Word# -> Int# -> Word#
+
+a `shiftL#` b   | b >=# WORD_SIZE_IN_BITS# = int2Word# 0#
+               | otherwise                = a `uncheckedShiftL#` b
+
+a `shiftR#` b   | b >=# WORD_SIZE_IN_BITS# = int2Word# 0#
+               | otherwise                = a `uncheckedShiftRL#` b
+
+iShiftL#, iShiftRA#, iShiftRL# :: Int# -> Int# -> Int#
+
+a `iShiftL#` b  | b >=# WORD_SIZE_IN_BITS# = 0#
+               | otherwise                = a `uncheckedIShiftL#` b
+
+a `iShiftRA#` b | b >=# WORD_SIZE_IN_BITS# = if a <# 0# then (-1#) else 0#
+               | otherwise                = a `uncheckedIShiftRA#` b
+
+a `iShiftRL#` b | b >=# WORD_SIZE_IN_BITS# = 0#
+               | otherwise                = a `uncheckedIShiftRL#` b
+
 #if WORD_SIZE_IN_BITS == 32
 {-# RULES
 "narrow32Int#"  forall x#. narrow32Int#   x# = x#