1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
use crate::core::integer::*;
use crate::core::undefined::*;
use crate::{Circle, CircleConstants, Integer, Scalar, ScalarConstants};
use core::ops::*;
use i256::I256;
use num_traits::{AsPrimitive, WrappingAdd, WrappingMul, WrappingNeg, WrappingSub};
#[allow(private_bounds)]
impl<
F: Integer
+ FullInt
+ Shl<isize, Output = F>
+ Shr<isize, Output = F>
+ Shl<F, Output = F>
+ Shr<F, Output = F>
+ Shl<E, Output = F>
+ Shr<E, Output = F>
+ WrappingNeg
+ WrappingAdd
+ WrappingMul
+ WrappingSub,
E: Integer
+ FullInt
+ Shl<isize, Output = E>
+ Shr<isize, Output = E>
+ Shl<E, Output = E>
+ Shr<E, Output = E>
+ Shl<F, Output = E>
+ Shr<F, Output = E>
+ WrappingNeg
+ WrappingAdd
+ WrappingMul
+ WrappingSub,
> Circle<F, E>
where
Circle<F, E>: CircleConstants,
Scalar<F, E>: ScalarConstants,
u8: AsPrimitive<F>,
u16: AsPrimitive<F>,
u32: AsPrimitive<F>,
u64: AsPrimitive<F>,
u128: AsPrimitive<F>,
usize: AsPrimitive<F>,
i8: AsPrimitive<F>,
i16: AsPrimitive<F>,
i32: AsPrimitive<F>,
i64: AsPrimitive<F>,
i128: AsPrimitive<F>,
isize: AsPrimitive<F>,
I256: From<F>,
u8: AsPrimitive<E>,
u16: AsPrimitive<E>,
u32: AsPrimitive<E>,
u64: AsPrimitive<E>,
u128: AsPrimitive<E>,
usize: AsPrimitive<E>,
i8: AsPrimitive<E>,
i16: AsPrimitive<E>,
i32: AsPrimitive<E>,
i64: AsPrimitive<E>,
i128: AsPrimitive<E>,
isize: AsPrimitive<E>,
I256: From<E>,
{
/// Multiplies this Circle by a Scalar
///
/// # Description
///
/// Performs multiplication between a Circle and a Scalar, scaling both real and imaginary components by the Scalar value while preserving the orientation of the Circle. For normal values, this multiplies each component of the Circle by the Scalar, following standard complex-real multiplication formula: (a+b·i)·c = (a·c) + (b·c)·i.
///
/// Multiplication process:
/// 0. Checks for and handle undefined states
/// 1. Uses wider integer types for component multiplication:
/// ```text
/// a → ■■■■■■■ = self.real c → ■■■■■■■ = scalar.fraction ⤪⤪⤪⤪⤪⤪ Multiply! a·c → ■■■■■■■ □□□□□□□ = Intermediate product for real part ↘↘↘↘↘↘↘ Real → ■■■■■■■ = High bits kept for real component
///
/// b → ■■■■■■■ = self.imaginary c → ■■■■■■■ = scalar.fraction ⤪⤪⤪⤪⤪⤪ Multiply! b·c → ■■■■■■■ □□□□□□□ = Intermediate product for imaginary part ↘↘↘↘↘↘↘ Imaginary → ■■■■■■■ = High bits kept for imaginary component
/// ```
/// 2. Calculates leading Zeros/Ones for both components to determine normalization shift
/// 3. If normal, adds exponents and adjusts by normalization shift (exponent_result = self.exponent + scalar.exponent - shift)
/// 4. Checks for overflow/underflow and returns appropriate escaped values if necessary
///
/// # Returns
///
/// - `[℘ ]` ➔ `[℘ ]` First undefined state encountered
/// - `[0]` × `[#]` or `[#]` × `[0]` ➔ `[0]` Zero (multiplicative annihilation)
/// - `[↑]` × `[↓]` ➔ `[℘ ↑×↓]` Undefined state (magnitude indeterminate)
/// - `[↓]` × `[↑]` ➔ `[℘ ↓×↑]` Undefined state (magnitude indeterminate)
/// - `[↑]` × `[#]` or `[#]` × `[↑]` ➔ `[↑]` Exploded with orientation following multiplication rule
/// - `[↓]` × `[#]` or `[#]` × `[↓]` ➔ `[↓]` Vanished with orientation following multiplication rule
/// - `[↑]` × `[↑]` ➔ `[↑]` Exploded with orientation following multiplication rule
/// - `[↓]` × `[↓]` ➔ `[↓]` Vanished with orientation following multiplication rule
/// - `[#]` × `[#]` ➔ `[#]` or `[↑]` or `[↓]` A finite, exploded or vanished Circle
///
/// # Examples
///
/// ```rust
/// use spirix::{Circle, Scalar, CircleF6E4, ScalarF6E4};
///
/// // Basic multiplication - scales both components let z = Circle::<i64, i16>::from((3_i64, 4_i64)); let s = ScalarF6E4::from(2_i64); let result = z * s; assert!(result.r() == 6_i64); assert!(result.i() == 8_i64);
///
/// // Multiplying by negative Scalar negates the Circle let neg = ScalarF6E4::from(-1_i64); let negated = z * neg; assert!(negated.r() == -3_i64); assert!(negated.i() == -4_i64);
///
/// // Multiplying by Zero produces Zero assert!((z * ScalarF6E4::ZERO).is_zero());
///
/// // Exploded values interact consistently with Scalars let huge: CircleF6E4 = CircleF6E4::MAX * 10_i64; assert!(huge.exploded()); let neg_huge = huge * ScalarF6E4::NEG_ONE; assert!(neg_huge.exploded());
///
/// // Undefined input stays undefined let undef: ScalarF6E4 = ScalarF6E4::ZERO / 0_i64; assert!((z * undef).is_undefined());
/// ```
pub(crate) fn circle_multiply_scalar(&self, other: &Scalar<F, E>) -> Self {
if self.is_normal() && other.is_normal() {
// AMBIG=0 unified pipeline. Circle * Scalar: each Circle component multiplies by the (N0→N1 converted) Scalar fraction; no cross-term since Scalar has no imaginary.
let a = self.real.sign_extend();
let b = self.imaginary.sign_extend();
let s = ((other.fraction >> 1isize) ^ F::min_value()).sign_extend();
let fb = Self::fraction_bits();
let real_product = a.w_mul(s);
let imag_product = b.w_mul(s);
if real_product.w_is_zero() && imag_product.w_is_zero() {
return Self::ZERO;
}
let leading_r = real_product.leading_same();
let leading_i = imag_product.leading_same();
let leading = leading_r.min(leading_i);
let shift = leading.wrapping_sub(1);
let real = real_product.w_shl(shift).w_shr(fb).deflate();
let imaginary = imag_product.w_shl(shift).w_shr(fb).deflate();
// Exp: stored_pos = pa + pb - expo_adjust - bo + 1 (same as Circle*Circle mul).
let pa = self.exponent.cycle_widen();
let pb = other.exponent.cycle_widen();
let expo_adjust_e: E = leading.wrapping_sub(2).as_();
let w_adj = expo_adjust_e.sign_extend();
let w_bo = Self::binade_origin().cycle_widen();
let w_one = E::one().cycle_widen();
let stored_pos = pa.w_add(pb).w_sub(w_adj).w_sub(w_bo).w_add(w_one);
let max_pos = Self::max_exponent().cycle_widen();
let min_pos = Self::min_exponent().cycle_widen();
return if stored_pos > max_pos {
Self {
real,
imaginary,
exponent: Self::ambiguous_exponent(),
}
} else if stored_pos < min_pos {
Self {
real: real >> 1isize,
imaginary: imaginary >> 1isize,
exponent: Self::ambiguous_exponent(),
}
} else {
Self {
real,
imaginary,
exponent: stored_pos.deflate(),
}
};
}
// Escape-class handling (at least one operand non-normal).
{
if self.is_undefined() {
return *self;
} else if other.is_undefined() {
return Self {
real: other.fraction,
imaginary: other.fraction,
exponent: other.exponent,
};
} else if self.is_infinite() && other.is_zero() {
return Self {
real: TRANSFINITE_MULTIPLY_NEGLIGIBLE.prefix.sa(),
imaginary: TRANSFINITE_MULTIPLY_NEGLIGIBLE.prefix.sa(),
exponent: Self::ambiguous_exponent(),
};
} else if self.is_zero() && other.is_infinite() {
return Self {
real: NEGLIGIBLE_MULTIPLY_TRANSFINITE.prefix.sa(),
imaginary: NEGLIGIBLE_MULTIPLY_TRANSFINITE.prefix.sa(),
exponent: Self::ambiguous_exponent(),
};
} else if self.is_infinite() || other.is_infinite() {
return Self::INFINITY;
} else if self.is_zero() || other.is_zero() {
return Self::ZERO;
} else if self.exploded() && other.vanished() {
return Self {
real: TRANSFINITE_MULTIPLY_NEGLIGIBLE.prefix.sa(),
imaginary: TRANSFINITE_MULTIPLY_NEGLIGIBLE.prefix.sa(),
exponent: Self::ambiguous_exponent(),
};
} else if self.vanished() && other.exploded() {
return Self {
real: NEGLIGIBLE_MULTIPLY_TRANSFINITE.prefix.sa(),
imaginary: NEGLIGIBLE_MULTIPLY_TRANSFINITE.prefix.sa(),
exponent: Self::ambiguous_exponent(),
};
} else {
// Mixed escape: same shape as scalar_multiply_circle's escape — compute the product via WideOps, renormalize to N-1 (one exploded) or N-2 (both vanished) at AMBIG exp. N0→N1 conversion only applies to normal-class fractions; escape patterns share layout across N0 and N1.
let n_level: isize = if self.exploded() || other.exploded() {
-1
} else {
-2
};
let multiplicand_narrow: F = if other.is_normal() {
(other.fraction >> 1isize) ^ F::min_value()
} else {
other.fraction
};
let m = multiplicand_narrow.sign_extend();
let a = self.real.sign_extend();
let b = self.imaginary.sign_extend();
let product_r = a.w_mul(m);
let product_i = b.w_mul(m);
let leading_r = product_r.leading_same();
let leading_i = product_i.leading_same();
let leading = leading_r.min(leading_i);
let shift = leading.wrapping_add(n_level);
let fb = Self::fraction_bits();
return Self {
real: product_r.w_shl(shift).w_shr(fb).deflate(),
imaginary: product_i.w_shl(shift).w_shr(fb).deflate(),
exponent: Self::ambiguous_exponent(),
};
}
}
}
}