[commit: base] data-proxy, master: Expose new Word operation to swap endianness for Word{16, 32, 64} (5c37ca9)

Richard Eisenberg eir at ghc.haskell.org
Tue Jul 23 16:04:12 CEST 2013


Repository : http://darcs.haskell.org/ghc.git/

On branches: data-proxy,master

http://hackage.haskell.org/trac/ghc/changeset/5c37ca912f41425b53df541091ee25f18ae2ae95

>---------------------------------------------------------------

commit 5c37ca912f41425b53df541091ee25f18ae2ae95
Author: Austin Seipp <aseipp at pobox.com>
Date:   Wed Jul 17 04:00:33 2013 -0500

    Expose new Word operation to swap endianness for Word{16,32,64}
    
    Authored-by: Vincent Hanquez <tab at snarc.org>
    Signed-off-by: Austin Seipp <aseipp at pobox.com>

>---------------------------------------------------------------

 Data/Word.hs |   18 ++++++++++++++++++
 GHC/Word.hs  |   18 +++++++++++++++++-
 2 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/Data/Word.hs b/Data/Word.hs
index 39aa1a8..c844c4d 100644
--- a/Data/Word.hs
+++ b/Data/Word.hs
@@ -22,6 +22,9 @@ module Data.Word
         Word,
         Word8, Word16, Word32, Word64,
 
+        -- * byte swapping
+        byteSwap16, byteSwap32, byteSwap64,
+
         -- * Notes
 
         -- $notes
@@ -33,6 +36,21 @@ import GHC.Word
 
 #ifdef __HUGS__
 import Hugs.Word
+
+byteSwap16 :: Word16 -> Word16
+byteSwap16 w = (w `shift` -8) .|. (w `shift` 8)
+
+byteSwap32 :: Word32 -> Word32
+byteSwap32 w =
+         (w `shift` -24)             .|. (w `shift` 24)
+    .|. ((w `shift` -8) .&. 0xff00) .|. ((w .&. 0xff00) `shift` 8)
+
+byteSwap64 :: Word64 -> Word64
+byteSwap64 w =
+        (w `shift` -56)                  .|. (w `shift` 56)
+    .|. ((w `shift` -40) .&. 0xff00)     .|. ((w .&. 0xff00) `shift` 40)
+    .|. ((w `shift` -24) .&. 0xff0000)   .|. ((w .&. 0xff0000) `shift` 24)
+    .|. ((w `shift` -8)  .&. 0xff000000) .|. ((w .&. 0xff000000) `shift` 8)
 #endif
 
 {- $notes
diff --git a/GHC/Word.hs b/GHC/Word.hs
index 75957df..3419a24 100644
--- a/GHC/Word.hs
+++ b/GHC/Word.hs
@@ -23,7 +23,10 @@
 module GHC.Word (
     Word(..), Word8(..), Word16(..), Word32(..), Word64(..),
     uncheckedShiftL64#,
-    uncheckedShiftRL64#
+    uncheckedShiftRL64#,
+    byteSwap16,
+    byteSwap32,
+    byteSwap64
     ) where
 
 import Data.Bits
@@ -300,6 +303,9 @@ instance Bits Word16 where
 instance FiniteBits Word16 where
     finiteBitSize _ = 16
 
+byteSwap16 :: Word16 -> Word16
+byteSwap16 (W16# w#) = W16# (byteSwap16# w#)
+
 {-# RULES
 "fromIntegral/Word8->Word16"   fromIntegral = \(W8# x#) -> W16# x#
 "fromIntegral/Word16->Word16"  fromIntegral = id :: Word16 -> Word16
@@ -524,6 +530,9 @@ instance Read Word32 where
     readsPrec p s = [(fromIntegral (x::Int), r) | (x, r) <- readsPrec p s]
 #endif
 
+byteSwap32 :: Word32 -> Word32
+byteSwap32 (W32# w#) = W32# (byteSwap32# w#)
+
 ------------------------------------------------------------------------
 -- type Word64
 ------------------------------------------------------------------------
@@ -772,3 +781,10 @@ instance Ix Word64 where
 instance Read Word64 where
     readsPrec p s = [(fromInteger x, r) | (x, r) <- readsPrec p s]
 
+#if WORD_SIZE_IN_BITS < 64
+byteSwap64 :: Word64 -> Word64
+byteSwap64 (W64# w#) = W64# (byteSwap64# w#)
+#else
+byteSwap64 :: Word64 -> Word64
+byteSwap64 (W64# w#) = W64# (byteSwap# w#)
+#endif






More information about the ghc-commits mailing list