softcore-prelude 0.6.0

The common functions used by the softcore-rs ecosystem
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
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
#![allow(incomplete_features, non_camel_case_types)]

use core::ops;
use std::cmp::min;

// NOTE: Ideally we would use unbounded integers for natural numbers. Yet in practice this would
// mess up with things such as the SMT solver during symbolic execution.
// After manual inspection, u128 are big enough for all the RISC-V use cases, so we keep that until
// a better solution is needed.
pub type nat = u128;

pub fn sail_branch_announce(_value: i128, _pc: BitVector<64>) {}

pub fn lteq_int(e1: i128, e2: i128) -> bool {
    e1 <= e2
}

pub fn gt_int(e1: i128, e2: i128) -> bool {
    e1 > e2
}

pub fn bitvector_length<const N: i128>(_e: BitVector<N>) -> i128 {
    N
}

pub fn parse_hex_bits<const N: i128>(_n: i128, _hex_str: &str) -> BitVector<N> {
    todo!("'parse_hex_bits' is not yet implemented");
}

pub fn bitvector_concat<const N: i128, const M: i128, const NM: i128>(
    e1: BitVector<N>,
    e2: BitVector<M>,
) -> BitVector<{ NM }> {
    bv::<{ NM }>((e1.bits() << M) | e2.bits())
}

pub const fn get_slice_int<const L: i128>(l: i128, n: i128, start: i128) -> BitVector<L> {
    let val = (n >> start) & (mask128(l as usize) as i128);
    bv(val as u64)
}

pub const fn slice<const N: i128, const M: i128>(
    bits: BitVector<M>,
    start: i128,
    len: i128,
) -> BitVector<N> {
    let mask = mask(len as usize);
    bv((bits.bits() >> start) & mask)
}

pub fn get_16_random_bits(_unit: ()) -> BitVector<16> {
    bv::<16>(0)
}

pub fn not_implemented<T>(_any: T) -> ! {
    panic!("Feature not implemented yet");
}

pub fn internal_error(_file: String, _line: i128, _s: String) -> ! {
    panic!("Softcore: internal error")
}

pub fn print_output<const N: i128>(text: String, _csr: BitVector<N>) {
    println!("{}", text)
}

pub fn print_platform(text: String) {
    println!("{}", text)
}

pub fn bits_str<const N: i128>(val: BitVector<N>) -> String {
    format!("{:b}", val.bits())
}

pub fn bitvector_access<const N: i128>(vec: BitVector<N>, idx: i128) -> bool {
    (vec.bits() & (1 << idx)) > 0
}

// Todo: implement truncate for other sizes if required
pub fn truncate(v: BitVector<64>, size: i128) -> BitVector<64> {
    assert!(size == 64);
    v
}

pub fn sail_sign_extend<const M: i128, const N: i128>(
    input: BitVector<M>,
    n: i128,
) -> BitVector<N> {
    assert!(n == N, "Mismatch `sail_sign_extend` size");
    assert!(N >= M, "Cannot sign extend to smaller size");
    assert!(N <= 64, "Maximum supported size is 64 for now");

    // Special case: when extending from same size to same size, it's a no-op
    if M == N {
        return bv::<N>(input.bits());
    }

    // Check if the sign bit (MSB) is set
    let sign_bit = (input.bits() >> (M - 1)) & 1;

    if sign_bit == 0 {
        // Positive number - just zero extend
        bv::<N>(input.bits())
    } else {
        // Negative number - fill upper bits with 1s
        // Handle the case where M=64 to avoid shift overflow
        let mask = if M == 64 { 0u64 } else { (1u64 << M) - 1 };
        let extension_bits = !mask & if N == 64 { u64::MAX } else { (1u64 << N) - 1 };
        bv::<N>(input.bits() | extension_bits)
    }
}

pub const fn sail_ones<const N: i128>(n: i128) -> BitVector<N> {
    assert!(n <= 64);
    bv::<N>(mask(n as usize))
}

