[Git][ghc/ghc][master] Bignum: add bigNatFromWordArray
Marge Bot
gitlab at gitlab.haskell.org
Sat Sep 26 09:37:30 UTC 2020
Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC
Commits:
1cde295c by Sylvain Henry at 2020-09-26T05:37:23-04:00
Bignum: add bigNatFromWordArray
Reimplementation of integer-gmp's byteArrayToBigNat#
- - - - -
2 changed files:
- libraries/ghc-bignum/src/GHC/Num/BigNat.hs
- libraries/integer-gmp/src/GHC/Integer/GMP/Internals.hs
Changes:
=====================================
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)
=====================================
libraries/integer-gmp/src/GHC/Integer/GMP/Internals.hs
=====================================
@@ -53,6 +53,7 @@ module GHC.Integer.GMP.Internals
-- ** Conversions to/from 'BigNat'
+ , byteArrayToBigNat#
, wordToBigNat
, wordToBigNat2
, bigNatToInt
@@ -432,3 +433,8 @@ importIntegerFromByteArray ba off sz endian = case runRW# (I.integerFromByteArra
exportIntegerToMutableByteArray :: Integer -> MutableByteArray# RealWorld -> Word# -> Int# -> IO Word
exportIntegerToMutableByteArray i mba off endian = IO (\s -> case I.integerToMutableByteArray# i mba off endian s of
(# s', r #) -> (# s', W# r #))
+
+
+{-# DEPRECATED byteArrayToBigNat# "Use bigNatFromWordArray instead" #-}
+byteArrayToBigNat# :: ByteArray# -> GmpSize# -> BigNat
+byteArrayToBigNat# ba n = B.bigNatFromWordArray ba (int2Word# n)
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/1cde295c543e209c3b81256b50e77f3c5132a4ad
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/1cde295c543e209c3b81256b50e77f3c5132a4ad
You're receiving this email because of your account on gitlab.haskell.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20200926/9dbdcd6c/attachment-0001.html>
More information about the ghc-commits
mailing list