kizzasi-core 0.2.1

Core SSM (State Space Model) engine for Kizzasi AGSP
Documentation
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
//! Fixed-Point Arithmetic for Embedded Systems
//!
//! This module provides fixed-point number representations and operations
//! for embedded systems without hardware floating-point units (FPUs).
//!
//! # Fixed-Point Formats
//!
//! - **Q15.16** (i32): 15 integer bits, 16 fractional bits, range [-32768, 32767]
//! - **Q7.8** (i16): 7 integer bits, 8 fractional bits, range [-128, 127]
//! - **Q31** (i32): 1 sign bit, 31 fractional bits, range [-1, 1)
//!
//! # Use Cases
//!
//! - Microcontrollers without FPU (ARM Cortex-M0/M3)
//! - Low-power embedded systems
//! - Real-time systems requiring deterministic timing
//! - DSP applications with fixed-point arithmetic
//!
//! # Performance
//!
//! - 10-100x faster than software floating-point
//! - Lower power consumption
//! - Deterministic execution time

use crate::error::{CoreError, CoreResult};
use core::ops::{Add, Div, Mul, Neg, Sub};

/// Q15.16 fixed-point number (32-bit signed integer)
///
/// Represents numbers in the range [-32768, 32767] with 16 fractional bits.
/// Precision: ~0.000015 (1/65536)
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Q15_16(i32);

impl Q15_16 {
    /// Number of fractional bits
    pub const FRAC_BITS: u32 = 16;

    /// Scaling factor (2^16)
    pub const SCALE: i32 = 1 << Self::FRAC_BITS;

    /// Minimum value (-32768)
    pub const MIN: Q15_16 = Q15_16(i32::MIN);

    /// Maximum value (~32767.99998)
    pub const MAX: Q15_16 = Q15_16(i32::MAX);

    /// Zero
    pub const ZERO: Q15_16 = Q15_16(0);

    /// One
    pub const ONE: Q15_16 = Q15_16(Self::SCALE);

    /// Create from raw i32 value (already scaled)
    #[inline]
    pub const fn from_raw(raw: i32) -> Self {
        Q15_16(raw)
    }

    /// Get raw i32 value
    #[inline]
    pub const fn raw(self) -> i32 {
        self.0
    }

    /// Create from f32 (for testing/initialization)
    #[inline]
    pub fn from_f32(val: f32) -> Self {
        Q15_16((val * Self::SCALE as f32) as i32)
    }

    /// Convert to f32 (for debugging/output)
    #[inline]
    pub fn to_f32(self) -> f32 {
        self.0 as f32 / Self::SCALE as f32
    }

    /// Create from integer
    #[inline]
    pub const fn from_int(val: i32) -> Self {
        Q15_16(val << Self::FRAC_BITS)
    }

    /// Get integer part
    #[inline]
    pub const fn to_int(self) -> i32 {
        self.0 >> Self::FRAC_BITS
    }

    /// Get fractional part (0.0 to 0.99998)
    #[inline]
    pub const fn frac(self) -> i32 {
        self.0 & ((1 << Self::FRAC_BITS) - 1)
    }

    /// Saturating addition
    #[inline]
    pub fn saturating_add(self, rhs: Self) -> Self {
        Q15_16(self.0.saturating_add(rhs.0))
    }

    /// Saturating subtraction
    #[inline]
    pub fn saturating_sub(self, rhs: Self) -> Self {
        Q15_16(self.0.saturating_sub(rhs.0))
    }

    /// Saturating multiplication
    #[inline]
    pub fn saturating_mul(self, rhs: Self) -> Self {
        let product = (self.0 as i64) * (rhs.0 as i64);
        let result = (product >> Self::FRAC_BITS) as i32;
        Q15_16(result.saturating_mul(1))
    }

    /// Absolute value
    #[inline]
    pub fn abs(self) -> Self {
        Q15_16(self.0.abs())
    }

    /// Square root (Newton-Raphson method)
    pub fn sqrt(self) -> Self {
        if self.0 <= 0 {
            return Q15_16::ZERO;
        }

        // Initial guess: x/2
        let mut x = Q15_16(self.0 >> 1);

        // Newton-Raphson: x_new = (x + n/x) / 2
        for _ in 0..8 {
            let x_next = (x + self / x) / Q15_16::from_int(2);
            if (x_next - x).abs().0 < 1 {
                break;
            }
            x = x_next;
        }

        x
    }

