You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

strings.h 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #ifndef _BITS_STRINGS_H
  2. #define _BITS_STRINGS_H
  3. /** @file
  4. *
  5. * String functions
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  9. /**
  10. * Find first (i.e. least significant) set bit
  11. *
  12. * @v value Value
  13. * @ret lsb Least significant bit set in value (LSB=1), or zero
  14. */
  15. static inline __attribute__ (( always_inline )) int __ffsll ( long long value ){
  16. unsigned long long bits = value;
  17. unsigned long long lsb;
  18. unsigned int lz;
  19. /* Extract least significant set bit */
  20. lsb = ( bits & -bits );
  21. /* Count number of leading zeroes before LSB */
  22. __asm__ ( "clz %0, %1" : "=r" ( lz ) : "r" ( lsb ) );
  23. return ( 64 - lz );
  24. }
  25. /**
  26. * Find first (i.e. least significant) set bit
  27. *
  28. * @v value Value
  29. * @ret lsb Least significant bit set in value (LSB=1), or zero
  30. */
  31. static inline __attribute__ (( always_inline )) int __ffsl ( long value ) {
  32. return __ffsll ( value );
  33. }
  34. /**
  35. * Find last (i.e. most significant) set bit
  36. *
  37. * @v value Value
  38. * @ret msb Most significant bit set in value (LSB=1), or zero
  39. */
  40. static inline __attribute__ (( always_inline )) int __flsll ( long long value ){
  41. unsigned int lz;
  42. /* Count number of leading zeroes */
  43. __asm__ ( "clz %0, %1" : "=r" ( lz ) : "r" ( value ) );
  44. return ( 64 - lz );
  45. }
  46. /**
  47. * Find last (i.e. most significant) set bit
  48. *
  49. * @v value Value
  50. * @ret msb Most significant bit set in value (LSB=1), or zero
  51. */
  52. static inline __attribute__ (( always_inline )) int __flsl ( long value ) {
  53. return __flsll ( value );
  54. }
  55. #endif /* _BITS_STRINGS_H */