edgevec 0.9.0

High-performance embedded vector database for Browser, Node, and Edge
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
//! Binary Quantization for vector compression.
//!
//! Compresses 768-dimensional f32 vectors into 96-byte binary representations
//! using sign-based quantization with Hamming distance computation.
//!
//! # Algorithm
//!
//! For each dimension `i` in the input vector:
//! - If `vector[i] > 0.0`, set bit `i` to 1
//! - Otherwise, set bit `i` to 0
//!
//! # Binary Format Specification
//!
//! The output is a fixed 96-byte array with the following layout:
//!
//! | Bytes | Bits | Description |
//! |-------|------|-------------|
//! | 0-95  | 0-767 | Packed sign bits |
//!
//! **Bit Ordering (Little-Endian):**
//! - Byte 0 contains bits [0..8] of the source vector
//! - Bit 0 (LSB) = dimension 0, Bit 7 (MSB) = dimension 7
//! - Byte 1 contains bits [8..16], and so on
//!
//! **Example:**
//! ```text
//! Source vector: [+, -, +, -, +, -, +, -, ...]
//! Bit pattern:   [1, 0, 1, 0, 1, 0, 1, 0, ...]
//! Byte 0:        0b01010101 = 0x55
//! ```
//!
//! # Complexity
//!
//! | Operation | Time | Space |
//! |-----------|------|-------|
//! | Quantize | O(n) | O(n/8) |
//! | Hamming Distance | O(n/8) | O(1) |
//! | Similarity | O(n/8) | O(1) |
//!
//! Where n = dimension (768 for standard embeddings).
//!
//! # Performance
//!
//! - Compression ratio: 8x (768 f32 = 3072 bytes → 96 bytes)
//! - Quantization: O(n) time, O(n/8) space
//! - Hamming distance: O(n/8) time, O(1) space
//! - Target: <50 CPU cycles per comparison (with SIMD in W9.3)
//!
//! # SIMD Alignment
//!
//! The `QuantizedVector` struct is 64-byte aligned (`#[repr(C, align(64))]`)
//! for compatibility with AVX-512 SIMD operations. This alignment is
//! guaranteed by the Rust compiler and applies to both stack and heap
//! allocations when using `Box<QuantizedVector>`.
//!
//! # Special Value Handling
//!
//! | Input Value | Bit Value | Reason |
//! |-------------|-----------|--------|
//! | NaN | 0 | `NaN > 0.0` is false (IEEE 754) |
//! | +Inf | 1 | `+Inf > 0.0` is true |
//! | -Inf | 0 | `-Inf > 0.0` is false |
//! | -0.0 | 0 | `-0.0 > 0.0` is false |
//! | Subnormal | depends | Compared normally |

// SPDX-License-Identifier: MIT
// Adapted from binary_semantic_cache v1.0 (MIT License)
// Copyright (c) 2024 Matteo Panzeri
// Original: https://github.com/MatteoPossamai/binary_semantic_cache

/// The expected dimension for binary quantization (768D embeddings).
pub const BINARY_QUANTIZATION_DIM: usize = 768;

/// The size of a quantized vector in bytes (768 bits = 96 bytes).
pub const QUANTIZED_VECTOR_SIZE: usize = 96;

/// Quantized binary vector (96 bytes, 768 bits).
///
/// Each bit represents the sign of the original f32 value:
/// - Bit = 1 if f32 > 0.0
/// - Bit = 0 if f32 <= 0.0
///
/// # Memory Layout
///
/// The struct is aligned to 64 bytes for SIMD compatibility.
///
/// # Example
///
/// ```
/// use edgevec::quantization::binary::{BinaryQuantizer, QuantizedVector};
///
/// let quantizer = BinaryQuantizer::new();
/// let vector = vec![1.0f32; 768];
/// let quantized = quantizer.quantize(&vector);
///
/// // All positive values result in all bits set
/// assert_eq!(quantized.data()[0], 0xFF);
/// ```
#[repr(C, align(64))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct QuantizedVector {
    /// Packed binary data (768 bits = 96 bytes).
    data: [u8; QUANTIZED_VECTOR_SIZE],
}