pub const fn sail_zeros<const N: i128>(_n: i128) -> BitVector<N> {
    bv::<N>(0)
}

pub const fn sail_shiftright<const N: i128>(bits: BitVector<N>, shift: i128) -> BitVector<N> {
    bv(bits.bits() >> (shift as u64))
}

pub const fn sail_shiftleft<const N: i128>(bits: BitVector<N>, shift: i128) -> BitVector<N> {
    bv(bits.bits() << (shift as u64))
}

pub fn min_int(v1: i128, v2: i128) -> i128 {
    min(v1, v2)
}

pub fn cancel_reservation(_unit: ()) {
    // In the future, extend this function
}

fn hex_bits<const N: i128>(bits: &str) -> BitVector<N> {
    let parsed = bits.parse::<u64>().expect("Could not parse hex bits");
    bv(parsed)
}

pub fn hex_bits_12_forwards(_reg: BitVector<12>) -> ! {
    todo!("Implement this function")
}

pub fn hex_bits_12_backwards(bits: &str) -> BitVector<12> {
    hex_bits(bits)
}

pub fn hex_bits_12_backwards_matches(bits: &str) -> bool {
    match bits.parse::<u64>() {
        Ok(n) => n < (1 << 12),
        Err(_) => false,
    }
}

pub fn subrange_bits<const IN: i128, const OUT: i128>(
    vec: BitVector<IN>,
    end: i128,
    start: i128,
) -> BitVector<OUT> {
    assert_eq!((end - start + 1), OUT);
    assert!(OUT <= IN);

    bv((vec.bits >> start) & mask(OUT as usize))
}

pub fn update_subrange_bits<const N: i128, const M: i128>(
    bits: BitVector<N>,
    to: u64,
    from: u64,
    value: BitVector<M>,
) -> BitVector<N> {
    assert!(to - from + 1 == M as u64, "size don't match");

    // Generate the 111111 mask
    let mut mask = (1 << M) - 1;
    // Shit and invert it
    mask = !(mask << from);

    // Now we can update and return the updated value
    bv((bits.bits & mask) | (value.bits() << from))
}

pub fn bitvector_update<const N: i128>(v: BitVector<N>, pos: i128, value: bool) -> BitVector<N> {
    let mask = 1 << pos;
    bv((v.bits() & !mask) | (value as u64) << pos)
}

#[derive(Eq, PartialEq, Clone, Copy, Debug, Default)]
pub struct BitVector<const N: i128> {
    bits: u64,
}

#[derive(Eq, PartialEq, Clone, Copy, Debug, Default)]
pub struct BitField<const T: i128> {
    pub bits: BitVector<T>,
}

impl<const N: i128> BitField<N> {
    pub const fn new(value: u64) -> Self {
        BitField { bits: bv(value) }
    }

    pub const fn subrange<const A: i128, const B: i128, const C: i128>(self) -> BitVector<C> {
        assert!(B - A == C, "Invalid subrange parameters");
        assert!(B <= N, "Invalid subrange");

        self.bits.subrange::<A, B, C>()
    }

    pub const fn set_subrange<const A: i128, const B: i128, const C: i128>(
        self,
        bitvector: BitVector<C>,
    ) -> Self {
        assert!(B - A == C, "Invalid subrange parameters");
        assert!(A <= B && B <= N, "Invalid subrange");

        BitField::<N> {
            bits: self.bits.set_subrange::<A, B, C>(bitvector),
        }
    }
}

impl<const N: i128> PartialOrd for BitVector<N> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.bits.partial_cmp(&other.bits)
    }
}

/// Create a fresh [BitVector].
///
/// This is equivalent to [BitVector::new], with a shorted syntax.
pub const fn bv<const N: i128>(val: u64) -> BitVector<N> {
    BitVector::new(val)
}

impl<const N: i128> BitVector<N> {
    pub const fn new(val: u64) -> Self {
        if N < 64 {
            Self {
                bits: val & ((1 << N) - 1),
            }
        } else {
            Self { bits: val }
        }
    }

