@@ -139,20 +139,13 @@ test "Sign extending from a constant bit-width2" {
139139/// represents the number to be sign-extended to the `target` type.
140140/// https://github.com/cryptocode/bithacks#sign-extending-from-a-variable-bit-width
141141pub fn signExtendVariable (comptime target : type , comptime bits : usize , val : anytype ) target {
142- const T = requireSignedInt (@TypeOf (val ));
143- const val_bits = @typeInfo (T ).Int .bits ;
144- comptime assert (val_bits >= bits );
145-
146- // Only necessary if any bits above `bits` are non-zero
147- const only_relevant_bits = val & ((@as (usize , 1 ) << bits ) - 1 );
148- const diff : usize = val_bits - bits ;
149- return (only_relevant_bits << diff ) >> diff ;
142+ return @as (target , @truncate (std .meta .Int (.signed , bits ), val ));
150143}
151144
152145test "Sign extending from a variable bit-width" {
153146 // Input is 0b10110110, but we only care about the lower 3 bits which we sign extend into an i16
154147 const res = signExtendVariable (i16 , 3 , @bitCast (i8 , @as (u8 , 0b10110110 )));
155- try expect (res == -2 );
148+ try expectEqual (res , -2 );
156149}
157150
158151/// Conditionally set or clear bits without branching
@@ -1688,7 +1681,7 @@ pub fn nextLexicographicPermutation(val: u32) u32 {
16881681 var t = val | (val - 1 );
16891682
16901683 // Set to 1 the most significant bit to change, set to 0 the least significant ones, and add the necessary 1 bits.
1691- return (t + 1 ) | (((~ t & -% ~ t ) - 1 ) >> @intCast (u5 , @ctz (u32 , val ) + 1 ));
1684+ return (t + 1 ) | (((~ t & -% ~ t ) - 1 ) >> @intCast (u5 , @ctz (val ) + 1 ));
16921685}
16931686
16941687test "Compute the lexicographically next bit permutation" {
0 commit comments