clz_ctz.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * lib/clz_ctz.c
  3. *
  4. * Copyright (C) 2013 Chanho Min <chanho.min@lge.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. * The functions in this file aren't called directly, but are required by
  10. * GCC builtins such as __builtin_ctz, and therefore they can't be removed
  11. * despite appearing unreferenced in kernel source.
  12. *
  13. * __c[lt]z[sd]i2 can be overridden by linking arch-specific versions.
  14. */
  15. #include <linux/export.h>
  16. #include <linux/kernel.h>
  17. int __weak __ctzsi2(int val);
  18. int __weak __ctzsi2(int val)
  19. {
  20. return __ffs(val);
  21. }
  22. EXPORT_SYMBOL(__ctzsi2);
  23. int __weak __clzsi2(int val);
  24. int __weak __clzsi2(int val)
  25. {
  26. return 32 - fls(val);
  27. }
  28. EXPORT_SYMBOL(__clzsi2);
  29. int __weak __clzdi2(long val);
  30. int __weak __ctzdi2(long val);
  31. #if BITS_PER_LONG == 32
  32. int __weak __clzdi2(long val)
  33. {
  34. return 32 - fls((int)val);
  35. }
  36. EXPORT_SYMBOL(__clzdi2);
  37. int __weak __ctzdi2(long val)
  38. {
  39. return __ffs((u32)val);
  40. }
  41. EXPORT_SYMBOL(__ctzdi2);
  42. #elif BITS_PER_LONG == 64
  43. int __weak __clzdi2(long val)
  44. {
  45. return 64 - fls64((u64)val);
  46. }
  47. EXPORT_SYMBOL(__clzdi2);
  48. int __weak __ctzdi2(long val)
  49. {
  50. return __ffs64((u64)val);
  51. }
  52. EXPORT_SYMBOL(__ctzdi2);
  53. #else
  54. #error BITS_PER_LONG not 32 or 64
  55. #endif