# Changeset View

Changeset View

# Standalone View

Standalone View

# testsuite/tests/primops/should_run/ArithInt8.hs

- This file was added.

1 | {-# LANGUAGE BangPatterns #-} | ||||
---|---|---|---|---|---|

2 | {-# LANGUAGE MagicHash #-} | ||||

3 | {-# LANGUAGE UnboxedTuples #-} | ||||

4 | | ||||

5 | module Main where | ||||

6 | | ||||

7 | import Data.Int | ||||

8 | import Data.List | ||||

9 | import GHC.Prim | ||||

10 | import GHC.Exts | ||||

11 | | ||||

12 | main :: IO () | ||||

13 | main = do | ||||

14 | | ||||

15 | -- | ||||

16 | -- Check if passing Int8# on the stack works (16 parameter function will | ||||

17 | -- need to use stack for some of the them) | ||||

18 | -- | ||||

19 | let input = | ||||

20 | [ ( (a + 0), (a + 1), (a + 2), (a + 3), | ||||

21 | (a + 4), (a + 5), (a + 6), (a + 7), | ||||

22 | (a + 8), (a + 9), (a + 10), (a + 11), | ||||

23 | (a + 12), (a + 13), (a + 14), (a + 15) ) | ||||

24 | | a <- allInt8 | ||||

25 | ] | ||||

26 | expected = | ||||

27 | [ toInt8 | ||||

28 | (a + b + c + d + e + f + g + h + | ||||

29 | i + j + k + l + m + n + o + p) | ||||

30 | | (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) <- input | ||||

31 | ] | ||||

32 | actual = | ||||

33 | [ addMany a b c d e f g h i j k l m n o p | ||||

34 | | (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) <- input | ||||

35 | ] | ||||

36 | checkResults "passing Int8# on the stack" input expected actual | ||||

37 | | ||||

38 | -- | ||||

39 | -- negateInt8# | ||||

40 | -- | ||||

41 | let input = allInt8 | ||||

42 | expected = [ toInt8 (negate a) | a <- input ] | ||||

43 | actual = [ apply1 negateInt8# a | a <- input ] | ||||

44 | checkResults "negateInt8#" input expected actual | ||||

45 | | ||||

46 | -- | ||||

47 | -- plusInt8# | ||||

48 | -- | ||||

49 | let input = [ (a, b) | a <- allInt8, b <- allInt8 ] | ||||

50 | expected = [ toInt8 (a + b) | (a, b) <- input ] | ||||

51 | actual = [ apply2 plusInt8# a b | (a, b) <- input ] | ||||

52 | checkResults "plusInt8#" input expected actual | ||||

53 | | ||||

54 | -- | ||||

55 | -- subInt8# | ||||

56 | -- | ||||

57 | let input = [ (a, b) | a <- allInt8, b <- allInt8 ] | ||||

58 | expected = [ toInt8 (a - b) | (a, b) <- input ] | ||||

59 | actual = [ apply2 subInt8# a b | (a, b) <- input ] | ||||

60 | checkResults "subInt8#" input expected actual | ||||

61 | | ||||

62 | -- | ||||

63 | -- timesInt8# | ||||

64 | -- | ||||

65 | let input = [ (a, b) | a <- allInt8, b <- allInt8 ] | ||||

66 | expected = [ toInt8 (a * b) | (a, b) <- input ] | ||||

67 | actual = [ apply2 timesInt8# a b | (a, b) <- input ] | ||||

68 | checkResults "timesInt8#" input expected actual | ||||

69 | | ||||

70 | -- | ||||

71 | -- remInt8# | ||||

72 | -- | ||||

73 | let input = | ||||

74 | [ (a, b) | a <- allInt8, b <- allInt8 | ||||

75 | -- Don't divide by 0 or cause overflow | ||||

76 | , b /= 0, not (a == -128 && b == -1) | ||||

77 | ] | ||||

78 | expected = [ toInt8 (a `rem` b) | (a, b) <- input ] | ||||

79 | actual = [ apply2 remInt8# a b | (a, b) <- input ] | ||||

80 | checkResults "remInt8#" input expected actual | ||||

81 | | ||||

82 | -- | ||||

83 | -- quotInt8# | ||||

84 | -- | ||||

85 | let input = | ||||

86 | [ (a, b) | a <- allInt8, b <- allInt8 | ||||

87 | , b /= 0, not (a == -128 && b == -1) | ||||

88 | ] | ||||

89 | expected = [ toInt8 (a `quot` b) | (a, b) <- input ] | ||||

90 | actual = [ apply2 quotInt8# a b | (a, b) <- input ] | ||||

91 | checkResults "quotInt8#" input expected actual | ||||

92 | | ||||

93 | -- | ||||

94 | -- quotRemInt8# | ||||

95 | -- | ||||

96 | let input = | ||||

97 | [ (a, b) | a <- allInt8, b <- allInt8 | ||||

98 | , b /= 0, not (a == -128 && b == -1) | ||||

99 | ] | ||||

100 | expected = | ||||

101 | [ (toInt8 q, toInt8 r) | (a, b) <- input | ||||

102 | , let (q, r) = a `quotRem` b | ||||

103 | ] | ||||

104 | actual = [ apply3 quotRemInt8# a b | (a, b) <- input ] | ||||

105 | checkResults "quotRemInt8#" input expected actual | ||||

106 | | ||||

107 | | ||||

108 | checkResults | ||||

109 | :: (Eq a, Eq b, Show a, Show b) => String -> [a] -> [b] -> [b] -> IO () | ||||

110 | checkResults test inputs expected actual = | ||||

111 | case findIndex (\(e, a) -> e /= a) (zip expected actual) of | ||||

112 | Nothing -> putStrLn $ "Pass: " ++ test | ||||

113 | Just i -> error $ | ||||

114 | "FAILED: " ++ test ++ " for input: " ++ show (inputs !! i) | ||||

115 | ++ " expected: " ++ show (expected !! i) | ||||

116 | ++ " but got: " ++ show (actual !! i) | ||||

117 | | ||||

118 | allInt8 :: [Int] | ||||

119 | allInt8 = [ minInt8 .. maxInt8 ] | ||||

120 | | ||||

121 | minInt8 :: Int | ||||

122 | minInt8 = fromIntegral (minBound :: Int8) | ||||

123 | | ||||

124 | maxInt8 :: Int | ||||

125 | maxInt8 = fromIntegral (maxBound :: Int8) | ||||

126 | | ||||

127 | toInt8 :: Int -> Int | ||||

128 | toInt8 a = fromIntegral (fromIntegral a :: Int8) | ||||

129 | | ||||

130 | addMany# | ||||

131 | :: Int8# -> Int8# -> Int8# -> Int8# | ||||

132 | -> Int8# -> Int8# -> Int8# -> Int8# | ||||

133 | -> Int8# -> Int8# -> Int8# -> Int8# | ||||

134 | -> Int8# -> Int8# -> Int8# -> Int8# | ||||

135 | -> Int8# | ||||

136 | addMany# a b c d e f g h i j k l m n o p = | ||||

137 | a `plusInt8#` b `plusInt8#` c `plusInt8#` d `plusInt8#` | ||||

138 | e `plusInt8#` f `plusInt8#` g `plusInt8#` h `plusInt8#` | ||||

139 | i `plusInt8#` j `plusInt8#` k `plusInt8#` l `plusInt8#` | ||||

140 | m `plusInt8#` n `plusInt8#` o `plusInt8#` p | ||||

141 | {-# NOINLINE addMany# #-} | ||||

142 | | ||||

143 | addMany | ||||

144 | :: Int -> Int -> Int -> Int | ||||

145 | -> Int -> Int -> Int -> Int | ||||

146 | -> Int -> Int -> Int -> Int | ||||

147 | -> Int -> Int -> Int -> Int | ||||

148 | -> Int | ||||

149 | addMany (I# a) (I# b) (I# c) (I# d) | ||||

150 | (I# e) (I# f) (I# g) (I# h) | ||||

151 | (I# i) (I# j) (I# k) (I# l) | ||||

152 | (I# m) (I# n) (I# o) (I# p) | ||||

153 | = I# (extendInt8# int8) | ||||

154 | where | ||||

155 | !int8 = addMany# | ||||

156 | (narrowInt8# a) (narrowInt8# b) (narrowInt8# c) (narrowInt8# d) | ||||

157 | (narrowInt8# e) (narrowInt8# f) (narrowInt8# g) (narrowInt8# h) | ||||

158 | (narrowInt8# i) (narrowInt8# j) (narrowInt8# k) (narrowInt8# l) | ||||

159 | (narrowInt8# m) (narrowInt8# n) (narrowInt8# o) (narrowInt8# p) | ||||

160 | {-# NOINLINE addMany #-} | ||||

161 | | ||||

162 | -- Convenient and also tests higher order functions on Int8# | ||||

163 | apply1 :: (Int8# -> Int8#) -> Int -> Int | ||||

164 | apply1 opToTest (I# a) = I# (extendInt8# (opToTest (narrowInt8# a))) | ||||

165 | {-# NOINLINE apply1 #-} | ||||

166 | | ||||

167 | apply2 :: (Int8# -> Int8# -> Int8#) -> Int -> Int -> Int | ||||

168 | apply2 opToTest (I# a) (I# b) = | ||||

169 | let (# sa, sb #) = (# narrowInt8# a, narrowInt8# b #) | ||||

170 | r = opToTest sa sb | ||||

171 | in I# (extendInt8# r) | ||||

172 | {-# NOINLINE apply2 #-} | ||||

173 | | ||||

174 | apply3 :: (Int8# -> Int8# -> (# Int8#, Int8# #)) -> Int -> Int -> (Int, Int) | ||||

175 | apply3 opToTest (I# a) (I# b) = | ||||

176 | let (# sa, sb #) = (# narrowInt8# a, narrowInt8# b #) | ||||

177 | (# ra, rb #) = opToTest sa sb | ||||

178 | in (I# (extendInt8# ra), I# (extendInt8# rb)) | ||||

179 | {-# NOINLINE apply3 #-} | ||||

180 | | ||||

181 | instance | ||||

182 | (Eq a, Eq b, Eq c, Eq d, Eq e, Eq f, Eq g, Eq h, | ||||

183 | Eq i, Eq j, Eq k, Eq l, Eq m, Eq n, Eq o, Eq p) | ||||

184 | => Eq (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) where | ||||

185 | (a1, b1, c1, d1, e1, f1, g1, h1, i1, j1, k1, l1, m1, n1, o1, p1) == | ||||

186 | (a2, b2, c2, d2, e2, f2, g2, h2, i2, j2, k2, l2, m2, n2, o2, p2) = | ||||

187 | a1 == a2 && b1 == b2 && c1 == c2 && d1 == d2 && | ||||

188 | e1 == e2 && f1 == f2 && g1 == g2 && h1 == h2 && | ||||

189 | i1 == i2 && j1 == j2 && k1 == k2 && l1 == l2 && | ||||

190 | m1 == m2 && n1 == n2 && o1 == o2 && p1 == p2 | ||||

191 | | ||||

192 | instance | ||||

193 | (Show a, Show b, Show c, Show d, Show e, Show f, Show g, Show h, | ||||

194 | Show i, Show j, Show k, Show l, Show m, Show n, Show o, Show p) | ||||

195 | => Show (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) where | ||||

196 | show (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) = | ||||

197 | "(" ++ show a ++ "," ++ show b ++ "," ++ show c ++ "," ++ show d ++ | ||||

198 | "," ++ show e ++ "," ++ show f ++ "," ++ show g ++ "," ++ show h ++ | ||||

199 | "," ++ show i ++ "," ++ show j ++ "," ++ show k ++ "," ++ show l ++ | ||||

200 | "," ++ show m ++ "," ++ show n ++ "," ++ show o ++ "," ++ show p ++ | ||||

201 | ")" |