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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
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>,
{
/// Adds a Scalar to this Circle
///
/// # Description
///
/// Performs addition between a Circle and a Scalar, adding the Scalar value to the real component of the Circle while leaving the imaginary component unchanged (except for normalization). Returns a finite Circle unless the result exceeds representable range, in which case it may return an exploded or vanished Circle.
///
/// Addition process:
/// 0. Checks for any escaped (vanished, exploded, infinity, or undefined) values and handles these first
/// 1. Checks for Zeros and returns the appropriate result
/// 2. Aligns fractions by shifting the smaller value right based on exponent difference
/// 3. Adds the Scalar to the real component of the Circle
/// 4. Normalizes result and adjusts exponent, considering both components
/// 5. Escapes for underflow if necessary, overflow is naturally handled by escaped exponent alignment
///
/// # Returns
///
/// - `[℘ ]` ➔ `[℘ ]` First undefined state encountered
/// - `[∞]` + `[#, #i]` ➔ `[℘ ↑+]` Undefined exploded plus finite state
/// - `[#, #i]` + `[∞]` ➔ `[℘ +↑]` Undefined finite plus exploded state
/// - `[∞]` + `[∞, #i]` ➔ `[℘ ↑+↑]` Undefined exploded plus exploded state
/// - `[↑]` + `[↑]` ➔ `[℘ ↑+↑]` Undefined exploded plus exploded state
/// - `[↑]` + `[#]` ➔ `[℘ ↑+]` Undefined exploded plus finite state
/// - `[#]` + `[↑]` ➔ `[℘ +↑]` Undefined finite plus exploded state
/// - `[↓]` + `[↓]` ➔ `[℘ ↓+↓]` Undefined vanished plus vanished state
/// - `[↓]` + `[#]` ➔ `[#]` The finite Scalar as real part, zero imaginary
/// - `[#]` + `[↓]` ➔ `[#]` The Circle unchanged
/// - `[0]` + `[#]` ➔ `[#, 0i]` The Scalar as real part, zero imaginary
/// - `[#]` + `[0]` ➔ `[#]` The Circle unchanged
/// - `[#]` + `[#]` ➔ `[#]` or `[↑]` or `[↓]` A finite, exploded or vanished Circle
///
/// # Examples
///
/// ```rust
/// use spirix::{Circle, CircleF5E3, Scalar, ScalarF5E3};
///
/// // Adding a Scalar to a Circle let a = Circle::<i32, i8>::from((3_i32, 4_i32)); let b = ScalarF5E3::from(5_i32); let sum = a + b; assert!(sum.r() == 8_i32); assert!(sum.i() == 4_i32);
///
/// // Adding with Zero assert!(a + ScalarF5E3::ZERO == a);
///
/// // Adding with infinity stays infinite let infinity: ScalarF5E3 = ScalarF5E3::ONE / 0_i32; assert!((a + infinity).is_infinite());
///
/// // Addition with vanished values let tiny: ScalarF5E3 = ScalarF5E3::MIN_POS / 5_i32; assert!(tiny.vanished()); assert!((a + tiny) == a);
///
/// // Addition with exploded values let huge: ScalarF5E3 = ScalarF5E3::MAX * 5_i32; assert!(huge.exploded()); assert!((a + huge).is_undefined());
///
/// // Adding two exploded values let huge_circle: CircleF5E3 = CircleF5E3::MAX * 3_i32; assert!((huge_circle + huge).is_undefined());
/// ```
pub(crate) fn circle_add_scalar(&self, scalar: &Scalar<F, E>) -> Self {
if self.is_normal() && scalar.is_normal() {
// AMBIG=0 native unified pipeline. Scalar's N0 fraction → N1 via (s >> 1) ^ F::MIN before sign_extend; Scalar contributes 0 to the imaginary side.
let self_is_big = self.exponent.into_unsigned() > scalar.exponent.into_unsigned();
let (big_exp, small_exp) = if self_is_big {
(self.exponent, scalar.exponent)
} else {
(scalar.exponent, self.exponent)
};
let exp_diff = big_exp.wrapping_sub(&small_exp);
let frac_bits_e: E = Self::fraction_bits().as_();
if exp_diff.into_unsigned() >= frac_bits_e.into_unsigned() {
return if self_is_big {
*self
} else {
Circle {
real: (scalar.fraction >> 1isize) ^ F::min_value(),
imaginary: F::zero(),
exponent: scalar.exponent,
}
};
}
let shift: isize = exp_diff.saturate();
let scalar_n1 = (scalar.fraction >> 1isize) ^ F::min_value();
let (big_r, big_i, small_r, small_i) = if self_is_big {
(
self.real.sign_extend().w_shl(shift),
self.imaginary.sign_extend().w_shl(shift),
scalar_n1.sign_extend(),
F::zero().sign_extend(),
)
} else {
(
scalar_n1.sign_extend().w_shl(shift),
F::zero().sign_extend(),
self.real.sign_extend(),
self.imaginary.sign_extend(),
)
};
let result_r = big_r.w_add(small_r);
let result_i = big_i.w_add(small_i);
if result_r.w_is_zero() && result_i.w_is_zero() {
return Self::ZERO;
}
let leading_r = result_r.leading_same();
let leading_i = result_i.leading_same();
let leading = leading_r.min(leading_i);
let fb = Self::fraction_bits();
let delta: isize = fb.wrapping_sub(leading).wrapping_add(1);
let delta_e: E = delta.as_();
let offset = small_exp.wrapping_add(&delta_e);
let shl_amount = leading.wrapping_sub(fb).wrapping_sub(1);
let canonical_r = if shl_amount >= 0 {
result_r.w_shl(shl_amount)
} else {
result_r.w_shr(shl_amount.wrapping_neg())
};
let canonical_i = if shl_amount >= 0 {
result_i.w_shl(shl_amount)
} else {
result_i.w_shr(shl_amount.wrapping_neg())
};
return Self {
real: canonical_r.deflate(),
imaginary: canonical_i.deflate(),
exponent: offset,
};
}
// Escape-class handling (at least one operand is non-normal). Mirrors scalar_add_scalar's order: undefined → INFINITY-absorbs → zero identity → escape combinations. The earlier `is_transfinite()` (inf OR exploded) caused inf cases to return TRANSFINITE_PLUS_FINITE (= undefined) instead of INFINITY.
{
if self.is_undefined() {
return *self;
}
if scalar.is_undefined() {
return Circle {
real: scalar.fraction,
imaginary: scalar.fraction,
exponent: scalar.exponent,
};
}
// [∞] absorbs (signless — −∞ no-op so ∞+X and X+∞ both yield ∞).
if self.is_infinite() || scalar.is_infinite() {
return Circle::<F, E>::INFINITY;
}
// Zero identity.
if scalar.is_zero() {
return *self;
}
if self.is_zero() {
return Circle::<F, E>::from_ri(*scalar, Scalar::<F, E>::ZERO);
}
if self.exploded() && scalar.exploded() {
return Self {
real: TRANSFINITE_PLUS_TRANSFINITE.prefix.sa(),
imaginary: TRANSFINITE_PLUS_TRANSFINITE.prefix.sa(),
exponent: Self::ambiguous_exponent(),
};
}
if self.vanished() && scalar.vanished() {
return Self {
real: VANISHED_PLUS_VANISHED.prefix.sa(),
imaginary: VANISHED_PLUS_VANISHED.prefix.sa(),
exponent: Self::ambiguous_exponent(),
};
}
// Single exploded: exp + van = exp; exp + normal = [℘].
if self.exploded() {
if scalar.vanished() {
return *self;
}
return Self {
real: TRANSFINITE_PLUS_FINITE.prefix.sa(),
imaginary: TRANSFINITE_PLUS_FINITE.prefix.sa(),
exponent: Self::ambiguous_exponent(),
};
}
if scalar.exploded() {
if self.vanished() {
return Circle::<F, E>::from_ri(*scalar, Scalar::<F, E>::ZERO);
}
return Self {
real: FINITE_PLUS_TRANSFINITE.prefix.sa(),
imaginary: FINITE_PLUS_TRANSFINITE.prefix.sa(),
exponent: Self::ambiguous_exponent(),
};
}
// Single vanished (other is normal here — zero/exp/inf/undef already handled).
if self.vanished() {
return Circle::<F, E>::from_ri(*scalar, Scalar::<F, E>::ZERO);
}
if scalar.vanished() {
return *self;
}
*self
}
}
}