@@ -115,6 +115,12 @@ public static int LeadingZeroCount(ulong value)
115
115
[ CLSCompliant ( false ) ]
116
116
public static int Log2 ( uint value )
117
117
{
118
+ // Enforce conventional contract 0->0 (Log(0) is undefined)
119
+ if ( value == 0 )
120
+ {
121
+ return 0 ;
122
+ }
123
+
118
124
// value lzcnt actual expected
119
125
// ..0000 32 0 0 (by convention, guard clause)
120
126
// ..0001 31 31-31 0
@@ -124,16 +130,15 @@ public static int Log2(uint value)
124
130
// 1000.. 0 31-0 31
125
131
if ( Lzcnt . IsSupported )
126
132
{
127
- // Enforce conventional contract 0->0 (Log(0) is undefined)
128
- if ( value == 0 )
129
- {
130
- return 0 ;
131
- }
132
-
133
133
// LZCNT contract is 0->32
134
134
return 31 - ( int ) Lzcnt . LeadingZeroCount ( value ) ;
135
135
}
136
136
137
+ if ( ArmBase . IsSupported )
138
+ {
139
+ return 31 - ( int ) ArmBase . LeadingZeroCount ( value ) ;
140
+ }
141
+
137
142
// Fallback contract is 0->0
138
143
return Log2SoftwareFallback ( value ) ;
139
144
}
@@ -147,18 +152,23 @@ public static int Log2(uint value)
147
152
[ CLSCompliant ( false ) ]
148
153
public static int Log2 ( ulong value )
149
154
{
150
- if ( Lzcnt . X64 . IsSupported )
155
+ // Enforce conventional contract 0->0 (Log(0) is undefined)
156
+ if ( value == 0 )
151
157
{
152
- // Enforce conventional contract 0->0 (Log(0) is undefined)
153
- if ( value == 0 )
154
- {
155
- return 0 ;
156
- }
158
+ return 0 ;
159
+ }
157
160
161
+ if ( Lzcnt . X64 . IsSupported )
162
+ {
158
163
// LZCNT contract is 0->64
159
164
return 63 - ( int ) Lzcnt . X64 . LeadingZeroCount ( value ) ;
160
165
}
161
166
167
+ if ( ArmBase . Arm64 . IsSupported )
168
+ {
169
+ return 63 - ( int ) ArmBase . Arm64 . LeadingZeroCount ( value ) ;
170
+ }
171
+
162
172
uint hi = ( uint ) ( value >> 32 ) ;
163
173
164
174
if ( hi == 0 )
0 commit comments