bit_intrinsics.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* Implementation of the bit intrinsics not implemented as GCC builtins.
  2. Copyright (C) 2009-2015 Free Software Foundation, Inc.
  3. This file is part of the GNU Fortran runtime library (libgfortran).
  4. Libgfortran is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public
  6. License as published by the Free Software Foundation; either
  7. version 3 of the License, or (at your option) any later version.
  8. Libgfortran is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include "libgfortran.h"
  20. #ifdef HAVE_GFC_INTEGER_16
  21. extern int clz128 (GFC_INTEGER_16);
  22. export_proto(clz128);
  23. int
  24. clz128 (GFC_INTEGER_16 x)
  25. {
  26. int res = 127;
  27. // We can't write 0xFFFFFFFFFFFFFFFF0000000000000000, so we work around it
  28. if (x & ((__uint128_t) 0xFFFFFFFFFFFFFFFF << 64))
  29. {
  30. res -= 64;
  31. x >>= 64;
  32. }
  33. if (x & 0xFFFFFFFF00000000)
  34. {
  35. res -= 32;
  36. x >>= 32;
  37. }
  38. if (x & 0xFFFF0000)
  39. {
  40. res -= 16;
  41. x >>= 16;
  42. }
  43. if (x & 0xFF00)
  44. {
  45. res -= 8;
  46. x >>= 8;
  47. }
  48. if (x & 0xF0)
  49. {
  50. res -= 4;
  51. x >>= 4;
  52. }
  53. if (x & 0xC)
  54. {
  55. res -= 2;
  56. x >>= 2;
  57. }
  58. if (x & 0x2)
  59. {
  60. res -= 1;
  61. x >>= 1;
  62. }
  63. return res;
  64. }
  65. #endif
  66. #ifdef HAVE_GFC_INTEGER_16
  67. extern int ctz128 (GFC_INTEGER_16);
  68. export_proto(ctz128);
  69. int
  70. ctz128 (GFC_INTEGER_16 x)
  71. {
  72. int res = 0;
  73. if ((x & 0xFFFFFFFFFFFFFFFF) == 0)
  74. {
  75. res += 64;
  76. x >>= 64;
  77. }
  78. if ((x & 0xFFFFFFFF) == 0)
  79. {
  80. res += 32;
  81. x >>= 32;
  82. }
  83. if ((x & 0xFFFF) == 0)
  84. {
  85. res += 16;
  86. x >>= 16;
  87. }
  88. if ((x & 0xFF) == 0)
  89. {
  90. res += 8;
  91. x >>= 8;
  92. }
  93. if ((x & 0xF) == 0)
  94. {
  95. res += 4;
  96. x >>= 4;
  97. }
  98. if ((x & 0x3) == 0)
  99. {
  100. res += 2;
  101. x >>= 2;
  102. }
  103. if ((x & 0x1) == 0)
  104. {
  105. res += 1;
  106. x >>= 1;
  107. }
  108. return res;
  109. }
  110. #endif