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