]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgfortran/intrinsics/bit_intrinsics.c
Update copyright years.
[thirdparty/gcc.git] / libgfortran / intrinsics / bit_intrinsics.c
CommitLineData
0a05c536 1/* Implementation of the bit intrinsics not implemented as GCC builtins.
818ab71a 2 Copyright (C) 2009-2016 Free Software Foundation, Inc.
0a05c536
FXC
3
4This file is part of the GNU Fortran runtime library (libgfortran).
5
6Libgfortran is free software; you can redistribute it and/or
7modify it under the terms of the GNU General Public
8License as published by the Free Software Foundation; either
9version 3 of the License, or (at your option) any later version.
10
11Libgfortran is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16Under Section 7 of GPL version 3, you are granted additional
17permissions described in the GCC Runtime Library Exception, version
183.1, as published by the Free Software Foundation.
19
20You should have received a copy of the GNU General Public License and
21a copy of the GCC Runtime Library Exception along with this program;
22see 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
29extern int clz128 (GFC_INTEGER_16);
30export_proto(clz128);
31
32int
33clz128 (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
86extern int ctz128 (GFC_INTEGER_16);
87export_proto(ctz128);
88
89int
90ctz128 (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