xref: /dragonfly/contrib/gcc-4.7/gcc/fixed-value.c (revision e4b17023)
1*e4b17023SJohn Marino /* Fixed-point arithmetic support.
2*e4b17023SJohn Marino    Copyright (C) 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
3*e4b17023SJohn Marino 
4*e4b17023SJohn Marino This file is part of GCC.
5*e4b17023SJohn Marino 
6*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under
7*e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free
8*e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later
9*e4b17023SJohn Marino version.
10*e4b17023SJohn Marino 
11*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
13*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14*e4b17023SJohn Marino for more details.
15*e4b17023SJohn Marino 
16*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
17*e4b17023SJohn Marino along with GCC; see the file COPYING3.  If not see
18*e4b17023SJohn Marino <http://www.gnu.org/licenses/>.  */
19*e4b17023SJohn Marino 
20*e4b17023SJohn Marino #include "config.h"
21*e4b17023SJohn Marino #include "system.h"
22*e4b17023SJohn Marino #include "coretypes.h"
23*e4b17023SJohn Marino #include "tm.h"
24*e4b17023SJohn Marino #include "tree.h"
25*e4b17023SJohn Marino #include "diagnostic-core.h"
26*e4b17023SJohn Marino 
27*e4b17023SJohn Marino /* Compare two fixed objects for bitwise identity.  */
28*e4b17023SJohn Marino 
29*e4b17023SJohn Marino bool
fixed_identical(const FIXED_VALUE_TYPE * a,const FIXED_VALUE_TYPE * b)30*e4b17023SJohn Marino fixed_identical (const FIXED_VALUE_TYPE *a, const FIXED_VALUE_TYPE *b)
31*e4b17023SJohn Marino {
32*e4b17023SJohn Marino   return (a->mode == b->mode
33*e4b17023SJohn Marino 	  && a->data.high == b->data.high
34*e4b17023SJohn Marino 	  && a->data.low == b->data.low);
35*e4b17023SJohn Marino }
36*e4b17023SJohn Marino 
37*e4b17023SJohn Marino /* Calculate a hash value.  */
38*e4b17023SJohn Marino 
39*e4b17023SJohn Marino unsigned int
fixed_hash(const FIXED_VALUE_TYPE * f)40*e4b17023SJohn Marino fixed_hash (const FIXED_VALUE_TYPE *f)
41*e4b17023SJohn Marino {
42*e4b17023SJohn Marino   return (unsigned int) (f->data.low ^ f->data.high);
43*e4b17023SJohn Marino }
44*e4b17023SJohn Marino 
45*e4b17023SJohn Marino /* Define the enum code for the range of the fixed-point value.  */
46*e4b17023SJohn Marino enum fixed_value_range_code {
47*e4b17023SJohn Marino   FIXED_OK,		/* The value is within the range.  */
48*e4b17023SJohn Marino   FIXED_UNDERFLOW,	/* The value is less than the minimum.  */
49*e4b17023SJohn Marino   FIXED_GT_MAX_EPS,	/* The value is greater than the maximum, but not equal
50*e4b17023SJohn Marino 			   to the maximum plus the epsilon.  */
51*e4b17023SJohn Marino   FIXED_MAX_EPS		/* The value equals the maximum plus the epsilon.  */
52*e4b17023SJohn Marino };
53*e4b17023SJohn Marino 
54*e4b17023SJohn Marino /* Check REAL_VALUE against the range of the fixed-point mode.
55*e4b17023SJohn Marino    Return FIXED_OK, if it is within the range.
56*e4b17023SJohn Marino           FIXED_UNDERFLOW, if it is less than the minimum.
57*e4b17023SJohn Marino           FIXED_GT_MAX_EPS, if it is greater than the maximum, but not equal to
58*e4b17023SJohn Marino 	    the maximum plus the epsilon.
59*e4b17023SJohn Marino           FIXED_MAX_EPS, if it is equal to the maximum plus the epsilon.  */
60*e4b17023SJohn Marino 
61*e4b17023SJohn Marino static enum fixed_value_range_code
check_real_for_fixed_mode(REAL_VALUE_TYPE * real_value,enum machine_mode mode)62*e4b17023SJohn Marino check_real_for_fixed_mode (REAL_VALUE_TYPE *real_value, enum machine_mode mode)
63*e4b17023SJohn Marino {
64*e4b17023SJohn Marino   REAL_VALUE_TYPE max_value, min_value, epsilon_value;
65*e4b17023SJohn Marino 
66*e4b17023SJohn Marino   real_2expN (&max_value, GET_MODE_IBIT (mode), mode);
67*e4b17023SJohn Marino   real_2expN (&epsilon_value, -GET_MODE_FBIT (mode), mode);
68*e4b17023SJohn Marino 
69*e4b17023SJohn Marino   if (SIGNED_FIXED_POINT_MODE_P (mode))
70*e4b17023SJohn Marino     min_value = real_value_negate (&max_value);
71*e4b17023SJohn Marino   else
72*e4b17023SJohn Marino     real_from_string (&min_value, "0.0");
73*e4b17023SJohn Marino 
74*e4b17023SJohn Marino   if (real_compare (LT_EXPR, real_value, &min_value))
75*e4b17023SJohn Marino     return FIXED_UNDERFLOW;
76*e4b17023SJohn Marino   if (real_compare (EQ_EXPR, real_value, &max_value))
77*e4b17023SJohn Marino     return FIXED_MAX_EPS;
78*e4b17023SJohn Marino   real_arithmetic (&max_value, MINUS_EXPR, &max_value, &epsilon_value);
79*e4b17023SJohn Marino   if (real_compare (GT_EXPR, real_value, &max_value))
80*e4b17023SJohn Marino     return FIXED_GT_MAX_EPS;
81*e4b17023SJohn Marino   return FIXED_OK;
82*e4b17023SJohn Marino }
83*e4b17023SJohn Marino 
84*e4b17023SJohn Marino /* Initialize from a decimal or hexadecimal string.  */
85*e4b17023SJohn Marino 
86*e4b17023SJohn Marino void
fixed_from_string(FIXED_VALUE_TYPE * f,const char * str,enum machine_mode mode)87*e4b17023SJohn Marino fixed_from_string (FIXED_VALUE_TYPE *f, const char *str, enum machine_mode mode)
88*e4b17023SJohn Marino {
89*e4b17023SJohn Marino   REAL_VALUE_TYPE real_value, fixed_value, base_value;
90*e4b17023SJohn Marino   unsigned int fbit;
91*e4b17023SJohn Marino   enum fixed_value_range_code temp;
92*e4b17023SJohn Marino 
93*e4b17023SJohn Marino   f->mode = mode;
94*e4b17023SJohn Marino   fbit = GET_MODE_FBIT (mode);
95*e4b17023SJohn Marino 
96*e4b17023SJohn Marino   real_from_string (&real_value, str);
97*e4b17023SJohn Marino   temp = check_real_for_fixed_mode (&real_value, f->mode);
98*e4b17023SJohn Marino   /* We don't want to warn the case when the _Fract value is 1.0.  */
99*e4b17023SJohn Marino   if (temp == FIXED_UNDERFLOW
100*e4b17023SJohn Marino       || temp == FIXED_GT_MAX_EPS
101*e4b17023SJohn Marino       || (temp == FIXED_MAX_EPS && ALL_ACCUM_MODE_P (f->mode)))
102*e4b17023SJohn Marino     warning (OPT_Woverflow,
103*e4b17023SJohn Marino 	     "large fixed-point constant implicitly truncated to fixed-point type");
104*e4b17023SJohn Marino   real_2expN (&base_value, fbit, mode);
105*e4b17023SJohn Marino   real_arithmetic (&fixed_value, MULT_EXPR, &real_value, &base_value);
106*e4b17023SJohn Marino   real_to_integer2 ((HOST_WIDE_INT *)&f->data.low, &f->data.high,
107*e4b17023SJohn Marino 		    &fixed_value);
108*e4b17023SJohn Marino 
109*e4b17023SJohn Marino   if (temp == FIXED_MAX_EPS && ALL_FRACT_MODE_P (f->mode))
110*e4b17023SJohn Marino     {
111*e4b17023SJohn Marino       /* From the spec, we need to evaluate 1 to the maximal value.  */
112*e4b17023SJohn Marino       f->data.low = -1;
113*e4b17023SJohn Marino       f->data.high = -1;
114*e4b17023SJohn Marino       f->data = double_int_ext (f->data,
115*e4b17023SJohn Marino 				GET_MODE_FBIT (f->mode)
116*e4b17023SJohn Marino 				+ GET_MODE_IBIT (f->mode), 1);
117*e4b17023SJohn Marino     }
118*e4b17023SJohn Marino   else
119*e4b17023SJohn Marino     f->data = double_int_ext (f->data,
120*e4b17023SJohn Marino 			      SIGNED_FIXED_POINT_MODE_P (f->mode)
121*e4b17023SJohn Marino 			      + GET_MODE_FBIT (f->mode)
122*e4b17023SJohn Marino 			      + GET_MODE_IBIT (f->mode),
123*e4b17023SJohn Marino 			      UNSIGNED_FIXED_POINT_MODE_P (f->mode));
124*e4b17023SJohn Marino }
125*e4b17023SJohn Marino 
126*e4b17023SJohn Marino /* Render F as a decimal floating point constant.  */
127*e4b17023SJohn Marino 
128*e4b17023SJohn Marino void
fixed_to_decimal(char * str,const FIXED_VALUE_TYPE * f_orig,size_t buf_size)129*e4b17023SJohn Marino fixed_to_decimal (char *str, const FIXED_VALUE_TYPE *f_orig,
130*e4b17023SJohn Marino 		  size_t buf_size)
131*e4b17023SJohn Marino {
132*e4b17023SJohn Marino   REAL_VALUE_TYPE real_value, base_value, fixed_value;
133*e4b17023SJohn Marino 
134*e4b17023SJohn Marino   real_2expN (&base_value, GET_MODE_FBIT (f_orig->mode), f_orig->mode);
135*e4b17023SJohn Marino   real_from_integer (&real_value, VOIDmode, f_orig->data.low, f_orig->data.high,
136*e4b17023SJohn Marino 		     UNSIGNED_FIXED_POINT_MODE_P (f_orig->mode));
137*e4b17023SJohn Marino   real_arithmetic (&fixed_value, RDIV_EXPR, &real_value, &base_value);
138*e4b17023SJohn Marino   real_to_decimal (str, &fixed_value, buf_size, 0, 1);
139*e4b17023SJohn Marino }
140*e4b17023SJohn Marino 
141*e4b17023SJohn Marino /* If SAT_P, saturate A to the maximum or the minimum, and save to *F based on
142*e4b17023SJohn Marino    the machine mode MODE.
143*e4b17023SJohn Marino    Do not modify *F otherwise.
144*e4b17023SJohn Marino    This function assumes the width of double_int is greater than the width
145*e4b17023SJohn Marino    of the fixed-point value (the sum of a possible sign bit, possible ibits,
146*e4b17023SJohn Marino    and fbits).
147*e4b17023SJohn Marino    Return true, if !SAT_P and overflow.  */
148*e4b17023SJohn Marino 
149*e4b17023SJohn Marino static bool
fixed_saturate1(enum machine_mode mode,double_int a,double_int * f,bool sat_p)150*e4b17023SJohn Marino fixed_saturate1 (enum machine_mode mode, double_int a, double_int *f,
151*e4b17023SJohn Marino 		 bool sat_p)
152*e4b17023SJohn Marino {
153*e4b17023SJohn Marino   bool overflow_p = false;
154*e4b17023SJohn Marino   bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
155*e4b17023SJohn Marino   int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
156*e4b17023SJohn Marino 
157*e4b17023SJohn Marino   if (unsigned_p) /* Unsigned type.  */
158*e4b17023SJohn Marino     {
159*e4b17023SJohn Marino       double_int max;
160*e4b17023SJohn Marino       max.low = -1;
161*e4b17023SJohn Marino       max.high = -1;
162*e4b17023SJohn Marino       max = double_int_ext (max, i_f_bits, 1);
163*e4b17023SJohn Marino       if (double_int_cmp (a, max, 1) == 1)
164*e4b17023SJohn Marino 	{
165*e4b17023SJohn Marino 	  if (sat_p)
166*e4b17023SJohn Marino 	    *f = max;
167*e4b17023SJohn Marino 	  else
168*e4b17023SJohn Marino 	    overflow_p = true;
169*e4b17023SJohn Marino 	}
170*e4b17023SJohn Marino     }
171*e4b17023SJohn Marino   else /* Signed type.  */
172*e4b17023SJohn Marino     {
173*e4b17023SJohn Marino       double_int max, min;
174*e4b17023SJohn Marino       max.high = -1;
175*e4b17023SJohn Marino       max.low = -1;
176*e4b17023SJohn Marino       max = double_int_ext (max, i_f_bits, 1);
177*e4b17023SJohn Marino       min.high = 0;
178*e4b17023SJohn Marino       min.low = 1;
179*e4b17023SJohn Marino       lshift_double (min.low, min.high, i_f_bits,
180*e4b17023SJohn Marino 		     2 * HOST_BITS_PER_WIDE_INT,
181*e4b17023SJohn Marino 		     &min.low, &min.high, 1);
182*e4b17023SJohn Marino       min = double_int_ext (min, 1 + i_f_bits, 0);
183*e4b17023SJohn Marino       if (double_int_cmp (a, max, 0) == 1)
184*e4b17023SJohn Marino 	{
185*e4b17023SJohn Marino 	  if (sat_p)
186*e4b17023SJohn Marino 	    *f = max;
187*e4b17023SJohn Marino 	  else
188*e4b17023SJohn Marino 	    overflow_p = true;
189*e4b17023SJohn Marino 	}
190*e4b17023SJohn Marino       else if (double_int_cmp (a, min, 0) == -1)
191*e4b17023SJohn Marino 	{
192*e4b17023SJohn Marino 	  if (sat_p)
193*e4b17023SJohn Marino 	    *f = min;
194*e4b17023SJohn Marino 	  else
195*e4b17023SJohn Marino 	    overflow_p = true;
196*e4b17023SJohn Marino 	}
197*e4b17023SJohn Marino     }
198*e4b17023SJohn Marino   return overflow_p;
199*e4b17023SJohn Marino }
200*e4b17023SJohn Marino 
201*e4b17023SJohn Marino /* If SAT_P, saturate {A_HIGH, A_LOW} to the maximum or the minimum, and
202*e4b17023SJohn Marino    save to *F based on the machine mode MODE.
203*e4b17023SJohn Marino    Do not modify *F otherwise.
204*e4b17023SJohn Marino    This function assumes the width of two double_int is greater than the width
205*e4b17023SJohn Marino    of the fixed-point value (the sum of a possible sign bit, possible ibits,
206*e4b17023SJohn Marino    and fbits).
207*e4b17023SJohn Marino    Return true, if !SAT_P and overflow.  */
208*e4b17023SJohn Marino 
209*e4b17023SJohn Marino static bool
fixed_saturate2(enum machine_mode mode,double_int a_high,double_int a_low,double_int * f,bool sat_p)210*e4b17023SJohn Marino fixed_saturate2 (enum machine_mode mode, double_int a_high, double_int a_low,
211*e4b17023SJohn Marino 		 double_int *f, bool sat_p)
212*e4b17023SJohn Marino {
213*e4b17023SJohn Marino   bool overflow_p = false;
214*e4b17023SJohn Marino   bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
215*e4b17023SJohn Marino   int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
216*e4b17023SJohn Marino 
217*e4b17023SJohn Marino   if (unsigned_p) /* Unsigned type.  */
218*e4b17023SJohn Marino     {
219*e4b17023SJohn Marino       double_int max_r, max_s;
220*e4b17023SJohn Marino       max_r.high = 0;
221*e4b17023SJohn Marino       max_r.low = 0;
222*e4b17023SJohn Marino       max_s.high = -1;
223*e4b17023SJohn Marino       max_s.low = -1;
224*e4b17023SJohn Marino       max_s = double_int_ext (max_s, i_f_bits, 1);
225*e4b17023SJohn Marino       if (double_int_cmp (a_high, max_r, 1) == 1
226*e4b17023SJohn Marino 	  || (double_int_equal_p (a_high, max_r) &&
227*e4b17023SJohn Marino 	      double_int_cmp (a_low, max_s, 1) == 1))
228*e4b17023SJohn Marino 	{
229*e4b17023SJohn Marino 	  if (sat_p)
230*e4b17023SJohn Marino 	    *f = max_s;
231*e4b17023SJohn Marino 	  else
232*e4b17023SJohn Marino 	    overflow_p = true;
233*e4b17023SJohn Marino 	}
234*e4b17023SJohn Marino     }
235*e4b17023SJohn Marino   else /* Signed type.  */
236*e4b17023SJohn Marino     {
237*e4b17023SJohn Marino       double_int max_r, max_s, min_r, min_s;
238*e4b17023SJohn Marino       max_r.high = 0;
239*e4b17023SJohn Marino       max_r.low = 0;
240*e4b17023SJohn Marino       max_s.high = -1;
241*e4b17023SJohn Marino       max_s.low = -1;
242*e4b17023SJohn Marino       max_s = double_int_ext (max_s, i_f_bits, 1);
243*e4b17023SJohn Marino       min_r.high = -1;
244*e4b17023SJohn Marino       min_r.low = -1;
245*e4b17023SJohn Marino       min_s.high = 0;
246*e4b17023SJohn Marino       min_s.low = 1;
247*e4b17023SJohn Marino       lshift_double (min_s.low, min_s.high, i_f_bits,
248*e4b17023SJohn Marino 		     2 * HOST_BITS_PER_WIDE_INT,
249*e4b17023SJohn Marino 		     &min_s.low, &min_s.high, 1);
250*e4b17023SJohn Marino       min_s = double_int_ext (min_s, 1 + i_f_bits, 0);
251*e4b17023SJohn Marino       if (double_int_cmp (a_high, max_r, 0) == 1
252*e4b17023SJohn Marino 	  || (double_int_equal_p (a_high, max_r) &&
253*e4b17023SJohn Marino 	      double_int_cmp (a_low, max_s, 1) == 1))
254*e4b17023SJohn Marino 	{
255*e4b17023SJohn Marino 	  if (sat_p)
256*e4b17023SJohn Marino 	    *f = max_s;
257*e4b17023SJohn Marino 	  else
258*e4b17023SJohn Marino 	    overflow_p = true;
259*e4b17023SJohn Marino 	}
260*e4b17023SJohn Marino       else if (double_int_cmp (a_high, min_r, 0) == -1
261*e4b17023SJohn Marino 	       || (double_int_equal_p (a_high, min_r) &&
262*e4b17023SJohn Marino 		   double_int_cmp (a_low, min_s, 1) == -1))
263*e4b17023SJohn Marino 	{
264*e4b17023SJohn Marino 	  if (sat_p)
265*e4b17023SJohn Marino 	    *f = min_s;
266*e4b17023SJohn Marino 	  else
267*e4b17023SJohn Marino 	    overflow_p = true;
268*e4b17023SJohn Marino 	}
269*e4b17023SJohn Marino     }
270*e4b17023SJohn Marino   return overflow_p;
271*e4b17023SJohn Marino }
272*e4b17023SJohn Marino 
273*e4b17023SJohn Marino /* Return the sign bit based on I_F_BITS.  */
274*e4b17023SJohn Marino 
275*e4b17023SJohn Marino static inline int
get_fixed_sign_bit(double_int a,int i_f_bits)276*e4b17023SJohn Marino get_fixed_sign_bit (double_int a, int i_f_bits)
277*e4b17023SJohn Marino {
278*e4b17023SJohn Marino   if (i_f_bits < HOST_BITS_PER_WIDE_INT)
279*e4b17023SJohn Marino     return (a.low >> i_f_bits) & 1;
280*e4b17023SJohn Marino   else
281*e4b17023SJohn Marino     return (a.high >> (i_f_bits - HOST_BITS_PER_WIDE_INT)) & 1;
282*e4b17023SJohn Marino }
283*e4b17023SJohn Marino 
284*e4b17023SJohn Marino /* Calculate F = A + (SUBTRACT_P ? -B : B).
285*e4b17023SJohn Marino    If SAT_P, saturate the result to the max or the min.
286*e4b17023SJohn Marino    Return true, if !SAT_P and overflow.  */
287*e4b17023SJohn Marino 
288*e4b17023SJohn Marino static bool
do_fixed_add(FIXED_VALUE_TYPE * f,const FIXED_VALUE_TYPE * a,const FIXED_VALUE_TYPE * b,bool subtract_p,bool sat_p)289*e4b17023SJohn Marino do_fixed_add (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
290*e4b17023SJohn Marino 	      const FIXED_VALUE_TYPE *b, bool subtract_p, bool sat_p)
291*e4b17023SJohn Marino {
292*e4b17023SJohn Marino   bool overflow_p = false;
293*e4b17023SJohn Marino   bool unsigned_p;
294*e4b17023SJohn Marino   double_int temp;
295*e4b17023SJohn Marino   int i_f_bits;
296*e4b17023SJohn Marino 
297*e4b17023SJohn Marino   /* This was a conditional expression but it triggered a bug in
298*e4b17023SJohn Marino      Sun C 5.5.  */
299*e4b17023SJohn Marino   if (subtract_p)
300*e4b17023SJohn Marino     temp = double_int_neg (b->data);
301*e4b17023SJohn Marino   else
302*e4b17023SJohn Marino     temp = b->data;
303*e4b17023SJohn Marino 
304*e4b17023SJohn Marino   unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
305*e4b17023SJohn Marino   i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
306*e4b17023SJohn Marino   f->mode = a->mode;
307*e4b17023SJohn Marino   f->data = double_int_add (a->data, temp);
308*e4b17023SJohn Marino   if (unsigned_p) /* Unsigned type.  */
309*e4b17023SJohn Marino     {
310*e4b17023SJohn Marino       if (subtract_p) /* Unsigned subtraction.  */
311*e4b17023SJohn Marino 	{
312*e4b17023SJohn Marino 	  if (double_int_cmp (a->data, b->data, 1) == -1)
313*e4b17023SJohn Marino 	    {
314*e4b17023SJohn Marino 	      if (sat_p)
315*e4b17023SJohn Marino 		{
316*e4b17023SJohn Marino 		  f->data.high = 0;
317*e4b17023SJohn Marino 		  f->data.low = 0;
318*e4b17023SJohn Marino 		 }
319*e4b17023SJohn Marino 	      else
320*e4b17023SJohn Marino 		overflow_p = true;
321*e4b17023SJohn Marino 	    }
322*e4b17023SJohn Marino 	}
323*e4b17023SJohn Marino       else /* Unsigned addition.  */
324*e4b17023SJohn Marino 	{
325*e4b17023SJohn Marino 	  f->data = double_int_ext (f->data, i_f_bits, 1);
326*e4b17023SJohn Marino 	  if (double_int_cmp (f->data, a->data, 1) == -1
327*e4b17023SJohn Marino 	      || double_int_cmp (f->data, b->data, 1) == -1)
328*e4b17023SJohn Marino 	    {
329*e4b17023SJohn Marino 	      if (sat_p)
330*e4b17023SJohn Marino 		{
331*e4b17023SJohn Marino 		  f->data.high = -1;
332*e4b17023SJohn Marino 		  f->data.low = -1;
333*e4b17023SJohn Marino 		}
334*e4b17023SJohn Marino 	      else
335*e4b17023SJohn Marino 		overflow_p = true;
336*e4b17023SJohn Marino 	    }
337*e4b17023SJohn Marino 	}
338*e4b17023SJohn Marino     }
339*e4b17023SJohn Marino   else /* Signed type.  */
340*e4b17023SJohn Marino     {
341*e4b17023SJohn Marino       if ((!subtract_p
342*e4b17023SJohn Marino 	   && (get_fixed_sign_bit (a->data, i_f_bits)
343*e4b17023SJohn Marino 	       == get_fixed_sign_bit (b->data, i_f_bits))
344*e4b17023SJohn Marino 	   && (get_fixed_sign_bit (a->data, i_f_bits)
345*e4b17023SJohn Marino 	       != get_fixed_sign_bit (f->data, i_f_bits)))
346*e4b17023SJohn Marino 	  || (subtract_p
347*e4b17023SJohn Marino 	      && (get_fixed_sign_bit (a->data, i_f_bits)
348*e4b17023SJohn Marino 		  != get_fixed_sign_bit (b->data, i_f_bits))
349*e4b17023SJohn Marino 	      && (get_fixed_sign_bit (a->data, i_f_bits)
350*e4b17023SJohn Marino 		  != get_fixed_sign_bit (f->data, i_f_bits))))
351*e4b17023SJohn Marino 	{
352*e4b17023SJohn Marino 	  if (sat_p)
353*e4b17023SJohn Marino 	    {
354*e4b17023SJohn Marino 	      f->data.low = 1;
355*e4b17023SJohn Marino 	      f->data.high = 0;
356*e4b17023SJohn Marino 	      lshift_double (f->data.low, f->data.high, i_f_bits,
357*e4b17023SJohn Marino 			     2 * HOST_BITS_PER_WIDE_INT,
358*e4b17023SJohn Marino 			     &f->data.low, &f->data.high, 1);
359*e4b17023SJohn Marino 	      if (get_fixed_sign_bit (a->data, i_f_bits) == 0)
360*e4b17023SJohn Marino 		{
361*e4b17023SJohn Marino 		  double_int one;
362*e4b17023SJohn Marino 		  one.low = 1;
363*e4b17023SJohn Marino 		  one.high = 0;
364*e4b17023SJohn Marino 		  f->data = double_int_sub (f->data, one);
365*e4b17023SJohn Marino 		}
366*e4b17023SJohn Marino 	    }
367*e4b17023SJohn Marino 	  else
368*e4b17023SJohn Marino 	    overflow_p = true;
369*e4b17023SJohn Marino 	}
370*e4b17023SJohn Marino     }
371*e4b17023SJohn Marino   f->data = double_int_ext (f->data, (!unsigned_p) + i_f_bits, unsigned_p);
372*e4b17023SJohn Marino   return overflow_p;
373*e4b17023SJohn Marino }
374*e4b17023SJohn Marino 
375*e4b17023SJohn Marino /* Calculate F = A * B.
376*e4b17023SJohn Marino    If SAT_P, saturate the result to the max or the min.
377*e4b17023SJohn Marino    Return true, if !SAT_P and overflow.  */
378*e4b17023SJohn Marino 
379*e4b17023SJohn Marino static bool
do_fixed_multiply(FIXED_VALUE_TYPE * f,const FIXED_VALUE_TYPE * a,const FIXED_VALUE_TYPE * b,bool sat_p)380*e4b17023SJohn Marino do_fixed_multiply (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
381*e4b17023SJohn Marino 		   const FIXED_VALUE_TYPE *b, bool sat_p)
382*e4b17023SJohn Marino {
383*e4b17023SJohn Marino   bool overflow_p = false;
384*e4b17023SJohn Marino   bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
385*e4b17023SJohn Marino   int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
386*e4b17023SJohn Marino   f->mode = a->mode;
387*e4b17023SJohn Marino   if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT)
388*e4b17023SJohn Marino     {
389*e4b17023SJohn Marino       f->data = double_int_mul (a->data, b->data);
390*e4b17023SJohn Marino       lshift_double (f->data.low, f->data.high,
391*e4b17023SJohn Marino 		     (-GET_MODE_FBIT (f->mode)),
392*e4b17023SJohn Marino 		     2 * HOST_BITS_PER_WIDE_INT,
393*e4b17023SJohn Marino 		     &f->data.low, &f->data.high, !unsigned_p);
394*e4b17023SJohn Marino       overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
395*e4b17023SJohn Marino     }
396*e4b17023SJohn Marino   else
397*e4b17023SJohn Marino     {
398*e4b17023SJohn Marino       /* The result of multiplication expands to two double_int.  */
399*e4b17023SJohn Marino       double_int a_high, a_low, b_high, b_low;
400*e4b17023SJohn Marino       double_int high_high, high_low, low_high, low_low;
401*e4b17023SJohn Marino       double_int r, s, temp1, temp2;
402*e4b17023SJohn Marino       int carry = 0;
403*e4b17023SJohn Marino 
404*e4b17023SJohn Marino       /* Decompose a and b to four double_int.  */
405*e4b17023SJohn Marino       a_high.low = a->data.high;
406*e4b17023SJohn Marino       a_high.high = 0;
407*e4b17023SJohn Marino       a_low.low = a->data.low;
408*e4b17023SJohn Marino       a_low.high = 0;
409*e4b17023SJohn Marino       b_high.low = b->data.high;
410*e4b17023SJohn Marino       b_high.high = 0;
411*e4b17023SJohn Marino       b_low.low = b->data.low;
412*e4b17023SJohn Marino       b_low.high = 0;
413*e4b17023SJohn Marino 
414*e4b17023SJohn Marino       /* Perform four multiplications.  */
415*e4b17023SJohn Marino       low_low = double_int_mul (a_low, b_low);
416*e4b17023SJohn Marino       low_high = double_int_mul (a_low, b_high);
417*e4b17023SJohn Marino       high_low = double_int_mul (a_high, b_low);
418*e4b17023SJohn Marino       high_high = double_int_mul (a_high, b_high);
419*e4b17023SJohn Marino 
420*e4b17023SJohn Marino       /* Accumulate four results to {r, s}.  */
421*e4b17023SJohn Marino       temp1.high = high_low.low;
422*e4b17023SJohn Marino       temp1.low = 0;
423*e4b17023SJohn Marino       s = double_int_add (low_low, temp1);
424*e4b17023SJohn Marino       if (double_int_cmp (s, low_low, 1) == -1
425*e4b17023SJohn Marino 	  || double_int_cmp (s, temp1, 1) == -1)
426*e4b17023SJohn Marino 	carry ++; /* Carry */
427*e4b17023SJohn Marino       temp1.high = s.high;
428*e4b17023SJohn Marino       temp1.low = s.low;
429*e4b17023SJohn Marino       temp2.high = low_high.low;
430*e4b17023SJohn Marino       temp2.low = 0;
431*e4b17023SJohn Marino       s = double_int_add (temp1, temp2);
432*e4b17023SJohn Marino       if (double_int_cmp (s, temp1, 1) == -1
433*e4b17023SJohn Marino 	  || double_int_cmp (s, temp2, 1) == -1)
434*e4b17023SJohn Marino 	carry ++; /* Carry */
435*e4b17023SJohn Marino 
436*e4b17023SJohn Marino       temp1.low = high_low.high;
437*e4b17023SJohn Marino       temp1.high = 0;
438*e4b17023SJohn Marino       r = double_int_add (high_high, temp1);
439*e4b17023SJohn Marino       temp1.low = low_high.high;
440*e4b17023SJohn Marino       temp1.high = 0;
441*e4b17023SJohn Marino       r = double_int_add (r, temp1);
442*e4b17023SJohn Marino       temp1.low = carry;
443*e4b17023SJohn Marino       temp1.high = 0;
444*e4b17023SJohn Marino       r = double_int_add (r, temp1);
445*e4b17023SJohn Marino 
446*e4b17023SJohn Marino       /* We need to subtract b from r, if a < 0.  */
447*e4b17023SJohn Marino       if (!unsigned_p && a->data.high < 0)
448*e4b17023SJohn Marino 	r = double_int_sub (r, b->data);
449*e4b17023SJohn Marino       /* We need to subtract a from r, if b < 0.  */
450*e4b17023SJohn Marino       if (!unsigned_p && b->data.high < 0)
451*e4b17023SJohn Marino 	r = double_int_sub (r, a->data);
452*e4b17023SJohn Marino 
453*e4b17023SJohn Marino       /* Shift right the result by FBIT.  */
454*e4b17023SJohn Marino       if (GET_MODE_FBIT (f->mode) == 2 * HOST_BITS_PER_WIDE_INT)
455*e4b17023SJohn Marino 	{
456*e4b17023SJohn Marino 	  s.low = r.low;
457*e4b17023SJohn Marino 	  s.high = r.high;
458*e4b17023SJohn Marino 	  if (unsigned_p)
459*e4b17023SJohn Marino 	    {
460*e4b17023SJohn Marino 	      r.low = 0;
461*e4b17023SJohn Marino 	      r.high = 0;
462*e4b17023SJohn Marino 	    }
463*e4b17023SJohn Marino 	  else
464*e4b17023SJohn Marino 	    {
465*e4b17023SJohn Marino 	      r.low = -1;
466*e4b17023SJohn Marino 	      r.high = -1;
467*e4b17023SJohn Marino 	    }
468*e4b17023SJohn Marino 	  f->data.low = s.low;
469*e4b17023SJohn Marino 	  f->data.high = s.high;
470*e4b17023SJohn Marino 	}
471*e4b17023SJohn Marino       else
472*e4b17023SJohn Marino 	{
473*e4b17023SJohn Marino 	  lshift_double (s.low, s.high,
474*e4b17023SJohn Marino 			 (-GET_MODE_FBIT (f->mode)),
475*e4b17023SJohn Marino 			 2 * HOST_BITS_PER_WIDE_INT,
476*e4b17023SJohn Marino 			 &s.low, &s.high, 0);
477*e4b17023SJohn Marino 	  lshift_double (r.low, r.high,
478*e4b17023SJohn Marino 			 (2 * HOST_BITS_PER_WIDE_INT
479*e4b17023SJohn Marino 			  - GET_MODE_FBIT (f->mode)),
480*e4b17023SJohn Marino 			 2 * HOST_BITS_PER_WIDE_INT,
481*e4b17023SJohn Marino 			 &f->data.low, &f->data.high, 0);
482*e4b17023SJohn Marino 	  f->data.low = f->data.low | s.low;
483*e4b17023SJohn Marino 	  f->data.high = f->data.high | s.high;
484*e4b17023SJohn Marino 	  s.low = f->data.low;
485*e4b17023SJohn Marino 	  s.high = f->data.high;
486*e4b17023SJohn Marino 	  lshift_double (r.low, r.high,
487*e4b17023SJohn Marino 			 (-GET_MODE_FBIT (f->mode)),
488*e4b17023SJohn Marino 			 2 * HOST_BITS_PER_WIDE_INT,
489*e4b17023SJohn Marino 			 &r.low, &r.high, !unsigned_p);
490*e4b17023SJohn Marino 	}
491*e4b17023SJohn Marino 
492*e4b17023SJohn Marino       overflow_p = fixed_saturate2 (f->mode, r, s, &f->data, sat_p);
493*e4b17023SJohn Marino     }
494*e4b17023SJohn Marino 
495*e4b17023SJohn Marino   f->data = double_int_ext (f->data, (!unsigned_p) + i_f_bits, unsigned_p);
496*e4b17023SJohn Marino   return overflow_p;
497*e4b17023SJohn Marino }
498*e4b17023SJohn Marino 
499*e4b17023SJohn Marino /* Calculate F = A / B.
500*e4b17023SJohn Marino    If SAT_P, saturate the result to the max or the min.
501*e4b17023SJohn Marino    Return true, if !SAT_P and overflow.  */
502*e4b17023SJohn Marino 
503*e4b17023SJohn Marino static bool
do_fixed_divide(FIXED_VALUE_TYPE * f,const FIXED_VALUE_TYPE * a,const FIXED_VALUE_TYPE * b,bool sat_p)504*e4b17023SJohn Marino do_fixed_divide (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
505*e4b17023SJohn Marino 		 const FIXED_VALUE_TYPE *b, bool sat_p)
506*e4b17023SJohn Marino {
507*e4b17023SJohn Marino   bool overflow_p = false;
508*e4b17023SJohn Marino   bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
509*e4b17023SJohn Marino   int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
510*e4b17023SJohn Marino   f->mode = a->mode;
511*e4b17023SJohn Marino   if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT)
512*e4b17023SJohn Marino     {
513*e4b17023SJohn Marino       lshift_double (a->data.low, a->data.high,
514*e4b17023SJohn Marino 		     GET_MODE_FBIT (f->mode),
515*e4b17023SJohn Marino 		     2 * HOST_BITS_PER_WIDE_INT,
516*e4b17023SJohn Marino 		     &f->data.low, &f->data.high, !unsigned_p);
517*e4b17023SJohn Marino       f->data = double_int_div (f->data, b->data, unsigned_p, TRUNC_DIV_EXPR);
518*e4b17023SJohn Marino       overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
519*e4b17023SJohn Marino     }
520*e4b17023SJohn Marino   else
521*e4b17023SJohn Marino     {
522*e4b17023SJohn Marino       double_int pos_a, pos_b, r, s;
523*e4b17023SJohn Marino       double_int quo_r, quo_s, mod, temp;
524*e4b17023SJohn Marino       int num_of_neg = 0;
525*e4b17023SJohn Marino       int i;
526*e4b17023SJohn Marino 
527*e4b17023SJohn Marino       /* If a < 0, negate a.  */
528*e4b17023SJohn Marino       if (!unsigned_p && a->data.high < 0)
529*e4b17023SJohn Marino 	{
530*e4b17023SJohn Marino 	  pos_a = double_int_neg (a->data);
531*e4b17023SJohn Marino 	  num_of_neg ++;
532*e4b17023SJohn Marino 	}
533*e4b17023SJohn Marino       else
534*e4b17023SJohn Marino 	pos_a = a->data;
535*e4b17023SJohn Marino 
536*e4b17023SJohn Marino       /* If b < 0, negate b.  */
537*e4b17023SJohn Marino       if (!unsigned_p && b->data.high < 0)
538*e4b17023SJohn Marino 	{
539*e4b17023SJohn Marino 	  pos_b = double_int_neg (b->data);
540*e4b17023SJohn Marino 	  num_of_neg ++;
541*e4b17023SJohn Marino 	}
542*e4b17023SJohn Marino       else
543*e4b17023SJohn Marino 	pos_b = b->data;
544*e4b17023SJohn Marino 
545*e4b17023SJohn Marino       /* Left shift pos_a to {r, s} by FBIT.  */
546*e4b17023SJohn Marino       if (GET_MODE_FBIT (f->mode) == 2 * HOST_BITS_PER_WIDE_INT)
547*e4b17023SJohn Marino 	{
548*e4b17023SJohn Marino 	  r = pos_a;
549*e4b17023SJohn Marino 	  s.high = 0;
550*e4b17023SJohn Marino 	  s.low = 0;
551*e4b17023SJohn Marino 	}
552*e4b17023SJohn Marino       else
553*e4b17023SJohn Marino  	{
554*e4b17023SJohn Marino 	  lshift_double (pos_a.low, pos_a.high,
555*e4b17023SJohn Marino 			 GET_MODE_FBIT (f->mode),
556*e4b17023SJohn Marino 			 2 * HOST_BITS_PER_WIDE_INT,
557*e4b17023SJohn Marino 			 &s.low, &s.high, 0);
558*e4b17023SJohn Marino 	  lshift_double (pos_a.low, pos_a.high,
559*e4b17023SJohn Marino 			 - (2 * HOST_BITS_PER_WIDE_INT
560*e4b17023SJohn Marino 			    - GET_MODE_FBIT (f->mode)),
561*e4b17023SJohn Marino 			 2 * HOST_BITS_PER_WIDE_INT,
562*e4b17023SJohn Marino 			 &r.low, &r.high, 0);
563*e4b17023SJohn Marino  	}
564*e4b17023SJohn Marino 
565*e4b17023SJohn Marino       /* Divide r by pos_b to quo_r.  The remainder is in mod.  */
566*e4b17023SJohn Marino       div_and_round_double (TRUNC_DIV_EXPR, 1, r.low, r.high, pos_b.low,
567*e4b17023SJohn Marino 			    pos_b.high, &quo_r.low, &quo_r.high, &mod.low,
568*e4b17023SJohn Marino 			    &mod.high);
569*e4b17023SJohn Marino 
570*e4b17023SJohn Marino       quo_s.high = 0;
571*e4b17023SJohn Marino       quo_s.low = 0;
572*e4b17023SJohn Marino 
573*e4b17023SJohn Marino       for (i = 0; i < 2 * HOST_BITS_PER_WIDE_INT; i++)
574*e4b17023SJohn Marino 	{
575*e4b17023SJohn Marino 	  /* Record the leftmost bit of mod.  */
576*e4b17023SJohn Marino 	  int leftmost_mod = (mod.high < 0);
577*e4b17023SJohn Marino 
578*e4b17023SJohn Marino 	  /* Shift left mod by 1 bit.  */
579*e4b17023SJohn Marino 	  lshift_double (mod.low, mod.high, 1, 2 * HOST_BITS_PER_WIDE_INT,
580*e4b17023SJohn Marino 			 &mod.low, &mod.high, 0);
581*e4b17023SJohn Marino 
582*e4b17023SJohn Marino 	  /* Test the leftmost bit of s to add to mod.  */
583*e4b17023SJohn Marino 	  if (s.high < 0)
584*e4b17023SJohn Marino 	    mod.low += 1;
585*e4b17023SJohn Marino 
586*e4b17023SJohn Marino 	  /* Shift left quo_s by 1 bit.  */
587*e4b17023SJohn Marino 	  lshift_double (quo_s.low, quo_s.high, 1, 2 * HOST_BITS_PER_WIDE_INT,
588*e4b17023SJohn Marino 			 &quo_s.low, &quo_s.high, 0);
589*e4b17023SJohn Marino 
590*e4b17023SJohn Marino 	  /* Try to calculate (mod - pos_b).  */
591*e4b17023SJohn Marino 	  temp = double_int_sub (mod, pos_b);
592*e4b17023SJohn Marino 
593*e4b17023SJohn Marino 	  if (leftmost_mod == 1 || double_int_cmp (mod, pos_b, 1) != -1)
594*e4b17023SJohn Marino 	    {
595*e4b17023SJohn Marino 	      quo_s.low += 1;
596*e4b17023SJohn Marino 	      mod = temp;
597*e4b17023SJohn Marino 	    }
598*e4b17023SJohn Marino 
599*e4b17023SJohn Marino 	  /* Shift left s by 1 bit.  */
600*e4b17023SJohn Marino 	  lshift_double (s.low, s.high, 1, 2 * HOST_BITS_PER_WIDE_INT,
601*e4b17023SJohn Marino 			 &s.low, &s.high, 0);
602*e4b17023SJohn Marino 
603*e4b17023SJohn Marino 	}
604*e4b17023SJohn Marino 
605*e4b17023SJohn Marino       if (num_of_neg == 1)
606*e4b17023SJohn Marino 	{
607*e4b17023SJohn Marino 	  quo_s = double_int_neg (quo_s);
608*e4b17023SJohn Marino 	  if (quo_s.high == 0 && quo_s.low == 0)
609*e4b17023SJohn Marino 	    quo_r = double_int_neg (quo_r);
610*e4b17023SJohn Marino 	  else
611*e4b17023SJohn Marino 	    {
612*e4b17023SJohn Marino 	      quo_r.low = ~quo_r.low;
613*e4b17023SJohn Marino 	      quo_r.high = ~quo_r.high;
614*e4b17023SJohn Marino 	    }
615*e4b17023SJohn Marino 	}
616*e4b17023SJohn Marino 
617*e4b17023SJohn Marino       f->data = quo_s;
618*e4b17023SJohn Marino       overflow_p = fixed_saturate2 (f->mode, quo_r, quo_s, &f->data, sat_p);
619*e4b17023SJohn Marino     }
620*e4b17023SJohn Marino 
621*e4b17023SJohn Marino   f->data = double_int_ext (f->data, (!unsigned_p) + i_f_bits, unsigned_p);
622*e4b17023SJohn Marino   return overflow_p;
623*e4b17023SJohn Marino }
624*e4b17023SJohn Marino 
625*e4b17023SJohn Marino /* Calculate F = A << B if LEFT_P.  Otherwise, F = A >> B.
626*e4b17023SJohn Marino    If SAT_P, saturate the result to the max or the min.
627*e4b17023SJohn Marino    Return true, if !SAT_P and overflow.  */
628*e4b17023SJohn Marino 
629*e4b17023SJohn Marino static bool
do_fixed_shift(FIXED_VALUE_TYPE * f,const FIXED_VALUE_TYPE * a,const FIXED_VALUE_TYPE * b,bool left_p,bool sat_p)630*e4b17023SJohn Marino do_fixed_shift (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
631*e4b17023SJohn Marino 	      const FIXED_VALUE_TYPE *b, bool left_p, bool sat_p)
632*e4b17023SJohn Marino {
633*e4b17023SJohn Marino   bool overflow_p = false;
634*e4b17023SJohn Marino   bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
635*e4b17023SJohn Marino   int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
636*e4b17023SJohn Marino   f->mode = a->mode;
637*e4b17023SJohn Marino 
638*e4b17023SJohn Marino   if (b->data.low == 0)
639*e4b17023SJohn Marino     {
640*e4b17023SJohn Marino       f->data = a->data;
641*e4b17023SJohn Marino       return overflow_p;
642*e4b17023SJohn Marino     }
643*e4b17023SJohn Marino 
644*e4b17023SJohn Marino   if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT || (!left_p))
645*e4b17023SJohn Marino     {
646*e4b17023SJohn Marino       lshift_double (a->data.low, a->data.high,
647*e4b17023SJohn Marino 		     left_p ? b->data.low : (-b->data.low),
648*e4b17023SJohn Marino 		     2 * HOST_BITS_PER_WIDE_INT,
649*e4b17023SJohn Marino 		     &f->data.low, &f->data.high, !unsigned_p);
650*e4b17023SJohn Marino       if (left_p) /* Only left shift saturates.  */
651*e4b17023SJohn Marino 	overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
652*e4b17023SJohn Marino     }
653*e4b17023SJohn Marino   else /* We need two double_int to store the left-shift result.  */
654*e4b17023SJohn Marino     {
655*e4b17023SJohn Marino       double_int temp_high, temp_low;
656*e4b17023SJohn Marino       if (b->data.low == 2 * HOST_BITS_PER_WIDE_INT)
657*e4b17023SJohn Marino 	{
658*e4b17023SJohn Marino 	  temp_high = a->data;
659*e4b17023SJohn Marino 	  temp_low.high = 0;
660*e4b17023SJohn Marino 	  temp_low.low = 0;
661*e4b17023SJohn Marino 	}
662*e4b17023SJohn Marino       else
663*e4b17023SJohn Marino 	{
664*e4b17023SJohn Marino 	  lshift_double (a->data.low, a->data.high,
665*e4b17023SJohn Marino 			 b->data.low,
666*e4b17023SJohn Marino 			 2 * HOST_BITS_PER_WIDE_INT,
667*e4b17023SJohn Marino 			 &temp_low.low, &temp_low.high, !unsigned_p);
668*e4b17023SJohn Marino 	  /* Logical shift right to temp_high.  */
669*e4b17023SJohn Marino 	  lshift_double (a->data.low, a->data.high,
670*e4b17023SJohn Marino 			 b->data.low - 2 * HOST_BITS_PER_WIDE_INT,
671*e4b17023SJohn Marino 			 2 * HOST_BITS_PER_WIDE_INT,
672*e4b17023SJohn Marino 			 &temp_high.low, &temp_high.high, 0);
673*e4b17023SJohn Marino 	}
674*e4b17023SJohn Marino       if (!unsigned_p && a->data.high < 0) /* Signed-extend temp_high.  */
675*e4b17023SJohn Marino 	temp_high = double_int_ext (temp_high, b->data.low, unsigned_p);
676*e4b17023SJohn Marino       f->data = temp_low;
677*e4b17023SJohn Marino       overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
678*e4b17023SJohn Marino 				    sat_p);
679*e4b17023SJohn Marino     }
680*e4b17023SJohn Marino   f->data = double_int_ext (f->data, (!unsigned_p) + i_f_bits, unsigned_p);
681*e4b17023SJohn Marino   return overflow_p;
682*e4b17023SJohn Marino }
683*e4b17023SJohn Marino 
684*e4b17023SJohn Marino /* Calculate F = -A.
685*e4b17023SJohn Marino    If SAT_P, saturate the result to the max or the min.
686*e4b17023SJohn Marino    Return true, if !SAT_P and overflow.  */
687*e4b17023SJohn Marino 
688*e4b17023SJohn Marino static bool
do_fixed_neg(FIXED_VALUE_TYPE * f,const FIXED_VALUE_TYPE * a,bool sat_p)689*e4b17023SJohn Marino do_fixed_neg (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a, bool sat_p)
690*e4b17023SJohn Marino {
691*e4b17023SJohn Marino   bool overflow_p = false;
692*e4b17023SJohn Marino   bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
693*e4b17023SJohn Marino   int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
694*e4b17023SJohn Marino   f->mode = a->mode;
695*e4b17023SJohn Marino   f->data = double_int_neg (a->data);
696*e4b17023SJohn Marino   f->data = double_int_ext (f->data, (!unsigned_p) + i_f_bits, unsigned_p);
697*e4b17023SJohn Marino 
698*e4b17023SJohn Marino   if (unsigned_p) /* Unsigned type.  */
699*e4b17023SJohn Marino     {
700*e4b17023SJohn Marino       if (f->data.low != 0 || f->data.high != 0)
701*e4b17023SJohn Marino 	{
702*e4b17023SJohn Marino 	  if (sat_p)
703*e4b17023SJohn Marino 	    {
704*e4b17023SJohn Marino 	      f->data.low = 0;
705*e4b17023SJohn Marino 	      f->data.high = 0;
706*e4b17023SJohn Marino 	    }
707*e4b17023SJohn Marino 	  else
708*e4b17023SJohn Marino 	    overflow_p = true;
709*e4b17023SJohn Marino 	}
710*e4b17023SJohn Marino     }
711*e4b17023SJohn Marino   else /* Signed type.  */
712*e4b17023SJohn Marino     {
713*e4b17023SJohn Marino       if (!(f->data.high == 0 && f->data.low == 0)
714*e4b17023SJohn Marino 	  && f->data.high == a->data.high && f->data.low == a->data.low )
715*e4b17023SJohn Marino 	{
716*e4b17023SJohn Marino 	  if (sat_p)
717*e4b17023SJohn Marino 	    {
718*e4b17023SJohn Marino 	      /* Saturate to the maximum by subtracting f->data by one.  */
719*e4b17023SJohn Marino 	      f->data.low = -1;
720*e4b17023SJohn Marino 	      f->data.high = -1;
721*e4b17023SJohn Marino 	      f->data = double_int_ext (f->data, i_f_bits, 1);
722*e4b17023SJohn Marino 	    }
723*e4b17023SJohn Marino 	  else
724*e4b17023SJohn Marino 	    overflow_p = true;
725*e4b17023SJohn Marino 	}
726*e4b17023SJohn Marino     }
727*e4b17023SJohn Marino   return overflow_p;
728*e4b17023SJohn Marino }
729*e4b17023SJohn Marino 
730*e4b17023SJohn Marino /* Perform the binary or unary operation described by CODE.
731*e4b17023SJohn Marino    Note that OP0 and OP1 must have the same mode for binary operators.
732*e4b17023SJohn Marino    For a unary operation, leave OP1 NULL.
733*e4b17023SJohn Marino    Return true, if !SAT_P and overflow.  */
734*e4b17023SJohn Marino 
735*e4b17023SJohn Marino bool
fixed_arithmetic(FIXED_VALUE_TYPE * f,int icode,const FIXED_VALUE_TYPE * op0,const FIXED_VALUE_TYPE * op1,bool sat_p)736*e4b17023SJohn Marino fixed_arithmetic (FIXED_VALUE_TYPE *f, int icode, const FIXED_VALUE_TYPE *op0,
737*e4b17023SJohn Marino 		  const FIXED_VALUE_TYPE *op1, bool sat_p)
738*e4b17023SJohn Marino {
739*e4b17023SJohn Marino   switch (icode)
740*e4b17023SJohn Marino     {
741*e4b17023SJohn Marino     case NEGATE_EXPR:
742*e4b17023SJohn Marino       return do_fixed_neg (f, op0, sat_p);
743*e4b17023SJohn Marino       break;
744*e4b17023SJohn Marino 
745*e4b17023SJohn Marino     case PLUS_EXPR:
746*e4b17023SJohn Marino       gcc_assert (op0->mode == op1->mode);
747*e4b17023SJohn Marino       return do_fixed_add (f, op0, op1, false, sat_p);
748*e4b17023SJohn Marino       break;
749*e4b17023SJohn Marino 
750*e4b17023SJohn Marino     case MINUS_EXPR:
751*e4b17023SJohn Marino       gcc_assert (op0->mode == op1->mode);
752*e4b17023SJohn Marino       return do_fixed_add (f, op0, op1, true, sat_p);
753*e4b17023SJohn Marino       break;
754*e4b17023SJohn Marino 
755*e4b17023SJohn Marino     case MULT_EXPR:
756*e4b17023SJohn Marino       gcc_assert (op0->mode == op1->mode);
757*e4b17023SJohn Marino       return do_fixed_multiply (f, op0, op1, sat_p);
758*e4b17023SJohn Marino       break;
759*e4b17023SJohn Marino 
760*e4b17023SJohn Marino     case TRUNC_DIV_EXPR:
761*e4b17023SJohn Marino       gcc_assert (op0->mode == op1->mode);
762*e4b17023SJohn Marino       return do_fixed_divide (f, op0, op1, sat_p);
763*e4b17023SJohn Marino       break;
764*e4b17023SJohn Marino 
765*e4b17023SJohn Marino     case LSHIFT_EXPR:
766*e4b17023SJohn Marino       return do_fixed_shift (f, op0, op1, true, sat_p);
767*e4b17023SJohn Marino       break;
768*e4b17023SJohn Marino 
769*e4b17023SJohn Marino     case RSHIFT_EXPR:
770*e4b17023SJohn Marino       return do_fixed_shift (f, op0, op1, false, sat_p);
771*e4b17023SJohn Marino       break;
772*e4b17023SJohn Marino 
773*e4b17023SJohn Marino     default:
774*e4b17023SJohn Marino       gcc_unreachable ();
775*e4b17023SJohn Marino     }
776*e4b17023SJohn Marino   return false;
777*e4b17023SJohn Marino }
778*e4b17023SJohn Marino 
779*e4b17023SJohn Marino /* Compare fixed-point values by tree_code.
780*e4b17023SJohn Marino    Note that OP0 and OP1 must have the same mode.  */
781*e4b17023SJohn Marino 
782*e4b17023SJohn Marino bool
fixed_compare(int icode,const FIXED_VALUE_TYPE * op0,const FIXED_VALUE_TYPE * op1)783*e4b17023SJohn Marino fixed_compare (int icode, const FIXED_VALUE_TYPE *op0,
784*e4b17023SJohn Marino 	       const FIXED_VALUE_TYPE *op1)
785*e4b17023SJohn Marino {
786*e4b17023SJohn Marino   enum tree_code code = (enum tree_code) icode;
787*e4b17023SJohn Marino   gcc_assert (op0->mode == op1->mode);
788*e4b17023SJohn Marino 
789*e4b17023SJohn Marino   switch (code)
790*e4b17023SJohn Marino     {
791*e4b17023SJohn Marino     case NE_EXPR:
792*e4b17023SJohn Marino       return !double_int_equal_p (op0->data, op1->data);
793*e4b17023SJohn Marino 
794*e4b17023SJohn Marino     case EQ_EXPR:
795*e4b17023SJohn Marino       return double_int_equal_p (op0->data, op1->data);
796*e4b17023SJohn Marino 
797*e4b17023SJohn Marino     case LT_EXPR:
798*e4b17023SJohn Marino       return double_int_cmp (op0->data, op1->data,
799*e4b17023SJohn Marino 			     UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) == -1;
800*e4b17023SJohn Marino 
801*e4b17023SJohn Marino     case LE_EXPR:
802*e4b17023SJohn Marino       return double_int_cmp (op0->data, op1->data,
803*e4b17023SJohn Marino 			     UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) != 1;
804*e4b17023SJohn Marino 
805*e4b17023SJohn Marino     case GT_EXPR:
806*e4b17023SJohn Marino       return double_int_cmp (op0->data, op1->data,
807*e4b17023SJohn Marino 			     UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) == 1;
808*e4b17023SJohn Marino 
809*e4b17023SJohn Marino     case GE_EXPR:
810*e4b17023SJohn Marino       return double_int_cmp (op0->data, op1->data,
811*e4b17023SJohn Marino 			     UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) != -1;
812*e4b17023SJohn Marino 
813*e4b17023SJohn Marino     default:
814*e4b17023SJohn Marino       gcc_unreachable ();
815*e4b17023SJohn Marino     }
816*e4b17023SJohn Marino }
817*e4b17023SJohn Marino 
818*e4b17023SJohn Marino /* Extend or truncate to a new mode.
819*e4b17023SJohn Marino    If SAT_P, saturate the result to the max or the min.
820*e4b17023SJohn Marino    Return true, if !SAT_P and overflow.  */
821*e4b17023SJohn Marino 
822*e4b17023SJohn Marino bool
fixed_convert(FIXED_VALUE_TYPE * f,enum machine_mode mode,const FIXED_VALUE_TYPE * a,bool sat_p)823*e4b17023SJohn Marino fixed_convert (FIXED_VALUE_TYPE *f, enum machine_mode mode,
824*e4b17023SJohn Marino                const FIXED_VALUE_TYPE *a, bool sat_p)
825*e4b17023SJohn Marino {
826*e4b17023SJohn Marino   bool overflow_p = false;
827*e4b17023SJohn Marino   if (mode == a->mode)
828*e4b17023SJohn Marino     {
829*e4b17023SJohn Marino       *f = *a;
830*e4b17023SJohn Marino       return overflow_p;
831*e4b17023SJohn Marino     }
832*e4b17023SJohn Marino 
833*e4b17023SJohn Marino   if (GET_MODE_FBIT (mode) > GET_MODE_FBIT (a->mode))
834*e4b17023SJohn Marino     {
835*e4b17023SJohn Marino       /* Left shift a to temp_high, temp_low based on a->mode.  */
836*e4b17023SJohn Marino       double_int temp_high, temp_low;
837*e4b17023SJohn Marino       int amount = GET_MODE_FBIT (mode) - GET_MODE_FBIT (a->mode);
838*e4b17023SJohn Marino       lshift_double (a->data.low, a->data.high,
839*e4b17023SJohn Marino 		     amount,
840*e4b17023SJohn Marino 		     2 * HOST_BITS_PER_WIDE_INT,
841*e4b17023SJohn Marino 		     &temp_low.low, &temp_low.high,
842*e4b17023SJohn Marino 		     SIGNED_FIXED_POINT_MODE_P (a->mode));
843*e4b17023SJohn Marino       /* Logical shift right to temp_high.  */
844*e4b17023SJohn Marino       lshift_double (a->data.low, a->data.high,
845*e4b17023SJohn Marino 		     amount - 2 * HOST_BITS_PER_WIDE_INT,
846*e4b17023SJohn Marino 		     2 * HOST_BITS_PER_WIDE_INT,
847*e4b17023SJohn Marino 		     &temp_high.low, &temp_high.high, 0);
848*e4b17023SJohn Marino       if (SIGNED_FIXED_POINT_MODE_P (a->mode)
849*e4b17023SJohn Marino 	  && a->data.high < 0) /* Signed-extend temp_high.  */
850*e4b17023SJohn Marino 	temp_high = double_int_ext (temp_high, amount, 0);
851*e4b17023SJohn Marino       f->mode = mode;
852*e4b17023SJohn Marino       f->data = temp_low;
853*e4b17023SJohn Marino       if (SIGNED_FIXED_POINT_MODE_P (a->mode) ==
854*e4b17023SJohn Marino 	  SIGNED_FIXED_POINT_MODE_P (f->mode))
855*e4b17023SJohn Marino 	overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
856*e4b17023SJohn Marino 				      sat_p);
857*e4b17023SJohn Marino       else
858*e4b17023SJohn Marino 	{
859*e4b17023SJohn Marino 	  /* Take care of the cases when converting between signed and
860*e4b17023SJohn Marino 	     unsigned.  */
861*e4b17023SJohn Marino 	  if (SIGNED_FIXED_POINT_MODE_P (a->mode))
862*e4b17023SJohn Marino 	    {
863*e4b17023SJohn Marino 	      /* Signed -> Unsigned.  */
864*e4b17023SJohn Marino 	      if (a->data.high < 0)
865*e4b17023SJohn Marino 		{
866*e4b17023SJohn Marino 		  if (sat_p)
867*e4b17023SJohn Marino 		    {
868*e4b17023SJohn Marino 		      f->data.low = 0;  /* Set to zero.  */
869*e4b17023SJohn Marino 		      f->data.high = 0;  /* Set to zero.  */
870*e4b17023SJohn Marino 		    }
871*e4b17023SJohn Marino 		  else
872*e4b17023SJohn Marino 		    overflow_p = true;
873*e4b17023SJohn Marino 		}
874*e4b17023SJohn Marino 	      else
875*e4b17023SJohn Marino 		overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
876*e4b17023SJohn Marino 					      &f->data, sat_p);
877*e4b17023SJohn Marino 	    }
878*e4b17023SJohn Marino 	  else
879*e4b17023SJohn Marino 	    {
880*e4b17023SJohn Marino 	      /* Unsigned -> Signed.  */
881*e4b17023SJohn Marino 	      if (temp_high.high < 0)
882*e4b17023SJohn Marino 		{
883*e4b17023SJohn Marino 		  if (sat_p)
884*e4b17023SJohn Marino 		    {
885*e4b17023SJohn Marino 		      /* Set to maximum.  */
886*e4b17023SJohn Marino 		      f->data.low = -1;  /* Set to all ones.  */
887*e4b17023SJohn Marino 		      f->data.high = -1;  /* Set to all ones.  */
888*e4b17023SJohn Marino 		      f->data = double_int_ext (f->data,
889*e4b17023SJohn Marino 						GET_MODE_FBIT (f->mode)
890*e4b17023SJohn Marino 						+ GET_MODE_IBIT (f->mode),
891*e4b17023SJohn Marino 						1); /* Clear the sign.  */
892*e4b17023SJohn Marino 		    }
893*e4b17023SJohn Marino 		  else
894*e4b17023SJohn Marino 		    overflow_p = true;
895*e4b17023SJohn Marino 		}
896*e4b17023SJohn Marino 	      else
897*e4b17023SJohn Marino 		overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
898*e4b17023SJohn Marino 					      &f->data, sat_p);
899*e4b17023SJohn Marino 	    }
900*e4b17023SJohn Marino 	}
901*e4b17023SJohn Marino     }
902*e4b17023SJohn Marino   else
903*e4b17023SJohn Marino     {
904*e4b17023SJohn Marino       /* Right shift a to temp based on a->mode.  */
905*e4b17023SJohn Marino       double_int temp;
906*e4b17023SJohn Marino       lshift_double (a->data.low, a->data.high,
907*e4b17023SJohn Marino 		     GET_MODE_FBIT (mode) - GET_MODE_FBIT (a->mode),
908*e4b17023SJohn Marino 		     2 * HOST_BITS_PER_WIDE_INT,
909*e4b17023SJohn Marino 		     &temp.low, &temp.high,
910*e4b17023SJohn Marino 		     SIGNED_FIXED_POINT_MODE_P (a->mode));
911*e4b17023SJohn Marino       f->mode = mode;
912*e4b17023SJohn Marino       f->data = temp;
913*e4b17023SJohn Marino       if (SIGNED_FIXED_POINT_MODE_P (a->mode) ==
914*e4b17023SJohn Marino 	  SIGNED_FIXED_POINT_MODE_P (f->mode))
915*e4b17023SJohn Marino 	overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
916*e4b17023SJohn Marino       else
917*e4b17023SJohn Marino 	{
918*e4b17023SJohn Marino 	  /* Take care of the cases when converting between signed and
919*e4b17023SJohn Marino 	     unsigned.  */
920*e4b17023SJohn Marino 	  if (SIGNED_FIXED_POINT_MODE_P (a->mode))
921*e4b17023SJohn Marino 	    {
922*e4b17023SJohn Marino 	      /* Signed -> Unsigned.  */
923*e4b17023SJohn Marino 	      if (a->data.high < 0)
924*e4b17023SJohn Marino 		{
925*e4b17023SJohn Marino 		  if (sat_p)
926*e4b17023SJohn Marino 		    {
927*e4b17023SJohn Marino 		      f->data.low = 0;  /* Set to zero.  */
928*e4b17023SJohn Marino 		      f->data.high = 0;  /* Set to zero.  */
929*e4b17023SJohn Marino 		    }
930*e4b17023SJohn Marino 		  else
931*e4b17023SJohn Marino 		    overflow_p = true;
932*e4b17023SJohn Marino 		}
933*e4b17023SJohn Marino 	      else
934*e4b17023SJohn Marino 		overflow_p = fixed_saturate1 (f->mode, f->data, &f->data,
935*e4b17023SJohn Marino 					      sat_p);
936*e4b17023SJohn Marino 	    }
937*e4b17023SJohn Marino 	  else
938*e4b17023SJohn Marino 	    {
939*e4b17023SJohn Marino 	      /* Unsigned -> Signed.  */
940*e4b17023SJohn Marino 	      if (temp.high < 0)
941*e4b17023SJohn Marino 		{
942*e4b17023SJohn Marino 		  if (sat_p)
943*e4b17023SJohn Marino 		    {
944*e4b17023SJohn Marino 		      /* Set to maximum.  */
945*e4b17023SJohn Marino 		      f->data.low = -1;  /* Set to all ones.  */
946*e4b17023SJohn Marino 		      f->data.high = -1;  /* Set to all ones.  */
947*e4b17023SJohn Marino 		      f->data = double_int_ext (f->data,
948*e4b17023SJohn Marino 						GET_MODE_FBIT (f->mode)
949*e4b17023SJohn Marino 						+ GET_MODE_IBIT (f->mode),
950*e4b17023SJohn Marino 						1); /* Clear the sign.  */
951*e4b17023SJohn Marino 		    }
952*e4b17023SJohn Marino 		  else
953*e4b17023SJohn Marino 		    overflow_p = true;
954*e4b17023SJohn Marino 		}
955*e4b17023SJohn Marino 	      else
956*e4b17023SJohn Marino 		overflow_p = fixed_saturate1 (f->mode, f->data, &f->data,
957*e4b17023SJohn Marino 					      sat_p);
958*e4b17023SJohn Marino 	    }
959*e4b17023SJohn Marino 	}
960*e4b17023SJohn Marino     }
961*e4b17023SJohn Marino 
962*e4b17023SJohn Marino   f->data = double_int_ext (f->data,
963*e4b17023SJohn Marino 			    SIGNED_FIXED_POINT_MODE_P (f->mode)
964*e4b17023SJohn Marino 			    + GET_MODE_FBIT (f->mode)
965*e4b17023SJohn Marino 			    + GET_MODE_IBIT (f->mode),
966*e4b17023SJohn Marino 			    UNSIGNED_FIXED_POINT_MODE_P (f->mode));
967*e4b17023SJohn Marino   return overflow_p;
968*e4b17023SJohn Marino }
969*e4b17023SJohn Marino 
970*e4b17023SJohn Marino /* Convert to a new fixed-point mode from an integer.
971*e4b17023SJohn Marino    If UNSIGNED_P, this integer is unsigned.
972*e4b17023SJohn Marino    If SAT_P, saturate the result to the max or the min.
973*e4b17023SJohn Marino    Return true, if !SAT_P and overflow.  */
974*e4b17023SJohn Marino 
975*e4b17023SJohn Marino bool
fixed_convert_from_int(FIXED_VALUE_TYPE * f,enum machine_mode mode,double_int a,bool unsigned_p,bool sat_p)976*e4b17023SJohn Marino fixed_convert_from_int (FIXED_VALUE_TYPE *f, enum machine_mode mode,
977*e4b17023SJohn Marino 			double_int a, bool unsigned_p, bool sat_p)
978*e4b17023SJohn Marino {
979*e4b17023SJohn Marino   bool overflow_p = false;
980*e4b17023SJohn Marino   /* Left shift a to temp_high, temp_low.  */
981*e4b17023SJohn Marino   double_int temp_high, temp_low;
982*e4b17023SJohn Marino   int amount = GET_MODE_FBIT (mode);
983*e4b17023SJohn Marino   if (amount == 2 * HOST_BITS_PER_WIDE_INT)
984*e4b17023SJohn Marino     {
985*e4b17023SJohn Marino        temp_high = a;
986*e4b17023SJohn Marino        temp_low.low = 0;
987*e4b17023SJohn Marino        temp_low.high = 0;
988*e4b17023SJohn Marino     }
989*e4b17023SJohn Marino   else
990*e4b17023SJohn Marino     {
991*e4b17023SJohn Marino       lshift_double (a.low, a.high,
992*e4b17023SJohn Marino 		     amount,
993*e4b17023SJohn Marino 		     2 * HOST_BITS_PER_WIDE_INT,
994*e4b17023SJohn Marino 		     &temp_low.low, &temp_low.high, 0);
995*e4b17023SJohn Marino 
996*e4b17023SJohn Marino       /* Logical shift right to temp_high.  */
997*e4b17023SJohn Marino       lshift_double (a.low, a.high,
998*e4b17023SJohn Marino 		     amount - 2 * HOST_BITS_PER_WIDE_INT,
999*e4b17023SJohn Marino 		     2 * HOST_BITS_PER_WIDE_INT,
1000*e4b17023SJohn Marino 		     &temp_high.low, &temp_high.high, 0);
1001*e4b17023SJohn Marino     }
1002*e4b17023SJohn Marino   if (!unsigned_p && a.high < 0) /* Signed-extend temp_high.  */
1003*e4b17023SJohn Marino     temp_high = double_int_ext (temp_high, amount, 0);
1004*e4b17023SJohn Marino 
1005*e4b17023SJohn Marino   f->mode = mode;
1006*e4b17023SJohn Marino   f->data = temp_low;
1007*e4b17023SJohn Marino 
1008*e4b17023SJohn Marino   if (unsigned_p == UNSIGNED_FIXED_POINT_MODE_P (f->mode))
1009*e4b17023SJohn Marino     overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
1010*e4b17023SJohn Marino 				  sat_p);
1011*e4b17023SJohn Marino   else
1012*e4b17023SJohn Marino     {
1013*e4b17023SJohn Marino       /* Take care of the cases when converting between signed and unsigned.  */
1014*e4b17023SJohn Marino       if (!unsigned_p)
1015*e4b17023SJohn Marino 	{
1016*e4b17023SJohn Marino 	  /* Signed -> Unsigned.  */
1017*e4b17023SJohn Marino 	  if (a.high < 0)
1018*e4b17023SJohn Marino 	    {
1019*e4b17023SJohn Marino 	      if (sat_p)
1020*e4b17023SJohn Marino 		{
1021*e4b17023SJohn Marino 		  f->data.low = 0;  /* Set to zero.  */
1022*e4b17023SJohn Marino 		  f->data.high = 0;  /* Set to zero.  */
1023*e4b17023SJohn Marino 		}
1024*e4b17023SJohn Marino 	      else
1025*e4b17023SJohn Marino 		overflow_p = true;
1026*e4b17023SJohn Marino 	    }
1027*e4b17023SJohn Marino 	  else
1028*e4b17023SJohn Marino 	    overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
1029*e4b17023SJohn Marino 					  &f->data, sat_p);
1030*e4b17023SJohn Marino 	}
1031*e4b17023SJohn Marino       else
1032*e4b17023SJohn Marino 	{
1033*e4b17023SJohn Marino 	  /* Unsigned -> Signed.  */
1034*e4b17023SJohn Marino 	  if (temp_high.high < 0)
1035*e4b17023SJohn Marino 	    {
1036*e4b17023SJohn Marino 	      if (sat_p)
1037*e4b17023SJohn Marino 		{
1038*e4b17023SJohn Marino 		  /* Set to maximum.  */
1039*e4b17023SJohn Marino 		  f->data.low = -1;  /* Set to all ones.  */
1040*e4b17023SJohn Marino 		  f->data.high = -1;  /* Set to all ones.  */
1041*e4b17023SJohn Marino 		  f->data = double_int_ext (f->data,
1042*e4b17023SJohn Marino 					    GET_MODE_FBIT (f->mode)
1043*e4b17023SJohn Marino 					    + GET_MODE_IBIT (f->mode),
1044*e4b17023SJohn Marino 					    1); /* Clear the sign.  */
1045*e4b17023SJohn Marino 		}
1046*e4b17023SJohn Marino 	      else
1047*e4b17023SJohn Marino 		overflow_p = true;
1048*e4b17023SJohn Marino 	    }
1049*e4b17023SJohn Marino 	  else
1050*e4b17023SJohn Marino 	    overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
1051*e4b17023SJohn Marino 					  &f->data, sat_p);
1052*e4b17023SJohn Marino 	}
1053*e4b17023SJohn Marino     }
1054*e4b17023SJohn Marino   f->data = double_int_ext (f->data,
1055*e4b17023SJohn Marino 			    SIGNED_FIXED_POINT_MODE_P (f->mode)
1056*e4b17023SJohn Marino 			    + GET_MODE_FBIT (f->mode)
1057*e4b17023SJohn Marino 			    + GET_MODE_IBIT (f->mode),
1058*e4b17023SJohn Marino 			    UNSIGNED_FIXED_POINT_MODE_P (f->mode));
1059*e4b17023SJohn Marino   return overflow_p;
1060*e4b17023SJohn Marino }
1061*e4b17023SJohn Marino 
1062*e4b17023SJohn Marino /* Convert to a new fixed-point mode from a real.
1063*e4b17023SJohn Marino    If SAT_P, saturate the result to the max or the min.
1064*e4b17023SJohn Marino    Return true, if !SAT_P and overflow.  */
1065*e4b17023SJohn Marino 
1066*e4b17023SJohn Marino bool
fixed_convert_from_real(FIXED_VALUE_TYPE * f,enum machine_mode mode,const REAL_VALUE_TYPE * a,bool sat_p)1067*e4b17023SJohn Marino fixed_convert_from_real (FIXED_VALUE_TYPE *f, enum machine_mode mode,
1068*e4b17023SJohn Marino 			 const REAL_VALUE_TYPE *a, bool sat_p)
1069*e4b17023SJohn Marino {
1070*e4b17023SJohn Marino   bool overflow_p = false;
1071*e4b17023SJohn Marino   REAL_VALUE_TYPE real_value, fixed_value, base_value;
1072*e4b17023SJohn Marino   bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
1073*e4b17023SJohn Marino   int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
1074*e4b17023SJohn Marino   unsigned int fbit = GET_MODE_FBIT (mode);
1075*e4b17023SJohn Marino   enum fixed_value_range_code temp;
1076*e4b17023SJohn Marino 
1077*e4b17023SJohn Marino   real_value = *a;
1078*e4b17023SJohn Marino   f->mode = mode;
1079*e4b17023SJohn Marino   real_2expN (&base_value, fbit, mode);
1080*e4b17023SJohn Marino   real_arithmetic (&fixed_value, MULT_EXPR, &real_value, &base_value);
1081*e4b17023SJohn Marino   real_to_integer2 ((HOST_WIDE_INT *)&f->data.low, &f->data.high, &fixed_value);
1082*e4b17023SJohn Marino   temp = check_real_for_fixed_mode (&real_value, mode);
1083*e4b17023SJohn Marino   if (temp == FIXED_UNDERFLOW) /* Minimum.  */
1084*e4b17023SJohn Marino     {
1085*e4b17023SJohn Marino       if (sat_p)
1086*e4b17023SJohn Marino 	{
1087*e4b17023SJohn Marino 	  if (unsigned_p)
1088*e4b17023SJohn Marino 	    {
1089*e4b17023SJohn Marino 	      f->data.low = 0;
1090*e4b17023SJohn Marino 	      f->data.high = 0;
1091*e4b17023SJohn Marino 	    }
1092*e4b17023SJohn Marino 	  else
1093*e4b17023SJohn Marino 	    {
1094*e4b17023SJohn Marino 	      f->data.low = 1;
1095*e4b17023SJohn Marino 	      f->data.high = 0;
1096*e4b17023SJohn Marino 	      lshift_double (f->data.low, f->data.high, i_f_bits,
1097*e4b17023SJohn Marino 			     2 * HOST_BITS_PER_WIDE_INT,
1098*e4b17023SJohn Marino 			     &f->data.low, &f->data.high, 1);
1099*e4b17023SJohn Marino 	      f->data = double_int_ext (f->data, 1 + i_f_bits, 0);
1100*e4b17023SJohn Marino 	    }
1101*e4b17023SJohn Marino 	}
1102*e4b17023SJohn Marino       else
1103*e4b17023SJohn Marino 	overflow_p = true;
1104*e4b17023SJohn Marino     }
1105*e4b17023SJohn Marino   else if (temp == FIXED_GT_MAX_EPS || temp == FIXED_MAX_EPS) /* Maximum.  */
1106*e4b17023SJohn Marino     {
1107*e4b17023SJohn Marino       if (sat_p)
1108*e4b17023SJohn Marino 	{
1109*e4b17023SJohn Marino 	  f->data.low = -1;
1110*e4b17023SJohn Marino 	  f->data.high = -1;
1111*e4b17023SJohn Marino 	  f->data = double_int_ext (f->data, i_f_bits, 1);
1112*e4b17023SJohn Marino 	}
1113*e4b17023SJohn Marino       else
1114*e4b17023SJohn Marino 	overflow_p = true;
1115*e4b17023SJohn Marino     }
1116*e4b17023SJohn Marino   f->data = double_int_ext (f->data, (!unsigned_p) + i_f_bits, unsigned_p);
1117*e4b17023SJohn Marino   return overflow_p;
1118*e4b17023SJohn Marino }
1119*e4b17023SJohn Marino 
1120*e4b17023SJohn Marino /* Convert to a new real mode from a fixed-point.  */
1121*e4b17023SJohn Marino 
1122*e4b17023SJohn Marino void
real_convert_from_fixed(REAL_VALUE_TYPE * r,enum machine_mode mode,const FIXED_VALUE_TYPE * f)1123*e4b17023SJohn Marino real_convert_from_fixed (REAL_VALUE_TYPE *r, enum machine_mode mode,
1124*e4b17023SJohn Marino 			 const FIXED_VALUE_TYPE *f)
1125*e4b17023SJohn Marino {
1126*e4b17023SJohn Marino   REAL_VALUE_TYPE base_value, fixed_value, real_value;
1127*e4b17023SJohn Marino 
1128*e4b17023SJohn Marino   real_2expN (&base_value, GET_MODE_FBIT (f->mode), f->mode);
1129*e4b17023SJohn Marino   real_from_integer (&fixed_value, VOIDmode, f->data.low, f->data.high,
1130*e4b17023SJohn Marino 		     UNSIGNED_FIXED_POINT_MODE_P (f->mode));
1131*e4b17023SJohn Marino   real_arithmetic (&real_value, RDIV_EXPR, &fixed_value, &base_value);
1132*e4b17023SJohn Marino   real_convert (r, mode, &real_value);
1133*e4b17023SJohn Marino }
1134*e4b17023SJohn Marino 
1135*e4b17023SJohn Marino /* Determine whether a fixed-point value F is negative.  */
1136*e4b17023SJohn Marino 
1137*e4b17023SJohn Marino bool
fixed_isneg(const FIXED_VALUE_TYPE * f)1138*e4b17023SJohn Marino fixed_isneg (const FIXED_VALUE_TYPE *f)
1139*e4b17023SJohn Marino {
1140*e4b17023SJohn Marino   if (SIGNED_FIXED_POINT_MODE_P (f->mode))
1141*e4b17023SJohn Marino     {
1142*e4b17023SJohn Marino       int i_f_bits = GET_MODE_IBIT (f->mode) + GET_MODE_FBIT (f->mode);
1143*e4b17023SJohn Marino       int sign_bit = get_fixed_sign_bit (f->data, i_f_bits);
1144*e4b17023SJohn Marino       if (sign_bit == 1)
1145*e4b17023SJohn Marino 	return true;
1146*e4b17023SJohn Marino     }
1147*e4b17023SJohn Marino 
1148*e4b17023SJohn Marino   return false;
1149*e4b17023SJohn Marino }
1150