@@ -1224,11 +1224,10 @@ public static T ShiftRight<T>(T op1, ulong op2) where T : INumber<T>
1224
1224
return op1 ;
1225
1225
}
1226
1226
1227
- public static T SignExtend < T > ( T n , int numBits , bool zeroExtend ) where T : struct , IComparable , IConvertible
1227
+ public static T SignExtend < T > ( T n , int numBits , bool zeroExtend ) where T : struct , IComparable , IConvertible , IBinaryInteger < T >
1228
1228
{
1229
1229
// Get the underlying integer value
1230
- dynamic value = n ;
1231
- value = ( long ) value ;
1230
+ long value = long . CreateTruncating ( n ) ;
1232
1231
1233
1232
// Mask to extract the lowest numBits
1234
1233
long mask = ( 1L << numBits ) - 1 ;
@@ -7613,15 +7612,15 @@ public static T Odd<T>(T even, T odd, int idx) where T : IBinaryInteger<T>
7613
7612
7614
7613
public static U ArithmeticShift < T , U > ( T value , int count , bool rounding = false , bool saturate = false )
7615
7614
where T : IBinaryInteger < T >
7616
- where U : IBinaryInteger < U >
7615
+ where U : IBinaryInteger < U > , IMinMaxValue < U >
7617
7616
{
7618
- dynamic v = value ;
7619
- dynamic shifted ;
7617
+ long v = long . CreateChecked ( value ) ;
7618
+ long shifted ;
7620
7619
if ( count > 0 )
7621
7620
{
7622
7621
if ( rounding )
7623
7622
{
7624
- dynamic bias = 1L << ( count - 1 ) ;
7623
+ long bias = 1L << ( count - 1 ) ;
7625
7624
shifted = v >= 0 ? ( v + bias ) >> count
7626
7625
: ( v - bias ) >> count ;
7627
7626
}
@@ -7641,21 +7640,21 @@ public static U ArithmeticShift<T, U>(T value, int count, bool rounding = false,
7641
7640
7642
7641
if ( saturate )
7643
7642
{
7644
- dynamic min = typeof ( U ) . GetField ( " MinValue" , BindingFlags . Static | BindingFlags . Public ) . GetValue ( null ) ;
7645
- dynamic max = typeof ( U ) . GetField ( " MaxValue" , BindingFlags . Static | BindingFlags . Public ) . GetValue ( null ) ;
7643
+ long min = long . CreateChecked ( U . MinValue ) ;
7644
+ long max = long . CreateChecked ( U . MaxValue ) ;
7646
7645
if ( shifted < min ) shifted = min ;
7647
7646
if ( shifted > max ) shifted = max ;
7648
7647
}
7649
7648
7650
- return ( U ) shifted ;
7649
+ return U . CreateTruncating ( shifted ) ;
7651
7650
}
7652
7651
7653
7652
public static U LogicalShift < T , U > ( T value , int count , bool rounding = false , bool saturate = false )
7654
7653
where T : IBinaryInteger < T >
7655
- where U : IBinaryInteger < U >
7654
+ where U : IBinaryInteger < U > , IMinMaxValue < U >
7656
7655
{
7657
- ulong v = Convert . ToUInt64 ( value ) ;
7658
- dynamic shifted ;
7656
+ ulong v = ulong . CreateTruncating ( value ) ;
7657
+ ulong shifted ;
7659
7658
if ( count > 0 )
7660
7659
{
7661
7660
if ( rounding )
@@ -7680,107 +7679,107 @@ public static U LogicalShift<T, U>(T value, int count, bool rounding = false, bo
7680
7679
7681
7680
if ( saturate )
7682
7681
{
7683
- dynamic max = typeof ( U ) . GetField ( " MaxValue" , BindingFlags . Static | BindingFlags . Public ) . GetValue ( null ) ;
7682
+ ulong max = ulong . CreateTruncating ( U . MaxValue ) ;
7684
7683
if ( shifted > max ) shifted = max ;
7685
7684
}
7686
7685
7687
- return ( U ) shifted ;
7686
+ return U . CreateTruncating ( shifted ) ;
7688
7687
}
7689
7688
7690
7689
public static U ShiftRightArithmeticNarrowingSaturateEven < T , U > ( T op1 , byte op2 , int i )
7691
7690
where T : IBinaryInteger < T >
7692
- where U : IBinaryInteger < U > , new ( )
7691
+ where U : IBinaryInteger < U > , IMinMaxValue < U > , new ( )
7693
7692
{
7694
7693
return Even < U > ( ArithmeticShift < T , U > ( op1 , op2 , saturate : true ) , i ) ;
7695
7694
}
7696
7695
7697
7696
public static U ShiftRightArithmeticNarrowingSaturateOdd < T , U > ( U op0 , T op1 , byte op2 , int i )
7698
7697
where T : IBinaryInteger < T >
7699
- where U : IBinaryInteger < U >
7698
+ where U : IBinaryInteger < U > , IMinMaxValue < U >
7700
7699
{
7701
7700
return Odd < U > ( op0 , ArithmeticShift < T , U > ( op1 , op2 , saturate : true ) , i ) ;
7702
7701
}
7703
7702
7704
7703
public static U ShiftRightArithmeticNarrowingSaturateUnsignedEven < T , U > ( T op1 , byte op2 , int i )
7705
7704
where T : IBinaryInteger < T >
7706
- where U : IBinaryInteger < U > , new ( )
7705
+ where U : IBinaryInteger < U > , IMinMaxValue < U > , new ( )
7707
7706
{
7708
7707
return ShiftRightArithmeticNarrowingSaturateEven < T , U > ( op1 , op2 , i ) ;
7709
7708
}
7710
7709
7711
7710
public static U ShiftRightArithmeticNarrowingSaturateUnsignedOdd < T , U > ( U op0 , T op1 , byte op2 , int i )
7712
7711
where T : IBinaryInteger < T >
7713
- where U : IBinaryInteger < U >
7712
+ where U : IBinaryInteger < U > , IMinMaxValue < U >
7714
7713
{
7715
7714
return ShiftRightArithmeticNarrowingSaturateOdd < T , U > ( op0 , op1 , op2 , i ) ;
7716
7715
}
7717
7716
7718
7717
public static U ShiftRightArithmeticRoundedNarrowingSaturateEven < T , U > ( T val , byte shift , int i )
7719
7718
where T : IBinaryInteger < T >
7720
- where U : IBinaryInteger < U > , new ( )
7719
+ where U : IBinaryInteger < U > , IMinMaxValue < U > , new ( )
7721
7720
{
7722
7721
return Even < U > ( ArithmeticShift < T , U > ( val , shift , rounding : true , saturate : true ) , i ) ;
7723
7722
}
7724
7723
7725
7724
public static U ShiftRightArithmeticRoundedNarrowingSaturateOdd < T , U > ( U even , T val , byte shift , int i )
7726
7725
where T : IBinaryInteger < T >
7727
- where U : IBinaryInteger < U >
7726
+ where U : IBinaryInteger < U > , IMinMaxValue < U >
7728
7727
{
7729
7728
return Odd < U > ( even , ArithmeticShift < T , U > ( val , shift , rounding : true , saturate : true ) , i ) ;
7730
7729
}
7731
7730
7732
7731
public static U ShiftRightArithmeticRoundedNarrowingSaturateUnsignedEven < T , U > ( T val , byte shift , int i )
7733
7732
where T : IBinaryInteger < T >
7734
- where U : IBinaryInteger < U > , new ( )
7733
+ where U : IBinaryInteger < U > , IMinMaxValue < U > , new ( )
7735
7734
{
7736
7735
return ShiftRightArithmeticRoundedNarrowingSaturateEven < T , U > ( val , shift , i ) ;
7737
7736
}
7738
7737
7739
7738
public static U ShiftRightArithmeticRoundedNarrowingSaturateUnsignedOdd < T , U > ( U even , T val , byte shift , int i )
7740
7739
where T : IBinaryInteger < T >
7741
- where U : IBinaryInteger < U > , new ( )
7740
+ where U : IBinaryInteger < U > , IMinMaxValue < U > , new ( )
7742
7741
{
7743
7742
return ShiftRightArithmeticRoundedNarrowingSaturateOdd < T , U > ( even , val , shift , i ) ;
7744
7743
}
7745
7744
7746
7745
public static U ShiftRightLogicalNarrowingEven < T , U > ( T val , byte shift , int i )
7747
7746
where T : IBinaryInteger < T >
7748
- where U : IBinaryInteger < U > , new ( )
7747
+ where U : IBinaryInteger < U > , IMinMaxValue < U > , new ( )
7749
7748
{
7750
7749
return Even < U > ( LogicalShift < T , U > ( val , shift ) , i ) ;
7751
7750
}
7752
7751
7753
7752
public static U ShiftRightLogicalNarrowingOdd < T , U > ( U even , T val , byte shift , int i )
7754
7753
where T : IBinaryInteger < T >
7755
- where U : IBinaryInteger < U >
7754
+ where U : IBinaryInteger < U > , IMinMaxValue < U >
7756
7755
{
7757
7756
return Odd < U > ( even , LogicalShift < T , U > ( val , shift ) , i ) ;
7758
7757
}
7759
7758
7760
7759
public static U ShiftRightLogicalRoundedNarrowingEven < T , U > ( T val , byte shift , int i )
7761
7760
where T : IBinaryInteger < T >
7762
- where U : IBinaryInteger < U > , new ( )
7761
+ where U : IBinaryInteger < U > , IMinMaxValue < U > , new ( )
7763
7762
{
7764
7763
return Even < U > ( LogicalShift < T , U > ( val , shift , rounding : true ) , i ) ;
7765
7764
}
7766
7765
7767
7766
public static U ShiftRightLogicalRoundedNarrowingOdd < T , U > ( U even , T val , byte shift , int i )
7768
7767
where T : IBinaryInteger < T >
7769
- where U : IBinaryInteger < U >
7768
+ where U : IBinaryInteger < U > , IMinMaxValue < U >
7770
7769
{
7771
7770
return Odd < U > ( even , LogicalShift < T , U > ( val , shift , rounding : true ) , i ) ;
7772
7771
}
7773
7772
7774
7773
public static U ShiftRightLogicalRoundedNarrowingSaturateEven < T , U > ( T val , byte shift , int i )
7775
7774
where T : IBinaryInteger < T >
7776
- where U : IBinaryInteger < U > , new ( )
7775
+ where U : IBinaryInteger < U > , IMinMaxValue < U > , new ( )
7777
7776
{
7778
7777
return Even < U > ( LogicalShift < T , U > ( val , shift , rounding : true , saturate : true ) , i ) ;
7779
7778
}
7780
7779
7781
7780
public static U ShiftRightLogicalRoundedNarrowingSaturateOdd < T , U > ( U even , T val , byte shift , int i )
7782
7781
where T : IBinaryInteger < T >
7783
- where U : IBinaryInteger < U >
7782
+ where U : IBinaryInteger < U > , IMinMaxValue < U >
7784
7783
{
7785
7784
return Odd < U > ( even , LogicalShift < T , U > ( val , shift , rounding : true , saturate : true ) , i ) ;
7786
7785
}
0 commit comments