1*e4b17023SJohn Marino /* Operations on HOST_WIDE_INT.
2*e4b17023SJohn Marino Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3*e4b17023SJohn Marino 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4*e4b17023SJohn Marino Free Software Foundation, Inc.
5*e4b17023SJohn Marino
6*e4b17023SJohn Marino This file is part of GCC.
7*e4b17023SJohn Marino
8*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under
9*e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free
10*e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later
11*e4b17023SJohn Marino version.
12*e4b17023SJohn Marino
13*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
15*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16*e4b17023SJohn Marino for more details.
17*e4b17023SJohn Marino
18*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
19*e4b17023SJohn Marino along with GCC; see the file COPYING3. If not see
20*e4b17023SJohn Marino <http://www.gnu.org/licenses/>. */
21*e4b17023SJohn Marino
22*e4b17023SJohn Marino #include "config.h"
23*e4b17023SJohn Marino #include "system.h"
24*e4b17023SJohn Marino #include "diagnostic-core.h"
25*e4b17023SJohn Marino
26*e4b17023SJohn Marino #if GCC_VERSION < 3004
27*e4b17023SJohn Marino
28*e4b17023SJohn Marino /* The functions clz_hwi, ctz_hwi, ffs_hwi, floor_log2 and exact_log2
29*e4b17023SJohn Marino are defined as inline functions in hwint.h if GCC_VERSION >= 3004.
30*e4b17023SJohn Marino The definitions here are used for older versions of GCC and non-GCC
31*e4b17023SJohn Marino bootstrap compilers. */
32*e4b17023SJohn Marino
33*e4b17023SJohn Marino /* Given X, an unsigned number, return the largest int Y such that 2**Y <= X.
34*e4b17023SJohn Marino If X is 0, return -1. */
35*e4b17023SJohn Marino
36*e4b17023SJohn Marino int
floor_log2(unsigned HOST_WIDE_INT x)37*e4b17023SJohn Marino floor_log2 (unsigned HOST_WIDE_INT x)
38*e4b17023SJohn Marino {
39*e4b17023SJohn Marino int t = 0;
40*e4b17023SJohn Marino
41*e4b17023SJohn Marino if (x == 0)
42*e4b17023SJohn Marino return -1;
43*e4b17023SJohn Marino
44*e4b17023SJohn Marino if (HOST_BITS_PER_WIDE_INT > 64)
45*e4b17023SJohn Marino if (x >= (unsigned HOST_WIDE_INT) 1 << (t + 64))
46*e4b17023SJohn Marino t += 64;
47*e4b17023SJohn Marino if (HOST_BITS_PER_WIDE_INT > 32)
48*e4b17023SJohn Marino if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 32))
49*e4b17023SJohn Marino t += 32;
50*e4b17023SJohn Marino if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 16))
51*e4b17023SJohn Marino t += 16;
52*e4b17023SJohn Marino if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 8))
53*e4b17023SJohn Marino t += 8;
54*e4b17023SJohn Marino if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 4))
55*e4b17023SJohn Marino t += 4;
56*e4b17023SJohn Marino if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 2))
57*e4b17023SJohn Marino t += 2;
58*e4b17023SJohn Marino if (x >= ((unsigned HOST_WIDE_INT) 1) << (t + 1))
59*e4b17023SJohn Marino t += 1;
60*e4b17023SJohn Marino
61*e4b17023SJohn Marino return t;
62*e4b17023SJohn Marino }
63*e4b17023SJohn Marino
64*e4b17023SJohn Marino /* Return the logarithm of X, base 2, considering X unsigned,
65*e4b17023SJohn Marino if X is a power of 2. Otherwise, returns -1. */
66*e4b17023SJohn Marino
67*e4b17023SJohn Marino int
exact_log2(unsigned HOST_WIDE_INT x)68*e4b17023SJohn Marino exact_log2 (unsigned HOST_WIDE_INT x)
69*e4b17023SJohn Marino {
70*e4b17023SJohn Marino if (x != (x & -x))
71*e4b17023SJohn Marino return -1;
72*e4b17023SJohn Marino return floor_log2 (x);
73*e4b17023SJohn Marino }
74*e4b17023SJohn Marino
75*e4b17023SJohn Marino /* Given X, an unsigned number, return the number of least significant bits
76*e4b17023SJohn Marino that are zero. When X == 0, the result is the word size. */
77*e4b17023SJohn Marino
78*e4b17023SJohn Marino int
ctz_hwi(unsigned HOST_WIDE_INT x)79*e4b17023SJohn Marino ctz_hwi (unsigned HOST_WIDE_INT x)
80*e4b17023SJohn Marino {
81*e4b17023SJohn Marino return x ? floor_log2 (x & -x) : HOST_BITS_PER_WIDE_INT;
82*e4b17023SJohn Marino }
83*e4b17023SJohn Marino
84*e4b17023SJohn Marino /* Similarly for most significant bits. */
85*e4b17023SJohn Marino
86*e4b17023SJohn Marino int
clz_hwi(unsigned HOST_WIDE_INT x)87*e4b17023SJohn Marino clz_hwi (unsigned HOST_WIDE_INT x)
88*e4b17023SJohn Marino {
89*e4b17023SJohn Marino return HOST_BITS_PER_WIDE_INT - 1 - floor_log2(x);
90*e4b17023SJohn Marino }
91*e4b17023SJohn Marino
92*e4b17023SJohn Marino /* Similar to ctz_hwi, except that the least significant bit is numbered
93*e4b17023SJohn Marino starting from 1, and X == 0 yields 0. */
94*e4b17023SJohn Marino
95*e4b17023SJohn Marino int
ffs_hwi(unsigned HOST_WIDE_INT x)96*e4b17023SJohn Marino ffs_hwi (unsigned HOST_WIDE_INT x)
97*e4b17023SJohn Marino {
98*e4b17023SJohn Marino return 1 + floor_log2 (x & -x);
99*e4b17023SJohn Marino }
100*e4b17023SJohn Marino
101*e4b17023SJohn Marino #endif /* GCC_VERSION < 3004 */
102*e4b17023SJohn Marino
103*e4b17023SJohn Marino /* Compute the absolute value of X. */
104*e4b17023SJohn Marino
105*e4b17023SJohn Marino HOST_WIDE_INT
abs_hwi(HOST_WIDE_INT x)106*e4b17023SJohn Marino abs_hwi (HOST_WIDE_INT x)
107*e4b17023SJohn Marino {
108*e4b17023SJohn Marino gcc_checking_assert (x != HOST_WIDE_INT_MIN);
109*e4b17023SJohn Marino return x >= 0 ? x : -x;
110*e4b17023SJohn Marino }
111*e4b17023SJohn Marino
112*e4b17023SJohn Marino /* Compute the absolute value of X as an unsigned type. */
113*e4b17023SJohn Marino
114*e4b17023SJohn Marino unsigned HOST_WIDE_INT
absu_hwi(HOST_WIDE_INT x)115*e4b17023SJohn Marino absu_hwi (HOST_WIDE_INT x)
116*e4b17023SJohn Marino {
117*e4b17023SJohn Marino return x >= 0 ? (unsigned HOST_WIDE_INT)x : -(unsigned HOST_WIDE_INT)x;
118*e4b17023SJohn Marino }
119*e4b17023SJohn Marino
120*e4b17023SJohn Marino /* Compute the greatest common divisor of two numbers A and B using
121*e4b17023SJohn Marino Euclid's algorithm. */
122*e4b17023SJohn Marino
123*e4b17023SJohn Marino HOST_WIDE_INT
gcd(HOST_WIDE_INT a,HOST_WIDE_INT b)124*e4b17023SJohn Marino gcd (HOST_WIDE_INT a, HOST_WIDE_INT b)
125*e4b17023SJohn Marino {
126*e4b17023SJohn Marino HOST_WIDE_INT x, y, z;
127*e4b17023SJohn Marino
128*e4b17023SJohn Marino x = abs_hwi (a);
129*e4b17023SJohn Marino y = abs_hwi (b);
130*e4b17023SJohn Marino
131*e4b17023SJohn Marino while (x > 0)
132*e4b17023SJohn Marino {
133*e4b17023SJohn Marino z = y % x;
134*e4b17023SJohn Marino y = x;
135*e4b17023SJohn Marino x = z;
136*e4b17023SJohn Marino }
137*e4b17023SJohn Marino
138*e4b17023SJohn Marino return y;
139*e4b17023SJohn Marino }
140*e4b17023SJohn Marino
141*e4b17023SJohn Marino /* For X and Y positive integers, return X multiplied by Y and check
142*e4b17023SJohn Marino that the result does not overflow. */
143*e4b17023SJohn Marino
144*e4b17023SJohn Marino HOST_WIDE_INT
pos_mul_hwi(HOST_WIDE_INT x,HOST_WIDE_INT y)145*e4b17023SJohn Marino pos_mul_hwi (HOST_WIDE_INT x, HOST_WIDE_INT y)
146*e4b17023SJohn Marino {
147*e4b17023SJohn Marino if (x != 0)
148*e4b17023SJohn Marino gcc_checking_assert ((HOST_WIDE_INT_MAX) / x >= y);
149*e4b17023SJohn Marino
150*e4b17023SJohn Marino return x * y;
151*e4b17023SJohn Marino }
152*e4b17023SJohn Marino
153*e4b17023SJohn Marino /* Return X multiplied by Y and check that the result does not
154*e4b17023SJohn Marino overflow. */
155*e4b17023SJohn Marino
156*e4b17023SJohn Marino HOST_WIDE_INT
mul_hwi(HOST_WIDE_INT x,HOST_WIDE_INT y)157*e4b17023SJohn Marino mul_hwi (HOST_WIDE_INT x, HOST_WIDE_INT y)
158*e4b17023SJohn Marino {
159*e4b17023SJohn Marino gcc_checking_assert (x != HOST_WIDE_INT_MIN
160*e4b17023SJohn Marino && y != HOST_WIDE_INT_MIN);
161*e4b17023SJohn Marino
162*e4b17023SJohn Marino if (x >= 0)
163*e4b17023SJohn Marino {
164*e4b17023SJohn Marino if (y >= 0)
165*e4b17023SJohn Marino return pos_mul_hwi (x, y);
166*e4b17023SJohn Marino
167*e4b17023SJohn Marino return -pos_mul_hwi (x, -y);
168*e4b17023SJohn Marino }
169*e4b17023SJohn Marino
170*e4b17023SJohn Marino if (y >= 0)
171*e4b17023SJohn Marino return -pos_mul_hwi (-x, y);
172*e4b17023SJohn Marino
173*e4b17023SJohn Marino return pos_mul_hwi (-x, -y);
174*e4b17023SJohn Marino }
175*e4b17023SJohn Marino
176*e4b17023SJohn Marino /* Compute the least common multiple of two numbers A and B . */
177*e4b17023SJohn Marino
178*e4b17023SJohn Marino HOST_WIDE_INT
least_common_multiple(HOST_WIDE_INT a,HOST_WIDE_INT b)179*e4b17023SJohn Marino least_common_multiple (HOST_WIDE_INT a, HOST_WIDE_INT b)
180*e4b17023SJohn Marino {
181*e4b17023SJohn Marino return mul_hwi (abs_hwi (a) / gcd (a, b), abs_hwi (b));
182*e4b17023SJohn Marino }
183