libmongocrypt-helper 1.7.4.0.1002 → 1.8.0.0.1001

Sign up to get free protection for your applications and to get access to all the features.
Files changed (396) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/ext/libmongocrypt/libmongocrypt/CHANGELOG.md +9 -7
  4. data/ext/libmongocrypt/libmongocrypt/CMakeLists.txt +17 -23
  5. data/ext/libmongocrypt/libmongocrypt/Earthfile +374 -0
  6. data/ext/libmongocrypt/libmongocrypt/README.md +7 -7
  7. data/ext/libmongocrypt/libmongocrypt/VERSION_CURRENT +1 -1
  8. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt/CryptClient.cs +25 -6
  9. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt/Library.cs +25 -3
  10. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt.Example/MongoDB.Libmongocrypt.Example.csproj +1 -1
  11. data/ext/libmongocrypt/libmongocrypt/bindings/cs/MongoDB.Libmongocrypt.Test/BasicTests.cs +1 -1
  12. data/ext/libmongocrypt/libmongocrypt/bindings/cs/Scripts/build.cake +3 -2
  13. data/ext/libmongocrypt/libmongocrypt/bindings/cs/cs.sln +79 -0
  14. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/build.gradle.kts +2 -2
  15. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/java/com/mongodb/crypt/capi/MongoCryptTest.java +6 -4
  16. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/resources/fle2-find-range-explicit-v2/int32/encrypted-payload.json +26 -0
  17. data/ext/libmongocrypt/libmongocrypt/bindings/node/CHANGELOG.md +24 -0
  18. data/ext/libmongocrypt/libmongocrypt/bindings/node/README.md +134 -5
  19. data/ext/libmongocrypt/libmongocrypt/bindings/node/etc/build-static.sh +3 -6
  20. data/ext/libmongocrypt/libmongocrypt/bindings/node/index.d.ts +203 -77
  21. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/autoEncrypter.js +1 -1
  22. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/clientEncryption.js +165 -43
  23. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/common.js +12 -14
  24. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/errors.js +75 -0
  25. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/index.js +34 -3
  26. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/providers/aws.js +26 -0
  27. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/providers/azure.js +178 -0
  28. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/providers/gcp.js +24 -0
  29. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/providers/index.js +54 -0
  30. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/providers/utils.js +39 -0
  31. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/stateMachine.js +9 -4
  32. data/ext/libmongocrypt/libmongocrypt/bindings/node/package-lock.json +4440 -5189
  33. data/ext/libmongocrypt/libmongocrypt/bindings/node/package.json +27 -8
  34. data/ext/libmongocrypt/libmongocrypt/bindings/node/src/mongocrypt.cc +65 -38
  35. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/autoEncrypter.test.js +4 -7
  36. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/clientEncryption.test.js +434 -42
  37. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/common.test.js +94 -0
  38. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/cryptoCallbacks.test.js +1 -45
  39. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/index.test.js +45 -0
  40. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/mongocryptdManager.test.js +1 -1
  41. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/providers/credentialsProvider.test.js +551 -0
  42. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/release.test.js +10 -3
  43. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/requirements.helper.js +23 -1
  44. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/stateMachine.test.js +1 -1
  45. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/tools/chai-addons.js +8 -0
  46. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/types/index.test-d.ts +63 -0
  47. data/ext/libmongocrypt/libmongocrypt/bindings/python/CHANGELOG.rst +26 -0
  48. data/ext/libmongocrypt/libmongocrypt/bindings/python/README.rst +2 -2
  49. data/ext/libmongocrypt/libmongocrypt/bindings/python/build-manylinux-wheel.sh +1 -1
  50. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/binding.py +128 -238
  51. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/compat.py +0 -27
  52. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/explicit_encrypter.py +1 -1
  53. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/mongocrypt.py +5 -21
  54. data/ext/libmongocrypt/libmongocrypt/bindings/python/pymongocrypt/version.py +2 -2
  55. data/ext/libmongocrypt/libmongocrypt/bindings/python/release.sh +5 -5
  56. data/ext/libmongocrypt/libmongocrypt/bindings/python/setup.py +14 -9
  57. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/__init__.py +1 -10
  58. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/fle2-find-range-explicit-v2/int32/encrypted-payload.json +26 -0
  59. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/test_mongocrypt.py +5 -5
  60. data/ext/libmongocrypt/libmongocrypt/bindings/python/test-requirements.txt +4 -12
  61. data/ext/libmongocrypt/libmongocrypt/cmake/ImportBSON.cmake +8 -6
  62. data/ext/libmongocrypt/libmongocrypt/cmake/IntelDFP.cmake +2 -1
  63. data/ext/libmongocrypt/libmongocrypt/cmake/Platform.cmake +50 -0
  64. data/ext/libmongocrypt/libmongocrypt/cmake/mongocrypt-config.cmake +21 -0
  65. data/ext/libmongocrypt/libmongocrypt/debian/gbp.conf +2 -2
  66. data/ext/libmongocrypt/libmongocrypt/etc/c6-vault.repo +39 -0
  67. data/ext/libmongocrypt/libmongocrypt/etc/fle2_aead_generate_tests.py +15 -24
  68. data/ext/libmongocrypt/libmongocrypt/etc/fle2_crypto.py +66 -54
  69. data/ext/libmongocrypt/libmongocrypt/etc/fle2_generate_tests.py +14 -23
  70. data/ext/libmongocrypt/libmongocrypt/etc/fle2v2_aead_generate_tests.py +32 -0
  71. data/ext/libmongocrypt/libmongocrypt/etc/format-all.sh +12 -0
  72. data/ext/libmongocrypt/libmongocrypt/etc/format.sh +16 -0
  73. data/ext/libmongocrypt/libmongocrypt/etc/install-package.sh +48 -0
  74. data/ext/libmongocrypt/libmongocrypt/etc/mongo-inteldfp-libmongocrypt-pr-625.patch +13 -0
  75. data/ext/libmongocrypt/libmongocrypt/src/crypto/cng.c +381 -436
  76. data/ext/libmongocrypt/libmongocrypt/src/crypto/commoncrypto.c +162 -227
  77. data/ext/libmongocrypt/libmongocrypt/src/crypto/libcrypto.c +180 -248
  78. data/ext/libmongocrypt/libmongocrypt/src/crypto/none.c +33 -55
  79. data/ext/libmongocrypt/libmongocrypt/src/csfle-markup.cpp +175 -205
  80. data/ext/libmongocrypt/libmongocrypt/src/mc-array-private.h +10 -21
  81. data/ext/libmongocrypt/libmongocrypt/src/mc-array.c +44 -56
  82. data/ext/libmongocrypt/libmongocrypt/src/mc-check-conversions-private.h +4 -8
  83. data/ext/libmongocrypt/libmongocrypt/src/mc-dec128.h +351 -463
  84. data/ext/libmongocrypt/libmongocrypt/src/mc-dec128.test.cpp +59 -66
  85. data/ext/libmongocrypt/libmongocrypt/src/mc-efc-private.h +7 -11
  86. data/ext/libmongocrypt/libmongocrypt/src/mc-efc.c +80 -92
  87. data/ext/libmongocrypt/libmongocrypt/src/mc-fle-blob-subtype-private.h +18 -10
  88. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-encryption-placeholder-private.h +59 -70
  89. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-encryption-placeholder.c +384 -439
  90. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-equality-payload-private-v2.h +41 -0
  91. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-equality-payload-private.h +11 -18
  92. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-equality-payload-v2.c +135 -0
  93. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-equality-payload.c +109 -126
  94. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-range-payload-private-v2.h +88 -0
  95. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-range-payload-private.h +24 -27
  96. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-range-payload-v2.c +137 -0
  97. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-find-range-payload.c +106 -125
  98. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-insert-update-payload-private-v2.h +117 -0
  99. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-insert-update-payload-private.h +28 -36
  100. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-insert-update-payload-v2.c +294 -0
  101. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-insert-update-payload.c +237 -278
  102. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-iev-private-v2.h +133 -0
  103. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-iev-private.h +67 -56
  104. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-iev-v2.c +437 -0
  105. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-iev.c +476 -325
  106. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev-common-private.h +69 -0
  107. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev-common.c +182 -0
  108. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev-private.h +23 -31
  109. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev-v2-private.h +85 -0
  110. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev-v2.c +142 -0
  111. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-payload-uev.c +104 -231
  112. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-range-operator-private.h +8 -7
  113. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-rfds-private.h +55 -56
  114. data/ext/libmongocrypt/libmongocrypt/src/mc-fle2-rfds.c +517 -580
  115. data/ext/libmongocrypt/libmongocrypt/src/mc-optional-private.h +36 -66
  116. data/ext/libmongocrypt/libmongocrypt/src/mc-range-edge-generation-private.h +57 -76
  117. data/ext/libmongocrypt/libmongocrypt/src/mc-range-edge-generation.c +158 -189
  118. data/ext/libmongocrypt/libmongocrypt/src/mc-range-encoding-private.h +36 -42
  119. data/ext/libmongocrypt/libmongocrypt/src/mc-range-encoding.c +515 -558
  120. data/ext/libmongocrypt/libmongocrypt/src/mc-range-mincover-generator.template.h +155 -191
  121. data/ext/libmongocrypt/libmongocrypt/src/mc-range-mincover-private.h +43 -53
  122. data/ext/libmongocrypt/libmongocrypt/src/mc-range-mincover.c +170 -240
  123. data/ext/libmongocrypt/libmongocrypt/src/mc-rangeopts-private.h +29 -34
  124. data/ext/libmongocrypt/libmongocrypt/src/mc-rangeopts.c +289 -338
  125. data/ext/libmongocrypt/libmongocrypt/src/mc-reader-private.h +36 -71
  126. data/ext/libmongocrypt/libmongocrypt/src/mc-reader.c +111 -164
  127. data/ext/libmongocrypt/libmongocrypt/src/mc-tokens-private.h +55 -49
  128. data/ext/libmongocrypt/libmongocrypt/src/mc-tokens.c +109 -125
  129. data/ext/libmongocrypt/libmongocrypt/src/mc-writer-private.h +66 -0
  130. data/ext/libmongocrypt/libmongocrypt/src/mc-writer.c +141 -0
  131. data/ext/libmongocrypt/libmongocrypt/src/mlib/check.hpp +37 -55
  132. data/ext/libmongocrypt/libmongocrypt/src/mlib/endian.h +11 -11
  133. data/ext/libmongocrypt/libmongocrypt/src/mlib/error.h +27 -32
  134. data/ext/libmongocrypt/libmongocrypt/src/mlib/int128.h +444 -499
  135. data/ext/libmongocrypt/libmongocrypt/src/mlib/int128.test.cpp +251 -334
  136. data/ext/libmongocrypt/libmongocrypt/src/mlib/macros.h +4 -5
  137. data/ext/libmongocrypt/libmongocrypt/src/mlib/path.h +196 -231
  138. data/ext/libmongocrypt/libmongocrypt/src/mlib/path.test.c +56 -79
  139. data/ext/libmongocrypt/libmongocrypt/src/mlib/str.h +411 -530
  140. data/ext/libmongocrypt/libmongocrypt/src/mlib/str.test.c +116 -131
  141. data/ext/libmongocrypt/libmongocrypt/src/mlib/thread.h +17 -26
  142. data/ext/libmongocrypt/libmongocrypt/src/mlib/user-check.h +2 -2
  143. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-binary-private.h +3 -6
  144. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-binary.c +31 -48
  145. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-buffer-private.h +55 -131
  146. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-buffer.c +444 -565
  147. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-collinfo-private.h +1 -2
  148. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-collinfo.c +28 -45
  149. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-key-private.h +11 -17
  150. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-key.c +103 -132
  151. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-oauth-private.h +8 -14
  152. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-oauth.c +78 -90
  153. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache-private.h +26 -45
  154. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-cache.c +220 -273
  155. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ciphertext-private.h +13 -25
  156. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ciphertext.c +147 -166
  157. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-compat.h +2 -2
  158. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-crypto-private.h +110 -204
  159. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-crypto.c +1137 -1565
  160. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx-datakey.c +425 -472
  161. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx-decrypt.c +817 -694
  162. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx-encrypt.c +2394 -2697
  163. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx-private.h +166 -186
  164. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx-rewrap-many-datakey.c +308 -351
  165. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-ctx.c +921 -1138
  166. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-dll-private.h +16 -24
  167. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-endian-private.h +44 -58
  168. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-endpoint-private.h +21 -25
  169. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-endpoint.c +167 -181
  170. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-kek-private.h +37 -43
  171. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-kek.c +215 -253
  172. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-key-broker-private.h +73 -108
  173. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-key-broker.c +889 -1034
  174. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-key-private.h +27 -44
  175. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-key.c +349 -402
  176. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-kms-ctx-private.h +92 -120
  177. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-kms-ctx.c +1397 -1612
  178. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-log-private.h +35 -67
  179. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-log.c +49 -83
  180. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-marking-private.h +30 -36
  181. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-marking.c +1821 -1470
  182. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-mutex-private.h +7 -12
  183. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-opts-private.h +80 -108
  184. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-opts.c +354 -424
  185. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-private.h +101 -117
  186. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-status-private.h +3 -8
  187. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-status.c +92 -119
  188. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-traverse-util-private.h +19 -29
  189. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-traverse-util.c +136 -176
  190. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-util-private.h +11 -21
  191. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt-util.c +96 -135
  192. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt.c +1092 -1320
  193. data/ext/libmongocrypt/libmongocrypt/src/mongocrypt.h +130 -295
  194. data/ext/libmongocrypt/libmongocrypt/src/os_posix/os_dll.c +66 -78
  195. data/ext/libmongocrypt/libmongocrypt/src/os_posix/os_mutex.c +20 -28
  196. data/ext/libmongocrypt/libmongocrypt/src/os_win/os_dll.c +60 -73
  197. data/ext/libmongocrypt/libmongocrypt/src/os_win/os_mutex.c +8 -16
  198. data/ext/libmongocrypt/libmongocrypt/test/crypt_shared-stub.cpp +57 -87
  199. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-delete/empty/encrypted-payload-v2.json +60 -0
  200. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-delete/success/encrypted-payload-v2.json +67 -0
  201. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explain/with-csfle/encrypted-payload.json +2 -2
  202. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explicit/find-indexed-contentionFactor1-v2.json +8 -0
  203. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explicit/find-indexed-v2.json +8 -0
  204. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explicit/insert-indexed-contentionFactor1-v2.json +8 -0
  205. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explicit/insert-indexed-same-user-and-index-key-v2.json +8 -0
  206. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-explicit/insert-indexed-v2.json +8 -0
  207. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-equality-v2/cmd.json +6 -0
  208. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-equality-v2/encrypted-field-map.json +22 -0
  209. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-equality-v2/encrypted-payload.json +40 -0
  210. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-equality-v2/mongocryptd-reply.json +19 -0
  211. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/date-v2/cmd.json +10 -0
  212. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/date-v2/encrypted-field-map.json +27 -0
  213. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/date-v2/encrypted-payload.json +41 -0
  214. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/date-v2/mongocryptd-reply.json +49 -0
  215. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-precision-v2/cmd.json +6 -0
  216. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-precision-v2/encrypted-field-map.json +30 -0
  217. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-precision-v2/encrypted-payload.json +50 -0
  218. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-precision-v2/mongocryptd-reply.json +58 -0
  219. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-v2/cmd.json +6 -0
  220. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-v2/encrypted-field-map.json +27 -0
  221. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-v2/encrypted-payload.json +41 -0
  222. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/decimal128-v2/mongocryptd-reply.json +49 -0
  223. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-precision-v2/cmd.json +8 -0
  224. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-precision-v2/encrypted-field-map.json +30 -0
  225. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-precision-v2/encrypted-payload.json +44 -0
  226. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-precision-v2/mongocryptd-reply.json +52 -0
  227. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-v2/cmd.json +8 -0
  228. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-v2/encrypted-field-map.json +27 -0
  229. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-v2/encrypted-payload.json +41 -0
  230. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/double-v2/mongocryptd-reply.json +49 -0
  231. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int32-v2/cmd.json +8 -0
  232. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int32-v2/encrypted-field-map.json +27 -0
  233. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int32-v2/encrypted-payload.json +41 -0
  234. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int32-v2/mongocryptd-reply.json +49 -0
  235. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int64-v2/cmd.json +8 -0
  236. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int64-v2/encrypted-field-map.json +27 -0
  237. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int64-v2/encrypted-payload.json +41 -0
  238. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range/int64-v2/mongocryptd-reply.json +49 -0
  239. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/double/encrypted-payload-v2.json +26 -0
  240. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/double-precision/encrypted-payload-v2.json +26 -0
  241. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/int32/encrypted-payload-v2.json +26 -0
  242. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/int32-nominmax/encrypted-payload-v2.json +26 -0
  243. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/int32-openinterval/encrypted-payload-v2.json +16 -0
  244. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert/cmd.json +1 -1
  245. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/date/RNG_DATA.h +65 -65
  246. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/date-v2/cmd.json +13 -0
  247. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/date-v2/encrypted-field-map.json +27 -0
  248. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/date-v2/encrypted-payload.json +44 -0
  249. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/date-v2/mongocryptd-reply.json +52 -0
  250. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128/RNG_DATA.h +132 -132
  251. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-precision/RNG_DATA.h +71 -71
  252. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-precision-v2/cmd.json +9 -0
  253. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-precision-v2/encrypted-field-map.json +30 -0
  254. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-precision-v2/encrypted-payload.json +53 -0
  255. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-precision-v2/mongocryptd-reply.json +61 -0
  256. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-v2/cmd.json +9 -0
  257. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-v2/encrypted-field-map.json +27 -0
  258. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-v2/encrypted-payload.json +44 -0
  259. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/decimal128-v2/mongocryptd-reply.json +52 -0
  260. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double/RNG_DATA.h +68 -68
  261. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-precision/RNG_DATA.h +19 -19
  262. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-precision-v2/cmd.json +11 -0
  263. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-precision-v2/encrypted-field-map.json +30 -0
  264. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-precision-v2/encrypted-payload.json +47 -0
  265. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-precision-v2/mongocryptd-reply.json +55 -0
  266. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-v2/cmd.json +11 -0
  267. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-v2/encrypted-field-map.json +27 -0
  268. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-v2/encrypted-payload.json +44 -0
  269. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/double-v2/mongocryptd-reply.json +52 -0
  270. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int32/RNG_DATA.h +25 -25
  271. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int32-v2/cmd.json +11 -0
  272. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int32-v2/encrypted-field-map.json +27 -0
  273. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int32-v2/encrypted-payload.json +44 -0
  274. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int32-v2/mongocryptd-reply.json +52 -0
  275. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int64/RNG_DATA.h +65 -65
  276. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int64-v2/cmd.json +11 -0
  277. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int64-v2/encrypted-field-map.json +27 -0
  278. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int64-v2/encrypted-payload.json +44 -0
  279. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range/int64-v2/mongocryptd-reply.json +52 -0
  280. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/double/RNG_DATA.h +68 -68
  281. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/double/encrypted-payload-v2.json +8 -0
  282. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/double-precision/RNG_DATA.h +19 -19
  283. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/double-precision/encrypted-payload-v2.json +8 -0
  284. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32/RNG_DATA.h +25 -25
  285. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32/encrypted-payload-v2.json +8 -0
  286. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32-nominmax/encrypted-payload-v2.json +8 -0
  287. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/sparsity-2/RNG_DATA.h +15 -15
  288. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/sparsity-2/encrypted-payload-v2.json +8 -0
  289. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-unindexed-v2/cmd.json +9 -0
  290. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-unindexed-v2/encrypted-field-map.json +18 -0
  291. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-unindexed-v2/encrypted-payload.json +14 -0
  292. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-unindexed-v2/mongocryptd-reply.json +41 -0
  293. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-v2/cmd.json +9 -0
  294. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-v2/encrypted-field-map.json +22 -0
  295. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-v2/encrypted-payload.json +39 -0
  296. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-v2/mongocryptd-reply.json +49 -0
  297. data/ext/libmongocrypt/libmongocrypt/test/data/iev-v2/FLECrudTest-insertOneRangeV2.json +10 -0
  298. data/ext/libmongocrypt/libmongocrypt/test/data/iev-v2/FLECrudTest-insertOneV2.json +10 -0
  299. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/README.md +17 -0
  300. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/aes-ctr.json +29 -0
  301. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2-fixed.json +10 -0
  302. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2-generated.json +38 -0
  303. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2aead-decrypt.json +35 -0
  304. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2aead-fixed.json +29 -0
  305. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2aead-generated.json +122 -0
  306. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2v2-aead-fixed.json +29 -0
  307. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/fle2v2-aead-generated.json +122 -0
  308. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/mcgrew.json +12 -0
  309. data/ext/libmongocrypt/libmongocrypt/test/data/roundtrip/nist.json +20 -0
  310. data/ext/libmongocrypt/libmongocrypt/test/data/tokens/README.md +27 -0
  311. data/ext/libmongocrypt/libmongocrypt/test/data/tokens/mc.json +21 -0
  312. data/ext/libmongocrypt/libmongocrypt/test/data/tokens/server.json +21 -0
  313. data/ext/libmongocrypt/libmongocrypt/test/example-no-bson.c +4 -4
  314. data/ext/libmongocrypt/libmongocrypt/test/example-state-machine.c +278 -323
  315. data/ext/libmongocrypt/libmongocrypt/test/fuzz_kms.c +8 -7
  316. data/ext/libmongocrypt/libmongocrypt/test/test-dll.cpp +6 -7
  317. data/ext/libmongocrypt/libmongocrypt/test/test-gcp-auth.c +221 -283
  318. data/ext/libmongocrypt/libmongocrypt/test/test-mc-efc.c +58 -73
  319. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-find-equality-payload-v2.c +78 -0
  320. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-find-range-payload-v2.c +89 -0
  321. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-iev-v2.c +248 -0
  322. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-iev.c +467 -414
  323. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-iup-v2.c +172 -0
  324. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-iup.c +141 -159
  325. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-uev-v2.c +338 -0
  326. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-payload-uev.c +238 -176
  327. data/ext/libmongocrypt/libmongocrypt/test/test-mc-fle2-rfds.c +373 -474
  328. data/ext/libmongocrypt/libmongocrypt/test/test-mc-range-edge-generation.c +346 -388
  329. data/ext/libmongocrypt/libmongocrypt/test/test-mc-range-encoding.c +708 -825
  330. data/ext/libmongocrypt/libmongocrypt/test/test-mc-range-mincover.c +417 -481
  331. data/ext/libmongocrypt/libmongocrypt/test/test-mc-rangeopts.c +110 -144
  332. data/ext/libmongocrypt/libmongocrypt/test/test-mc-reader.c +124 -207
  333. data/ext/libmongocrypt/libmongocrypt/test/test-mc-tokens.c +223 -213
  334. data/ext/libmongocrypt/libmongocrypt/test/test-mc-writer.c +176 -0
  335. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-assert-match-bson.c +634 -807
  336. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-assert-match-bson.h +1 -2
  337. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-assert.h +168 -189
  338. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-buffer.c +187 -211
  339. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-cache-oauth.c +34 -36
  340. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-cache.c +210 -233
  341. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-ciphertext.c +185 -220
  342. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-compact.c +331 -445
  343. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-crypto-hooks.c +670 -814
  344. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-crypto-std-hooks.c +102 -135
  345. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-crypto-std-hooks.h +54 -71
  346. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-crypto.c +394 -846
  347. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-csfle-lib.c +159 -183
  348. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-ctx-decrypt.c +867 -1045
  349. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-ctx-encrypt.c +4349 -4713
  350. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-ctx-rewrap-many-datakey.c +750 -964
  351. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-ctx-setopt.c +991 -1178
  352. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-datakey.c +342 -419
  353. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-dll.c +23 -30
  354. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-endpoint.c +98 -111
  355. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-kek.c +49 -52
  356. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-key-broker.c +770 -920
  357. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-key-cache.c +354 -407
  358. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-key.c +197 -245
  359. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-kms-ctx.c +286 -370
  360. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-kms-responses.c +147 -166
  361. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-local-kms.c +50 -61
  362. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-log.c +85 -100
  363. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-marking.c +656 -692
  364. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-status.c +46 -58
  365. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-traverse-util.c +377 -451
  366. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-util.c +48 -67
  367. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt-util.h +4 -10
  368. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt.c +827 -918
  369. data/ext/libmongocrypt/libmongocrypt/test/test-mongocrypt.h +111 -172
  370. data/ext/libmongocrypt/libmongocrypt/test/util/csfle.c +508 -559
  371. data/ext/libmongocrypt/libmongocrypt/test/util/util.c +735 -881
  372. data/ext/libmongocrypt/libmongocrypt/test/util/util.h +33 -55
  373. data/lib/libmongocrypt_helper/version.rb +2 -2
  374. data.tar.gz.sig +0 -0
  375. metadata +154 -26
  376. metadata.gz.sig +0 -0
  377. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/resources/fle2-find-range-explicit/int32/encrypted-payload.json +0 -26
  378. data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/resources/fle2-find-range-explicit/int32/key-document.json +0 -0
  379. data/ext/libmongocrypt/libmongocrypt/bindings/node/lib/credentialsProvider.js +0 -33
  380. data/ext/libmongocrypt/libmongocrypt/bindings/node/test/credentialsProvider.test.js +0 -163
  381. data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/fle2-find-range-explicit/int32/encrypted-payload.json +0 -26
  382. data/ext/libmongocrypt/libmongocrypt/test/data/aes-ctr.cstructs +0 -359
  383. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-aead.cstructs +0 -109
  384. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/int32-nominmax/encrypted-payload.json +0 -26
  385. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/int32-nominmax/rangeopts.json +0 -5
  386. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-find-range-explicit/int32-nominmax/value-to-encrypt.json +0 -20
  387. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32-nominmax/RNG_DATA.h +0 -70
  388. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32-nominmax/encrypted-payload.json +0 -8
  389. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32-nominmax/rangeopts.json +0 -5
  390. data/ext/libmongocrypt/libmongocrypt/test/data/fle2-insert-range-explicit/int32-nominmax/value-to-encrypt.json +0 -5
  391. data/ext/libmongocrypt/libmongocrypt/test/data/fle2.cstructs +0 -33
  392. /data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/resources/{fle2-find-range-explicit → fle2-find-range-explicit-v2}/int32/key-filter.json +0 -0
  393. /data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/resources/{fle2-find-range-explicit → fle2-find-range-explicit-v2}/int32/rangeopts.json +0 -0
  394. /data/ext/libmongocrypt/libmongocrypt/bindings/java/mongocrypt/src/test/resources/{fle2-find-range-explicit → fle2-find-range-explicit-v2}/int32/value-to-encrypt.json +0 -0
  395. /data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/{fle2-find-range-explicit → fle2-find-range-explicit-v2}/int32/rangeopts.json +0 -0
  396. /data/ext/libmongocrypt/libmongocrypt/bindings/python/test/data/{fle2-find-range-explicit → fle2-find-range-explicit-v2}/int32/value-to-encrypt.json +0 -0
