File tree Expand file tree Collapse file tree 1 file changed +21
-5
lines changed
src/libraries/System.Private.CoreLib/src/System/Numerics Expand file tree Collapse file tree 1 file changed +21
-5
lines changed Original file line number Diff line number Diff line change @@ -142,12 +142,28 @@ static int CompareSignificant(T x, T y)
142
142
// To match the integer semantic comparison above, here we compare all the significant bits
143
143
// Revisit this if decimals are added
144
144
145
- Span < byte > significantX = stackalloc byte [ x ! . GetSignificandByteCount ( ) ] ;
146
- Span < byte > significantY = stackalloc byte [ y ! . GetSignificandByteCount ( ) ] ;
147
- x . WriteSignificandBigEndian ( significantX ) ;
148
- y . WriteSignificandBigEndian ( significantY ) ;
145
+ // Leave the space for custom floating-point type that has variable significand length
149
146
150
- return significantX . SequenceCompareTo ( significantY ) ;
147
+ if ( x ! . GetSignificandBitLength ( ) == y ! . GetSignificandBitLength ( ) )
148
+ {
149
+ // Prevent stack overflow for huge numbers
150
+ const int StackAllocThreshold = 256 ;
151
+
152
+ int xSignificandLength = x ! . GetSignificandByteCount ( ) ;
153
+ int ySignificandLength = y ! . GetSignificandByteCount ( ) ;
154
+
155
+ Span < byte > significantX = xSignificandLength <= StackAllocThreshold ? stackalloc byte [ xSignificandLength ] : new byte [ xSignificandLength ] ;
156
+ Span < byte > significantY = ySignificandLength <= StackAllocThreshold ? stackalloc byte [ ySignificandLength ] : new byte [ ySignificandLength ] ;
157
+
158
+ x . WriteSignificandBigEndian ( significantX ) ;
159
+ y . WriteSignificandBigEndian ( significantY ) ;
160
+
161
+ return significantX . SequenceCompareTo ( significantY ) ;
162
+ }
163
+ else
164
+ {
165
+ return x . GetSignificandBitLength ( ) . CompareTo ( y . GetSignificandBitLength ( ) ) ;
166
+ }
151
167
}
152
168
153
169
if ( T . IsNaN ( x ) )
You can’t perform that action at this time.
0 commit comments