impl QuantizedVector {
    /// Creates a new `QuantizedVector` from raw bytes.
    ///
    /// # Arguments
    ///
    /// * `data` - Exactly 96 bytes of packed binary data.
    ///
    /// # Example
    ///
    /// ```
    /// use edgevec::quantization::binary::QuantizedVector;
    ///
    /// let data = [0u8; 96];
    /// let qv = QuantizedVector::from_bytes(data);
    /// assert_eq!(qv.data(), &[0u8; 96]);
    /// ```
    #[must_use]
    pub const fn from_bytes(data: [u8; QUANTIZED_VECTOR_SIZE]) -> Self {
        Self { data }
    }

    /// Returns a reference to the underlying byte data.
    ///
    /// # Returns
    ///
    /// A reference to the 96-byte array containing the packed binary representation.
    /// Each byte contains 8 bits of quantized data in little-endian bit order.
    ///
    /// # Example
    ///
    /// ```
    /// use edgevec::quantization::binary::QuantizedVector;
    ///
    /// let qv = QuantizedVector::from_bytes([0xAA; 96]);
    /// let data = qv.data();
    /// assert_eq!(data.len(), 96);
    /// assert_eq!(data[0], 0xAA);
    /// ```
    #[must_use]
    pub const fn data(&self) -> &[u8; QUANTIZED_VECTOR_SIZE] {
        &self.data
    }

    /// Computes the Hamming distance to another quantized vector.
    ///
    /// Hamming distance is the number of differing bits between two vectors.
    ///
    /// # Algorithm
    ///
    /// 1. XOR the two binary vectors (differing bits become 1)
    /// 2. Count the number of 1 bits (popcount)
    ///
    /// # Performance
    ///
    /// Automatically uses the fastest available implementation:
    /// - AVX2 (x86_64 with AVX2): ~47 CPU cycles
    /// - Portable fallback: ~300 CPU cycles
    ///
    /// Implementation uses runtime CPU feature detection for optimal performance
    /// across all platforms.
    ///
    /// # Example
    ///
    /// ```
    /// use edgevec::quantization::binary::QuantizedVector;
    ///
    /// let v1 = QuantizedVector::from_bytes([0x00; 96]);
    /// let v2 = QuantizedVector::from_bytes([0xFF; 96]);
    ///
    /// // All 768 bits differ
    /// assert_eq!(v1.hamming_distance(&v2), 768);
    /// ```
    #[must_use]
    pub fn hamming_distance(&self, other: &Self) -> u32 {
        // Delegate to SIMD dispatcher
        // This automatically selects AVX2 on capable hardware or portable fallback
        crate::quantization::simd::hamming_distance(&self.data, &other.data)
    }

    /// Returns the Hamming distance as a normalized similarity score [0, 1].
    ///
    /// - 1.0 = identical vectors (distance = 0)
    /// - 0.0 = completely opposite vectors (distance = 768)
    ///
    /// # Example
    ///
    /// ```
    /// use edgevec::quantization::binary::QuantizedVector;
    ///
    /// let v1 = QuantizedVector::from_bytes([0xAA; 96]); // 10101010...
    /// let v2 = QuantizedVector::from_bytes([0xAA; 96]); // same pattern
    ///
    /// assert!((v1.similarity(&v2) - 1.0).abs() < f32::EPSILON);
    /// ```
    #[must_use]
    #[allow(clippy::cast_precision_loss)] // 768 and distances up to 768 fit easily in f32's 23-bit mantissa
    pub fn similarity(&self, other: &Self) -> f32 {
        let distance = self.hamming_distance(other);
        1.0 - (distance as f32 / BINARY_QUANTIZATION_DIM as f32)
    }
}

impl Default for QuantizedVector {
    fn default() -> Self {
        Self {
            data: [0u8; QUANTIZED_VECTOR_SIZE],
        }
    }
}