@@ -4,10 +4,10 @@
4
4
  #include "./macros.h"
5
5
  #include "./str.h"
6
6
 
7
- #include <stdbool.h>
8
7
  #include <inttypes.h>
9
- #include <string.h>
8
+ #include <stdbool.h>
10
9
  #include <stdlib.h>
10
+ #include <string.h>
11
11
 
12
12
  MLIB_C_LINKAGE_BEGIN
13
13
 
@@ -15,33 +15,31 @@ MLIB_C_LINKAGE_BEGIN
15
15
  * @brief A 128-bit binary integer
16
16
  */
17
17
  typedef union {
18
- struct {
19
- uint64_t lo;
20
- uint64_t hi;
21
- } r;
18
+ struct {
19
+ uint64_t lo;
20
+ uint64_t hi;
21
+ } r;
22
22
  #if defined(__SIZEOF_INT128__)
23
- // These union members are only for the purpose of debugging visualization
24
- // and testing, and will only appear correctly on little-endian platforms.
25
- __int128_t signed_;
26
- __uint128_t unsigned_;
23
+ // These union members are only for the purpose of debugging visualization
24
+ // and testing, and will only appear correctly on little-endian platforms.
25
+ __int128_t signed_;
26
+ __uint128_t unsigned_;
27
27
  #endif
28
28
  } mlib_int128;