    /// Reciprocal (1/x) using Newton-Raphson
    pub fn recip(self) -> CoreResult<Self> {
        if self.0 == 0 {
            return Err(CoreError::Generic("Division by zero".to_string()));
        }

        // Initial guess: use a lookup table approach for better convergence
        // For x in range, 1/x is approximated
        let abs_val = self.abs().0;
        let mut x = if abs_val < (Self::SCALE / 2) {
            // |self| < 0.5, so 1/|self| > 2
            Q15_16::from_int(4)
        } else if abs_val < Self::SCALE {
            // 0.5 <= |self| < 1, so 1 < 1/|self| <= 2
            Q15_16::from_int(2)
        } else if abs_val < (Self::SCALE * 2) {
            // 1 <= |self| < 2, so 0.5 < 1/|self| <= 1
            Q15_16::ONE
        } else {
            // |self| >= 2, so 1/|self| <= 0.5
            Q15_16::from_f32(0.25)
        };

        // Handle sign
        if self.0 < 0 {
            x = -x;
        }

        // Newton-Raphson: x_new = x * (2 - self * x)
        for _ in 0..10 {
            let two = Q15_16::from_int(2);
            let x_next = x * (two - self * x);
            if (x_next - x).abs().0 < 2 {
                break;
            }
            x = x_next;
        }

        Ok(x)
    }

    /// Exponential approximation (e^x) using Taylor series
    pub fn exp(self) -> Self {
        // Taylor series: e^x = 1 + x + x²/2! + x³/3! + x⁴/4! + ...
        let mut result = Q15_16::ONE;
        let mut term = Q15_16::ONE;

        for n in 1..10 {
            term = term * self / Q15_16::from_int(n);
            result = result + term;
            if term.abs().0 < 4 {
                // Converged
                break;
            }
        }

        result
    }

    /// Natural logarithm approximation (ln(x))
    pub fn ln(self) -> CoreResult<Self> {
        if self.0 <= 0 {
            return Err(CoreError::Generic("ln of non-positive number".to_string()));
        }

        // ln(x) = ln(2^n * m) = n*ln(2) + ln(m) where m in [1, 2)
        let mut x = self;
        let mut n = 0i32;

        // Normalize to [1, 2)
        while x.0 >= (2 << Self::FRAC_BITS) {
            x = Q15_16(x.0 >> 1);
            n += 1;
        }
        while x.0 < Self::SCALE {
            x = Q15_16(x.0 << 1);
            n -= 1;
        }

        // Taylor series for ln(1 + y) where x = 1 + y
        let y = x - Q15_16::ONE;
        let mut result = y;
        let mut term = y;

        for i in 2..10 {
            term = term * y * Q15_16::from_int(-1) / Q15_16::from_int(i);
            result = result + term;
            if term.abs().0 < 4 {
                break;
            }
        }

        // Add n * ln(2)
        let ln2 = Q15_16::from_f32(core::f32::consts::LN_2);
        result = result + Q15_16::from_int(n) * ln2;

        Ok(result)
    }
}

impl Add for Q15_16 {
    type Output = Self;

    #[inline]
    fn add(self, rhs: Self) -> Self {
        Q15_16(self.0 + rhs.0)
    }
}

impl Sub for Q15_16 {
    type Output = Self;

    #[inline]
    fn sub(self, rhs: Self) -> Self {
        Q15_16(self.0 - rhs.0)
    }
}

impl Mul for Q15_16 {
    type Output = Self;

    #[inline]
    fn mul(self, rhs: Self) -> Self {
        // Use i64 to prevent overflow
        let product = (self.0 as i64) * (rhs.0 as i64);
        Q15_16((product >> Self::FRAC_BITS) as i32)
    }
}

impl Div for Q15_16 {
    type Output = Self;

    #[inline]
    fn div(self, rhs: Self) -> Self {
        // Use i64 to prevent overflow
        let dividend = (self.0 as i64) << Self::FRAC_BITS;
        Q15_16((dividend / rhs.0 as i64) as i32)
    }
}

impl Neg for Q15_16 {
    type Output = Self;

    #[inline]
    fn neg(self) -> Self {
        Q15_16(-self.0)
    }
}

/// Q7.8 fixed-point number (16-bit signed integer)
///
/// Represents numbers in the range [-128, 127] with 8 fractional bits.
/// More compact than Q15.16 but less precision.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Q7_8(i16);

impl Q7_8 {
    pub const FRAC_BITS: u32 = 8;
    pub const SCALE: i16 = 1 << Self::FRAC_BITS;
    pub const MIN: Q7_8 = Q7_8(i16::MIN);
    pub const MAX: Q7_8 = Q7_8(i16::MAX);
    pub const ZERO: Q7_8 = Q7_8(0);
    pub const ONE: Q7_8 = Q7_8(Self::SCALE);

