Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC
Commits:
-
71808e29
by Sylvain Henry at 2026-10-22T04:50:24-04:00
-
33247780
by Sylvain Henry at 2026-10-22T04:50:24-04:00
21 changed files:
- + changelog.d/unboxed-do
- docs/users_guide/exts/qualified_do.rst
- libraries/ghc-internal/CHANGELOG.md
- libraries/ghc-internal/ghc-internal.cabal.in
- libraries/ghc-internal/src/GHC/Internal/Bignum/Backend/GMP.hs
- libraries/ghc-internal/src/GHC/Internal/Bignum/Backend/Native.hs
- libraries/ghc-internal/src/GHC/Internal/Bignum/BigNat.hs
- libraries/ghc-internal/src/GHC/Internal/Bignum/Integer.hs
- libraries/ghc-internal/src/GHC/Internal/Bignum/Natural.hs
- libraries/ghc-internal/src/GHC/Internal/Bignum/Primitives.hs
- libraries/ghc-internal/src/GHC/Internal/Bignum/WordArray.hs
- + libraries/ghc-internal/src/GHC/Internal/Unboxed/Do.hs
- testsuite/tests/qualifieddo/should_compile/all.T
- + testsuite/tests/qualifieddo/should_compile/qdocompile003.hs
- + testsuite/tests/qualifieddo/should_compile/qdocompile003.stderr
- testsuite/tests/qualifieddo/should_fail/all.T
- + testsuite/tests/qualifieddo/should_fail/qdofail006.hs
- + testsuite/tests/qualifieddo/should_fail/qdofail006.stderr
- testsuite/tests/qualifieddo/should_run/all.T
- + testsuite/tests/qualifieddo/should_run/qdorun008.hs
- + testsuite/tests/qualifieddo/should_run/qdorun008.stdout
Changes:
| 1 | +section: ghc-internal
|
|
| 2 | +issues: #27944
|
|
| 3 | +mrs: !16808
|
|
| 4 | +synopsis:
|
|
| 5 | + Add ``GHC.Internal.Unboxed.Do`` for do-notation over unboxed values
|
|
| 6 | +description:
|
|
| 7 | + The new module ``GHC.Internal.Unboxed.Do`` provides ``QualifiedDo``
|
|
| 8 | + operations for ``State#``-passing code. Bound variables can have any
|
|
| 9 | + representation (``Word#``, ``MutableByteArray# s``, ``(# Word#, Word# #)``,
|
|
| 10 | + ...) and unit actions of type ``State# s -> State# s`` can be used as
|
|
| 11 | + statements. The ``GHC.Internal.Bignum`` modules now use it. |
| ... | ... | @@ -118,6 +118,17 @@ If no qualifier is specified with ``-XQualifiedDo`` enabled, it defaults to the |
| 118 | 118 | Note that the operations to be qualified must be in scope for QualifiedDo to work. I.e. ``import MAC (label)`` in the
|
| 119 | 119 | example above would result in an error, since ``MAC.>>=`` and ``MAC.>>`` would not be in scope.
|
| 120 | 120 | |
| 121 | +.. _qualified-do-rep-poly:
|
|
| 122 | + |
|
| 123 | +Representation-polymorphic operations
|
|
| 124 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| 125 | + |
|
| 126 | +The operations can be representation-polymorphic, so that the variables bound
|
|
| 127 | +by ``pat <- u`` can have unboxed types such as ``Word#``. The representation of
|
|
| 128 | +each bound variable must be fixed by the type of ``u``
|
|
| 129 | +(see :ref:`representation-polymorphism-restrictions`). This does not work with
|
|
| 130 | +:extension:`ApplicativeDo`.
|
|
| 131 | + |
|
| 121 | 132 | Examples
|
| 122 | 133 | ~~~~~~~~
|
| 123 | 134 |
| ... | ... | @@ -4,6 +4,7 @@ |
| 4 | 4 | |
| 5 | 5 | * Introduce `dataToCodeQ` and `liftDataTyped`, typed variants of `dataToExpQ` and `liftData` respectively.
|
| 6 | 6 | * Add new `gc_sync_elapsed_ns` counter to GHC.Internal.Stats
|
| 7 | +* Add `GHC.Internal.Unboxed.Do`: `QualifiedDo` operations for `State#`-passing code that binds unboxed values.
|
|
| 7 | 8 | |
| 8 | 9 | ## 9.1001.0 -- 2024-05-01
|
| 9 | 10 |
| ... | ... | @@ -94,6 +94,7 @@ Library |
| 94 | 94 | OverloadedStrings
|
| 95 | 95 | ParallelListComp
|
| 96 | 96 | PolyKinds
|
| 97 | + QualifiedDo
|
|
| 97 | 98 | RankNTypes
|
| 98 | 99 | RecordWildCards
|
| 99 | 100 | RoleAnnotations
|
| ... | ... | @@ -328,6 +329,7 @@ Library |
| 328 | 329 | GHC.Internal.Text.Show
|
| 329 | 330 | GHC.Internal.Type.Reflection
|
| 330 | 331 | GHC.Internal.Type.Reflection.Unsafe
|
| 332 | + GHC.Internal.Unboxed.Do
|
|
| 331 | 333 | GHC.Internal.Unsafe.Coerce
|
| 332 | 334 | |
| 333 | 335 | GHC.Internal.CString
|
| ... | ... | @@ -9,6 +9,7 @@ |
| 9 | 9 | {-# LANGUAGE NegativeLiterals #-}
|
| 10 | 10 | {-# LANGUAGE BlockArguments #-}
|
| 11 | 11 | {-# LANGUAGE LambdaCase #-}
|
| 12 | +{-# LANGUAGE QualifiedDo #-}
|
|
| 12 | 13 | |
| 13 | 14 | {-# OPTIONS_GHC -Wno-name-shadowing #-}
|
| 14 | 15 | |
| ... | ... | @@ -26,6 +27,7 @@ import GHC.Internal.Bignum.Primitives |
| 26 | 27 | import GHC.Internal.Prim
|
| 27 | 28 | import GHC.Internal.Types
|
| 28 | 29 | import GHC.Internal.Magic (runRW#)
|
| 30 | +import qualified GHC.Internal.Unboxed.Do as Ubx
|
|
| 29 | 31 | import {-# SOURCE #-} GHC.Internal.Bignum.Integer
|
| 30 | 32 | import {-# SOURCE #-} GHC.Internal.Bignum.BigNat
|
| 31 | 33 | import {-# SOURCE #-} GHC.Internal.Bignum.Natural
|
| ... | ... | @@ -75,15 +77,17 @@ bignat_add |
| 75 | 77 | -> State# RealWorld
|
| 76 | 78 | -> State# RealWorld
|
| 77 | 79 | {-# INLINE bignat_add #-}
|
| 78 | -bignat_add mwa wa wb s
|
|
| 80 | +bignat_add mwa wa wb
|
|
| 79 | 81 | -- weird GMP requirement: the biggest comes first
|
| 80 | 82 | | isTrue# (wordArraySize# wb ># wordArraySize# wa)
|
| 81 | - = case ioWord# (c_mpn_add mwa wb (wordArraySize# wb) wa (wordArraySize# wa)) s of
|
|
| 82 | - (# s', c #) -> mwaWriteMostSignificant mwa c s'
|
|
| 83 | + = Ubx.do
|
|
| 84 | + c <- ioWord# (c_mpn_add mwa wb (wordArraySize# wb) wa (wordArraySize# wa))
|
|
| 85 | + mwaWriteMostSignificant mwa c
|
|
| 83 | 86 | |
| 84 | 87 | | True
|
| 85 | - = case ioWord# (c_mpn_add mwa wa (wordArraySize# wa) wb (wordArraySize# wb)) s of
|
|
| 86 | - (# s', c #) -> mwaWriteMostSignificant mwa c s'
|
|
| 88 | + = Ubx.do
|
|
| 89 | + c <- ioWord# (c_mpn_add mwa wa (wordArraySize# wa) wb (wordArraySize# wb))
|
|
| 90 | + mwaWriteMostSignificant mwa c
|
|
| 87 | 91 | |
| 88 | 92 | bignat_add_word
|
| 89 | 93 | :: MutableWordArray# RealWorld
|
| ... | ... | @@ -92,9 +96,9 @@ bignat_add_word |
| 92 | 96 | -> State# RealWorld
|
| 93 | 97 | -> State# RealWorld
|
| 94 | 98 | {-# INLINE bignat_add_word #-}
|
| 95 | -bignat_add_word mwa wa b s = do
|
|
| 96 | - case ioWord# (c_mpn_add_1 mwa wa (wordArraySize# wa) b) s of
|
|
| 97 | - (# s', c #) -> mwaWriteMostSignificant mwa c s'
|
|
| 99 | +bignat_add_word mwa wa b = Ubx.do
|
|
| 100 | + c <- ioWord# (c_mpn_add_1 mwa wa (wordArraySize# wa) b)
|
|
| 101 | + mwaWriteMostSignificant mwa c
|
|
| 98 | 102 | |
| 99 | 103 | bignat_sub
|
| 100 | 104 | :: MutableWordArray# RealWorld
|
| ... | ... | @@ -103,10 +107,11 @@ bignat_sub |
| 103 | 107 | -> State# RealWorld
|
| 104 | 108 | -> (# State# RealWorld, Bool# #)
|
| 105 | 109 | {-# INLINE bignat_sub #-}
|
| 106 | -bignat_sub mwa wa wb s =
|
|
| 107 | - case ioWord# (c_mpn_sub mwa wa (wordArraySize# wa) wb (wordArraySize# wb)) s of
|
|
| 108 | - (# s', 1## #) -> (# s', 0# #) -- underflow
|
|
| 109 | - (# s', _ #) -> (# s', 1# #) -- no underflow
|
|
| 110 | +bignat_sub mwa wa wb = Ubx.do
|
|
| 111 | + c <- ioWord# (c_mpn_sub mwa wa (wordArraySize# wa) wb (wordArraySize# wb))
|
|
| 112 | + case c of
|
|
| 113 | + 1## -> Ubx.pure 0# -- underflow
|
|
| 114 | + _ -> Ubx.pure 1# -- no underflow
|
|
| 110 | 115 | |
| 111 | 116 | bignat_sub_word
|
| 112 | 117 | :: MutableWordArray# RealWorld
|
| ... | ... | @@ -115,10 +120,11 @@ bignat_sub_word |
| 115 | 120 | -> State# RealWorld
|
| 116 | 121 | -> (# State# RealWorld, Bool# #)
|
| 117 | 122 | {-# INLINE bignat_sub_word #-}
|
| 118 | -bignat_sub_word mwa wa b s =
|
|
| 119 | - case ioWord# (c_mpn_sub_1 mwa wa (wordArraySize# wa) b) s of
|
|
| 120 | - (# s', 1## #) -> (# s', 0# #) -- underflow
|
|
| 121 | - (# s', _ #) -> (# s', 1# #) -- no underflow
|
|
| 123 | +bignat_sub_word mwa wa b = Ubx.do
|
|
| 124 | + c <- ioWord# (c_mpn_sub_1 mwa wa (wordArraySize# wa) b)
|
|
| 125 | + case c of
|
|
| 126 | + 1## -> Ubx.pure 0# -- underflow
|
|
| 127 | + _ -> Ubx.pure 1# -- no underflow
|
|
| 122 | 128 | |
| 123 | 129 | bignat_mul
|
| 124 | 130 | :: MutableWordArray# RealWorld
|
| ... | ... | @@ -127,11 +133,12 @@ bignat_mul |
| 127 | 133 | -> State# RealWorld
|
| 128 | 134 | -> State# RealWorld
|
| 129 | 135 | {-# INLINE bignat_mul #-}
|
| 130 | -bignat_mul mwa wa wb s = do
|
|
| 131 | - case ioWord# (c_mpn_mul mwa wa (wordArraySize# wa) wb (wordArraySize# wb)) s of
|
|
| 132 | - (# s', _msl #) -> s' -- we don't care about the most-significant
|
|
| 133 | - -- limb. The caller shrink the mwa if
|
|
| 134 | - -- necessary anyway.
|
|
| 136 | +bignat_mul mwa wa wb = Ubx.do
|
|
| 137 | + _msl <- ioWord# (c_mpn_mul mwa wa (wordArraySize# wa) wb (wordArraySize# wb))
|
|
| 138 | + -- _msl is the most-significant limb of the result. It tells if the result
|
|
| 139 | + -- has a leading zero limb. We ignore it: the caller removes leading zero
|
|
| 140 | + -- limbs anyway (see withNewWordArrayTrimmed#).
|
|
| 141 | + Ubx.nop
|
|
| 135 | 142 | |
| 136 | 143 | bignat_mul_word
|
| 137 | 144 | :: MutableWordArray# RealWorld -- ^ Result
|
| ... | ... | @@ -140,9 +147,9 @@ bignat_mul_word |
| 140 | 147 | -> State# RealWorld
|
| 141 | 148 | -> State# RealWorld
|
| 142 | 149 | {-# INLINE bignat_mul_word #-}
|
| 143 | -bignat_mul_word mwa wa b s =
|
|
| 144 | - case ioWord# (c_mpn_mul_1 mwa wa (wordArraySize# wa) b) s of
|
|
| 145 | - (# s', c #) -> mwaWriteMostSignificant mwa c s'
|
|
| 150 | +bignat_mul_word mwa wa b = Ubx.do
|
|
| 151 | + c <- ioWord# (c_mpn_mul_1 mwa wa (wordArraySize# wa) b)
|
|
| 152 | + mwaWriteMostSignificant mwa c
|
|
| 146 | 153 | |
| 147 | 154 | bignat_popcount :: WordArray# -> Word#
|
| 148 | 155 | {-# INLINE bignat_popcount #-}
|
| ... | ... | @@ -156,11 +163,12 @@ bignat_shiftl |
| 156 | 163 | -> State# RealWorld
|
| 157 | 164 | -> State# RealWorld
|
| 158 | 165 | {-# INLINE bignat_shiftl #-}
|
| 159 | -bignat_shiftl mwa wa n s =
|
|
| 160 | - case ioWord# (c_mpn_lshift mwa wa (wordArraySize# wa) n) s of
|
|
| 161 | - (# s', _msl #) -> s' -- we don't care about the most-significant
|
|
| 162 | - -- limb. The caller shrink the mwa if
|
|
| 163 | - -- necessary anyway.
|
|
| 166 | +bignat_shiftl mwa wa n = Ubx.do
|
|
| 167 | + _msl <- ioWord# (c_mpn_lshift mwa wa (wordArraySize# wa) n)
|
|
| 168 | + -- _msl is the most-significant limb of the result. It tells if the result
|
|
| 169 | + -- has a leading zero limb. We ignore it: the caller removes leading zero
|
|
| 170 | + -- limbs anyway (see withNewWordArrayTrimmed#).
|
|
| 171 | + Ubx.nop
|
|
| 164 | 172 | |
| 165 | 173 | bignat_shiftr
|
| 166 | 174 | :: MutableWordArray# RealWorld
|
| ... | ... | @@ -169,11 +177,12 @@ bignat_shiftr |
| 169 | 177 | -> State# RealWorld
|
| 170 | 178 | -> State# RealWorld
|
| 171 | 179 | {-# INLINE bignat_shiftr #-}
|
| 172 | -bignat_shiftr mwa wa n s =
|
|
| 173 | - case ioWord# (c_mpn_rshift mwa wa (wordArraySize# wa) n) s of
|
|
| 174 | - (# s', _msl #) -> s' -- we don't care about the most-significant
|
|
| 175 | - -- limb. The caller shrink the mwa if
|
|
| 176 | - -- necessary anyway.
|
|
| 180 | +bignat_shiftr mwa wa n = Ubx.do
|
|
| 181 | + _msl <- ioWord# (c_mpn_rshift mwa wa (wordArraySize# wa) n)
|
|
| 182 | + -- _msl is the most-significant limb of the result. It tells if the result
|
|
| 183 | + -- has a leading zero limb. We ignore it: the caller removes leading zero
|
|
| 184 | + -- limbs anyway (see withNewWordArrayTrimmed#).
|
|
| 185 | + Ubx.nop
|
|
| 177 | 186 | |
| 178 | 187 | bignat_or
|
| 179 | 188 | :: MutableWordArray# RealWorld
|
| ... | ... | @@ -182,15 +191,16 @@ bignat_or |
| 182 | 191 | -> State# RealWorld
|
| 183 | 192 | -> State# RealWorld
|
| 184 | 193 | {-# INLINE bignat_or #-}
|
| 185 | -bignat_or mwa wa wb s1
|
|
| 186 | - | isTrue# (szA >=# szB) = go wa szA wb szB s1
|
|
| 187 | - | True = go wb szB wa szA s1
|
|
| 194 | +bignat_or mwa wa wb
|
|
| 195 | + | isTrue# (szA >=# szB) = go wa szA wb szB
|
|
| 196 | + | True = go wb szB wa szA
|
|
| 188 | 197 | where
|
| 189 | 198 | !szA = wordArraySize# wa
|
| 190 | 199 | !szB = wordArraySize# wb
|
| 191 | 200 | -- nx >= ny
|
| 192 | - go wx nx wy ny s = case ioVoid (c_mpn_ior_n mwa wx wy ny) s of
|
|
| 193 | - s' -> mwaArrayCopy# mwa ny wx ny (nx -# ny) s'
|
|
| 201 | + go wx nx wy ny = Ubx.do
|
|
| 202 | + ioVoid (c_mpn_ior_n mwa wx wy ny)
|
|
| 203 | + mwaArrayCopy# mwa ny wx ny (nx -# ny)
|
|
| 194 | 204 | |
| 195 | 205 | bignat_xor
|
| 196 | 206 | :: MutableWordArray# RealWorld
|
| ... | ... | @@ -199,15 +209,16 @@ bignat_xor |
| 199 | 209 | -> State# RealWorld
|
| 200 | 210 | -> State# RealWorld
|
| 201 | 211 | {-# INLINE bignat_xor #-}
|
| 202 | -bignat_xor mwa wa wb s1
|
|
| 203 | - | isTrue# (szA >=# szB) = go wa szA wb szB s1
|
|
| 204 | - | True = go wb szB wa szA s1
|
|
| 212 | +bignat_xor mwa wa wb
|
|
| 213 | + | isTrue# (szA >=# szB) = go wa szA wb szB
|
|
| 214 | + | True = go wb szB wa szA
|
|
| 205 | 215 | where
|
| 206 | 216 | !szA = wordArraySize# wa
|
| 207 | 217 | !szB = wordArraySize# wb
|
| 208 | 218 | -- nx >= ny
|
| 209 | - go wx nx wy ny s = case ioVoid (c_mpn_xor_n mwa wx wy ny) s of
|
|
| 210 | - s' -> mwaArrayCopy# mwa ny wx ny (nx -# ny) s'
|
|
| 219 | + go wx nx wy ny = Ubx.do
|
|
| 220 | + ioVoid (c_mpn_xor_n mwa wx wy ny)
|
|
| 221 | + mwaArrayCopy# mwa ny wx ny (nx -# ny)
|
|
| 211 | 222 | |
| 212 | 223 | bignat_and
|
| 213 | 224 | :: MutableWordArray# RealWorld -- ^ Result
|
| ... | ... | @@ -229,9 +240,9 @@ bignat_and_not |
| 229 | 240 | -> State# RealWorld
|
| 230 | 241 | -> State# RealWorld
|
| 231 | 242 | {-# INLINE bignat_and_not #-}
|
| 232 | -bignat_and_not mwa wa wb s =
|
|
| 233 | - case ioVoid (c_mpn_andn_n mwa wa wb n) s of
|
|
| 234 | - s' -> mwaArrayCopy# mwa szB wa szB (szA -# szB) s'
|
|
| 243 | +bignat_and_not mwa wa wb = Ubx.do
|
|
| 244 | + ioVoid (c_mpn_andn_n mwa wa wb n)
|
|
| 245 | + mwaArrayCopy# mwa szB wa szB (szA -# szB)
|
|
| 235 | 246 | where
|
| 236 | 247 | !szA = wordArraySize# wa
|
| 237 | 248 | !szB = wordArraySize# wb
|
| ... | ... | @@ -291,9 +302,7 @@ bignat_quot_word |
| 291 | 302 | -> Word#
|
| 292 | 303 | -> State# RealWorld
|
| 293 | 304 | -> State# RealWorld
|
| 294 | -bignat_quot_word mwq wa b s =
|
|
| 295 | - case bignat_quotrem_word mwq wa b s of
|
|
| 296 | - (# s', _ #) -> s'
|
|
| 305 | +bignat_quot_word mwq wa b = Ubx.void (bignat_quotrem_word mwq wa b)
|
|
| 297 | 306 | |
| 298 | 307 | bignat_rem_word
|
| 299 | 308 | :: WordArray#
|
| ... | ... | @@ -309,10 +318,10 @@ bignat_gcd |
| 309 | 318 | -> WordArray#
|
| 310 | 319 | -> State# RealWorld
|
| 311 | 320 | -> State# RealWorld
|
| 312 | -bignat_gcd mwr wa wb s =
|
|
| 321 | +bignat_gcd mwr wa wb = Ubx.do
|
|
| 313 | 322 | -- wa > wb
|
| 314 | - case ioInt# (c_mpn_gcd# mwr wa (wordArraySize# wa) wb (wordArraySize# wb)) s of
|
|
| 315 | - (# s', sz #) -> mwaSetSize# mwr (narrowGmpSize# sz) s'
|
|
| 323 | + sz <- ioInt# (c_mpn_gcd# mwr wa (wordArraySize# wa) wb (wordArraySize# wb))
|
|
| 324 | + mwaSetSize# mwr (narrowGmpSize# sz)
|
|
| 316 | 325 | |
| 317 | 326 | bignat_gcd_word
|
| 318 | 327 | :: WordArray#
|
| ... | ... | @@ -379,7 +388,7 @@ integer_powmod b e m = naturalFromBigNat# (withNewWordArray# szm io) |
| 379 | 388 | 0# -> szb
|
| 380 | 389 | _ -> negateInt# szb
|
| 381 | 390 | |
| 382 | - io r s = sbignat_powmod r ssb bb be bm s
|
|
| 391 | + io r = sbignat_powmod r ssb bb be bm
|
|
| 383 | 392 | |
| 384 | 393 | |
| 385 | 394 | sbignat_powmod
|
| ... | ... | @@ -390,9 +399,9 @@ sbignat_powmod |
| 390 | 399 | -> WordArray#
|
| 391 | 400 | -> State# RealWorld
|
| 392 | 401 | -> State# RealWorld
|
| 393 | -sbignat_powmod r b_signed_size b e m s =
|
|
| 394 | - case ioInt# (integer_gmp_powm# r b b_signed_size e (wordArraySize# e) m (wordArraySize# m)) s of
|
|
| 395 | - (# s', n #) -> mwaSetSize# r (narrowGmpSize# n) s'
|
|
| 402 | +sbignat_powmod r b_signed_size b e m = Ubx.do
|
|
| 403 | + n <- ioInt# (integer_gmp_powm# r b b_signed_size e (wordArraySize# e) m (wordArraySize# m))
|
|
| 404 | + mwaSetSize# r (narrowGmpSize# n)
|
|
| 396 | 405 | |
| 397 | 406 | integer_gcde
|
| 398 | 407 | :: Integer
|
| ... | ... | @@ -422,40 +431,39 @@ integer_gcde a b = case runRW# io of (# _, a #) -> a |
| 422 | 431 | !x_init_sz = szb
|
| 423 | 432 | !y_init_sz = sza
|
| 424 | 433 | |
| 425 | - io s =
|
|
| 434 | + io = Ubx.do
|
|
| 426 | 435 | -- allocate output arrays
|
| 427 | - case newWordArray# g_init_sz s of { (# s, mbg #) ->
|
|
| 428 | - case newWordArray# x_init_sz s of { (# s, mbx #) ->
|
|
| 429 | - case newWordArray# y_init_sz s of { (# s, mby #) ->
|
|
| 436 | + mbg <- newWordArray# g_init_sz
|
|
| 437 | + mbx <- newWordArray# x_init_sz
|
|
| 438 | + mby <- newWordArray# y_init_sz
|
|
| 430 | 439 | -- allocate space to return sizes (3x4 = 12)
|
| 431 | - case newPinnedByteArray# 12# s of { (# s, mszs #) ->
|
|
| 432 | - case unsafeFreezeByteArray# mszs s of { (# s, szs #) ->
|
|
| 433 | - let !ssx_ptr = byteArrayContents# szs in
|
|
| 434 | - let !ssy_ptr = ssx_ptr `plusAddr#` 4# in
|
|
| 435 | - let !sg_ptr = ssy_ptr `plusAddr#` 4# in
|
|
| 440 | + mszs <- newPinnedByteArray# 12#
|
|
| 441 | + szs <- unsafeFreezeByteArray# mszs
|
|
| 442 | + let !ssx_ptr = byteArrayContents# szs
|
|
| 443 | + !ssy_ptr = ssx_ptr `plusAddr#` 4#
|
|
| 444 | + !sg_ptr = ssy_ptr `plusAddr#` 4#
|
|
| 436 | 445 | -- call GMP
|
| 437 | - case ioVoid (integer_gmp_gcdext# mbx ssx_ptr mby ssy_ptr mbg sg_ptr ba ssa bb ssb) s of { s ->
|
|
| 446 | + ioVoid (integer_gmp_gcdext# mbx ssx_ptr mby ssy_ptr mbg sg_ptr ba ssa bb ssb)
|
|
| 438 | 447 | -- read sizes
|
| 439 | - case readInt32OffAddr# ssx_ptr 0# s of { (# s, ssx #) ->
|
|
| 440 | - case readInt32OffAddr# ssy_ptr 0# s of { (# s, ssy #) ->
|
|
| 441 | - case readInt32OffAddr# sg_ptr 0# s of { (# s, sg #) ->
|
|
| 442 | - case touch# szs s of { s ->
|
|
| 448 | + ssx <- readInt32OffAddr# ssx_ptr 0#
|
|
| 449 | + ssy <- readInt32OffAddr# ssy_ptr 0#
|
|
| 450 | + sg <- readInt32OffAddr# sg_ptr 0#
|
|
| 451 | + touch# szs
|
|
| 443 | 452 | -- shrink x, y and g to their actual sizes and freeze them
|
| 444 | - let !sx = absI# (int32ToInt# ssx) in
|
|
| 445 | - let !sy = absI# (int32ToInt# ssy) in
|
|
| 446 | - case mwaSetSize# mbx sx s of { s ->
|
|
| 447 | - case mwaSetSize# mby sy s of { s ->
|
|
| 448 | - case mwaSetSize# mbg (int32ToInt# sg) s of { s ->
|
|
| 453 | + let !sx = absI# (int32ToInt# ssx)
|
|
| 454 | + !sy = absI# (int32ToInt# ssy)
|
|
| 455 | + mwaSetSize# mbx sx
|
|
| 456 | + mwaSetSize# mby sy
|
|
| 457 | + mwaSetSize# mbg (int32ToInt# sg)
|
|
| 449 | 458 | |
| 450 | 459 | -- return x, y and g as Integer
|
| 451 | - case unsafeFreezeByteArray# mbx s of { (# s, bx #) ->
|
|
| 452 | - case unsafeFreezeByteArray# mby s of { (# s, by #) ->
|
|
| 453 | - case unsafeFreezeByteArray# mbg s of { (# s, bg #) ->
|
|
| 460 | + bx <- unsafeFreezeByteArray# mbx
|
|
| 461 | + by <- unsafeFreezeByteArray# mby
|
|
| 462 | + bg <- unsafeFreezeByteArray# mbg
|
|
| 454 | 463 | |
| 455 | - (# s, (# integerFromBigNat# bg
|
|
| 456 | - , integerFromBigNatSign# (int32ToInt# ssx <# 0#) bx
|
|
| 457 | - , integerFromBigNatSign# (int32ToInt# ssy <# 0#) by #) #)
|
|
| 458 | - }}}}}}}}}}}}}}}}
|
|
| 464 | + Ubx.pure (# integerFromBigNat# bg
|
|
| 465 | + , integerFromBigNatSign# (int32ToInt# ssx <# 0#) bx
|
|
| 466 | + , integerFromBigNatSign# (int32ToInt# ssy <# 0#) by #)
|
|
| 459 | 467 | |
| 460 | 468 | |
| 461 | 469 | |
| ... | ... | @@ -477,8 +485,9 @@ integer_recip_mod x m = |
| 477 | 485 | sbignat_recip_mod :: Int# -> BigNat# -> BigNat# -> BigNat#
|
| 478 | 486 | sbignat_recip_mod sign_x x m = withNewWordArray# szm io
|
| 479 | 487 | where
|
| 480 | - io r s = case ioInt# (integer_gmp_invert# r x ssx m szm) s of
|
|
| 481 | - (# s, rn #) -> mwaSetSize# r (narrowGmpSize# rn) s
|
|
| 488 | + io r = Ubx.do
|
|
| 489 | + rn <- ioInt# (integer_gmp_invert# r x ssx m szm)
|
|
| 490 | + mwaSetSize# r (narrowGmpSize# rn)
|
|
| 482 | 491 | !szx = bigNatSize# x
|
| 483 | 492 | !szm = bigNatSize# m
|
| 484 | 493 | !ssx = case sign_x of -- signed size of x
|
| ... | ... | @@ -7,6 +7,7 @@ |
| 7 | 7 | {-# LANGUAGE NegativeLiterals #-}
|
| 8 | 8 | {-# LANGUAGE MultiWayIf #-}
|
| 9 | 9 | {-# LANGUAGE BinaryLiterals #-}
|
| 10 | +{-# LANGUAGE QualifiedDo #-}
|
|
| 10 | 11 | {-# OPTIONS_GHC -Wno-name-shadowing #-}
|
| 11 | 12 | |
| 12 | 13 | module GHC.Internal.Bignum.Backend.Native where
|
| ... | ... | @@ -27,6 +28,7 @@ import GHC.Internal.Bignum.WordArray |
| 27 | 28 | import GHC.Internal.Bignum.Primitives
|
| 28 | 29 | import GHC.Internal.Prim
|
| 29 | 30 | import GHC.Internal.Types
|
| 31 | +import qualified GHC.Internal.Unboxed.Do as Ubx
|
|
| 30 | 32 | |
| 31 | 33 | -- See W1 of Note [Tracking dependencies on primitives] in GHC.Internal.Base
|
| 32 | 34 | -- (This module uses the empty tuple () and string literals.)
|
| ... | ... | @@ -96,39 +98,41 @@ bignat_add mwa wa wb = addABc 0# 0## |
| 96 | 98 | -- 4) We only have a potential carry
|
| 97 | 99 | -- => write the carry or shrink the array
|
| 98 | 100 | |
| 99 | - addABc i carry s
|
|
| 100 | - | isTrue# (i <# szMin) =
|
|
| 101 | + addABc i carry
|
|
| 102 | + | isTrue# (i <# szMin) = Ubx.do
|
|
| 101 | 103 | let
|
| 102 | 104 | !(# carry', r #) = plusWord3#
|
| 103 | 105 | (indexWordArray# wa i)
|
| 104 | 106 | (indexWordArray# wb i)
|
| 105 | 107 | carry
|
| 106 | - in case mwaWrite# mwa i r s of
|
|
| 107 | - s' -> addABc (i +# 1#) carry' s'
|
|
| 108 | + mwaWrite# mwa i r
|
|
| 109 | + addABc (i +# 1#) carry'
|
|
| 108 | 110 | |
| 109 | 111 | | isTrue# ((i ==# szA) &&# (i ==# szB))
|
| 110 | - = mwaWriteOrShrink mwa carry i s
|
|
| 112 | + = mwaWriteOrShrink mwa carry i
|
|
| 111 | 113 | |
| 112 | 114 | | isTrue# (i ==# szA)
|
| 113 | - = addAoBc wb i carry s
|
|
| 115 | + = addAoBc wb i carry
|
|
| 114 | 116 | |
| 115 | 117 | | True
|
| 116 | - = addAoBc wa i carry s
|
|
| 118 | + = addAoBc wa i carry
|
|
| 117 | 119 | |
| 118 | - addAoBc wab i carry s
|
|
| 120 | + addAoBc wab i carry
|
|
| 119 | 121 | | isTrue# (i ==# wordArraySize# wab)
|
| 120 | - = mwaWriteOrShrink mwa carry i s
|
|
| 122 | + = mwaWriteOrShrink mwa carry i
|
|
| 121 | 123 | |
| 122 | 124 | | 0## <- carry
|
| 123 | - = -- copy the remaining words and remove the word allocated for the
|
|
| 124 | - -- potential carry
|
|
| 125 | - case mwaArrayCopy# mwa i wab i (wordArraySize# wab -# i) s of
|
|
| 126 | - s' -> mwaShrink# mwa 1# s'
|
|
| 125 | + = Ubx.do
|
|
| 126 | + -- copy the remaining words and remove the word allocated for the
|
|
| 127 | + -- potential carry
|
|
| 128 | + mwaArrayCopy# mwa i wab i (wordArraySize# wab -# i)
|
|
| 129 | + mwaShrink# mwa 1#
|
|
| 127 | 130 | |
| 128 | 131 | | True
|
| 129 | - = let !(# carry', r #) = plusWord2# (indexWordArray# wab i) carry
|
|
| 130 | - in case mwaWrite# mwa i r s of
|
|
| 131 | - s' -> addAoBc wab (i +# 1#) carry' s'
|
|
| 132 | + = Ubx.do
|
|
| 133 | + let !(# carry', r #) = plusWord2# (indexWordArray# wab i) carry
|
|
| 134 | + mwaWrite# mwa i r
|
|
| 135 | + addAoBc wab (i +# 1#) carry'
|
|
| 132 | 136 | |
| 133 | 137 | -- | Add a non-zero BigNat and a non-zero Word#
|
| 134 | 138 | --
|
| ... | ... | @@ -163,22 +167,25 @@ bignat_sub_word |
| 163 | 167 | bignat_sub_word mwa wa b = go b 0#
|
| 164 | 168 | where
|
| 165 | 169 | !sz = wordArraySize# wa
|
| 166 | - go carry i s
|
|
| 170 | + go carry i
|
|
| 167 | 171 | | isTrue# (i >=# sz)
|
| 168 | - = (# s, carry `eqWord#` 0## #)
|
|
| 172 | + = Ubx.pure (carry `eqWord#` 0##)
|
|
| 169 | 173 | |
| 170 | 174 | | 0## <- carry
|
| 171 | - = case mwaArrayCopy# mwa i wa i (sz -# i) s of
|
|
| 172 | - s' -> (# s', 1# #) -- no underflow
|
|
| 175 | + = Ubx.do
|
|
| 176 | + mwaArrayCopy# mwa i wa i (sz -# i)
|
|
| 177 | + Ubx.pure 1# -- no underflow
|
|
| 173 | 178 | |
| 174 | 179 | | True
|
| 175 | 180 | = case subWordC# (indexWordArray# wa i) carry of
|
| 176 | 181 | (# 0##, 0# #)
|
| 177 | - | isTrue# (i ==# sz) -> case mwaShrink# mwa 1# s of
|
|
| 178 | - s' -> (# s', 1# #) -- no underflow
|
|
| 182 | + | isTrue# (i ==# sz) -> Ubx.do
|
|
| 183 | + mwaShrink# mwa 1#
|
|
| 184 | + Ubx.pure 1# -- no underflow
|
|
| 179 | 185 | |
| 180 | - (# l , c #) -> case mwaWrite# mwa i l s of
|
|
| 181 | - s1 -> go (int2Word# c) (i +# 1#) s1
|
|
| 186 | + (# l , c #) -> Ubx.do
|
|
| 187 | + mwaWrite# mwa i l
|
|
| 188 | + go (int2Word# c) (i +# 1#)
|
|
| 182 | 189 | |
| 183 | 190 | -- | Multiply a non-zero BigNat and a non-zero Word#
|
| 184 | 191 | --
|
| ... | ... | @@ -196,14 +203,14 @@ bignat_mul_word |
| 196 | 203 | bignat_mul_word mwa wa b = go 0# 0##
|
| 197 | 204 | where
|
| 198 | 205 | !szA = wordArraySize# wa
|
| 199 | - go i carry s
|
|
| 200 | - | isTrue# (i ==# szA) = mwaWriteOrShrink mwa carry i s
|
|
| 201 | - | True =
|
|
| 206 | + go i carry
|
|
| 207 | + | isTrue# (i ==# szA) = mwaWriteOrShrink mwa carry i
|
|
| 208 | + | True = Ubx.do
|
|
| 202 | 209 | let
|
| 203 | 210 | ai = indexWordArray# wa i
|
| 204 | 211 | !(# carry', r #) = plusWord12# carry (timesWord2# ai b)
|
| 205 | - in case mwaWrite# mwa i r s of
|
|
| 206 | - s' -> go (i +# 1#) carry' s'
|
|
| 212 | + mwaWrite# mwa i r
|
|
| 213 | + go (i +# 1#) carry'
|
|
| 207 | 214 | |
| 208 | 215 | |
| 209 | 216 | -- | Multiply two non-zero BigNat
|
| ... | ... | @@ -219,10 +226,10 @@ bignat_mul |
| 219 | 226 | -> WordArray#
|
| 220 | 227 | -> State# RealWorld
|
| 221 | 228 | -> State# RealWorld
|
| 222 | -bignat_mul mwa wa wb s1 =
|
|
| 229 | +bignat_mul mwa wa wb = Ubx.do
|
|
| 223 | 230 | -- initialize the resulting WordArray
|
| 224 | - case mwaFill# mwa 0## 0## (int2Word# sz) s1 of
|
|
| 225 | - s' -> mulEachB ctzB s' -- loop on b Words
|
|
| 231 | + mwaFill# mwa 0## 0## (int2Word# sz)
|
|
| 232 | + mulEachB ctzB -- loop on b Words
|
|
| 226 | 233 | where
|
| 227 | 234 | !szA = wordArraySize# wa
|
| 228 | 235 | !szB = wordArraySize# wb
|
| ... | ... | @@ -232,27 +239,29 @@ bignat_mul mwa wa wb s1 = |
| 232 | 239 | !ctzB = word2Int# (bigNatCtzWord# wb)
|
| 233 | 240 | |
| 234 | 241 | -- multiply a single bj Word# to the whole wa WordArray
|
| 235 | - mul mwa wa bj j i carry s
|
|
| 242 | + mul mwa wa bj j i carry
|
|
| 236 | 243 | | isTrue# (i ==# wordArraySize# wa)
|
| 237 | 244 | -- write the carry
|
| 238 | - = mwaAddInplaceWord# mwa (i +# j) carry s
|
|
| 245 | + = mwaAddInplaceWord# mwa (i +# j) carry
|
|
| 239 | 246 | |
| 240 | - | True = let
|
|
| 241 | - ai = indexWordArray# wa i
|
|
| 242 | - !(# c',r' #) = timesWord2# ai bj
|
|
| 243 | - !(# c'',r #) = plusWord2# r' carry
|
|
| 244 | - carry' = plusWord# c' c''
|
|
| 245 | - in case mwaAddInplaceWord# mwa (i +# j) r s of
|
|
| 246 | - s' -> mul mwa wa bj j (i +# 1#) carry' s'
|
|
| 247 | + | True = Ubx.do
|
|
| 248 | + let
|
|
| 249 | + ai = indexWordArray# wa i
|
|
| 250 | + !(# c',r' #) = timesWord2# ai bj
|
|
| 251 | + !(# c'',r #) = plusWord2# r' carry
|
|
| 252 | + carry' = plusWord# c' c''
|
|
| 253 | + mwaAddInplaceWord# mwa (i +# j) r
|
|
| 254 | + mul mwa wa bj j (i +# 1#) carry'
|
|
| 247 | 255 | |
| 248 | 256 | -- for each bj in wb, call `mul bj wa`
|
| 249 | - mulEachB i s
|
|
| 250 | - | isTrue# (i ==# szB) = s
|
|
| 257 | + mulEachB i
|
|
| 258 | + | isTrue# (i ==# szB) = Ubx.nop
|
|
| 251 | 259 | | True = case indexWordArray# wb i of
|
| 252 | 260 | -- detect bj == 0## and skip the loop
|
| 253 | - 0## -> mulEachB (i +# 1#) s
|
|
| 254 | - bi -> case mul mwa wa bi i ctzA 0## s of
|
|
| 255 | - s' -> mulEachB (i +# 1#) s'
|
|
| 261 | + 0## -> mulEachB (i +# 1#)
|
|
| 262 | + bi -> Ubx.do
|
|
| 263 | + mul mwa wa bi i ctzA 0##
|
|
| 264 | + mulEachB (i +# 1#)
|
|
| 256 | 265 | |
| 257 | 266 | -- | Sub two non-zero BigNat
|
| 258 | 267 | --
|
| ... | ... | @@ -269,12 +278,12 @@ bignat_sub |
| 269 | 278 | -> WordArray#
|
| 270 | 279 | -> State# RealWorld
|
| 271 | 280 | -> (# State# RealWorld, Bool# #)
|
| 272 | -bignat_sub mwa wa wb s =
|
|
| 281 | +bignat_sub mwa wa wb = Ubx.do
|
|
| 273 | 282 | -- initialize the resulting WordArray
|
| 274 | 283 | -- Note: we could avoid the copy by subtracting the first non-zero
|
| 275 | 284 | -- less-significant word of b...
|
| 276 | - case mwaArrayCopy# mwa 0# wa 0# (wordArraySize# wa) s of
|
|
| 277 | - s' -> mwaSubInplaceArray mwa 0# wb s'
|
|
| 285 | + mwaArrayCopy# mwa 0# wa 0# (wordArraySize# wa)
|
|
| 286 | + mwaSubInplaceArray mwa 0# wb
|
|
| 278 | 287 | |
| 279 | 288 | -- | PopCount of a non-zero BigNat
|
| 280 | 289 | bignat_popcount :: WordArray# -> Word#
|
| ... | ... | @@ -298,30 +307,29 @@ bignat_shiftl |
| 298 | 307 | -> Word#
|
| 299 | 308 | -> State# s
|
| 300 | 309 | -> State# s
|
| 301 | -bignat_shiftl mwa wa n s1 =
|
|
| 310 | +bignat_shiftl mwa wa n = Ubx.do
|
|
| 302 | 311 | -- set the lower words to 0
|
| 303 | - case mwaFill# mwa 0## 0## (int2Word# nw) s1 of
|
|
| 304 | - s2 -> if
|
|
| 305 | - | 0# <- nb -> mwaArrayCopy# mwa nw wa 0# szA s2
|
|
| 306 | - | True -> mwaBitShift 0# 0## s2
|
|
| 312 | + mwaFill# mwa 0## 0## (int2Word# nw)
|
|
| 313 | + if | 0# <- nb -> mwaArrayCopy# mwa nw wa 0# szA
|
|
| 314 | + | True -> mwaBitShift 0# 0##
|
|
| 307 | 315 | where
|
| 308 | 316 | !szA = wordArraySize# wa
|
| 309 | 317 | !(# nw, nb #) = count_words_bits_int n
|
| 310 | 318 | !sh = WORD_SIZE_IN_BITS# -# nb
|
| 311 | 319 | |
| 312 | 320 | -- Bit granularity (c is the carry from the previous shift)
|
| 313 | - mwaBitShift i c s
|
|
| 321 | + mwaBitShift i c
|
|
| 314 | 322 | -- write the carry
|
| 315 | 323 | | isTrue# (i ==# szA)
|
| 316 | - = mwaWriteOrShrink mwa c (i +# nw) s
|
|
| 324 | + = mwaWriteOrShrink mwa c (i +# nw)
|
|
| 317 | 325 | |
| 318 | - | True =
|
|
| 326 | + | True = Ubx.do
|
|
| 319 | 327 | let
|
| 320 | 328 | !ai = indexWordArray# wa i
|
| 321 | 329 | !v = c `or#` (ai `uncheckedShiftL#` nb)
|
| 322 | 330 | !c' = ai `uncheckedShiftRL#` sh
|
| 323 | - in case mwaWrite# mwa (i +# nw) v s of
|
|
| 324 | - s' -> mwaBitShift (i +# 1#) c' s'
|
|
| 331 | + mwaWrite# mwa (i +# nw) v
|
|
| 332 | + mwaBitShift (i +# 1#) c'
|
|
| 325 | 333 | |
| 326 | 334 | |
| 327 | 335 | -- | Right-shift a non-zero BigNat by a non-zero amount of bits
|
| ... | ... | @@ -337,9 +345,9 @@ bignat_shiftr |
| 337 | 345 | -> Word#
|
| 338 | 346 | -> State# s
|
| 339 | 347 | -> State# s
|
| 340 | -bignat_shiftr mwa wa n s1
|
|
| 341 | - | isTrue# (nb ==# 0#) = mwaArrayCopy# mwa 0# wa nw sz s1
|
|
| 342 | - | True = mwaBitShift (sz -# 1#) 0## s1
|
|
| 348 | +bignat_shiftr mwa wa n
|
|
| 349 | + | isTrue# (nb ==# 0#) = mwaArrayCopy# mwa 0# wa nw sz
|
|
| 350 | + | True = mwaBitShift (sz -# 1#) 0##
|
|
| 343 | 351 | where
|
| 344 | 352 | !szA = wordArraySize# wa
|
| 345 | 353 | !(# nw, nb #) = count_words_bits_int n
|
| ... | ... | @@ -347,15 +355,15 @@ bignat_shiftr mwa wa n s1 |
| 347 | 355 | !sh = WORD_SIZE_IN_BITS# -# nb
|
| 348 | 356 | |
| 349 | 357 | -- Bit granularity (c is the carry from the previous shift)
|
| 350 | - mwaBitShift i c s
|
|
| 351 | - | isTrue# (i <# 0#) = s
|
|
| 352 | - | True =
|
|
| 358 | + mwaBitShift i c
|
|
| 359 | + | isTrue# (i <# 0#) = Ubx.nop
|
|
| 360 | + | True = Ubx.do
|
|
| 353 | 361 | let
|
| 354 | 362 | !ai = indexWordArray# wa (i +# nw)
|
| 355 | 363 | !v = c `or#` (ai `uncheckedShiftRL#` nb)
|
| 356 | 364 | !c' = ai `uncheckedShiftL#` sh
|
| 357 | - in case mwaWrite# mwa i v s of
|
|
| 358 | - s' -> mwaBitShift (i -# 1#) c' s'
|
|
| 365 | + mwaWrite# mwa i v
|
|
| 366 | + mwaBitShift (i -# 1#) c'
|
|
| 359 | 367 | |
| 360 | 368 | -- | Right-shift a non-zero BigNat by a non-zero amount of bits by first
|
| 361 | 369 | -- converting it into its two's complement representation and then again after
|
| ... | ... | @@ -372,15 +380,15 @@ bignat_shiftr_neg |
| 372 | 380 | -> Word#
|
| 373 | 381 | -> State# s
|
| 374 | 382 | -> State# s
|
| 375 | -bignat_shiftr_neg mwa wa n s1
|
|
| 383 | +bignat_shiftr_neg mwa wa n = Ubx.do
|
|
| 376 | 384 | -- initialize higher limb of mwa
|
| 377 | - = case mwaSize# mwa s1 of
|
|
| 378 | - (# s2, sz_mwa #) -> case mwaWrite# mwa (sz_mwa -# 1#) 0## s2 of
|
|
| 379 | - s3 -> case bignat_shiftr mwa wa n s3 of
|
|
| 380 | - s4 -> if nz_shifted_out
|
|
| 381 | - -- round if non-zero bits were shifted out
|
|
| 382 | - then mwaAddInplaceWord# mwa 0# 1## s4
|
|
| 383 | - else s4
|
|
| 385 | + sz_mwa <- mwaSize# mwa
|
|
| 386 | + mwaWrite# mwa (sz_mwa -# 1#) 0##
|
|
| 387 | + bignat_shiftr mwa wa n
|
|
| 388 | + if nz_shifted_out
|
|
| 389 | + -- round if non-zero bits were shifted out
|
|
| 390 | + then mwaAddInplaceWord# mwa 0# 1##
|
|
| 391 | + else Ubx.nop
|
|
| 384 | 392 | where
|
| 385 | 393 | !(# nw, nb #) = count_words_bits_int n
|
| 386 | 394 | |
| ... | ... | @@ -412,16 +420,16 @@ bignat_or |
| 412 | 420 | -> WordArray#
|
| 413 | 421 | -> State# RealWorld
|
| 414 | 422 | -> State# RealWorld
|
| 415 | -bignat_or mwa wa wb s1
|
|
| 416 | - | isTrue# (szA >=# szB) = go wa szA wb szB s1
|
|
| 417 | - | True = go wb szB wa szA s1
|
|
| 423 | +bignat_or mwa wa wb
|
|
| 424 | + | isTrue# (szA >=# szB) = go wa szA wb szB
|
|
| 425 | + | True = go wb szB wa szA
|
|
| 418 | 426 | where
|
| 419 | 427 | !szA = wordArraySize# wa
|
| 420 | 428 | !szB = wordArraySize# wb
|
| 421 | 429 | -- nx >= ny
|
| 422 | - go wx nx wy ny s =
|
|
| 423 | - case mwaInitArrayBinOp mwa wx wy or# s of
|
|
| 424 | - s' -> mwaArrayCopy# mwa ny wx ny (nx -# ny) s'
|
|
| 430 | + go wx nx wy ny = Ubx.do
|
|
| 431 | + mwaInitArrayBinOp mwa wx wy or#
|
|
| 432 | + mwaArrayCopy# mwa ny wx ny (nx -# ny)
|
|
| 425 | 433 | |
| 426 | 434 | -- | XOR two non-zero BigNat
|
| 427 | 435 | --
|
| ... | ... | @@ -436,16 +444,16 @@ bignat_xor |
| 436 | 444 | -> WordArray#
|
| 437 | 445 | -> State# RealWorld
|
| 438 | 446 | -> State# RealWorld
|
| 439 | -bignat_xor mwa wa wb s1
|
|
| 440 | - | isTrue# (szA >=# szB) = go wa szA wb szB s1
|
|
| 441 | - | True = go wb szB wa szA s1
|
|
| 447 | +bignat_xor mwa wa wb
|
|
| 448 | + | isTrue# (szA >=# szB) = go wa szA wb szB
|
|
| 449 | + | True = go wb szB wa szA
|
|
| 442 | 450 | where
|
| 443 | 451 | !szA = wordArraySize# wa
|
| 444 | 452 | !szB = wordArraySize# wb
|
| 445 | 453 | -- nx >= ny
|
| 446 | - go wx nx wy ny s =
|
|
| 447 | - case mwaInitArrayBinOp mwa wx wy xor# s of
|
|
| 448 | - s' -> mwaArrayCopy# mwa ny wx ny (nx -# ny) s'
|
|
| 454 | + go wx nx wy ny = Ubx.do
|
|
| 455 | + mwaInitArrayBinOp mwa wx wy xor#
|
|
| 456 | + mwaArrayCopy# mwa ny wx ny (nx -# ny)
|
|
| 449 | 457 | |
| 450 | 458 | -- | AND two non-zero BigNat
|
| 451 | 459 | --
|
| ... | ... | @@ -475,9 +483,9 @@ bignat_and_not |
| 475 | 483 | -> WordArray#
|
| 476 | 484 | -> State# RealWorld
|
| 477 | 485 | -> State# RealWorld
|
| 478 | -bignat_and_not mwa wa wb s =
|
|
| 479 | - case mwaInitArrayBinOp mwa wa wb (\x y -> x `and#` not# y) s of
|
|
| 480 | - s' -> mwaArrayCopy# mwa szB wa szB (szA -# szB) s'
|
|
| 486 | +bignat_and_not mwa wa wb = Ubx.do
|
|
| 487 | + mwaInitArrayBinOp mwa wa wb (\x y -> x `and#` not# y)
|
|
| 488 | + mwaArrayCopy# mwa szB wa szB (szA -# szB)
|
|
| 481 | 489 | where
|
| 482 | 490 | !szA = wordArraySize# wa
|
| 483 | 491 | !szB = wordArraySize# wb
|
| ... | ... | @@ -497,7 +505,7 @@ bignat_quotrem |
| 497 | 505 | -> WordArray#
|
| 498 | 506 | -> State# s
|
| 499 | 507 | -> State# s
|
| 500 | -bignat_quotrem mwq mwr uwa uwb s0 =
|
|
| 508 | +bignat_quotrem mwq mwr uwa uwb = Ubx.do
|
|
| 501 | 509 | -- Normalization consists in left-shifting bits in B and A so that the
|
| 502 | 510 | -- most-significant bit of the most-significant word of B is 1. It makes
|
| 503 | 511 | -- quotient prediction much more efficient as we only use the two most
|
| ... | ... | @@ -513,30 +521,27 @@ bignat_quotrem mwq mwr uwa uwb s0 = |
| 513 | 521 | --
|
| 514 | 522 | -- We allocate it here with an additionnal Word compared to A because
|
| 515 | 523 | -- normalizing can left shift at most (N-1) bits (on N-bit arch).
|
| 516 | - in case newWordArray# (wordArraySize# uwa +# 1#) s0 of { (# s1, mnwa #) ->
|
|
| 524 | + mnwa <- newWordArray# (wordArraySize# uwa +# 1#)
|
|
| 517 | 525 | |
| 518 | 526 | -- normalized A in mnwa
|
| 519 | - let normalizeA s = case mwaWrite# mnwa (wordArraySize# uwa) 0## s of -- init potential carry
|
|
| 520 | - s -> case bignat_shiftl mnwa uwa clzb s of -- left shift
|
|
| 521 | - s -> mwaTrimZeroes# mnwa s -- remove null carry if any
|
|
| 522 | - in case normalizeA s1 of { s2 ->
|
|
| 527 | + mwaWrite# mnwa (wordArraySize# uwa) 0## -- init potential carry
|
|
| 528 | + bignat_shiftl mnwa uwa clzb -- left shift
|
|
| 529 | + mwaTrimZeroes# mnwa -- remove null carry if any
|
|
| 523 | 530 | |
| 524 | 531 | -- normalize B. We don't do it in a MutableWordArray because it will remain
|
| 525 | 532 | -- constant during the whole computation.
|
| 526 | - let !nwb = bigNatShiftL# uwb clzb in
|
|
| 533 | + let !nwb = bigNatShiftL# uwb clzb
|
|
| 527 | 534 | |
| 528 | 535 | -- perform quotrem on normalized inputs
|
| 529 | - case bignat_quotrem_normalized mwq mnwa nwb s2 of { s3 ->
|
|
| 536 | + bignat_quotrem_normalized mwq mnwa nwb
|
|
| 530 | 537 | |
| 531 | 538 | -- denormalize the remainder now stored in mnwa. We just have to right shift
|
| 532 | 539 | -- of "clzb" bits. We copy the result into "mwr" array.
|
| 533 | - let denormalizeR s = case mwaTrimZeroes# mnwa s of
|
|
| 534 | - s -> case unsafeFreezeByteArray# mnwa s of
|
|
| 535 | - (# s, wr #) -> case mwaSetSize# mwr (wordArraySize# wr) s of
|
|
| 536 | - s -> case bignat_shiftr mwr wr clzb s of
|
|
| 537 | - s -> mwaTrimZeroes# mwr s
|
|
| 538 | - in denormalizeR s3
|
|
| 539 | - }}}
|
|
| 540 | + mwaTrimZeroes# mnwa
|
|
| 541 | + wr <- unsafeFreezeByteArray# mnwa
|
|
| 542 | + mwaSetSize# mwr (wordArraySize# wr)
|
|
| 543 | + bignat_shiftr mwr wr clzb
|
|
| 544 | + mwaTrimZeroes# mwr
|
|
| 540 | 545 | |
| 541 | 546 | |
| 542 | 547 | |
| ... | ... | @@ -553,10 +558,10 @@ bignat_quot |
| 553 | 558 | -> WordArray#
|
| 554 | 559 | -> State# RealWorld
|
| 555 | 560 | -> State# RealWorld
|
| 556 | -bignat_quot mwq wa wb s =
|
|
| 561 | +bignat_quot mwq wa wb = Ubx.do
|
|
| 557 | 562 | -- allocate a temporary array for the remainder and call quotrem
|
| 558 | - case newWordArray# (wordArraySize# wb) s of
|
|
| 559 | - (# s, mwr #) -> bignat_quotrem mwq mwr wa wb s
|
|
| 563 | + mwr <- newWordArray# (wordArraySize# wb)
|
|
| 564 | + bignat_quotrem mwq mwr wa wb
|
|
| 560 | 565 | |
| 561 | 566 | -- | Remainder of two non-zero BigNat
|
| 562 | 567 | --
|
| ... | ... | @@ -571,12 +576,12 @@ bignat_rem |
| 571 | 576 | -> WordArray#
|
| 572 | 577 | -> State# RealWorld
|
| 573 | 578 | -> State# RealWorld
|
| 574 | -bignat_rem mwr wa wb s =
|
|
| 579 | +bignat_rem mwr wa wb = Ubx.do
|
|
| 575 | 580 | -- allocate a temporary array for the quotient and call quotrem
|
| 576 | 581 | -- (we could avoid allocating it as it is not used to compute the result but
|
| 577 | 582 | -- it would require non trivial modification of bignat_quotrem)
|
| 578 | - case newWordArray# szQ s of
|
|
| 579 | - (# s, mwq #) -> bignat_quotrem mwq mwr wa wb s
|
|
| 583 | + mwq <- newWordArray# szQ
|
|
| 584 | + bignat_quotrem mwq mwr wa wb
|
|
| 580 | 585 | where
|
| 581 | 586 | szA = wordArraySize# wa
|
| 582 | 587 | szB = wordArraySize# wb
|
| ... | ... | @@ -593,14 +598,14 @@ bignat_quotrem_normalized |
| 593 | 598 | -> WordArray#
|
| 594 | 599 | -> State# s
|
| 595 | 600 | -> State# s
|
| 596 | -bignat_quotrem_normalized mwq mwa b s0 =
|
|
| 601 | +bignat_quotrem_normalized mwq mwa b = Ubx.do
|
|
| 597 | 602 | |
| 598 | 603 | -- n is the size of B
|
| 599 | 604 | let !n = wordArraySize# b
|
| 600 | 605 | |
| 601 | 606 | -- m+n is the size of A (m >= 0)
|
| 602 | - in case mwaSize# mwa s0 of { (# s1, szA #) ->
|
|
| 603 | - let !m = szA -# n in
|
|
| 607 | + szA <- mwaSize# mwa
|
|
| 608 | + let !m = szA -# n
|
|
| 604 | 609 | |
| 605 | 610 | -- Definitions:
|
| 606 | 611 | -- MSW(x) is the most-significant word of x
|
| ... | ... | @@ -624,18 +629,22 @@ bignat_quotrem_normalized mwq mwa b s0 = |
| 624 | 629 | -- A := A - (B << m words)
|
| 625 | 630 | -- else Qm = 0
|
| 626 | 631 | -- A unchanged
|
| 627 | - let computeQm s = case mwaTrimCompare m mwa b s of
|
|
| 628 | - (# s, LT #) -> (# s, 0## #)
|
|
| 629 | - (# s, _ #) -> (# s, 1## #)
|
|
| 630 | - |
|
| 631 | - updateQj j qj qjb s = case mwaWrite# mwq j qj s of -- write Qj
|
|
| 632 | - s | 0## <- qj -> s
|
|
| 633 | - | True -> case mwaSubInplaceArray mwa j qjb s of -- subtract (qjB << j words)
|
|
| 634 | - (# s, _ #) -> s
|
|
| 632 | + let computeQm = Ubx.do
|
|
| 633 | + c <- mwaTrimCompare m mwa b
|
|
| 634 | + case c of
|
|
| 635 | + LT -> Ubx.pure 0##
|
|
| 636 | + _ -> Ubx.pure 1##
|
|
| 637 | + |
|
| 638 | + updateQj j qj qjb = Ubx.do
|
|
| 639 | + mwaWrite# mwq j qj -- write Qj
|
|
| 640 | + case qj of
|
|
| 641 | + 0## -> Ubx.nop
|
|
| 642 | + _ -> Ubx.void (mwaSubInplaceArray mwa j qjb) -- subtract (qjB << j words)
|
|
| 635 | 643 | |
| 636 | 644 | -- update the highest word of Q
|
| 637 | - updateQm s = case computeQm s of
|
|
| 638 | - (# s, qm #) -> updateQj m qm b s
|
|
| 645 | + updateQm = Ubx.do
|
|
| 646 | + qm <- computeQm
|
|
| 647 | + updateQj m qm b
|
|
| 639 | 648 | |
| 640 | 649 | -- the size of Q is szA+szB+1 BEFORE normalization. Normalization may add
|
| 641 | 650 | -- an additional higher word to A.
|
| ... | ... | @@ -649,11 +658,13 @@ bignat_quotrem_normalized mwq mwa b s0 = |
| 649 | 658 | -- accordingly
|
| 650 | 659 | --
|
| 651 | 660 | -- We detect if A has an additional limb by comparing the size of Q with m
|
| 652 | - updateQmMaybe s = case mwaSize# mwq s of
|
|
| 653 | - (# s, szQ #) | isTrue# (m <# szQ) -> updateQm s
|
|
| 654 | - | True -> s
|
|
| 661 | + updateQmMaybe = Ubx.do
|
|
| 662 | + szQ <- mwaSize# mwq
|
|
| 663 | + if isTrue# (m <# szQ)
|
|
| 664 | + then updateQm
|
|
| 665 | + else Ubx.nop
|
|
| 655 | 666 | |
| 656 | - in case updateQmMaybe s1 of { s2 ->
|
|
| 667 | + updateQmMaybe
|
|
| 657 | 668 | |
| 658 | 669 | |
| 659 | 670 | -- main loop: for j from (m-1) downto 0
|
| ... | ... | @@ -669,33 +680,37 @@ bignat_quotrem_normalized mwq mwa b s0 = |
| 669 | 680 | |
| 670 | 681 | let bmsw = wordArrayLast# b -- most significant word of B
|
| 671 | 682 | |
| 672 | - estimateQj j s =
|
|
| 673 | - case mwaRead# mwa (n +# j) s of
|
|
| 674 | - (# s, a1 #) -> case mwaRead# mwa (n +# j -# 1#) s of
|
|
| 675 | - (# s, a0 #) -> case quotRemWord3# (# a1, a0 #) bmsw of
|
|
| 676 | - (# (# 0##, qj #), _ #) -> (# s, qj #)
|
|
| 677 | - (# (# _, _ #), _ #) -> (# s, WORD_MAXBOUND## #)
|
|
| 683 | + estimateQj j = Ubx.do
|
|
| 684 | + a1 <- mwaRead# mwa (n +# j)
|
|
| 685 | + a0 <- mwaRead# mwa (n +# j -# 1#)
|
|
| 686 | + case quotRemWord3# (# a1, a0 #) bmsw of
|
|
| 687 | + (# (# 0##, qj #), _ #) -> Ubx.pure qj
|
|
| 688 | + (# (# _, _ #), _ #) -> Ubx.pure WORD_MAXBOUND##
|
|
| 678 | 689 | |
| 679 | 690 | -- we perform the qj*B multiplication once and then we subtract B from
|
| 680 | 691 | -- qj*B as much as needed until (qj'*B << j words) <= A
|
| 681 | - findRealQj j qj s = findRealQj' j qj (bigNatMulWord# b qj) s
|
|
| 692 | + findRealQj j qj = findRealQj' j qj (bigNatMulWord# b qj)
|
|
| 682 | 693 | |
| 683 | - findRealQj' j qj qjB s = case mwaTrimCompare j mwa qjB s of
|
|
| 684 | - (# s, LT #) -> findRealQj' j (qj `minusWord#` 1##) (bigNatSubUnsafe qjB b) s
|
|
| 694 | + findRealQj' j qj qjB = Ubx.do
|
|
| 695 | + c <- mwaTrimCompare j mwa qjB
|
|
| 696 | + case c of
|
|
| 697 | + LT -> findRealQj' j (qj `minusWord#` 1##) (bigNatSubUnsafe qjB b)
|
|
| 685 | 698 | -- TODO: we could do the sub inplace to
|
| 686 | 699 | -- reduce allocations
|
| 687 | - (# s, _ #) -> (# s, qj, qjB #)
|
|
| 700 | + _ -> Ubx.pure (# qj, qjB #)
|
|
| 688 | 701 | |
| 689 | - loop j s = case estimateQj j s of
|
|
| 690 | - (# s, qj #) -> case findRealQj j qj s of
|
|
| 691 | - (# s, qj, qjB #) -> case updateQj j qj qjB s of
|
|
| 692 | - s | 0# <- j -> s
|
|
| 693 | - | True -> loop (j -# 1#) s
|
|
| 702 | + loop j = Ubx.do
|
|
| 703 | + qj <- estimateQj j
|
|
| 704 | + (# qj', qjB #) <- findRealQj j qj
|
|
| 705 | + updateQj j qj' qjB
|
|
| 706 | + case j of
|
|
| 707 | + 0# -> Ubx.nop
|
|
| 708 | + _ -> loop (j -# 1#)
|
|
| 694 | 709 | |
| 695 | 710 | |
| 696 | - in if | 0# <- m -> s2
|
|
| 697 | - | True -> loop (m -# 1#) s2
|
|
| 698 | - }}
|
|
| 711 | + case m of
|
|
| 712 | + 0# -> Ubx.nop
|
|
| 713 | + _ -> loop (m -# 1#)
|
|
| 699 | 714 | |
| 700 | 715 | -- | QuotRem of a non-zero BigNat and a non-zero Word
|
| 701 | 716 | --
|
| ... | ... | @@ -712,17 +727,17 @@ bignat_quotrem_word |
| 712 | 727 | -> Word#
|
| 713 | 728 | -> State# s
|
| 714 | 729 | -> (# State# s, Word# #)
|
| 715 | -bignat_quotrem_word mwq wa b s = go (sz -# 1#) 0## s
|
|
| 730 | +bignat_quotrem_word mwq wa b = go (sz -# 1#) 0##
|
|
| 716 | 731 | where
|
| 717 | 732 | sz = wordArraySize# wa
|
| 718 | - go i r s
|
|
| 719 | - | isTrue# (i <# 0#) = (# s, r #)
|
|
| 720 | - | True =
|
|
| 733 | + go i r
|
|
| 734 | + | isTrue# (i <# 0#) = Ubx.pure r
|
|
| 735 | + | True = Ubx.do
|
|
| 721 | 736 | let
|
| 722 | 737 | ai = indexWordArray# wa i
|
| 723 | 738 | !(# q,r' #) = quotRemWord2# r ai b
|
| 724 | - in case mwaWrite# mwq i q s of
|
|
| 725 | - s' -> go (i -# 1#) r' s'
|
|
| 739 | + mwaWrite# mwq i q
|
|
| 740 | + go (i -# 1#) r'
|
|
| 726 | 741 | |
| 727 | 742 | -- | Quot of a non-zero BigNat and a non-zero Word
|
| 728 | 743 | --
|
| ... | ... | @@ -737,17 +752,17 @@ bignat_quot_word |
| 737 | 752 | -> Word#
|
| 738 | 753 | -> State# s
|
| 739 | 754 | -> State# s
|
| 740 | -bignat_quot_word mwq wa b s = go (sz -# 1#) 0## s
|
|
| 755 | +bignat_quot_word mwq wa b = go (sz -# 1#) 0##
|
|
| 741 | 756 | where
|
| 742 | 757 | sz = wordArraySize# wa
|
| 743 | - go i r s
|
|
| 744 | - | isTrue# (i <# 0#) = s
|
|
| 745 | - | True =
|
|
| 758 | + go i r
|
|
| 759 | + | isTrue# (i <# 0#) = Ubx.nop
|
|
| 760 | + | True = Ubx.do
|
|
| 746 | 761 | let
|
| 747 | 762 | ai = indexWordArray# wa i
|
| 748 | 763 | !(# q,r' #) = quotRemWord2# r ai b
|
| 749 | - in case mwaWrite# mwq i q s of
|
|
| 750 | - s' -> go (i -# 1#) r' s'
|
|
| 764 | + mwaWrite# mwq i q
|
|
| 765 | + go (i -# 1#) r'
|
|
| 751 | 766 | |
| 752 | 767 | -- | Remainder of a non-zero BigNat and a non-zero Word
|
| 753 | 768 | --
|
| ... | ... | @@ -784,15 +799,15 @@ bignat_gcd |
| 784 | 799 | -> State# s
|
| 785 | 800 | bignat_gcd mwr = go
|
| 786 | 801 | where
|
| 787 | - go wmax wmin s
|
|
| 802 | + go wmax wmin
|
|
| 788 | 803 | | isTrue# (wordArraySize# wmin ==# 0#)
|
| 789 | - = mwaInitCopyShrink# mwr wmax s
|
|
| 804 | + = mwaInitCopyShrink# mwr wmax
|
|
| 790 | 805 | |
| 791 | 806 | | True
|
| 792 | 807 | = let
|
| 793 | 808 | wmax' = wmin
|
| 794 | 809 | !wmin' = bigNatRem wmax wmin
|
| 795 | - in go wmax' wmin' s
|
|
| 810 | + in go wmax' wmin'
|
|
| 796 | 811 | |
| 797 | 812 | -- | Greatest common divisor (GCD) of a non-zero/non-one BigNat and a
|
| 798 | 813 | -- non-zero/non-one Word#
|
| ... | ... | @@ -9,6 +9,11 @@ |
| 9 | 9 | {-# LANGUAGE PolyKinds #-}
|
| 10 | 10 | {-# LANGUAGE KindSignatures #-}
|
| 11 | 11 | {-# LANGUAGE BinaryLiterals #-}
|
| 12 | +{-# LANGUAGE QualifiedDo #-}
|
|
| 13 | +{-# LANGUAGE MonoLocalBinds #-}
|
|
| 14 | + -- Local State#-passing loops (e.g. in bigNatFromAddrLE#) must stay
|
|
| 15 | + -- monomorphic in the state type, otherwise GHC does not turn them into
|
|
| 16 | + -- join points (see #14620).
|
|
| 12 | 17 | |
| 13 | 18 | {-# OPTIONS_GHC -fdefines-known-key-names #-}
|
| 14 | 19 | -- Defines lots of functions that have BuiltinRules
|
| ... | ... | @@ -28,6 +33,7 @@ import GHC.Internal.Magic |
| 28 | 33 | import GHC.Internal.Bignum.Primitives
|
| 29 | 34 | import GHC.Internal.Bignum.WordArray
|
| 30 | 35 | import GHC.Internal.Bignum.Backend
|
| 36 | +import qualified GHC.Internal.Unboxed.Do as Ubx
|
|
| 31 | 37 | |
| 32 | 38 | -- | A BigNat
|
| 33 | 39 | --
|
| ... | ... | @@ -198,10 +204,10 @@ bigNatFromWordListUnsafe xs = |
| 198 | 204 | length i [] = i
|
| 199 | 205 | length i (_:ys) = length (i +# 1#) ys
|
| 200 | 206 | !lxs = length 0# xs
|
| 201 | - writeWordList _mwa _i [] s = s
|
|
| 202 | - writeWordList mwa i (W# w:ws) s =
|
|
| 203 | - case mwaWrite# mwa i w s of
|
|
| 204 | - s1 -> writeWordList mwa (i -# 1#) ws s1
|
|
| 207 | + writeWordList _mwa _i [] = Ubx.nop
|
|
| 208 | + writeWordList mwa i (W# w:ws) = Ubx.do
|
|
| 209 | + mwaWrite# mwa i w
|
|
| 210 | + writeWordList mwa (i -# 1#) ws
|
|
| 205 | 211 | in withNewWordArray# lxs \mwa ->
|
| 206 | 212 | writeWordList mwa (lxs -# 1#) xs
|
| 207 | 213 | |
| ... | ... | @@ -503,17 +509,18 @@ bigNatSubWordUnsafe# x y |
| 503 | 509 | where
|
| 504 | 510 | !sz = wordArraySize# x
|
| 505 | 511 | |
| 506 | - go mwa carry i s
|
|
| 512 | + go mwa carry i
|
|
| 507 | 513 | | isTrue# (i >=# sz)
|
| 508 | - = s
|
|
| 514 | + = Ubx.nop
|
|
| 509 | 515 | |
| 510 | 516 | | 0## <- carry
|
| 511 | - = mwaArrayCopy# mwa i x i (sz -# i) s
|
|
| 517 | + = mwaArrayCopy# mwa i x i (sz -# i)
|
|
| 512 | 518 | |
| 513 | 519 | | True
|
| 514 | - = case subWordC# (indexWordArray# x i) carry of
|
|
| 515 | - (# l, c #) -> case mwaWrite# mwa i l s of
|
|
| 516 | - s1 -> go mwa (int2Word# c) (i +# 1#) s1
|
|
| 520 | + = Ubx.do
|
|
| 521 | + let !(# l, c #) = subWordC# (indexWordArray# x i) carry
|
|
| 522 | + mwaWrite# mwa i l
|
|
| 523 | + go mwa (int2Word# c) (i +# 1#)
|
|
| 517 | 524 | |
| 518 | 525 | -- | Subtract a Word# from a BigNat
|
| 519 | 526 | --
|
| ... | ... | @@ -537,13 +544,14 @@ bigNatSubUnsafe a b |
| 537 | 544 | | bigNatIsZero b = a
|
| 538 | 545 | | True =
|
| 539 | 546 | let szA = wordArraySize# a
|
| 540 | - in withNewWordArrayTrimmed# szA \mwa s->
|
|
| 541 | - case inline bignat_sub mwa a b s of
|
|
| 542 | - (# s', 1# #) -> s'
|
|
| 543 | - (# s', _ #) -> case raiseUnderflow of
|
|
| 544 | - !_ -> s'
|
|
| 545 | - -- see Note [ghc-bignum exceptions] in
|
|
| 546 | - -- GHC.Internal.Bignum.Primitives
|
|
| 547 | + in withNewWordArrayTrimmed# szA \mwa -> Ubx.do
|
|
| 548 | + ok <- inline bignat_sub mwa a b
|
|
| 549 | + case ok of
|
|
| 550 | + 1# -> Ubx.nop
|
|
| 551 | + _ -> case raiseUnderflow of
|
|
| 552 | + !_ -> Ubx.nop
|
|
| 553 | + -- see Note [ghc-bignum exceptions] in
|
|
| 554 | + -- GHC.Internal.Bignum.Primitives
|
|
| 547 | 555 | |
| 548 | 556 | -- | Subtract two BigNat
|
| 549 | 557 | bigNatSub :: BigNat# -> BigNat# -> (# (# #) | BigNat# #)
|
| ... | ... | @@ -620,13 +628,12 @@ bigNatQuotRemWord# a b |
| 620 | 628 | | True =
|
| 621 | 629 | let
|
| 622 | 630 | sz = wordArraySize# a
|
| 623 | - io s =
|
|
| 624 | - case newWordArray# sz s of { (# s1, mwq #) ->
|
|
| 625 | - case inline bignat_quotrem_word mwq a b s1 of { (# s2, r #) ->
|
|
| 626 | - case mwaTrimZeroes# mwq s2 of { s3 ->
|
|
| 627 | - case unsafeFreezeByteArray# mwq s3 of { (# s4, wq #) ->
|
|
| 628 | - (# s4, (# wq, r #) #)
|
|
| 629 | - }}}}
|
|
| 631 | + io = Ubx.do
|
|
| 632 | + mwq <- newWordArray# sz
|
|
| 633 | + r <- inline bignat_quotrem_word mwq a b
|
|
| 634 | + mwaTrimZeroes# mwq
|
|
| 635 | + wq <- unsafeFreezeByteArray# mwq
|
|
| 636 | + Ubx.pure (# wq, r #)
|
|
| 630 | 637 | in case runRW# io of
|
| 631 | 638 | (# _, (# wq,r #) #) -> (# wq, r #)
|
| 632 | 639 | |
| ... | ... | @@ -815,9 +822,9 @@ bigNatOrWord# a b |
| 815 | 822 | | 0## <- b = a
|
| 816 | 823 | | True =
|
| 817 | 824 | let sz = wordArraySize# a
|
| 818 | - in withNewWordArray# sz \mwa s ->
|
|
| 819 | - case mwaArrayCopy# mwa 1# a 1# (sz -# 1#) s of
|
|
| 820 | - s' -> mwaWrite# mwa 0# (indexWordArray# a 0# `or#` b) s'
|
|
| 825 | + in withNewWordArray# sz \mwa -> Ubx.do
|
|
| 826 | + mwaArrayCopy# mwa 1# a 1# (sz -# 1#)
|
|
| 827 | + mwaWrite# mwa 0# (indexWordArray# a 0# `or#` b)
|
|
| 821 | 828 | |
| 822 | 829 | -- | Bitwise AND
|
| 823 | 830 | bigNatAnd :: BigNat# -> BigNat# -> BigNat#
|
| ... | ... | @@ -852,11 +859,10 @@ bigNatAndNotWord# :: BigNat# -> Word# -> BigNat# |
| 852 | 859 | bigNatAndNotWord# a b
|
| 853 | 860 | | bigNatIsZero a = a
|
| 854 | 861 | | szA <- bigNatSize# a
|
| 855 | - = withNewWordArray# szA \mwa s ->
|
|
| 862 | + = withNewWordArray# szA \mwa -> Ubx.do
|
|
| 856 | 863 | -- duplicate higher limbs
|
| 857 | - case mwaArrayCopy# mwa 1# a 1# (szA -# 1#) s of
|
|
| 858 | - s' -> writeWordArray# mwa 0#
|
|
| 859 | - (indexWordArray# a 0# `and#` not# b) s'
|
|
| 864 | + mwaArrayCopy# mwa 1# a 1# (szA -# 1#)
|
|
| 865 | + writeWordArray# mwa 0# (indexWordArray# a 0# `and#` not# b)
|
|
| 860 | 866 | |
| 861 | 867 | -- | Bitwise AND with Int#
|
| 862 | 868 | bigNatAndInt# :: BigNat# -> Int# -> BigNat#
|
| ... | ... | @@ -864,11 +870,10 @@ bigNatAndInt# a b |
| 864 | 870 | | bigNatIsZero a = a
|
| 865 | 871 | | isTrue# (b >=# 0#) = bigNatAndWord# a (int2Word# b)
|
| 866 | 872 | | szA <- bigNatSize# a
|
| 867 | - = withNewWordArray# szA \mwa s ->
|
|
| 873 | + = withNewWordArray# szA \mwa -> Ubx.do
|
|
| 868 | 874 | -- duplicate higher limbs (because of sign-extension of b)
|
| 869 | - case mwaArrayCopy# mwa 1# a 1# (szA -# 1#) s of
|
|
| 870 | - s' -> writeWordArray# mwa 0#
|
|
| 871 | - (indexWordArray# a 0# `and#` int2Word# b) s'
|
|
| 875 | + mwaArrayCopy# mwa 1# a 1# (szA -# 1#)
|
|
| 876 | + writeWordArray# mwa 0# (indexWordArray# a 0# `and#` int2Word# b)
|
|
| 872 | 877 | |
| 873 | 878 | |
| 874 | 879 | -- | Bitwise XOR
|
| ... | ... | @@ -891,9 +896,9 @@ bigNatXorWord# a b |
| 891 | 896 | | True =
|
| 892 | 897 | let
|
| 893 | 898 | sz = wordArraySize# a
|
| 894 | - in withNewWordArray# sz \mwa s ->
|
|
| 895 | - case mwaArrayCopy# mwa 1# a 1# (sz -# 1#) s of
|
|
| 896 | - s' -> mwaWrite# mwa 0# (indexWordArray# a 0# `xor#` b) s'
|
|
| 899 | + in withNewWordArray# sz \mwa -> Ubx.do
|
|
| 900 | + mwaArrayCopy# mwa 1# a 1# (sz -# 1#)
|
|
| 901 | + mwaWrite# mwa 0# (indexWordArray# a 0# `xor#` b)
|
|
| 897 | 902 | |
| 898 | 903 | -- | PopCount for BigNat
|
| 899 | 904 | bigNatPopCount :: BigNat# -> Word
|
| ... | ... | @@ -1005,11 +1010,11 @@ bigNatBit# i |
| 1005 | 1010 | !nb = word2Int# (i `and#` WORD_SIZE_BITS_MASK##)
|
| 1006 | 1011 | !sz = nw +# 1#
|
| 1007 | 1012 | !v = 1## `uncheckedShiftL#` nb
|
| 1008 | - in withNewWordArray# sz \mwa s ->
|
|
| 1013 | + in withNewWordArray# sz \mwa -> Ubx.do
|
|
| 1009 | 1014 | -- clear the array
|
| 1010 | - case mwaFill# mwa 0## 0## (int2Word# sz) s of
|
|
| 1011 | - -- set the bit in the most-significant word
|
|
| 1012 | - s2 -> mwaWrite# mwa (sz -# 1#) v s2
|
|
| 1015 | + mwaFill# mwa 0## 0## (int2Word# sz)
|
|
| 1016 | + -- set the bit in the most-significant word
|
|
| 1017 | + mwaWrite# mwa (sz -# 1#) v
|
|
| 1013 | 1018 | |
| 1014 | 1019 | -- | Return a BigNat whose bit `i` is the only one set.
|
| 1015 | 1020 | --
|
| ... | ... | @@ -1042,9 +1047,9 @@ bigNatClearBit# a n |
| 1042 | 1047 | mwaArrayCopy# mwa 0# a 0# nsz s
|
| 1043 | 1048 | |
| 1044 | 1049 | | True ->
|
| 1045 | - withNewWordArray# sz \mwa s ->
|
|
| 1046 | - case mwaArrayCopy# mwa 0# a 0# sz s of
|
|
| 1047 | - s' -> writeWordArray# mwa nw nv s'
|
|
| 1050 | + withNewWordArray# sz \mwa -> Ubx.do
|
|
| 1051 | + mwaArrayCopy# mwa 0# a 0# sz
|
|
| 1052 | + writeWordArray# mwa nw nv
|
|
| 1048 | 1053 | |
| 1049 | 1054 | -- | BigNat set bit
|
| 1050 | 1055 | bigNatSetBit# :: BigNat# -> Word# -> BigNat#
|
| ... | ... | @@ -1061,15 +1066,15 @@ bigNatSetBit# a n |
| 1061 | 1066 | in if
|
| 1062 | 1067 | -- result BigNat will have more limbs
|
| 1063 | 1068 | | isTrue# (d ># 0#)
|
| 1064 | - -> withNewWordArray# (nw +# 1#) \mwa s ->
|
|
| 1065 | - case mwaArrayCopy# mwa 0# a 0# sz s of
|
|
| 1066 | - s' -> case mwaFill# mwa 0## (int2Word# sz) (int2Word# (d -# 1#)) s' of
|
|
| 1067 | - s'' -> writeWordArray# mwa nw (bitW# nb) s''
|
|
| 1069 | + -> withNewWordArray# (nw +# 1#) \mwa -> Ubx.do
|
|
| 1070 | + mwaArrayCopy# mwa 0# a 0# sz
|
|
| 1071 | + mwaFill# mwa 0## (int2Word# sz) (int2Word# (d -# 1#))
|
|
| 1072 | + writeWordArray# mwa nw (bitW# nb)
|
|
| 1068 | 1073 | |
| 1069 | 1074 | | nv <- bigNatIndex# a nw `or#` bitW# nb
|
| 1070 | - -> withNewWordArray# sz \mwa s ->
|
|
| 1071 | - case mwaArrayCopy# mwa 0# a 0# sz s of
|
|
| 1072 | - s' -> writeWordArray# mwa nw nv s'
|
|
| 1075 | + -> withNewWordArray# sz \mwa -> Ubx.do
|
|
| 1076 | + mwaArrayCopy# mwa 0# a 0# sz
|
|
| 1077 | + writeWordArray# mwa nw nv
|
|
| 1073 | 1078 | |
| 1074 | 1079 | -- | Reverse the given bit
|
| 1075 | 1080 | bigNatComplementBit# :: BigNat# -> Word# -> BigNat#
|
| ... | ... | @@ -1082,15 +1087,15 @@ bigNatComplementBit# a n = |
| 1082 | 1087 | in if
|
| 1083 | 1088 | -- result BigNat will have more limbs
|
| 1084 | 1089 | | isTrue# (d ># 0#)
|
| 1085 | - -> withNewWordArray# (nw +# 1#) \mwa s ->
|
|
| 1086 | - case mwaArrayCopy# mwa 0# a 0# sz s of
|
|
| 1087 | - s' -> case mwaFill# mwa 0## (int2Word# sz) (int2Word# (d -# 1#)) s' of
|
|
| 1088 | - s'' -> writeWordArray# mwa nw (bitW# nb) s''
|
|
| 1090 | + -> withNewWordArray# (nw +# 1#) \mwa -> Ubx.do
|
|
| 1091 | + mwaArrayCopy# mwa 0# a 0# sz
|
|
| 1092 | + mwaFill# mwa 0## (int2Word# sz) (int2Word# (d -# 1#))
|
|
| 1093 | + writeWordArray# mwa nw (bitW# nb)
|
|
| 1089 | 1094 | |
| 1090 | 1095 | | nv <- bigNatIndex# a nw `xor#` bitW# nb
|
| 1091 | - -> withNewWordArrayTrimmed# sz \mwa s ->
|
|
| 1092 | - case mwaArrayCopy# mwa 0# a 0# sz s of
|
|
| 1093 | - s' -> writeWordArray# mwa nw nv s'
|
|
| 1096 | + -> withNewWordArrayTrimmed# sz \mwa -> Ubx.do
|
|
| 1097 | + mwaArrayCopy# mwa 0# a 0# sz
|
|
| 1098 | + writeWordArray# mwa nw nv
|
|
| 1094 | 1099 | |
| 1095 | 1100 | -------------------------------------------------
|
| 1096 | 1101 | -- Log operations
|
| ... | ... | @@ -1259,11 +1264,12 @@ bigNatCtzWord a = W# (bigNatCtzWord# a) |
| 1259 | 1264 | -- zero bytes written.
|
| 1260 | 1265 | bigNatToAddrLE# :: BigNat# -> Addr# -> State# s -> (# State# s, Word# #)
|
| 1261 | 1266 | {-# NOINLINE bigNatToAddrLE# #-}
|
| 1262 | -bigNatToAddrLE# a addr s0
|
|
| 1263 | - | isTrue# (sz ==# 0#) = (# s0, 0## #)
|
|
| 1264 | - | True = case writeMSB s0 of
|
|
| 1265 | - (# s1, k #) -> case go 0# s1 of
|
|
| 1266 | - s2 -> (# s2, k `plusWord#` (int2Word# li `uncheckedShiftL#` WORD_SIZE_BYTES_SHIFT#) #)
|
|
| 1267 | +bigNatToAddrLE# a addr
|
|
| 1268 | + | isTrue# (sz ==# 0#) = Ubx.pure 0##
|
|
| 1269 | + | True = Ubx.do
|
|
| 1270 | + k <- writeMSB
|
|
| 1271 | + go 0#
|
|
| 1272 | + Ubx.pure (k `plusWord#` (int2Word# li `uncheckedShiftL#` WORD_SIZE_BYTES_SHIFT#))
|
|
| 1267 | 1273 | where
|
| 1268 | 1274 | !sz = wordArraySize# a
|
| 1269 | 1275 | !li = sz -# 1#
|
| ... | ... | @@ -1271,15 +1277,16 @@ bigNatToAddrLE# a addr s0 |
| 1271 | 1277 | writeMSB = wordToAddrLE# (indexWordArray# a li)
|
| 1272 | 1278 | (addr `plusAddr#` (li `uncheckedIShiftL#` WORD_SIZE_BYTES_SHIFT#))
|
| 1273 | 1279 | |
| 1274 | - go i s
|
|
| 1280 | + go i
|
|
| 1275 | 1281 | | isTrue# (i <# li)
|
| 1276 | 1282 | , off <- i `uncheckedIShiftL#` WORD_SIZE_BYTES_SHIFT#
|
| 1277 | 1283 | , w <- indexWordArray# a i
|
| 1278 | - = case wordWriteAddrLE# w (addr `plusAddr#` off) s of
|
|
| 1279 | - s -> go (i +# 1#) s
|
|
| 1284 | + = Ubx.do
|
|
| 1285 | + wordWriteAddrLE# w (addr `plusAddr#` off)
|
|
| 1286 | + go (i +# 1#)
|
|
| 1280 | 1287 | |
| 1281 | 1288 | | True
|
| 1282 | - = s
|
|
| 1289 | + = Ubx.nop
|
|
| 1283 | 1290 | |
| 1284 | 1291 | -- | Write a BigNat in base-256 big-endian representation and return the
|
| 1285 | 1292 | -- number of bytes written.
|
| ... | ... | @@ -1289,23 +1296,24 @@ bigNatToAddrLE# a addr s0 |
| 1289 | 1296 | -- zero bytes written.
|
| 1290 | 1297 | bigNatToAddrBE# :: BigNat# -> Addr# -> State# s -> (# State# s, Word# #)
|
| 1291 | 1298 | {-# NOINLINE bigNatToAddrBE# #-}
|
| 1292 | -bigNatToAddrBE# a addr s0
|
|
| 1293 | - | isTrue# (sz ==# 0#) = (# s0, 0## #)
|
|
| 1299 | +bigNatToAddrBE# a addr
|
|
| 1300 | + | isTrue# (sz ==# 0#) = Ubx.pure 0##
|
|
| 1294 | 1301 | | msw <- indexWordArray# a (sz -# 1#)
|
| 1295 | - = case wordToAddrBE# msw addr s0 of
|
|
| 1296 | - (# s1, k #) -> case go (sz -# 1#) (addr `plusAddr#` word2Int# k) s1 of
|
|
| 1297 | - s2 -> (# s2, k `plusWord#` (int2Word# (sz -# 1#) `uncheckedShiftL#` WORD_SIZE_BYTES_SHIFT#) #)
|
|
| 1302 | + = Ubx.do
|
|
| 1303 | + k <- wordToAddrBE# msw addr
|
|
| 1304 | + go (sz -# 1#) (addr `plusAddr#` word2Int# k)
|
|
| 1305 | + Ubx.pure (k `plusWord#` (int2Word# (sz -# 1#) `uncheckedShiftL#` WORD_SIZE_BYTES_SHIFT#))
|
|
| 1298 | 1306 | where
|
| 1299 | 1307 | sz = wordArraySize# a
|
| 1300 | 1308 | |
| 1301 | - go i adr s
|
|
| 1309 | + go i adr
|
|
| 1302 | 1310 | | 0# <- i
|
| 1303 | - = s
|
|
| 1311 | + = Ubx.nop
|
|
| 1304 | 1312 | |
| 1305 | 1313 | | w <- indexWordArray# a (i -# 1#)
|
| 1306 | - = case wordWriteAddrBE# w adr s of
|
|
| 1307 | - s' -> go (i -# 1#)
|
|
| 1308 | - (adr `plusAddr#` WORD_SIZE_IN_BYTES# ) s'
|
|
| 1314 | + = Ubx.do
|
|
| 1315 | + wordWriteAddrBE# w adr
|
|
| 1316 | + go (i -# 1#) (adr `plusAddr#` WORD_SIZE_IN_BYTES# )
|
|
| 1309 | 1317 | |
| 1310 | 1318 | |
| 1311 | 1319 | -- | Write a BigNat in base-256 representation and return the
|
| ... | ... | @@ -1333,8 +1341,9 @@ bigNatToAddr# a addr _ s = bigNatToAddrBE# a addr s |
| 1333 | 1341 | -- written in advance. In case of @/i/ == 0@, the function will write and report
|
| 1334 | 1342 | -- zero bytes written.
|
| 1335 | 1343 | bigNatToAddr :: BigNat# -> Addr# -> Bool# -> IO Word
|
| 1336 | -bigNatToAddr a addr e = IO \s -> case bigNatToAddr# a addr e s of
|
|
| 1337 | - (# s', w #) -> (# s', W# w #)
|
|
| 1344 | +bigNatToAddr a addr e = IO Ubx.do
|
|
| 1345 | + w <- bigNatToAddr# a addr e
|
|
| 1346 | + Ubx.pure (W# w)
|
|
| 1338 | 1347 | |
| 1339 | 1348 | |
| 1340 | 1349 | |
| ... | ... | @@ -1349,34 +1358,37 @@ bigNatToAddr a addr e = IO \s -> case bigNatToAddr# a addr e s of |
| 1349 | 1358 | -- Higher limbs equal to 0 are automatically trimmed.
|
| 1350 | 1359 | bigNatFromAddrLE# :: Word# -> Addr# -> State# s -> (# State# s, BigNat# #)
|
| 1351 | 1360 | {-# NOINLINE bigNatFromAddrLE# #-}
|
| 1352 | -bigNatFromAddrLE# 0## _ s = (# s, bigNatZero# (# #) #)
|
|
| 1353 | -bigNatFromAddrLE# sz addr s =
|
|
| 1361 | +bigNatFromAddrLE# 0## _ = Ubx.pure (bigNatZero# (# #))
|
|
| 1362 | +bigNatFromAddrLE# sz addr =
|
|
| 1354 | 1363 | let
|
| 1355 | 1364 | !nw = sz `uncheckedShiftRL#` WORD_SIZE_BYTES_SHIFT#
|
| 1356 | 1365 | !nb = sz `and#` WORD_SIZE_BYTES_MASK##
|
| 1357 | 1366 | |
| 1358 | - readMSB mwa s
|
|
| 1367 | + readMSB mwa
|
|
| 1359 | 1368 | | 0## <- nb
|
| 1360 | - = s
|
|
| 1369 | + = Ubx.nop
|
|
| 1361 | 1370 | |
| 1362 | 1371 | | off <- word2Int# (nw `uncheckedShiftL#` WORD_SIZE_BYTES_SHIFT#)
|
| 1363 | - = case wordFromAddrLE# nb (addr `plusAddr#` off) s of
|
|
| 1364 | - (# s, w #) -> mwaWrite# mwa (word2Int# nw) w s
|
|
| 1372 | + = Ubx.do
|
|
| 1373 | + w <- wordFromAddrLE# nb (addr `plusAddr#` off)
|
|
| 1374 | + mwaWrite# mwa (word2Int# nw) w
|
|
| 1365 | 1375 | |
| 1366 | - go mwa i s
|
|
| 1376 | + go mwa i
|
|
| 1367 | 1377 | | isTrue# (i ==# word2Int# nw)
|
| 1368 | - = s
|
|
| 1378 | + = Ubx.nop
|
|
| 1369 | 1379 | |
| 1370 | 1380 | | off <- i `uncheckedIShiftL#` WORD_SIZE_BYTES_SHIFT#
|
| 1371 | - = case wordFromAddrLE# WORD_SIZE_IN_BYTES## (addr `plusAddr#` off) s of
|
|
| 1372 | - (# s, w #) -> case mwaWrite# mwa i w s of
|
|
| 1373 | - s -> go mwa (i +# 1#) s
|
|
| 1374 | - |
|
| 1375 | - in case newWordArray# (word2Int# nw +# (word2Int# nb /=# 0#)) s of
|
|
| 1376 | - (# s, mwa #) -> case readMSB mwa s of
|
|
| 1377 | - s -> case go mwa 0# s of
|
|
| 1378 | - s -> case mwaTrimZeroes# mwa s of
|
|
| 1379 | - s -> unsafeFreezeByteArray# mwa s
|
|
| 1381 | + = Ubx.do
|
|
| 1382 | + w <- wordFromAddrLE# WORD_SIZE_IN_BYTES## (addr `plusAddr#` off)
|
|
| 1383 | + mwaWrite# mwa i w
|
|
| 1384 | + go mwa (i +# 1#)
|
|
| 1385 | + |
|
| 1386 | + in Ubx.do
|
|
| 1387 | + mwa <- newWordArray# (word2Int# nw +# (word2Int# nb /=# 0#))
|
|
| 1388 | + readMSB mwa
|
|
| 1389 | + go mwa 0#
|
|
| 1390 | + mwaTrimZeroes# mwa
|
|
| 1391 | + unsafeFreezeByteArray# mwa
|
|
| 1380 | 1392 | |
| 1381 | 1393 | -- | Read a BigNat in base-256 big-endian representation from an Addr#.
|
| 1382 | 1394 | --
|
| ... | ... | @@ -1385,35 +1397,38 @@ bigNatFromAddrLE# sz addr s = |
| 1385 | 1397 | -- Null higher limbs are automatically trimmed.
|
| 1386 | 1398 | bigNatFromAddrBE# :: Word# -> Addr# -> State# s -> (# State# s, BigNat# #)
|
| 1387 | 1399 | {-# NOINLINE bigNatFromAddrBE# #-}
|
| 1388 | -bigNatFromAddrBE# 0## _ s = (# s, bigNatZero# (# #) #)
|
|
| 1389 | -bigNatFromAddrBE# sz addr s =
|
|
| 1400 | +bigNatFromAddrBE# 0## _ = Ubx.pure (bigNatZero# (# #))
|
|
| 1401 | +bigNatFromAddrBE# sz addr =
|
|
| 1390 | 1402 | let
|
| 1391 | 1403 | !nw = word2Int# (sz `uncheckedShiftRL#` WORD_SIZE_BYTES_SHIFT#)
|
| 1392 | 1404 | !nb = sz `and#` WORD_SIZE_BYTES_MASK##
|
| 1393 | 1405 | |
| 1394 | - goMSB mwa s
|
|
| 1406 | + goMSB mwa
|
|
| 1395 | 1407 | | 0## <- nb
|
| 1396 | - = s
|
|
| 1408 | + = Ubx.nop
|
|
| 1397 | 1409 | |
| 1398 | 1410 | | True
|
| 1399 | - = case wordFromAddrBE# nb addr s of
|
|
| 1400 | - (# s, w #) -> mwaWrite# mwa nw w s
|
|
| 1411 | + = Ubx.do
|
|
| 1412 | + w <- wordFromAddrBE# nb addr
|
|
| 1413 | + mwaWrite# mwa nw w
|
|
| 1401 | 1414 | |
| 1402 | - go mwa i s
|
|
| 1415 | + go mwa i
|
|
| 1403 | 1416 | | isTrue# (i ==# nw)
|
| 1404 | - = s
|
|
| 1417 | + = Ubx.nop
|
|
| 1405 | 1418 | |
| 1406 | 1419 | | k <- nw -# 1# -# i
|
| 1407 | 1420 | , off <- (k `uncheckedIShiftL#` WORD_SIZE_BYTES_SHIFT#) +# word2Int# nb
|
| 1408 | - = case wordFromAddrBE# WORD_SIZE_IN_BYTES## (addr `plusAddr#` off) s of
|
|
| 1409 | - (# s, w #) -> case mwaWrite# mwa i w s of
|
|
| 1410 | - s -> go mwa (i +# 1#) s
|
|
| 1411 | - |
|
| 1412 | - in case newWordArray# (nw +# (word2Int# nb /=# 0#)) s of
|
|
| 1413 | - (# s, mwa #) -> case goMSB mwa s of
|
|
| 1414 | - s -> case go mwa 0# s of
|
|
| 1415 | - s -> case mwaTrimZeroes# mwa s of
|
|
| 1416 | - s -> unsafeFreezeByteArray# mwa s
|
|
| 1421 | + = Ubx.do
|
|
| 1422 | + w <- wordFromAddrBE# WORD_SIZE_IN_BYTES## (addr `plusAddr#` off)
|
|
| 1423 | + mwaWrite# mwa i w
|
|
| 1424 | + go mwa (i +# 1#)
|
|
| 1425 | + |
|
| 1426 | + in Ubx.do
|
|
| 1427 | + mwa <- newWordArray# (nw +# (word2Int# nb /=# 0#))
|
|
| 1428 | + goMSB mwa
|
|
| 1429 | + go mwa 0#
|
|
| 1430 | + mwaTrimZeroes# mwa
|
|
| 1431 | + unsafeFreezeByteArray# mwa
|
|
| 1417 | 1432 | |
| 1418 | 1433 | -- | Read a BigNat in base-256 representation from an Addr#.
|
| 1419 | 1434 | --
|
| ... | ... | @@ -1440,11 +1455,12 @@ bigNatFromAddr# sz addr _ s = bigNatFromAddrBE# sz addr s |
| 1440 | 1455 | -- zero bytes written.
|
| 1441 | 1456 | bigNatToMutableByteArrayLE# :: BigNat# -> MutableByteArray# s -> Word# -> State# s -> (# State# s, Word# #)
|
| 1442 | 1457 | {-# NOINLINE bigNatToMutableByteArrayLE# #-}
|
| 1443 | -bigNatToMutableByteArrayLE# a mba moff s0
|
|
| 1444 | - | isTrue# (sz ==# 0#) = (# s0, 0## #)
|
|
| 1445 | - | True = case writeMSB s0 of
|
|
| 1446 | - (# s1, k #) -> case go 0# s1 of
|
|
| 1447 | - s2 -> (# s2, k `plusWord#` (int2Word# li `uncheckedShiftL#` WORD_SIZE_BYTES_SHIFT#) #)
|
|
| 1458 | +bigNatToMutableByteArrayLE# a mba moff
|
|
| 1459 | + | isTrue# (sz ==# 0#) = Ubx.pure 0##
|
|
| 1460 | + | True = Ubx.do
|
|
| 1461 | + k <- writeMSB
|
|
| 1462 | + go 0#
|
|
| 1463 | + Ubx.pure (k `plusWord#` (int2Word# li `uncheckedShiftL#` WORD_SIZE_BYTES_SHIFT#))
|
|
| 1448 | 1464 | where
|
| 1449 | 1465 | !sz = wordArraySize# a
|
| 1450 | 1466 | !li = sz -# 1#
|
| ... | ... | @@ -1452,15 +1468,16 @@ bigNatToMutableByteArrayLE# a mba moff s0 |
| 1452 | 1468 | writeMSB = wordToMutableByteArrayLE# (indexWordArray# a li)
|
| 1453 | 1469 | mba (moff `plusWord#` int2Word# (li `uncheckedIShiftL#` WORD_SIZE_BYTES_SHIFT#))
|
| 1454 | 1470 | |
| 1455 | - go i s
|
|
| 1471 | + go i
|
|
| 1456 | 1472 | | isTrue# (i <# li)
|
| 1457 | 1473 | , off <- int2Word# i `uncheckedShiftL#` WORD_SIZE_BYTES_SHIFT#
|
| 1458 | 1474 | , w <- indexWordArray# a i
|
| 1459 | - = case wordWriteMutableByteArrayLE# w mba (moff `plusWord#` off) s of
|
|
| 1460 | - s -> go (i +# 1#) s
|
|
| 1475 | + = Ubx.do
|
|
| 1476 | + wordWriteMutableByteArrayLE# w mba (moff `plusWord#` off)
|
|
| 1477 | + go (i +# 1#)
|
|
| 1461 | 1478 | |
| 1462 | 1479 | | True
|
| 1463 | - = s
|
|
| 1480 | + = Ubx.nop
|
|
| 1464 | 1481 | |
| 1465 | 1482 | -- | Write a BigNat in base-256 big-endian representation and return the
|
| 1466 | 1483 | -- number of bytes written.
|
| ... | ... | @@ -1470,23 +1487,24 @@ bigNatToMutableByteArrayLE# a mba moff s0 |
| 1470 | 1487 | -- zero bytes written.
|
| 1471 | 1488 | bigNatToMutableByteArrayBE# :: BigNat# -> MutableByteArray# s -> Word# -> State# s -> (# State# s, Word# #)
|
| 1472 | 1489 | {-# NOINLINE bigNatToMutableByteArrayBE# #-}
|
| 1473 | -bigNatToMutableByteArrayBE# a mba moff s0
|
|
| 1474 | - | isTrue# (sz ==# 0#) = (# s0, 0## #)
|
|
| 1490 | +bigNatToMutableByteArrayBE# a mba moff
|
|
| 1491 | + | isTrue# (sz ==# 0#) = Ubx.pure 0##
|
|
| 1475 | 1492 | | msw <- indexWordArray# a (sz -# 1#)
|
| 1476 | - = case wordToMutableByteArrayBE# msw mba moff s0 of
|
|
| 1477 | - (# s1, k #) -> case go (sz -# 1#) k s1 of
|
|
| 1478 | - s2 -> (# s2, k `plusWord#` (int2Word# (sz -# 1#) `uncheckedShiftL#` WORD_SIZE_BYTES_SHIFT#) #)
|
|
| 1493 | + = Ubx.do
|
|
| 1494 | + k <- wordToMutableByteArrayBE# msw mba moff
|
|
| 1495 | + go (sz -# 1#) k
|
|
| 1496 | + Ubx.pure (k `plusWord#` (int2Word# (sz -# 1#) `uncheckedShiftL#` WORD_SIZE_BYTES_SHIFT#))
|
|
| 1479 | 1497 | where
|
| 1480 | 1498 | sz = wordArraySize# a
|
| 1481 | 1499 | |
| 1482 | - go i c s
|
|
| 1500 | + go i c
|
|
| 1483 | 1501 | | 0# <- i
|
| 1484 | - = s
|
|
| 1502 | + = Ubx.nop
|
|
| 1485 | 1503 | |
| 1486 | 1504 | | w <- indexWordArray# a (i -# 1#)
|
| 1487 | - = case wordWriteMutableByteArrayBE# w mba (moff `plusWord#` c) s of
|
|
| 1488 | - s' -> go (i -# 1#)
|
|
| 1489 | - (c `plusWord#` WORD_SIZE_IN_BYTES## ) s'
|
|
| 1505 | + = Ubx.do
|
|
| 1506 | + wordWriteMutableByteArrayBE# w mba (moff `plusWord#` c)
|
|
| 1507 | + go (i -# 1#) (c `plusWord#` WORD_SIZE_IN_BYTES## )
|
|
| 1490 | 1508 | |
| 1491 | 1509 | |
| 1492 | 1510 | -- | Write a BigNat in base-256 representation and return the
|
| ... | ... | @@ -1514,34 +1532,36 @@ bigNatToMutableByteArray# a mba off _ s = bigNatToMutableByteArrayBE# a mba off |
| 1514 | 1532 | -- Null higher limbs are automatically trimmed.
|
| 1515 | 1533 | bigNatFromByteArrayLE# :: Word# -> ByteArray# -> Word# -> State# s -> (# State# s, BigNat# #)
|
| 1516 | 1534 | {-# NOINLINE bigNatFromByteArrayLE# #-}
|
| 1517 | -bigNatFromByteArrayLE# 0## _ _ s = (# s, bigNatZero# (# #) #)
|
|
| 1518 | -bigNatFromByteArrayLE# sz ba moff s =
|
|
| 1535 | +bigNatFromByteArrayLE# 0## _ _ = Ubx.pure (bigNatZero# (# #))
|
|
| 1536 | +bigNatFromByteArrayLE# sz ba moff =
|
|
| 1519 | 1537 | let
|
| 1520 | 1538 | !nw = sz `uncheckedShiftRL#` WORD_SIZE_BYTES_SHIFT#
|
| 1521 | 1539 | !nb = sz `and#` WORD_SIZE_BYTES_MASK##
|
| 1522 | 1540 | |
| 1523 | - readMSB mwa s
|
|
| 1541 | + readMSB mwa
|
|
| 1524 | 1542 | | 0## <- nb
|
| 1525 | - = s
|
|
| 1543 | + = Ubx.nop
|
|
| 1526 | 1544 | |
| 1527 | 1545 | | off <- nw `uncheckedShiftL#` WORD_SIZE_BYTES_SHIFT#
|
| 1528 | 1546 | = case wordFromByteArrayLE# nb ba (moff `plusWord#` off) of
|
| 1529 | - w -> mwaWrite# mwa (word2Int# nw) w s
|
|
| 1547 | + w -> mwaWrite# mwa (word2Int# nw) w
|
|
| 1530 | 1548 | |
| 1531 | - go mwa i s
|
|
| 1549 | + go mwa i
|
|
| 1532 | 1550 | | isTrue# (i `eqWord#` nw)
|
| 1533 | - = s
|
|
| 1551 | + = Ubx.nop
|
|
| 1534 | 1552 | |
| 1535 | 1553 | | off <- i `uncheckedShiftL#` WORD_SIZE_BYTES_SHIFT#
|
| 1536 | 1554 | = case wordFromByteArrayLE# WORD_SIZE_IN_BYTES## ba (moff `plusWord#` off) of
|
| 1537 | - w -> case mwaWrite# mwa (word2Int# i) w s of
|
|
| 1538 | - s -> go mwa (i `plusWord#` 1##) s
|
|
| 1555 | + w -> Ubx.do
|
|
| 1556 | + mwaWrite# mwa (word2Int# i) w
|
|
| 1557 | + go mwa (i `plusWord#` 1##)
|
|
| 1539 | 1558 | |
| 1540 | - in case newWordArray# (word2Int# nw +# (word2Int# nb /=# 0#)) s of
|
|
| 1541 | - (# s, mwa #) -> case readMSB mwa s of
|
|
| 1542 | - s -> case go mwa 0## s of
|
|
| 1543 | - s -> case mwaTrimZeroes# mwa s of
|
|
| 1544 | - s -> unsafeFreezeByteArray# mwa s
|
|
| 1559 | + in Ubx.do
|
|
| 1560 | + mwa <- newWordArray# (word2Int# nw +# (word2Int# nb /=# 0#))
|
|
| 1561 | + readMSB mwa
|
|
| 1562 | + go mwa 0##
|
|
| 1563 | + mwaTrimZeroes# mwa
|
|
| 1564 | + unsafeFreezeByteArray# mwa
|
|
| 1545 | 1565 | |
| 1546 | 1566 | -- | Read a BigNat in base-256 big-endian representation from a ByteArray#.
|
| 1547 | 1567 | --
|
| ... | ... | @@ -1550,35 +1570,37 @@ bigNatFromByteArrayLE# sz ba moff s = |
| 1550 | 1570 | -- Null higher limbs are automatically trimmed.
|
| 1551 | 1571 | bigNatFromByteArrayBE# :: Word# -> ByteArray# -> Word# -> State# s -> (# State# s, BigNat# #)
|
| 1552 | 1572 | {-# NOINLINE bigNatFromByteArrayBE# #-}
|
| 1553 | -bigNatFromByteArrayBE# 0## _ _ s = (# s, bigNatZero# (# #) #)
|
|
| 1554 | -bigNatFromByteArrayBE# sz ba moff s =
|
|
| 1573 | +bigNatFromByteArrayBE# 0## _ _ = Ubx.pure (bigNatZero# (# #))
|
|
| 1574 | +bigNatFromByteArrayBE# sz ba moff =
|
|
| 1555 | 1575 | let
|
| 1556 | 1576 | !nw = sz `uncheckedShiftRL#` WORD_SIZE_BYTES_SHIFT#
|
| 1557 | 1577 | !nb = sz `and#` WORD_SIZE_BYTES_MASK##
|
| 1558 | 1578 | |
| 1559 | - goMSB mwa s
|
|
| 1579 | + goMSB mwa
|
|
| 1560 | 1580 | | 0## <- nb
|
| 1561 | - = s
|
|
| 1581 | + = Ubx.nop
|
|
| 1562 | 1582 | |
| 1563 | 1583 | | True
|
| 1564 | 1584 | = case wordFromByteArrayBE# nb ba moff of
|
| 1565 | - w -> mwaWrite# mwa (word2Int# nw) w s
|
|
| 1585 | + w -> mwaWrite# mwa (word2Int# nw) w
|
|
| 1566 | 1586 | |
| 1567 | - go mwa i s
|
|
| 1587 | + go mwa i
|
|
| 1568 | 1588 | | isTrue# (i `eqWord#` nw)
|
| 1569 | - = s
|
|
| 1589 | + = Ubx.nop
|
|
| 1570 | 1590 | |
| 1571 | 1591 | | k <- nw `minusWord#` 1## `minusWord#` i
|
| 1572 | 1592 | , off <- (k `uncheckedShiftL#` WORD_SIZE_BYTES_SHIFT#) `plusWord#` nb
|
| 1573 | 1593 | = case wordFromByteArrayBE# WORD_SIZE_IN_BYTES## ba (moff `plusWord#` off) of
|
| 1574 | - w -> case mwaWrite# mwa (word2Int# i) w s of
|
|
| 1575 | - s -> go mwa (i `plusWord#` 1##) s
|
|
| 1576 | - |
|
| 1577 | - in case newWordArray# (word2Int# nw +# (word2Int# nb /=# 0#)) s of
|
|
| 1578 | - (# s, mwa #) -> case goMSB mwa s of
|
|
| 1579 | - s -> case go mwa 0## s of
|
|
| 1580 | - s -> case mwaTrimZeroes# mwa s of
|
|
| 1581 | - s -> unsafeFreezeByteArray# mwa s
|
|
| 1594 | + w -> Ubx.do
|
|
| 1595 | + mwaWrite# mwa (word2Int# i) w
|
|
| 1596 | + go mwa (i `plusWord#` 1##)
|
|
| 1597 | + |
|
| 1598 | + in Ubx.do
|
|
| 1599 | + mwa <- newWordArray# (word2Int# nw +# (word2Int# nb /=# 0#))
|
|
| 1600 | + goMSB mwa
|
|
| 1601 | + go mwa 0##
|
|
| 1602 | + mwaTrimZeroes# mwa
|
|
| 1603 | + unsafeFreezeByteArray# mwa
|
|
| 1582 | 1604 | |
| 1583 | 1605 | -- | Read a BigNat in base-256 representation from a ByteArray#.
|
| 1584 | 1606 | --
|
| ... | ... | @@ -7,6 +7,7 @@ |
| 7 | 7 | {-# LANGUAGE BinaryLiterals #-}
|
| 8 | 8 | {-# LANGUAGE BlockArguments #-}
|
| 9 | 9 | {-# LANGUAGE LambdaCase #-}
|
| 10 | +{-# LANGUAGE QualifiedDo #-}
|
|
| 10 | 11 | |
| 11 | 12 | {-# OPTIONS_GHC -fdefines-known-key-names #-}
|
| 12 | 13 | -- Defines lots of functions that have BuiltinRules
|
| ... | ... | @@ -166,6 +167,7 @@ import GHC.Internal.Bignum.Primitives |
| 166 | 167 | import GHC.Internal.Bignum.BigNat
|
| 167 | 168 | import GHC.Internal.Bignum.Natural
|
| 168 | 169 | import qualified GHC.Internal.Bignum.Backend as Backend
|
| 170 | +import qualified GHC.Internal.Unboxed.Do as Ubx
|
|
| 169 | 171 | |
| 170 | 172 | -- | Arbitrary precision integers. In contrast with fixed-size integral types
|
| 171 | 173 | -- such as 'Int', the 'Integer' type represents the entire infinite range of
|
| ... | ... | @@ -1334,8 +1336,9 @@ integerToAddr# (IN n) = bigNatToAddr# n |
| 1334 | 1336 | -- byte first (big-endian) if @1#@ or least significant byte first
|
| 1335 | 1337 | -- (little-endian) if @0#@.
|
| 1336 | 1338 | integerToAddr :: Integer -> Addr# -> Bool# -> IO Word
|
| 1337 | -integerToAddr a addr e = IO \s -> case integerToAddr# a addr e s of
|
|
| 1338 | - (# s', w #) -> (# s', W# w #)
|
|
| 1339 | +integerToAddr a addr e = IO Ubx.do
|
|
| 1340 | + w <- integerToAddr# a addr e
|
|
| 1341 | + Ubx.pure (W# w)
|
|
| 1339 | 1342 | |
| 1340 | 1343 | -- | Read an 'Integer' (without sign) in base-256 representation from an Addr#.
|
| 1341 | 1344 | --
|
| ... | ... | @@ -1347,9 +1350,9 @@ integerToAddr a addr e = IO \s -> case integerToAddr# a addr e s of |
| 1347 | 1350 | --
|
| 1348 | 1351 | -- Null higher limbs are automatically trimed.
|
| 1349 | 1352 | integerFromAddr# :: Word# -> Addr# -> Bool# -> State# s -> (# State# s, Integer #)
|
| 1350 | -integerFromAddr# sz addr e s =
|
|
| 1351 | - case bigNatFromAddr# sz addr e s of
|
|
| 1352 | - (# s', n #) -> (# s', integerFromBigNat# n #)
|
|
| 1353 | +integerFromAddr# sz addr e = Ubx.do
|
|
| 1354 | + n <- bigNatFromAddr# sz addr e
|
|
| 1355 | + Ubx.pure (integerFromBigNat# n)
|
|
| 1353 | 1356 | |
| 1354 | 1357 | -- | Read an 'Integer' (without sign) in base-256 representation from an Addr#.
|
| 1355 | 1358 | --
|
| ... | ... | @@ -1383,8 +1386,9 @@ integerToMutableByteArray# (IN a) = bigNatToMutableByteArray# a |
| 1383 | 1386 | -- byte first (big-endian) if @1#@ or least significant byte first
|
| 1384 | 1387 | -- (little-endian) if @0#@.
|
| 1385 | 1388 | integerToMutableByteArray :: Integer -> MutableByteArray# RealWorld -> Word# -> Bool# -> IO Word
|
| 1386 | -integerToMutableByteArray i mba w e = IO \s -> case integerToMutableByteArray# i mba w e s of
|
|
| 1387 | - (# s', r #) -> (# s', W# r #)
|
|
| 1389 | +integerToMutableByteArray i mba w e = IO Ubx.do
|
|
| 1390 | + r <- integerToMutableByteArray# i mba w e
|
|
| 1391 | + Ubx.pure (W# r)
|
|
| 1388 | 1392 | |
| 1389 | 1393 | -- | Read an 'Integer' (without sign) in base-256 representation from a ByteArray#.
|
| 1390 | 1394 | --
|
| ... | ... | @@ -1396,8 +1400,9 @@ integerToMutableByteArray i mba w e = IO \s -> case integerToMutableByteArray# i |
| 1396 | 1400 | --
|
| 1397 | 1401 | -- Null higher limbs are automatically trimed.
|
| 1398 | 1402 | integerFromByteArray# :: Word# -> ByteArray# -> Word# -> Bool# -> State# s -> (# State# s, Integer #)
|
| 1399 | -integerFromByteArray# sz ba off e s = case bigNatFromByteArray# sz ba off e s of
|
|
| 1400 | - (# s', a #) -> (# s', integerFromBigNat# a #)
|
|
| 1403 | +integerFromByteArray# sz ba off e = Ubx.do
|
|
| 1404 | + a <- bigNatFromByteArray# sz ba off e
|
|
| 1405 | + Ubx.pure (integerFromBigNat# a)
|
|
| 1401 | 1406 | |
| 1402 | 1407 | -- | Read an 'Integer' (without sign) in base-256 representation from a ByteArray#.
|
| 1403 | 1408 | --
|
| ... | ... | @@ -4,6 +4,7 @@ |
| 4 | 4 | {-# LANGUAGE UnboxedTuples #-}
|
| 5 | 5 | {-# LANGUAGE BangPatterns #-}
|
| 6 | 6 | {-# LANGUAGE BlockArguments #-}
|
| 7 | +{-# LANGUAGE QualifiedDo #-}
|
|
| 7 | 8 | |
| 8 | 9 | {-# OPTIONS_GHC -fdefines-known-key-names #-}
|
| 9 | 10 | -- Defines lots of functions that have BuiltinRules
|
| ... | ... | @@ -121,6 +122,7 @@ import GHC.Internal.Classes |
| 121 | 122 | |
| 122 | 123 | import GHC.Internal.Bignum.BigNat
|
| 123 | 124 | import GHC.Internal.Bignum.Primitives
|
| 125 | +import qualified GHC.Internal.Unboxed.Do as Ubx
|
|
| 124 | 126 | |
| 125 | 127 | -- | Natural number
|
| 126 | 128 | --
|
| ... | ... | @@ -656,8 +658,9 @@ naturalToAddr# (NB n) = bigNatToAddr# n |
| 656 | 658 | -- byte first (big-endian) if @1#@ or least significant byte first
|
| 657 | 659 | -- (little-endian) if @0#@.
|
| 658 | 660 | naturalToAddr :: Natural -> Addr# -> Bool# -> IO Word
|
| 659 | -naturalToAddr a addr e = IO \s -> case naturalToAddr# a addr e s of
|
|
| 660 | - (# s', w #) -> (# s', W# w #)
|
|
| 661 | +naturalToAddr a addr e = IO Ubx.do
|
|
| 662 | + w <- naturalToAddr# a addr e
|
|
| 663 | + Ubx.pure (W# w)
|
|
| 661 | 664 | |
| 662 | 665 | |
| 663 | 666 | -- | Read a Natural in base-256 representation from an Addr#.
|
| ... | ... | @@ -670,9 +673,9 @@ naturalToAddr a addr e = IO \s -> case naturalToAddr# a addr e s of |
| 670 | 673 | --
|
| 671 | 674 | -- Null higher limbs are automatically trimed.
|
| 672 | 675 | naturalFromAddr# :: Word# -> Addr# -> Bool# -> State# s -> (# State# s, Natural #)
|
| 673 | -naturalFromAddr# sz addr e s =
|
|
| 674 | - case bigNatFromAddr# sz addr e s of
|
|
| 675 | - (# s', n #) -> (# s', naturalFromBigNat# n #)
|
|
| 676 | +naturalFromAddr# sz addr e = Ubx.do
|
|
| 677 | + n <- bigNatFromAddr# sz addr e
|
|
| 678 | + Ubx.pure (naturalFromBigNat# n)
|
|
| 676 | 679 | |
| 677 | 680 | -- | Read a Natural in base-256 representation from an Addr#.
|
| 678 | 681 | --
|
| ... | ... | @@ -707,8 +710,9 @@ naturalToMutableByteArray# (NB a) = bigNatToMutableByteArray# a |
| 707 | 710 | --
|
| 708 | 711 | -- Null higher limbs are automatically trimed.
|
| 709 | 712 | naturalFromByteArray# :: Word# -> ByteArray# -> Word# -> Bool# -> State# s -> (# State# s, Natural #)
|
| 710 | -naturalFromByteArray# sz ba off e s = case bigNatFromByteArray# sz ba off e s of
|
|
| 711 | - (# s', a #) -> (# s', naturalFromBigNat# a #)
|
|
| 713 | +naturalFromByteArray# sz ba off e = Ubx.do
|
|
| 714 | + a <- bigNatFromByteArray# sz ba off e
|
|
| 715 | + Ubx.pure (naturalFromBigNat# a)
|
|
| 712 | 716 | |
| 713 | 717 | |
| 714 | 718 |
| ... | ... | @@ -11,6 +11,7 @@ |
| 11 | 11 | {-# LANGUAGE ScopedTypeVariables #-}
|
| 12 | 12 | {-# LANGUAGE BlockArguments #-}
|
| 13 | 13 | {-# LANGUAGE BinaryLiterals #-}
|
| 14 | +{-# LANGUAGE QualifiedDo #-}
|
|
| 14 | 15 | {-# OPTIONS_GHC -fexpose-all-unfoldings #-}
|
| 15 | 16 | |
| 16 | 17 | module GHC.Internal.Bignum.Primitives
|
| ... | ... | @@ -93,6 +94,7 @@ import GHC.Internal.Prim.Exception |
| 93 | 94 | |
| 94 | 95 | import GHC.Internal.Prim
|
| 95 | 96 | import GHC.Internal.Types
|
| 97 | +import qualified GHC.Internal.Unboxed.Do as Ubx
|
|
| 96 | 98 | |
| 97 | 99 | ----------------------------------
|
| 98 | 100 | -- Bool#
|
| ... | ... | @@ -322,13 +324,14 @@ wordReverseBits32# x0 = wordReverseBits# x0 |
| 322 | 324 | wordToAddrLE# :: Word# -> Addr# -> State# s -> (# State# s, Word# #)
|
| 323 | 325 | wordToAddrLE# x addr = go x 0#
|
| 324 | 326 | where
|
| 325 | - go w c s
|
|
| 327 | + go w c
|
|
| 326 | 328 | | 0## <- w
|
| 327 | - = (# s, int2Word# c #)
|
|
| 329 | + = Ubx.pure (int2Word# c)
|
|
| 328 | 330 | |
| 329 | 331 | | True
|
| 330 | - = case writeWord8OffAddr# addr c (wordToWord8# w) s of
|
|
| 331 | - s' -> go (w `uncheckedShiftRL#` 8#) (c +# 1#) s'
|
|
| 332 | + = Ubx.do
|
|
| 333 | + writeWord8OffAddr# addr c (wordToWord8# w)
|
|
| 334 | + go (w `uncheckedShiftRL#` 8#) (c +# 1#)
|
|
| 332 | 335 | |
| 333 | 336 | -- | Write a Word to @/addr/@ in base-256 big-endian representation and
|
| 334 | 337 | -- return the number of bytes written.
|
| ... | ... | @@ -337,14 +340,15 @@ wordToAddrBE# w addr = go 0# (WORD_SIZE_IN_BITS# -# clz) |
| 337 | 340 | where
|
| 338 | 341 | !clz = word2Int# (clz# w `and#` (not# 0b0111##)) -- keep complete bytes
|
| 339 | 342 | |
| 340 | - go c sh s
|
|
| 343 | + go c sh
|
|
| 341 | 344 | | 0# <- sh
|
| 342 | - = (# s, int2Word# c #)
|
|
| 345 | + = Ubx.pure (int2Word# c)
|
|
| 343 | 346 | |
| 344 | 347 | | True
|
| 345 | 348 | , w' <- wordToWord8# (w `uncheckedShiftRL#` (sh -# 8#))
|
| 346 | - = case writeWord8OffAddr# addr c w' s of
|
|
| 347 | - s' -> go (c +# 1#) (sh -# 8#) s'
|
|
| 349 | + = Ubx.do
|
|
| 350 | + writeWord8OffAddr# addr c w'
|
|
| 351 | + go (c +# 1#) (sh -# 8#)
|
|
| 348 | 352 | |
| 349 | 353 | -- | Write a Word to @/addr/@ in base-256 representation and
|
| 350 | 354 | -- return the number of bytes written.
|
| ... | ... | @@ -361,53 +365,55 @@ wordToAddr# a addr _ s = wordToAddrBE# a addr s |
| 361 | 365 | --
|
| 362 | 366 | -- @'n' is the number of bytes to read.
|
| 363 | 367 | wordFromAddrLE# :: Word# -> Addr# -> State# s -> (# State# s, Word# #)
|
| 364 | -wordFromAddrLE# n addr s
|
|
| 368 | +wordFromAddrLE# n addr
|
|
| 365 | 369 | -- Optimize when we read a full word
|
| 366 | 370 | | WORD_SIZE_IN_BYTES## <- n
|
| 367 | - = case readWordOffAddr# addr 0# s of
|
|
| 371 | + = Ubx.do
|
|
| 372 | + w <- readWordOffAddr# addr 0#
|
|
| 368 | 373 | #if defined(WORDS_BIGENDIAN)
|
| 369 | - (# s', w #) -> (# s', wordReverseBytes# w #)
|
|
| 374 | + Ubx.pure (wordReverseBytes# w)
|
|
| 370 | 375 | #else
|
| 371 | - (# s', w #) -> (# s', w #)
|
|
| 376 | + Ubx.pure w
|
|
| 372 | 377 | #endif
|
| 373 | 378 | |
| 374 | -wordFromAddrLE# n addr s0 = go 0## 0# s0
|
|
| 379 | +wordFromAddrLE# n addr = go 0## 0#
|
|
| 375 | 380 | where
|
| 376 | - go w c s
|
|
| 381 | + go w c
|
|
| 377 | 382 | | isTrue# (c ==# word2Int# n)
|
| 378 | - = (# s, w #)
|
|
| 383 | + = Ubx.pure w
|
|
| 379 | 384 | |
| 380 | 385 | | True
|
| 381 | - = case readWord8OffAddr# addr c s of
|
|
| 382 | - (# s', b #) -> go (w `or#` (word8ToWord# b `uncheckedShiftL#` (c `uncheckedIShiftL#` 3#)))
|
|
| 383 | - (c +# 1#)
|
|
| 384 | - s'
|
|
| 386 | + = Ubx.do
|
|
| 387 | + b <- readWord8OffAddr# addr c
|
|
| 388 | + go (w `or#` (word8ToWord# b `uncheckedShiftL#` (c `uncheckedIShiftL#` 3#)))
|
|
| 389 | + (c +# 1#)
|
|
| 385 | 390 | |
| 386 | 391 | -- | Read a Word from @/addr/@ in base-256 big-endian representation.
|
| 387 | 392 | --
|
| 388 | 393 | -- @'n' is the number of bytes to read.
|
| 389 | 394 | wordFromAddrBE# :: Word# -> Addr# -> State# s -> (# State# s, Word# #)
|
| 390 | -wordFromAddrBE# n addr s
|
|
| 395 | +wordFromAddrBE# n addr
|
|
| 391 | 396 | -- Optimize when we read a full word
|
| 392 | 397 | | WORD_SIZE_IN_BYTES## <- n
|
| 393 | - = case readWordOffAddr# addr 0# s of
|
|
| 398 | + = Ubx.do
|
|
| 399 | + w <- readWordOffAddr# addr 0#
|
|
| 394 | 400 | #if defined(WORDS_BIGENDIAN)
|
| 395 | - (# s', w #) -> (# s', w #)
|
|
| 401 | + Ubx.pure w
|
|
| 396 | 402 | #else
|
| 397 | - (# s', w #) -> (# s', wordReverseBytes# w #)
|
|
| 403 | + Ubx.pure (wordReverseBytes# w)
|
|
| 398 | 404 | #endif
|
| 399 | 405 | |
| 400 | -wordFromAddrBE# n addr s0 = go 0## 0# s0
|
|
| 406 | +wordFromAddrBE# n addr = go 0## 0#
|
|
| 401 | 407 | where
|
| 402 | - go w c s
|
|
| 408 | + go w c
|
|
| 403 | 409 | | isTrue# (c ==# word2Int# n)
|
| 404 | - = (# s, w #)
|
|
| 410 | + = Ubx.pure w
|
|
| 405 | 411 | |
| 406 | 412 | | True
|
| 407 | - = case readWord8OffAddr# addr c s of
|
|
| 408 | - (# s', b #) -> go ((w `uncheckedShiftL#` 8#) `or#` word8ToWord# b)
|
|
| 409 | - (c +# 1#)
|
|
| 410 | - s'
|
|
| 413 | + = Ubx.do
|
|
| 414 | + b <- readWord8OffAddr# addr c
|
|
| 415 | + go ((w `uncheckedShiftL#` 8#) `or#` word8ToWord# b)
|
|
| 416 | + (c +# 1#)
|
|
| 411 | 417 | |
| 412 | 418 | -- | Read a Word from @/addr/@ in base-256 representation.
|
| 413 | 419 | --
|
| ... | ... | @@ -447,13 +453,14 @@ wordWriteAddrBE# w addr = writeWordOffAddr# addr 0# |
| 447 | 453 | wordToMutableByteArrayLE# :: Word# -> MutableByteArray# s -> Word# -> State# s -> (# State# s, Word# #)
|
| 448 | 454 | wordToMutableByteArrayLE# x mba off = go x 0#
|
| 449 | 455 | where
|
| 450 | - go w c s
|
|
| 456 | + go w c
|
|
| 451 | 457 | | 0## <- w
|
| 452 | - = (# s, int2Word# c #)
|
|
| 458 | + = Ubx.pure (int2Word# c)
|
|
| 453 | 459 | |
| 454 | 460 | | True
|
| 455 | - = case writeWord8Array# mba (word2Int# off +# c) (wordToWord8# w) s of
|
|
| 456 | - s' -> go (w `uncheckedShiftRL#` 8#) (c +# 1#) s'
|
|
| 461 | + = Ubx.do
|
|
| 462 | + writeWord8Array# mba (word2Int# off +# c) (wordToWord8# w)
|
|
| 463 | + go (w `uncheckedShiftRL#` 8#) (c +# 1#)
|
|
| 457 | 464 | |
| 458 | 465 | -- | Write a Word to @/MutableByteArray/@ in base-256 big-endian representation and
|
| 459 | 466 | -- return the number of bytes written.
|
| ... | ... | @@ -464,14 +471,15 @@ wordToMutableByteArrayBE# w mba off = go 0# (WORD_SIZE_IN_BITS# -# clz) |
| 464 | 471 | where
|
| 465 | 472 | !clz = word2Int# (clz# w `and#` (not# 0b0111##)) -- keep complete bytes
|
| 466 | 473 | |
| 467 | - go c sh s
|
|
| 474 | + go c sh
|
|
| 468 | 475 | | 0# <- sh
|
| 469 | - = (# s, int2Word# c #)
|
|
| 476 | + = Ubx.pure (int2Word# c)
|
|
| 470 | 477 | |
| 471 | 478 | | True
|
| 472 | 479 | , w' <- wordToWord8# (w `uncheckedShiftRL#` (sh -# 8#))
|
| 473 | - = case writeWord8Array# mba (word2Int# off +# c) w' s of
|
|
| 474 | - s' -> go (c +# 1#) (sh -# 8#) s'
|
|
| 480 | + = Ubx.do
|
|
| 481 | + writeWord8Array# mba (word2Int# off +# c) w'
|
|
| 482 | + go (c +# 1#) (sh -# 8#)
|
|
| 475 | 483 | |
| 476 | 484 | -- | Write a Word to @/MutableByteArray/@ in base-256 representation and
|
| 477 | 485 | -- return the number of bytes written.
|
| ... | ... | @@ -568,21 +576,24 @@ wordFromByteArray# a ba off _ = wordFromByteArrayBE# a ba off |
| 568 | 576 | ----------------------------------
|
| 569 | 577 | |
| 570 | 578 | ioVoid :: IO a -> State# RealWorld -> State# RealWorld
|
| 571 | -ioVoid (IO io) s = case io s of
|
|
| 572 | - (# s', _ #) -> s'
|
|
| 579 | +ioVoid (IO io) = Ubx.void io
|
|
| 573 | 580 | |
| 574 | 581 | ioWord# :: IO Word -> State# RealWorld -> (# State# RealWorld, Word# #)
|
| 575 | -ioWord# (IO io) s = case io s of
|
|
| 576 | - (# s', W# w #) -> (# s', w #)
|
|
| 582 | +ioWord# (IO io) = Ubx.do
|
|
| 583 | + W# w <- io
|
|
| 584 | + Ubx.pure w
|
|
| 577 | 585 | |
| 578 | 586 | ioInt# :: IO Int -> State# RealWorld -> (# State# RealWorld, Int# #)
|
| 579 | -ioInt# (IO io) s = case io s of
|
|
| 580 | - (# s', I# i #) -> (# s', i #)
|
|
| 587 | +ioInt# (IO io) = Ubx.do
|
|
| 588 | + I# i <- io
|
|
| 589 | + Ubx.pure i
|
|
| 581 | 590 | |
| 582 | 591 | ioBool :: IO Bool -> State# RealWorld -> (# State# RealWorld, Bool# #)
|
| 583 | -ioBool (IO io) s = case io s of
|
|
| 584 | - (# s', False #) -> (# s', 0# #)
|
|
| 585 | - (# s', True #) -> (# s', 1# #)
|
|
| 592 | +ioBool (IO io) = Ubx.do
|
|
| 593 | + b <- io
|
|
| 594 | + case b of
|
|
| 595 | + False -> Ubx.pure 0#
|
|
| 596 | + True -> Ubx.pure 1#
|
|
| 586 | 597 | |
| 587 | 598 | |
| 588 | 599 | ----------------------------------
|
| ... | ... | @@ -8,7 +8,7 @@ |
| 8 | 8 | {-# LANGUAGE LambdaCase #-}
|
| 9 | 9 | {-# LANGUAGE PolyKinds #-}
|
| 10 | 10 | {-# LANGUAGE KindSignatures #-}
|
| 11 | -{-# OPTIONS_GHC -Wno-name-shadowing #-}
|
|
| 11 | +{-# LANGUAGE QualifiedDo #-}
|
|
| 12 | 12 | |
| 13 | 13 | module GHC.Internal.Bignum.WordArray where
|
| 14 | 14 | |
| ... | ... | @@ -16,6 +16,7 @@ import GHC.Internal.Prim |
| 16 | 16 | import GHC.Internal.Magic
|
| 17 | 17 | import GHC.Internal.Types
|
| 18 | 18 | import GHC.Internal.Bignum.Primitives
|
| 19 | +import qualified GHC.Internal.Unboxed.Do as Ubx
|
|
| 19 | 20 | |
| 20 | 21 | #include "MachDeps.h"
|
| 21 | 22 | #include "WordSize.h"
|
| ... | ... | @@ -44,11 +45,10 @@ withNewWordArray# |
| 44 | 45 | -> WordArray#
|
| 45 | 46 | withNewWordArray# sz act = case runRW# io of (# _, a #) -> a
|
| 46 | 47 | where
|
| 47 | - io s =
|
|
| 48 | - case newWordArray# sz s of { (# s, mwa #) ->
|
|
| 49 | - case act mwa s of { s ->
|
|
| 50 | - unsafeFreezeByteArray# mwa s
|
|
| 51 | - }}
|
|
| 48 | + io = Ubx.do
|
|
| 49 | + mwa <- newWordArray# sz
|
|
| 50 | + act mwa
|
|
| 51 | + unsafeFreezeByteArray# mwa
|
|
| 52 | 52 | |
| 53 | 53 | -- | Create two new WordArray# of the given sizes (*in Word#*) and apply the
|
| 54 | 54 | -- action to them before returning them frozen
|
| ... | ... | @@ -62,14 +62,13 @@ withNewWordArray2# |
| 62 | 62 | -> (# WordArray#, WordArray# #)
|
| 63 | 63 | withNewWordArray2# sz1 sz2 act = case runRW# io of (# _, a #) -> a
|
| 64 | 64 | where
|
| 65 | - io s =
|
|
| 66 | - case newWordArray# sz1 s of { (# s, mwa1 #) ->
|
|
| 67 | - case newWordArray# sz2 s of { (# s, mwa2 #) ->
|
|
| 68 | - case act mwa1 mwa2 s of { s ->
|
|
| 69 | - case unsafeFreezeByteArray# mwa1 s of { (# s, wa1 #) ->
|
|
| 70 | - case unsafeFreezeByteArray# mwa2 s of { (# s, wa2 #) ->
|
|
| 71 | - (# s, (# wa1, wa2 #) #)
|
|
| 72 | - }}}}}
|
|
| 65 | + io = Ubx.do
|
|
| 66 | + mwa1 <- newWordArray# sz1
|
|
| 67 | + mwa2 <- newWordArray# sz2
|
|
| 68 | + act mwa1 mwa2
|
|
| 69 | + wa1 <- unsafeFreezeByteArray# mwa1
|
|
| 70 | + wa2 <- unsafeFreezeByteArray# mwa2
|
|
| 71 | + Ubx.pure (# wa1, wa2 #)
|
|
| 73 | 72 | |
| 74 | 73 | -- | Create a new WordArray#
|
| 75 | 74 | newWordArray# :: Int# -> State# s -> (# State# s, MutableWordArray# s #)
|
| ... | ... | @@ -81,9 +80,9 @@ withNewWordArrayTrimmed# |
| 81 | 80 | :: Int# -- ^ Size in Word
|
| 82 | 81 | -> (MutableWordArray# RealWorld -> State# RealWorld -> State# RealWorld)
|
| 83 | 82 | -> WordArray#
|
| 84 | -withNewWordArrayTrimmed# sz act = withNewWordArray# sz \mwa s ->
|
|
| 85 | - case act mwa s of
|
|
| 86 | - s' -> mwaTrimZeroes# mwa s'
|
|
| 83 | +withNewWordArrayTrimmed# sz act = withNewWordArray# sz \mwa -> Ubx.do
|
|
| 84 | + act mwa
|
|
| 85 | + mwaTrimZeroes# mwa
|
|
| 87 | 86 | |
| 88 | 87 | -- | Create two new WordArray# of the given sizes (*in Word#*), apply the action
|
| 89 | 88 | -- to them, trim their most significant zeroes, then return them frozen
|
| ... | ... | @@ -95,10 +94,10 @@ withNewWordArray2Trimmed# |
| 95 | 94 | -> State# RealWorld
|
| 96 | 95 | -> State# RealWorld)
|
| 97 | 96 | -> (# WordArray#, WordArray# #)
|
| 98 | -withNewWordArray2Trimmed# sz1 sz2 act = withNewWordArray2# sz1 sz2 \mwa1 mwa2 s ->
|
|
| 99 | - case act mwa1 mwa2 s of
|
|
| 100 | - s' -> case mwaTrimZeroes# mwa1 s' of
|
|
| 101 | - s'' -> mwaTrimZeroes# mwa2 s''
|
|
| 97 | +withNewWordArray2Trimmed# sz1 sz2 act = withNewWordArray2# sz1 sz2 \mwa1 mwa2 -> Ubx.do
|
|
| 98 | + act mwa1 mwa2
|
|
| 99 | + mwaTrimZeroes# mwa1
|
|
| 100 | + mwaTrimZeroes# mwa2
|
|
| 102 | 101 | |
| 103 | 102 | -- | Create a new WordArray# of the given size (*in Word#*), apply the action to
|
| 104 | 103 | -- it. If the action returns true#, trim its most significant zeroes, then
|
| ... | ... | @@ -109,13 +108,15 @@ withNewWordArrayTrimmedMaybe# |
| 109 | 108 | -> (# (# #) | WordArray# #)
|
| 110 | 109 | withNewWordArrayTrimmedMaybe# sz act = case runRW# io of (# _, a #) -> a
|
| 111 | 110 | where
|
| 112 | - io s =
|
|
| 113 | - case newWordArray# sz s of
|
|
| 114 | - (# s, mwa #) -> case act mwa s of
|
|
| 115 | - (# s, 0# #) -> (# s, (# (# #) | #) #)
|
|
| 116 | - (# s, _ #) -> case mwaTrimZeroes# mwa s of
|
|
| 117 | - s -> case unsafeFreezeByteArray# mwa s of
|
|
| 118 | - (# s, ba #) -> (# s, (# | ba #) #)
|
|
| 111 | + io = Ubx.do
|
|
| 112 | + mwa <- newWordArray# sz
|
|
| 113 | + b <- act mwa
|
|
| 114 | + case b of
|
|
| 115 | + 0# -> Ubx.pure (# (# #) | #)
|
|
| 116 | + _ -> Ubx.do
|
|
| 117 | + mwaTrimZeroes# mwa
|
|
| 118 | + ba <- unsafeFreezeByteArray# mwa
|
|
| 119 | + Ubx.pure (# | ba #)
|
|
| 119 | 120 | |
| 120 | 121 | -- | Create a WordArray# from two Word#
|
| 121 | 122 | --
|
| ... | ... | @@ -124,9 +125,9 @@ withNewWordArrayTrimmedMaybe# sz act = case runRW# io of (# _, a #) -> a |
| 124 | 125 | -- l is the least significant word
|
| 125 | 126 | wordArrayFromWord2# :: Word# -> Word# -> WordArray#
|
| 126 | 127 | wordArrayFromWord2# h l =
|
| 127 | - withNewWordArray# 2# \mwa s ->
|
|
| 128 | - case mwaWrite# mwa 0# l s of
|
|
| 129 | - s -> mwaWrite# mwa 1# h s
|
|
| 128 | + withNewWordArray# 2# \mwa -> Ubx.do
|
|
| 129 | + mwaWrite# mwa 0# l
|
|
| 130 | + mwaWrite# mwa 1# h
|
|
| 130 | 131 | |
| 131 | 132 | -- | Create a WordArray# from one Word#
|
| 132 | 133 | wordArrayFromWord# :: Word# -> WordArray#
|
| ... | ... | @@ -143,8 +144,9 @@ wordArraySize# ba = bytesToWords# (sizeofByteArray# ba) |
| 143 | 144 | |
| 144 | 145 | -- | Get size in Words
|
| 145 | 146 | mwaSize# :: MutableWordArray# s-> State# s -> (# State# s, Int# #)
|
| 146 | -mwaSize# mba s = case getSizeofMutableByteArray# mba s of
|
|
| 147 | - (# s2, sz #) -> (# s2, bytesToWords# sz #)
|
|
| 147 | +mwaSize# mba = Ubx.do
|
|
| 148 | + sz <- getSizeofMutableByteArray# mba
|
|
| 149 | + Ubx.pure (bytesToWords# sz)
|
|
| 148 | 150 | |
| 149 | 151 | -- | Get the last Word (must be non empty!)
|
| 150 | 152 | wordArrayLast# :: WordArray# -> Word#
|
| ... | ... | @@ -163,10 +165,10 @@ mwaArrayCopy# dst dstIdx src srcIdx n s |
| 163 | 165 | |
| 164 | 166 | -- | Shrink last words of a WordArray
|
| 165 | 167 | mwaShrink# :: MutableByteArray# s -> Int# -> State# s -> State# s
|
| 166 | -mwaShrink# _mwa 0# s = s
|
|
| 167 | -mwaShrink# mwa i s =
|
|
| 168 | - case mwaSize# mwa s of
|
|
| 169 | - (# s, n #) -> shrinkMutableByteArray# mwa (wordsToBytes# (n -# i)) s
|
|
| 168 | +mwaShrink# _mwa 0# = Ubx.nop
|
|
| 169 | +mwaShrink# mwa i = Ubx.do
|
|
| 170 | + n <- mwaSize# mwa
|
|
| 171 | + shrinkMutableByteArray# mwa (wordsToBytes# (n -# i))
|
|
| 170 | 172 | |
| 171 | 173 | -- | Set size
|
| 172 | 174 | mwaSetSize# :: MutableByteArray# s -> Int# -> State# s -> State# s
|
| ... | ... | @@ -175,31 +177,35 @@ mwaSetSize# mwa n s = shrinkMutableByteArray# mwa (wordsToBytes# n) s |
| 175 | 177 | -- | Copy the WordArray into the MWA and shrink the size of MWA to the one of
|
| 176 | 178 | -- the WordArray
|
| 177 | 179 | mwaInitCopyShrink# :: MutableByteArray# s -> WordArray# -> State# s -> State# s
|
| 178 | -mwaInitCopyShrink# mwa wa s =
|
|
| 179 | - case mwaArrayCopy# mwa 0# wa 0# (wordArraySize# wa) s of
|
|
| 180 | - s -> mwaSetSize# mwa (wordArraySize# wa) s
|
|
| 180 | +mwaInitCopyShrink# mwa wa = Ubx.do
|
|
| 181 | + mwaArrayCopy# mwa 0# wa 0# (wordArraySize# wa)
|
|
| 182 | + mwaSetSize# mwa (wordArraySize# wa)
|
|
| 181 | 183 | |
| 182 | 184 | -- | Trim ending zeroes
|
| 183 | 185 | mwaTrimZeroes# :: MutableByteArray# s -> State# s -> State# s
|
| 184 | -mwaTrimZeroes# mwa s1 =
|
|
| 185 | - case mwaClz mwa s1 of
|
|
| 186 | - (# s2, 0# #) -> s2
|
|
| 187 | - (# s2, c #) -> mwaShrink# mwa c s2
|
|
| 186 | +mwaTrimZeroes# mwa = Ubx.do
|
|
| 187 | + c <- mwaClz mwa
|
|
| 188 | + case c of
|
|
| 189 | + 0# -> Ubx.nop
|
|
| 190 | + _ -> mwaShrink# mwa c
|
|
| 188 | 191 | |
| 189 | 192 | -- | Count leading zero Words
|
| 190 | 193 | mwaClz :: MutableWordArray# s -> State# s -> (# State# s, Int# #)
|
| 191 | -mwaClz mwa s1 = case mwaSize# mwa s1 of
|
|
| 192 | - (# s2,sz #) -> mwaClzAt mwa (sz -# 1#) s2
|
|
| 194 | +mwaClz mwa = Ubx.do
|
|
| 195 | + sz <- mwaSize# mwa
|
|
| 196 | + mwaClzAt mwa (sz -# 1#)
|
|
| 193 | 197 | |
| 194 | 198 | -- | Count leading zero Words starting at given position
|
| 195 | 199 | mwaClzAt :: MutableWordArray# s -> Int# -> State# s -> (# State# s, Int# #)
|
| 196 | 200 | mwaClzAt mwa = go 0#
|
| 197 | 201 | where
|
| 198 | - go c i s
|
|
| 199 | - | isTrue# (i <# 0#) = (# s, c #)
|
|
| 200 | - | True = case readWordArray# mwa i s of
|
|
| 201 | - (# s', 0## #) -> go (c +# 1#) (i -# 1#) s'
|
|
| 202 | - (# s', _ #) -> (# s', c #)
|
|
| 202 | + go c i
|
|
| 203 | + | isTrue# (i <# 0#) = Ubx.pure c
|
|
| 204 | + | True = Ubx.do
|
|
| 205 | + w <- readWordArray# mwa i
|
|
| 206 | + case w of
|
|
| 207 | + 0## -> go (c +# 1#) (i -# 1#)
|
|
| 208 | + _ -> Ubx.pure c
|
|
| 203 | 209 | |
| 204 | 210 | -- | Count leading zero Words starting at given position
|
| 205 | 211 | waClzAt :: WordArray# -> Int# -> Int#
|
| ... | ... | @@ -258,15 +264,16 @@ mwaInitArrayPlusWord :: MutableWordArray# s -> WordArray# -> Word# -> State# s - |
| 258 | 264 | mwaInitArrayPlusWord mwa wa = go 0#
|
| 259 | 265 | where
|
| 260 | 266 | sz = wordArraySize# wa
|
| 261 | - go i carry s
|
|
| 262 | - | isTrue# (i ># sz) = s
|
|
| 263 | - | isTrue# (i ==# sz) = mwaWriteOrShrink mwa carry i s
|
|
| 264 | - | 0## <- carry = -- copy higher remaining words and shrink the mwa
|
|
| 265 | - case mwaArrayCopy# mwa i wa i (sz -# i) s of
|
|
| 266 | - s2 -> mwaShrink# mwa 1# s2
|
|
| 267 | - | True = let !(# l,c #) = addWordC# (indexWordArray# wa i) carry
|
|
| 268 | - in case mwaWrite# mwa i l s of
|
|
| 269 | - s2 -> go (i +# 1#) (int2Word# c) s2
|
|
| 267 | + go i carry
|
|
| 268 | + | isTrue# (i ># sz) = Ubx.nop
|
|
| 269 | + | isTrue# (i ==# sz) = mwaWriteOrShrink mwa carry i
|
|
| 270 | + | 0## <- carry = Ubx.do -- copy higher remaining words and shrink the mwa
|
|
| 271 | + mwaArrayCopy# mwa i wa i (sz -# i)
|
|
| 272 | + mwaShrink# mwa 1#
|
|
| 273 | + | True = Ubx.do
|
|
| 274 | + let !(# l, c #) = addWordC# (indexWordArray# wa i) carry
|
|
| 275 | + mwaWrite# mwa i l
|
|
| 276 | + go (i +# 1#) (int2Word# c)
|
|
| 270 | 277 | |
| 271 | 278 | -- | Write the most-significant Word:
|
| 272 | 279 | -- * if it is 0: shrink the array of 1 Word
|
| ... | ... | @@ -277,23 +284,24 @@ mwaWriteOrShrink mwa w i s = mwaWrite# mwa i w s |
| 277 | 284 | |
| 278 | 285 | -- | Compute the index of the most-significant Word and write it.
|
| 279 | 286 | mwaWriteMostSignificant :: MutableWordArray# s -> Word# -> State# s -> State# s
|
| 280 | -mwaWriteMostSignificant mwa w s =
|
|
| 281 | - case mwaSize# mwa s of
|
|
| 282 | - (# s', sz #) -> mwaWriteOrShrink mwa w (sz -# 1#) s'
|
|
| 287 | +mwaWriteMostSignificant mwa w = Ubx.do
|
|
| 288 | + sz <- mwaSize# mwa
|
|
| 289 | + mwaWriteOrShrink mwa w (sz -# 1#)
|
|
| 283 | 290 | |
| 284 | 291 | -- | MutableWordArray <- zipWith op wa1 wa2
|
| 285 | 292 | --
|
| 286 | 293 | -- Required output: Size(MutableWordArray) = min Size(wa1) Size(wa2)
|
| 287 | 294 | mwaInitArrayBinOp :: MutableWordArray# s -> WordArray# -> WordArray# -> (Word# -> Word# -> Word#) -> State# s -> State#s
|
| 288 | -mwaInitArrayBinOp mwa wa wb op s = go 0# s
|
|
| 295 | +mwaInitArrayBinOp mwa wa wb op = go 0#
|
|
| 289 | 296 | where
|
| 290 | 297 | !sz = minI# (wordArraySize# wa) (wordArraySize# wb)
|
| 291 | - go i s'
|
|
| 292 | - | isTrue# (i ==# sz) = s'
|
|
| 298 | + go i
|
|
| 299 | + | isTrue# (i ==# sz) = Ubx.nop
|
|
| 293 | 300 | | True =
|
| 294 | 301 | case indexWordArray# wa i `op` indexWordArray# wb i of
|
| 295 | - v -> case mwaWrite# mwa i v s' of
|
|
| 296 | - s'' -> go (i +# 1#) s''
|
|
| 302 | + v -> Ubx.do
|
|
| 303 | + mwaWrite# mwa i v
|
|
| 304 | + go (i +# 1#)
|
|
| 297 | 305 | |
| 298 | 306 | -- | Write an element of the MutableWordArray
|
| 299 | 307 | mwaWrite# :: MutableWordArray# s -> Int# -> Word# -> State# s -> State# s
|
| ... | ... | @@ -301,17 +309,19 @@ mwaWrite# = writeWordArray# |
| 301 | 309 | |
| 302 | 310 | -- | Fill some part of a MutableWordArray with the given Word#
|
| 303 | 311 | mwaFill# :: MutableWordArray# s -> Word# -> Word# -> Word# -> State# s -> State# s
|
| 304 | -mwaFill# _ _ _ 0## s = s
|
|
| 305 | -mwaFill# mwa v off n s = case mwaWrite# mwa (word2Int# off) v s of
|
|
| 306 | - s' -> mwaFill# mwa v (off `plusWord#` 1##) (n `minusWord#` 1##) s'
|
|
| 312 | +mwaFill# _ _ _ 0## = Ubx.nop
|
|
| 313 | +mwaFill# mwa v off n = Ubx.do
|
|
| 314 | + mwaWrite# mwa (word2Int# off) v
|
|
| 315 | + mwaFill# mwa v (off `plusWord#` 1##) (n `minusWord#` 1##)
|
|
| 307 | 316 | |
| 308 | 317 | -- | Add Word# inplace (a the specified offset) in the mwa with carry propagation.
|
| 309 | 318 | mwaAddInplaceWord# :: MutableWordArray# d -> Int# -> Word# -> State# d -> State# d
|
| 310 | -mwaAddInplaceWord# _ _ 0## s = s
|
|
| 311 | -mwaAddInplaceWord# mwa i y s = case readWordArray# mwa i s of
|
|
| 312 | - (# s1, x #) -> let !(# h,l #) = plusWord2# x y
|
|
| 313 | - in case mwaWrite# mwa i l s1 of
|
|
| 314 | - s2 -> mwaAddInplaceWord# mwa (i +# 1#) h s2
|
|
| 319 | +mwaAddInplaceWord# _ _ 0## = Ubx.nop
|
|
| 320 | +mwaAddInplaceWord# mwa i y = Ubx.do
|
|
| 321 | + x <- readWordArray# mwa i
|
|
| 322 | + let !(# h, l #) = plusWord2# x y
|
|
| 323 | + mwaWrite# mwa i l
|
|
| 324 | + mwaAddInplaceWord# mwa (i +# 1#) h
|
|
| 315 | 325 | |
| 316 | 326 | -- | Sub Word# inplace (at the specified offset) in the mwa with carry
|
| 317 | 327 | -- propagation.
|
| ... | ... | @@ -323,43 +333,44 @@ mwaSubInplaceWord# |
| 323 | 333 | -> Word#
|
| 324 | 334 | -> State# d
|
| 325 | 335 | -> (# State# d, Bool# #)
|
| 326 | -mwaSubInplaceWord# mwa ii iw s1 = case mwaSize# mwa s1 of
|
|
| 327 | - (# is, sz #) ->
|
|
| 328 | - let
|
|
| 329 | - go _ 0## s = (# s, 1# #) -- no underflow
|
|
| 330 | - go i y s
|
|
| 331 | - | isTrue# (i >=# sz) = (# s, 0# #) -- underflow
|
|
| 332 | - | True = case readWordArray# mwa i s of
|
|
| 333 | - (# s1, x #) -> let !(# l,h #) = subWordC# x y
|
|
| 334 | - in case mwaWrite# mwa i l s1 of
|
|
| 335 | - s2 -> go (i +# 1#) (int2Word# h) s2
|
|
| 336 | - in go ii iw is
|
|
| 336 | +mwaSubInplaceWord# mwa ii iw = Ubx.do
|
|
| 337 | + sz <- mwaSize# mwa
|
|
| 338 | + let
|
|
| 339 | + go _ 0## = Ubx.pure 1# -- no underflow
|
|
| 340 | + go i y
|
|
| 341 | + | isTrue# (i >=# sz) = Ubx.pure 0# -- underflow
|
|
| 342 | + | True = Ubx.do
|
|
| 343 | + x <- readWordArray# mwa i
|
|
| 344 | + let !(# l, h #) = subWordC# x y
|
|
| 345 | + mwaWrite# mwa i l
|
|
| 346 | + go (i +# 1#) (int2Word# h)
|
|
| 347 | + go ii iw
|
|
| 337 | 348 | |
| 338 | 349 | |
| 339 | 350 | -- | Trim `a` of `k` less significant limbs and then compare the result with `b`
|
| 340 | 351 | --
|
| 341 | 352 | -- "mwa" doesn't need to be trimmed
|
| 342 | 353 | mwaTrimCompare :: Int# -> MutableWordArray# s -> WordArray# -> State# s -> (# State# s, Ordering #)
|
| 343 | -mwaTrimCompare k mwa wb s1
|
|
| 344 | - | (# s, szA #) <- mwaSize# mwa s1
|
|
| 345 | - , szB <- wordArraySize# wb
|
|
| 346 | - =
|
|
| 347 | - let
|
|
| 348 | - go i s
|
|
| 349 | - | isTrue# (i <# 0#) = (# s, EQ #)
|
|
| 350 | - | True = case readWordArray# mwa (i +# k) s of
|
|
| 351 | - (# s2, ai #) ->
|
|
| 352 | - let bi = if isTrue# (i >=# szB)
|
|
| 353 | - then 0##
|
|
| 354 | - else indexWordArray# wb i
|
|
| 355 | - in if | isTrue# (ai `gtWord#` bi) -> (# s2, GT #)
|
|
| 356 | - | isTrue# (bi `gtWord#` ai) -> (# s2, LT #)
|
|
| 357 | - | True -> go (i -# 1#) s2
|
|
| 354 | +mwaTrimCompare k mwa wb = Ubx.do
|
|
| 355 | + szA <- mwaSize# mwa
|
|
| 356 | + let
|
|
| 357 | + szB = wordArraySize# wb
|
|
| 358 | + |
|
| 359 | + go i
|
|
| 360 | + | isTrue# (i <# 0#) = Ubx.pure EQ
|
|
| 361 | + | True = Ubx.do
|
|
| 362 | + ai <- readWordArray# mwa (i +# k)
|
|
| 363 | + let bi = if isTrue# (i >=# szB)
|
|
| 364 | + then 0##
|
|
| 365 | + else indexWordArray# wb i
|
|
| 366 | + if | isTrue# (ai `gtWord#` bi) -> Ubx.pure GT
|
|
| 367 | + | isTrue# (bi `gtWord#` ai) -> Ubx.pure LT
|
|
| 368 | + | True -> go (i -# 1#)
|
|
| 358 | 369 | |
| 359 | 370 | szTrimA = szA -# k
|
| 360 | 371 | |
| 361 | - in if | isTrue# (szTrimA <# szB) -> (# s, LT #)
|
|
| 362 | - | True -> go (szA -# k -# 1#) s
|
|
| 372 | + if | isTrue# (szTrimA <# szB) -> Ubx.pure LT
|
|
| 373 | + | True -> go (szA -# k -# 1#)
|
|
| 363 | 374 | |
| 364 | 375 | |
| 365 | 376 | -- | Sub array inplace (at the specified offset) in the mwa with carry propagation.
|
| ... | ... | @@ -370,12 +381,13 @@ mwaTrimCompare k mwa wb s1 |
| 370 | 381 | mwaSubInplaceArray :: MutableWordArray# d -> Int# -> WordArray# -> State# d -> (# State# d, Bool# #)
|
| 371 | 382 | mwaSubInplaceArray mwa off wb = go (wordArraySize# wb -# 1#)
|
| 372 | 383 | where
|
| 373 | - go i s
|
|
| 374 | - | isTrue# (i <# 0#) = (# s, 1# #) -- no underflow
|
|
| 375 | - | True
|
|
| 376 | - = case mwaSubInplaceWord# mwa (off +# i) (indexWordArray# wb i) s of
|
|
| 377 | - (# s2, 1# #) -> go (i -# 1#) s2
|
|
| 378 | - (# s2, _ #) -> (# s2, 0# #) -- underflow
|
|
| 384 | + go i
|
|
| 385 | + | isTrue# (i <# 0#) = Ubx.pure 1# -- no underflow
|
|
| 386 | + | True = Ubx.do
|
|
| 387 | + b <- mwaSubInplaceWord# mwa (off +# i) (indexWordArray# wb i)
|
|
| 388 | + case b of
|
|
| 389 | + 1# -> go (i -# 1#)
|
|
| 390 | + _ -> Ubx.pure 0# -- underflow
|
|
| 379 | 391 | |
| 380 | 392 | -- | Add array inplace (a the specified offset) in the mwa with carry propagation.
|
| 381 | 393 | --
|
| ... | ... | @@ -384,13 +396,13 @@ mwaAddInplaceArray :: MutableWordArray# d -> Int# -> WordArray# -> State# d -> S |
| 384 | 396 | mwaAddInplaceArray mwa off wb = go 0# 0##
|
| 385 | 397 | where
|
| 386 | 398 | !maxi = wordArraySize# wb
|
| 387 | - go i c s
|
|
| 388 | - | isTrue# (i ==# maxi) = mwaAddInplaceWord# mwa (i +# off) c s
|
|
| 389 | - | True
|
|
| 390 | - = case readWordArray# mwa (i +# off) s of
|
|
| 391 | - (# s, v #) -> case plusWord3# v (indexWordArray# wb i) c of
|
|
| 392 | - (# c', v' #) -> case writeWordArray# mwa (i +# off) v' s of
|
|
| 393 | - s -> go (i +# 1#) c' s
|
|
| 399 | + go i c
|
|
| 400 | + | isTrue# (i ==# maxi) = mwaAddInplaceWord# mwa (i +# off) c
|
|
| 401 | + | True = Ubx.do
|
|
| 402 | + v <- readWordArray# mwa (i +# off)
|
|
| 403 | + let !(# c', v' #) = plusWord3# v (indexWordArray# wb i) c
|
|
| 404 | + writeWordArray# mwa (i +# off) v'
|
|
| 405 | + go (i +# 1#) c'
|
|
| 394 | 406 | |
| 395 | 407 | -- | Sub array inplace (at the specified offset) in the mwa with carry propagation.
|
| 396 | 408 | --
|
| ... | ... | @@ -398,35 +410,36 @@ mwaAddInplaceArray mwa off wb = go 0# 0## |
| 398 | 410 | --
|
| 399 | 411 | -- Return False# on underflow.
|
| 400 | 412 | mwaSubInplaceMutableArray :: MutableWordArray# d -> Int# -> MutableWordArray# d -> State# d -> (# State# d, Bool# #)
|
| 401 | -mwaSubInplaceMutableArray mwa off mwb s0 =
|
|
| 402 | - case mwaSize# mwb s0 of
|
|
| 403 | - (# s1, szB #) -> go (szB -# 1#) s1
|
|
| 413 | +mwaSubInplaceMutableArray mwa off mwb = Ubx.do
|
|
| 414 | + szB <- mwaSize# mwb
|
|
| 415 | + go (szB -# 1#)
|
|
| 404 | 416 | where
|
| 405 | - go i s
|
|
| 406 | - | isTrue# (i <# 0#) = (# s, 1# #) -- no underflow
|
|
| 407 | - | True
|
|
| 408 | - = case readWordArray# mwb i s of
|
|
| 409 | - (# s1, bi #) -> case mwaSubInplaceWord# mwa (off +# i) bi s1 of
|
|
| 410 | - (# s2, 1# #) -> go (i -# 1#) s2
|
|
| 411 | - (# s2, _ #) -> (# s2, 0# #) -- underflow
|
|
| 417 | + go i
|
|
| 418 | + | isTrue# (i <# 0#) = Ubx.pure 1# -- no underflow
|
|
| 419 | + | True = Ubx.do
|
|
| 420 | + bi <- readWordArray# mwb i
|
|
| 421 | + b <- mwaSubInplaceWord# mwa (off +# i) bi
|
|
| 422 | + case b of
|
|
| 423 | + 1# -> go (i -# 1#)
|
|
| 424 | + _ -> Ubx.pure 0# -- underflow
|
|
| 412 | 425 | |
| 413 | 426 | -- | Sub an array inplace and then trim zeroes
|
| 414 | 427 | --
|
| 415 | 428 | -- Don't check overflow. The caller must ensure that a>=b
|
| 416 | 429 | mwaSubInplaceArrayTrim :: MutableWordArray# d -> Int# -> WordArray# -> State# d -> State# d
|
| 417 | -mwaSubInplaceArrayTrim mwa off wb s =
|
|
| 418 | - case mwaSubInplaceArray mwa off wb s of
|
|
| 419 | - (# s', _ #) -> mwaTrimZeroes# mwa s'
|
|
| 430 | +mwaSubInplaceArrayTrim mwa off wb = Ubx.do
|
|
| 431 | + _ <- mwaSubInplaceArray mwa off wb
|
|
| 432 | + mwaTrimZeroes# mwa
|
|
| 420 | 433 | |
| 421 | 434 | |
| 422 | 435 | -- | Read an indexed Word in the MutableWordArray. If the index is out-of-bound,
|
| 423 | 436 | -- return zero.
|
| 424 | 437 | mwaReadOrZero :: MutableWordArray# s -> Int# -> State# s -> (# State# s, Word# #)
|
| 425 | -mwaReadOrZero mwa i s = case mwaSize# mwa s of
|
|
| 426 | - (# s2, sz #)
|
|
| 427 | - | isTrue# (i >=# sz) -> (# s2, 0## #)
|
|
| 428 | - | isTrue# (i <# 0#) -> (# s2, 0## #)
|
|
| 429 | - | True -> readWordArray# mwa i s2
|
|
| 438 | +mwaReadOrZero mwa i = Ubx.do
|
|
| 439 | + sz <- mwaSize# mwa
|
|
| 440 | + if | isTrue# (i >=# sz) -> Ubx.pure 0##
|
|
| 441 | + | isTrue# (i <# 0#) -> Ubx.pure 0##
|
|
| 442 | + | True -> readWordArray# mwa i
|
|
| 430 | 443 | |
| 431 | 444 | mwaRead# :: MutableWordArray# s -> Int# -> State# s -> (# State# s, Word# #)
|
| 432 | 445 | mwaRead# = readWordArray# |
| 1 | +{-# LANGUAGE CPP #-}
|
|
| 2 | +{-# LANGUAGE NoImplicitPrelude #-}
|
|
| 3 | +{-# LANGUAGE MagicHash #-}
|
|
| 4 | +{-# LANGUAGE UnboxedTuples #-}
|
|
| 5 | +{-# LANGUAGE DataKinds #-}
|
|
| 6 | +{-# LANGUAGE PolyKinds #-}
|
|
| 7 | +{-# LANGUAGE KindSignatures #-}
|
|
| 8 | +{-# LANGUAGE RankNTypes #-}
|
|
| 9 | +{-# LANGUAGE FlexibleInstances #-}
|
|
| 10 | +{-# LANGUAGE MultiParamTypeClasses #-}
|
|
| 11 | +{-# LANGUAGE TypeFamilies #-}
|
|
| 12 | +{-# LANGUAGE TypeOperators #-}
|
|
| 13 | +{-# LANGUAGE StandaloneKindSignatures #-}
|
|
| 14 | + |
|
| 15 | +-- |
|
|
| 16 | +-- Module : GHC.Internal.Unboxed.Do
|
|
| 17 | +-- Copyright : (c) The University of Glasgow 2026
|
|
| 18 | +-- License : see libraries/ghc-internal/LICENSE
|
|
| 19 | +--
|
|
| 20 | +-- Maintainer : ghc-devs@haskell.org
|
|
| 21 | +-- Stability : internal
|
|
| 22 | +-- Portability : non-portable (GHC Extensions)
|
|
| 23 | +--
|
|
| 24 | +-- Do-notation for @State#@-passing code that binds unboxed values.
|
|
| 25 | +--
|
|
| 26 | +-- Import this module qualified and use it with @QualifiedDo@:
|
|
| 27 | +--
|
|
| 28 | +-- > {-# LANGUAGE QualifiedDo, MagicHash, UnboxedTuples #-}
|
|
| 29 | +-- > import qualified GHC.Internal.Unboxed.Do as Ubx
|
|
| 30 | +-- >
|
|
| 31 | +-- > sumTwo :: MutableByteArray# s -> State# s -> (# State# s, Word# #)
|
|
| 32 | +-- > sumTwo mba = Ubx.do
|
|
| 33 | +-- > a <- readWordArray# mba 0# -- a :: Word#
|
|
| 34 | +-- > b <- readWordArray# mba 1#
|
|
| 35 | +-- > writeWordArray# mba 2# (plusWord# a b) -- a unit action is a statement too
|
|
| 36 | +-- > Ubx.pure (plusWord# a b)
|
|
| 37 | +--
|
|
| 38 | +-- A block has type @State# s -> o@ for any @o@: it can end with a unit
|
|
| 39 | +-- action (@State# s -> State# s@) or with an action that returns a value
|
|
| 40 | +-- (@State# s -> (# State# s, a #)@). Bound variables can have any
|
|
| 41 | +-- representation for which there is a 'Bind#' instance.
|
|
| 42 | +--
|
|
| 43 | +-- See #27944.
|
|
| 44 | + |
|
| 45 | +module GHC.Internal.Unboxed.Do
|
|
| 46 | + ( ST#
|
|
| 47 | + , Bind# (..)
|
|
| 48 | + , Then# (..)
|
|
| 49 | + , nop
|
|
| 50 | + , void
|
|
| 51 | + )
|
|
| 52 | +where
|
|
| 53 | + |
|
| 54 | +import GHC.Internal.Prim
|
|
| 55 | +import GHC.Internal.Types
|
|
| 56 | + |
|
| 57 | +-- | A @State#@-passing action that returns a value of any representation.
|
|
| 58 | +type ST# :: forall (r :: RuntimeRep). Type -> TYPE r -> Type
|
|
| 59 | +type ST# s a = State# s -> (# State# s, a #)
|
|
| 60 | + |
|
| 61 | +-- | Operations of 'ST#' actions whose result has representation @r@.
|
|
| 62 | +-- One instance per representation, so that the bound variable has a fixed one.
|
|
| 63 | +--
|
|
| 64 | +-- The continuation result @o@ is arbitrary, so that a block can end with a
|
|
| 65 | +-- unit action (@o = State# s@) or with an 'ST#' action.
|
|
| 66 | +class Bind# (r :: RuntimeRep) where
|
|
| 67 | + (>>=) :: forall s (a :: TYPE r) {r'} (o :: TYPE r').
|
|
| 68 | + ST# s a -> (a -> State# s -> o) -> State# s -> o
|
|
| 69 | + |
|
| 70 | + -- | Sequence two actions, discard the result of the first one
|
|
| 71 | + then# :: forall s (a :: TYPE r) {r'} (o :: TYPE r').
|
|
| 72 | + ST# s a -> (State# s -> o) -> State# s -> o
|
|
| 73 | + |
|
| 74 | + pure :: forall s (a :: TYPE r). a -> ST# s a
|
|
| 75 | + |
|
| 76 | +-- | Sequencing. The discarded action @m@ is a unit action
|
|
| 77 | +-- (@State# s -> State# s@) or an 'ST#' action.
|
|
| 78 | +class Then# (m :: Type) (n :: Type) where
|
|
| 79 | + (>>) :: m -> n -> n
|
|
| 80 | + |
|
| 81 | +infixl 1 >>=, >>
|
|
| 82 | + |
|
| 83 | +-- The `s ~ s'` contexts give instance improvement: the state type of the
|
|
| 84 | +-- continuation is inferred from the discarded action.
|
|
| 85 | +instance forall s s' r' (o :: TYPE r'). s ~ s'
|
|
| 86 | + => Then# (State# s -> State# s) (State# s' -> o) where
|
|
| 87 | + {-# INLINE (>>) #-}
|
|
| 88 | + m >> k = \s -> case m s of s2 -> k s2
|
|
| 89 | + |
|
| 90 | +instance forall s s' r (a :: TYPE r) r' (o :: TYPE r'). (s ~ s', Bind# r)
|
|
| 91 | + => Then# (State# s -> (# State# s, a #)) (State# s' -> o) where
|
|
| 92 | + {-# INLINE (>>) #-}
|
|
| 93 | + m >> k = m `then#` k
|
|
| 94 | + |
|
| 95 | +-- | Unit action that does nothing.
|
|
| 96 | +nop :: State# s -> State# s
|
|
| 97 | +nop s = s
|
|
| 98 | +{-# INLINE nop #-}
|
|
| 99 | + |
|
| 100 | +-- | Run an action and discard its result.
|
|
| 101 | +void :: forall s r (a :: TYPE r). Bind# r => ST# s a -> State# s -> State# s
|
|
| 102 | +void m = m `then#` nop
|
|
| 103 | +{-# INLINE void #-}
|
|
| 104 | + |
|
| 105 | +#define BIND_INSTANCE(rep) \
|
|
| 106 | +instance Bind# (rep) where { \
|
|
| 107 | + {-# INLINE (>>=) #-} ; \
|
|
| 108 | + m >>= k = \s -> case m s of { (# s2, a #) -> k a s2 } ; \
|
|
| 109 | + {-# INLINE then# #-} ; \
|
|
| 110 | + m `then#` k = \s -> case m s of { (# s2, _ #) -> k s2 } ; \
|
|
| 111 | + {-# INLINE pure #-} ; \
|
|
| 112 | + pure a = \s -> (# s, a #) }
|
|
| 113 | + |
|
| 114 | +-- Traditional CPP: no commas in macro arguments, hence the type synonyms.
|
|
| 115 | +type TupleRep_WW = TupleRep '[WordRep, WordRep] -- (# Word#, Word# #)
|
|
| 116 | +type TupleRep_WI = TupleRep '[WordRep, IntRep] -- (# Word#, Int# #)
|
|
| 117 | +type TupleRep_IW = TupleRep '[IntRep, WordRep] -- (# Int#, Word# #)
|
|
| 118 | +type TupleRep_II = TupleRep '[IntRep, IntRep] -- (# Int#, Int# #)
|
|
| 119 | +type TupleRep_UU = TupleRep '[UnliftedRep, UnliftedRep] -- (# ByteArray#, ByteArray# #)
|
|
| 120 | +type TupleRep_UW = TupleRep '[UnliftedRep, WordRep] -- (# ByteArray#, Word# #)
|
|
| 121 | +type TupleRep_WU = TupleRep '[WordRep, UnliftedRep] -- (# Word#, ByteArray# #)
|
|
| 122 | +type TupleRep_LL = TupleRep '[LiftedRep, LiftedRep] -- (# a, b #)
|
|
| 123 | +type TupleRep_LLL = TupleRep '[LiftedRep, LiftedRep, LiftedRep] -- (# a, b, c #)
|
|
| 124 | +type SumRep_0U = SumRep '[ZeroBitRep, UnliftedRep] -- (# (# #) | ByteArray# #)
|
|
| 125 | + |
|
| 126 | +-- Only the representations used in ghc-internal. Extend as needed (#27944).
|
|
| 127 | +BIND_INSTANCE(IntRep)
|
|
| 128 | +BIND_INSTANCE(Int8Rep)
|
|
| 129 | +BIND_INSTANCE(Int16Rep)
|
|
| 130 | +BIND_INSTANCE(Int32Rep)
|
|
| 131 | +BIND_INSTANCE(Int64Rep)
|
|
| 132 | +BIND_INSTANCE(WordRep)
|
|
| 133 | +BIND_INSTANCE(Word8Rep)
|
|
| 134 | +BIND_INSTANCE(Word16Rep)
|
|
| 135 | +BIND_INSTANCE(Word32Rep)
|
|
| 136 | +BIND_INSTANCE(Word64Rep)
|
|
| 137 | +BIND_INSTANCE(AddrRep)
|
|
| 138 | +BIND_INSTANCE(FloatRep)
|
|
| 139 | +BIND_INSTANCE(DoubleRep)
|
|
| 140 | +BIND_INSTANCE(LiftedRep)
|
|
| 141 | +BIND_INSTANCE(UnliftedRep)
|
|
| 142 | +BIND_INSTANCE(ZeroBitRep)
|
|
| 143 | +BIND_INSTANCE(TupleRep_WW)
|
|
| 144 | +BIND_INSTANCE(TupleRep_WI)
|
|
| 145 | +BIND_INSTANCE(TupleRep_IW)
|
|
| 146 | +BIND_INSTANCE(TupleRep_II)
|
|
| 147 | +BIND_INSTANCE(TupleRep_UU)
|
|
| 148 | +BIND_INSTANCE(TupleRep_UW)
|
|
| 149 | +BIND_INSTANCE(TupleRep_WU)
|
|
| 150 | +BIND_INSTANCE(TupleRep_LL)
|
|
| 151 | +BIND_INSTANCE(TupleRep_LLL)
|
|
| 152 | +BIND_INSTANCE(SumRep_0U) |
| ... | ... | @@ -2,3 +2,4 @@ setTestOpts(only_ways(['normal'])); |
| 2 | 2 | |
| 3 | 3 | test('qdocompile001', normal, compile, ['-v0 -ddump-rn -dsuppress-uniques'])
|
| 4 | 4 | test('qdocompile002', normal, compile, ['-v0'])
|
| 5 | +test('qdocompile003', normal, compile, ['-O -ddump-simpl -dsuppress-all -dsuppress-uniques -dno-typeable-binds']) |
| 1 | +{-# LANGUAGE QualifiedDo #-}
|
|
| 2 | +{-# LANGUAGE MagicHash #-}
|
|
| 3 | +{-# LANGUAGE UnboxedTuples #-}
|
|
| 4 | + |
|
| 5 | +-- With -O, the block must compile to nested case expressions.
|
|
| 6 | +module QDoCompile003 where
|
|
| 7 | + |
|
| 8 | +import GHC.Exts
|
|
| 9 | +import qualified GHC.Internal.Unboxed.Do as Ubx
|
|
| 10 | + |
|
| 11 | +sumTwo :: MutableByteArray# s -> State# s -> (# State# s, Word# #)
|
|
| 12 | +sumTwo mba = Ubx.do
|
|
| 13 | + a <- readWordArray# mba 0#
|
|
| 14 | + b <- readWordArray# mba 1#
|
|
| 15 | + writeWordArray# mba 2# (plusWord# a b)
|
|
| 16 | + Ubx.pure (plusWord# a b) |
| 1 | + |
|
| 2 | +==================== Tidy Core ====================
|
|
| 3 | +Result size of Tidy Core
|
|
| 4 | + = {terms: 30, types: 44, coercions: 0, joins: 0/0}
|
|
| 5 | + |
|
| 6 | +sumTwo
|
|
| 7 | + = \ @s mba s1 ->
|
|
| 8 | + case readWordArray# mba 0# s1 of { (# ipv, ipv1 #) ->
|
|
| 9 | + case readWordArray# mba 1# ipv of { (# ipv2, ipv3 #) ->
|
|
| 10 | + case writeWordArray# mba 2# (plusWord# ipv1 ipv3) ipv2 of s2
|
|
| 11 | + { __DEFAULT ->
|
|
| 12 | + (# s2, plusWord# ipv1 ipv3 #)
|
|
| 13 | + }
|
|
| 14 | + }
|
|
| 15 | + }
|
|
| 16 | + |
|
| 17 | + |
|
| 18 | + |
| ... | ... | @@ -5,3 +5,4 @@ test('qdofail002', normal, compile_fail, ['-v0']) |
| 5 | 5 | test('qdofail003', normal, compile_fail, ['-v0'])
|
| 6 | 6 | test('qdofail004', normal, compile_fail, ['-v0'])
|
| 7 | 7 | test('qdofail005', normal, compile_fail, ['-v0'])
|
| 8 | +test('qdofail006', normal, compile_fail, ['-v0']) |
| 1 | +{-# LANGUAGE QualifiedDo #-}
|
|
| 2 | +{-# LANGUAGE MagicHash #-}
|
|
| 3 | +{-# LANGUAGE UnboxedTuples #-}
|
|
| 4 | +{-# LANGUAGE PolyKinds #-}
|
|
| 5 | +{-# LANGUAGE DataKinds #-}
|
|
| 6 | +{-# LANGUAGE RankNTypes #-}
|
|
| 7 | + |
|
| 8 | +-- A bound variable must have a fixed runtime representation.
|
|
| 9 | +module QDoFail006 where
|
|
| 10 | + |
|
| 11 | +import GHC.Exts
|
|
| 12 | +import qualified GHC.Internal.Unboxed.Do as Ubx
|
|
| 13 | +import GHC.Internal.Unboxed.Do (ST#)
|
|
| 14 | + |
|
| 15 | +bad :: forall s r (a :: TYPE r). ST# s a -> ST# s a
|
|
| 16 | +bad m = Ubx.do
|
|
| 17 | + x <- m
|
|
| 18 | + Ubx.pure x |
| 1 | +qdofail006.hs:17:3: error: [GHC-55287]
|
|
| 2 | + • The binder of the lambda expression
|
|
| 3 | + does not have a fixed runtime representation.
|
|
| 4 | + Its type is:
|
|
| 5 | + a :: TYPE r
|
|
| 6 | + • In a stmt of a qualified 'do' block: x <- m
|
|
| 7 | + In the expression:
|
|
| 8 | + Ubx.do x <- m
|
|
| 9 | + Ubx.pure x
|
|
| 10 | + In an equation for ‘bad’:
|
|
| 11 | + bad m
|
|
| 12 | + = Ubx.do x <- m
|
|
| 13 | + Ubx.pure x
|
|
| 14 | + |
| ... | ... | @@ -9,3 +9,4 @@ test('qdorun004', normal, compile_and_run, ['']) |
| 9 | 9 | test('qdorun005', [qextra_files], multimod_compile_and_run, ['qdorun005', ''])
|
| 10 | 10 | test('qdorun006', [req_th,qextra_files], multimod_compile_and_run, ['qdorun006', ''])
|
| 11 | 11 | test('qdorun007', [qextra_files], multimod_compile_and_run, ['qdorun007', ''])
|
| 12 | +test('qdorun008', normal, compile_and_run, ['']) |
| 1 | +{-# LANGUAGE QualifiedDo #-}
|
|
| 2 | +{-# LANGUAGE MagicHash #-}
|
|
| 3 | +{-# LANGUAGE UnboxedTuples #-}
|
|
| 4 | +{-# LANGUAGE BangPatterns #-}
|
|
| 5 | +{-# LANGUAGE MultiWayIf #-}
|
|
| 6 | + |
|
| 7 | +module Main where
|
|
| 8 | + |
|
| 9 | +import GHC.Exts
|
|
| 10 | +import qualified GHC.Internal.Unboxed.Do as Ubx
|
|
| 11 | +import GHC.Internal.Unboxed.Do (ST#)
|
|
| 12 | + |
|
| 13 | +fill :: MutableByteArray# s -> Word# -> State# s -> State# s -- unit result
|
|
| 14 | +fill mba x = Ubx.do
|
|
| 15 | + writeWordArray# mba 0# x -- unit action as a statement
|
|
| 16 | + n <- getSizeofMutableByteArray# mba -- binds an Int#
|
|
| 17 | + writeWordArray# mba 1# (int2Word# n)
|
|
| 18 | + |
|
| 19 | +sumAll :: Int# -> ST# s Word#
|
|
| 20 | +sumAll n = Ubx.do
|
|
| 21 | + mba <- newByteArray# n -- binds an unlifted pointer
|
|
| 22 | + fill mba 40##
|
|
| 23 | + a <- readWordArray# mba 0# -- binds a Word#
|
|
| 24 | + b <- readWordArray# mba 1#
|
|
| 25 | + (# c, ov #) <- Ubx.pure (addWordC# a b) -- binds an unboxed tuple
|
|
| 26 | + let !(# hi, lo #) = timesWord2# a 2##
|
|
| 27 | + Ubx.pure (plusWord# (plusWord# c (int2Word# ov)) (plusWord# hi lo))
|
|
| 28 | + |
|
| 29 | +ordering :: MutableByteArray# s -> ST# s Ordering -- lifted result
|
|
| 30 | +ordering mba = Ubx.do
|
|
| 31 | + a <- readWordArray# mba 0#
|
|
| 32 | + b <- readWordArray# mba 1#
|
|
| 33 | + if | isTrue# (ltWord# a b) -> Ubx.pure LT
|
|
| 34 | + | isTrue# (gtWord# a b) -> Ubx.pure GT
|
|
| 35 | + | True -> Ubx.pure EQ
|
|
| 36 | + |
|
| 37 | +main :: IO ()
|
|
| 38 | +main = do
|
|
| 39 | + print (W# (case runRW# (sumAll 16#) of (# _, w #) -> w))
|
|
| 40 | + print (case runRW# (\s -> case newByteArray# 16# s of
|
|
| 41 | + (# s1, mba #) -> case fill mba 5## s1 of
|
|
| 42 | + s2 -> ordering mba s2) of
|
|
| 43 | + (# _, o #) -> o) |
| 1 | +136
|
|
| 2 | +LT |