29
29
 
30
30
  /// Define an int128 from a literal within [INT64_MIN, INT64_MAX]
31
- #define MLIB_INT128(N) MLIB_INIT (mlib_int128) MLIB_INT128_C (N)
31
+ #define MLIB_INT128(N) MLIB_INIT(mlib_int128) MLIB_INT128_C(N)
32
32
  /// Define an int128 from a literal within [INT64_MIN, INT64_MAX] (usable as a
33
33
  /// constant init)
34
- #define MLIB_INT128_C(N) \
35
- MLIB_INT128_FROM_PARTS ((uint64_t) INT64_C (N), \
36
- (INT64_C (N) < 0 ? UINT64_MAX : 0))
34
+ #define MLIB_INT128_C(N) MLIB_INT128_FROM_PARTS((uint64_t)INT64_C(N), (INT64_C(N) < 0 ? UINT64_MAX : 0))
37
35
  /**
38
36
  * @brief Cast an integral value to an mlib_int128
39
37
  *
40
38
  * If the argument is signed and less-than zero, it will be sign-extended
41
39
  */
42
- #define MLIB_INT128_CAST(N) \
43
- MLIB_INIT (mlib_int128) \
44
- MLIB_INT128_FROM_PARTS ((uint64_t) (N), ((N) < 0 ? UINT64_MAX : 0))
40
+ #define MLIB_INT128_CAST(N) \
41
+ MLIB_INIT(mlib_int128) \
42
+ MLIB_INT128_FROM_PARTS((uint64_t)(N), ((N) < 0 ? UINT64_MAX : 0))
45
43
 
