@@ -77,19 +77,21 @@ static Func<T, T> UnaryOp(Func<Expression, UnaryExpression> op)
7777 [ Ops ]
7878 internal static class EnumOps < T > where T : Enum
7979 {
80+ private static bool IsUnsigned = typeof ( T ) . GetEnumUnderlyingType ( ) == typeof ( UInt64 ) ;
81+
8082 [ ForbidPythonThreads ]
8183#pragma warning disable IDE1006 // Naming Styles - must match Python
8284 public static PyInt __int__ ( T value )
8385#pragma warning restore IDE1006 // Naming Styles
84- => typeof ( T ) . GetEnumUnderlyingType ( ) == typeof ( UInt64 )
86+ => IsUnsigned
8587 ? new PyInt ( Convert . ToUInt64 ( value ) )
8688 : new PyInt ( Convert . ToInt64 ( value ) ) ;
8789
8890 #region Arithmetic operators
8991
9092 public static double op_Addition ( T a , double b )
9193 {
92- if ( typeof ( T ) . GetEnumUnderlyingType ( ) == typeof ( UInt64 ) )
94+ if ( IsUnsigned )
9395 {
9496 return Convert . ToUInt64 ( a ) + b ;
9597 }
@@ -103,7 +105,7 @@ public static double op_Addition(double a, T b)
103105
104106 public static double op_Subtraction ( T a , double b )
105107 {
106- if ( typeof ( T ) . GetEnumUnderlyingType ( ) == typeof ( UInt64 ) )
108+ if ( IsUnsigned )
107109 {
108110 return Convert . ToUInt64 ( a ) - b ;
109111 }
@@ -112,12 +114,16 @@ public static double op_Subtraction(T a, double b)
112114
113115 public static double op_Subtraction ( double a , T b )
114116 {
115- return op_Subtraction ( b , a ) ;
117+ if ( IsUnsigned )
118+ {
119+ return a - Convert . ToUInt64 ( b ) ;
120+ }
121+ return a - Convert . ToInt64 ( b ) ;
116122 }
117123
118124 public static double op_Multiply ( T a , double b )
119125 {
120- if ( typeof ( T ) . GetEnumUnderlyingType ( ) == typeof ( UInt64 ) )
126+ if ( IsUnsigned )
121127 {
122128 return Convert . ToUInt64 ( a ) * b ;
123129 }
@@ -131,7 +137,7 @@ public static double op_Multiply(double a, T b)
131137
132138 public static double op_Division ( T a , double b )
133139 {
134- if ( typeof ( T ) . GetEnumUnderlyingType ( ) == typeof ( UInt64 ) )
140+ if ( IsUnsigned )
135141 {
136142 return Convert . ToUInt64 ( a ) / b ;
137143 }
@@ -140,7 +146,11 @@ public static double op_Division(T a, double b)
140146
141147 public static double op_Division ( double a , T b )
142148 {
143- return op_Division ( b , a ) ;
149+ if ( IsUnsigned )
150+ {
151+ return a / Convert . ToUInt64 ( b ) ;
152+ }
153+ return a / Convert . ToInt64 ( b ) ;
144154 }
145155
146156 #endregion
@@ -149,7 +159,7 @@ public static double op_Division(double a, T b)
149159
150160 public static bool op_Equality ( T a , long b )
151161 {
152- if ( typeof ( T ) . GetEnumUnderlyingType ( ) == typeof ( UInt64 ) )
162+ if ( IsUnsigned )
153163 {
154164 var uvalue = Convert . ToUInt64 ( a ) ;
155165 return b >= 0 && ( ( ulong ) b ) == uvalue ;
@@ -159,7 +169,7 @@ public static bool op_Equality(T a, long b)
159169
160170 public static bool op_Equality ( T a , ulong b )
161171 {
162- if ( typeof ( T ) . GetEnumUnderlyingType ( ) == typeof ( UInt64 ) )
172+ if ( IsUnsigned )
163173 {
164174 var uvalue = Convert . ToUInt64 ( a ) ;
165175 return b == uvalue ;
@@ -200,7 +210,7 @@ public static bool op_Inequality(ulong a, T b)
200210
201211 public static bool op_LessThan ( T a , long b )
202212 {
203- if ( typeof ( T ) . GetEnumUnderlyingType ( ) == typeof ( UInt64 ) )
213+ if ( IsUnsigned )
204214 {
205215 var uvalue = Convert . ToUInt64 ( a ) ;
206216 return b >= 0 && ( ( ulong ) b ) > uvalue ;
@@ -210,7 +220,7 @@ public static bool op_LessThan(T a, long b)
210220
211221 public static bool op_LessThan ( T a , ulong b )
212222 {
213- if ( typeof ( T ) . GetEnumUnderlyingType ( ) == typeof ( UInt64 ) )
223+ if ( IsUnsigned )
214224 {
215225 var uvalue = Convert . ToUInt64 ( a ) ;
216226 return b > uvalue ;
@@ -231,7 +241,7 @@ public static bool op_LessThan(ulong a, T b)
231241
232242 public static bool op_GreaterThan ( T a , long b )
233243 {
234- if ( typeof ( T ) . GetEnumUnderlyingType ( ) == typeof ( UInt64 ) )
244+ if ( IsUnsigned )
235245 {
236246 var uvalue = Convert . ToUInt64 ( a ) ;
237247 return b >= 0 && ( ( ulong ) b ) < uvalue ;
@@ -241,7 +251,7 @@ public static bool op_GreaterThan(T a, long b)
241251
242252 public static bool op_GreaterThan ( T a , ulong b )
243253 {
244- if ( typeof ( T ) . GetEnumUnderlyingType ( ) == typeof ( UInt64 ) )
254+ if ( IsUnsigned )
245255 {
246256 var uvalue = Convert . ToUInt64 ( a ) ;
247257 return b < uvalue ;
@@ -262,7 +272,7 @@ public static bool op_GreaterThan(ulong a, T b)
262272
263273 public static bool op_LessThanOrEqual ( T a , long b )
264274 {
265- if ( typeof ( T ) . GetEnumUnderlyingType ( ) == typeof ( UInt64 ) )
275+ if ( IsUnsigned )
266276 {
267277 var uvalue = Convert . ToUInt64 ( a ) ;
268278 return b >= 0 && ( ( ulong ) b ) >= uvalue ;
@@ -272,7 +282,7 @@ public static bool op_LessThanOrEqual(T a, long b)
272282
273283 public static bool op_LessThanOrEqual ( T a , ulong b )
274284 {
275- if ( typeof ( T ) . GetEnumUnderlyingType ( ) == typeof ( UInt64 ) )
285+ if ( IsUnsigned )
276286 {
277287 var uvalue = Convert . ToUInt64 ( a ) ;
278288 return b >= uvalue ;
@@ -293,7 +303,7 @@ public static bool op_LessThanOrEqual(ulong a, T b)
293303
294304 public static bool op_GreaterThanOrEqual ( T a , long b )
295305 {
296- if ( typeof ( T ) . GetEnumUnderlyingType ( ) == typeof ( UInt64 ) )
306+ if ( IsUnsigned )
297307 {
298308 var uvalue = Convert . ToUInt64 ( a ) ;
299309 return b >= 0 && ( ( ulong ) b ) <= uvalue ;
@@ -303,7 +313,7 @@ public static bool op_GreaterThanOrEqual(T a, long b)
303313
304314 public static bool op_GreaterThanOrEqual ( T a , ulong b )
305315 {
306- if ( typeof ( T ) . GetEnumUnderlyingType ( ) == typeof ( UInt64 ) )
316+ if ( IsUnsigned )
307317 {
308318 var uvalue = Convert . ToUInt64 ( a ) ;
309319 return b <= uvalue ;
@@ -328,7 +338,7 @@ public static bool op_GreaterThanOrEqual(ulong a, T b)
328338
329339 public static bool op_Equality ( T a , double b )
330340 {
331- if ( typeof ( T ) . GetEnumUnderlyingType ( ) == typeof ( UInt64 ) )
341+ if ( IsUnsigned )
332342 {
333343 return Convert . ToUInt64 ( a ) == b ;
334344 }
@@ -352,7 +362,7 @@ public static bool op_Inequality(double a, T b)
352362
353363 public static bool op_LessThan ( T a , double b )
354364 {
355- if ( typeof ( T ) . GetEnumUnderlyingType ( ) == typeof ( UInt64 ) )
365+ if ( IsUnsigned )
356366 {
357367 return Convert . ToUInt64 ( a ) < b ;
358368 }
@@ -366,7 +376,7 @@ public static bool op_LessThan(double a, T b)
366376
367377 public static bool op_GreaterThan ( T a , double b )
368378 {
369- if ( typeof ( T ) . GetEnumUnderlyingType ( ) == typeof ( UInt64 ) )
379+ if ( IsUnsigned )
370380 {
371381 return Convert . ToUInt64 ( a ) > b ;
372382 }
@@ -380,7 +390,7 @@ public static bool op_GreaterThan(double a, T b)
380390
381391 public static bool op_LessThanOrEqual ( T a , double b )
382392 {
383- if ( typeof ( T ) . GetEnumUnderlyingType ( ) == typeof ( UInt64 ) )
393+ if ( IsUnsigned )
384394 {
385395 return Convert . ToUInt64 ( a ) <= b ;
386396 }
@@ -394,7 +404,7 @@ public static bool op_LessThanOrEqual(double a, T b)
394404
395405 public static bool op_GreaterThanOrEqual ( T a , double b )
396406 {
397- if ( typeof ( T ) . GetEnumUnderlyingType ( ) == typeof ( UInt64 ) )
407+ if ( IsUnsigned )
398408 {
399409 return Convert . ToUInt64 ( a ) >= b ;
400410 }
@@ -422,7 +432,7 @@ public static bool op_Inequality(T a, T b)
422432
423433 public static bool op_LessThan ( T a , T b )
424434 {
425- if ( typeof ( T ) . GetEnumUnderlyingType ( ) == typeof ( UInt64 ) )
435+ if ( IsUnsigned )
426436 {
427437 return Convert . ToUInt64 ( a ) < Convert . ToUInt64 ( b ) ;
428438 }
@@ -431,7 +441,7 @@ public static bool op_LessThan(T a, T b)
431441
432442 public static bool op_GreaterThan ( T a , T b )
433443 {
434- if ( typeof ( T ) . GetEnumUnderlyingType ( ) == typeof ( UInt64 ) )
444+ if ( IsUnsigned )
435445 {
436446 return Convert . ToUInt64 ( a ) > Convert . ToUInt64 ( b ) ;
437447 }
@@ -440,7 +450,7 @@ public static bool op_GreaterThan(T a, T b)
440450
441451 public static bool op_LessThanOrEqual ( T a , T b )
442452 {
443- if ( typeof ( T ) . GetEnumUnderlyingType ( ) == typeof ( UInt64 ) )
453+ if ( IsUnsigned )
444454 {
445455 return Convert . ToUInt64 ( a ) <= Convert . ToUInt64 ( b ) ;
446456 }
@@ -449,7 +459,7 @@ public static bool op_LessThanOrEqual(T a, T b)
449459
450460 public static bool op_GreaterThanOrEqual ( T a , T b )
451461 {
452- if ( typeof ( T ) . GetEnumUnderlyingType ( ) == typeof ( UInt64 ) )
462+ if ( IsUnsigned )
453463 {
454464 return Convert . ToUInt64 ( a ) >= Convert . ToUInt64 ( b ) ;
455465 }
0 commit comments