    pub const fn new_empty() -> Self {
        Self { bits: 0 }
    }

    pub const fn bits(self) -> u64 {
        self.bits
    }

    /// Get the bits as an integer.
    ///
    /// The bitvector is interpreted as unsigned.
    pub const fn unsigned(self) -> i128 {
        // Note that bits is unsigned, so converting to a bigger i128 guarantees the result is
        // still positive.
        self.bits as i128
    }

    /// Get the bits as an integer.
    ///
    /// The bitvector is interpreted as signed
    pub const fn signed(self) -> i128 {
        let value = self.bits as u128;
        let sign_bit_mask = 1 << (N - 1);
        if value & sign_bit_mask == 0 {
            // The number is positive, nothing to do
            value as i128
        } else {
            // The number is negative, need to fill upper bits with 1s
            let fill_mask = !((1 << N) - 1);
            (value | fill_mask) as i128
        }
    }

    pub const fn as_usize(self) -> usize {
        self.bits as usize
    }

    pub const fn as_i128(self) -> i128 {
        self.bits as i128
    }

    pub const fn zero_extend<const M: i128>(self) -> BitVector<M> {
        assert!(M >= N, "Can not zero-extend to a smaller size!");
        assert!(M <= 64, "Maximum zero-extend supported size if 64");

        // Here we have nothing to do, we already use 64 bits with zeroes for MSBs
        BitVector { bits: self.bits }
    }

    pub fn set_bit(self, idx: i128, value: bool) -> Self {
        assert!(idx < N, "Out of bounds array check");
        let new_value = if value {
            self.bits | 1u64 << idx
        } else {
            self.bits & !(1u64 << idx)
        };
        BitVector { bits: new_value }
    }

    pub const fn subrange<const A: i128, const B: i128, const C: i128>(self) -> BitVector<C> {
        assert!(B - A == C, "Invalid subrange parameters");
        assert!(B <= N, "Invalid subrange");

        let mut val = self.bits; // The current value
        val &= BitVector::<B>::bit_mask(); // Remove top bits
        val >>= A; // Shift all the bits
        bv(val)
    }

    pub const fn set_subrange<const A: i128, const B: i128, const C: i128>(
        self,
        bits: BitVector<C>,
    ) -> Self {
        assert!(B - A == C, "Invalid set_subrange parameters");
        assert!(B <= N, "Invalid subrange");

        let mask = !(BitVector::<C>::bit_mask() << A);
        let new_bits = bits.bits() << A;
        bv((self.bits & mask) | new_bits)
    }

    pub const fn wrapped_add(self, other: BitVector<N>) -> BitVector<N> {
        bv::<N>(self.bits.wrapping_add(other.bits))
    }

    /// Returns a bit mask with 1 for the first [N] bits.
    const fn bit_mask() -> u64 {
        assert!(N <= 64);

        if N == 64 { u64::MAX } else { (1 << N) - 1 }
    }
}

impl<const N: i128> ops::BitAnd for BitVector<N> {
    type Output = Self;

    fn bitand(self, rhs: Self) -> Self::Output {
        Self {
            bits: self.bits & rhs.bits,
        }
    }
}

impl<const N: i128> ops::BitOr for BitVector<N> {
    type Output = Self;

    fn bitor(self, rhs: Self) -> Self::Output {
        Self {
            bits: self.bits | rhs.bits,
        }
    }
}

impl<const N: i128> ops::BitXor for BitVector<N> {
    type Output = Self;

    fn bitxor(self, rhs: Self) -> Self::Output {
        Self {
            bits: self.bits ^ rhs.bits,
        }
    }
}

impl<const N: i128> ops::Shl<usize> for BitVector<N> {
    type Output = Self;

    fn shl(self, rhs: usize) -> Self::Output {
        Self {
            bits: self.bits << rhs,
        }
    }
}

