@@ -172,12 +172,41 @@ NS_INLINE uint_fast8_t common_count_bits(uint8_t byte);
172
172
/*
173
173
* Count leading zeros in a byte
174
174
*
175
+ * \deprecated Use common_count_leading_zeros_8
176
+ *
175
177
* \param byte byte to inspect
176
178
*
177
179
* \return number of leading zeros in byte (0-8)
178
180
*/
179
181
NS_INLINE uint_fast8_t common_count_leading_zeros (uint8_t byte );
180
182
183
+ /*
184
+ * Count leading zeros in a byte
185
+ *
186
+ * \param byte byte to inspect
187
+ *
188
+ * \return number of leading zeros in byte (0-8)
189
+ */
190
+ NS_INLINE uint_fast8_t common_count_leading_zeros_8 (uint8_t byte );
191
+
192
+ /*
193
+ * Count leading zeros in a 16-bit value
194
+ *
195
+ * \param value value to inspect
196
+ *
197
+ * \return number of leading zeros in byte (0-16)
198
+ */
199
+ NS_INLINE uint_fast8_t common_count_leading_zeros_16 (uint16_t value );
200
+
201
+ /*
202
+ * Count leading zeros in a 32-bit value
203
+ *
204
+ * \param value value to inspect
205
+ *
206
+ * \return number of leading zeros in byte (0-32)
207
+ */
208
+ NS_INLINE uint_fast8_t common_count_leading_zeros_32 (uint32_t value );
209
+
181
210
/*
182
211
* Compare 8-bit serial numbers
183
212
*
@@ -434,6 +463,11 @@ COMMON_FUNCTIONS_FN uint_fast8_t common_count_bits(uint8_t byte)
434
463
}
435
464
436
465
COMMON_FUNCTIONS_FN uint_fast8_t common_count_leading_zeros (uint8_t byte )
466
+ {
467
+ return common_count_leading_zeros_8 (byte );
468
+ }
469
+
470
+ COMMON_FUNCTIONS_FN uint_fast8_t common_count_leading_zeros_8 (uint8_t byte )
437
471
{
438
472
#ifdef __CC_ARM
439
473
return byte ? __clz ((unsigned int ) byte << 24 ) : 8 ;
@@ -460,6 +494,72 @@ COMMON_FUNCTIONS_FN uint_fast8_t common_count_leading_zeros(uint8_t byte)
460
494
#endif
461
495
}
462
496
497
+ COMMON_FUNCTIONS_FN uint_fast8_t common_count_leading_zeros_16 (uint16_t value )
498
+ {
499
+ #ifdef __CC_ARM
500
+ return value ? __clz ((unsigned int ) value << 16 ) : 16 ;
501
+ #elif defined __GNUC__
502
+ return value ? __builtin_clz ((unsigned int ) value << 16 ) : 16 ;
503
+ #else
504
+ uint_fast8_t cnt = 0 ;
505
+ if (value == 0 ) {
506
+ return 16 ;
507
+ }
508
+ if ((value & 0xFF00 ) == 0 ) {
509
+ value <<= 8 ;
510
+ cnt += 8 ;
511
+ }
512
+ if ((value & 0xF000 ) == 0 ) {
513
+ value <<= 4 ;
514
+ cnt += 4 ;
515
+ }
516
+ if ((value & 0xC000 ) == 0 ) {
517
+ value <<= 2 ;
518
+ cnt += 2 ;
519
+ }
520
+ if ((value & 0x8000 ) == 0 ) {
521
+ cnt += 1 ;
522
+ }
523
+
524
+ return cnt ;
525
+ #endif
526
+ }
527
+
528
+ COMMON_FUNCTIONS_FN uint_fast8_t common_count_leading_zeros_32 (uint32_t value )
529
+ {
530
+ #ifdef __CC_ARM
531
+ return __clz (value );
532
+ #elif defined __GNUC__
533
+ return value ? __builtin_clz (value ) : 32 ;
534
+ #else
535
+ uint_fast8_t cnt = 0 ;
536
+ if (value == 0 ) {
537
+ return 32 ;
538
+ }
539
+ if ((value & 0xFFFF0000 ) == 0 ) {
540
+ value <<= 16 ;
541
+ cnt += 16 ;
542
+ }
543
+ if ((value & 0xFF000000 ) == 0 ) {
544
+ value <<= 8 ;
545
+ cnt += 8 ;
546
+ }
547
+ if ((value & 0xF0000000 ) == 0 ) {
548
+ value <<= 4 ;
549
+ cnt += 4 ;
550
+ }
551
+ if ((value & 0xC0000000 ) == 0 ) {
552
+ value <<= 2 ;
553
+ cnt += 2 ;
554
+ }
555
+ if ((value & 0x80000000 ) == 0 ) {
556
+ cnt += 1 ;
557
+ }
558
+
559
+ return cnt ;
560
+ #endif
561
+ }
562
+
463
563
COMMON_FUNCTIONS_FN bool common_serial_number_greater_8 (uint8_t s1 , uint8_t s2 )
464
564
{
465
565
return (s1 > s2 && s1 - s2 < UINT8_C (0x80 )) || (s1 < s2 && s2 - s1 > UINT8_C (0x80 ));
0 commit comments