@series-inc/rundot-syncplay 6.0.0-rc.2 → 6.0.0-rc.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (329) hide show
  1. package/README.md +133 -0
  2. package/core/deterministic-math.d.ts +75 -1
  3. package/core/native/instance_ledger.hpp +1095 -0
  4. package/core/native/legacy_checkpoint_ids.hpp +107 -0
  5. package/core/native/physics/abi.cpp +39 -211
  6. package/core/native/physics/abi.hpp +2 -6
  7. package/core/native/physics/articulation.cpp +1434 -0
  8. package/core/native/physics/articulation.hpp +279 -0
  9. package/core/native/physics/buoyancy.cpp +1588 -0
  10. package/core/native/physics/buoyancy.hpp +254 -0
  11. package/core/native/physics/capsule_triangle.cpp +168 -0
  12. package/core/native/physics/capsule_triangle.hpp +40 -0
  13. package/core/native/physics/capsule_triangle_native.inc +291 -0
  14. package/core/native/physics/certified_sum.hpp +218 -0
  15. package/core/native/physics/character.cpp +638 -0
  16. package/core/native/physics/character.hpp +169 -0
  17. package/core/native/physics/checkpoint_schema.hpp +379 -0
  18. package/core/native/physics/cloth.cpp +1401 -0
  19. package/core/native/physics/cloth.hpp +357 -0
  20. package/core/native/physics/constraint_rows.cpp +363 -2
  21. package/core/native/physics/constraint_rows.hpp +72 -1
  22. package/core/native/physics/deformable_query.cpp +1630 -0
  23. package/core/native/physics/deformable_query.hpp +578 -0
  24. package/core/native/physics/distance.cpp +1166 -0
  25. package/core/native/physics/distance.hpp +151 -0
  26. package/core/native/physics/exact/exact-applied.cpp +101 -0
  27. package/core/native/physics/exact/exact-applied.hpp +112 -0
  28. package/core/native/physics/exact/exact-dyadic.cpp +691 -0
  29. package/core/native/physics/exact/exact-dyadic.hpp +58 -0
  30. package/core/native/physics/exact/exact-operation.hpp +51 -0
  31. package/core/native/physics/exact/word-core.hpp +262 -0
  32. package/core/native/physics/extended_abi.cpp +1242 -0
  33. package/core/native/physics/extended_abi.hpp +355 -0
  34. package/core/native/physics/extended_abi_characters.cpp +261 -0
  35. package/core/native/physics/extended_abi_characters.hpp +434 -0
  36. package/core/native/physics/extended_abi_core_features.cpp +713 -0
  37. package/core/native/physics/extended_abi_core_features.hpp +226 -0
  38. package/core/native/physics/extended_abi_deformables.cpp +448 -0
  39. package/core/native/physics/extended_abi_deformables.hpp +867 -0
  40. package/core/native/physics/extended_abi_vehicles.cpp +411 -0
  41. package/core/native/physics/extended_abi_vehicles.hpp +222 -0
  42. package/core/native/physics/fracture.cpp +1981 -0
  43. package/core/native/physics/fracture.hpp +460 -0
  44. package/core/native/physics/gear.cpp +563 -0
  45. package/core/native/physics/gear.hpp +158 -0
  46. package/core/native/physics/geometry_memory.hpp +476 -0
  47. package/core/native/physics/geometry_store.cpp +547 -0
  48. package/core/native/physics/geometry_store.hpp +134 -0
  49. package/core/native/physics/lag_compensation.cpp +1626 -0
  50. package/core/native/physics/lag_compensation.hpp +405 -0
  51. package/core/native/physics/mass_properties.cpp +449 -0
  52. package/core/native/physics/mass_properties.hpp +23 -0
  53. package/core/native/physics/material_pose.hpp +988 -0
  54. package/core/native/physics/particles.cpp +1065 -0
  55. package/core/native/physics/particles.hpp +261 -0
  56. package/core/native/physics/path_joint.cpp +781 -0
  57. package/core/native/physics/path_joint.hpp +154 -0
  58. package/core/native/physics/physics.cpp +13043 -1065
  59. package/core/native/physics/physics.hpp +184 -8
  60. package/core/native/physics/planar_deformable.cpp +2433 -0
  61. package/core/native/physics/planar_deformable.hpp +889 -0
  62. package/core/native/physics/platformer_2d.cpp +1353 -0
  63. package/core/native/physics/platformer_2d.hpp +340 -0
  64. package/core/native/physics/powered_ragdoll.cpp +1100 -0
  65. package/core/native/physics/powered_ragdoll.hpp +279 -0
  66. package/core/native/physics/powertrain.cpp +586 -0
  67. package/core/native/physics/powertrain.hpp +180 -0
  68. package/core/native/physics/pulley.cpp +311 -0
  69. package/core/native/physics/pulley.hpp +121 -0
  70. package/core/native/physics/ragdoll.cpp +586 -0
  71. package/core/native/physics/ragdoll.hpp +221 -0
  72. package/core/native/physics/rope.cpp +692 -0
  73. package/core/native/physics/rope.hpp +215 -0
  74. package/core/native/physics/rounded_convex.cpp +1229 -0
  75. package/core/native/physics/rounded_convex.hpp +287 -0
  76. package/core/native/physics/sdf_geometry.cpp +1821 -0
  77. package/core/native/physics/sdf_geometry.hpp +474 -0
  78. package/core/native/physics/soft_body.cpp +1449 -0
  79. package/core/native/physics/soft_body.hpp +328 -0
  80. package/core/native/physics/soft_collision.cpp +1862 -0
  81. package/core/native/physics/soft_collision.hpp +561 -0
  82. package/core/native/physics/surface_velocity.cpp +366 -0
  83. package/core/native/physics/surface_velocity.hpp +243 -0
  84. package/core/native/physics/topology.cpp +357 -39
  85. package/core/native/physics/topology.hpp +97 -6
  86. package/core/native/physics/tracks.cpp +1179 -0
  87. package/core/native/physics/tracks.hpp +404 -0
  88. package/core/native/physics/vehicle.cpp +629 -0
  89. package/core/native/physics/vehicle.hpp +191 -0
  90. package/core/native/physics/voxel_cooking.cpp +1929 -0
  91. package/core/native/physics/voxel_cooking.hpp +395 -0
  92. package/core/native/physics/world.cpp +5290 -854
  93. package/core/native/physics/world.hpp +796 -24
  94. package/core/native/physics/world_abi.cpp +3840 -306
  95. package/core/native/physics/world_abi.hpp +530 -10
  96. package/core/native/physics/world_digest_visitor.hpp +277 -0
  97. package/core/native/physics/world_strong_digest.hpp +557 -0
  98. package/core/native/physics2d/ccd.cpp +1302 -0
  99. package/core/native/physics2d/ccd.hpp +141 -0
  100. package/core/native/physics2d/checkpoint_memory.hpp +349 -0
  101. package/core/native/physics2d/clock_primitives.hpp +138 -0
  102. package/core/native/physics2d/commands.cpp +174 -28
  103. package/core/native/physics2d/commands.hpp +3 -1
  104. package/core/native/physics2d/compound.cpp +581 -0
  105. package/core/native/physics2d/compound.hpp +248 -0
  106. package/core/native/physics2d/contour.cpp +325 -0
  107. package/core/native/physics2d/contour.hpp +143 -0
  108. package/core/native/physics2d/frame_scope.hpp +124 -0
  109. package/core/native/physics2d/joint_phases.hpp +93 -0
  110. package/core/native/physics2d/joints.cpp +587 -150
  111. package/core/native/physics2d/joints.hpp +5 -0
  112. package/core/native/physics2d/memory_private.hpp +125 -0
  113. package/core/native/physics2d/owned_abi.cpp +1354 -879
  114. package/core/native/physics2d/owned_abi.hpp +190 -52
  115. package/core/native/physics2d/phase_test_hooks.hpp +17 -0
  116. package/core/native/physics2d/physics2d.cpp +2073 -120
  117. package/core/native/physics2d/physics2d.hpp +154 -2
  118. package/core/native/physics2d/queries.cpp +1055 -79
  119. package/core/native/physics2d/queries.hpp +71 -5
  120. package/core/native/physics2d/world.cpp +1313 -353
  121. package/core/native/physics2d/world.hpp +124 -13
  122. package/core/native/sha256/sha256_compress_unsigned.hpp +68 -0
  123. package/core/native/sha256/sha256_fips_constants.hpp +24 -0
  124. package/core/native/sha256/sha256_length.hpp +56 -0
  125. package/core/native/sha256/sha256_memo.hpp +48 -0
  126. package/core/native/sha256/sha256_sink.hpp +234 -0
  127. package/core/physics2d-owned.d.ts +466 -126
  128. package/core/runtime.d.ts +192 -19
  129. package/core/scripts/build-native.mjs +34 -0
  130. package/core/scripts/build-wasm-physics.mjs +272 -23
  131. package/core/scripts/extended-physics-profile.mjs +634 -0
  132. package/core/scripts/generate-physics2d-vectors.mjs +59 -0
  133. package/core/src/kinetix-deterministic-math.d.ts +2 -0
  134. package/core/src/kinetix-deterministic-math.mjs +513 -27
  135. package/core/src/kinetix-owned-runtime-adapter.mjs +788 -0
  136. package/core/src/kinetix-runtime-adapter-utils.mjs +54 -6
  137. package/core/src/kinetix-runtime-contract.mjs +55 -9
  138. package/core/src/kinetix-runtime-identity.mjs +12 -1
  139. package/core/src/kinetix-session-config.mjs +7 -2
  140. package/core/src/kinetix-state-report.mjs +36 -0
  141. package/core/src/physics/abi-constants.mjs +5 -4
  142. package/core/src/physics/articulation.d.mts +194 -0
  143. package/core/src/physics/articulation.mjs +1141 -0
  144. package/core/src/physics/buoyancy.d.mts +143 -0
  145. package/core/src/physics/buoyancy.mjs +268 -0
  146. package/core/src/physics/cloth.d.mts +314 -0
  147. package/core/src/physics/cloth.mjs +1156 -0
  148. package/core/src/physics/deformable-query.d.mts +217 -0
  149. package/core/src/physics/deformable-query.mjs +1412 -0
  150. package/core/src/physics/distance.d.mts +160 -0
  151. package/core/src/physics/distance.mjs +175 -0
  152. package/core/src/physics/extended-wasm-instance.d.mts +5 -0
  153. package/core/src/physics/extended-wasm-instance.mjs +48 -0
  154. package/core/src/physics/feature-api.d.mts +74 -0
  155. package/core/src/physics/feature-api.mjs +98 -0
  156. package/core/src/physics/fracture.d.mts +133 -0
  157. package/core/src/physics/fracture.mjs +1324 -0
  158. package/core/src/physics/gear.d.mts +585 -0
  159. package/core/src/physics/gear.mjs +955 -0
  160. package/core/src/physics/lag-compensation.d.mts +101 -0
  161. package/core/src/physics/lag-compensation.mjs +1100 -0
  162. package/core/src/physics/mass-properties.d.mts +114 -0
  163. package/core/src/physics/mass-properties.mjs +137 -468
  164. package/core/src/physics/particles.d.mts +177 -0
  165. package/core/src/physics/particles.mjs +758 -0
  166. package/core/src/physics/path-joint.d.mts +171 -0
  167. package/core/src/physics/path-joint.mjs +1231 -0
  168. package/core/src/physics/physics-extended-wasm-module.generated.mjs +13 -0
  169. package/core/src/physics/physics-profile-wasm-module.generated.mjs +48 -0
  170. package/core/src/physics/physics-wasm-module.generated.mjs +34 -5
  171. package/core/src/physics/powered-ragdoll.d.mts +188 -0
  172. package/core/src/physics/powered-ragdoll.mjs +1043 -0
  173. package/core/src/physics/powertrain.d.mts +260 -0
  174. package/core/src/physics/powertrain.mjs +621 -0
  175. package/core/src/physics/pulley.d.mts +98 -0
  176. package/core/src/physics/pulley.mjs +686 -0
  177. package/core/src/physics/ragdoll.d.mts +289 -0
  178. package/core/src/physics/ragdoll.mjs +1323 -0
  179. package/core/src/physics/rope.d.mts +241 -0
  180. package/core/src/physics/rope.mjs +319 -0
  181. package/core/src/physics/rounded-convex.d.mts +328 -0
  182. package/core/src/physics/rounded-convex.mjs +1404 -0
  183. package/core/src/physics/sdf-geometry.d.mts +117 -0
  184. package/core/src/physics/sdf-geometry.mjs +1204 -0
  185. package/core/src/physics/soft-body.d.mts +144 -0
  186. package/core/src/physics/soft-body.mjs +1285 -0
  187. package/core/src/physics/soft-collision.d.mts +651 -0
  188. package/core/src/physics/soft-collision.mjs +1883 -0
  189. package/core/src/physics/surface-velocity.d.mts +156 -0
  190. package/core/src/physics/surface-velocity.mjs +722 -0
  191. package/core/src/physics/tracks.d.mts +411 -0
  192. package/core/src/physics/tracks.mjs +1074 -0
  193. package/core/src/physics/vehicle.d.mts +3 -0
  194. package/core/src/physics/vehicle.mjs +172 -0
  195. package/core/src/physics/voxel-cooking.d.mts +219 -0
  196. package/core/src/physics/voxel-cooking.mjs +1499 -0
  197. package/core/src/physics/voxel-swept-quad.mjs +110 -0
  198. package/core/src/physics/wasm-instance.mjs +160 -23
  199. package/core/src/physics/world.d.mts +62 -0
  200. package/core/src/physics/world.mjs +1947 -152
  201. package/core/src/physics2d/body-state.mjs +218 -15
  202. package/core/src/physics2d/collision.d.mts +52 -0
  203. package/core/src/physics2d/collision.mjs +234 -0
  204. package/core/src/physics2d/compound.mjs +454 -0
  205. package/core/src/physics2d/contour-state.mjs +771 -0
  206. package/core/src/physics2d/feature-api.d.mts +211 -0
  207. package/core/src/physics2d/feature-api.mjs +3 -0
  208. package/core/src/physics2d/fluid-state.mjs +145 -0
  209. package/core/src/physics2d/joint-state.mjs +1 -1
  210. package/core/src/physics2d/owned-queries.mjs +3 -1
  211. package/core/src/physics2d/owned-transport.mjs +268 -122
  212. package/core/src/physics2d/owned-world.mjs +353 -230
  213. package/core/src/physics2d/particle-fluid.d.mts +152 -0
  214. package/core/src/physics2d/particle-fluid.mjs +233 -0
  215. package/core/src/physics2d/physics2d-wasm-module.generated.mjs +27 -10
  216. package/core/src/physics2d/query-codec.mjs +61 -13
  217. package/core/src/physics2d/submerged-geometry.d.mts +51 -0
  218. package/core/src/physics2d/submerged-geometry.mjs +186 -0
  219. package/core/src/runtime.mjs +23 -0
  220. package/core/src/shared/bytes.d.mts +2 -0
  221. package/core/src/shared/bytes.mjs +33 -0
  222. package/core/src/shared/sha256.d.mts +3 -0
  223. package/core/src/shared/sha256.mjs +72 -22
  224. package/core/units.d.ts +50 -0
  225. package/core/world.d.ts +253 -5
  226. package/dist/authority-room.d.ts +9 -0
  227. package/dist/authority-room.js +45 -16
  228. package/dist/browser.d.ts +6 -5
  229. package/dist/browser.js +4 -3
  230. package/dist/cadence.d.ts +7 -0
  231. package/dist/cadence.js +19 -0
  232. package/dist/canonical.d.ts +1 -0
  233. package/dist/canonical.js +3 -0
  234. package/dist/checksum-policy.d.ts +10 -0
  235. package/dist/checksum-policy.js +14 -0
  236. package/dist/collider-cooking-hull.d.ts +18 -0
  237. package/dist/collider-cooking-hull.js +248 -0
  238. package/dist/collider-cooking.d.ts +140 -0
  239. package/dist/collider-cooking.js +794 -0
  240. package/dist/compat/v5.d.ts +121 -0
  241. package/dist/compat/v5.js +479 -0
  242. package/dist/creator.d.ts +7 -6
  243. package/dist/creator.js +4 -3
  244. package/dist/game-byte-section.d.ts +36 -0
  245. package/dist/game-byte-section.js +350 -0
  246. package/dist/index.d.ts +13 -6
  247. package/dist/index.js +9 -4
  248. package/dist/instant-replay.d.ts +27 -2
  249. package/dist/instant-replay.js +91 -16
  250. package/dist/lag-compensation3d.js +11 -10
  251. package/dist/match-log.d.ts +21 -4
  252. package/dist/match-log.js +92 -9
  253. package/dist/math.d.ts +2 -0
  254. package/dist/math.js +1 -0
  255. package/dist/modules/character-3d.d.ts +16 -1
  256. package/dist/modules/character-3d.js +42 -5
  257. package/dist/modules/combat.js +6 -5
  258. package/dist/movement3d-revision1.d.ts +2 -0
  259. package/dist/movement3d-revision1.js +973 -0
  260. package/dist/movement3d.d.ts +53 -3
  261. package/dist/movement3d.js +917 -247
  262. package/dist/networked-client.d.ts +15 -0
  263. package/dist/networked-client.js +378 -105
  264. package/dist/node.d.ts +3 -2
  265. package/dist/node.js +2 -1
  266. package/dist/physics/2d.d.ts +38 -4
  267. package/dist/physics/2d.js +57 -36
  268. package/dist/physics/3d.d.ts +755 -111
  269. package/dist/physics/3d.js +2355 -158
  270. package/dist/physics/edit-result-graph.d.ts +36 -0
  271. package/dist/physics/edit-result-graph.js +141 -0
  272. package/dist/physics/game-codec3d.d.ts +41 -0
  273. package/dist/physics/game-codec3d.js +544 -0
  274. package/dist/physics/installed-checkpoint3d.d.ts +13 -0
  275. package/dist/physics/installed-checkpoint3d.js +120 -0
  276. package/dist/physics/installed-runtime3d.d.ts +51 -0
  277. package/dist/physics/installed-runtime3d.js +372 -0
  278. package/dist/physics/installed-state-commitment3d.d.ts +2 -0
  279. package/dist/physics/installed-state-commitment3d.js +24 -0
  280. package/dist/physics/units.d.ts +38 -0
  281. package/dist/physics/units.js +56 -0
  282. package/dist/physics/vehicle-state3d.d.ts +93 -0
  283. package/dist/physics/vehicle-state3d.js +1191 -0
  284. package/dist/physics/voxel-body-state.d.ts +61 -0
  285. package/dist/physics/voxel-body-state.js +410 -0
  286. package/dist/r3f-render-adapter.d.ts +59 -1
  287. package/dist/r3f-render-adapter.js +137 -2
  288. package/dist/replay.d.ts +13 -0
  289. package/dist/replay.js +95 -70
  290. package/dist/run-room-transport.js +5 -1
  291. package/dist/runner.d.ts +17 -2
  292. package/dist/runner.js +472 -84
  293. package/dist/runtime-cleanup.d.ts +54 -0
  294. package/dist/runtime-cleanup.js +266 -0
  295. package/dist/runtime-lease.d.ts +11 -1
  296. package/dist/runtime-lease.js +23 -10
  297. package/dist/runtime-room-metadata.d.ts +2 -8
  298. package/dist/runtime-room-metadata.js +11 -14
  299. package/dist/runtime-session.d.ts +8 -0
  300. package/dist/runtime-session.js +222 -58
  301. package/dist/schema-codegen.js +3 -0
  302. package/dist/schema-dsl-binary.d.ts +10 -4
  303. package/dist/schema-dsl-binary.js +375 -76
  304. package/dist/scratch-pool.d.ts +32 -0
  305. package/dist/scratch-pool.js +60 -0
  306. package/dist/sdk-session.d.ts +1 -0
  307. package/dist/sdk-session.js +4 -0
  308. package/dist/secret-config.js +2 -60
  309. package/dist/secret-crypto.js +70 -36
  310. package/dist/session-snapshot.d.ts +15 -4
  311. package/dist/session-snapshot.js +35 -33
  312. package/dist/session-wire.js +14 -22
  313. package/dist/sparse-set.d.ts +1 -0
  314. package/dist/sparse-set.js +22 -0
  315. package/dist/spatial/spatial-grid-3d.d.ts +41 -0
  316. package/dist/spatial/spatial-grid-3d.js +115 -0
  317. package/dist/synctest.d.ts +9 -0
  318. package/dist/synctest.js +106 -17
  319. package/dist/testing-network.d.ts +2 -0
  320. package/dist/testing-network.js +122 -18
  321. package/dist/testing.d.ts +27 -6
  322. package/dist/testing.js +297 -56
  323. package/dist/tools/index.d.ts +3 -2
  324. package/dist/tools/index.js +3 -2
  325. package/dist/tools/module-certification.d.ts +6 -0
  326. package/dist/tools/module-certification.js +26 -2
  327. package/package.json +18 -5
  328. package/core/scripts/run-box3d-benchmark.mjs +0 -350
  329. package/core/src/physics/index.mjs +0 -3169