    #[inline]
    pub const fn from_raw(raw: i16) -> Self {
        Q7_8(raw)
    }

    #[inline]
    pub const fn raw(self) -> i16 {
        self.0
    }

    #[inline]
    pub fn from_f32(val: f32) -> Self {
        Q7_8((val * Self::SCALE as f32) as i16)
    }

    #[inline]
    pub fn to_f32(self) -> f32 {
        self.0 as f32 / Self::SCALE as f32
    }

    #[inline]
    pub fn abs(self) -> Self {
        Q7_8(self.0.abs())
    }
}

impl Add for Q7_8 {
    type Output = Self;

    #[inline]
    fn add(self, rhs: Self) -> Self {
        Q7_8(self.0 + rhs.0)
    }
}

impl Sub for Q7_8 {
    type Output = Self;

    #[inline]
    fn sub(self, rhs: Self) -> Self {
        Q7_8(self.0 - rhs.0)
    }
}

impl Mul for Q7_8 {
    type Output = Self;

    #[inline]
    fn mul(self, rhs: Self) -> Self {
        let product = (self.0 as i32) * (rhs.0 as i32);
        Q7_8((product >> Self::FRAC_BITS) as i16)
    }
}

impl Div for Q7_8 {
    type Output = Self;

    #[inline]
    fn div(self, rhs: Self) -> Self {
        let dividend = (self.0 as i32) << Self::FRAC_BITS;
        Q7_8((dividend / rhs.0 as i32) as i16)
    }
}

impl Neg for Q7_8 {
    type Output = Self;

    #[inline]
    fn neg(self) -> Self {
        Q7_8(-self.0)
    }
}

/// Fixed-point vector operations
pub mod vec_ops {
    use super::*;

    /// Dot product for Q15.16 vectors
    pub fn dot_product_q15_16(a: &[Q15_16], b: &[Q15_16]) -> Q15_16 {
        let mut sum = Q15_16::ZERO;
        for (x, y) in a.iter().zip(b) {
            sum = sum + (*x * *y);
        }
        sum
    }

    /// ReLU activation for Q15.16
    pub fn relu_q15_16(x: &[Q15_16], y: &mut [Q15_16]) {
        for (xi, yi) in x.iter().zip(y) {
            *yi = if xi.0 > 0 { *xi } else { Q15_16::ZERO };
        }
    }

    /// Softmax approximation for Q15.16 (simplified for embedded)
    pub fn softmax_q15_16(x: &[Q15_16], y: &mut [Q15_16]) -> CoreResult<()> {
        // Find max for numerical stability
        let max = x.iter().max().copied().unwrap_or(Q15_16::ZERO);

        // Compute exp(x - max) for each element
        let mut sum = Q15_16::ZERO;
        for i in 0..x.len() {
            let shifted = x[i] - max;
            y[i] = shifted.exp();
            sum = sum + y[i];
        }

        // Normalize
        let recip_sum = sum.recip()?;
        for yi in y.iter_mut() {
            *yi = *yi * recip_sum;
        }

        Ok(())
    }