impl<const N: i128> ops::Shl<i128> for BitVector<N> {
    type Output = Self;

    fn shl(self, rhs: i128) -> Self::Output {
        Self {
            bits: self.bits << rhs,
        }
    }
}

impl<const N: i128> ops::Shl<i32> for BitVector<N> {
    type Output = Self;

    fn shl(self, rhs: i32) -> Self::Output {
        Self {
            bits: self.bits << rhs,
        }
    }
}

impl<const N: i128> ops::Shr<usize> for BitVector<N> {
    type Output = Self;

    fn shr(self, rhs: usize) -> Self::Output {
        Self {
            bits: self.bits >> rhs,
        }
    }
}

impl<const N: i128> ops::Shr<i128> for BitVector<N> {
    type Output = Self;

    fn shr(self, rhs: i128) -> Self::Output {
        Self {
            bits: self.bits >> rhs,
        }
    }
}

impl<const N: i128> ops::Shr<i32> for BitVector<N> {
    type Output = Self;

    fn shr(self, rhs: i32) -> Self::Output {
        Self {
            bits: self.bits >> rhs,
        }
    }
}

impl<const N: i128> ops::Not for BitVector<N> {
    type Output = Self;

    fn not(self) -> Self::Output {
        bv((!self.bits) & Self::bit_mask())
    }
}

impl<const N: i128> std::ops::Add<i64> for BitVector<N> {
    type Output = Self;

    fn add(self, rhs: i64) -> BitVector<N> {
        let result = self.bits as i64 + rhs;
        // If the result is out of bounds, we may want to handle overflow
        bv::<N>(result as u64) // Returning the result as BitVector
    }
}

// ———————————————————————————————— Helpers ————————————————————————————————— //

const fn mask(nb_ones: usize) -> u64 {
    assert!(nb_ones <= 64, "Unsupported mask size");
    if nb_ones == 64 {
        u64::MAX
    } else {
        (1 << nb_ones) - 1
    }
}

const fn mask128(nb_ones: usize) -> u128 {
    assert!(nb_ones <= 128, "Unsupported mask size");
    if nb_ones == 128 {
        u128::MAX
    } else {
        (1 << nb_ones) - 1
    }
}

