blob: d464f9822bfedb8c29c1b69a280deab43ac83224 (
plain)
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
|
{-# LANGUAGE MagicHash, ExtendedLiterals #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE UnboxedSums, UnboxedTuples #-}
-- needed on 32bit
{-# OPTIONS_GHC -fno-warn-overflowed-literals #-}
module Ex where
import GHC.Exts
import GHC.Word
import GHC.Int
-- Precise 'Int8#'/'Int8' range tests
exI8g1, exI8g2, exI8g3 :: Int8
exI8g1 = I8# 0x00#Int8
exI8g2 = I8# 0x7F#Int8
exI8g3 = I8# -0x80#Int8
-- Showcase various syntax for equivalent 'Int' terms
exIg1, exIg2, exIg3 :: Int
exIg1 = 0x7FFFFFFFFFFFFFFF
exIg2 = I# 0x7FFFFFFFFFFFFFFF#
exIg3 = I# 0x7FFFFFFFFFFFFFFF#Int
-- Motivating example: unboxed 'Word8#' parsing
data CEnum = Cons00 | Cons01 | ConsFF deriving Show
parseCEnum :: Word8# -> (# (##) | CEnum #)
parseCEnum = \case 0x00#Word8 -> (# | Cons00 #)
0x01#Word8 -> (# | Cons01 #)
0xFF#Word8 -> (# | ConsFF #)
_ -> (# (##) | #)
w8ToBool# :: Word8# -> Int#
w8ToBool# = \case 0#Word8 -> 0#
_ -> 1#
i8IsPole# :: Int8# -> Int#
i8IsPole# = \case 0x7F#Int8 -> 1#
-0x80#Int8 -> 1#
_ -> 0#
|