diff options
Diffstat (limited to 'libraries/ghc-bignum/src/GHC/Num/BigNat.hs')
-rw-r--r-- | libraries/ghc-bignum/src/GHC/Num/BigNat.hs | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/libraries/ghc-bignum/src/GHC/Num/BigNat.hs b/libraries/ghc-bignum/src/GHC/Num/BigNat.hs index f2d9e58e17..b2a84b323c 100644 --- a/libraries/ghc-bignum/src/GHC/Num/BigNat.hs +++ b/libraries/ghc-bignum/src/GHC/Num/BigNat.hs @@ -1520,3 +1520,42 @@ bigNatFromByteArrayBE# sz ba moff s = bigNatFromByteArray# :: Word# -> ByteArray# -> Word# -> Bool# -> State# s -> (# State# s, BigNat# #) bigNatFromByteArray# sz ba off 0# s = bigNatFromByteArrayLE# sz ba off s bigNatFromByteArray# sz ba off _ s = bigNatFromByteArrayBE# sz ba off s + + + + +-- | Create a BigNat# from a WordArray# containing /n/ limbs in +-- least-significant-first order. +-- +-- If possible 'WordArray#', will be used directly (i.e. shared +-- /without/ cloning the 'WordArray#' into a newly allocated one) +bigNatFromWordArray# :: WordArray# -> Word# -> BigNat# +bigNatFromWordArray# wa n0 + | isTrue# (n `eqWord#` 0##) + = bigNatZero# (# #) + + | isTrue# (r `eqWord#` 0##) -- i.e. wa is multiple of limb-size + , isTrue# (q `eqWord#` n) + = wa + + | True = withNewWordArray# (word2Int# n) \mwa s -> + mwaArrayCopy# mwa 0# wa 0# (word2Int# n) s + where + !(# q, r #) = quotRemWord# (int2Word# (sizeofByteArray# wa)) + WORD_SIZE_IN_BYTES## + -- find real size in Words by removing trailing null limbs + !n = real_size n0 + real_size 0## = 0## + real_size i + | 0## <- bigNatIndex# wa (word2Int# (i `minusWord#` 1##)) + = real_size (i `minusWord#` 1##) + real_size i = i + + +-- | Create a BigNat from a WordArray# containing /n/ limbs in +-- least-significant-first order. +-- +-- If possible 'WordArray#', will be used directly (i.e. shared +-- /without/ cloning the 'WordArray#' into a newly allocated one) +bigNatFromWordArray :: WordArray# -> Word# -> BigNat +bigNatFromWordArray wa n = BN# (bigNatFromWordArray# wa n) |