1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
-- |
-- Module : Basement.Compat.Primitive
-- License : BSD-style
-- Maintainer : Vincent Hanquez <vincent@snarc.org>
-- Stability : experimental
-- Portability : portable
--
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE UnliftedFFITypes #-}
module Basement.Compat.Primitive
( bool#
, PinnedStatus(..), toPinnedStatus#
, compatMkWeak#
, compatIsByteArrayPinned#
, compatIsMutableByteArrayPinned#
, unsafeCoerce#
, Word(..)
, Word8#
, Word16#
, Word32#
, Int8#
, Int16#
, Int32#
-- word upper sizing
, word8ToWord16#
, word8ToWord32#
, word8ToWord#
, word16ToWord8#
, word16ToWord32#
, word16ToWord#
, word32ToWord#
-- word down sizing
, word32ToWord8#
, word32ToWord16#
, wordToWord32#
, wordToWord16#
, wordToWord8#
-- int upper sizing
, int8ToInt16#
, int8ToInt32#
, int8ToInt#
, int16ToInt32#
, int16ToInt#
, int32ToInt#
-- int down sizing
, intToInt8#
, intToInt16#
, intToInt32#
-- other
, word8ToInt#
, word8ToInt16#
, word8ToInt32#
, charToWord32#
, word8ToChar#
, word16ToChar#
, word32ToChar#
, wordToChar#
-- word8 ops
, plusWord8#
-- word16 ops
, uncheckedShiftRLWord16#
, plusWord16#
-- word32 ops
, uncheckedShiftRLWord32#
, plusWord32#
-- int8 ops
, plusInt8#
-- int16 ops
, plusInt16#
-- int32 ops
, plusInt32#
) where
import qualified Prelude
import GHC.Exts hiding (Word8#, Word16#, Word32#, Int8#, Int16#, Int32#, plusWord8#, plusWord16#, plusInt8#, plusInt16#)
import GHC.Prim hiding (Word8#, Word16#, Word32#, Int8#, Int16#, Int32#, plusWord8#, plusWord16#, plusInt8#, plusInt16#)
import GHC.Word
import GHC.IO
import Basement.Compat.PrimTypes
#if __GLASGOW_HASKELL__ >= 902
import GHC.Exts (Word8#, Word16#, Word32#, Int8#, Int16#, Int32#, plusWord8#, plusWord16#, plusInt8#, plusInt16#)
#endif
-- GHC 9.2 | Base 4.16
-- GHC 9.0 | Base 4.15
-- GHC 8.8 | Base 4.13 4.14
-- GHC 8.6 | Base 4.12
-- GHC 8.4 | Base 4.11
-- GHC 8.2 | Base 4.10
-- GHC 8.0 | Base 4.9
-- GHC 7.10 | Base 4.8
-- GHC 7.8 | Base 4.7
-- GHC 7.6 | Base 4.6
-- GHC 7.4 | Base 4.5
--
-- More complete list:
-- https://wiki.haskell.org/Base_package
-- | Flag record whether a specific byte array is pinned or not
data PinnedStatus = Pinned | Unpinned
deriving (Prelude.Eq)
toPinnedStatus# :: Pinned# -> PinnedStatus
toPinnedStatus# 0# = Unpinned
toPinnedStatus# _ = Pinned
-- | turn an Int# into a Bool
bool# :: Int# -> Prelude.Bool
bool# v = isTrue# v
{-# INLINE bool# #-}
-- | A mkWeak# version that keep working on 8.0
--
-- signature change in ghc-prim:
-- * 0.4: mkWeak# :: o -> b -> c -> State# RealWorld -> (#State# RealWorld, Weak# b#)
-- * 0.5 :mkWeak# :: o -> b -> (State# RealWorld -> (#State# RealWorld, c#)) -> State# RealWorld -> (#State# RealWorld, Weak# b#)
--
compatMkWeak# :: o -> b -> Prelude.IO () -> State# RealWorld -> (#State# RealWorld, Weak# b #)
compatMkWeak# o b c s = mkWeak# o b (case c of { IO f -> f }) s
{-# INLINE compatMkWeak# #-}
#if __GLASGOW_HASKELL__ >= 802
compatIsByteArrayPinned# :: ByteArray# -> Pinned#
compatIsByteArrayPinned# ba = isByteArrayPinned# ba
compatIsMutableByteArrayPinned# :: MutableByteArray# s -> Pinned#
compatIsMutableByteArrayPinned# ba = isMutableByteArrayPinned# ba
#else
foreign import ccall unsafe "basement_is_bytearray_pinned"
compatIsByteArrayPinned# :: ByteArray# -> Pinned#
foreign import ccall unsafe "basement_is_bytearray_pinned"
compatIsMutableByteArrayPinned# :: MutableByteArray# s -> Pinned#
#endif
#if __GLASGOW_HASKELL__ >= 902
word8ToWord16# :: Word8# -> Word16#
word8ToWord16# a = wordToWord16# (word8ToWord# a)
word8ToWord32# :: Word8# -> Word32#
word8ToWord32# a = wordToWord32# (word8ToWord# a)
word16ToWord8# :: Word16# -> Word8#
word16ToWord8# a = wordToWord8# (word16ToWord# a)
word16ToWord32# :: Word16# -> Word32#
word16ToWord32# a = wordToWord32# (word16ToWord# a)
word32ToWord8# :: Word32# -> Word8#
word32ToWord8# a = wordToWord8# (word32ToWord# a)
word32ToWord16# :: Word32# -> Word16#
word32ToWord16# a = wordToWord16# (word32ToWord# a)
int8ToInt16# :: Int8# -> Int16#
int8ToInt16# i = intToInt16# (int8ToInt# i)
int8ToInt32# :: Int8# -> Int32#
int8ToInt32# i = intToInt32# (int8ToInt# i)
int16ToInt32# :: Int16# -> Int32#
int16ToInt32# i = intToInt32# (int16ToInt# i)
word8ToInt16# :: Word8# -> Int16#
word8ToInt16# i = intToInt16# (word2Int# (word8ToWord# i))
word8ToInt32# :: Word8# -> Int32#
word8ToInt32# i = intToInt32# (word2Int# (word8ToWord# i))
word8ToInt# :: Word8# -> Int#
word8ToInt# i = word2Int# (word8ToWord# i)
charToWord32# :: Char# -> Word32#
charToWord32# ch = wordToWord32# (int2Word# (ord# ch))
word8ToChar# :: Word8# -> Char#
word8ToChar# ch = chr# (word2Int# (word8ToWord# ch))
word16ToChar# :: Word16# -> Char#
word16ToChar# ch = chr# (word2Int# (word16ToWord# ch))
word32ToChar# :: Word32# -> Char#
word32ToChar# ch = chr# (word2Int# (word32ToWord# ch))
wordToChar# :: Word# -> Char#
wordToChar# ch = chr# (word2Int# ch)
#else
type Word8# = Word#
type Word16# = Word#
type Word32# = Word#
type Int8# = Int#
type Int16# = Int#
type Int32# = Int#
word8ToWord16# :: Word8# -> Word16#
word8ToWord16# a = a
word8ToWord32# :: Word8# -> Word32#
word8ToWord32# a = a
word8ToWord# :: Word8# -> Word#
word8ToWord# a = a
word16ToWord32# :: Word16# -> Word32#
word16ToWord32# a = a
word16ToWord8# :: Word16# -> Word8#
word16ToWord8# w = narrow8Word# w
word16ToWord# :: Word16# -> Word#
word16ToWord# a = a
word32ToWord8# :: Word32# -> Word8#
word32ToWord8# w = narrow8Word# w
word32ToWord16# :: Word32# -> Word16#
word32ToWord16# w = narrow16Word# w
word32ToWord# :: Word32# -> Word#
word32ToWord# a = a
wordToWord32# :: Word# -> Word32#
wordToWord32# w = narrow32Word# w
wordToWord16# :: Word# -> Word16#
wordToWord16# w = narrow16Word# w
wordToWord8# :: Word# -> Word8#
wordToWord8# w = narrow8Word# w
charToWord32# :: Char# -> Word32#
charToWord32# ch = int2Word# (ord# ch)
word8ToInt16# :: Word8# -> Int16#
word8ToInt16# w = word2Int# w
word8ToInt32# :: Word8# -> Int32#
word8ToInt32# w = word2Int# w
word8ToInt# :: Word8# -> Int#
word8ToInt# w = word2Int# w
word8ToChar# :: Word8# -> Char#
word8ToChar# w = chr# (word2Int# w)
word16ToChar# :: Word16# -> Char#
word16ToChar# w = chr# (word2Int# w)
word32ToChar# :: Word32# -> Char#
word32ToChar# w = chr# (word2Int# w)
wordToChar# :: Word# -> Char#
wordToChar# ch = chr# (word2Int# ch)
int8ToInt16# :: Int8# -> Int16#
int8ToInt16# a = a
int8ToInt32# :: Int8# -> Int32#
int8ToInt32# a = a
int8ToInt# :: Int8# -> Int#
int8ToInt# a = a
int16ToInt32# :: Int16# -> Int32#
int16ToInt32# a = a
int16ToInt# :: Int16# -> Int#
int16ToInt# a = a
int32ToInt# :: Int32# -> Int#
int32ToInt# a = a
intToInt8# :: Int# -> Int8#
intToInt8# i = narrow8Int# i
intToInt16# :: Int# -> Int16#
intToInt16# i = narrow16Int# i
intToInt32# :: Int# -> Int32#
intToInt32# i = narrow32Int# i
uncheckedShiftRLWord16# = uncheckedShiftRL#
uncheckedShiftRLWord32# = uncheckedShiftRL#
plusWord8# :: Word8# -> Word8# -> Word8#
plusWord8# a b = narrow8Word# (plusWord# a b)
plusWord16# :: Word16# -> Word16# -> Word16#
plusWord16# a b = narrow16Word# (plusWord# a b)
plusWord32# :: Word32# -> Word32# -> Word32#
plusWord32# a b = narrow32Word# (plusWord# a b)
plusInt8# :: Int8# -> Int8# -> Int8#
plusInt8# a b = narrow8Int# (a +# b)
plusInt16# :: Int16# -> Int16# -> Int16#
plusInt16# a b = narrow16Int# (a +# b)
plusInt32# :: Int32# -> Int32# -> Int32#
plusInt32# a b = narrow32Int# (a +# b)
#endif
|