summaryrefslogtreecommitdiff
path: root/libraries/ghc-bignum/src/GHC/Num/Backend/Check.hs
blob: 9e5181c0bbc27c2190c55198421c64507c01a42b (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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
{-# LANGUAGE CPP #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE GHCForeignImportPrim #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE UnliftedFFITypes #-}
{-# LANGUAGE NegativeLiterals #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# OPTIONS_GHC -Wno-name-shadowing #-}

-- | Check Native implementation against another backend
module GHC.Num.Backend.Check where

import GHC.Prim
import GHC.Types
import GHC.Num.WordArray
import GHC.Num.Primitives
import {-# SOURCE #-} GHC.Num.Integer
import qualified GHC.Num.Backend.Native   as Native
import qualified GHC.Num.Backend.Selected as Other

#if defined(BIGNUM_NATIVE)
#error You can't validate Native backend against itself. Choose another backend (e.g. gmp, ffi)
#endif

default ()

bignat_compare
   :: WordArray#
   -> WordArray#
   -> Int#
bignat_compare a b =
   let
      gr = Other.bignat_compare a b
      nr = Native.bignat_compare a b
   in case gr ==# nr of
         0# -> unexpectedValue_Int# (# #)
         _  -> gr

mwaCompare
   :: MutableWordArray# s
   -> MutableWordArray# s
   -> State# s
   -> (# State# s, Bool# #)
mwaCompare mwa mwb s =
   case mwaSize# mwa s of
      (# s, szA #) -> case mwaSize# mwb s of
         (# s, szB #) -> case szA ==# szB of
            0# -> (# s, 0# #)
            _  -> let
                     go i s
                        | isTrue# (i <# 0#) = (# s, 1# #)
                        | True =
                           case readWordArray# mwa i s of
                              (# s, a #) -> case readWordArray# mwb i s of
                                 (# s, b #) -> case a `eqWord#` b of
                                    0# -> (# s, 0# #)
                                    _  -> go (i -# 1#) s
                  in go (szA -# 1#) s

mwaCompareOp
   :: MutableWordArray# s
   -> (MutableWordArray# s -> State# s -> State# s)
   -> (MutableWordArray# s -> State# s -> State# s)
   -> State# s
   -> State# s
mwaCompareOp mwa f g s =
   case mwaSize# mwa s of { (# s, sz #) ->
   case newWordArray# sz s of { (# s, mwb #) ->
   case f mwa s of { s ->
   case g mwb s of { s ->
   case mwaTrimZeroes# mwa s of { s ->
   case mwaTrimZeroes# mwb s of { s ->
   case mwaCompare mwa mwb s of
      (# s, 0# #) -> case unexpectedValue of
                        !_ -> s
                        -- see Note [ghc-bignum exceptions] in
                        -- GHC.Num.Primitives
      (# s, _  #) -> s
   }}}}}}

mwaCompareOp2
   :: MutableWordArray# s
   -> MutableWordArray# s
   -> (MutableWordArray# s -> MutableWordArray# s -> State# s -> State# s)
   -> (MutableWordArray# s -> MutableWordArray# s -> State# s -> State# s)
   -> State# s
   -> State# s
mwaCompareOp2 mwa mwb f g s =
   case mwaSize# mwa s of { (# s, szA #) ->
   case mwaSize# mwb s of { (# s, szB #) ->
   case newWordArray# szA s of { (# s, mwa' #) ->
   case newWordArray# szB s of { (# s, mwb' #) ->
   case f mwa  mwb  s of { s ->
   case g mwa' mwb' s of { s ->
   case mwaTrimZeroes# mwa s of { s ->
   case mwaTrimZeroes# mwb s of { s ->
   case mwaTrimZeroes# mwa' s of { s ->
   case mwaTrimZeroes# mwb' s of { s ->
   case mwaCompare mwa mwa' s of { (# s, ba #) ->
   case mwaCompare mwb mwb' s of { (# s, bb #) ->
   case ba &&# bb of
      0# -> case unexpectedValue of
               !_ -> s
               -- see Note [ghc-bignum exceptions] in GHC.Num.Primitives
      _  -> s
   }}}}}}}}}}}}

mwaCompareOpBool
   :: MutableWordArray# s
   -> (MutableWordArray# s -> State# s -> (#State# s, Bool# #))
   -> (MutableWordArray# s -> State# s -> (#State# s, Bool# #))
   -> State# s
   -> (# State# s, Bool# #)
mwaCompareOpBool mwa f g s =
   case mwaSize# mwa s of { (# s, sz #) ->
   case newWordArray# sz s of { (# s, mwb #) ->
   case f mwa s of { (# s, ra #) ->
   case g mwb s of { (# s, rb #) ->
   case ra ==# rb of
      0# -> case unexpectedValue of
               !_ -> (# s, ra #)
               -- see Note [ghc-bignum exceptions] in GHC.Num.Primitives
      _  -> case ra of -- don't compare MWAs if underflow signaled!
         0# -> (# s, ra #) -- underflow
         _  -> case mwaTrimZeroes# mwa s of { s ->
               case mwaTrimZeroes# mwb s of { s ->
               case mwaCompare mwa mwb s of
                  (# s, 0# #) -> case unexpectedValue of
                                    !_ -> (# s, ra #)
                                    -- see Note [ghc-bignum exceptions] in
                                    -- GHC.Num.Primitives
                  _  -> (# s, ra #)
   }}}}}}

mwaCompareOpWord
   :: MutableWordArray# s
   -> (MutableWordArray# s -> State# s -> (#State# s, Word# #))
   -> (MutableWordArray# s -> State# s -> (#State# s, Word# #))
   -> State# s
   -> (# State# s, Word# #)
mwaCompareOpWord mwa f g s =
   case mwaSize# mwa s of { (# s, sz #) ->
   case newWordArray# sz s of { (# s, mwb #) ->
   case f mwa s of { (# s, ra #) ->
   case g mwb s of { (# s, rb #) ->
   case mwaTrimZeroes# mwa s of { s ->
   case mwaTrimZeroes# mwb s of { s ->
   case mwaCompare mwa mwb s of
      (# s, b #) -> case b &&# (ra `eqWord#` rb) of
         0# -> case unexpectedValue of
                  !_ -> (# s, ra #)
                  -- see Note [ghc-bignum exceptions] in GHC.Num.Primitives
         _  -> (# s, ra #)
   }}}}}}

bignat_add
   :: MutableWordArray# RealWorld -- ^ Result
   -> WordArray#
   -> WordArray#
   -> State# RealWorld
   -> State# RealWorld
bignat_add mwa wa wb
   = mwaCompareOp mwa
      (\m -> Other.bignat_add m wa wb)
      (\m -> Native.bignat_add m wa wb)

bignat_add_word
   :: MutableWordArray# RealWorld -- ^ Result
   -> WordArray#
   -> Word#
   -> State# RealWorld
   -> State# RealWorld
bignat_add_word mwa wa b
   = mwaCompareOp mwa
      (\m -> Other.bignat_add_word m wa b)
      (\m -> Native.bignat_add_word m wa b)

bignat_mul_word
   :: MutableWordArray# RealWorld -- ^ Result
   -> WordArray#
   -> Word#
   -> State# RealWorld
   -> State# RealWorld
bignat_mul_word mwa wa b
   = mwaCompareOp mwa
      (\m -> Other.bignat_mul_word m wa b)
      (\m -> Native.bignat_mul_word m wa b)

bignat_sub
   :: MutableWordArray# RealWorld
   -> WordArray#
   -> WordArray#
   -> State# RealWorld
   -> (# State# RealWorld, Bool# #)
bignat_sub mwa wa wb
   = mwaCompareOpBool mwa
      (\m -> Other.bignat_sub m wa wb)
      (\m -> Native.bignat_sub m wa wb)

bignat_sub_word
   :: MutableWordArray# RealWorld
   -> WordArray#
   -> Word#
   -> State# RealWorld
   -> (# State# RealWorld, Bool# #)
bignat_sub_word mwa wa b
   = mwaCompareOpBool mwa
      (\m -> Other.bignat_sub_word m wa b)
      (\m -> Native.bignat_sub_word m wa b)

bignat_mul
   :: MutableWordArray# RealWorld -- ^ Result
   -> WordArray#
   -> WordArray#
   -> State# RealWorld
   -> State# RealWorld
bignat_mul mwa wa wb
   = mwaCompareOp mwa
      (\m -> Other.bignat_mul m wa wb)
      (\m -> Native.bignat_mul m wa wb)

bignat_popcount :: WordArray# -> Word#
bignat_popcount wa =
   let
      gr = Other.bignat_popcount wa
      nr = Native.bignat_popcount wa
   in case gr `eqWord#` nr of
         0# -> 1## `quotWord#` 0##
         _  -> gr

bignat_shiftl
   :: MutableWordArray# RealWorld
   -> WordArray#
   -> Word#
   -> State# RealWorld
   -> State# RealWorld
bignat_shiftl mwa wa n
   = mwaCompareOp mwa
      (\m -> Other.bignat_shiftl m wa n)
      (\m -> Native.bignat_shiftl m wa n)

bignat_shiftr
   :: MutableWordArray# RealWorld
   -> WordArray#
   -> Word#
   -> State# RealWorld
   -> State# RealWorld
bignat_shiftr mwa wa n
   = mwaCompareOp mwa
      (\m -> Other.bignat_shiftr m wa n)
      (\m -> Native.bignat_shiftr m wa n)

bignat_shiftr_neg
   :: MutableWordArray# RealWorld
   -> WordArray#
   -> Word#
   -> State# RealWorld
   -> State# RealWorld
bignat_shiftr_neg mwa wa n
   = mwaCompareOp mwa
      (\m -> Other.bignat_shiftr_neg m wa n)
      (\m -> Native.bignat_shiftr_neg m wa n)

bignat_or
   :: MutableWordArray# RealWorld
   -> WordArray#
   -> WordArray#
   -> State# RealWorld
   -> State# RealWorld
bignat_or mwa wa wb
   = mwaCompareOp mwa
      (\m -> Other.bignat_or m wa wb)
      (\m -> Native.bignat_or m wa wb)

bignat_xor
   :: MutableWordArray# RealWorld
   -> WordArray#
   -> WordArray#
   -> State# RealWorld
   -> State# RealWorld
bignat_xor mwa wa wb
   = mwaCompareOp mwa
      (\m -> Other.bignat_xor m wa wb)
      (\m -> Native.bignat_xor m wa wb)

bignat_and
   :: MutableWordArray# RealWorld
   -> WordArray#
   -> WordArray#
   -> State# RealWorld
   -> State# RealWorld
bignat_and mwa wa wb
   = mwaCompareOp mwa
      (\m -> Other.bignat_and m wa wb)
      (\m -> Native.bignat_and m wa wb)

bignat_and_not
   :: MutableWordArray# RealWorld
   -> WordArray#
   -> WordArray#
   -> State# RealWorld
   -> State# RealWorld
bignat_and_not mwa wa wb
   = mwaCompareOp mwa
      (\m -> Other.bignat_and_not m wa wb)
      (\m -> Native.bignat_and_not m wa wb)

bignat_quotrem
   :: MutableWordArray# RealWorld
   -> MutableWordArray# RealWorld
   -> WordArray#
   -> WordArray#
   -> State# RealWorld
   -> State# RealWorld
bignat_quotrem mwq mwr wa wb
   = mwaCompareOp2 mwq mwr
      (\m1 m2 -> Other.bignat_quotrem m1 m2 wa wb)
      (\m1 m2 -> Native.bignat_quotrem m1 m2 wa wb)

bignat_quot
   :: MutableWordArray# RealWorld
   -> WordArray#
   -> WordArray#
   -> State# RealWorld
   -> State# RealWorld
bignat_quot mwq wa wb
   = mwaCompareOp mwq
      (\m -> Other.bignat_quot m wa wb)
      (\m -> Native.bignat_quot m wa wb)

bignat_rem
   :: MutableWordArray# RealWorld
   -> WordArray#
   -> WordArray#
   -> State# RealWorld
   -> State# RealWorld
bignat_rem mwr wa wb
   = mwaCompareOp mwr
      (\m -> Other.bignat_rem m wa wb)
      (\m -> Native.bignat_rem m wa wb)

bignat_quotrem_word
   :: MutableWordArray# RealWorld
   -> WordArray#
   -> Word#
   -> State# RealWorld
   -> (# State# RealWorld, Word# #)
bignat_quotrem_word mwq wa b
   = mwaCompareOpWord mwq
      (\m -> Other.bignat_quotrem_word m wa b)
      (\m -> Native.bignat_quotrem_word m wa b)

bignat_quot_word
   :: MutableWordArray# RealWorld
   -> WordArray#
   -> Word#
   -> State# RealWorld
   -> State# RealWorld
bignat_quot_word mwq wa b
   = mwaCompareOp mwq
      (\m -> Other.bignat_quot_word m wa b)
      (\m -> Native.bignat_quot_word m wa b)

bignat_rem_word
   :: WordArray#
   -> Word#
   -> Word#
bignat_rem_word wa b =
   let
      gr = Other.bignat_rem_word wa b
      nr = Native.bignat_rem_word wa b
   in case gr `eqWord#` nr of
       1# -> gr
       _  -> unexpectedValue_Word# (# #)

bignat_gcd
   :: MutableWordArray# RealWorld
   -> WordArray#
   -> WordArray#
   -> State# RealWorld
   -> State# RealWorld
bignat_gcd mwr wa wb
   = mwaCompareOp mwr
      (\m -> Other.bignat_gcd m wa wb)
      (\m -> Native.bignat_gcd m wa wb)

bignat_gcd_word
   :: WordArray#
   -> Word#
   -> Word#
bignat_gcd_word wa b =
   let
      gr = Other.bignat_gcd_word wa b
      nr = Native.bignat_gcd_word wa b
   in case gr `eqWord#` nr of
       1# -> gr
       _  -> unexpectedValue_Word# (# #)

bignat_gcd_word_word
   :: Word#
   -> Word#
   -> Word#
bignat_gcd_word_word a b =
   let
      gr = Other.bignat_gcd_word_word a b
      nr = Native.bignat_gcd_word_word a b
   in case gr `eqWord#` nr of
       1# -> gr
       _  -> unexpectedValue_Word# (# #)

bignat_encode_double :: WordArray# -> Int# -> Double#
bignat_encode_double a e =
   let
      gr = Other.bignat_encode_double a e
      nr = Native.bignat_encode_double a e
   in case gr ==## nr of
       1# -> gr
       _  -> case unexpectedValue of
               !_ -> 0.0##
               -- see Note [ghc-bignum exceptions] in GHC.Num.Primitives

bignat_powmod_word :: WordArray# -> WordArray# -> Word# -> Word#
bignat_powmod_word b e m =
   let
      gr = Other.bignat_powmod_word b e m
      nr = Native.bignat_powmod_word b e m
   in case gr `eqWord#` nr of
       1# -> gr
       _  -> unexpectedValue_Word# (# #)

bignat_powmod
   :: MutableWordArray# RealWorld
   -> WordArray#
   -> WordArray#
   -> WordArray#
   -> State# RealWorld
   -> State# RealWorld
bignat_powmod r b e m
   = mwaCompareOp r
      (\r' -> Other.bignat_powmod r' b e m)
      (\r' -> Native.bignat_powmod r' b e m)

bignat_powmod_words
   :: Word#
   -> Word#
   -> Word#
   -> Word#
bignat_powmod_words b e m =
   let
      gr = Other.bignat_powmod_words b e m
      nr = Native.bignat_powmod_words b e m
   in case gr `eqWord#` nr of
       1# -> gr
       _  -> unexpectedValue_Word# (# #)

integer_gcde
   :: Integer
   -> Integer
   -> (# Integer, Integer, Integer #)
integer_gcde a b =
   let
      !(# g0,x0,y0 #) = Other.integer_gcde a b
      !(# g1,x1,y1 #) = Native.integer_gcde a b
   in if isTrue# (integerEq# x0 x1
                  &&# integerEq# y0 y1
                  &&# integerEq# g0 g1)
         then (# g0, x0, y0 #)
         else case unexpectedValue of
            !_ -> (# integerZero, integerZero, integerZero #)

integer_recip_mod
   :: Integer
   -> Integer
   -> (# Integer | () #)
integer_recip_mod x m =
   let
      !r0 = Other.integer_recip_mod x m
      !r1 = Native.integer_recip_mod x m
   in case (# r0, r1 #) of
         (# (# | () #), (# | () #) #) -> r0
         (# (# y0 | #), (# y1 | #) #)
            | isTrue# (integerEq# y0 y1) -> r0
         _ -> case unexpectedValue of
            !_ -> (# | () #)