/// Binary quantizer compressing f32 vectors to binary (u8) representation.
///
/// # Memory Layout
///
/// - Input: `[f32; 768]` (3072 bytes)
/// - Output: `[u8; 96]` (96 bytes, 64-byte aligned)
/// - Compression: 8x
///
/// # Algorithm
///
/// Sign-based quantization: each bit represents whether the corresponding
/// f32 value is positive (1) or non-positive (0).
///
/// # Example
///
/// ```
/// use edgevec::quantization::binary::BinaryQuantizer;
///
/// let quantizer = BinaryQuantizer::new();
/// let vector = vec![0.5f32; 768];
/// let quantized = quantizer.quantize(&vector);
///
/// // 768 dimensions compressed to 96 bytes
/// assert_eq!(quantized.data().len(), 96);
/// ```
#[derive(Clone, Debug, Default)]
pub struct BinaryQuantizer {
    // Stateless quantizer - no configuration needed for sign-based quantization
    _marker: std::marker::PhantomData<()>,
}

impl BinaryQuantizer {
    /// Creates a new `BinaryQuantizer`.
    ///
    /// The quantizer is stateless - no training or configuration required
    /// for simple sign-based quantization.
    ///
    /// # Example
    ///
    /// ```
    /// use edgevec::quantization::binary::BinaryQuantizer;
    ///
    /// let quantizer = BinaryQuantizer::new();
    /// ```
    #[must_use]
    pub const fn new() -> Self {
        Self {
            _marker: std::marker::PhantomData,
        }
    }

    /// Quantizes a 768-dimensional f32 vector to binary representation.
    ///
    /// # Arguments
    ///
    /// * `vector` - A 768-dimensional f32 vector.
    ///
    /// # Panics
    ///
    /// Panics if the input vector length is not exactly 768.
    ///
    /// # Algorithm
    ///
    /// For each dimension `i` in `[0, 768)`:
    /// - If `vector[i] > 0.0`, set bit `i` to 1
    /// - Else, set bit `i` to 0
    ///
    /// Bits are packed into bytes in little-endian order:
    /// - Byte 0 contains bits `[0..8]`
    /// - Byte 1 contains bits `[8..16]`
    /// - ...
    /// - Byte 95 contains bits `[760..768]`
    ///
    /// # Performance
    ///
    /// Target: <1ms per vector.
    ///
    /// # Example
    ///
    /// ```
    /// use edgevec::quantization::binary::BinaryQuantizer;
    ///
    /// let quantizer = BinaryQuantizer::new();
    ///
    /// // All positive -> all bits 1
    /// let positive = vec![1.0f32; 768];
    /// let q_pos = quantizer.quantize(&positive);
    /// assert_eq!(q_pos.data()[0], 0xFF);
    ///
    /// // All negative -> all bits 0
    /// let negative = vec![-1.0f32; 768];
    /// let q_neg = quantizer.quantize(&negative);
    /// assert_eq!(q_neg.data()[0], 0x00);
    /// ```
    #[must_use]
    pub fn quantize(&self, vector: &[f32]) -> QuantizedVector {
        assert_eq!(
            vector.len(),
            BINARY_QUANTIZATION_DIM,
            "Input must be {BINARY_QUANTIZATION_DIM}-dimensional, got {}",
            vector.len()
        );

        let mut data = [0u8; QUANTIZED_VECTOR_SIZE];

        // Adapted from binary_semantic_cache v1.0 (MIT License)
        // Copyright (c) 2024 Matteo Panzeri
        for (i, &value) in vector.iter().enumerate() {
            if value > 0.0 {
                let byte_idx = i / 8;
                let bit_idx = i % 8;
                data[byte_idx] |= 1 << bit_idx;
            }
        }

        QuantizedVector { data }
    }

    /// Quantizes a vector of arbitrary dimension.
    ///
    /// Unlike `quantize`, this method accepts vectors of any length and
    /// pads or truncates as needed. Primarily for testing and flexibility.
    ///
    /// # Arguments
    ///
    /// * `vector` - An f32 vector of any length.
    ///
    /// # Returns
    ///
    /// A `QuantizedVector` where:
    /// - If input is shorter than 768, remaining bits are 0
    /// - If input is longer than 768, extra values are ignored
    ///
    /// # Examples
    ///
    /// ```
    /// use edgevec::quantization::binary::BinaryQuantizer;
    ///
    /// let quantizer = BinaryQuantizer::new();
    ///
    /// // Short vector (16 elements) - remaining bits are 0
    /// let short = vec![1.0f32; 16];
    /// let q_short = quantizer.quantize_flexible(&short);
    /// assert_eq!(q_short.data()[0], 0xFF); // First 8 bits set
    /// assert_eq!(q_short.data()[1], 0xFF); // Next 8 bits set
    /// assert_eq!(q_short.data()[2], 0x00); // Rest are 0
    ///
    /// // Long vector (1000 elements) - truncated to 768
    /// let long = vec![1.0f32; 1000];
    /// let q_long = quantizer.quantize_flexible(&long);
    /// assert_eq!(q_long.data().len(), 96); // Always 96 bytes
    /// ```
    #[must_use]
    pub fn quantize_flexible(&self, vector: &[f32]) -> QuantizedVector {
        let mut data = [0u8; QUANTIZED_VECTOR_SIZE];
        let len = vector.len().min(BINARY_QUANTIZATION_DIM);

        for (i, &value) in vector.iter().take(len).enumerate() {
            if value > 0.0 {
                let byte_idx = i / 8;
                let bit_idx = i % 8;
                data[byte_idx] |= 1 << bit_idx;
            }
        }

        QuantizedVector { data }
    }