// ————————————————————————————————— Tests —————————————————————————————————— //

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

    #[test]
    fn bitvec_masks() {
        assert_eq!(BitVector::<0>::bit_mask(), 0b0);
        assert_eq!(BitVector::<1>::bit_mask(), 0b1);
        assert_eq!(BitVector::<2>::bit_mask(), 0b11);
        assert_eq!(BitVector::<8>::bit_mask(), 0b11111111);
        assert_eq!(BitVector::<64>::bit_mask(), 0xffffffffffffffff);
    }

    #[test]
    fn bitvec_not() {
        assert_eq!((!bv::<1>(0b1)).bits(), 0b0);
        assert_eq!((!bv::<1>(0b0)).bits(), 0b1);
        assert_eq!((!bv::<2>(0b01)).bits(), 0b10);
        assert_eq!((!bv::<2>(0b11)).bits(), 0b00);
    }

    #[test]
    fn subrange_bitvector() {
        let v = bv::<32>(0b10110111);

        assert_eq!(v.subrange::<0, 1, 1>().bits(), 0b1);
        assert_eq!(v.subrange::<0, 2, 2>().bits(), 0b11);
        assert_eq!(v.subrange::<0, 3, 3>().bits(), 0b111);
        assert_eq!(v.subrange::<0, 4, 4>().bits(), 0b0111);
        assert_eq!(v.subrange::<0, 5, 5>().bits(), 0b10111);

        assert_eq!(v.subrange::<2, 3, 1>().bits(), 0b1);
        assert_eq!(v.subrange::<2, 4, 2>().bits(), 0b01);
        assert_eq!(v.subrange::<2, 5, 3>().bits(), 0b101);
        assert_eq!(v.subrange::<2, 6, 4>().bits(), 0b1101);
        assert_eq!(v.subrange::<2, 7, 5>().bits(), 0b01101);

        assert_eq!(bv::<32>(0xffffffff).subrange::<7, 23, 16>().bits(), 0xffff);
        assert_eq!(v.subrange::<2, 7, 5>().bits(), 0b01101);

        let v = bv::<32>(0b10110111);
        assert_eq!(v.set_subrange::<0, 1, 1>(bv(0b0)).bits(), 0b10110110);
        assert_eq!(v.set_subrange::<0, 1, 1>(bv(0b1)).bits(), 0b10110111);
        assert_eq!(v.set_subrange::<0, 2, 2>(bv(0b00)).bits(), 0b10110100);
        assert_eq!(v.set_subrange::<2, 5, 3>(bv(0b010)).bits(), 0b10101011);

        assert_eq!(
            bv::<64>(0x0000000000000000).subrange::<60, 64, 4>().bits(),
            0x0
        );
        assert_eq!(
            bv::<64>(0xa000000000000000).subrange::<60, 64, 4>().bits(),
            0xa
        );
        assert_eq!(
            bv::<64>(0xb000000000000000).subrange::<60, 64, 4>().bits(),
            0xb
        );
        assert_eq!(
            bv::<64>(0xc000000000000000).subrange::<60, 64, 4>().bits(),
            0xc
        );
        assert_eq!(
            bv::<64>(0xd000000000000000).subrange::<60, 64, 4>().bits(),
            0xd
        );
        assert_eq!(
            bv::<64>(0xe000000000000000).subrange::<60, 64, 4>().bits(),
            0xe
        );
        assert_eq!(
            bv::<64>(0xf000000000000000).subrange::<60, 64, 4>().bits(),
            0xf
        );
    }

    // TODO: In the future squash with the previous function
    #[test]
    fn subrange_bitfield() {
        let bitfield = BitField::<32>::new(0b10110111);

        assert_eq!(bitfield.subrange::<0, 1, 1>().bits(), 0b1);
        assert_eq!(bitfield.subrange::<0, 2, 2>().bits(), 0b11);
        assert_eq!(bitfield.subrange::<0, 3, 3>().bits(), 0b111);
        assert_eq!(bitfield.subrange::<0, 4, 4>().bits(), 0b0111);
        assert_eq!(bitfield.subrange::<0, 5, 5>().bits(), 0b10111);

        assert_eq!(bitfield.subrange::<2, 3, 1>().bits(), 0b1);
        assert_eq!(bitfield.subrange::<2, 4, 2>().bits(), 0b01);
        assert_eq!(bitfield.subrange::<2, 5, 3>().bits(), 0b101);
        assert_eq!(bitfield.subrange::<2, 6, 4>().bits(), 0b1101);
        assert_eq!(bitfield.subrange::<2, 7, 5>().bits(), 0b01101);

        let v = bv::<32>(0b10110111);
        assert_eq!(v.set_subrange::<0, 1, 1>(bv(0b0)).bits(), 0b10110110);
        assert_eq!(v.set_subrange::<0, 1, 1>(bv(0b1)).bits(), 0b10110111);
        assert_eq!(v.set_subrange::<0, 2, 2>(bv(0b00)).bits(), 0b10110100);
        assert_eq!(v.set_subrange::<2, 5, 3>(bv(0b010)).bits(), 0b10101011);
    }

    #[test]
    fn test_update_subrange_bits() {
        assert_eq!(
            update_subrange_bits(bv::<8>(0b11111100), 1, 0, bv::<2>(0b11)).bits,
            0b11111111
        );
        assert_eq!(
            update_subrange_bits(bv::<8>(0b00000000), 0, 0, bv::<1>(0b1)).bits,
            0b00000001
        );
        assert_eq!(
            update_subrange_bits(bv::<8>(0b00000000), 1, 1, bv::<1>(0b1)).bits,
            0b00000010
        );
        assert_eq!(
            update_subrange_bits(bv::<8>(0b00000000), 2, 2, bv::<1>(0b1)).bits,
            0b00000100
        );
        assert_eq!(
            update_subrange_bits(bv::<8>(0b00000000), 3, 3, bv::<1>(0b1)).bits,
            0b00001000
        );
        assert_eq!(
            update_subrange_bits(bv::<8>(0b00000000), 4, 4, bv::<1>(0b1)).bits,
            0b00010000
        );
        assert_eq!(
            update_subrange_bits(bv::<8>(0b00000000), 5, 5, bv::<1>(0b1)).bits,
            0b00100000
        );
        assert_eq!(
            update_subrange_bits(bv::<8>(0b00000000), 6, 6, bv::<1>(0b1)).bits,
            0b01000000
        );
        assert_eq!(
            update_subrange_bits(bv::<8>(0b00000000), 7, 7, bv::<1>(0b1)).bits,
            0b10000000
        );
    }

    #[test]
    fn bitwise_operators() {
        let v = bv::<32>(0b1);

        assert_eq!(v, v | v);
        assert_eq!(v, v & v);
        assert_eq!(v, v ^ v ^ v);
        assert_eq!(v, !!v);

        for i in 0..30 {
            assert_eq!(v, (v << (i as usize)) >> (i as usize));
        }

        // Test i32 shift operators
        for i in 0i32..30i32 {
            assert_eq!(v, (v << i) >> i);
        }
    }

    #[test]
    fn test_zero_extend() {
        let v = bv::<8>(0b1010);

        assert_eq!(v.bits, v.zero_extend::<16>().bits);
        assert_eq!(v.bits, v.zero_extend::<63>().bits);
        assert_eq!(v.bits, v.zero_extend::<64>().bits);
    }

    #[test]
    fn test_bitvector_concat() {
        const SIZE: i128 = 20;
        const NEW_SIZE: i128 = 40;

        for i in 0..(1 << (SIZE as usize)) {
            let v = bv::<SIZE>(i);
            assert_eq!(
                bitvector_concat::<SIZE, SIZE, NEW_SIZE>(v, v).bits,
                i + (i << (SIZE as usize))
            );
        }
    }

    #[test]
    fn test_bitvector_access() {
        const SIZE: i128 = 10;

        for i in 0..(1 << (SIZE as usize)) {
            let v = bv::<SIZE>(i);
            for idx in 0..(SIZE as usize) {
                assert_eq!((i & (1 << idx)) > 0, bitvector_access(v, idx as i128))
            }
        }
    }

    #[test]
    fn test_set_bit() {
        const SIZE: i128 = 60;

        let mut v = bv::<SIZE>(0);
        let mut val: u64 = 0;
        for idx in 0..(SIZE as usize) {
            val |= 1u64 << idx;
            v = v.set_bit(idx as i128, true);

            assert_eq!(v.bits, val);
        }

        for i in 0..(SIZE as usize) {
            v = v.set_bit(i as i128, false);
        }

        assert_eq!(v.bits, 0);
    }

    #[test]
    fn test_signed_interpretation() {
        // Test 1-bit signed values
        assert_eq!(bv::<1>(0b0).signed(), 0);
        assert_eq!(bv::<1>(0b1).signed(), -1);

        // Test 2-bit signed values
        assert_eq!(bv::<2>(0b00).signed(), 0);
        assert_eq!(bv::<2>(0b01).signed(), 1);
        assert_eq!(bv::<2>(0b10).signed(), -2);
        assert_eq!(bv::<2>(0b11).signed(), -1);

        // Test 3-bit signed values
        assert_eq!(bv::<3>(0b000).signed(), 0);
        assert_eq!(bv::<3>(0b001).signed(), 1);
        assert_eq!(bv::<3>(0b010).signed(), 2);
        assert_eq!(bv::<3>(0b011).signed(), 3);
        assert_eq!(bv::<3>(0b100).signed(), -4);
        assert_eq!(bv::<3>(0b101).signed(), -3);
        assert_eq!(bv::<3>(0b110).signed(), -2);
        assert_eq!(bv::<3>(0b111).signed(), -1);

        // Test 4-bit signed values
        assert_eq!(bv::<4>(0b0000).signed(), 0);
        assert_eq!(bv::<4>(0b0001).signed(), 1);
        assert_eq!(bv::<4>(0b0111).signed(), 7);
        assert_eq!(bv::<4>(0b1000).signed(), -8);
        assert_eq!(bv::<4>(0b1001).signed(), -7);
        assert_eq!(bv::<4>(0b1111).signed(), -1);

        // Test 8-bit signed values
        assert_eq!(bv::<8>(0x00).signed(), 0);
        assert_eq!(bv::<8>(0x01).signed(), 1);
        assert_eq!(bv::<8>(0x7F).signed(), 127);
        assert_eq!(bv::<8>(0x80).signed(), -128);
        assert_eq!(bv::<8>(0xFF).signed(), -1);

        // Test 16-bit signed values
        assert_eq!(bv::<16>(0x0000).signed(), 0);
        assert_eq!(bv::<16>(0x0001).signed(), 1);
        assert_eq!(bv::<16>(0x7FFF).signed(), 32767);
        assert_eq!(bv::<16>(0x8000).signed(), -32768);
        assert_eq!(bv::<16>(0xFFFF).signed(), -1);

        // Test 32-bit signed values
        assert_eq!(bv::<32>(0x00000000).signed(), 0);
        assert_eq!(bv::<32>(0x00000001).signed(), 1);
        assert_eq!(bv::<32>(0x7FFFFFFF).signed(), 2147483647);
        assert_eq!(bv::<32>(0x80000000).signed(), -2147483648);
        assert_eq!(bv::<32>(0xFFFFFFFF).signed(), -1);

        // Test 64-bit signed values
        assert_eq!(bv::<64>(0x0000000000000000).signed(), 0);
        assert_eq!(bv::<64>(0x0000000000000001).signed(), 1);
        assert_eq!(bv::<64>(0x7FFFFFFFFFFFFFFF).signed(), 9223372036854775807);
        assert_eq!(bv::<64>(0x8000000000000000).signed(), -9223372036854775808);
        assert_eq!(bv::<64>(0xFFFFFFFFFFFFFFFF).signed(), -1);
    }

    #[test]
    fn test_signed_vs_unsigned() {
        // Test that unsigned and signed give different results for negative values
        let v = bv::<8>(0xFF);
        assert_eq!(v.unsigned(), 255);
        assert_eq!(v.signed(), -1);

        let v = bv::<8>(0x80);
        assert_eq!(v.unsigned(), 128);
        assert_eq!(v.signed(), -128);

        let v = bv::<16>(0x8000);
        assert_eq!(v.unsigned(), 32768);
        assert_eq!(v.signed(), -32768);

        // Test that unsigned and signed give same results for positive values
        let v = bv::<8>(0x7F);
        assert_eq!(v.unsigned(), 127);
        assert_eq!(v.signed(), 127);

        let v = bv::<8>(0x00);
        assert_eq!(v.unsigned(), 0);
        assert_eq!(v.signed(), 0);
    }

    #[test]
    fn test_sign_extend() {
        // Test sign extending positive values from 4 to 8 bits
        let input = bv::<4>(0b0111); // 7 in 4 bits
        let result = sail_sign_extend::<4, 8>(input, 8);
        assert_eq!(result.bits(), 0b00000111); // Should remain 7 in 8 bits

        // Test sign extending negative values from 4 to 8 bits
        let input = bv::<4>(0b1000); // -8 in 4 bits (two's complement)
        let result = sail_sign_extend::<4, 8>(input, 8);
        assert_eq!(result.bits(), 0b11111000); // Should become -8 in 8 bits

        let input = bv::<4>(0b1111); // -1 in 4 bits
        let result = sail_sign_extend::<4, 8>(input, 8);
        assert_eq!(result.bits(), 0b11111111); // Should become -1 in 8 bits

        // Test sign extending from 8 to 16 bits
        let input = bv::<8>(0x7F); // 127 in 8 bits (positive)
        let result = sail_sign_extend::<8, 16>(input, 16);
        assert_eq!(result.bits(), 0x007F); // Should remain 127 in 16 bits

        let input = bv::<8>(0x80); // -128 in 8 bits (negative)
        let result = sail_sign_extend::<8, 16>(input, 16);
        assert_eq!(result.bits(), 0xFF80); // Should become -128 in 16 bits

        let input = bv::<8>(0xFF); // -1 in 8 bits
        let result = sail_sign_extend::<8, 16>(input, 16);
        assert_eq!(result.bits(), 0xFFFF); // Should become -1 in 16 bits

        // Test sign extending from 16 to 32 bits
        let input = bv::<16>(0x7FFF); // 32767 in 16 bits (positive)
        let result = sail_sign_extend::<16, 32>(input, 32);
        assert_eq!(result.bits(), 0x00007FFF); // Should remain 32767 in 32 bits

        let input = bv::<16>(0x8000); // -32768 in 16 bits (negative)
        let result = sail_sign_extend::<16, 32>(input, 32);
        assert_eq!(result.bits(), 0xFFFF8000); // Should become -32768 in 32 bits

        // Test sign extending from 32 to 64 bits
        let input = bv::<32>(0x7FFFFFFF); // Positive value
        let result = sail_sign_extend::<32, 64>(input, 64);
        assert_eq!(result.bits(), 0x000000007FFFFFFF);

        let input = bv::<32>(0x80000000); // Negative value
        let result = sail_sign_extend::<32, 64>(input, 64);
        assert_eq!(result.bits(), 0xFFFFFFFF80000000);

        // Test edge cases - extending by 1 bit
        let input = bv::<1>(0b0); // 0 in 1 bit
        let result = sail_sign_extend::<1, 2>(input, 2);
        assert_eq!(result.bits(), 0b00); // Should remain 0

        let input = bv::<1>(0b1); // -1 in 1 bit
        let result = sail_sign_extend::<1, 2>(input, 2);
        assert_eq!(result.bits(), 0b11); // Should become -1 in 2 bits

        // Test extending smaller values
        let input = bv::<3>(0b101); // -3 in 3 bits
        let result = sail_sign_extend::<3, 8>(input, 8);
        assert_eq!(result.bits(), 0b11111101); // Should become -3 in 8 bits

        let input = bv::<3>(0b011); // 3 in 3 bits
        let result = sail_sign_extend::<3, 8>(input, 8);
        assert_eq!(result.bits(), 0b00000011); // Should remain 3 in 8 bits

        // Test extending from 64 to 64 bits (no-op, but widely used)
        let input = bv::<64>(0x7FFFFFFFFFFFFFFF); // Maximum positive 64-bit value
        let result = sail_sign_extend::<64, 64>(input, 64);
        assert_eq!(result.bits(), 0x7FFFFFFFFFFFFFFF); // Should remain unchanged

        let input = bv::<64>(0x8000000000000000); // Minimum negative 64-bit value
        let result = sail_sign_extend::<64, 64>(input, 64);
        assert_eq!(result.bits(), 0x8000000000000000); // Should remain unchanged

        let input = bv::<64>(0xFFFFFFFFFFFFFFFF); // -1 in 64 bits
        let result = sail_sign_extend::<64, 64>(input, 64);
        assert_eq!(result.bits(), 0xFFFFFFFFFFFFFFFF); // Should remain unchanged

        let input = bv::<64>(0x0000000000000000); // 0 in 64 bits
        let result = sail_sign_extend::<64, 64>(input, 64);
        assert_eq!(result.bits(), 0x0000000000000000); // Should remain unchanged
    }
}