re PR driver/57651 (gcc-ar and gcc-nm don't find the lto plugin)
[official-gcc.git] / libgfortran / intrinsics / bit_intrinsics.c
blob1a69606f477775e8e119d45c6198a3967c8adfe1
1 /* Implementation of the bit intrinsics not implemented as GCC builtins.
2 Copyright (C) 2009-2013 Free Software Foundation, Inc.
4 This file is part of the GNU Fortran runtime library (libgfortran).
6 Libgfortran is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public
8 License as published by the Free Software Foundation; either
9 version 3 of the License, or (at your option) any later version.
11 Libgfortran is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
25 #include "libgfortran.h"
28 #ifdef HAVE_GFC_INTEGER_16
29 extern int clz128 (GFC_INTEGER_16);
30 export_proto(clz128);
32 int
33 clz128 (GFC_INTEGER_16 x)
35 int res = 127;
37 // We can't write 0xFFFFFFFFFFFFFFFF0000000000000000, so we work around it
38 if (x & ((__uint128_t) 0xFFFFFFFFFFFFFFFF << 64))
40 res -= 64;
41 x >>= 64;
44 if (x & 0xFFFFFFFF00000000)
46 res -= 32;
47 x >>= 32;
50 if (x & 0xFFFF0000)
52 res -= 16;
53 x >>= 16;
56 if (x & 0xFF00)
58 res -= 8;
59 x >>= 8;
62 if (x & 0xF0)
64 res -= 4;
65 x >>= 4;
68 if (x & 0xC)
70 res -= 2;
71 x >>= 2;
74 if (x & 0x2)
76 res -= 1;
77 x >>= 1;
80 return res;
82 #endif
85 #ifdef HAVE_GFC_INTEGER_16
86 extern int ctz128 (GFC_INTEGER_16);
87 export_proto(ctz128);
89 int
90 ctz128 (GFC_INTEGER_16 x)
92 int res = 0;
94 if ((x & 0xFFFFFFFFFFFFFFFF) == 0)
96 res += 64;
97 x >>= 64;
100 if ((x & 0xFFFFFFFF) == 0)
102 res += 32;
103 x >>= 32;
106 if ((x & 0xFFFF) == 0)
108 res += 16;
109 x >>= 16;
112 if ((x & 0xFF) == 0)
114 res += 8;
115 x >>= 8;
118 if ((x & 0xF) == 0)
120 res += 4;
121 x >>= 4;
124 if ((x & 0x3) == 0)
126 res += 2;
127 x >>= 2;
130 if ((x & 0x1) == 0)
132 res += 1;
133 x >>= 1;
136 return res;
138 #endif