    /// Quantizes a vector of arbitrary dimension to a byte vector.
    ///
    /// This is a static method that converts an f32 vector of any size to
    /// a packed binary vector (1 bit per dimension).
    ///
    /// # Arguments
    ///
    /// * `vector` - An f32 vector of any length.
    ///
    /// # Returns
    ///
    /// A `Vec<u8>` of length `ceil(vector.len() / 8)` containing the packed bits.
    ///
    /// # Algorithm
    ///
    /// For each dimension `i`:
    /// - If `vector[i] > 0.0`, set bit `i` to 1
    /// - Otherwise, set bit `i` to 0
    ///
    /// # Examples
    ///
    /// ```
    /// use edgevec::quantization::binary::BinaryQuantizer;
    ///
    /// // 16-element vector -> 2 bytes
    /// let v = vec![1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0,
    ///              1.0, -1.0, 1.0, -1.0, 1.0, -1.0, 1.0, -1.0];
    /// let binary = BinaryQuantizer::quantize_to_bytes(&v);
    /// assert_eq!(binary.len(), 2);
    /// assert_eq!(binary[0], 0b01010101); // LSB-first: positions 0,2,4,6 are 1
    /// ```
    #[must_use]
    pub fn quantize_to_bytes(vector: &[f32]) -> Vec<u8> {
        let num_bytes = (vector.len() + 7) / 8;
        let mut data = vec![0u8; num_bytes];

        for (i, &value) in vector.iter().enumerate() {
            if value > 0.0 {
                let byte_idx = i / 8;
                let bit_idx = i % 8;
                data[byte_idx] |= 1 << bit_idx;
            }
        }

        data
    }
}

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

    #[test]
    fn test_quantize_zero_vector() {
        let quantizer = BinaryQuantizer::new();
        let zero = vec![0.0f32; BINARY_QUANTIZATION_DIM];
        let quantized = quantizer.quantize(&zero);

        // All bits should be 0 (0.0 <= 0.0)
        assert_eq!(quantized.data, [0u8; QUANTIZED_VECTOR_SIZE]);
    }

    #[test]
    fn test_quantize_positive_vector() {
        let quantizer = BinaryQuantizer::new();
        let positive = vec![1.0f32; BINARY_QUANTIZATION_DIM];
        let quantized = quantizer.quantize(&positive);

        // All bits should be 1 (1.0 > 0.0)
        assert_eq!(quantized.data, [0xFFu8; QUANTIZED_VECTOR_SIZE]);
    }

    #[test]
    fn test_quantize_negative_vector() {
        let quantizer = BinaryQuantizer::new();
        let negative = vec![-1.0f32; BINARY_QUANTIZATION_DIM];
        let quantized = quantizer.quantize(&negative);

        // All bits should be 0 (-1.0 <= 0.0)
        assert_eq!(quantized.data, [0u8; QUANTIZED_VECTOR_SIZE]);
    }

    #[test]
    fn test_quantize_mixed_vector() {
        let quantizer = BinaryQuantizer::new();
        let mut mixed = vec![-1.0f32; BINARY_QUANTIZATION_DIM];
        mixed[0] = 1.0; // First bit should be 1
        mixed[8] = 1.0; // Ninth bit should be 1 (second byte, bit 0)

        let quantized = quantizer.quantize(&mixed);

        assert_eq!(quantized.data[0], 0b0000_0001); // Bit 0 set
        assert_eq!(quantized.data[1], 0b0000_0001); // Bit 8 set
                                                    // Rest should be 0
        for i in 2..QUANTIZED_VECTOR_SIZE {
            assert_eq!(quantized.data[i], 0);
        }
    }

    #[test]
    fn test_quantize_alternating() {
        let quantizer = BinaryQuantizer::new();
        let alternating: Vec<f32> = (0..BINARY_QUANTIZATION_DIM)
            .map(|i| if i % 2 == 0 { 1.0 } else { -1.0 })
            .collect();

        let quantized = quantizer.quantize(&alternating);

        // Even indices are positive -> bits 0, 2, 4, 6 set in each byte
        // Binary: 01010101 = 0x55
        for byte in &quantized.data {
            assert_eq!(*byte, 0x55);
        }
    }

    #[test]
    fn test_hamming_distance_identical() {
        let quantizer = BinaryQuantizer::new();
        let vec = vec![0.5f32; BINARY_QUANTIZATION_DIM];
        let q1 = quantizer.quantize(&vec);
        let q2 = quantizer.quantize(&vec);

        assert_eq!(q1.hamming_distance(&q2), 0);
    }

    #[test]
    #[allow(clippy::cast_possible_truncation)]
    fn test_hamming_distance_opposite() {
        let q1 = QuantizedVector::from_bytes([0x00u8; QUANTIZED_VECTOR_SIZE]);
        let q2 = QuantizedVector::from_bytes([0xFFu8; QUANTIZED_VECTOR_SIZE]);

        assert_eq!(q1.hamming_distance(&q2), BINARY_QUANTIZATION_DIM as u32);
    }

    #[test]
    #[allow(clippy::cast_possible_truncation)]
    fn test_hamming_distance_symmetric() {
        let q1 = QuantizedVector::from_bytes([0xAAu8; QUANTIZED_VECTOR_SIZE]); // 10101010...
        let q2 = QuantizedVector::from_bytes([0x55u8; QUANTIZED_VECTOR_SIZE]); // 01010101...

        assert_eq!(q1.hamming_distance(&q2), q2.hamming_distance(&q1));
        // Each byte has all 8 bits different
        assert_eq!(q1.hamming_distance(&q2), BINARY_QUANTIZATION_DIM as u32);
    }

    #[test]
    #[allow(clippy::cast_possible_truncation)]
    fn test_hamming_distance_partial() {
        let q1 = QuantizedVector::from_bytes([0xF0u8; QUANTIZED_VECTOR_SIZE]); // 11110000
        let q2 = QuantizedVector::from_bytes([0x0Fu8; QUANTIZED_VECTOR_SIZE]); // 00001111

        // Each byte differs in all 8 bits
        assert_eq!(q1.hamming_distance(&q2), BINARY_QUANTIZATION_DIM as u32);
    }

    #[test]
    fn test_quantize_deterministic() {
        let quantizer = BinaryQuantizer::new();
        let vec = vec![0.123f32; BINARY_QUANTIZATION_DIM];

        let q1 = quantizer.quantize(&vec);
        let q2 = quantizer.quantize(&vec);

        assert_eq!(q1, q2); // Must be deterministic
    }

    #[test]
    fn test_alignment() {
        let q = QuantizedVector::default();
        let ptr = std::ptr::addr_of!(q) as usize;

        assert_eq!(
            ptr % 64,
            0,
            "QuantizedVector must be 64-byte aligned, got alignment {}",
            ptr % 64
        );
    }

    #[test]
    fn test_struct_size() {
        assert_eq!(
            std::mem::size_of::<QuantizedVector>(),
            128, // 96 bytes + padding for 64-byte alignment
            "QuantizedVector size should be 128 bytes (96 data + padding)"
        );
    }

    #[test]
    fn test_struct_alignment() {
        assert_eq!(
            std::mem::align_of::<QuantizedVector>(),
            64,
            "QuantizedVector must have 64-byte alignment"
        );
    }

    #[test]
    fn test_similarity_identical() {
        let q1 = QuantizedVector::from_bytes([0xAAu8; QUANTIZED_VECTOR_SIZE]);
        let q2 = QuantizedVector::from_bytes([0xAAu8; QUANTIZED_VECTOR_SIZE]);

        assert!((q1.similarity(&q2) - 1.0).abs() < f32::EPSILON);
    }

    #[test]
    fn test_similarity_opposite() {
        let q1 = QuantizedVector::from_bytes([0x00u8; QUANTIZED_VECTOR_SIZE]);
        let q2 = QuantizedVector::from_bytes([0xFFu8; QUANTIZED_VECTOR_SIZE]);

        assert!((q1.similarity(&q2) - 0.0).abs() < f32::EPSILON);
    }

    #[test]
    fn test_quantize_flexible_short() {
        let quantizer = BinaryQuantizer::new();
        let short = vec![1.0f32; 16]; // Only 16 elements

        let quantized = quantizer.quantize_flexible(&short);

        // First 2 bytes should be 0xFF (16 bits set)
        assert_eq!(quantized.data[0], 0xFF);
        assert_eq!(quantized.data[1], 0xFF);
        // Rest should be 0
        for i in 2..QUANTIZED_VECTOR_SIZE {
            assert_eq!(quantized.data[i], 0);
        }
    }

    #[test]
    #[should_panic(expected = "Input must be 768-dimensional")]
    fn test_quantize_wrong_dimension() {
        let quantizer = BinaryQuantizer::new();
        let wrong_dim = vec![1.0f32; 100];
        let _ = quantizer.quantize(&wrong_dim);
    }

    #[test]
    fn test_edge_case_nan() {
        let quantizer = BinaryQuantizer::new();
        let mut vec = vec![1.0f32; BINARY_QUANTIZATION_DIM];
        vec[0] = f32::NAN; // NaN is not > 0.0, so bit should be 0

        let quantized = quantizer.quantize(&vec);

        // First bit should be 0 (NaN is not positive)
        assert_eq!(quantized.data[0] & 1, 0);
    }

    #[test]
    fn test_edge_case_infinity() {
        let quantizer = BinaryQuantizer::new();
        let mut vec = vec![1.0f32; BINARY_QUANTIZATION_DIM];
        vec[0] = f32::INFINITY; // +Inf > 0.0, so bit should be 1
        vec[1] = f32::NEG_INFINITY; // -Inf < 0.0, so bit should be 0

        let quantized = quantizer.quantize(&vec);

        // Bit 0 should be 1 (positive infinity)
        assert_eq!(quantized.data[0] & 0b01, 0b01);
        // Bit 1 should be 0 (negative infinity)
        assert_eq!(quantized.data[0] & 0b10, 0b00);
    }

    #[test]
    fn test_edge_case_negative_zero() {
        let quantizer = BinaryQuantizer::new();
        let mut vec = vec![1.0f32; BINARY_QUANTIZATION_DIM];
        vec[0] = -0.0f32; // -0.0 is not > 0.0, so bit should be 0

        let quantized = quantizer.quantize(&vec);

        // First bit should be 0 (-0.0 is not positive)
        assert_eq!(quantized.data[0] & 1, 0);
    }

    #[test]
    #[allow(clippy::cast_possible_truncation)]
    fn test_hamming_bounds() {
        let q1 = QuantizedVector::default();
        let q2 = QuantizedVector::from_bytes([0xFFu8; QUANTIZED_VECTOR_SIZE]);

        let distance = q1.hamming_distance(&q2);

        // Distance must be in valid range [0, 768]
        assert!(distance <= BINARY_QUANTIZATION_DIM as u32);
    }
}