@@ -0,0 +1,691 @@
1
+ #include "exact-dyadic.hpp"
2
+ #include "word-core.hpp"
3
+ #include "exact-operation.hpp"
4
+ #include <utility>
5
+
6
+ namespace exact_dyadic {
7
+ bool EncodingView::valid() const noexcept {
8
+ if ((!exponentMagnitude.empty() && exponentMagnitude.back() == 0)
9
+ || (!oddMagnitude.empty() && oddMagnitude.back() == 0)) return false;
10
+ switch (exponentSign) {
11
+ case Sign::Zero: if (!exponentMagnitude.empty()) return false; break;
12
+ case Sign::Positive: case Sign::Negative: if (exponentMagnitude.empty()) return false; break;
13
+ default: return false;
14
+ }
15
+ switch (sign) {
16
+ case Sign::Zero: return exponentSign == Sign::Zero && oddMagnitude.empty();
17
+ case Sign::Positive: case Sign::Negative:
18
+ return !oddMagnitude.empty() && (oddMagnitude.front() & 1) != 0;
19
+ }
20
+ return false;
21
+ }
22
+ exact_applied::Status Operation::addAppliedAtom(const exact_applied::Value& current,
23
+ const Atom& atom, exact_applied::Value& output) {
24
+ return exact_word_private::addAppliedAtom(current.value_, atom, output.value_, memory_)
25
+ ? exact_applied::Status::Ok : exact_applied::Status::ResourceExhausted;
26
+ }
27
+
28
+ namespace {
29
+ using namespace exact_word_private;
30
+ using Exponent = SignedExponentEncoding;
31
+ Sign opposite(Sign sign) { return sign == Sign::Positive ? Sign::Negative : sign == Sign::Negative ? Sign::Positive : Sign::Zero; }
32
+ bool validDyadic(const Encoding& value) {
33
+ return EncodingView{value.sign, value.exponent.sign,
34
+ value.exponent.magnitude, value.oddMagnitude}.valid();
35
+ }
36
+ bool cloneExponent(const Exponent& a, Exponent& out, Memory& memory) {
37
+ out.sign = a.sign;
38
+ return copy(a.magnitude, out.magnitude, memory);
39
+ }
40
+ bool cloneDyadic(const Encoding& a, Encoding& out, Memory& memory) {
41
+ out.sign = a.sign;
42
+ return cloneExponent(a.exponent, out.exponent, memory) && copy(a.oddMagnitude, out.oddMagnitude, memory);
43
+ }
44
+ int compareExponent(const Exponent& a, const Exponent& b) {
45
+ const auto rank = [](Sign s) { return s == Sign::Negative ? -1 : s == Sign::Positive ? 1 : 0; };
46
+ if (a.sign != b.sign) return rank(a.sign) < rank(b.sign) ? -1 : 1;
47
+ const auto result = compare(a.magnitude, b.magnitude);
48
+ return a.sign == Sign::Negative ? -result : result;
49
+ }
50
+ bool addExponent(const Exponent& a, const Exponent& b, Exponent& out, Memory& memory) {
51
+ if (a.sign == Sign::Zero) return cloneExponent(b, out, memory);
52
+ if (b.sign == Sign::Zero) return cloneExponent(a, out, memory);
53
+ if (a.sign == b.sign) {
54
+ out.sign = a.sign;
55
+ return exact_word_private::sum(a.magnitude, b.magnitude, out.magnitude, memory);
56
+ }
57
+ const auto order = compare(a.magnitude, b.magnitude);
58
+ if (order == 0) return true;
59
+ out.sign = order > 0 ? a.sign : b.sign;
60
+ return order > 0 ? difference(a.magnitude, b.magnitude, out.magnitude, memory)
61
+ : difference(b.magnitude, a.magnitude, out.magnitude, memory);
62
+ }
63
+ bool exponentFromApplied(const exact_applied::Encoding& a, Exponent& out, Memory& memory) {
64
+ Exponent offset, quantum;
65
+ offset.sign = a.lowBitOffset.empty() ? Sign::Zero : Sign::Positive;
66
+ if (!copy(a.lowBitOffset, offset.magnitude, memory) || !natural(1074, quantum.magnitude, memory)) return false;
67
+ quantum.sign = Sign::Negative;
68
+ return addExponent(offset, quantum, out, memory);
69
+ }
70
+ bool lift(const exact_applied::Encoding& a, Encoding& out, Memory& memory) {
71
+ if (a.sign == Sign::Zero) return true;
72
+ out.sign = a.sign;
73
+ return exponentFromApplied(a, out.exponent, memory) && copy(a.oddMagnitude, out.oddMagnitude, memory);
74
+ }
75
+ bool atomDyadic(const Atom& atom, Encoding& out, Memory& memory) {
76
+ exact_applied::Encoding a;
77
+ return decode(atom, a, memory) && lift(a, out, memory);
78
+ }
79
+ bool normalizeDyadic(Encoding& value, Memory& memory) {
80
+ std::array<std::uint64_t, 2> removed;
81
+ if (!removeLowZeros(value.oddMagnitude, removed)) return false;
82
+ if (value.oddMagnitude.empty()) { value = {}; return true; }
83
+ if (removed == std::array<std::uint64_t, 2>{}) return true;
84
+ Exponent count, exponent;
85
+ count.sign = Sign::Positive;
86
+ if (!memory.resize(count.magnitude, removed[1] == 0 ? 1 : 2)) return false;
87
+ count.magnitude[0] = removed[0];
88
+ if (count.magnitude.size() == 2) count.magnitude[1] = removed[1];
89
+ if (!addExponent(value.exponent, count, exponent, memory)) return false;
90
+ value.exponent = std::move(exponent);
91
+ return true;
92
+ }
93
+ bool productDyadic(const Encoding& a, const Encoding& b, Encoding& out, Memory& memory) {
94
+ if (a.sign == Sign::Zero || b.sign == Sign::Zero) return true;
95
+ out.sign = a.sign == b.sign ? Sign::Positive : Sign::Negative;
96
+ return multiply(a.oddMagnitude, b.oddMagnitude, out.oddMagnitude, memory)
97
+ && addExponent(a.exponent, b.exponent, out.exponent, memory) && normalizeDyadic(out, memory);
98
+ }
99
+ bool sumDyadic(const Encoding& a, const Encoding& b, Encoding& out, Memory& memory) {
100
+ if (a.sign == Sign::Zero) return cloneDyadic(b, out, memory);
101
+ if (b.sign == Sign::Zero) return cloneDyadic(a, out, memory);
102
+ if (a.sign != b.sign && a.exponent == b.exponent && a.oddMagnitude == b.oddMagnitude) return true;
103
+ const auto order = compareExponent(a.exponent, b.exponent);
104
+ const auto& lower = order <= 0 ? a : b;
105
+ const auto& higher = order <= 0 ? b : a;
106
+ Exponent negativeLower, distance;
107
+ if (!cloneExponent(lower.exponent, negativeLower, memory)) return false;
108
+ negativeLower.sign = opposite(negativeLower.sign);
109
+ Words low, high;
110
+ if (!addExponent(higher.exponent, negativeLower, distance, memory)
111
+ || !cloneExponent(lower.exponent, out.exponent, memory)
112
+ || !copy(lower.oddMagnitude, low, memory)
113
+ || !leftShift(higher.oddMagnitude, distance.magnitude, high, memory)) return false;
114
+ if (a.sign == b.sign) {
115
+ out.sign = a.sign;
116
+ if (!exact_word_private::sum(low, high, out.oddMagnitude, memory)) return false;
117
+ } else {
118
+ const auto magnitudeOrder = compare(low, high);
119
+ out.sign = magnitudeOrder >= 0 ? lower.sign : higher.sign;
120
+ if (!(magnitudeOrder >= 0 ? difference(low, high, out.oddMagnitude, memory)
121
+ : difference(high, low, out.oddMagnitude, memory))) return false;
122
+ }
123
+ return normalizeDyadic(out, memory);
124
+ }
125
+ Status roundDyadic(const Encoding& value, std::uint64_t& output) {
126
+ if (value.sign == Sign::Zero) { output = 0; return Status::Ok; }
127
+ const auto signBit = value.sign == Sign::Negative ? UINT64_C(0x8000000000000000) : 0;
128
+ const auto size = value.oddMagnitude.size();
129
+ const auto highBits = 64 - static_cast<unsigned>(std::countl_zero(value.oddMagnitude.back()));
130
+ // The magnitude top index uses two stack words. It is not a size_t bit index.
131
+ const auto count = static_cast<std::uint64_t>(size - 1);
132
+ const std::array<std::uint64_t, 2> index{(count << 6) | (highBits - 1), count >> 58};
133
+ const auto& exponent = value.exponent;
134
+ const auto e = exponent.magnitude.empty() ? 0 : exponent.magnitude[0];
135
+ int top;
136
+ if (exponent.sign != Sign::Negative) {
137
+ if (index[1] != 0 || exponent.magnitude.size() > 1 || e > 1023 || index[0] > 1023 - e) return Status::OutputOverflow;
138
+ top = static_cast<int>(e + index[0]);
139
+ } else {
140
+ if (exponent.magnitude.size() > 2) { output = signBit; return Status::Ok; }
141
+ const std::array<std::uint64_t, 2> power{e, exponent.magnitude.size() == 2 ? exponent.magnitude[1] : 0};
142
+ const bool below = power[1] > index[1] || (power[1] == index[1] && power[0] > index[0]);
143
+ const auto& higher = below ? power : index;
144
+ const auto& lower = below ? index : power;
145
+ const auto low = higher[0] - lower[0];
146
+ const auto high = higher[1] - lower[1] - static_cast<std::uint64_t>(higher[0] < lower[0]);
147
+ if (high != 0 || low > (below ? 1075u : 1023u)) {
148
+ if (!below) return Status::OutputOverflow;
149
+ output = signBit; return Status::Ok;
150
+ }
151
+ top = below ? -static_cast<int>(low) : static_cast<int>(low);
152
+ }
153
+ const auto quantum = top >= -1022 ? top - 52 : -1074;
154
+ const auto retained = top - quantum;
155
+ std::uint64_t significand = 0;
156
+ if (retained < 0) {
157
+ // top == -1075. The odd magnitude distinguishes the exact half from a sticky tail.
158
+ if (value.oddMagnitude.size() != 1 || value.oddMagnitude[0] != 1) significand = 1;
159
+ } else if (size > 1 || highBits - 1 >= static_cast<unsigned>(retained)) {
160
+ auto removedWord = size - 1;
161
+ auto removedBit = highBits - 1;
162
+ const auto r = static_cast<unsigned>(retained);
163
+ if (r > removedBit) { --removedWord; removedBit += 64; }
164
+ removedBit -= r;
165
+ for (unsigned i = 0; i <= r; ++i) {
166
+ const auto position = removedBit + i;
167
+ if (((value.oddMagnitude[removedWord + position / 64] >> (position % 64)) & 1) != 0)
168
+ significand |= UINT64_C(1) << i;
169
+ }
170
+ if (removedWord != 0 || removedBit != 0) {
171
+ auto guardWord = removedWord;
172
+ auto guardBit = removedBit;
173
+ if (guardBit == 0) { --guardWord; guardBit = 64; }
174
+ --guardBit;
175
+ const bool guard = ((value.oddMagnitude[guardWord] >> guardBit) & 1) != 0;
176
+ bool sticky = guardBit != 0 && (value.oddMagnitude[guardWord] & ((UINT64_C(1) << guardBit) - 1)) != 0;
177
+ for (std::size_t i = 0; i < guardWord; ++i) sticky = value.oddMagnitude[i] != 0 || sticky;
178
+ if (guard && (sticky || (significand & 1) != 0)) ++significand;
179
+ }
180
+ } else {
181
+ const auto left = static_cast<unsigned>(retained) - (highBits - 1);
182
+ significand = value.oddMagnitude[0] << left;
183
+ }
184
+ auto roundedTop = top;
185
+ if (top >= -1022 && significand == (UINT64_C(1) << 53)) { significand >>= 1; ++roundedTop; }
186
+ if (roundedTop > 1023) return Status::OutputOverflow;
187
+ if (top < -1022) { output = signBit | significand; return Status::Ok; }
188
+ output = signBit | (std::uint64_t{static_cast<unsigned>(roundedTop + 1023)} << 52)
189
+ | (significand & ((UINT64_C(1) << 52) - 1));
190
+ return Status::Ok;
191
+ }
192
+ // These addresses describe borrowed words. They do not store another value.
193
+ bool smallExponent(std::size_t value, Sign sign, Exponent& out, Memory& memory) {
194
+ out.sign = value == 0 ? Sign::Zero : sign;
195
+ return natural(value, out.magnitude, memory);
196
+ }
197
+ bool addSmall(const Exponent& base, std::size_t count, Sign sign, Exponent& out, Memory& memory) {
198
+ Exponent step;
199
+ return smallExponent(count, sign, step, memory) && addExponent(base, step, out, memory);
200
+ }
201
+ bool floorWordAddress(const Exponent& power, Exponent& address, unsigned& remainder, Memory& memory) {
202
+ remainder = power.magnitude.empty() ? 0 : static_cast<unsigned>(power.magnitude[0] & 63);
203
+ if (!memory.resize(address.magnitude, power.magnitude.size())) return false;
204
+ for (std::size_t i = 0; i < power.magnitude.size(); ++i) {
205
+ address.magnitude[i] = power.magnitude[i] >> 6;
206
+ if (i + 1 < power.magnitude.size()) address.magnitude[i] |= power.magnitude[i + 1] << 58;
207
+ }
208
+ trim(address.magnitude);
209
+ address.sign = address.magnitude.empty() ? Sign::Zero : power.sign;
210
+ if (power.sign == Sign::Negative && remainder != 0) {
211
+ Exponent adjusted;
212
+ if (!addSmall(address, 1, Sign::Negative, adjusted, memory)) return false;
213
+ address = std::move(adjusted);
214
+ remainder = 64 - remainder;
215
+ }
216
+ return true;
217
+ }
218
+ std::uint64_t shiftedWord(const Words& words, std::size_t index, unsigned shift) {
219
+ auto word = index < words.size() ? words[index] << shift : 0;
220
+ if (shift != 0 && index != 0 && index - 1 < words.size()) word |= words[index - 1] >> (64 - shift);
221
+ return word;
222
+ }
223
+ bool shiftedCount(const Words& words, unsigned shift, std::size_t& count) {
224
+ count = words.size();
225
+ if (!words.empty() && shift != 0 && (words.back() >> (64 - shift)) != 0) {
226
+ if (count == std::numeric_limits<std::size_t>::max()) return false;
227
+ ++count;
228
+ }
229
+ return true;
230
+ }
231
+ struct WordRange {
232
+ const Words* words = nullptr;
233
+ Exponent address;
234
+ unsigned shift = 0;
235
+ std::size_t index = 0;
236
+ std::size_t count = 0;
237
+ bool active() const { return index < count; }
238
+ };
239
+ bool magnitudeSumComparison(const Encoding& left, const Encoding& a, const Encoding& b,
240
+ int& output, Memory& memory) {
241
+ const std::array<const Encoding*, 3> values{&left, &a, &b};
242
+ std::array<WordRange, 3> ranges;
243
+ for (unsigned i = 0; i < 3; ++i) {
244
+ ranges[i].words = &values[i]->oddMagnitude;
245
+ if (values[i]->sign == Sign::Zero) continue;
246
+ if (!floorWordAddress(values[i]->exponent, ranges[i].address, ranges[i].shift, memory)
247
+ || !shiftedCount(*ranges[i].words, ranges[i].shift, ranges[i].count)) return false;
248
+ }
249
+ bool carry = false;
250
+ Exponent carryAddress;
251
+ int mostSignificantDifference = 0;
252
+ for (;;) {
253
+ const Exponent* next = carry ? &carryAddress : nullptr;
254
+ for (const auto& range : ranges)
255
+ if (range.active() && (next == nullptr || compareExponent(range.address, *next) < 0)) next = &range.address;
256
+ if (next == nullptr) break;
257
+ Exponent current;
258
+ if (!cloneExponent(*next, current, memory)) return false;
259
+ std::array<std::uint64_t, 3> words{};
260
+ for (unsigned i = 0; i < 3; ++i)
261
+ if (ranges[i].active() && compareExponent(ranges[i].address, current) == 0)
262
+ words[i] = shiftedWord(*ranges[i].words, ranges[i].index, ranges[i].shift);
263
+ const bool incoming = carry && compareExponent(carryAddress, current) == 0;
264
+ const auto first = words[1] + words[2];
265
+ const auto total = first + static_cast<std::uint64_t>(incoming);
266
+ carry = first < words[1] || total < first;
267
+ if (words[0] != total) mostSignificantDifference = words[0] < total ? -1 : 1;
268
+ for (auto& range : ranges)
269
+ if (range.active() && compareExponent(range.address, current) == 0) {
270
+ ++range.index;
271
+ if (range.active()) {
272
+ Exponent advanced;
273
+ if (!addSmall(current, 1, Sign::Positive, advanced, memory)) return false;
274
+ range.address = std::move(advanced);
275
+ }
276
+ }
277
+ if (carry) {
278
+ Exponent advanced;
279
+ if (!addSmall(current, 1, Sign::Positive, advanced, memory)) return false;
280
+ carryAddress = std::move(advanced);
281
+ }
282
+ }
283
+ output = mostSignificantDifference;
284
+ return true;
285
+ }
286
+ bool magnitudeComparison(const Encoding& a, const Encoding& b, int& output, Memory& memory) {
287
+ const Encoding zero;
288
+ return magnitudeSumComparison(a, b, zero, output, memory);
289
+ }
290
+ bool topIndex(const Words& value, Exponent& out, Memory& memory) {
291
+ const auto count = static_cast<std::uint64_t>(value.size() - 1);
292
+ if (!memory.resize(out.magnitude, 2)) return false;
293
+ out.magnitude[0] = (count << 6) | (63 - static_cast<unsigned>(std::countl_zero(value.back())));
294
+ out.magnitude[1] = count >> 58;
295
+ trim(out.magnitude);
296
+ out.sign = out.magnitude.empty() ? Sign::Zero : Sign::Positive;
297
+ return true;
298
+ }
299
+ int compareShifted(const Words& a, unsigned shiftA, const Words& b, unsigned shiftB) {
300
+ std::size_t countA, countB;
301
+ // The bounded quotient path checks the possible extra word before this call.
302
+ shiftedCount(a, shiftA, countA);
303
+ shiftedCount(b, shiftB, countB);
304
+ if (countA != countB) return countA < countB ? -1 : 1;
305
+ for (std::size_t i = countA; i != 0; --i) {
306
+ const auto av = shiftedWord(a, i - 1, shiftA);
307
+ const auto bv = shiftedWord(b, i - 1, shiftB);
308
+ if (av != bv) return av < bv ? -1 : 1;
309
+ }
310
+ return 0;
311
+ }
312
+ void subtractShiftedInPlace(Words& remainder, const Words& divisor, unsigned shift) {
313
+ std::uint64_t borrow = 0;
314
+ for (std::size_t i = 0; i < remainder.size(); ++i) {
315
+ const auto d = shiftedWord(divisor, i, shift);
316
+ const auto first = remainder[i] - d;
317
+ const auto second = first - borrow;
318
+ borrow = static_cast<std::uint64_t>(remainder[i] < d || first < borrow);
319
+ remainder[i] = second;
320
+ }
321
+ trim(remainder);
322
+ }
323
+ RatioReadStatus roundRatio(const Encoding& numerator, const Encoding& denominator,
324
+ std::uint64_t& output, Memory& memory) {
325
+ if (denominator.sign != Sign::Positive) return RatioReadStatus::InvalidDenominator;
326
+ if (numerator.sign == Sign::Zero) { output = 0; return RatioReadStatus::Ok; }
327
+ const auto signBit = numerator.sign == Sign::Negative ? UINT64_C(0x8000000000000000) : 0;
328
+ Exponent negativeD, delta, indexN, indexD, indexDifference, top;
329
+ if (!cloneExponent(denominator.exponent, negativeD, memory)) return RatioReadStatus::ResourceExhausted;
330
+ negativeD.sign = opposite(negativeD.sign);
331
+ if (!addExponent(numerator.exponent, negativeD, delta, memory)
332
+ || !topIndex(numerator.oddMagnitude, indexN, memory)
333
+ || !topIndex(denominator.oddMagnitude, indexD, memory)) return RatioReadStatus::ResourceExhausted;
334
+ indexD.sign = opposite(indexD.sign);
335
+ if (!addExponent(indexN, indexD, indexDifference, memory)
336
+ || !addExponent(delta, indexDifference, top, memory)) return RatioReadStatus::ResourceExhausted;
337
+ Encoding normalizedN, normalizedD;
338
+ normalizedN.sign = normalizedD.sign = Sign::Positive;
339
+ if (!copy(numerator.oddMagnitude, normalizedN.oddMagnitude, memory)
340
+ || !copy(denominator.oddMagnitude, normalizedD.oddMagnitude, memory)
341
+ || !cloneExponent(indexDifference, normalizedD.exponent, memory)) return RatioReadStatus::ResourceExhausted;
342
+ int mantissaOrder;
343
+ if (!magnitudeComparison(normalizedN, normalizedD, mantissaOrder, memory)) return RatioReadStatus::ResourceExhausted;
344
+ if (mantissaOrder < 0) {
345
+ Exponent adjusted;
346
+ if (!addSmall(top, 1, Sign::Negative, adjusted, memory)) return RatioReadStatus::ResourceExhausted;
347
+ top = std::move(adjusted);
348
+ }
349
+ const auto topLow = top.magnitude.empty() ? 0 : top.magnitude[0];
350
+ if (top.sign == Sign::Negative && (top.magnitude.size() > 1 || topLow > 1075)) {
351
+ output = signBit; return RatioReadStatus::Ok;
352
+ }
353
+ if (top.sign == Sign::Positive && (top.magnitude.size() > 1 || topLow > 1023))
354
+ return RatioReadStatus::OutputOverflow;
355
+ const int machineTop = top.sign == Sign::Negative ? -static_cast<int>(topLow) : static_cast<int>(topLow);
356
+ const int quantum = machineTop >= -1022 ? machineTop - 52 : -1074;
357
+ Exponent scale;
358
+ if (!addSmall(delta, static_cast<std::size_t>(quantum < 0 ? -quantum : quantum),
359
+ quantum < 0 ? Sign::Positive : Sign::Negative, scale, memory)) return RatioReadStatus::ResourceExhausted;
360
+ Words remainder, divisor;
361
+ if (scale.sign == Sign::Negative) {
362
+ if (!copy(numerator.oddMagnitude, remainder, memory)
363
+ || !leftShift(denominator.oddMagnitude, scale.magnitude, divisor, memory)) return RatioReadStatus::ResourceExhausted;
364
+ } else {
365
+ if (!leftShift(numerator.oddMagnitude, scale.magnitude, remainder, memory)
366
+ || !copy(denominator.oddMagnitude, divisor, memory)) return RatioReadStatus::ResourceExhausted;
367
+ }
368
+ if (divisor.size() == std::numeric_limits<std::size_t>::max()
369
+ || remainder.size() == std::numeric_limits<std::size_t>::max()) return RatioReadStatus::ResourceExhausted;
370
+ std::uint64_t significand = 0;
371
+ for (unsigned bit = 53; bit != 0; --bit) {
372
+ const auto shift = bit - 1;
373
+ if (compareShifted(remainder, 0, divisor, shift) >= 0) {
374
+ subtractShiftedInPlace(remainder, divisor, shift);
375
+ significand |= UINT64_C(1) << shift;
376
+ }
377
+ }
378
+ const auto halfway = compareShifted(remainder, 1, divisor, 0);
379
+ if (halfway > 0 || (halfway == 0 && (significand & 1) != 0)) ++significand;
380
+ auto roundedTop = machineTop;
381
+ if (machineTop >= -1022 && significand == (UINT64_C(1) << 53)) { significand >>= 1; ++roundedTop; }
382
+ if (roundedTop > 1023) return RatioReadStatus::OutputOverflow;
383
+ if (machineTop < -1022) { output = signBit | significand; return RatioReadStatus::Ok; }
384
+ output = signBit | (std::uint64_t{static_cast<unsigned>(roundedTop + 1023)} << 52)
385
+ | (significand & ((UINT64_C(1) << 52) - 1));
386
+ return RatioReadStatus::Ok;
387
+ }
388
+ }
389
+ Dyadic::Dyadic(Dyadic&& source) noexcept : value_(std::move(source.value_)) { source.value_ = {}; }
390
+ Dyadic& Dyadic::operator=(Dyadic&& source) noexcept {
391
+ if (this != &source) { value_ = std::move(source.value_); source.value_ = {}; }
392
+ return *this;
393
+ }
394
+ Status Adapter::validateBasis(const std::array<std::array<std::uint64_t, 3>, 3>& input, OriginalBasis& output) {
395
+ OriginalBasis staged;
396
+ for (unsigned row = 0; row < 3; ++row)
397
+ for (unsigned axis = 0; axis < 3; ++axis) {
398
+ const auto status = Atom::validate(input[row][axis], staged.rows[row][axis]);
399
+ if (status != Status::Ok) return status;
400
+ }
401
+ output = staged; return Status::Ok;
402
+ }
403
+ Status Adapter::import(const Encoding& input, Dyadic& output, Budget budget) {
404
+ Operation operation(budget);
405
+ return operation.import(input, output);
406
+ }
407
+ Status Adapter::capture(const Dyadic& input, Encoding& output, Budget budget) {
408
+ Operation operation(budget);
409
+ return operation.capture(input, output);
410
+ }
411
+ Status Adapter::liftApplied(const exact_applied::Value& input, Dyadic& output, Budget budget) {
412
+ Operation operation(budget);
413
+ return operation.liftApplied(input, output);
414
+ }
415
+ Status Adapter::product(const Atom& a, const Atom& b, Dyadic& output, Budget budget) {
416
+ Operation operation(budget);
417
+ Dyadic first, second;
418
+ if (operation.fromAtom(a, first) != Status::Ok || operation.fromAtom(b, second) != Status::Ok)
419
+ return Status::ResourceExhausted;
420
+ return operation.product(first, second, output);
421
+ }
422
+ Status Adapter::product(const Dyadic& a, const Dyadic& b, Dyadic& output, Budget budget) {
423
+ Operation operation(budget);
424
+ return operation.product(a, b, output);
425
+ }
426
+ Status Adapter::sum(const Dyadic& a, const Dyadic& b, Dyadic& output, Budget budget) {
427
+ Operation operation(budget);
428
+ return operation.sum(a, b, output);
429
+ }
430
+ Status Adapter::roundedBits(const Dyadic& input, std::uint64_t& output) { return roundDyadic(input.value_, output); }
431
+ Status Adapter::anchoredBits(const exact_applied::Value& x, const exact_applied::Value& y,
432
+ const exact_applied::Value& z, const OriginalBasis& original, std::array<std::uint64_t, 3>& output, Budget budget) {
433
+ Operation operation(budget);
434
+ return operation.anchoredBits(x, y, z, original, output);
435
+ }
436
+ Status Operation::anchoredBits(const exact_applied::Value& x, const exact_applied::Value& y,
437
+ const exact_applied::Value& z, const OriginalBasis& original, std::array<std::uint64_t, 3>& output) {
438
+ auto& memory = memory_;
439
+ const std::array<const exact_applied::Value*, 3> ledgers{&x, &y, &z};
440
+ std::array<Encoding, 3> histories, sums;
441
+ std::array<std::uint64_t, 3> staged;
442
+ for (unsigned axis = 0; axis < 3; ++axis)
443
+ if (!lift(ledgers[axis]->value_, histories[axis], memory)) return Status::ResourceExhausted;
444
+ for (unsigned row = 0; row < 3; ++row) {
445
+ std::array<Encoding, 3> terms;
446
+ for (unsigned axis = 0; axis < 3; ++axis) {
447
+ Encoding coefficient;
448
+ if (!atomDyadic(original.rows[row][axis], coefficient, memory)
449
+ || !productDyadic(histories[axis], coefficient, terms[axis], memory)) return Status::ResourceExhausted;
450
+ }
451
+ for (unsigned a = 0; a < 3; ++a)
452
+ for (unsigned b = a + 1; b < 3; ++b)
453
+ if (terms[a].sign != Sign::Zero && terms[a].sign == opposite(terms[b].sign)
454
+ && terms[a].exponent == terms[b].exponent && terms[a].oddMagnitude == terms[b].oddMagnitude) {
455
+ terms[a] = {}; terms[b] = {};
456
+ }
457
+ std::array<unsigned, 3> order{0, 1, 2};
458
+ std::sort(order.begin(), order.end(), [&](unsigned a, unsigned b) {
459
+ const auto compared = compareExponent(terms[a].exponent, terms[b].exponent);
460
+ return compared == 0 ? a < b : compared < 0;
461
+ });
462
+ for (const auto axis : order) {
463
+ Encoding next;
464
+ if (!sumDyadic(sums[row], terms[axis], next, memory)) return Status::ResourceExhausted;
465
+ sums[row] = std::move(next);
466
+ }
467
+ const auto status = roundDyadic(sums[row], staged[row]);
468
+ if (status != Status::Ok) return status;
469
+ }
470
+ output = staged; return Status::Ok;
471
+ }
472
+ Status Operation::import(const Encoding& input, Dyadic& output) {
473
+ return import(EncodingView{input.sign, input.exponent.sign,
474
+ input.exponent.magnitude, input.oddMagnitude}, output);
475
+ }
476
+ Status Operation::import(const EncodingView& input, Dyadic& output) {
477
+ if (!input.valid()) return Status::InvalidEncoding;
478
+ Encoding staged;
479
+ staged.sign = input.sign;
480
+ staged.exponent.sign = input.exponentSign;
481
+ if (!memory_.resize(staged.exponent.magnitude, input.exponentMagnitude.size())
482
+ || !memory_.resize(staged.oddMagnitude, input.oddMagnitude.size())) return Status::ResourceExhausted;
483
+ std::copy(input.exponentMagnitude.begin(), input.exponentMagnitude.end(), staged.exponent.magnitude.begin());
484
+ std::copy(input.oddMagnitude.begin(), input.oddMagnitude.end(), staged.oddMagnitude.begin());
485
+ output.value_ = std::move(staged); return Status::Ok;
486
+ }
487
+ Status Operation::fromAtom(const Atom& atom, Dyadic& output) {
488
+ Encoding staged;
489
+ if (!atomDyadic(atom, staged, memory_)) return Status::ResourceExhausted;
490
+ output.value_ = std::move(staged); return Status::Ok;
491
+ }
492
+ Status Operation::liftApplied(const exact_applied::Value& input, Dyadic& output) {
493
+ Encoding staged;
494
+ if (!lift(input.value_, staged, memory_)) return Status::ResourceExhausted;
495
+ output.value_ = std::move(staged); return Status::Ok;
496
+ }
497
+ Status Operation::clone(const Dyadic& input, Dyadic& output) {
498
+ Encoding staged;
499
+ if (!cloneDyadic(input.value_, staged, memory_)) return Status::ResourceExhausted;
500
+ output.value_ = std::move(staged); return Status::Ok;
501
+ }
502
+ Status Operation::capture(const Dyadic& input, Encoding& output) {
503
+ Encoding staged;
504
+ if (!cloneDyadic(input.value_, staged, memory_)) return Status::ResourceExhausted;
505
+ output = std::move(staged); return Status::Ok;
506
+ }
507
+ Status Operation::product(const Dyadic& a, const Dyadic& b, Dyadic& output) {
508
+ Encoding staged;
509
+ if (!productDyadic(a.value_, b.value_, staged, memory_)) return Status::ResourceExhausted;
510
+ output.value_ = std::move(staged); return Status::Ok;
511
+ }
512
+ Status Operation::sum(const Dyadic& a, const Dyadic& b, Dyadic& output) {
513
+ Encoding staged;
514
+ if (!sumDyadic(a.value_, b.value_, staged, memory_)) return Status::ResourceExhausted;
515
+ output.value_ = std::move(staged); return Status::Ok;
516
+ }
517
+ Status Operation::difference(const Dyadic& a, const Dyadic& b, Dyadic& output) {
518
+ Encoding negative, staged;
519
+ if (!cloneDyadic(b.value_, negative, memory_)) return Status::ResourceExhausted;
520
+ negative.sign = opposite(negative.sign);
521
+ if (!sumDyadic(a.value_, negative, staged, memory_)) return Status::ResourceExhausted;
522
+ output.value_ = std::move(staged); return Status::Ok;
523
+ }
524
+ Status Operation::sumTerms(std::span<const Dyadic* const> terms, Dyadic& output) {
525
+ Words order, cancelled;
526
+ if (!memory_.resize(order, terms.size()) || !memory_.resize(cancelled, terms.size()))
527
+ return Status::ResourceExhausted;
528
+ for (std::size_t i = 0; i < terms.size(); ++i) {
529
+ if (terms[i] == nullptr) return Status::InvalidEncoding;
530
+ order[i] = static_cast<std::uint64_t>(i);
531
+ }
532
+ for (std::size_t i = 0; i < terms.size(); ++i)
533
+ for (std::size_t j = i + 1; j < terms.size() && !cancelled[i]; ++j)
534
+ if (!cancelled[j] && terms[i]->value_.sign != Sign::Zero
535
+ && terms[i]->value_.sign == opposite(terms[j]->value_.sign)
536
+ && terms[i]->value_.exponent == terms[j]->value_.exponent
537
+ && terms[i]->value_.oddMagnitude == terms[j]->value_.oddMagnitude) {
538
+ cancelled[i] = cancelled[j] = 1;
539
+ }
540
+ std::sort(order.begin(), order.end(), [&](std::uint64_t a, std::uint64_t b) {
541
+ const auto compared = compareExponent(terms[a]->value_.exponent, terms[b]->value_.exponent);
542
+ return compared == 0 ? a < b : compared < 0;
543
+ });
544
+ Encoding total;
545
+ for (const auto index : order) {
546
+ if (cancelled[index]) continue;
547
+ Encoding next;
548
+ if (!sumDyadic(total, terms[index]->value_, next, memory_)) return Status::ResourceExhausted;
549
+ total = std::move(next);
550
+ }
551
+ output.value_ = std::move(total); return Status::Ok;
552
+ }
553
+ Status Operation::compareNonnegativeSum(const Dyadic& left, const Dyadic& a, const Dyadic& b, int& output) {
554
+ if (left.value_.sign == Sign::Negative || a.value_.sign == Sign::Negative || b.value_.sign == Sign::Negative)
555
+ return Status::InvalidEncoding;
556
+ int staged;
557
+ if (!magnitudeSumComparison(left.value_, a.value_, b.value_, staged, memory_)) return Status::ResourceExhausted;
558
+ output = staged; return Status::Ok;
559
+ }
560
+ Status Operation::compare(const Dyadic& a, const Dyadic& b, int& output) {
561
+ const auto rank = [](Sign sign) { return sign == Sign::Negative ? -1 : sign == Sign::Positive ? 1 : 0; };
562
+ if (a.value_.sign != b.value_.sign) { output = rank(a.value_.sign) < rank(b.value_.sign) ? -1 : 1; return Status::Ok; }
563
+ int staged;
564
+ if (!magnitudeComparison(a.value_, b.value_, staged, memory_)) return Status::ResourceExhausted;
565
+ output = a.value_.sign == Sign::Negative ? -staged : staged;
566
+ return Status::Ok;
567
+ }
568
+ RatioReadStatus Operation::roundRatioBits(const Dyadic& numerator, const Dyadic& denominator, std::uint64_t& output) {
569
+ std::uint64_t staged;
570
+ const auto status = roundRatio(numerator.value_, denominator.value_, staged, memory_);
571
+ if (status == RatioReadStatus::Ok) output = staged;
572
+ return status;
573
+ }
574
+
575
+ ConeReadStatus Operation::insideFrictionCone(const BasisCoordinates& coordinates,
576
+ const Atom& friction, bool& output) {
577
+ if (sign(coordinates.denominator) != Sign::Positive)
578
+ return ConeReadStatus::InvalidCoordinates;
579
+ if ((friction.bits() >> 63) != 0
580
+ && (friction.bits() & UINT64_C(0x7fffffffffffffff)) != 0)
581
+ return ConeReadStatus::InvalidFriction;
582
+ if (sign(coordinates.numerators[0]) == Sign::Negative) {
583
+ output = false;
584
+ return ConeReadStatus::Ok;
585
+ }
586
+ Dyadic mu, muSquared, normalSquared, limit, firstSquared, secondSquared;
587
+ constexpr auto exhausted = ConeReadStatus::ResourceExhausted;
588
+ if (fromAtom(friction, mu) != Status::Ok
589
+ || product(mu, mu, muSquared) != Status::Ok
590
+ || product(coordinates.numerators[0], coordinates.numerators[0], normalSquared)
591
+ != Status::Ok
592
+ || product(muSquared, normalSquared, limit) != Status::Ok
593
+ || product(coordinates.numerators[1], coordinates.numerators[1], firstSquared)
594
+ != Status::Ok
595
+ || product(coordinates.numerators[2], coordinates.numerators[2], secondSquared)
596
+ != Status::Ok) return exhausted;
597
+ int compared;
598
+ if (compareNonnegativeSum(limit, firstSquared, secondSquared, compared) != Status::Ok)
599
+ return exhausted;
600
+ output = compared >= 0;
601
+ return ConeReadStatus::Ok;
602
+ }
603
+
604
+ CoordinateStatus Operation::coefficientCoordinates(const exact_applied::Value& x,
605
+ const exact_applied::Value& y, const exact_applied::Value& z,
606
+ const OriginalBasis& original, BasisCoordinates& output) {
607
+ std::array<Dyadic, 3> world;
608
+ const std::array<const exact_applied::Value*, 3> histories{&x, &y, &z};
609
+ for (unsigned axis = 0; axis < 3; ++axis) {
610
+ if (liftApplied(*histories[axis], world[axis]) != Status::Ok)
611
+ return CoordinateStatus::ResourceExhausted;
612
+ }
613
+ return coordinatesFromDyadics(world, original, output);
614
+ }
615
+
616
+ CoordinateStatus Operation::materialCoordinates(const std::array<Atom, 3>& impulses,
617
+ const OriginalBasis& source, const OriginalBasis& current, BasisCoordinates& output) {
618
+ std::array<Dyadic, 3> world, scalars;
619
+ for (unsigned row = 0; row < 3; ++row) {
620
+ if (fromAtom(impulses[row], scalars[row]) != Status::Ok)
621
+ return CoordinateStatus::ResourceExhausted;
622
+ }
623
+ for (unsigned axis = 0; axis < 3; ++axis) {
624
+ for (unsigned row = 0; row < 3; ++row) {
625
+ Dyadic coefficient, term, next;
626
+ if (fromAtom(source.rows[row][axis], coefficient) != Status::Ok
627
+ || product(coefficient, scalars[row], term) != Status::Ok
628
+ || sum(world[axis], term, next) != Status::Ok)
629
+ return CoordinateStatus::ResourceExhausted;
630
+ world[axis] = std::move(next);
631
+ }
632
+ }
633
+ return coordinatesFromDyadics(world, current, output);
634
+ }
635
+
636
+ CoordinateStatus Operation::coordinatesFromDyadics(const std::array<Dyadic, 3>& world,
637
+ const OriginalBasis& original, BasisCoordinates& output) {
638
+ constexpr auto exhausted = CoordinateStatus::ResourceExhausted;
639
+ std::array<std::array<Dyadic, 3>, 3> basis, cofactors;
640
+ for (unsigned axis = 0; axis < 3; ++axis) {
641
+ for (unsigned row = 0; row < 3; ++row) {
642
+ if (fromAtom(original.rows[row][axis], basis[row][axis]) != Status::Ok)
643
+ return exhausted;
644
+ }
645
+ }
646
+ for (unsigned row = 0; row < 3; ++row) {
647
+ const auto first = (row + 1) % 3;
648
+ const auto second = (row + 2) % 3;
649
+ for (unsigned axis = 0; axis < 3; ++axis) {
650
+ const auto a = (axis + 1) % 3;
651
+ const auto b = (axis + 2) % 3;
652
+ Dyadic positive, negative;
653
+ if (product(basis[first][a], basis[second][b], positive) != Status::Ok
654
+ || product(basis[first][b], basis[second][a], negative) != Status::Ok
655
+ || difference(positive, negative, cofactors[row][axis]) != Status::Ok)
656
+ return exhausted;
657
+ }
658
+ }
659
+ const auto dotExact = [&](const std::array<Dyadic, 3>& left,
660
+ const std::array<Dyadic, 3>& right, Dyadic& result) {
661
+ Dyadic total;
662
+ for (unsigned axis = 0; axis < 3; ++axis) {
663
+ Dyadic term, next;
664
+ if (product(left[axis], right[axis], term) != Status::Ok
665
+ || sum(total, term, next) != Status::Ok) return false;
666
+ total = std::move(next);
667
+ }
668
+ result = std::move(total);
669
+ return true;
670
+ };
671
+ BasisCoordinates staged;
672
+ if (!dotExact(basis[0], cofactors[0], staged.denominator)) return exhausted;
673
+ if (sign(staged.denominator) == Sign::Zero) return CoordinateStatus::SingularBasis;
674
+ for (unsigned row = 0; row < 3; ++row) {
675
+ if (!dotExact(world, cofactors[row], staged.numerators[row])) return exhausted;
676
+ }
677
+ if (sign(staged.denominator) == Sign::Negative) {
678
+ const Dyadic zero;
679
+ Dyadic positive;
680
+ if (difference(zero, staged.denominator, positive) != Status::Ok) return exhausted;
681
+ staged.denominator = std::move(positive);
682
+ for (auto& numerator : staged.numerators) {
683
+ Dyadic negated;
684
+ if (difference(zero, numerator, negated) != Status::Ok) return exhausted;
685
+ numerator = std::move(negated);
686
+ }
687
+ }
688
+ output = std::move(staged);
689
+ return CoordinateStatus::Ok;
690
+ }
691
+ }