Built-in Natural literals in Core
[ghc.git] / libraries / base / GHC / Natural.hs
1 {-# LANGUAGE NoImplicitPrelude #-}
2 {-# LANGUAGE BangPatterns #-}
3 {-# LANGUAGE CPP #-}
4 {-# LANGUAGE MagicHash #-}
5 {-# LANGUAGE UnboxedTuples #-}
6
7 -----------------------------------------------------------------------------
8 -- |
9 -- Module : GHC.Natural
10 -- Copyright : (C) 2014 Herbert Valerio Riedel,
11 -- (C) 2011 Edward Kmett
12 -- License : see libraries/base/LICENSE
13 --
14 -- Maintainer : libraries@haskell.org
15 -- Stability : internal
16 -- Portability : non-portable (GHC Extensions)
17 --
18 -- The arbitrary-precision 'Natural' number type.
19 --
20 -- __Note__: This is an internal GHC module with an API subject to
21 -- change. It's recommended use the "Numeric.Natural" module to import
22 -- the 'Natural' type.
23 --
24 -- @since 4.8.0.0
25 -----------------------------------------------------------------------------
26 module GHC.Natural
27 ( -- * The 'Natural' number type
28 --
29 -- | __Warning__: The internal implementation of 'Natural'
30 -- (i.e. which constructors are available) depends on the
31 -- 'Integer' backend used!
32 Natural(..)
33 , mkNatural
34 , isValidNatural
35 -- * Arithmetic
36 , plusNatural
37 , minusNatural
38 , minusNaturalMaybe
39 , timesNatural
40 , negateNatural
41 , signumNatural
42 , quotRemNatural
43 , quotNatural
44 , remNatural
45 #if defined(MIN_VERSION_integer_gmp)
46 , gcdNatural
47 , lcmNatural
48 #endif
49 -- * Bits
50 , andNatural
51 , orNatural
52 , xorNatural
53 , bitNatural
54 , testBitNatural
55 #if defined(MIN_VERSION_integer_gmp)
56 , popCountNatural
57 #endif
58 , shiftLNatural
59 , shiftRNatural
60 -- * Conversions
61 , naturalToInteger
62 , naturalToWord
63 , naturalToInt
64 , naturalFromInteger
65 , wordToNatural
66 , intToNatural
67 , naturalToWordMaybe
68 , wordToNatural#
69 , wordToNaturalBase
70 -- * Modular arithmetic
71 , powModNatural
72 ) where
73
74 #include "MachDeps.h"
75
76 import GHC.Classes
77 import GHC.Maybe
78 import GHC.Types
79 import GHC.Prim
80 import {-# SOURCE #-} GHC.Exception.Type (underflowException, divZeroException)
81 #if defined(MIN_VERSION_integer_gmp)
82 import GHC.Integer.GMP.Internals
83 #else
84 import GHC.Integer
85 #endif
86
87 default ()
88
89 -- Most high-level operations need to be marked `NOINLINE` as
90 -- otherwise GHC doesn't recognize them and fails to apply constant
91 -- folding to `Natural`-typed expression.
92 --
93 -- To this end, the CPP hack below allows to write the pseudo-pragma
94 --
95 -- {-# CONSTANT_FOLDED plusNatural #-}
96 --
97 -- which is simply expanded into a
98 --
99 -- {-# NOINLINE plusNatural #-}
100 --
101 #define CONSTANT_FOLDED NOINLINE
102
103 -------------------------------------------------------------------------------
104 -- Arithmetic underflow
105 -------------------------------------------------------------------------------
106
107 -- We put them here because they are needed relatively early
108 -- in the libraries before the Exception type has been defined yet.
109
110 {-# NOINLINE underflowError #-}
111 underflowError :: a
112 underflowError = raise# underflowException
113
114 {-# NOINLINE divZeroError #-}
115 divZeroError :: a
116 divZeroError = raise# divZeroException
117
118 -------------------------------------------------------------------------------
119 -- Natural type
120 -------------------------------------------------------------------------------
121
122 #if defined(MIN_VERSION_integer_gmp)
123 -- TODO: if saturated arithmetic is to used, replace 'underflowError' by '0'
124
125 -- | Type representing arbitrary-precision non-negative integers.
126 --
127 -- >>> 2^20 :: Natural
128 -- 1267650600228229401496703205376
129 --
130 -- Operations whose result would be negative @'throw' ('Underflow' :: 'ArithException')@,
131 --
132 -- >>> -1 :: Natural
133 -- *** Exception: arithmetic underflow
134 --
135 -- @since 4.8.0.0
136 data Natural = NatS# GmpLimb# -- ^ in @[0, maxBound::Word]@
137 | NatJ# {-# UNPACK #-} !BigNat -- ^ in @]maxBound::Word, +inf[@
138 --
139 -- __Invariant__: 'NatJ#' is used
140 -- /iff/ value doesn't fit in
141 -- 'NatS#' constructor.
142 -- NB: Order of constructors *must*
143 -- coincide with 'Ord' relation
144 deriving ( Eq -- ^ @since 4.8.0.0
145 , Ord -- ^ @since 4.8.0.0
146 )
147
148
149 -- | Test whether all internal invariants are satisfied by 'Natural' value
150 --
151 -- This operation is mostly useful for test-suites and/or code which
152 -- constructs 'Integer' values directly.
153 --
154 -- @since 4.8.0.0
155 isValidNatural :: Natural -> Bool
156 isValidNatural (NatS# _) = True
157 isValidNatural (NatJ# bn) = isTrue# (isValidBigNat# bn)
158 && isTrue# (sizeofBigNat# bn ># 0#)
159
160 signumNatural :: Natural -> Natural
161 signumNatural (NatS# 0##) = NatS# 0##
162 signumNatural _ = NatS# 1##
163 {-# CONSTANT_FOLDED signumNatural #-}
164
165 negateNatural :: Natural -> Natural
166 negateNatural (NatS# 0##) = NatS# 0##
167 negateNatural _ = underflowError
168 {-# CONSTANT_FOLDED negateNatural #-}
169
170 -- | @since 4.10.0.0
171 naturalFromInteger :: Integer -> Natural
172 naturalFromInteger (S# i#)
173 | isTrue# (i# >=# 0#) = NatS# (int2Word# i#)
174 naturalFromInteger (Jp# bn) = bigNatToNatural bn
175 naturalFromInteger _ = underflowError
176 {-# CONSTANT_FOLDED naturalFromInteger #-}
177
178 -- | Compute greatest common divisor.
179 gcdNatural :: Natural -> Natural -> Natural
180 gcdNatural (NatS# 0##) y = y
181 gcdNatural x (NatS# 0##) = x
182 gcdNatural (NatS# 1##) _ = NatS# 1##
183 gcdNatural _ (NatS# 1##) = NatS# 1##
184 gcdNatural (NatJ# x) (NatJ# y) = bigNatToNatural (gcdBigNat x y)
185 gcdNatural (NatJ# x) (NatS# y) = NatS# (gcdBigNatWord x y)
186 gcdNatural (NatS# x) (NatJ# y) = NatS# (gcdBigNatWord y x)
187 gcdNatural (NatS# x) (NatS# y) = NatS# (gcdWord x y)
188
189 -- | compute least common multiplier.
190 lcmNatural :: Natural -> Natural -> Natural
191 lcmNatural (NatS# 0##) _ = NatS# 0##
192 lcmNatural _ (NatS# 0##) = NatS# 0##
193 lcmNatural (NatS# 1##) y = y
194 lcmNatural x (NatS# 1##) = x
195 lcmNatural x y = (x `quotNatural` (gcdNatural x y)) `timesNatural` y
196
197 ----------------------------------------------------------------------------
198
199 quotRemNatural :: Natural -> Natural -> (Natural, Natural)
200 quotRemNatural _ (NatS# 0##) = divZeroError
201 quotRemNatural n (NatS# 1##) = (n,NatS# 0##)
202 quotRemNatural n@(NatS# _) (NatJ# _) = (NatS# 0##, n)
203 quotRemNatural (NatS# n) (NatS# d) = case quotRemWord# n d of
204 (# q, r #) -> (NatS# q, NatS# r)
205 quotRemNatural (NatJ# n) (NatS# d) = case quotRemBigNatWord n d of
206 (# q, r #) -> (bigNatToNatural q, NatS# r)
207 quotRemNatural (NatJ# n) (NatJ# d) = case quotRemBigNat n d of
208 (# q, r #) -> (bigNatToNatural q, bigNatToNatural r)
209 {-# CONSTANT_FOLDED quotRemNatural #-}
210
211 quotNatural :: Natural -> Natural -> Natural
212 quotNatural _ (NatS# 0##) = divZeroError
213 quotNatural n (NatS# 1##) = n
214 quotNatural (NatS# _) (NatJ# _) = NatS# 0##
215 quotNatural (NatS# n) (NatS# d) = NatS# (quotWord# n d)
216 quotNatural (NatJ# n) (NatS# d) = bigNatToNatural (quotBigNatWord n d)
217 quotNatural (NatJ# n) (NatJ# d) = bigNatToNatural (quotBigNat n d)
218 {-# CONSTANT_FOLDED quotNatural #-}
219
220 remNatural :: Natural -> Natural -> Natural
221 remNatural _ (NatS# 0##) = divZeroError
222 remNatural _ (NatS# 1##) = NatS# 0##
223 remNatural n@(NatS# _) (NatJ# _) = n
224 remNatural (NatS# n) (NatS# d) = NatS# (remWord# n d)
225 remNatural (NatJ# n) (NatS# d) = NatS# (remBigNatWord n d)
226 remNatural (NatJ# n) (NatJ# d) = bigNatToNatural (remBigNat n d)
227 {-# CONSTANT_FOLDED remNatural #-}
228
229 -- | @since 4.X.0.0
230 naturalToInteger :: Natural -> Integer
231 naturalToInteger (NatS# w) = wordToInteger w
232 naturalToInteger (NatJ# bn) = Jp# bn
233 {-# CONSTANT_FOLDED naturalToInteger #-}
234
235 andNatural :: Natural -> Natural -> Natural
236 andNatural (NatS# n) (NatS# m) = NatS# (n `and#` m)
237 andNatural (NatS# n) (NatJ# m) = NatS# (n `and#` bigNatToWord m)
238 andNatural (NatJ# n) (NatS# m) = NatS# (bigNatToWord n `and#` m)
239 andNatural (NatJ# n) (NatJ# m) = bigNatToNatural (andBigNat n m)
240 {-# CONSTANT_FOLDED andNatural #-}
241
242 orNatural :: Natural -> Natural -> Natural
243 orNatural (NatS# n) (NatS# m) = NatS# (n `or#` m)
244 orNatural (NatS# n) (NatJ# m) = NatJ# (orBigNat (wordToBigNat n) m)
245 orNatural (NatJ# n) (NatS# m) = NatJ# (orBigNat n (wordToBigNat m))
246 orNatural (NatJ# n) (NatJ# m) = NatJ# (orBigNat n m)
247 {-# CONSTANT_FOLDED orNatural #-}
248
249 xorNatural :: Natural -> Natural -> Natural
250 xorNatural (NatS# n) (NatS# m) = NatS# (n `xor#` m)
251 xorNatural (NatS# n) (NatJ# m) = NatJ# (xorBigNat (wordToBigNat n) m)
252 xorNatural (NatJ# n) (NatS# m) = NatJ# (xorBigNat n (wordToBigNat m))
253 xorNatural (NatJ# n) (NatJ# m) = bigNatToNatural (xorBigNat n m)
254 {-# CONSTANT_FOLDED xorNatural #-}
255
256 bitNatural :: Int# -> Natural
257 bitNatural i#
258 | isTrue# (i# <# WORD_SIZE_IN_BITS#) = NatS# (1## `uncheckedShiftL#` i#)
259 | True = NatJ# (bitBigNat i#)
260 {-# CONSTANT_FOLDED bitNatural #-}
261
262 testBitNatural :: Natural -> Int -> Bool
263 testBitNatural (NatS# w) (I# i#)
264 | isTrue# (i# <# WORD_SIZE_IN_BITS#) =
265 isTrue# ((w `and#` (1## `uncheckedShiftL#` i#)) `neWord#` 0##)
266 | True = False
267 testBitNatural (NatJ# bn) (I# i#) = testBitBigNat bn i#
268 {-# CONSTANT_FOLDED testBitNatural #-}
269
270 popCountNatural :: Natural -> Int
271 popCountNatural (NatS# w) = I# (word2Int# (popCnt# w))
272 popCountNatural (NatJ# bn) = I# (popCountBigNat bn)
273 {-# CONSTANT_FOLDED popCountNatural #-}
274
275 shiftLNatural :: Natural -> Int -> Natural
276 shiftLNatural n (I# 0#) = n
277 shiftLNatural (NatS# 0##) _ = NatS# 0##
278 shiftLNatural (NatS# 1##) (I# i#) = bitNatural i#
279 shiftLNatural (NatS# w) (I# i#)
280 = bigNatToNatural (shiftLBigNat (wordToBigNat w) i#)
281 shiftLNatural (NatJ# bn) (I# i#)
282 = bigNatToNatural (shiftLBigNat bn i#)
283 {-# CONSTANT_FOLDED shiftLNatural #-}
284
285 shiftRNatural :: Natural -> Int -> Natural
286 shiftRNatural n (I# 0#) = n
287 shiftRNatural (NatS# w) (I# i#)
288 | isTrue# (i# >=# WORD_SIZE_IN_BITS#) = NatS# 0##
289 | True = NatS# (w `uncheckedShiftRL#` i#)
290 shiftRNatural (NatJ# bn) (I# i#) = bigNatToNatural (shiftRBigNat bn i#)
291 {-# CONSTANT_FOLDED shiftRNatural #-}
292
293 ----------------------------------------------------------------------------
294
295 -- | 'Natural' Addition
296 plusNatural :: Natural -> Natural -> Natural
297 plusNatural (NatS# 0##) y = y
298 plusNatural x (NatS# 0##) = x
299 plusNatural (NatS# x) (NatS# y)
300 = case plusWord2# x y of
301 (# 0##, l #) -> NatS# l
302 (# h, l #) -> NatJ# (wordToBigNat2 h l)
303 plusNatural (NatS# x) (NatJ# y) = NatJ# (plusBigNatWord y x)
304 plusNatural (NatJ# x) (NatS# y) = NatJ# (plusBigNatWord x y)
305 plusNatural (NatJ# x) (NatJ# y) = NatJ# (plusBigNat x y)
306 {-# CONSTANT_FOLDED plusNatural #-}
307
308 -- | 'Natural' multiplication
309 timesNatural :: Natural -> Natural -> Natural
310 timesNatural _ (NatS# 0##) = NatS# 0##
311 timesNatural (NatS# 0##) _ = NatS# 0##
312 timesNatural x (NatS# 1##) = x
313 timesNatural (NatS# 1##) y = y
314 timesNatural (NatS# x) (NatS# y) = case timesWord2# x y of
315 (# 0##, 0## #) -> NatS# 0##
316 (# 0##, xy #) -> NatS# xy
317 (# h , l #) -> NatJ# (wordToBigNat2 h l)
318 timesNatural (NatS# x) (NatJ# y) = NatJ# (timesBigNatWord y x)
319 timesNatural (NatJ# x) (NatS# y) = NatJ# (timesBigNatWord x y)
320 timesNatural (NatJ# x) (NatJ# y) = NatJ# (timesBigNat x y)
321 {-# CONSTANT_FOLDED timesNatural #-}
322
323 -- | 'Natural' subtraction. May @'throw' 'Underflow'@.
324 minusNatural :: Natural -> Natural -> Natural
325 minusNatural x (NatS# 0##) = x
326 minusNatural (NatS# x) (NatS# y) = case subWordC# x y of
327 (# l, 0# #) -> NatS# l
328 _ -> underflowError
329 minusNatural (NatS# _) (NatJ# _) = underflowError
330 minusNatural (NatJ# x) (NatS# y)
331 = bigNatToNatural (minusBigNatWord x y)
332 minusNatural (NatJ# x) (NatJ# y)
333 = bigNatToNatural (minusBigNat x y)
334 {-# CONSTANT_FOLDED minusNatural #-}
335
336 -- | 'Natural' subtraction. Returns 'Nothing's for non-positive results.
337 --
338 -- @since 4.8.0.0
339 minusNaturalMaybe :: Natural -> Natural -> Maybe Natural
340 minusNaturalMaybe x (NatS# 0##) = Just x
341 minusNaturalMaybe (NatS# x) (NatS# y) = case subWordC# x y of
342 (# l, 0# #) -> Just (NatS# l)
343 _ -> Nothing
344 minusNaturalMaybe (NatS# _) (NatJ# _) = Nothing
345 minusNaturalMaybe (NatJ# x) (NatS# y)
346 = Just (bigNatToNatural (minusBigNatWord x y))
347 minusNaturalMaybe (NatJ# x) (NatJ# y)
348 | isTrue# (isNullBigNat# res) = Nothing
349 | True = Just (bigNatToNatural res)
350 where
351 res = minusBigNat x y
352
353 -- | Convert 'BigNat' to 'Natural'.
354 -- Throws 'Underflow' if passed a 'nullBigNat'.
355 bigNatToNatural :: BigNat -> Natural
356 bigNatToNatural bn
357 | isTrue# (sizeofBigNat# bn ==# 1#) = NatS# (bigNatToWord bn)
358 | isTrue# (isNullBigNat# bn) = underflowError
359 | True = NatJ# bn
360
361 naturalToBigNat :: Natural -> BigNat
362 naturalToBigNat (NatS# w#) = wordToBigNat w#
363 naturalToBigNat (NatJ# bn) = bn
364
365 naturalToWord :: Natural -> Word
366 naturalToWord (NatS# w#) = W# w#
367 naturalToWord (NatJ# bn) = W# (bigNatToWord bn)
368
369 naturalToInt :: Natural -> Int
370 naturalToInt (NatS# w#) = I# (word2Int# w#)
371 naturalToInt (NatJ# bn) = I# (bigNatToInt bn)
372
373 ----------------------------------------------------------------------------
374
375 -- | Convert a Word# into a Natural
376 --
377 -- Built-in rule ensures that applications of this function to literal Word# are
378 -- lifted into Natural literals.
379 wordToNatural# :: Word# -> Natural
380 wordToNatural# w# = NatS# w#
381 {-# CONSTANT_FOLDED wordToNatural# #-}
382
383 -- | Convert a Word# into a Natural
384 --
385 -- In base we can't use wordToNatural# as built-in rules transform some of them
386 -- into Natural literals. Use this function instead.
387 wordToNaturalBase :: Word# -> Natural
388 wordToNaturalBase w# = NatS# w#
389
390 #else /* !defined(MIN_VERSION_integer_gmp) */
391 ----------------------------------------------------------------------------
392 -- Use wrapped 'Integer' as fallback; taken from Edward Kmett's nats package
393
394 -- | Type representing arbitrary-precision non-negative integers.
395 --
396 -- Operations whose result would be negative
397 -- @'throw' ('Underflow' :: 'ArithException')@.
398 --
399 -- @since 4.8.0.0
400 newtype Natural = Natural Integer -- ^ __Invariant__: non-negative 'Integer'
401 deriving (Eq,Ord)
402
403
404 -- | Test whether all internal invariants are satisfied by 'Natural' value
405 --
406 -- This operation is mostly useful for test-suites and/or code which
407 -- constructs 'Natural' values directly.
408 --
409 -- @since 4.8.0.0
410 isValidNatural :: Natural -> Bool
411 isValidNatural (Natural i) = i >= wordToInteger 0##
412
413 -- | Convert a Word# into a Natural
414 --
415 -- Built-in rule ensures that applications of this function to literal Word# are
416 -- lifted into Natural literals.
417 wordToNatural# :: Word# -> Natural
418 wordToNatural# w## = Natural (wordToInteger w##)
419 {-# CONSTANT_FOLDED wordToNatural# #-}
420
421 -- | Convert a Word# into a Natural
422 --
423 -- In base we can't use wordToNatural# as built-in rules transform some of them
424 -- into Natural literals. Use this function instead.
425 wordToNaturalBase :: Word# -> Natural
426 wordToNaturalBase w## = Natural (wordToInteger w##)
427
428 -- | @since 4.10.0.0
429 naturalFromInteger :: Integer -> Natural
430 naturalFromInteger n
431 | n >= wordToInteger 0## = Natural n
432 | True = underflowError
433 {-# INLINE naturalFromInteger #-}
434
435 -- | 'Natural' subtraction. Returns 'Nothing's for non-positive results.
436 --
437 -- @since 4.8.0.0
438 minusNaturalMaybe :: Natural -> Natural -> Maybe Natural
439 minusNaturalMaybe (Natural x) (Natural y)
440 | x >= y = Just (Natural (x `minusInteger` y))
441 | True = Nothing
442
443 shiftLNatural :: Natural -> Int -> Natural
444 shiftLNatural (Natural n) (I# i) = Natural (n `shiftLInteger` i)
445 {-# CONSTANT_FOLDED shiftLNatural #-}
446
447 shiftRNatural :: Natural -> Int -> Natural
448 shiftRNatural (Natural n) (I# i) = Natural (n `shiftRInteger` i)
449 {-# CONSTANT_FOLDED shiftRNatural #-}
450
451 plusNatural :: Natural -> Natural -> Natural
452 plusNatural (Natural x) (Natural y) = Natural (x `plusInteger` y)
453 {-# CONSTANT_FOLDED plusNatural #-}
454
455 minusNatural :: Natural -> Natural -> Natural
456 minusNatural (Natural x) (Natural y) = Natural (x `minusInteger` y)
457 {-# CONSTANT_FOLDED minusNatural #-}
458
459 timesNatural :: Natural -> Natural -> Natural
460 timesNatural (Natural x) (Natural y) = Natural (x `timesInteger` y)
461 {-# CONSTANT_FOLDED timesNatural #-}
462
463 orNatural :: Natural -> Natural -> Natural
464 orNatural (Natural x) (Natural y) = Natural (x `orInteger` y)
465 {-# CONSTANT_FOLDED orNatural #-}
466
467 xorNatural :: Natural -> Natural -> Natural
468 xorNatural (Natural x) (Natural y) = Natural (x `xorInteger` y)
469 {-# CONSTANT_FOLDED xorNatural #-}
470
471 andNatural :: Natural -> Natural -> Natural
472 andNatural (Natural x) (Natural y) = Natural (x `andInteger` y)
473 {-# CONSTANT_FOLDED andNatural #-}
474
475 naturalToInt :: Natural -> Int
476 naturalToInt (Natural i) = I# (integerToInt i)
477
478 naturalToWord :: Natural -> Word
479 naturalToWord (Natural i) = W# (integerToWord i)
480
481 naturalToInteger :: Natural -> Integer
482 naturalToInteger (Natural i) = i
483 {-# CONSTANT_FOLDED naturalToInteger #-}
484
485 testBitNatural :: Natural -> Int -> Bool
486 testBitNatural (Natural n) (I# i) = testBitInteger n i
487 {-# CONSTANT_FOLDED testBitNatural #-}
488
489 bitNatural :: Int# -> Natural
490 bitNatural i#
491 | isTrue# (i# <# WORD_SIZE_IN_BITS#) = wordToNaturalBase (1## `uncheckedShiftL#` i#)
492 | True = Natural (1 `shiftLInteger` i#)
493 {-# CONSTANT_FOLDED bitNatural #-}
494
495 quotNatural :: Natural -> Natural -> Natural
496 quotNatural n@(Natural x) (Natural y)
497 | y == wordToInteger 0## = divZeroError
498 | y == wordToInteger 1## = n
499 | True = Natural (x `quotInteger` y)
500 {-# CONSTANT_FOLDED quotNatural #-}
501
502 remNatural :: Natural -> Natural -> Natural
503 remNatural (Natural x) (Natural y)
504 | y == wordToInteger 0## = divZeroError
505 | y == wordToInteger 1## = wordToNaturalBase 0##
506 | True = Natural (x `remInteger` y)
507 {-# CONSTANT_FOLDED remNatural #-}
508
509 quotRemNatural :: Natural -> Natural -> (Natural, Natural)
510 quotRemNatural n@(Natural x) (Natural y)
511 | y == wordToInteger 0## = divZeroError
512 | y == wordToInteger 1## = (n,wordToNaturalBase 0##)
513 | True = case quotRemInteger x y of
514 (# k, r #) -> (Natural k, Natural r)
515 {-# CONSTANT_FOLDED quotRemNatural #-}
516
517 signumNatural :: Natural -> Natural
518 signumNatural (Natural x)
519 | x == wordToInteger 0## = wordToNaturalBase 0##
520 | True = wordToNaturalBase 1##
521 {-# CONSTANT_FOLDED signumNatural #-}
522
523 negateNatural :: Natural -> Natural
524 negateNatural (Natural x)
525 | x == wordToInteger 0## = wordToNaturalBase 0##
526 | True = underflowError
527 {-# CONSTANT_FOLDED negateNatural #-}
528
529 #endif
530
531 -- | Construct 'Natural' from 'Word' value.
532 --
533 -- @since 4.8.0.0
534 wordToNatural :: Word -> Natural
535 wordToNatural (W# w#) = wordToNatural# w#
536
537 -- | Try downcasting 'Natural' to 'Word' value.
538 -- Returns 'Nothing' if value doesn't fit in 'Word'.
539 --
540 -- @since 4.8.0.0
541 naturalToWordMaybe :: Natural -> Maybe Word
542 #if defined(MIN_VERSION_integer_gmp)
543 naturalToWordMaybe (NatS# w#) = Just (W# w#)
544 naturalToWordMaybe (NatJ# _) = Nothing
545 #else
546 naturalToWordMaybe (Natural i)
547 | i < maxw = Just (W# (integerToWord i))
548 | True = Nothing
549 where
550 maxw = 1 `shiftLInteger` WORD_SIZE_IN_BITS#
551 #endif
552
553 -- | \"@'powModNatural' /b/ /e/ /m/@\" computes base @/b/@ raised to
554 -- exponent @/e/@ modulo @/m/@.
555 --
556 -- @since 4.8.0.0
557 powModNatural :: Natural -> Natural -> Natural -> Natural
558 #if defined(MIN_VERSION_integer_gmp)
559 powModNatural _ _ (NatS# 0##) = divZeroError
560 powModNatural _ _ (NatS# 1##) = NatS# 0##
561 powModNatural _ (NatS# 0##) _ = NatS# 1##
562 powModNatural (NatS# 0##) _ _ = NatS# 0##
563 powModNatural (NatS# 1##) _ _ = NatS# 1##
564 powModNatural (NatS# b) (NatS# e) (NatS# m) = NatS# (powModWord b e m)
565 powModNatural b e (NatS# m)
566 = NatS# (powModBigNatWord (naturalToBigNat b) (naturalToBigNat e) m)
567 powModNatural b e (NatJ# m)
568 = bigNatToNatural (powModBigNat (naturalToBigNat b) (naturalToBigNat e) m)
569 #else
570 -- Portable reference fallback implementation
571 powModNatural (Natural b0) (Natural e0) (Natural m)
572 | m == wordToInteger 0## = divZeroError
573 | m == wordToInteger 1## = wordToNaturalBase 0##
574 | e0 == wordToInteger 0## = wordToNaturalBase 1##
575 | b0 == wordToInteger 0## = wordToNaturalBase 0##
576 | b0 == wordToInteger 1## = wordToNaturalBase 1##
577 | True = go b0 e0 (wordToInteger 1##)
578 where
579 go !b e !r
580 | e `testBitInteger` 0# = go b' e' ((r `timesInteger` b) `modInteger` m)
581 | e == wordToInteger 0## = naturalFromInteger r
582 | True = go b' e' r
583 where
584 b' = (b `timesInteger` b) `modInteger` m
585 e' = e `shiftRInteger` 1# -- slightly faster than "e `div` 2"
586 #endif
587
588
589 -- | Construct 'Natural' value from list of 'Word's.
590 --
591 -- This function is used by GHC for constructing 'Natural' literals.
592 mkNatural :: [Word] -- ^ value expressed in 32 bit chunks, least
593 -- significant first
594 -> Natural
595 mkNatural [] = wordToNaturalBase 0##
596 mkNatural (W# i : is') = wordToNaturalBase (i `and#` 0xffffffff##) `orNatural`
597 shiftLNatural (mkNatural is') 31
598 {-# CONSTANT_FOLDED mkNatural #-}
599
600 -- | Convert 'Int' to 'Natural'.
601 -- Throws 'Underflow' when passed a negative 'Int'.
602 intToNatural :: Int -> Natural
603 intToNatural (I# i#)
604 | isTrue# (i# <# 0#) = underflowError
605 | True = wordToNaturalBase (int2Word# i#)