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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #ifndef _STRINGS_H
  2. #define _STRINGS_H
  3. /** @file
  4. *
  5. * String functions
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  9. #include <string.h>
  10. #include <bits/strings.h>
  11. /**
  12. * Find first (i.e. least significant) set bit
  13. *
  14. * @v x Value
  15. * @ret lsb Least significant bit set in value (LSB=1), or zero
  16. */
  17. static inline __attribute__ (( always_inline )) int
  18. __constant_ffsll ( unsigned long long x ) {
  19. int r = 0;
  20. if ( ! ( x & 0x00000000ffffffffULL ) ) {
  21. x >>= 32;
  22. r += 32;
  23. }
  24. if ( ! ( x & 0x0000ffffUL ) ) {
  25. x >>= 16;
  26. r += 16;
  27. }
  28. if ( ! ( x & 0x00ff ) ) {
  29. x >>= 8;
  30. r += 8;
  31. }
  32. if ( ! ( x & 0x0f ) ) {
  33. x >>= 4;
  34. r += 4;
  35. }
  36. if ( ! ( x & 0x3 ) ) {
  37. x >>= 2;
  38. r += 2;
  39. }
  40. if ( ! ( x & 0x1 ) ) {
  41. x >>= 1;
  42. r += 1;
  43. }
  44. return ( x ? ( r + 1 ) : 0 );
  45. }
  46. /**
  47. * Find first (i.e. least significant) set bit
  48. *
  49. * @v x Value
  50. * @ret lsb Least significant bit set in value (LSB=1), or zero
  51. */
  52. static inline __attribute__ (( always_inline )) int
  53. __constant_ffsl ( unsigned long x ) {
  54. return __constant_ffsll ( x );
  55. }
  56. /**
  57. * Find last (i.e. most significant) set bit
  58. *
  59. * @v x Value
  60. * @ret msb Most significant bit set in value (LSB=1), or zero
  61. */
  62. static inline __attribute__ (( always_inline )) int
  63. __constant_flsll ( unsigned long long x ) {
  64. int r = 0;
  65. if ( x & 0xffffffff00000000ULL ) {
  66. x >>= 32;
  67. r += 32;
  68. }
  69. if ( x & 0xffff0000UL ) {
  70. x >>= 16;
  71. r += 16;
  72. }
  73. if ( x & 0xff00 ) {
  74. x >>= 8;
  75. r += 8;
  76. }
  77. if ( x & 0xf0 ) {
  78. x >>= 4;
  79. r += 4;
  80. }
  81. if ( x & 0xc ) {
  82. x >>= 2;
  83. r += 2;
  84. }
  85. if ( x & 0x2 ) {
  86. x >>= 1;
  87. r += 1;
  88. }
  89. return ( x ? ( r + 1 ) : 0 );
  90. }
  91. /**
  92. * Find last (i.e. most significant) set bit
  93. *
  94. * @v x Value
  95. * @ret msb Most significant bit set in value (LSB=1), or zero
  96. */
  97. static inline __attribute__ (( always_inline )) int
  98. __constant_flsl ( unsigned long x ) {
  99. return __constant_flsll ( x );
  100. }
  101. int __ffsll ( long long x );
  102. int __ffsl ( long x );
  103. int __flsll ( long long x );
  104. int __flsl ( long x );
  105. /**
  106. * Find first (i.e. least significant) set bit
  107. *
  108. * @v x Value
  109. * @ret lsb Least significant bit set in value (LSB=1), or zero
  110. */
  111. #define ffsll( x ) \
  112. ( __builtin_constant_p ( x ) ? __constant_ffsll ( x ) : __ffsll ( x ) )
  113. /**
  114. * Find first (i.e. least significant) set bit
  115. *
  116. * @v x Value
  117. * @ret lsb Least significant bit set in value (LSB=1), or zero
  118. */
  119. #define ffsl( x ) \
  120. ( __builtin_constant_p ( x ) ? __constant_ffsl ( x ) : __ffsl ( x ) )
  121. /**
  122. * Find first (i.e. least significant) set bit
  123. *
  124. * @v x Value
  125. * @ret lsb Least significant bit set in value (LSB=1), or zero
  126. */
  127. #define ffs( x ) ffsl ( x )
  128. /**
  129. * Find last (i.e. most significant) set bit
  130. *
  131. * @v x Value
  132. * @ret msb Most significant bit set in value (LSB=1), or zero
  133. */
  134. #define flsll( x ) \
  135. ( __builtin_constant_p ( x ) ? __constant_flsll ( x ) : __flsll ( x ) )
  136. /**
  137. * Find last (i.e. most significant) set bit
  138. *
  139. * @v x Value
  140. * @ret msb Most significant bit set in value (LSB=1), or zero
  141. */
  142. #define flsl( x ) \
  143. ( __builtin_constant_p ( x ) ? __constant_flsl ( x ) : __flsl ( x ) )
  144. /**
  145. * Find last (i.e. most significant) set bit
  146. *
  147. * @v x Value
  148. * @ret msb Most significant bit set in value (LSB=1), or zero
  149. */
  150. #define fls( x ) flsl ( x )
  151. /**
  152. * Copy memory
  153. *
  154. * @v src Source
  155. * @v dest Destination
  156. * @v len Length
  157. */
  158. static inline __attribute__ (( always_inline )) void
  159. bcopy ( const void *src, void *dest, size_t len ) {
  160. memmove ( dest, src, len );
  161. }
  162. /**
  163. * Zero memory
  164. *
  165. * @v dest Destination
  166. * @v len Length
  167. */
  168. static inline __attribute__ (( always_inline )) void
  169. bzero ( void *dest, size_t len ) {
  170. memset ( dest, 0, len );
  171. }
  172. int __pure strcasecmp ( const char *first, const char *second ) __nonnull;
  173. #endif /* _STRINGS_H */