#[cfg(test)]
#[allow(clippy::cast_possible_truncation)]
mod proptests {
    use super::*;
    use proptest::prelude::*;

    /// Generate a valid 768-dimensional vector with values in [-1, 1].
    fn valid_vector_strategy() -> impl Strategy<Value = Vec<f32>> {
        proptest::collection::vec(-1.0f32..=1.0f32, BINARY_QUANTIZATION_DIM)
    }

    proptest! {
        /// Property: Quantization is deterministic.
        /// quantize(v) == quantize(v) for all valid vectors.
        #[test]
        fn prop_quantize_deterministic(v in valid_vector_strategy()) {
            let quantizer = BinaryQuantizer::new();
            let q1 = quantizer.quantize(&v);
            let q2 = quantizer.quantize(&v);
            prop_assert_eq!(q1, q2);
        }

        /// Property: Self-distance is always zero.
        /// hamming_distance(q, q) == 0 for all quantized vectors.
        #[test]
        fn prop_self_distance_zero(v in valid_vector_strategy()) {
            let quantizer = BinaryQuantizer::new();
            let q = quantizer.quantize(&v);
            prop_assert_eq!(q.hamming_distance(&q), 0);
        }

        /// Property: Hamming distance is symmetric.
        /// hamming_distance(a, b) == hamming_distance(b, a).
        #[test]
        fn prop_hamming_symmetric(
            v1 in valid_vector_strategy(),
            v2 in valid_vector_strategy()
        ) {
            let quantizer = BinaryQuantizer::new();
            let q1 = quantizer.quantize(&v1);
            let q2 = quantizer.quantize(&v2);
            prop_assert_eq!(q1.hamming_distance(&q2), q2.hamming_distance(&q1));
        }

        /// Property: Hamming distance is bounded by dimension.
        /// 0 <= hamming_distance(a, b) <= 768.
        #[test]
        fn prop_hamming_bounded(
            v1 in valid_vector_strategy(),
            v2 in valid_vector_strategy()
        ) {
            let quantizer = BinaryQuantizer::new();
            let q1 = quantizer.quantize(&v1);
            let q2 = quantizer.quantize(&v2);
            let dist = q1.hamming_distance(&q2);
            prop_assert!(dist <= BINARY_QUANTIZATION_DIM as u32);
        }

        /// Property: Similarity is in valid range [0, 1].
        #[test]
        fn prop_similarity_bounded(
            v1 in valid_vector_strategy(),
            v2 in valid_vector_strategy()
        ) {
            let quantizer = BinaryQuantizer::new();
            let q1 = quantizer.quantize(&v1);
            let q2 = quantizer.quantize(&v2);
            let sim = q1.similarity(&q2);
            prop_assert!(sim >= 0.0);
            prop_assert!(sim <= 1.0);
        }

        /// Property: Triangle inequality for Hamming distance.
        /// hamming_distance(a, c) <= hamming_distance(a, b) + hamming_distance(b, c).
        #[test]
        fn prop_triangle_inequality(
            v1 in valid_vector_strategy(),
            v2 in valid_vector_strategy(),
            v3 in valid_vector_strategy()
        ) {
            let quantizer = BinaryQuantizer::new();
            let q1 = quantizer.quantize(&v1);
            let q2 = quantizer.quantize(&v2);
            let q3 = quantizer.quantize(&v3);

            let d12 = q1.hamming_distance(&q2);
            let d23 = q2.hamming_distance(&q3);
            let d13 = q1.hamming_distance(&q3);

            prop_assert!(d13 <= d12 + d23,
                "Triangle inequality violated: {} > {} + {}", d13, d12, d23);
        }

        /// Property: Output size is always exactly 96 bytes.
        #[test]
        fn prop_output_size_constant(v in valid_vector_strategy()) {
            let quantizer = BinaryQuantizer::new();
            let q = quantizer.quantize(&v);
            prop_assert_eq!(q.data().len(), QUANTIZED_VECTOR_SIZE);
        }

        /// Property: Sign preservation - if all values are positive, all bits are 1.
        #[test]
        fn prop_all_positive_all_ones(scale in 0.001f32..10.0f32) {
            let v: Vec<f32> = (0..BINARY_QUANTIZATION_DIM).map(|_| scale).collect();
            let quantizer = BinaryQuantizer::new();
            let q = quantizer.quantize(&v);
            for byte in q.data() {
                prop_assert_eq!(*byte, 0xFF);
            }
        }

        /// Property: Sign preservation - if all values are negative, all bits are 0.
        #[test]
        fn prop_all_negative_all_zeros(scale in 0.001f32..10.0f32) {
            let v: Vec<f32> = (0..BINARY_QUANTIZATION_DIM).map(|_| -scale).collect();
            let quantizer = BinaryQuantizer::new();
            let q = quantizer.quantize(&v);
            for byte in q.data() {
                prop_assert_eq!(*byte, 0x00);
            }
        }
    }
}