@xort/rcmg 0.0.0-stage → 0.1.0-preview.1

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 (143) hide show
  1. package/LICENSE-APACHE +176 -0
  2. package/LICENSE-MIT +21 -0
  3. package/PROVENANCE.json +1601 -0
  4. package/README.md +152 -2
  5. package/licenses/registry/allocator-api2-0.2.22-c19b7c6f923b/LICENSE-APACHE +176 -0
  6. package/licenses/registry/allocator-api2-0.2.22-c19b7c6f923b/LICENSE-MIT +23 -0
  7. package/licenses/registry/autocfg-1.5.1-c19b7c6f923b/LICENSE-APACHE +201 -0
  8. package/licenses/registry/autocfg-1.5.1-c19b7c6f923b/LICENSE-MIT +25 -0
  9. package/licenses/registry/bumpalo-3.20.4-c19b7c6f923b/LICENSE-APACHE +201 -0
  10. package/licenses/registry/bumpalo-3.20.4-c19b7c6f923b/LICENSE-MIT +25 -0
  11. package/licenses/registry/bumpalo-3.20.4-c19b7c6f923b/src/alloc.rs +794 -0
  12. package/licenses/registry/bumpalo-3.20.4-c19b7c6f923b/src/collections/mod.rs +93 -0
  13. package/licenses/registry/bumpalo-3.20.4-c19b7c6f923b/src/collections/raw_vec.rs +781 -0
  14. package/licenses/registry/bumpalo-3.20.4-c19b7c6f923b/src/collections/str/lossy.rs +209 -0
  15. package/licenses/registry/bumpalo-3.20.4-c19b7c6f923b/src/collections/str/mod.rs +43 -0
  16. package/licenses/registry/bumpalo-3.20.4-c19b7c6f923b/src/collections/string.rs +2191 -0
  17. package/licenses/registry/bumpalo-3.20.4-c19b7c6f923b/src/collections/vec.rs +3051 -0
  18. package/licenses/registry/cfg-if-1.0.5-c19b7c6f923b/LICENSE-APACHE +201 -0
  19. package/licenses/registry/cfg-if-1.0.5-c19b7c6f923b/LICENSE-MIT +25 -0
  20. package/licenses/registry/equivalent-1.0.3-c19b7c6f923b/LICENSE-APACHE +201 -0
  21. package/licenses/registry/equivalent-1.0.3-c19b7c6f923b/LICENSE-MIT +25 -0
  22. package/licenses/registry/foldhash-0.2.0-c19b7c6f923b/LICENSE +19 -0
  23. package/licenses/registry/hashbrown-0.16.1-c19b7c6f923b/LICENSE-APACHE +201 -0
  24. package/licenses/registry/hashbrown-0.16.1-c19b7c6f923b/LICENSE-MIT +25 -0
  25. package/licenses/registry/libm-0.2.16-c19b7c6f923b/LICENSE.txt +258 -0
  26. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/acos.rs +112 -0
  27. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/acosf.rs +79 -0
  28. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/asin.rs +115 -0
  29. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/asinf.rs +68 -0
  30. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/atan.rs +180 -0
  31. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/atan2.rs +131 -0
  32. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/atan2f.rs +90 -0
  33. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/atanf.rs +108 -0
  34. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/cbrt.rs +219 -0
  35. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/cbrtf.rs +73 -0
  36. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/cos.rs +77 -0
  37. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/cosf.rs +86 -0
  38. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/erf.rs +314 -0
  39. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/erff.rs +226 -0
  40. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/exp.rs +156 -0
  41. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/exp2.rs +400 -0
  42. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/exp2f.rs +141 -0
  43. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/expf.rs +103 -0
  44. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/expm1.rs +142 -0
  45. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/expm1f.rs +134 -0
  46. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/fma.rs +171 -0
  47. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/generic/ceil.rs +174 -0
  48. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/generic/floor.rs +157 -0
  49. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/generic/fma.rs +278 -0
  50. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/generic/fmax.rs +23 -0
  51. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/generic/fmaximum.rs +27 -0
  52. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/generic/fmaximum_num.rs +29 -0
  53. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/generic/fmin.rs +23 -0
  54. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/generic/fminimum.rs +27 -0
  55. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/generic/fminimum_num.rs +29 -0
  56. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/generic/fmod.rs +116 -0
  57. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/generic/rint.rs +130 -0
  58. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/generic/sqrt.rs +541 -0
  59. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/generic/trunc.rs +148 -0
  60. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/j0.rs +426 -0
  61. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/j0f.rs +363 -0
  62. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/j1.rs +418 -0
  63. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/j1f.rs +383 -0
  64. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/jn.rs +339 -0
  65. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/jnf.rs +253 -0
  66. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/k_cos.rs +62 -0
  67. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/k_cosf.rs +29 -0
  68. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/k_sin.rs +57 -0
  69. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/k_sinf.rs +30 -0
  70. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/k_tan.rs +105 -0
  71. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/k_tanf.rs +46 -0
  72. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/lgamma_r.rs +321 -0
  73. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/lgammaf_r.rs +256 -0
  74. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/log.rs +118 -0
  75. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/log10.rs +116 -0
  76. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/log10f.rs +90 -0
  77. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/log1p.rs +142 -0
  78. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/log1pf.rs +97 -0
  79. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/log2.rs +105 -0
  80. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/log2f.rs +86 -0
  81. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/logf.rs +66 -0
  82. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/pow.rs +624 -0
  83. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/powf.rs +343 -0
  84. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/rem_pio2.rs +235 -0
  85. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/rem_pio2_large.rs +476 -0
  86. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/rem_pio2f.rs +65 -0
  87. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/sin.rs +95 -0
  88. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/sincos.rs +137 -0
  89. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/sincosf.rs +176 -0
  90. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/sinf.rs +96 -0
  91. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/support/int_traits/narrowing_div.rs +175 -0
  92. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/support/modular.rs +304 -0
  93. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/tan.rs +74 -0
  94. package/licenses/registry/libm-0.2.16-c19b7c6f923b/src/math/tanf.rs +81 -0
  95. package/licenses/registry/num-traits-0.2.19-c19b7c6f923b/LICENSE-APACHE +201 -0
  96. package/licenses/registry/num-traits-0.2.19-c19b7c6f923b/LICENSE-MIT +25 -0
  97. package/licenses/registry/num-traits-0.2.19-c19b7c6f923b/src/lib.rs +635 -0
  98. package/licenses/registry/once_cell-1.21.4-c19b7c6f923b/LICENSE-APACHE +201 -0
  99. package/licenses/registry/once_cell-1.21.4-c19b7c6f923b/LICENSE-MIT +23 -0
  100. package/licenses/registry/proc-macro2-1.0.107-c19b7c6f923b/LICENSE-APACHE +176 -0
  101. package/licenses/registry/proc-macro2-1.0.107-c19b7c6f923b/LICENSE-MIT +23 -0
  102. package/licenses/registry/quote-1.0.47-c19b7c6f923b/LICENSE-APACHE +176 -0
  103. package/licenses/registry/quote-1.0.47-c19b7c6f923b/LICENSE-MIT +23 -0
  104. package/licenses/registry/robust-1.2.0-c19b7c6f923b/LICENSE-APACHE +201 -0
  105. package/licenses/registry/robust-1.2.0-c19b7c6f923b/LICENSE-MIT +26 -0
  106. package/licenses/registry/robust-1.2.0-c19b7c6f923b/src/lib.rs +2224 -0
  107. package/licenses/registry/rustversion-1.0.23-c19b7c6f923b/LICENSE-APACHE +176 -0
  108. package/licenses/registry/rustversion-1.0.23-c19b7c6f923b/LICENSE-MIT +23 -0
  109. package/licenses/registry/smallvec-1.16.6-c19b7c6f923b/LICENSE-APACHE +201 -0
  110. package/licenses/registry/smallvec-1.16.6-c19b7c6f923b/LICENSE-MIT +25 -0
  111. package/licenses/registry/spade-2.15.1-c19b7c6f923b/LICENSE-APACHE +202 -0
  112. package/licenses/registry/spade-2.15.1-c19b7c6f923b/LICENSE-MIT +25 -0
  113. package/licenses/registry/syn-2.0.119-c19b7c6f923b/LICENSE-APACHE +176 -0
  114. package/licenses/registry/syn-2.0.119-c19b7c6f923b/LICENSE-MIT +23 -0
  115. package/licenses/registry/unicode-ident-1.0.27-c19b7c6f923b/LICENSE-APACHE +176 -0
  116. package/licenses/registry/unicode-ident-1.0.27-c19b7c6f923b/LICENSE-MIT +23 -0
  117. package/licenses/registry/unicode-ident-1.0.27-c19b7c6f923b/LICENSE-UNICODE +39 -0
  118. package/licenses/registry/wasm-bindgen-0.2.118-c19b7c6f923b/LICENSE-APACHE +201 -0
  119. package/licenses/registry/wasm-bindgen-0.2.118-c19b7c6f923b/LICENSE-MIT +25 -0
  120. package/licenses/registry/wasm-bindgen-macro-0.2.118-c19b7c6f923b/LICENSE-APACHE +201 -0
  121. package/licenses/registry/wasm-bindgen-macro-0.2.118-c19b7c6f923b/LICENSE-MIT +25 -0
  122. package/licenses/registry/wasm-bindgen-macro-support-0.2.118-c19b7c6f923b/LICENSE-APACHE +201 -0
  123. package/licenses/registry/wasm-bindgen-macro-support-0.2.118-c19b7c6f923b/LICENSE-MIT +25 -0
  124. package/licenses/registry/wasm-bindgen-shared-0.2.118-c19b7c6f923b/LICENSE-APACHE +201 -0
  125. package/licenses/registry/wasm-bindgen-shared-0.2.118-c19b7c6f923b/LICENSE-MIT +25 -0
  126. package/licenses/registry-licenses.json +1413 -0
  127. package/licenses/source/LICENSE-APACHE +176 -0
  128. package/licenses/source/LICENSE-MIT +21 -0
  129. package/licenses/source/crates/rcmg-mesh/src/isosurface/LICENSE-PyMCubes +27 -0
  130. package/licenses/source/crates/rcmg-mesh/src/isosurface/NOTICE.md +11 -0
  131. package/licenses/source/vendor/cadcraft-dxf/LICENSE-APACHE +177 -0
  132. package/licenses/source/vendor/cadcraft-dxf/LICENSE-MIT +21 -0
  133. package/node/index.mjs +2 -0
  134. package/node/package.json +3 -0
  135. package/node/rcmg_ffi.d.ts +51 -0
  136. package/node/rcmg_ffi.js +269 -0
  137. package/node/rcmg_ffi_bg.wasm +0 -0
  138. package/node/rcmg_ffi_bg.wasm.d.ts +32 -0
  139. package/package.json +57 -6
  140. package/web/rcmg_ffi.d.ts +108 -0
  141. package/web/rcmg_ffi.js +358 -0
  142. package/web/rcmg_ffi_bg.wasm +0 -0
  143. package/web/rcmg_ffi_bg.wasm.d.ts +32 -0
