{-# LANGUAGE CPP #-}
#ifdef USE_MONO_PAT_BINDS
{-# LANGUAGE MonoPatBinds #-}
#endif
#if defined(__GLASGOW_HASKELL__) && !defined(__HADDOCK__)
#include "MachDeps.h"
#endif
module Blaze.ByteString.Builder.Word
(
writeWord8
, writeWord16be
, writeWord32be
, writeWord64be
, writeWord16le
, writeWord32le
, writeWord64le
, writeWordhost
, writeWord16host
, writeWord32host
, writeWord64host
, fromWord8
, fromWord8s
, fromWord16be
, fromWord32be
, fromWord64be
, fromWord32sbe
, fromWord16sbe
, fromWord64sbe
, fromWord16le
, fromWord32le
, fromWord64le
, fromWord16sle
, fromWord32sle
, fromWord64sle
, fromWordhost
, fromWord16host
, fromWord32host
, fromWord64host
, fromWordshost
, fromWord16shost
, fromWord32shost
, fromWord64shost
) where
import Blaze.ByteString.Builder.Internal
import Blaze.ByteString.Builder.Internal.UncheckedShifts
import Foreign
writeWord8 :: Word8 -> Write
writeWord8 x = exactWrite 1 (\pf -> poke pf x)
{-# INLINE writeWord8 #-}
writeWord16be :: Word16 -> Write
writeWord16be w = exactWrite 2 $ \p -> do
poke p (fromIntegral (shiftr_w16 w 8) :: Word8)
poke (p `plusPtr` 1) (fromIntegral (w) :: Word8)
{-# INLINE writeWord16be #-}
writeWord16le :: Word16 -> Write
writeWord16le w = exactWrite 2 $ \p -> do
poke p (fromIntegral (w) :: Word8)
poke (p `plusPtr` 1) (fromIntegral (shiftr_w16 w 8) :: Word8)
{-# INLINE writeWord16le #-}
writeWord32be :: Word32 -> Write
writeWord32be w = exactWrite 4 $ \p -> do
poke p (fromIntegral (shiftr_w32 w 24) :: Word8)
poke (p `plusPtr` 1) (fromIntegral (shiftr_w32 w 16) :: Word8)
poke (p `plusPtr` 2) (fromIntegral (shiftr_w32 w 8) :: Word8)
poke (p `plusPtr` 3) (fromIntegral (w) :: Word8)
{-# INLINE writeWord32be #-}
writeWord32le :: Word32 -> Write
writeWord32le w = exactWrite 4 $ \p -> do
poke p (fromIntegral (w) :: Word8)
poke (p `plusPtr` 1) (fromIntegral (shiftr_w32 w 8) :: Word8)
poke (p `plusPtr` 2) (fromIntegral (shiftr_w32 w 16) :: Word8)
poke (p `plusPtr` 3) (fromIntegral (shiftr_w32 w 24) :: Word8)
{-# INLINE writeWord32le #-}
writeWord64be :: Word64 -> Write
#if WORD_SIZE_IN_BITS < 64
writeWord64be w =
let a = fromIntegral (shiftr_w64 w 32) :: Word32
b = fromIntegral w :: Word32
in exactWrite 8 $ \p -> do
poke p (fromIntegral (shiftr_w32 a 24) :: Word8)
poke (p `plusPtr` 1) (fromIntegral (shiftr_w32 a 16) :: Word8)
poke (p `plusPtr` 2) (fromIntegral (shiftr_w32 a 8) :: Word8)
poke (p `plusPtr` 3) (fromIntegral (a) :: Word8)
poke (p `plusPtr` 4) (fromIntegral (shiftr_w32 b 24) :: Word8)
poke (p `plusPtr` 5) (fromIntegral (shiftr_w32 b 16) :: Word8)
poke (p `plusPtr` 6) (fromIntegral (shiftr_w32 b 8) :: Word8)
poke (p `plusPtr` 7) (fromIntegral (b) :: Word8)
#else
writeWord64be w = exactWrite 8 $ \p -> do
poke p (fromIntegral (shiftr_w64 w 56) :: Word8)
poke (p `plusPtr` 1) (fromIntegral (shiftr_w64 w 48) :: Word8)
poke (p `plusPtr` 2) (fromIntegral (shiftr_w64 w 40) :: Word8)
poke (p `plusPtr` 3) (fromIntegral (shiftr_w64 w 32) :: Word8)
poke (p `plusPtr` 4) (fromIntegral (shiftr_w64 w 24) :: Word8)
poke (p `plusPtr` 5) (fromIntegral (shiftr_w64 w 16) :: Word8)
poke (p `plusPtr` 6) (fromIntegral (shiftr_w64 w 8) :: Word8)
poke (p `plusPtr` 7) (fromIntegral (w) :: Word8)
#endif
{-# INLINE writeWord64be #-}
writeWord64le :: Word64 -> Write
#if WORD_SIZE_IN_BITS < 64
writeWord64le w =
let b = fromIntegral (shiftr_w64 w 32) :: Word32
a = fromIntegral w :: Word32
in exactWrite 8 $ \p -> do
poke (p) (fromIntegral (a) :: Word8)
poke (p `plusPtr` 1) (fromIntegral (shiftr_w32 a 8) :: Word8)
poke (p `plusPtr` 2) (fromIntegral (shiftr_w32 a 16) :: Word8)
poke (p `plusPtr` 3) (fromIntegral (shiftr_w32 a 24) :: Word8)
poke (p `plusPtr` 4) (fromIntegral (b) :: Word8)
poke (p `plusPtr` 5) (fromIntegral (shiftr_w32 b 8) :: Word8)
poke (p `plusPtr` 6) (fromIntegral (shiftr_w32 b 16) :: Word8)
poke (p `plusPtr` 7) (fromIntegral (shiftr_w32 b 24) :: Word8)
#else
writeWord64le w = exactWrite 8 $ \p -> do
poke p (fromIntegral (w) :: Word8)
poke (p `plusPtr` 1) (fromIntegral (shiftr_w64 w 8) :: Word8)
poke (p `plusPtr` 2) (fromIntegral (shiftr_w64 w 16) :: Word8)
poke (p `plusPtr` 3) (fromIntegral (shiftr_w64 w 24) :: Word8)
poke (p `plusPtr` 4) (fromIntegral (shiftr_w64 w 32) :: Word8)
poke (p `plusPtr` 5) (fromIntegral (shiftr_w64 w 40) :: Word8)
poke (p `plusPtr` 6) (fromIntegral (shiftr_w64 w 48) :: Word8)
poke (p `plusPtr` 7) (fromIntegral (shiftr_w64 w 56) :: Word8)
#endif
{-# INLINE writeWord64le #-}
writeWordhost :: Word -> Write
writeWordhost w =
exactWrite (sizeOf (undefined :: Word)) (\p -> poke (castPtr p) w)
{-# INLINE writeWordhost #-}
writeWord16host :: Word16 -> Write
writeWord16host w16 =
exactWrite (sizeOf (undefined :: Word16)) (\p -> poke (castPtr p) w16)
{-# INLINE writeWord16host #-}
writeWord32host :: Word32 -> Write
writeWord32host w32 =
exactWrite (sizeOf (undefined :: Word32)) (\p -> poke (castPtr p) w32)
{-# INLINE writeWord32host #-}
writeWord64host :: Word64 -> Write
writeWord64host w =
exactWrite (sizeOf (undefined :: Word64)) (\p -> poke (castPtr p) w)
{-# INLINE writeWord64host #-}
fromWord8 :: Word8 -> Builder
fromWord8 = fromWriteSingleton writeWord8
fromWord8s :: [Word8] -> Builder
fromWord8s = fromWriteList writeWord8
fromWord16be :: Word16 -> Builder
fromWord16be = fromWriteSingleton writeWord16be
{-# INLINE fromWord16be #-}
fromWord16sbe :: [Word16] -> Builder
fromWord16sbe = fromWriteList writeWord16be
{-# INLINE fromWord16sbe #-}
fromWord16le :: Word16 -> Builder
fromWord16le = fromWriteSingleton writeWord16le
{-# INLINE fromWord16le #-}
fromWord16sle :: [Word16] -> Builder
fromWord16sle = fromWriteList writeWord16le
{-# INLINE fromWord16sle #-}
fromWord32be :: Word32 -> Builder
fromWord32be = fromWriteSingleton writeWord32be
{-# INLINE fromWord32be #-}
fromWord32sbe :: [Word32] -> Builder
fromWord32sbe = fromWriteList writeWord32be
{-# INLINE fromWord32sbe #-}
fromWord32le :: Word32 -> Builder
fromWord32le = fromWriteSingleton writeWord32le
{-# INLINE fromWord32le #-}
fromWord32sle :: [Word32] -> Builder
fromWord32sle = fromWriteList writeWord32le
{-# INLINE fromWord32sle #-}
fromWord64be :: Word64 -> Builder
fromWord64be = fromWriteSingleton writeWord64be
{-# INLINE fromWord64be #-}
fromWord64sbe :: [Word64] -> Builder
fromWord64sbe = fromWriteList writeWord64be
{-# INLINE fromWord64sbe #-}
fromWord64le :: Word64 -> Builder
fromWord64le = fromWriteSingleton writeWord64le
{-# INLINE fromWord64le #-}
fromWord64sle :: [Word64] -> Builder
fromWord64sle = fromWriteList writeWord64le
{-# INLINE fromWord64sle #-}
fromWordhost :: Word -> Builder
fromWordhost = fromWriteSingleton writeWordhost
{-# INLINE fromWordhost #-}
fromWordshost :: [Word] -> Builder
fromWordshost = fromWriteList writeWordhost
{-# INLINE fromWordshost #-}
fromWord16host :: Word16 -> Builder
fromWord16host = fromWriteSingleton writeWord16host
{-# INLINE fromWord16host #-}
fromWord16shost :: [Word16] -> Builder
fromWord16shost = fromWriteList writeWord16host
{-# INLINE fromWord16shost #-}
fromWord32host :: Word32 -> Builder
fromWord32host = fromWriteSingleton writeWord32host
{-# INLINE fromWord32host #-}
fromWord32shost :: [Word32] -> Builder
fromWord32shost = fromWriteList writeWord32host
{-# INLINE fromWord32shost #-}
fromWord64host :: Word64 -> Builder
fromWord64host = fromWriteSingleton writeWord64host
{-# INLINE fromWord64host #-}
fromWord64shost :: [Word64] -> Builder
fromWord64shost = fromWriteList writeWord64host
{-# INLINE fromWord64shost #-}