46
44
  /**
47
45
  * @brief Create an mlib_int128 from the low and high parts of the integer
@@ -49,20 +47,17 @@ typedef union {
49
47
  * @param LowWord_u64 The low-value 64 bits of the number
50
48
  * @param HighWord_u64 The high-value 64 bits of the number
51
49
  */
52
- #define MLIB_INT128_FROM_PARTS(LowWord_u64, HighWord_u64) \
53
- { \
54
- {LowWord_u64, HighWord_u64}, \
55
- }
50
+ #define MLIB_INT128_FROM_PARTS(LowWord_u64, HighWord_u64) \
51
+ { {LowWord_u64, HighWord_u64}, }
56
52
 
57
53
  /// Maximum value of int128 when treated as a signed integer
58
- #define MLIB_INT128_SMAX \
59
- MLIB_INT128_FROM_PARTS (UINT64_MAX, UINT64_MAX & ~(UINT64_C (1) << 63))
54
+ #define MLIB_INT128_SMAX MLIB_INT128_FROM_PARTS(UINT64_MAX, UINT64_MAX & ~(UINT64_C(1) << 63))
60
55
 
61
56
  /// Minimum value of int128, when treated as a signed integer
62
- #define MLIB_INT128_SMIN MLIB_INT128_FROM_PARTS (0, UINT64_C (1) << 63)
57
+ #define MLIB_INT128_SMIN MLIB_INT128_FROM_PARTS(0, UINT64_C(1) << 63)
63
58
 
64
59
  /// Maximum value of int128, when treated as an unsigned integer
65
- #define MLIB_INT128_UMAX MLIB_INT128_FROM_PARTS (UINT64_MAX, UINT64_MAX)
60
+ #define MLIB_INT128_UMAX MLIB_INT128_FROM_PARTS(UINT64_MAX, UINT64_MAX)
66
61
 
67
62
  /**
68
63
  * @brief Compare two 128-bit integers as unsigned integers
@@ -71,20 +66,18 @@ typedef union {
71
66
  * @return (R > 0) if (left > right)
72
67
  * @return (R = 0) if (left == right)
73
68
  */