@@ -0,0 +1,157 @@
1
+ /* SPDX-License-Identifier: MIT
2
+ * origin: musl src/math/floor.c */
3
+
4
+ //! Generic `floor` algorithm.
5
+ //!
6
+ //! Note that this uses the algorithm from musl's `floorf` rather than `floor` or `floorl` because
7
+ //! performance seems to be better (based on icount) and it does not seem to experience rounding
8
+ //! errors on i386.
9
+
10
+ use crate::support::{Float, FpResult, Int, IntTy, MinInt, Status};
11
+
12
+ #[inline]
13
+ pub fn floor<F: Float>(x: F) -> F {
14
+ floor_status(x).val
15
+ }
16
+
17
+ #[inline]
18
+ pub fn floor_status<F: Float>(x: F) -> FpResult<F> {
19
+ let zero = IntTy::<F>::ZERO;
20
+
21
+ let mut ix = x.to_bits();
22
+ let e = x.exp_unbiased();
23
+
24
+ // If the represented value has no fractional part, no truncation is needed.
25
+ if e >= F::SIG_BITS as i32 {
26
+ return FpResult::ok(x);
27
+ }
28
+
29
+ let status;
30
+ let res = if e >= 0 {
31
+ // |x| >= 1.0
32
+ let m = F::SIG_MASK >> e.unsigned();
33
+ if ix & m == zero {
34
+ // Portion to be masked is already zero; no adjustment needed.
35
+ return FpResult::ok(x);
36
+ }
37
+
38
+ // Otherwise, raise an inexact exception.
39
+ status = Status::INEXACT;
40
+
41
+ if x.is_sign_negative() {
42
+ ix += m;
43
+ }
44
+
45
+ ix &= !m;
46
+ F::from_bits(ix)
47
+ } else {
48
+ // |x| < 1.0, raise an inexact exception since truncation will happen.
49
+ if ix & F::SIG_MASK == F::Int::ZERO {
50
+ status = Status::OK;
51
+ } else {
52
+ status = Status::INEXACT;
53
+ }
54
+
55
+ if x.is_sign_positive() {
56
+ // 0.0 <= x < 1.0; rounding down goes toward +0.0.
57
+ F::ZERO
58
+ } else if ix << 1 != zero {
59
+ // -1.0 < x < 0.0; rounding down goes toward -1.0.
60
+ F::NEG_ONE
61
+ } else {
62
+ // -0.0 remains unchanged
63
+ x
64
+ }
65
+ };
66
+
67
+ FpResult::new(res, status)
68
+ }
69
+
70
+ #[cfg(test)]
71
+ mod tests {
72
+ use super::*;
73
+ use crate::support::Hexf;
74
+
75
+ /// Test against https://en.cppreference.com/w/cpp/numeric/math/floor
76
+ fn spec_test<F: Float>(cases: &[(F, F, Status)]) {
77
+ let roundtrip = [
78
+ F::ZERO,
79
+ F::ONE,
80
+ F::NEG_ONE,
81
+ F::NEG_ZERO,
82
+ F::INFINITY,
83
+ F::NEG_INFINITY,
84
+ ];
85
+
86
+ for x in roundtrip {
87
+ let FpResult { val, status } = floor_status(x);
88
+ assert_biteq!(val, x, "{}", Hexf(x));
89
+ assert_eq!(status, Status::OK, "{}", Hexf(x));
90
+ }
91
+
92
+ for &(x, res, res_stat) in cases {
93
+ let FpResult { val, status } = floor_status(x);
94
+ assert_biteq!(val, res, "{}", Hexf(x));
95
+ assert_eq!(status, res_stat, "{}", Hexf(x));
96
+ }
97
+ }
98
+
99
+ /* Skipping f16 / f128 "sanity_check"s and spec cases due to rejected literal lexing at MSRV */
100
+
101
+ #[test]
102
+ #[cfg(f16_enabled)]
103
+ fn spec_tests_f16() {
104
+ let cases = [];
105
+ spec_test::<f16>(&cases);
106
+ }
107
+
108
+ #[test]
109
+ fn sanity_check_f32() {
110
+ assert_eq!(floor(0.5f32), 0.0);
111
+ assert_eq!(floor(1.1f32), 1.0);
112
+ assert_eq!(floor(2.9f32), 2.0);
113
+ }
114
+
115
+ #[test]
116
+ fn spec_tests_f32() {
117
+ let cases = [
118
+ (0.1, 0.0, Status::INEXACT),
119
+ (-0.1, -1.0, Status::INEXACT),
120
+ (0.9, 0.0, Status::INEXACT),
121
+ (-0.9, -1.0, Status::INEXACT),
122
+ (1.1, 1.0, Status::INEXACT),
123
+ (-1.1, -2.0, Status::INEXACT),
124
+ (1.9, 1.0, Status::INEXACT),
125
+ (-1.9, -2.0, Status::INEXACT),
126
+ ];
127
+ spec_test::<f32>(&cases);
128
+ }
129
+
130
+ #[test]
131
+ fn sanity_check_f64() {
132
+ assert_eq!(floor(1.1f64), 1.0);
133
+ assert_eq!(floor(2.9f64), 2.0);
134
+ }
135
+
136
+ #[test]
137
+ fn spec_tests_f64() {
138
+ let cases = [
139
+ (0.1, 0.0, Status::INEXACT),
140
+ (-0.1, -1.0, Status::INEXACT),
141
+ (0.9, 0.0, Status::INEXACT),
142
+ (-0.9, -1.0, Status::INEXACT),
143
+ (1.1, 1.0, Status::INEXACT),
144
+ (-1.1, -2.0, Status::INEXACT),
145
+ (1.9, 1.0, Status::INEXACT),
146
+ (-1.9, -2.0, Status::INEXACT),
147
+ ];
148
+ spec_test::<f64>(&cases);
149
+ }
150
+
151
+ #[test]
152
+ #[cfg(f128_enabled)]
153
+ fn spec_tests_f128() {
154
+ let cases = [];
155
+ spec_test::<f128>(&cases);
156
+ }
157
+ }
@@ -0,0 +1,278 @@
1
+ /* SPDX-License-Identifier: MIT */
2
+ /* origin: musl src/math/fma.c. Ported to generic Rust algorithm in 2025, TG. */
3
+
4
+ use crate::support::{
5
+ CastFrom, CastInto, DInt, Float, FpResult, HInt, Int, IntTy, MinInt, Round, Status,
6
+ };
7
+
8
+ /// Fused multiply-add that works when there is not a larger float size available. Computes
9
+ /// `(x * y) + z`.
10
+ #[inline]
11
+ pub fn fma_round<F>(x: F, y: F, z: F, _round: Round) -> FpResult<F>
12
+ where
13
+ F: Float,
14
+ F: CastFrom<F::SignedInt>,
15
+ F: CastFrom<i8>,
16
+ F::Int: HInt,
17
+ u32: CastInto<F::Int>,
18
+ {
19
+ let one = IntTy::<F>::ONE;
20
+ let zero = IntTy::<F>::ZERO;
21
+
22
+ // Normalize such that the top of the mantissa is zero and we have a guard bit.
23
+ let nx = Norm::from_float(x);
24
+ let ny = Norm::from_float(y);
25
+ let nz = Norm::from_float(z);
26
+
27
+ if nx.is_zero_nan_inf() || ny.is_zero_nan_inf() {
28
+ // Value will overflow, defer to non-fused operations.
29
+ return FpResult::ok(x * y + z);
30
+ }
31
+
32
+ if nz.is_zero_nan_inf() {
33
+ if nz.is_zero() {
34
+ // Empty add component means we only need to multiply.
35
+ return FpResult::ok(x * y);
36
+ }
37
+ // `z` is NaN or infinity, which sets the result.
38
+ return FpResult::ok(z);
39
+ }
40
+
41
+ // multiply: r = x * y
42
+ let zhi: F::Int;
43
+ let zlo: F::Int;
44
+ let (mut rlo, mut rhi) = nx.m.widen_mul(ny.m).lo_hi();
45
+
46
+ // Exponent result of multiplication
47
+ let mut e: i32 = nx.e + ny.e;
48
+ // Needed shift to align `z` to the multiplication result
49
+ let mut d: i32 = nz.e - e;
50
+ let sbits = F::BITS as i32;
51
+
52
+ // Scale `z`. Shift `z <<= kz`, `r >>= kr`, so `kz+kr == d`, set `e = e+kr` (== ez-kz)
53
+ if d > 0 {
54
+ // The magnitude of `z` is larger than `x * y`
55
+ if d < sbits {
56
+ // Maximum shift of one `F::BITS` means shifted `z` will fit into `2 * F::BITS`. Shift
57
+ // it into `(zhi, zlo)`. No exponent adjustment necessary.
58
+ zlo = nz.m << d;
59
+ zhi = nz.m >> (sbits - d);
60
+ } else {
61
+ // Shift larger than `sbits`, `z` only needs the top half `zhi`. Place it there (acts
62
+ // as a shift by `sbits`).
63
+ zlo = zero;
64
+ zhi = nz.m;
65
+ d -= sbits;
66
+
67
+ // `z`'s exponent is large enough that it now needs to be taken into account.
68
+ e = nz.e - sbits;
69
+
70
+ if d == 0 {
71
+ // Exactly `sbits`, nothing to do
72
+ } else if d < sbits {
73
+ // Remaining shift fits within `sbits`. Leave `z` in place, shift `x * y`
74
+ rlo = (rhi << (sbits - d)) | (rlo >> d);
75
+ // Set the sticky bit
76
+ rlo |= IntTy::<F>::from((rlo << (sbits - d)) != zero);
77
+ rhi = rhi >> d;
78
+ } else {
79
+ // `z`'s magnitude is enough that `x * y` is irrelevant. It was nonzero, so set
80
+ // the sticky bit.
81
+ rlo = one;
82
+ rhi = zero;
83
+ }
84
+ }
85
+ } else {
86
+ // `z`'s magnitude once shifted fits entirely within `zlo`
87
+ zhi = zero;
88
+ d = -d;
89
+ if d == 0 {
90
+ // No shift needed
91
+ zlo = nz.m;
92
+ } else if d < sbits {
93
+ // Shift s.t. `nz.m` fits into `zlo`
94
+ let sticky = IntTy::<F>::from((nz.m << (sbits - d)) != zero);
95
+ zlo = (nz.m >> d) | sticky;
96
+ } else {
97
+ // Would be entirely shifted out, only set the sticky bit
98
+ zlo = one;
99
+ }
100
+ }
101
+
102
+ /* addition */
103
+
104
+ let mut neg = nx.neg ^ ny.neg;
105
+ let samesign: bool = !neg ^ nz.neg;
106
+ let mut rhi_nonzero = true;
107
+
108
+ if samesign {
109
+ // r += z
110
+ rlo = rlo.wrapping_add(zlo);
111
+ rhi += zhi + IntTy::<F>::from(rlo < zlo);
112
+ } else {
113
+ // r -= z
114
+ let (res, borrow) = rlo.overflowing_sub(zlo);
115
+ rlo = res;
116
+ rhi = rhi.wrapping_sub(zhi.wrapping_add(IntTy::<F>::from(borrow)));
117
+ if (rhi >> (F::BITS - 1)) != zero {
118
+ rlo = rlo.signed().wrapping_neg().unsigned();
119
+ rhi = rhi.signed().wrapping_neg().unsigned() - IntTy::<F>::from(rlo != zero);
120
+ neg = !neg;
121
+ }
122
+ rhi_nonzero = rhi != zero;
123
+ }
124
+
125
+ /* Construct result */
126
+
127
+ // Shift result into `rhi`, left-aligned. Last bit is sticky
128
+ if rhi_nonzero {
129
+ // `d` > 0, need to shift both `rhi` and `rlo` into result
130
+ e += sbits;
131
+ d = rhi.leading_zeros() as i32 - 1;
132
+ rhi = (rhi << d) | (rlo >> (sbits - d));
133
+ // Update sticky
134
+ rhi |= IntTy::<F>::from((rlo << d) != zero);
135
+ } else if rlo != zero {
136
+ // `rhi` is zero, `rlo` is the entire result and needs to be shifted
137
+ d = rlo.leading_zeros() as i32 - 1;
138
+ if d < 0 {
139
+ // Shift and set sticky
140
+ rhi = (rlo >> 1) | (rlo & one);
141
+ } else {
142
+ rhi = rlo << d;
143
+ }
144
+ } else {
145
+ // exact +/- 0.0
146
+ return FpResult::ok(x * y + z);
147
+ }
148
+
149
+ e -= d;
150
+
151
+ // Use int->float conversion to populate the significand.
152
+ // i is in [1 << (BITS - 2), (1 << (BITS - 1)) - 1]
153
+ let mut i: F::SignedInt = rhi.signed();
154
+
155
+ if neg {
156
+ i = -i;
157
+ }
158
+
159
+ // `|r|` is in `[0x1p62,0x1p63]` for `f64`
160
+ let mut r: F = F::cast_from_lossy(i);
161
+
162
+ /* Account for subnormal and rounding */
163
+
164
+ // Unbiased exponent for the maximum value of `r`
165
+ let max_pow = F::BITS - 1 + F::EXP_BIAS;
166
+
167
+ let mut status = Status::OK;
168
+
169
+ if e < -(max_pow as i32 - 2) {
170
+ // Result is subnormal before rounding
171
+ if e == -(max_pow as i32 - 1) {
172
+ let mut c = F::from_parts(false, max_pow, zero);
173
+ if neg {
174
+ c = -c;
175
+ }
176
+
177
+ if r == c {
178
+ // Min normal after rounding,
179
+ status.set_underflow(true);
180
+ r = F::MIN_POSITIVE_NORMAL.copysign(r);
181
+ return FpResult::new(r, status);
182
+ }
183
+
184
+ if (rhi << (F::SIG_BITS + 1)) != zero {
185
+ // Account for truncated bits. One bit will be lost in the `scalbn` call, add
186
+ // another top bit to avoid double rounding if inexact.
187
+ let iu: F::Int = (rhi >> 1) | (rhi & one) | (one << (F::BITS - 2));
188
+ i = iu.signed();
189
+
190
+ if neg {
191
+ i = -i;
192
+ }
193
+
194
+ r = F::cast_from_lossy(i);
195
+
196
+ // Remove the top bit
197
+ r = F::cast_from(2i8) * r - c;
198
+ status.set_underflow(true);
199
+ }
200
+ } else {
201
+ // Only round once when scaled
202
+ d = F::EXP_BITS as i32 - 1;
203
+ let sticky = IntTy::<F>::from(rhi << (F::BITS as i32 - d) != zero);
204
+ i = (((rhi >> d) | sticky) << d).signed();
205
+
206
+ if neg {
207
+ i = -i;
208
+ }
209
+
210
+ r = F::cast_from_lossy(i);
211
+ }
212
+ }
213
+
214
+ // Use our exponent to scale the final value.
215
+ FpResult::new(super::scalbn(r, e), status)
216
+ }
217
+
218
+ /// Representation of `F` that has handled subnormals.
219
+ #[derive(Clone, Copy, Debug)]
220
+ struct Norm<F: Float> {
221
+ /// Normalized significand with one guard bit, unsigned.
222
+ m: F::Int,
223
+ /// Exponent of the mantissa such that `m * 2^e = x`. Accounts for the shift in the mantissa
224
+ /// and the guard bit; that is, 1.0 will normalize as `m = 1 << 53` and `e = -53`.
225
+ e: i32,
226
+ neg: bool,
227
+ }
228
+
229
+ impl<F: Float> Norm<F> {
230
+ /// Unbias the exponent and account for the mantissa's precision, including the guard bit.
231
+ const EXP_UNBIAS: u32 = F::EXP_BIAS + F::SIG_BITS + 1;
232
+
233
+ /// Values greater than this had a saturated exponent (infinity or NaN), OR were zero and we
234
+ /// adjusted the exponent such that it exceeds this threashold.
235
+ const ZERO_INF_NAN: u32 = F::EXP_SAT - Self::EXP_UNBIAS;
236
+
237
+ fn from_float(x: F) -> Self {
238
+ let mut ix = x.to_bits();
239
+ let mut e = x.ex() as i32;
240
+ let neg = x.is_sign_negative();
241
+ if e == 0 {
242
+ // Normalize subnormals by multiplication
243
+ let scale_i = F::BITS - 1;
244
+ let scale_f = F::from_parts(false, scale_i + F::EXP_BIAS, F::Int::ZERO);
245
+ let scaled = x * scale_f;
246
+ ix = scaled.to_bits();
247
+ e = scaled.ex() as i32;
248
+ e = if e == 0 {
249
+ // If the exponent is still zero, the input was zero. Artifically set this value
250
+ // such that the final `e` will exceed `ZERO_INF_NAN`.
251
+ 1 << F::EXP_BITS
252
+ } else {
253
+ // Otherwise, account for the scaling we just did.
254
+ e - scale_i as i32
255
+ };
256
+ }
257
+
258
+ e -= Self::EXP_UNBIAS as i32;
259
+
260
+ // Absolute value, set the implicit bit, and shift to create a guard bit
261
+ ix &= F::SIG_MASK;
262
+ ix |= F::IMPLICIT_BIT;
263
+ ix <<= 1;
264
+
265
+ Self { m: ix, e, neg }
266
+ }
267
+
268
+ /// True if the value was zero, infinity, or NaN.
269
+ fn is_zero_nan_inf(self) -> bool {
270
+ self.e >= Self::ZERO_INF_NAN as i32
271
+ }
272
+
273
+ /// The only value we have
274
+ fn is_zero(self) -> bool {
275
+ // The only exponent that strictly exceeds this value is our sentinel value for zero.
276
+ self.e > Self::ZERO_INF_NAN as i32
277
+ }
278
+ }
@@ -0,0 +1,23 @@
1
+ /* SPDX-License-Identifier: MIT OR Apache-2.0 */
2
+ //! IEEE 754-2011 `maxNum`. This has been superseded by IEEE 754-2019 `maximumNumber`.
3
+ //!
4
+ //! Per the spec, returns the canonicalized result of:
5
+ //! - `x` if `x > y`
6
+ //! - `y` if `y > x`
7
+ //! - The other number if one is NaN
8
+ //! - Otherwise, either `x` or `y`, canonicalized
9
+ //! - -0.0 and +0.0 may be disregarded (unlike newer operations)
10
+ //!
11
+ //! Excluded from our implementation is sNaN handling.
12
+ //!
13
+ //! More on the differences: [link].
14
+ //!
15
+ //! [link]: https://grouper.ieee.org/groups/msc/ANSI_IEEE-Std-754-2019/background/minNum_maxNum_Removal_Demotion_v3.pdf
16
+
17
+ use crate::support::Float;
18
+
19
+ #[inline]
20
+ pub fn fmax<F: Float>(x: F, y: F) -> F {
21
+ let res = if x.is_nan() || x < y { y } else { x };
22
+ res.canonicalize()
23
+ }
@@ -0,0 +1,27 @@
1
+ /* SPDX-License-Identifier: MIT OR Apache-2.0 */
2
+ //! IEEE 754-2019 `maximum`.
3
+ //!
4
+ //! Per the spec, returns the canonicalized result of:
5
+ //! - `x` if `x > y`
6
+ //! - `y` if `y > x`
7
+ //! - +0.0 if x and y are zero with opposite signs
8
+ //! - qNaN if either operation is NaN
9
+ //!
10
+ //! Excluded from our implementation is sNaN handling.
11
+
12
+ use crate::support::Float;
13
+
14
+ #[inline]
15
+ pub fn fmaximum<F: Float>(x: F, y: F) -> F {
16
+ let res = if x.is_nan() {
17
+ x
18
+ } else if y.is_nan() {
19
+ y
20
+ } else if x > y || (y.biteq(F::NEG_ZERO) && x.is_sign_positive()) {
21
+ x
22
+ } else {
23
+ y
24
+ };
25
+
26
+ res.canonicalize()
27
+ }
@@ -0,0 +1,29 @@
1
+ /* SPDX-License-Identifier: MIT OR Apache-2.0 */
2
+ //! IEEE 754-2019 `maximumNumber`.
3
+ //!
4
+ //! Per the spec, returns:
5
+ //! - `x` if `x > y`
6
+ //! - `y` if `y > x`
7
+ //! - +0.0 if x and y are zero with opposite signs
8
+ //! - Either `x` or `y` if `x == y` and the signs are the same
9
+ //! - Non-NaN if one operand is NaN
10
+ //! - qNaN if both operands are NaNx
11
+ //!
12
+ //! Excluded from our implementation is sNaN handling.
13
+
14
+ use crate::support::Float;
15
+
16
+ #[inline]
17
+ pub fn fmaximum_num<F: Float>(x: F, y: F) -> F {
18
+ let res = if x > y || y.is_nan() {
19
+ x
20
+ } else if y > x || x.is_nan() {
21
+ y
22
+ } else if x.is_sign_positive() {
23
+ x
24
+ } else {
25
+ y
26
+ };
27
+
28
+ res.canonicalize()
29
+ }
@@ -0,0 +1,23 @@
1
+ /* SPDX-License-Identifier: MIT OR Apache-2.0 */
2
+ //! IEEE 754-2008 `minNum`. This has been superseded by IEEE 754-2019 `minimumNumber`.
3
+ //!
4
+ //! Per the spec, returns the canonicalized result of:
5
+ //! - `x` if `x < y`
6
+ //! - `y` if `y < x`
7
+ //! - The other number if one is NaN
8
+ //! - Otherwise, either `x` or `y`, canonicalized
9
+ //! - -0.0 and +0.0 may be disregarded (unlike newer operations)
10
+ //!
11
+ //! Excluded from our implementation is sNaN handling.
12
+ //!
13
+ //! More on the differences: [link].
14
+ //!
15
+ //! [link]: https://grouper.ieee.org/groups/msc/ANSI_IEEE-Std-754-2019/background/minNum_maxNum_Removal_Demotion_v3.pdf
16
+
17
+ use crate::support::Float;
18
+
19
+ #[inline]
20
+ pub fn fmin<F: Float>(x: F, y: F) -> F {
21
+ let res = if y.is_nan() || x < y { x } else { y };
22
+ res.canonicalize()
23
+ }
@@ -0,0 +1,27 @@
1
+ /* SPDX-License-Identifier: MIT OR Apache-2.0 */
2
+ //! IEEE 754-2019 `minimum`.
3
+ //!
4
+ //! Per the spec, returns the canonicalized result of:
5
+ //! - `x` if `x < y`
6
+ //! - `y` if `y < x`
7
+ //! - -0.0 if x and y are zero with opposite signs
8
+ //! - qNaN if either operation is NaN
9
+ //!
10
+ //! Excluded from our implementation is sNaN handling.
11
+
12
+ use crate::support::Float;
13
+
14
+ #[inline]
15
+ pub fn fminimum<F: Float>(x: F, y: F) -> F {
16
+ let res = if x.is_nan() {
17
+ x
18
+ } else if y.is_nan() {
19
+ y
20
+ } else if x < y || (x.biteq(F::NEG_ZERO) && y.is_sign_positive()) {
21
+ x
22
+ } else {
23
+ y
24
+ };
25
+
26
+ res.canonicalize()
27
+ }
@@ -0,0 +1,29 @@
1
+ /* SPDX-License-Identifier: MIT OR Apache-2.0 */
2
+ //! IEEE 754-2019 `minimum`.
3
+ //!
4
+ //! Per the spec, returns:
5
+ //! - `x` if `x < y`
6
+ //! - `y` if `y < x`
7
+ //! - -0.0 if x and y are zero with opposite signs
8
+ //! - Either `x` or `y` if `x == y` and the signs are the same
9
+ //! - Non-NaN if one operand is NaN
10
+ //! - qNaN if both operands are NaNx
11
+ //!
12
+ //! Excluded from our implementation is sNaN handling.
13
+
14
+ use crate::support::Float;
15
+
16
+ #[inline]
17
+ pub fn fminimum_num<F: Float>(x: F, y: F) -> F {
18
+ let res = if x > y || x.is_nan() {
19
+ y
20
+ } else if y > x || y.is_nan() {
21
+ x
22
+ } else if x.is_sign_positive() {
23
+ y
24
+ } else {
25
+ x
26
+ };
27
+
28
+ res.canonicalize()
29
+ }