|
| 1 | + |
| 2 | +/// Creates a One-Hot encoding of the given integer. |
| 3 | +/// |
| 4 | +/// Examples: |
| 5 | +/// `ToOneHot#(SIZE: 5)(3) == [false, false, false, true, false]` |
| 6 | +/// `ToOneHot#(SIZE: 2)(0) == [true, false]` |
| 7 | +module ToOneHot #(int SIZE) { |
| 8 | + interface ToOneHot : int#(FROM: 0, TO: SIZE - 1) selection'0 -> bool[SIZE] bits'0 |
| 9 | + |
| 10 | + bits = RepeatGen#(T: type bool, V: false, SIZE) |
| 11 | + |
| 12 | + bits[selection] = true |
| 13 | +} |
| 14 | + |
| 15 | +/// This module returns the index of the first `true` bit in the given bitstring. |
| 16 | +/// If the whole bitstring is `false`, then `is_nonzero` is not fired. |
| 17 | +/// If there is at least one set ('1') bit, `is_nonzero` triggers with the index of the first '1' bit. |
| 18 | +/// |
| 19 | +/// Usage: |
| 20 | +/// ´´´sus |
| 21 | +/// FindFirst find_first_bit |
| 22 | +/// find_first_bit.find([false, false, true, false, true]) |
| 23 | +/// when find_first_bit.is_nonzero : int first_nonzero { |
| 24 | +/// // Fires with `2` |
| 25 | +/// } |
| 26 | +/// ´´´ |
| 27 | +module FindFirst #(int SIZE) { |
| 28 | + trigger is_nonzero : int#(FROM: 0, TO: SIZE) first_nonzero'0 |
| 29 | + |
| 30 | + action find : bool[SIZE] bits'0 { |
| 31 | + gen int BASE_CASE_SIZE = 6 |
| 32 | + |
| 33 | + if SIZE <= BASE_CASE_SIZE { |
| 34 | + // Iterate from high downwards |
| 35 | + for int I in 0..SIZE { |
| 36 | + gen int BIT = SIZE - 1 - I |
| 37 | + |
| 38 | + // Later (therefore lower-index) bits override this call, hence the first bit effect. |
| 39 | + when bits[BIT] { |
| 40 | + is_nonzero(BIT) |
| 41 | + } |
| 42 | + } |
| 43 | + } else { |
| 44 | + // Split recursively |
| 45 | + gen int LEFT_SIZE = SIZE / 2 |
| 46 | + |
| 47 | + FindFirst left_part |
| 48 | + FindFirst right_part |
| 49 | + |
| 50 | + left_part.find(bits[LEFT_SIZE:]) |
| 51 | + right_part.find(bits[:LEFT_SIZE]) |
| 52 | + |
| 53 | + when left_part.is_nonzero : int left_first_nonzero { |
| 54 | + is_nonzero(left_first_nonzero) |
| 55 | + } else when right_part.is_nonzero : int right_first_nonzero { |
| 56 | + is_nonzero(right_first_nonzero + LEFT_SIZE) |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/// Counts the number of set ('1') bits in the bitstring. |
| 63 | +/// |
| 64 | +/// Example: |
| 65 | +/// `PopCount([true, false, true, true, true]) == 4` |
| 66 | +/// `PopCount(8'b00111011) == 5` |
| 67 | +module PopCount #(int SIZE) { |
| 68 | + // Should be chosen based on what's most efficient for the target architecture |
| 69 | + gen int BASE_CASE_SIZE = 6 |
| 70 | + |
| 71 | + interface PopCount : bool[SIZE] bits'0 -> int#(FROM: 0, TO: SIZE+1) popcount |
| 72 | + |
| 73 | + if SIZE == 0 { |
| 74 | + int zero'0 = 0 |
| 75 | + popcount = zero |
| 76 | + } else if SIZE <= BASE_CASE_SIZE { |
| 77 | + int[SIZE] cvt |
| 78 | + for int I in 0..SIZE { |
| 79 | + when bits[I] { |
| 80 | + cvt[I] = 1 |
| 81 | + } else { |
| 82 | + cvt[I] = 0 |
| 83 | + } |
| 84 | + } |
| 85 | + if SIZE == 1 { |
| 86 | + popcount = cvt[0] |
| 87 | + } else if SIZE == 2 { |
| 88 | + popcount = cvt[0] + cvt[1] |
| 89 | + } else if SIZE == 3 { |
| 90 | + popcount = cvt[0] + cvt[1] + cvt[2] |
| 91 | + } else if SIZE == 4 { |
| 92 | + popcount = cvt[0] + cvt[1] + cvt[2] + cvt[3] |
| 93 | + } else if SIZE == 5 { |
| 94 | + popcount = cvt[0] + cvt[1] + cvt[2] + cvt[3] + cvt[4] |
| 95 | + } else { |
| 96 | + assert#(C: false) |
| 97 | + } |
| 98 | + } else { |
| 99 | + popcount = PopCount(bits[:SIZE / 2]) + PopCount(bits[SIZE / 2:]) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +/// Splits the given integer. The upper [LOWER_BITS:] bits are returned as upper, the lower [:LOWER_BITS] bits are returned in lower |
| 105 | +/// See also [BitwiseIntConcat] |
| 106 | +module BitwiseIntSplit #(int TO, int LOWER_BITS) { |
| 107 | + gen int UPPER_TO = (TO-1) / pow2#(E: LOWER_BITS) + 1 |
| 108 | + interface BitwiseIntSplit : |
| 109 | + int#(FROM: 0, TO) v'0 -> |
| 110 | + int#(FROM: 0, TO: UPPER_TO) upper'0, |
| 111 | + int#(FROM: 0, TO: pow2#(E: LOWER_BITS)) lower'0 |
| 112 | + |
| 113 | + bool[clog2#(V: TO)] v_bits = UIntToBits(v) |
| 114 | + |
| 115 | + lower = BitsToUInt(v_bits[:LOWER_BITS]) |
| 116 | + // we use FromBits rather than BitsToUInt, because we can then immediately adjust the range too |
| 117 | + upper = FromBits#(T: type int#(FROM: 0, TO: UPPER_TO))(v_bits[LOWER_BITS:]) |
| 118 | +} |
| 119 | + |
| 120 | +/// Recombines a lower and upper integer parts. |
| 121 | +/// See also [BitwiseIntSplit] |
| 122 | +module BitwiseIntConcat #(int UPPER_TO, int LOWER_BITS) { |
| 123 | + interface BitwiseIntConcat : |
| 124 | + int#(FROM: 0, TO: UPPER_TO) upper'0, |
| 125 | + int#(FROM: 0, TO: pow2#(E: LOWER_BITS)) lower'0 -> |
| 126 | + int#(FROM: 0, TO: UPPER_TO * pow2#(E: LOWER_BITS)) v'0 |
| 127 | + |
| 128 | + bool[clog2#(V: UPPER_TO) + LOWER_BITS] v_bits |
| 129 | + v_bits[:LOWER_BITS] = UIntToBits(lower) |
| 130 | + v_bits[LOWER_BITS:] = UIntToBits(upper) |
| 131 | + // we use FromBits rather than BitsToUInt, because we can then immediately adjust the range too |
| 132 | + v = FromBits#(T: type int#(FROM: 0, TO: UPPER_TO * pow2#(E: LOWER_BITS)))(v_bits) |
| 133 | +} |
| 134 | + |
| 135 | +/// Sets the lower [:LOWER_BITS] bits to false, thus aligning the value |
| 136 | +/// Works for unsigned and signed integers alike |
| 137 | +module AlignToPow2 #(int FROM, int TO, int LOWER_BITS) { |
| 138 | + gen int ALIGNED_FROM = FROM - (FROM mod clog2#(V: LOWER_BITS)) |
| 139 | + gen int ALIGNED_TO = TO - (TO + 1 mod clog2#(V: LOWER_BITS)) |
| 140 | + interface AlignToPow2 : |
| 141 | + int#(FROM, TO) i'0 -> |
| 142 | + int#(FROM: ALIGNED_FROM, TO: ALIGNED_TO) o'0 |
| 143 | + |
| 144 | + bool[sizeof#(T: type int#(FROM, TO))] bits = ToBits(i) |
| 145 | + bool[sizeof#(T: type int#(FROM, TO))] aligned_bits |
| 146 | + aligned_bits[:LOWER_BITS] = Repeat(false) |
| 147 | + aligned_bits[LOWER_BITS:] = bits[LOWER_BITS:] |
| 148 | + // we use FromBits rather than BitsToInt, because we can then immediately adjust the range too |
| 149 | + o = FromBits#(T: type int#(FROM: ALIGNED_FROM, TO: ALIGNED_TO))(aligned_bits) |
| 150 | +} |
0 commit comments