    /// Layer normalization for Q15.16
    pub fn layer_norm_q15_16(x: &[Q15_16], y: &mut [Q15_16], eps: Q15_16) -> CoreResult<()> {
        let n = Q15_16::from_int(x.len() as i32);

        // Compute mean
        let mut sum = Q15_16::ZERO;
        for &xi in x {
            sum = sum + xi;
        }
        let mean = sum / n;

        // Compute variance
        let mut var_sum = Q15_16::ZERO;
        for &xi in x {
            let diff = xi - mean;
            var_sum = var_sum + (diff * diff);
        }
        let variance = var_sum / n;

        // Compute 1/sqrt(variance + eps)
        let std_inv = (variance + eps).sqrt().recip()?;

        // Normalize
        for (i, &xi) in x.iter().enumerate() {
            y[i] = (xi - mean) * std_inv;
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_q15_16_basic() {
        let a = Q15_16::from_f32(3.5);
        let b = Q15_16::from_f32(2.0);

        let sum = a + b;
        assert!((sum.to_f32() - 5.5).abs() < 0.001);

        let diff = a - b;
        assert!((diff.to_f32() - 1.5).abs() < 0.001);

        let prod = a * b;
        assert!((prod.to_f32() - 7.0).abs() < 0.001);

        let quot = a / b;
        assert!((quot.to_f32() - 1.75).abs() < 0.001);
    }

    #[test]
    fn test_q15_16_sqrt() {
        let x = Q15_16::from_f32(9.0);
        let result = x.sqrt();
        assert!((result.to_f32() - 3.0).abs() < 0.01);

        let x2 = Q15_16::from_f32(2.0);
        let result2 = x2.sqrt();
        assert!((result2.to_f32() - 1.414).abs() < 0.01);
    }

    #[test]
    fn test_q15_16_exp() {
        let x = Q15_16::from_f32(1.0);
        let result = x.exp();
        assert!((result.to_f32() - core::f32::consts::E).abs() < 0.1);

        let x2 = Q15_16::from_f32(0.0);
        let result2 = x2.exp();
        assert!((result2.to_f32() - 1.0).abs() < 0.01);
    }

    #[test]
    fn test_q15_16_ln() {
        let x = Q15_16::from_f32(core::f32::consts::E);
        let result = x.ln().unwrap();
        assert!((result.to_f32() - 1.0).abs() < 0.1);

        let x2 = Q15_16::from_f32(1.0);
        let result2 = x2.ln().unwrap();
        assert!(result2.to_f32().abs() < 0.01);
    }

    #[test]
    fn test_q15_16_saturating() {
        let max = Q15_16::from_int(30000);
        let big = Q15_16::from_int(10000);

        let sum = max.saturating_add(big);
        assert_eq!(sum, Q15_16::MAX);
    }

    #[test]
    fn test_q7_8_basic() {
        let a = Q7_8::from_f32(3.5);
        let b = Q7_8::from_f32(2.0);

        let sum = a + b;
        assert!((sum.to_f32() - 5.5).abs() < 0.01);

        let prod = a * b;
        assert!((prod.to_f32() - 7.0).abs() < 0.01);
    }

    #[test]
    fn test_dot_product() {
        let a = vec![
            Q15_16::from_f32(1.0),
            Q15_16::from_f32(2.0),
            Q15_16::from_f32(3.0),
        ];
        let b = vec![
            Q15_16::from_f32(4.0),
            Q15_16::from_f32(5.0),
            Q15_16::from_f32(6.0),
        ];

        let result = vec_ops::dot_product_q15_16(&a, &b);
        let expected = 1.0 * 4.0 + 2.0 * 5.0 + 3.0 * 6.0; // 32.0

        assert!((result.to_f32() - expected).abs() < 0.01);
    }

    #[test]
    fn test_relu() {
        let x = vec![
            Q15_16::from_f32(-2.0),
            Q15_16::from_f32(-1.0),
            Q15_16::from_f32(0.0),
            Q15_16::from_f32(1.0),
            Q15_16::from_f32(2.0),
        ];
        let mut y = vec![Q15_16::ZERO; 5];

        vec_ops::relu_q15_16(&x, &mut y);

        assert_eq!(y[0], Q15_16::ZERO);
        assert_eq!(y[1], Q15_16::ZERO);
        assert_eq!(y[2], Q15_16::ZERO);
        assert!((y[3].to_f32() - 1.0).abs() < 0.01);
        assert!((y[4].to_f32() - 2.0).abs() < 0.01);
    }

    #[test]
    fn test_layer_norm() {
        let x = vec![
            Q15_16::from_f32(1.0),
            Q15_16::from_f32(2.0),
            Q15_16::from_f32(3.0),
            Q15_16::from_f32(4.0),
        ];
        let mut y = vec![Q15_16::ZERO; 4];
        let eps = Q15_16::from_f32(1e-5);

        vec_ops::layer_norm_q15_16(&x, &mut y, eps).unwrap();

        // Verify mean is close to 0
        let mean: f32 = y.iter().map(|yi| yi.to_f32()).sum::<f32>() / y.len() as f32;
        assert!(mean.abs() < 0.1);

        // Verify variance is close to 1 (fixed-point has lower precision)
        let variance: f32 = y.iter().map(|yi| yi.to_f32().powi(2)).sum::<f32>() / y.len() as f32;
        assert!((variance - 1.0).abs() < 0.5, "variance={}", variance);
    }

    #[test]
    fn test_conversion_precision() {
        let values = [0.0, 0.5, 1.0, -1.0, 100.5, -100.5];

        for &val in &values {
            let fixed = Q15_16::from_f32(val);
            let recovered = fixed.to_f32();
            assert!((val - recovered).abs() < 0.001, "Failed for {}", val);
        }
    }
}