logo

oasis

Own branch of Oasis Linux (upstream: <https://git.sr.ht/~mcf/oasis/>) git clone https://anongit.hacktivis.me/git/oasis.git

0001-Add-portable-fallback-for-ctz-clz-and-clzll.patch (1732B)


  1. From 881ceef5adda6f1cc5f5a9a15ae74b068bf85dd4 Mon Sep 17 00:00:00 2001
  2. From: Michael Forney <mforney@mforney.org>
  3. Date: Fri, 30 Apr 2021 18:45:18 -0700
  4. Subject: [PATCH] Add portable fallback for ctz, clz, and clzll
  5. ---
  6. include/common/attributes.h | 27 +++++++++++++++++++++++++--
  7. 1 file changed, 25 insertions(+), 2 deletions(-)
  8. diff --git a/include/common/attributes.h b/include/common/attributes.h
  9. index 4ccc421..72d5202 100644
  10. --- a/include/common/attributes.h
  11. +++ b/include/common/attributes.h
  12. @@ -32,6 +32,7 @@
  13. #include <stddef.h>
  14. #include <assert.h>
  15. +#include <strings.h>
  16. #ifndef __has_attribute
  17. #define __has_attribute(x) 0
  18. @@ -156,7 +157,7 @@ static inline int clzll(const unsigned long long mask) {
  19. return clz((unsigned)mask) + 32;
  20. }
  21. #endif /* _WIN64 */
  22. -#else /* !_MSC_VER */
  23. +#elif defined(__GNUC__)
  24. static inline int ctz(const unsigned int mask) {
  25. return __builtin_ctz(mask);
  26. }
  27. @@ -168,7 +169,29 @@ static inline int clz(const unsigned int mask) {
  28. static inline int clzll(const unsigned long long mask) {
  29. return __builtin_clzll(mask);
  30. }
  31. -#endif /* !_MSC_VER */
  32. +#else /* __GNUC__ */
  33. +static inline int ctz(const unsigned int mask) {
  34. + return ffs(mask) - 1;
  35. +}
  36. +
  37. +static inline int clz(unsigned int mask) {
  38. + mask >>= 1;
  39. + mask |= mask >> 1;
  40. + mask |= mask >> 2;
  41. + mask |= mask >> 4;
  42. + mask |= mask >> 8;
  43. + mask |= mask >> 16;
  44. + mask++;
  45. + return 32 - ffs(mask);
  46. +}
  47. +
  48. +static inline int clzll(unsigned long long mask) {
  49. + if (mask >> 32)
  50. + return clz((unsigned)(mask >> 32));
  51. + else
  52. + return clz((unsigned)mask) + 32;
  53. +}
  54. +#endif /* !_MSC_VER && !__GNUC__ */
  55. #ifndef static_assert
  56. #define CHECK_OFFSET(type, field, name) \
  57. --
  58. 2.32.0