74
- static mlib_constexpr_fn int
75
- mlib_int128_ucmp (mlib_int128 left, mlib_int128 right)
76
- {
77
- if (left.r.hi > right.r.hi) {
78
- return 1;
79
- } else if (left.r.hi < right.r.hi) {
80
- return -1;
81
- } else if (left.r.lo > right.r.lo) {
82
- return 1;
83
- } else if (left.r.lo < right.r.lo) {
84
- return -1;
85
- } else {
86
- return 0;
87
- }
69
+ static mlib_constexpr_fn int mlib_int128_ucmp(mlib_int128 left, mlib_int128 right) {
70
+ if (left.r.hi > right.r.hi) {
71
+ return 1;
72
+ } else if (left.r.hi < right.r.hi) {
73
+ return -1;
74
+ } else if (left.r.lo > right.r.lo) {
75
+ return 1;
76
+ } else if (left.r.lo < right.r.lo) {
77
+ return -1;
78
+ } else {
79
+ return 0;
80
+ }
88
81
  }
89
82
 
90
83
  /**
@@ -94,19 +87,17 @@ mlib_int128_ucmp (mlib_int128 left, mlib_int128 right)
94
87
  * @return (R > 0) if (left > right)
95
88
  * @return (R = 0) if (left == right)
96
89
  */
97
- static mlib_constexpr_fn int
98
- mlib_int128_scmp (mlib_int128 left, mlib_int128 right)
99
- {
100
- if ((left.r.hi & (1ull << 63)) == (right.r.hi & (1ull << 63))) {
101
- // Same signed-ness, so they are as comparable as unsigned
102
- return mlib_int128_ucmp (left, right);
103
- } else if (left.r.hi & (1ull << 63)) {
104
- // The left is negative
105
- return -1;
106
- } else {
107
- // The right is negative
108
- return 1;
109
- }
90
+ static mlib_constexpr_fn int mlib_int128_scmp(mlib_int128 left, mlib_int128 right) {
91
+ if ((left.r.hi & (1ull << 63)) == (right.r.hi & (1ull << 63))) {
92
+ // Same signed-ness, so they are as comparable as unsigned
93
+ return mlib_int128_ucmp(left, right);
94
+ } else if (left.r.hi & (1ull << 63)) {
95
+ // The left is negative
96
+ return -1;
97
+ } else {
98
+ // The right is negative
99
+ return 1;
100
+ }
110
101
  }
111
102
 
112
103
  /**
@@ -115,10 +106,8 @@ mlib_int128_scmp (mlib_int128 left, mlib_int128 right)
115
106
  * @retval true If left == right
116
107
  * @retval false Otherwise
117
108
  */
118
- static mlib_constexpr_fn bool
119
- mlib_int128_eq (mlib_int128 left, mlib_int128 right)
120
- {
121
- return mlib_int128_ucmp (left, right) == 0;
109
+ static mlib_constexpr_fn bool mlib_int128_eq(mlib_int128 left, mlib_int128 right) {
110
+ return mlib_int128_ucmp(left, right) == 0;
122
111
  }
123
112
 
124
113
  /**
@@ -126,26 +115,22 @@ mlib_int128_eq (mlib_int128 left, mlib_int128 right)
126
115
  *
127
116
  * @return mlib_int128 The sum of the two addends. Overflow will wrap.
128
117
  */
129
- static mlib_constexpr_fn mlib_int128
130
- mlib_int128_add (mlib_int128 left, mlib_int128 right)
131
- {
132
- uint64_t losum = left.r.lo + right.r.lo;
133
- // Overflow check
134
- unsigned carry = (losum < left.r.lo || losum < right.r.lo);
135
- uint64_t hisum = left.r.hi + right.r.hi + carry;
136
- return MLIB_INIT (mlib_int128) MLIB_INT128_FROM_PARTS (losum, hisum);
118
+ static mlib_constexpr_fn mlib_int128 mlib_int128_add(mlib_int128 left, mlib_int128 right) {
119
+ uint64_t losum = left.r.lo + right.r.lo;
120
+ // Overflow check
121
+ unsigned carry = (losum < left.r.lo || losum < right.r.lo);
122
+ uint64_t hisum = left.r.hi + right.r.hi + carry;
123
+ return MLIB_INIT(mlib_int128) MLIB_INT128_FROM_PARTS(losum, hisum);
137
124
  }
138
125
 
139
126
  /**
140
127
  * @brief Treat the given 128-bit integer as signed, and return its
141
128
  * negated value
142
129
  */
143
- static mlib_constexpr_fn mlib_int128
144
- mlib_int128_negate (mlib_int128 v)
145
- {
146
- mlib_int128 r = MLIB_INT128_FROM_PARTS (~v.r.lo, ~v.r.hi);
147
- r = mlib_int128_add (r, MLIB_INT128 (1));
148
- return r;
130
+ static mlib_constexpr_fn mlib_int128 mlib_int128_negate(mlib_int128 v) {
131
+ mlib_int128 r = MLIB_INT128_FROM_PARTS(~v.r.lo, ~v.r.hi);
132
+ r = mlib_int128_add(r, MLIB_INT128(1));
133
+ return r;
149
134
  }
150
135
 
151
136
  /**
@@ -153,14 +138,12 @@ mlib_int128_negate (mlib_int128 v)
153
138
  *
154
139
  * @return mlib_int128 The difference between `from` and `less`
155
140
  */
156
- static mlib_constexpr_fn mlib_int128
157
- mlib_int128_sub (mlib_int128 from, mlib_int128 less)
158
- {
159
- unsigned borrow = from.r.lo < less.r.lo;
160
- uint64_t low = from.r.lo - less.r.lo;
161
- uint64_t high = from.r.hi - less.r.hi;
162
- high -= borrow;
163
- return MLIB_INIT (mlib_int128) MLIB_INT128_FROM_PARTS (low, high);
141
+ static mlib_constexpr_fn mlib_int128 mlib_int128_sub(mlib_int128 from, mlib_int128 less) {
142
+ unsigned borrow = from.r.lo < less.r.lo;
143
+ uint64_t low = from.r.lo - less.r.lo;
144
+ uint64_t high = from.r.hi - less.r.hi;
145
+ high -= borrow;
146
+ return MLIB_INIT(mlib_int128) MLIB_INT128_FROM_PARTS(low, high);
164
147
  }
165
148
 
166
149
  /**
@@ -170,35 +153,33 @@ mlib_int128_sub (mlib_int128 from, mlib_int128 less)
170
153
  * @param off The offset to shift left. If negative, shifts right
171
154
  * @return The result of the shift operation
172
155
  */
173
- static mlib_constexpr_fn mlib_int128
174
- mlib_int128_lshift (mlib_int128 val, int off)
175
- {
176
- if (off > 0) {
177
- if (off >= 64) {
178
- off -= 64;
179
- uint64_t high = val.r.lo << off;
180
- return MLIB_INIT (mlib_int128) MLIB_INT128_FROM_PARTS (0, high);
181
- } else {
182
- uint64_t low = val.r.lo << off;
183
- uint64_t high = val.r.hi << off;
184
- high |= val.r.lo >> (64 - off);
185
- return MLIB_INIT (mlib_int128) MLIB_INT128_FROM_PARTS (low, high);
186
- }
187
- } else if (off < 0) {
188
- off = -off;
189
- if (off >= 64) {
190
- off -= 64;
191
- uint64_t low = val.r.hi >> off;
192
- return MLIB_INIT (mlib_int128) MLIB_INT128_FROM_PARTS (low, 0);
193
- } else {
194
- uint64_t high = val.r.hi >> off;
195
- uint64_t low = val.r.lo >> off;
196
- low |= val.r.hi << (64 - off);
197
- return MLIB_INIT (mlib_int128) MLIB_INT128_FROM_PARTS (low, high);
198
- }
199
- } else {
200
- return val;
201
- }
156
+ static mlib_constexpr_fn mlib_int128 mlib_int128_lshift(mlib_int128 val, int off) {
157
+ if (off > 0) {
158
+ if (off >= 64) {
159
+ off -= 64;
160
+ uint64_t high = val.r.lo << off;
161
+ return MLIB_INIT(mlib_int128) MLIB_INT128_FROM_PARTS(0, high);
162
+ } else {
163
+ uint64_t low = val.r.lo << off;
164
+ uint64_t high = val.r.hi << off;
165
+ high |= val.r.lo >> (64 - off);
166
+ return MLIB_INIT(mlib_int128) MLIB_INT128_FROM_PARTS(low, high);
167
+ }
168
+ } else if (off < 0) {
169
+ off = -off;
170
+ if (off >= 64) {
171
+ off -= 64;
172
+ uint64_t low = val.r.hi >> off;
173
+ return MLIB_INIT(mlib_int128) MLIB_INT128_FROM_PARTS(low, 0);
174
+ } else {
175
+ uint64_t high = val.r.hi >> off;
176
+ uint64_t low = val.r.lo >> off;
177
+ low |= val.r.hi << (64 - off);
178
+ return MLIB_INIT(mlib_int128) MLIB_INT128_FROM_PARTS(low, high);
179
+ }
180
+ } else {
181
+ return val;
182
+ }
202
183
  }
203
184
 
204
185
  /**
@@ -208,241 +189,223 @@ mlib_int128_lshift (mlib_int128 val, int off)
208
189
  * @param off The offset to shift right. If negative, shifts left
209
190
  * @return The result of the shift operation
210
191
  */
211
- static mlib_constexpr_fn mlib_int128
212
- mlib_int128_rshift (mlib_int128 val, int off)
213
- {
214
- return mlib_int128_lshift (val, -off);
192
+ static mlib_constexpr_fn mlib_int128 mlib_int128_rshift(mlib_int128 val, int off) {
193
+ return mlib_int128_lshift(val, -off);
215
194
  }
216
195
 
217
196
  /**
218
197
  * @brief Bitwise-or two 128-bit integers
219
198
  */
220
- static mlib_constexpr_fn mlib_int128
221
- mlib_int128_bitor (mlib_int128 l, mlib_int128 r)
222
- {
223
- return MLIB_INIT (mlib_int128)
224
- MLIB_INT128_FROM_PARTS (l.r.lo | r.r.lo, l.r.hi | r.r.hi);
199
+ static mlib_constexpr_fn mlib_int128 mlib_int128_bitor(mlib_int128 l, mlib_int128 r) {
200
+ return MLIB_INIT(mlib_int128) MLIB_INT128_FROM_PARTS(l.r.lo | r.r.lo, l.r.hi | r.r.hi);
225
201
  }
226
202
 
227
203
  // Multiply two 64bit integers to get a 128-bit result without overflow
228
- static mlib_constexpr_fn mlib_int128
229
- _mlibUnsignedMult128 (uint64_t left, uint64_t right)
230
- {
231
- // Perform a Knuth 4.3.1M multiplication
232
- uint32_t u[2] = {(uint32_t) left, (uint32_t) (left >> 32)};
233
- uint32_t v[2] = {(uint32_t) right, (uint32_t) (right >> 32)};
234
- uint32_t w[4] = {0};
235
-
236
- for (int j = 0; j < 2; ++j) {
237
- uint64_t t = 0;
238
- for (int i = 0; i < 2; ++i) {
239
- t += (uint64_t) (u[i]) * v[j] + w[i + j];
240
- w[i + j] = (uint32_t) t;
241
- t >>= 32;
242
- }
243
- w[j + 2] = (uint32_t) t;
244
- }
245
-
246
- return MLIB_INIT (mlib_int128) MLIB_INT128_FROM_PARTS (
247
- ((uint64_t) w[1] << 32) | w[0], ((uint64_t) w[3] << 32) | w[2]);
204
+ static mlib_constexpr_fn mlib_int128 _mlibUnsignedMult128(uint64_t left, uint64_t right) {
205
+ // Perform a Knuth 4.3.1M multiplication
206
+ uint32_t u[2] = {(uint32_t)left, (uint32_t)(left >> 32)};
207
+ uint32_t v[2] = {(uint32_t)right, (uint32_t)(right >> 32)};
208
+ uint32_t w[4] = {0};
209
+
210
+ for (int j = 0; j < 2; ++j) {
211
+ uint64_t t = 0;
212
+ for (int i = 0; i < 2; ++i) {
213
+ t += (uint64_t)(u[i]) * v[j] + w[i + j];
214
+ w[i + j] = (uint32_t)t;
215
+ t >>= 32;
216
+ }
217
+ w[j + 2] = (uint32_t)t;
218
+ }
219
+
220
+ return MLIB_INIT(mlib_int128) MLIB_INT128_FROM_PARTS(((uint64_t)w[1] << 32) | w[0], ((uint64_t)w[3] << 32) | w[2]);
248
221
  }
249
222
 
250
223
  /**
251
224
  * @brief Multiply two mlib_int128s together. Overflow will wrap.
252
225
  */
253
- static mlib_constexpr_fn mlib_int128
254
- mlib_int128_mul (mlib_int128 l, mlib_int128 r)
255
- {
256
- // Multiply the low-order word
257
- mlib_int128 ret = _mlibUnsignedMult128 (l.r.lo, r.r.lo);
258
- // Accumulate the high-order parts:
259
- ret.r.hi += l.r.lo * r.r.hi;
260
- ret.r.hi += l.r.hi * r.r.lo;
261
- return ret;
226
+ static mlib_constexpr_fn mlib_int128 mlib_int128_mul(mlib_int128 l, mlib_int128 r) {
227
+ // Multiply the low-order word
228
+ mlib_int128 ret = _mlibUnsignedMult128(l.r.lo, r.r.lo);
229
+ // Accumulate the high-order parts:
230
+ ret.r.hi += l.r.lo * r.r.hi;
231
+ ret.r.hi += l.r.hi * r.r.lo;
232
+ return ret;
262
233
  }
263
234
 
264
235
  /// Get the number of leading zeros in a 64bit number.
265
- static mlib_constexpr_fn int
266
- _mlibCountLeadingZeros_u64 (uint64_t bits)
267
- {
268
- int n = 0;
269
- if (bits == 0) {
270
- return 64;
271
- }
272
- while (!(1ull << 63 & bits)) {
273
- ++n;
274
- bits <<= 1;
275
- }
276
- return n;
236
+ static mlib_constexpr_fn int _mlibCountLeadingZeros_u64(uint64_t bits) {
237
+ int n = 0;
238
+ if (bits == 0) {
239
+ return 64;
240
+ }
241
+ while (!(1ull << 63 & bits)) {
242
+ ++n;
243
+ bits <<= 1;
244
+ }
245
+ return n;
277
246
  }
278
247
 
279
248
  /// Implementation of Knuth's algorithm 4.3.1 D for unsigned integer division
280
249
  static mlib_constexpr_fn void
281
- _mlibKnuth431D (uint32_t *const u,
282
- const int ulen,
283
- const uint32_t *const v,
284
- const int vlen,
285
- uint32_t *quotient)
286
- {
287
- // Part D1 (normalization) is done by caller,
288
- // normalized in u and v (radix b is 2^32)
289
- typedef uint64_t u64;
290
- typedef int64_t i64;
291
- typedef uint32_t u32;
292
- const int m = ulen - vlen - 1;
293
- const int n = vlen;
294
-
295
- // 'd' is 2^32. Shifting left and right is equivalent to mult and division by
296
- // d, respectively.
297
-
298
- // D2
299
- int j = m;
300
- for (;;) {
301
- // D3: Select two u32 as a u64:
302
- u64 two = ((u64) (u[j + n]) << 32) | u[j + n - 1];
303
- // D3: Partial quotient:
304
- u64 q = two / v[n - 1];
305
- // D3: Partial remainder: r̂
306
- u64 r = two % v[n - 1];
307
-
308
- // D3: Compute q̂ and r̂
309
- while (q >> 32 || q * (u64) v[n - 2] > (r << 32 | u[j + n - 2])) {
310
- q--;
311
- r += v[n - 1];
312
- if (r >> 32) {
250
+ _mlibKnuth431D(uint32_t *const u, const int ulen, const uint32_t *const v, const int vlen, uint32_t *quotient) {
251
+ // Part D1 (normalization) is done by caller,
252
+ // normalized in u and v (radix b is 2^32)
253
+ typedef uint64_t u64;
254
+ typedef int64_t i64;
255
+ typedef uint32_t u32;
256
+ const int m = ulen - vlen - 1;
257
+ const int n = vlen;
258
+
259
+ // 'd' is 2^32. Shifting left and right is equivalent to mult and division by
260
+ // d, respectively.
261
+
262
+ // D2
263
+ int j = m;
264
+ for (;;) {
265
+ // D3: Select two u32 as a u64:
266
+ u64 two = ((u64)(u[j + n]) << 32) | u[j + n - 1];
267
+ // D3: Partial quotient: q̂
268
+ u64 q = two / v[n - 1];
269
+ // D3: Partial remainder: r̂
270
+ u64 r = two % v[n - 1];
271
+
272
+ // D3: Compute and r̂
273
+ while (q >> 32 || q * (u64)v[n - 2] > (r << 32 | u[j + n - 2])) {
274
+ q--;
275
+ r += v[n - 1];
276
+ if (r >> 32) {
277
+ break;
278
+ }
279
+ }
280
+
281
+ // D4: Multiply and subtract
282
+ i64 k = 0;
283
+ i64 t = 0;
284
+ for (int i = 0; i < n; ++i) {
285
+ u64 prod = (u32)q * (u64)(v[i]);
286
+ t = u[i + j] - k - (u32)prod;
287
+ u[i + j] = (u32)t;
288
+ k = (i64)(prod >> 32) - (t >> 32);
289
+ }
290
+ t = u[j + n] - k;
291
+ u[j + n] = (u32)t;
292
+
293
+ quotient[j] = (u32)q;
294
+
295
+ // D5: Test remainder
296
+ if (t < 0) {
297
+ // D6: Add back
298
+ --quotient[j];
299
+ k = 0;
300
+ for (int i = 0; i < n; ++i) {
301
+ t = u[i + j] + k + v[i];
302
+ u[i + j] = (u32)(t);
303
+ k = t >> 32;
304
+ }
305
+ u[j + n] += (u32)k;
306
+ }
307
+
308
+ // D7:
309
+ --j;
310
+ if (j < 0) {
313
311
  break;
314
- }
315
- }
316
-
317
- // D4: Multiply and subtract
318
- i64 k = 0;
319
- i64 t = 0;
320
- for (int i = 0; i < n; ++i) {
321
- u64 prod = (u32) q * (u64) (v[i]);
322
- t = u[i + j] - k - (u32) prod;
323
- u[i + j] = (u32) t;
324
- k = (i64) (prod >> 32) - (t >> 32);
325
- }
326
- t = u[j + n] - k;
327
- u[j + n] = (u32) t;
328
-
329
- quotient[j] = (u32) q;
330
-
331
- // D5: Test remainder
332
- if (t < 0) {
333
- // D6: Add back
334
- --quotient[j];
335
- k = 0;
336
- for (int i = 0; i < n; ++i) {
337
- t = u[i + j] + k + v[i];
338
- u[i + j] = (u32) (t);
339
- k = t >> 32;
340
- }
341
- u[j + n] += (u32) k;
342
- }
343
-
344
- // D7:
345
- --j;
346
- if (j < 0) {
347
- break;
348
- }
349
- }
350
-
351
- // Denormalization (D8) is done by caller.
312
+ }
313
+ }
314
+
315
+ // Denormalization (D8) is done by caller.
352
316
  }
353
317
 
354
318
  /// The result of 128-bit division
355
319
  typedef struct mlib_int128_divmod_result {
356
- /// The quotient of the division operation (rounds to zero)
357
- mlib_int128 quotient;
358
- /// The remainder of the division operation
359
- mlib_int128 remainder;
320
+ /// The quotient of the division operation (rounds to zero)
321
+ mlib_int128 quotient;
322
+ /// The remainder of the division operation
323
+ mlib_int128 remainder;
360
324
  } mlib_int128_divmod_result;
361
325
 
362
326
  /// Divide a 128-bit number by a 64bit number.
363
- static mlib_constexpr_fn struct mlib_int128_divmod_result
364
- _mlibDivide_u128_by_u64 (const mlib_int128 numer, const uint64_t denom)
365
- {
366
- mlib_int128 adjusted = numer;
367
- adjusted.r.hi %= denom;
368
- int d = _mlibCountLeadingZeros_u64 (denom);
369
-
370
- typedef uint32_t u32;
371
- typedef uint64_t u64;
372
-
373
- if (d >= 32) {
374
- // jk: We're dividing by less than UINT32_MAX: We can do a simple short
375
- // division of two base32 numbers.
376
- // Treat the denominator as a single base32 digit:
377
- const u32 d0 = (u32) denom;
378
-
379
- // And the numerator as four base32 digits:
380
- const u64 n0 = (u32) (numer.r.lo);
381
- const u64 n1 = (u32) (numer.r.lo >> 32);
382
-
383
- // We don't need to split n2 and n3. (n3,n2) will be the first partial
384
- // dividend
385
- const u64 n3_n2 = numer.r.hi;
386
-
387
- // First partial remainder: (n3,n2) % d0
388
- const u64 r1 = n3_n2 % d0;
389
- // Second partial dividend: (r1,n1)
390
- const u64 r1_n1 = (r1 << 32) + n1;
391
- // Second partial remainder: (r1,n1) % d0
392
- const u64 r0 = r1_n1 % d0;
393
- // Final partial dividend: (r0,n0)
394
- const u64 r0_n0 = (r0 << 32) + n0;
395
- // Final remainder: (r0,n0) % d0
396
- const u64 rem = r0_n0 % d0;
397
-
398
- // Form the quotient as four base32 digits:
399
- // Least quotient digit: (r0,n0) / d0
400
- const u64 q0 = r0_n0 / d0;
401
- // Second quotient digit: (r1,n1) / d0
402
- const u64 q1 = r1_n1 / d0;
403
- // Third and fourth quotient digit: (n3,n2) / d0
404
- const u64 q3_q2 = n3_n2 / d0;
405
-
406
- // Low word of the quotient: (q1,q0)
407
- const u64 q1_q0 = (q1 << 32) + q0;
408
-
409
- return MLIB_INIT (mlib_int128_divmod_result){
410
- MLIB_INIT (mlib_int128) MLIB_INT128_FROM_PARTS (q1_q0, q3_q2),
411
- MLIB_INIT (mlib_int128) MLIB_INT128_FROM_PARTS (rem, 0),
412
- };
413
- }
414
-
415
- // Normalize for a Knuth 4.3.1D division. Convert the integers into two
416
- // base-32 numbers, with u and v being arrays of digits:
417
- u32 u[5] = {
418
- (u32) (adjusted.r.lo << d),
419
- (u32) (adjusted.r.lo >> (32 - d)),
420
- (u32) (adjusted.r.hi << d),
421
- (u32) (adjusted.r.hi >> (32 - d)),
422
- 0,
423
- };
424
-
425
- if (d != 0) {
426
- // Extra bits from overlap:
427
- u[2] |= (u32) (adjusted.r.lo >> (64 - d));
428
- u[4] |= (u32) (adjusted.r.hi >> (64 - d));
429
- }
430
-
431
- u32 v[2] = {
432
- (u32) (denom << d),
433
- (u32) (denom >> (32 - d)),
434
- };
435
-
436
- u32 qparts[3] = {0};
437
-
438
- _mlibKnuth431D (u, 5, v, 2, qparts);
439
-
440
- u64 rem = ((u64) u[1] << (32 - d)) | (u[0] >> d);
441
- u64 quo = ((u64) qparts[1] << 32) | qparts[0];
442
- return MLIB_INIT (mlib_int128_divmod_result){
443
- MLIB_INIT (mlib_int128) MLIB_INT128_FROM_PARTS (quo, numer.r.hi / denom),
444
- MLIB_INIT (mlib_int128) MLIB_INT128_FROM_PARTS (rem, 0),
445
- };
327
+ static mlib_constexpr_fn struct mlib_int128_divmod_result _mlibDivide_u128_by_u64(const mlib_int128 numer,
328
+ const uint64_t denom) {
329
+ mlib_int128 adjusted = numer;
330
+ adjusted.r.hi %= denom;
331
+ int d = _mlibCountLeadingZeros_u64(denom);
332
+
333
+ typedef uint32_t u32;
334
+ typedef uint64_t u64;
335
+
336
+ if (d >= 32) {
337
+ // jk: We're dividing by less than UINT32_MAX: We can do a simple short
338
+ // division of two base32 numbers.
339
+ // Treat the denominator as a single base32 digit:
340
+ const u32 d0 = (u32)denom;
341
+
342
+ // And the numerator as four base32 digits:
343
+ const u64 n0 = (u32)(numer.r.lo);
344
+ const u64 n1 = (u32)(numer.r.lo >> 32);
345
+
346
+ // We don't need to split n2 and n3. (n3,n2) will be the first partial
347
+ // dividend
348
+ const u64 n3_n2 = numer.r.hi;
349
+
350
+ // First partial remainder: (n3,n2) % d0
351
+ const u64 r1 = n3_n2 % d0;
352
+ // Second partial dividend: (r1,n1)
353
+ const u64 r1_n1 = (r1 << 32) + n1;
354
+ // Second partial remainder: (r1,n1) % d0
355
+ const u64 r0 = r1_n1 % d0;
356
+ // Final partial dividend: (r0,n0)
357
+ const u64 r0_n0 = (r0 << 32) + n0;
358
+ // Final remainder: (r0,n0) % d0
359
+ const u64 rem = r0_n0 % d0;
360
+
361
+ // Form the quotient as four base32 digits:
362
+ // Least quotient digit: (r0,n0) / d0
363
+ const u64 q0 = r0_n0 / d0;
364
+ // Second quotient digit: (r1,n1) / d0
365
+ const u64 q1 = r1_n1 / d0;
366
+ // Third and fourth quotient digit: (n3,n2) / d0
367
+ const u64 q3_q2 = n3_n2 / d0;
368
+
369
+ // Low word of the quotient: (q1,q0)
370
+ const u64 q1_q0 = (q1 << 32) + q0;
371
+
372
+ return MLIB_INIT(mlib_int128_divmod_result){
373
+ MLIB_INIT(mlib_int128) MLIB_INT128_FROM_PARTS(q1_q0, q3_q2),
374
+ MLIB_INIT(mlib_int128) MLIB_INT128_FROM_PARTS(rem, 0),
375
+ };
376
+ }
377
+
378
+ // Normalize for a Knuth 4.3.1D division. Convert the integers into two
379
+ // base-32 numbers, with u and v being arrays of digits:
380
+ u32 u[5] = {
381
+ (u32)(adjusted.r.lo << d),
382
+ (u32)(adjusted.r.lo >> (32 - d)),
383
+ (u32)(adjusted.r.hi << d),
384
+ (u32)(adjusted.r.hi >> (32 - d)),
385
+ 0,
386
+ };
387
+
388
+ if (d != 0) {
389
+ // Extra bits from overlap:
390
+ u[2] |= (u32)(adjusted.r.lo >> (64 - d));
391
+ u[4] |= (u32)(adjusted.r.hi >> (64 - d));
392
+ }
393
+
394
+ u32 v[2] = {
395
+ (u32)(denom << d),
396
+ (u32)(denom >> (32 - d)),
397
+ };
398
+
399
+ u32 qparts[3] = {0};
400
+
401
+ _mlibKnuth431D(u, 5, v, 2, qparts);
402
+
403
+ u64 rem = ((u64)u[1] << (32 - d)) | (u[0] >> d);
404
+ u64 quo = ((u64)qparts[1] << 32) | qparts[0];
405
+ return MLIB_INIT(mlib_int128_divmod_result){
406
+ MLIB_INIT(mlib_int128) MLIB_INT128_FROM_PARTS(quo, numer.r.hi / denom),
407
+ MLIB_INIT(mlib_int128) MLIB_INT128_FROM_PARTS(rem, 0),
408
+ };
446
409
  }
447
410
 
448
411
  /**
@@ -452,200 +415,184 @@ _mlibDivide_u128_by_u64 (const mlib_int128 numer, const uint64_t denom)
452
415
  * @param denom The divisor
453
416
  * @return A struct with .quotient and .remainder results
454
417
  */
455
- static mlib_constexpr_fn mlib_int128_divmod_result
456
- mlib_int128_divmod (mlib_int128 numer, mlib_int128 denom)
457
- {
458
- const uint64_t nhi = numer.r.hi;
459
- const uint64_t nlo = numer.r.lo;
460
- const uint64_t dhi = denom.r.hi;
461
- const uint64_t dlo = denom.r.lo;
462
- if (dhi > nhi) {
463
- // Denominator is definitely larger than numerator. Quotient is zero,
464
- // remainder is full numerator.
465
- return MLIB_INIT (mlib_int128_divmod_result){MLIB_INT128 (0), numer};
466
- } else if (dhi == nhi) {
467
- // High words are equal
468
- if (nhi == 0) {
469
- // Both high words are zero, so this is just a division of two 64bit
470
- // numbers
471
- return MLIB_INIT (mlib_int128_divmod_result){
472
- MLIB_INT128_CAST (nlo / dlo),
473
- MLIB_INT128_CAST (nlo % dlo),
474
- };
475
- } else if (nlo > dlo) {
476
- // The numerator is larger than the denom and the high word on the
477
- // denom is non-zero, so this cannot divide to anything greater than 1.
478
- return MLIB_INIT (mlib_int128_divmod_result){
479
- MLIB_INT128 (1),
480
- mlib_int128_sub (numer, denom),
481
- };
482
- } else if (nlo < dlo) {
483
- // numer.r.lo < denom.r.lo and denom.r.hi > denom.r.lo, so the
484
- // integer division becomes zero
485
- return MLIB_INIT (mlib_int128_divmod_result){
486
- MLIB_INT128 (0),
487
- numer,
488
- };
489
- } else {
490
- // N / N is one
491
- return MLIB_INIT (mlib_int128_divmod_result){MLIB_INT128 (1),
492
- MLIB_INT128 (0)};
493
- }
494
- } else if (dhi == 0) {
495
- // No high in denominator. We can use a u128/u64
496
- return _mlibDivide_u128_by_u64 (numer, denom.r.lo);
497
- } else {
498
- // We'll need to do a full u128/u128 division
499
- // Normalize for Knuth 4.3.1D
500
- int d = _mlibCountLeadingZeros_u64 (denom.r.hi);
501
- // Does the denom have only three base32 digits?
502
- const bool has_three = d >= 32;
503
- d &= 31;
504
-
505
- uint32_t u[5] = {
506
- (uint32_t) (numer.r.lo << d),
507
- (uint32_t) (numer.r.lo >> (32 - d)),
508
- (uint32_t) (numer.r.hi << d),
509
- (uint32_t) (numer.r.hi >> (32 - d)),
510
- 0,
511
- };
512
- uint32_t v[4] = {
513
- (uint32_t) (denom.r.lo << d),
514
- (uint32_t) (denom.r.lo >> (32 - d)),
515
- (uint32_t) (denom.r.hi << d),
516
- (uint32_t) (denom.r.hi >> (32 - d)),
517
- };
518
- if (d != 0) {
519
- u[2] |= (uint32_t) (numer.r.lo >> (64 - d));
520
- u[4] |= (uint32_t) (numer.r.hi >> (64 - d));
521
- v[2] |= (uint32_t) (denom.r.lo >> (64 - d));
522
- };
523
-
524
- uint32_t q[2] = {0};
525
- if (has_three) {
526
- _mlibKnuth431D (u, 5, v, 3, q);
527
- } else {
528
- _mlibKnuth431D (u, 5, v, 4, q);
529
- }
530
-
531
- mlib_int128 remainder = MLIB_INT128_FROM_PARTS (
532
- ((uint64_t) u[1] << 32) | u[0], ((uint64_t) u[3] << 32) | u[2]);
533
- remainder = mlib_int128_rshift (remainder, d);
534
-
535
- return MLIB_INIT (mlib_int128_divmod_result){
536
- MLIB_INT128_CAST (q[0] | (uint64_t) q[1] << 32),
537
- remainder,
538
- };
539
- }
418
+ static mlib_constexpr_fn mlib_int128_divmod_result mlib_int128_divmod(mlib_int128 numer, mlib_int128 denom) {
419
+ const uint64_t nhi = numer.r.hi;
420
+ const uint64_t nlo = numer.r.lo;
421
+ const uint64_t dhi = denom.r.hi;
422
+ const uint64_t dlo = denom.r.lo;
423
+ if (dhi > nhi) {
424
+ // Denominator is definitely larger than numerator. Quotient is zero,
425
+ // remainder is full numerator.
426
+ return MLIB_INIT(mlib_int128_divmod_result){MLIB_INT128(0), numer};
427
+ } else if (dhi == nhi) {
428
+ // High words are equal
429
+ if (nhi == 0) {
430
+ // Both high words are zero, so this is just a division of two 64bit
431
+ // numbers
432
+ return MLIB_INIT(mlib_int128_divmod_result){
433
+ MLIB_INT128_CAST(nlo / dlo),
434
+ MLIB_INT128_CAST(nlo % dlo),
435
+ };
436
+ } else if (nlo > dlo) {
437
+ // The numerator is larger than the denom and the high word on the
438
+ // denom is non-zero, so this cannot divide to anything greater than 1.
439
+ return MLIB_INIT(mlib_int128_divmod_result){
440
+ MLIB_INT128(1),
441
+ mlib_int128_sub(numer, denom),
442
+ };
443
+ } else if (nlo < dlo) {
444
+ // numer.r.lo < denom.r.lo and denom.r.hi > denom.r.lo, so the
445
+ // integer division becomes zero
446
+ return MLIB_INIT(mlib_int128_divmod_result){
447
+ MLIB_INT128(0),
448
+ numer,
449
+ };
450
+ } else {
451
+ // N / N is one
452
+ return MLIB_INIT(mlib_int128_divmod_result){MLIB_INT128(1), MLIB_INT128(0)};
453
+ }
454
+ } else if (dhi == 0) {
455
+ // No high in denominator. We can use a u128/u64
456
+ return _mlibDivide_u128_by_u64(numer, denom.r.lo);
457
+ } else {
458
+ // We'll need to do a full u128/u128 division
459
+ // Normalize for Knuth 4.3.1D
460
+ int d = _mlibCountLeadingZeros_u64(denom.r.hi);
461
+ // Does the denom have only three base32 digits?
462
+ const bool has_three = d >= 32;
463
+ d &= 31;
464
+
465
+ uint32_t u[5] = {
466
+ (uint32_t)(numer.r.lo << d),
467
+ (uint32_t)(numer.r.lo >> (32 - d)),
468
+ (uint32_t)(numer.r.hi << d),
469
+ (uint32_t)(numer.r.hi >> (32 - d)),
470
+ 0,
471
+ };
472
+ uint32_t v[4] = {
473
+ (uint32_t)(denom.r.lo << d),
474
+ (uint32_t)(denom.r.lo >> (32 - d)),
475
+ (uint32_t)(denom.r.hi << d),
476
+ (uint32_t)(denom.r.hi >> (32 - d)),
477
+ };
478
+ if (d != 0) {
479
+ u[2] |= (uint32_t)(numer.r.lo >> (64 - d));
480
+ u[4] |= (uint32_t)(numer.r.hi >> (64 - d));
481
+ v[2] |= (uint32_t)(denom.r.lo >> (64 - d));
482
+ };
483
+
484
+ uint32_t q[2] = {0};
485
+ if (has_three) {
486
+ _mlibKnuth431D(u, 5, v, 3, q);
487
+ } else {
488
+ _mlibKnuth431D(u, 5, v, 4, q);
489
+ }
490
+
491
+ mlib_int128 remainder = MLIB_INT128_FROM_PARTS(((uint64_t)u[1] << 32) | u[0], ((uint64_t)u[3] << 32) | u[2]);
492
+ remainder = mlib_int128_rshift(remainder, d);
493
+
494
+ return MLIB_INIT(mlib_int128_divmod_result){
495
+ MLIB_INT128_CAST(q[0] | (uint64_t)q[1] << 32),
496
+ remainder,
497
+ };
498
+ }
540
499
  }
541
500
 
542
501
  /**
543
502
  * @brief Perform a division of two 128-bit numbers
544
503
  */
545
- static mlib_constexpr_fn mlib_int128
546
- mlib_int128_div (mlib_int128 numer, mlib_int128 denom)
547
- {
548
- return mlib_int128_divmod (numer, denom).quotient;
504
+ static mlib_constexpr_fn mlib_int128 mlib_int128_div(mlib_int128 numer, mlib_int128 denom) {
505
+ return mlib_int128_divmod(numer, denom).quotient;
549
506
  }
550
507
 
551
508
  /**
552
509
  * @brief Perform a modulus of two 128-bit numbers
553
510
  */
554
- static mlib_constexpr_fn mlib_int128
555
- mlib_int128_mod (mlib_int128 numer, mlib_int128 denom)
556
- {
557
- return mlib_int128_divmod (numer, denom).remainder;
511
+ static mlib_constexpr_fn mlib_int128 mlib_int128_mod(mlib_int128 numer, mlib_int128 denom) {
512
+ return mlib_int128_divmod(numer, denom).remainder;
558
513
  }
559
514
 
560
515
  /**
561
516
  * @brief Get the nth power of ten as a 128-bit number
562
517
  */
563
- static mlib_constexpr_fn mlib_int128
564
- mlib_int128_pow10 (uint8_t nth)
565
- {
566
- mlib_int128 r = MLIB_INT128 (1);
567
- while (nth-- > 0) {
568
- r = mlib_int128_mul (r, MLIB_INT128 (10));
569
- }
570
- return r;
518
+ static mlib_constexpr_fn mlib_int128 mlib_int128_pow10(uint8_t nth) {
519
+ mlib_int128 r = MLIB_INT128(1);
520
+ while (nth-- > 0) {
521
+ r = mlib_int128_mul(r, MLIB_INT128(10));
522
+ }
523
+ return r;
571
524
  }
572
525
 
573
526
  /**
574
527
  * @brief Get the Nth power of two as a 128-bit number
575
528
  */
576
- static mlib_constexpr_fn mlib_int128
577
- mlib_int128_pow2 (uint8_t nth)
578
- {
579
- return mlib_int128_lshift (MLIB_INT128 (1), (int) nth);
529
+ static mlib_constexpr_fn mlib_int128 mlib_int128_pow2(uint8_t nth) {
530
+ return mlib_int128_lshift(MLIB_INT128(1), (int)nth);
580
531
  }
581
532
 
582
533
  /**
583
534
  * @brief Read a 128-bit unsigned integer from a base-10 string
584
535
  */
585
- static mlib_constexpr_fn mlib_int128
586
- mlib_int128_from_string (const char *s, const char **end)
587
- {
588
- int radix = 10;
589
- if (mlib_strlen (s) > 2 && s[0] == '0') {
590
- // Check for a different radix
591
- char b = s[1];
592
- if (b == 'b' || b == 'B') {
593
- radix = 2;
594
- s += 2;
595
- } else if (b == 'c' || b == 'C') {
596
- radix = 8;
597
- s += 2;
598
- } else if (b == 'x' || b == 'X') {
599
- radix = 16;
600
- s += 2;
601
- } else {
602
- radix = 8;
603
- s += 1;
604
- }
605
- }
606
-
607
- mlib_int128 ret = MLIB_INT128 (0);
608
- for (; *s; ++s) {
609
- char c = *s;
610
- if (c == '\'') {
611
- // Digit separator. Skip it;
612
- continue;
613
- }
614
- if (c >= 'a') {
615
- // Uppercase (if a letter, otherwise some other punct):
616
- c = (char) (c - ('a' - 'A'));
617
- }
618
- int digit = c - '0';
619
- if (c >= 'A') {
620
- // It's actually a letter (or garbage, which we'll catch later)
621
- digit = (c - 'A') + 10;
622
- }
623
- if (digit > radix || digit < 0) {
624
- // The digit is outside of our radix, or garbage
625
- break;
626
- }
627
- ret = mlib_int128_mul (ret, MLIB_INT128_CAST (radix));
628
- ret = mlib_int128_add (ret, MLIB_INT128_CAST (digit));
629
- }
630
- if (end) {
631
- *end = s;
632
- }
633
- return ret;
536
+ static mlib_constexpr_fn mlib_int128 mlib_int128_from_string(const char *s, const char **end) {
537
+ int radix = 10;
538
+ if (mlib_strlen(s) > 2 && s[0] == '0') {
539
+ // Check for a different radix
540
+ char b = s[1];
541
+ if (b == 'b' || b == 'B') {
542
+ radix = 2;
543
+ s += 2;
544
+ } else if (b == 'c' || b == 'C') {
545
+ radix = 8;
546
+ s += 2;
547
+ } else if (b == 'x' || b == 'X') {
548
+ radix = 16;
549
+ s += 2;
550
+ } else {
551
+ radix = 8;
552
+ s += 1;
553
+ }
554
+ }
555
+
556
+ mlib_int128 ret = MLIB_INT128(0);
557
+ for (; *s; ++s) {
558
+ char c = *s;
559
+ if (c == '\'') {
560
+ // Digit separator. Skip it;
561
+ continue;
562
+ }
563
+ if (c >= 'a') {
564
+ // Uppercase (if a letter, otherwise some other punct):
565
+ c = (char)(c - ('a' - 'A'));
566
+ }
567
+ int digit = c - '0';
568
+ if (c >= 'A') {
569
+ // It's actually a letter (or garbage, which we'll catch later)
570
+ digit = (c - 'A') + 10;
571
+ }
572
+ if (digit > radix || digit < 0) {
573
+ // The digit is outside of our radix, or garbage
574
+ break;
575
+ }
576
+ ret = mlib_int128_mul(ret, MLIB_INT128_CAST(radix));
577
+ ret = mlib_int128_add(ret, MLIB_INT128_CAST(digit));
578
+ }
579
+ if (end) {
580
+ *end = s;
581
+ }
582
+ return ret;
634
583
  }
635
584
 
636
585
  /**
637
586
  * @brief Truncate a 128-bit number to a 64-bit number
638
587
  */
639
- static mlib_constexpr_fn uint64_t
640
- mlib_int128_to_u64 (mlib_int128 v)
641
- {
642
- return v.r.lo;
588
+ static mlib_constexpr_fn uint64_t mlib_int128_to_u64(mlib_int128 v) {
589
+ return v.r.lo;
643
590
  }
644
591
 
645
592
  /// The result type of formatting a 128-bit number
646
593
  typedef struct {
647
- /// The character array of the number as a base10 string. Null-terminated.
648
- char str[40];
594
+ /// The character array of the number as a base10 string. Null-terminated.
595
+ char str[40];
649
596
  } mlib_int128_charbuf;
650
597
 
651
598
  /**
@@ -653,31 +600,29 @@ typedef struct {
653
600
  *
654
601
  * @return mlib_int128_charbuf a struct containing a .str character array
655
602
  */
656
- static mlib_constexpr_fn mlib_int128_charbuf
657
- mlib_int128_format (mlib_int128 i)
658
- {
659
- mlib_int128_charbuf into = {{0}};
660
- char *out = into.str + (sizeof into) - 1;
661
- int len = 0;
662
- if (mlib_int128_eq (i, MLIB_INT128 (0))) {
663
- *out-- = '0';
664
- len = 1;
665
- }
666
- while (!mlib_int128_eq (i, MLIB_INT128 (0))) {
667
- mlib_int128_divmod_result dm = mlib_int128_divmod (i, MLIB_INT128 (10));
668
- uint64_t v = mlib_int128_to_u64 (dm.remainder);
669
- char digits[] = "0123456789";
670
- char d = digits[v];
671
- *out = d;
672
- --out;
673
- i = dm.quotient;
674
- ++len;
675
- }
676
- for (int i = 0; i < len; ++i) {
677
- into.str[i] = out[i + 1];
678
- }
679
- into.str[len] = 0;
680
- return into;
603
+ static mlib_constexpr_fn mlib_int128_charbuf mlib_int128_format(mlib_int128 i) {
604
+ mlib_int128_charbuf into = {{0}};
605
+ char *out = into.str + (sizeof into) - 1;
606
+ int len = 0;
607
+ if (mlib_int128_eq(i, MLIB_INT128(0))) {
608
+ *out-- = '0';
609
+ len = 1;
610
+ }
611
+ while (!mlib_int128_eq(i, MLIB_INT128(0))) {
612
+ mlib_int128_divmod_result dm = mlib_int128_divmod(i, MLIB_INT128(10));
613
+ uint64_t v = mlib_int128_to_u64(dm.remainder);
614
+ char digits[] = "0123456789";
615
+ char d = digits[v];
616
+ *out = d;
617
+ --out;
618
+ i = dm.quotient;
619
+ ++len;
620
+ }
621
+ for (int j = 0; j < len; ++j) {
622
+ into.str[j] = out[j + 1];
623
+ }
624
+ into.str[len] = 0;
625
+ return into;
681
626
  }
682
627
 
683
628
  MLIB_C_LINKAGE_END