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 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #ifndef _STRINGS_H
  2. #define _STRINGS_H
  3. /** @file
  4. *
  5. * String functions
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER );
  9. #include <string.h>
  10. #include <bits/strings.h>
  11. /**
  12. * Find last (i.e. most significant) set bit
  13. *
  14. * @v x Value
  15. * @ret msb Most significant bit set in value (LSB=1), or zero
  16. */
  17. static inline __attribute__ (( always_inline )) int
  18. __constant_flsll ( unsigned long long x ) {
  19. int r = 0;
  20. if ( x & 0xffffffff00000000ULL ) {
  21. x >>= 32;
  22. r += 32;
  23. }
  24. if ( x & 0xffff0000UL ) {
  25. x >>= 16;
  26. r += 16;
  27. }
  28. if ( x & 0xff00 ) {
  29. x >>= 8;
  30. r += 8;
  31. }
  32. if ( x & 0xf0 ) {
  33. x >>= 4;
  34. r += 4;
  35. }
  36. if ( x & 0xc ) {
  37. x >>= 2;
  38. r += 2;
  39. }
  40. if ( x & 0x2 ) {
  41. x >>= 1;
  42. r += 1;
  43. }
  44. if ( x & 0x1 ) {
  45. r += 1;
  46. }
  47. return r;
  48. }
  49. /**
  50. * Find last (i.e. most significant) set bit
  51. *
  52. * @v x Value
  53. * @ret msb Most significant bit set in value (LSB=1), or zero
  54. */
  55. static inline __attribute__ (( always_inline )) int
  56. __constant_flsl ( unsigned long x ) {
  57. return __constant_flsll ( x );
  58. }
  59. int __flsll ( long long x );
  60. int __flsl ( long x );
  61. /**
  62. * Find last (i.e. most significant) set bit
  63. *
  64. * @v x Value
  65. * @ret msb Most significant bit set in value (LSB=1), or zero
  66. */
  67. #define flsll( x ) \
  68. ( __builtin_constant_p ( x ) ? __constant_flsll ( x ) : __flsll ( x ) )
  69. /**
  70. * Find last (i.e. most significant) set bit
  71. *
  72. * @v x Value
  73. * @ret msb Most significant bit set in value (LSB=1), or zero
  74. */
  75. #define flsl( x ) \
  76. ( __builtin_constant_p ( x ) ? __constant_flsl ( x ) : __flsl ( x ) )
  77. /**
  78. * Find last (i.e. most significant) set bit
  79. *
  80. * @v x Value
  81. * @ret msb Most significant bit set in value (LSB=1), or zero
  82. */
  83. #define fls( x ) flsl ( x )
  84. /**
  85. * Copy memory
  86. *
  87. * @v src Source
  88. * @v dest Destination
  89. * @v len Length
  90. */
  91. static inline __attribute__ (( always_inline )) void
  92. bcopy ( const void *src, void *dest, size_t len ) {
  93. memmove ( dest, src, len );
  94. }
  95. /**
  96. * Zero memory
  97. *
  98. * @v dest Destination
  99. * @v len Length
  100. */
  101. static inline __attribute__ (( always_inline )) void
  102. bzero ( void *dest, size_t len ) {
  103. memset ( dest, 0, len );
  104. }
  105. int __pure strcasecmp ( const char *first, const char *second ) __nonnull;
  106. #endif /* _STRINGS_H */