clzsi2.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* ===-- clzsi2.c - Implement __clzsi2 -------------------------------------===
  2. *
  3. * The LLVM Compiler Infrastructure
  4. *
  5. * This file is dual licensed under the MIT and the University of Illinois Open
  6. * Source Licenses. See LICENSE.TXT for details.
  7. *
  8. * ===----------------------------------------------------------------------===
  9. *
  10. * This file implements __clzsi2 for the compiler_rt library.
  11. *
  12. * ===----------------------------------------------------------------------===
  13. */
  14. #include "int_lib.h"
  15. /* Returns: the number of leading 0-bits */
  16. /* Precondition: a != 0 */
  17. COMPILER_RT_ABI si_int
  18. __clzsi2(si_int a)
  19. {
  20. su_int x = (su_int)a;
  21. si_int t = ((x & 0xFFFF0000) == 0) << 4; /* if (x is small) t = 16 else 0 */
  22. x >>= 16 - t; /* x = [0 - 0xFFFF] */
  23. su_int r = t; /* r = [0, 16] */
  24. /* return r + clz(x) */
  25. t = ((x & 0xFF00) == 0) << 3;
  26. x >>= 8 - t; /* x = [0 - 0xFF] */
  27. r += t; /* r = [0, 8, 16, 24] */
  28. /* return r + clz(x) */
  29. t = ((x & 0xF0) == 0) << 2;
  30. x >>= 4 - t; /* x = [0 - 0xF] */
  31. r += t; /* r = [0, 4, 8, 12, 16, 20, 24, 28] */
  32. /* return r + clz(x) */
  33. t = ((x & 0xC) == 0) << 1;
  34. x >>= 2 - t; /* x = [0 - 3] */
  35. r += t; /* r = [0 - 30] and is even */
  36. /* return r + clz(x) */
  37. /* switch (x)
  38. * {
  39. * case 0:
  40. * return r + 2;
  41. * case 1:
  42. * return r + 1;
  43. * case 2:
  44. * case 3:
  45. * return r;
  46. * }
  47. */
  48. return r + ((2 - x) & -((x & 2) == 0));
  49. }