OSDN Git Service

missed hunk from last commit
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / bit_intrinsics.c
1 /* Implementation of the bit intrinsics not implemented as GCC builtins.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3
4 This file is part of the GNU Fortran runtime library (libgfortran).
5
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.
10
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.
15
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.
19
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/>.  */
24
25 #include "libgfortran.h"
26
27
28 #ifdef HAVE_GFC_INTEGER_16
29 extern int clz128 (GFC_INTEGER_16);
30 export_proto(clz128);
31
32 int
33 clz128 (GFC_INTEGER_16 x)
34 {
35   int res = 127;
36
37   // We can't write 0xFFFFFFFFFFFFFFFF0000000000000000, so we work around it
38   if (x & ((__uint128_t) 0xFFFFFFFFFFFFFFFF << 64))
39     {
40       res -= 64;
41       x >>= 64;
42     }
43
44   if (x & 0xFFFFFFFF00000000)
45     {
46       res -= 32;
47       x >>= 32;
48     }
49
50   if (x & 0xFFFF0000)
51     {
52       res -= 16;
53       x >>= 16;
54     }
55
56   if (x & 0xFF00)
57     {
58       res -= 8;
59       x >>= 8;
60     }
61
62   if (x & 0xF0)
63     {
64       res -= 4;
65       x >>= 4;
66     }
67
68   if (x & 0xC)
69     {
70       res -= 2;
71       x >>= 2;
72     }
73
74   if (x & 0x2)
75     {
76       res -= 1;
77       x >>= 1;
78     }
79
80   return res;
81 }
82 #endif
83
84
85 #ifdef HAVE_GFC_INTEGER_16
86 extern int ctz128 (GFC_INTEGER_16);
87 export_proto(ctz128);
88
89 int
90 ctz128 (GFC_INTEGER_16 x)
91 {
92   int res = 0;
93
94   if ((x & 0xFFFFFFFFFFFFFFFF) == 0)
95     {
96       res += 64;
97       x >>= 64;
98     }
99
100   if ((x & 0xFFFFFFFF) == 0)
101     {
102       res += 32;
103       x >>= 32;
104     }
105
106   if ((x & 0xFFFF) == 0)
107     {
108       res += 16;
109       x >>= 16;
110     }
111
112   if ((x & 0xFF) == 0)
113     {
114       res += 8;
115       x >>= 8;
116     }
117
118   if ((x & 0xF) == 0)
119     {
120       res += 4;
121       x >>= 4;
122     }
123
124   if ((x & 0x3) == 0)
125     {
126       res += 2;
127       x >>= 2;
128     }
129
130   if ((x & 0x1) == 0)
131     {
132       res += 1;
133       x >>= 1;
134     }
135
136   return res;
137 }
138 #endif