Daily bump.
[official-gcc.git] / gcc / fixed-value.c
blob151b6a10ba142739c863bb19a71239122d3f9c73
1 /* Fixed-point arithmetic support.
2 Copyright (C) 2006-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "alias.h"
25 #include "symtab.h"
26 #include "tree.h"
27 #include "diagnostic-core.h"
29 /* Compare two fixed objects for bitwise identity. */
31 bool
32 fixed_identical (const FIXED_VALUE_TYPE *a, const FIXED_VALUE_TYPE *b)
34 return (a->mode == b->mode
35 && a->data.high == b->data.high
36 && a->data.low == b->data.low);
39 /* Calculate a hash value. */
41 unsigned int
42 fixed_hash (const FIXED_VALUE_TYPE *f)
44 return (unsigned int) (f->data.low ^ f->data.high);
47 /* Define the enum code for the range of the fixed-point value. */
48 enum fixed_value_range_code {
49 FIXED_OK, /* The value is within the range. */
50 FIXED_UNDERFLOW, /* The value is less than the minimum. */
51 FIXED_GT_MAX_EPS, /* The value is greater than the maximum, but not equal
52 to the maximum plus the epsilon. */
53 FIXED_MAX_EPS /* The value equals the maximum plus the epsilon. */
56 /* Check REAL_VALUE against the range of the fixed-point mode.
57 Return FIXED_OK, if it is within the range.
58 FIXED_UNDERFLOW, if it is less than the minimum.
59 FIXED_GT_MAX_EPS, if it is greater than the maximum, but not equal to
60 the maximum plus the epsilon.
61 FIXED_MAX_EPS, if it is equal to the maximum plus the epsilon. */
63 static enum fixed_value_range_code
64 check_real_for_fixed_mode (REAL_VALUE_TYPE *real_value, machine_mode mode)
66 REAL_VALUE_TYPE max_value, min_value, epsilon_value;
68 real_2expN (&max_value, GET_MODE_IBIT (mode), mode);
69 real_2expN (&epsilon_value, -GET_MODE_FBIT (mode), mode);
71 if (SIGNED_FIXED_POINT_MODE_P (mode))
72 min_value = real_value_negate (&max_value);
73 else
74 real_from_string (&min_value, "0.0");
76 if (real_compare (LT_EXPR, real_value, &min_value))
77 return FIXED_UNDERFLOW;
78 if (real_compare (EQ_EXPR, real_value, &max_value))
79 return FIXED_MAX_EPS;
80 real_arithmetic (&max_value, MINUS_EXPR, &max_value, &epsilon_value);
81 if (real_compare (GT_EXPR, real_value, &max_value))
82 return FIXED_GT_MAX_EPS;
83 return FIXED_OK;
87 /* Construct a CONST_FIXED from a bit payload and machine mode MODE.
88 The bits in PAYLOAD are sign-extended/zero-extended according to MODE. */
90 FIXED_VALUE_TYPE
91 fixed_from_double_int (double_int payload, machine_mode mode)
93 FIXED_VALUE_TYPE value;
95 gcc_assert (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_DOUBLE_INT);
97 if (SIGNED_SCALAR_FIXED_POINT_MODE_P (mode))
98 value.data = payload.sext (1 + GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode));
99 else if (UNSIGNED_SCALAR_FIXED_POINT_MODE_P (mode))
100 value.data = payload.zext (GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode));
101 else
102 gcc_unreachable ();
104 value.mode = mode;
106 return value;
110 /* Initialize from a decimal or hexadecimal string. */
112 void
113 fixed_from_string (FIXED_VALUE_TYPE *f, const char *str, machine_mode mode)
115 REAL_VALUE_TYPE real_value, fixed_value, base_value;
116 unsigned int fbit;
117 enum fixed_value_range_code temp;
118 bool fail;
120 f->mode = mode;
121 fbit = GET_MODE_FBIT (mode);
123 real_from_string (&real_value, str);
124 temp = check_real_for_fixed_mode (&real_value, f->mode);
125 /* We don't want to warn the case when the _Fract value is 1.0. */
126 if (temp == FIXED_UNDERFLOW
127 || temp == FIXED_GT_MAX_EPS
128 || (temp == FIXED_MAX_EPS && ALL_ACCUM_MODE_P (f->mode)))
129 warning (OPT_Woverflow,
130 "large fixed-point constant implicitly truncated to fixed-point type");
131 real_2expN (&base_value, fbit, mode);
132 real_arithmetic (&fixed_value, MULT_EXPR, &real_value, &base_value);
133 wide_int w = real_to_integer (&fixed_value, &fail,
134 GET_MODE_PRECISION (mode));
135 f->data.low = w.elt (0);
136 f->data.high = w.elt (1);
138 if (temp == FIXED_MAX_EPS && ALL_FRACT_MODE_P (f->mode))
140 /* From the spec, we need to evaluate 1 to the maximal value. */
141 f->data.low = -1;
142 f->data.high = -1;
143 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
144 + GET_MODE_IBIT (f->mode));
146 else
147 f->data = f->data.ext (SIGNED_FIXED_POINT_MODE_P (f->mode)
148 + GET_MODE_FBIT (f->mode)
149 + GET_MODE_IBIT (f->mode),
150 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
153 /* Render F as a decimal floating point constant. */
155 void
156 fixed_to_decimal (char *str, const FIXED_VALUE_TYPE *f_orig,
157 size_t buf_size)
159 REAL_VALUE_TYPE real_value, base_value, fixed_value;
161 signop sgn = UNSIGNED_FIXED_POINT_MODE_P (f_orig->mode) ? UNSIGNED : SIGNED;
162 real_2expN (&base_value, GET_MODE_FBIT (f_orig->mode), f_orig->mode);
163 real_from_integer (&real_value, VOIDmode,
164 wide_int::from (f_orig->data,
165 GET_MODE_PRECISION (f_orig->mode), sgn),
166 sgn);
167 real_arithmetic (&fixed_value, RDIV_EXPR, &real_value, &base_value);
168 real_to_decimal (str, &fixed_value, buf_size, 0, 1);
171 /* If SAT_P, saturate A to the maximum or the minimum, and save to *F based on
172 the machine mode MODE.
173 Do not modify *F otherwise.
174 This function assumes the width of double_int is greater than the width
175 of the fixed-point value (the sum of a possible sign bit, possible ibits,
176 and fbits).
177 Return true, if !SAT_P and overflow. */
179 static bool
180 fixed_saturate1 (machine_mode mode, double_int a, double_int *f,
181 bool sat_p)
183 bool overflow_p = false;
184 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
185 int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
187 if (unsigned_p) /* Unsigned type. */
189 double_int max;
190 max.low = -1;
191 max.high = -1;
192 max = max.zext (i_f_bits);
193 if (a.ugt (max))
195 if (sat_p)
196 *f = max;
197 else
198 overflow_p = true;
201 else /* Signed type. */
203 double_int max, min;
204 max.high = -1;
205 max.low = -1;
206 max = max.zext (i_f_bits);
207 min.high = 0;
208 min.low = 1;
209 min = min.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
210 min = min.sext (1 + i_f_bits);
211 if (a.sgt (max))
213 if (sat_p)
214 *f = max;
215 else
216 overflow_p = true;
218 else if (a.slt (min))
220 if (sat_p)
221 *f = min;
222 else
223 overflow_p = true;
226 return overflow_p;
229 /* If SAT_P, saturate {A_HIGH, A_LOW} to the maximum or the minimum, and
230 save to *F based on the machine mode MODE.
231 Do not modify *F otherwise.
232 This function assumes the width of two double_int is greater than the width
233 of the fixed-point value (the sum of a possible sign bit, possible ibits,
234 and fbits).
235 Return true, if !SAT_P and overflow. */
237 static bool
238 fixed_saturate2 (machine_mode mode, double_int a_high, double_int a_low,
239 double_int *f, bool sat_p)
241 bool overflow_p = false;
242 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
243 int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
245 if (unsigned_p) /* Unsigned type. */
247 double_int max_r, max_s;
248 max_r.high = 0;
249 max_r.low = 0;
250 max_s.high = -1;
251 max_s.low = -1;
252 max_s = max_s.zext (i_f_bits);
253 if (a_high.ugt (max_r)
254 || (a_high == max_r &&
255 a_low.ugt (max_s)))
257 if (sat_p)
258 *f = max_s;
259 else
260 overflow_p = true;
263 else /* Signed type. */
265 double_int max_r, max_s, min_r, min_s;
266 max_r.high = 0;
267 max_r.low = 0;
268 max_s.high = -1;
269 max_s.low = -1;
270 max_s = max_s.zext (i_f_bits);
271 min_r.high = -1;
272 min_r.low = -1;
273 min_s.high = 0;
274 min_s.low = 1;
275 min_s = min_s.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
276 min_s = min_s.sext (1 + i_f_bits);
277 if (a_high.sgt (max_r)
278 || (a_high == max_r &&
279 a_low.ugt (max_s)))
281 if (sat_p)
282 *f = max_s;
283 else
284 overflow_p = true;
286 else if (a_high.slt (min_r)
287 || (a_high == min_r &&
288 a_low.ult (min_s)))
290 if (sat_p)
291 *f = min_s;
292 else
293 overflow_p = true;
296 return overflow_p;
299 /* Return the sign bit based on I_F_BITS. */
301 static inline int
302 get_fixed_sign_bit (double_int a, int i_f_bits)
304 if (i_f_bits < HOST_BITS_PER_WIDE_INT)
305 return (a.low >> i_f_bits) & 1;
306 else
307 return (a.high >> (i_f_bits - HOST_BITS_PER_WIDE_INT)) & 1;
310 /* Calculate F = A + (SUBTRACT_P ? -B : B).
311 If SAT_P, saturate the result to the max or the min.
312 Return true, if !SAT_P and overflow. */
314 static bool
315 do_fixed_add (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
316 const FIXED_VALUE_TYPE *b, bool subtract_p, bool sat_p)
318 bool overflow_p = false;
319 bool unsigned_p;
320 double_int temp;
321 int i_f_bits;
323 /* This was a conditional expression but it triggered a bug in
324 Sun C 5.5. */
325 if (subtract_p)
326 temp = -b->data;
327 else
328 temp = b->data;
330 unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
331 i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
332 f->mode = a->mode;
333 f->data = a->data + temp;
334 if (unsigned_p) /* Unsigned type. */
336 if (subtract_p) /* Unsigned subtraction. */
338 if (a->data.ult (b->data))
340 if (sat_p)
342 f->data.high = 0;
343 f->data.low = 0;
345 else
346 overflow_p = true;
349 else /* Unsigned addition. */
351 f->data = f->data.zext (i_f_bits);
352 if (f->data.ult (a->data)
353 || f->data.ult (b->data))
355 if (sat_p)
357 f->data.high = -1;
358 f->data.low = -1;
360 else
361 overflow_p = true;
365 else /* Signed type. */
367 if ((!subtract_p
368 && (get_fixed_sign_bit (a->data, i_f_bits)
369 == get_fixed_sign_bit (b->data, i_f_bits))
370 && (get_fixed_sign_bit (a->data, i_f_bits)
371 != get_fixed_sign_bit (f->data, i_f_bits)))
372 || (subtract_p
373 && (get_fixed_sign_bit (a->data, i_f_bits)
374 != get_fixed_sign_bit (b->data, i_f_bits))
375 && (get_fixed_sign_bit (a->data, i_f_bits)
376 != get_fixed_sign_bit (f->data, i_f_bits))))
378 if (sat_p)
380 f->data.low = 1;
381 f->data.high = 0;
382 f->data = f->data.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
383 if (get_fixed_sign_bit (a->data, i_f_bits) == 0)
385 --f->data;
388 else
389 overflow_p = true;
392 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
393 return overflow_p;
396 /* Calculate F = A * B.
397 If SAT_P, saturate the result to the max or the min.
398 Return true, if !SAT_P and overflow. */
400 static bool
401 do_fixed_multiply (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
402 const FIXED_VALUE_TYPE *b, bool sat_p)
404 bool overflow_p = false;
405 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
406 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
407 f->mode = a->mode;
408 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT)
410 f->data = a->data * b->data;
411 f->data = f->data.lshift (-GET_MODE_FBIT (f->mode),
412 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
413 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
415 else
417 /* The result of multiplication expands to two double_int. */
418 double_int a_high, a_low, b_high, b_low;
419 double_int high_high, high_low, low_high, low_low;
420 double_int r, s, temp1, temp2;
421 int carry = 0;
423 /* Decompose a and b to four double_int. */
424 a_high.low = a->data.high;
425 a_high.high = 0;
426 a_low.low = a->data.low;
427 a_low.high = 0;
428 b_high.low = b->data.high;
429 b_high.high = 0;
430 b_low.low = b->data.low;
431 b_low.high = 0;
433 /* Perform four multiplications. */
434 low_low = a_low * b_low;
435 low_high = a_low * b_high;
436 high_low = a_high * b_low;
437 high_high = a_high * b_high;
439 /* Accumulate four results to {r, s}. */
440 temp1.high = high_low.low;
441 temp1.low = 0;
442 s = low_low + temp1;
443 if (s.ult (low_low)
444 || s.ult (temp1))
445 carry ++; /* Carry */
446 temp1.high = s.high;
447 temp1.low = s.low;
448 temp2.high = low_high.low;
449 temp2.low = 0;
450 s = temp1 + temp2;
451 if (s.ult (temp1)
452 || s.ult (temp2))
453 carry ++; /* Carry */
455 temp1.low = high_low.high;
456 temp1.high = 0;
457 r = high_high + temp1;
458 temp1.low = low_high.high;
459 temp1.high = 0;
460 r += temp1;
461 temp1.low = carry;
462 temp1.high = 0;
463 r += temp1;
465 /* We need to subtract b from r, if a < 0. */
466 if (!unsigned_p && a->data.high < 0)
467 r -= b->data;
468 /* We need to subtract a from r, if b < 0. */
469 if (!unsigned_p && b->data.high < 0)
470 r -= a->data;
472 /* Shift right the result by FBIT. */
473 if (GET_MODE_FBIT (f->mode) == HOST_BITS_PER_DOUBLE_INT)
475 s.low = r.low;
476 s.high = r.high;
477 if (unsigned_p)
479 r.low = 0;
480 r.high = 0;
482 else
484 r.low = -1;
485 r.high = -1;
487 f->data.low = s.low;
488 f->data.high = s.high;
490 else
492 s = s.llshift ((-GET_MODE_FBIT (f->mode)), HOST_BITS_PER_DOUBLE_INT);
493 f->data = r.llshift ((HOST_BITS_PER_DOUBLE_INT
494 - GET_MODE_FBIT (f->mode)),
495 HOST_BITS_PER_DOUBLE_INT);
496 f->data.low = f->data.low | s.low;
497 f->data.high = f->data.high | s.high;
498 s.low = f->data.low;
499 s.high = f->data.high;
500 r = r.lshift (-GET_MODE_FBIT (f->mode),
501 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
504 overflow_p = fixed_saturate2 (f->mode, r, s, &f->data, sat_p);
507 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
508 return overflow_p;
511 /* Calculate F = A / B.
512 If SAT_P, saturate the result to the max or the min.
513 Return true, if !SAT_P and overflow. */
515 static bool
516 do_fixed_divide (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
517 const FIXED_VALUE_TYPE *b, bool sat_p)
519 bool overflow_p = false;
520 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
521 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
522 f->mode = a->mode;
523 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT)
525 f->data = a->data.lshift (GET_MODE_FBIT (f->mode),
526 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
527 f->data = f->data.div (b->data, unsigned_p, TRUNC_DIV_EXPR);
528 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
530 else
532 double_int pos_a, pos_b, r, s;
533 double_int quo_r, quo_s, mod, temp;
534 int num_of_neg = 0;
535 int i;
537 /* If a < 0, negate a. */
538 if (!unsigned_p && a->data.high < 0)
540 pos_a = -a->data;
541 num_of_neg ++;
543 else
544 pos_a = a->data;
546 /* If b < 0, negate b. */
547 if (!unsigned_p && b->data.high < 0)
549 pos_b = -b->data;
550 num_of_neg ++;
552 else
553 pos_b = b->data;
555 /* Left shift pos_a to {r, s} by FBIT. */
556 if (GET_MODE_FBIT (f->mode) == HOST_BITS_PER_DOUBLE_INT)
558 r = pos_a;
559 s.high = 0;
560 s.low = 0;
562 else
564 s = pos_a.llshift (GET_MODE_FBIT (f->mode), HOST_BITS_PER_DOUBLE_INT);
565 r = pos_a.llshift (- (HOST_BITS_PER_DOUBLE_INT
566 - GET_MODE_FBIT (f->mode)),
567 HOST_BITS_PER_DOUBLE_INT);
570 /* Divide r by pos_b to quo_r. The remainder is in mod. */
571 quo_r = r.divmod (pos_b, 1, TRUNC_DIV_EXPR, &mod);
572 quo_s = double_int_zero;
574 for (i = 0; i < HOST_BITS_PER_DOUBLE_INT; i++)
576 /* Record the leftmost bit of mod. */
577 int leftmost_mod = (mod.high < 0);
579 /* Shift left mod by 1 bit. */
580 mod = mod.lshift (1);
582 /* Test the leftmost bit of s to add to mod. */
583 if (s.high < 0)
584 mod.low += 1;
586 /* Shift left quo_s by 1 bit. */
587 quo_s = quo_s.lshift (1);
589 /* Try to calculate (mod - pos_b). */
590 temp = mod - pos_b;
592 if (leftmost_mod == 1 || mod.ucmp (pos_b) != -1)
594 quo_s.low += 1;
595 mod = temp;
598 /* Shift left s by 1 bit. */
599 s = s.lshift (1);
603 if (num_of_neg == 1)
605 quo_s = -quo_s;
606 if (quo_s.high == 0 && quo_s.low == 0)
607 quo_r = -quo_r;
608 else
610 quo_r.low = ~quo_r.low;
611 quo_r.high = ~quo_r.high;
615 f->data = quo_s;
616 overflow_p = fixed_saturate2 (f->mode, quo_r, quo_s, &f->data, sat_p);
619 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
620 return overflow_p;
623 /* Calculate F = A << B if LEFT_P. Otherwise, F = A >> B.
624 If SAT_P, saturate the result to the max or the min.
625 Return true, if !SAT_P and overflow. */
627 static bool
628 do_fixed_shift (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
629 const FIXED_VALUE_TYPE *b, bool left_p, bool sat_p)
631 bool overflow_p = false;
632 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
633 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
634 f->mode = a->mode;
636 if (b->data.low == 0)
638 f->data = a->data;
639 return overflow_p;
642 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT || (!left_p))
644 f->data = a->data.lshift (left_p ? b->data.low : -b->data.low,
645 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
646 if (left_p) /* Only left shift saturates. */
647 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
649 else /* We need two double_int to store the left-shift result. */
651 double_int temp_high, temp_low;
652 if (b->data.low == HOST_BITS_PER_DOUBLE_INT)
654 temp_high = a->data;
655 temp_low.high = 0;
656 temp_low.low = 0;
658 else
660 temp_low = a->data.lshift (b->data.low,
661 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
662 /* Logical shift right to temp_high. */
663 temp_high = a->data.llshift (b->data.low - HOST_BITS_PER_DOUBLE_INT,
664 HOST_BITS_PER_DOUBLE_INT);
666 if (!unsigned_p && a->data.high < 0) /* Signed-extend temp_high. */
667 temp_high = temp_high.ext (b->data.low, unsigned_p);
668 f->data = temp_low;
669 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
670 sat_p);
672 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
673 return overflow_p;
676 /* Calculate F = -A.
677 If SAT_P, saturate the result to the max or the min.
678 Return true, if !SAT_P and overflow. */
680 static bool
681 do_fixed_neg (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a, bool sat_p)
683 bool overflow_p = false;
684 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
685 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
686 f->mode = a->mode;
687 f->data = -a->data;
688 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
690 if (unsigned_p) /* Unsigned type. */
692 if (f->data.low != 0 || f->data.high != 0)
694 if (sat_p)
696 f->data.low = 0;
697 f->data.high = 0;
699 else
700 overflow_p = true;
703 else /* Signed type. */
705 if (!(f->data.high == 0 && f->data.low == 0)
706 && f->data.high == a->data.high && f->data.low == a->data.low )
708 if (sat_p)
710 /* Saturate to the maximum by subtracting f->data by one. */
711 f->data.low = -1;
712 f->data.high = -1;
713 f->data = f->data.zext (i_f_bits);
715 else
716 overflow_p = true;
719 return overflow_p;
722 /* Perform the binary or unary operation described by CODE.
723 Note that OP0 and OP1 must have the same mode for binary operators.
724 For a unary operation, leave OP1 NULL.
725 Return true, if !SAT_P and overflow. */
727 bool
728 fixed_arithmetic (FIXED_VALUE_TYPE *f, int icode, const FIXED_VALUE_TYPE *op0,
729 const FIXED_VALUE_TYPE *op1, bool sat_p)
731 switch (icode)
733 case NEGATE_EXPR:
734 return do_fixed_neg (f, op0, sat_p);
735 break;
737 case PLUS_EXPR:
738 gcc_assert (op0->mode == op1->mode);
739 return do_fixed_add (f, op0, op1, false, sat_p);
740 break;
742 case MINUS_EXPR:
743 gcc_assert (op0->mode == op1->mode);
744 return do_fixed_add (f, op0, op1, true, sat_p);
745 break;
747 case MULT_EXPR:
748 gcc_assert (op0->mode == op1->mode);
749 return do_fixed_multiply (f, op0, op1, sat_p);
750 break;
752 case TRUNC_DIV_EXPR:
753 gcc_assert (op0->mode == op1->mode);
754 return do_fixed_divide (f, op0, op1, sat_p);
755 break;
757 case LSHIFT_EXPR:
758 return do_fixed_shift (f, op0, op1, true, sat_p);
759 break;
761 case RSHIFT_EXPR:
762 return do_fixed_shift (f, op0, op1, false, sat_p);
763 break;
765 default:
766 gcc_unreachable ();
768 return false;
771 /* Compare fixed-point values by tree_code.
772 Note that OP0 and OP1 must have the same mode. */
774 bool
775 fixed_compare (int icode, const FIXED_VALUE_TYPE *op0,
776 const FIXED_VALUE_TYPE *op1)
778 enum tree_code code = (enum tree_code) icode;
779 gcc_assert (op0->mode == op1->mode);
781 switch (code)
783 case NE_EXPR:
784 return op0->data != op1->data;
786 case EQ_EXPR:
787 return op0->data == op1->data;
789 case LT_EXPR:
790 return op0->data.cmp (op1->data,
791 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) == -1;
793 case LE_EXPR:
794 return op0->data.cmp (op1->data,
795 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) != 1;
797 case GT_EXPR:
798 return op0->data.cmp (op1->data,
799 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) == 1;
801 case GE_EXPR:
802 return op0->data.cmp (op1->data,
803 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) != -1;
805 default:
806 gcc_unreachable ();
810 /* Extend or truncate to a new mode.
811 If SAT_P, saturate the result to the max or the min.
812 Return true, if !SAT_P and overflow. */
814 bool
815 fixed_convert (FIXED_VALUE_TYPE *f, machine_mode mode,
816 const FIXED_VALUE_TYPE *a, bool sat_p)
818 bool overflow_p = false;
819 if (mode == a->mode)
821 *f = *a;
822 return overflow_p;
825 if (GET_MODE_FBIT (mode) > GET_MODE_FBIT (a->mode))
827 /* Left shift a to temp_high, temp_low based on a->mode. */
828 double_int temp_high, temp_low;
829 int amount = GET_MODE_FBIT (mode) - GET_MODE_FBIT (a->mode);
830 temp_low = a->data.lshift (amount,
831 HOST_BITS_PER_DOUBLE_INT,
832 SIGNED_FIXED_POINT_MODE_P (a->mode));
833 /* Logical shift right to temp_high. */
834 temp_high = a->data.llshift (amount - HOST_BITS_PER_DOUBLE_INT,
835 HOST_BITS_PER_DOUBLE_INT);
836 if (SIGNED_FIXED_POINT_MODE_P (a->mode)
837 && a->data.high < 0) /* Signed-extend temp_high. */
838 temp_high = temp_high.sext (amount);
839 f->mode = mode;
840 f->data = temp_low;
841 if (SIGNED_FIXED_POINT_MODE_P (a->mode) ==
842 SIGNED_FIXED_POINT_MODE_P (f->mode))
843 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
844 sat_p);
845 else
847 /* Take care of the cases when converting between signed and
848 unsigned. */
849 if (SIGNED_FIXED_POINT_MODE_P (a->mode))
851 /* Signed -> Unsigned. */
852 if (a->data.high < 0)
854 if (sat_p)
856 f->data.low = 0; /* Set to zero. */
857 f->data.high = 0; /* Set to zero. */
859 else
860 overflow_p = true;
862 else
863 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
864 &f->data, sat_p);
866 else
868 /* Unsigned -> Signed. */
869 if (temp_high.high < 0)
871 if (sat_p)
873 /* Set to maximum. */
874 f->data.low = -1; /* Set to all ones. */
875 f->data.high = -1; /* Set to all ones. */
876 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
877 + GET_MODE_IBIT (f->mode));
878 /* Clear the sign. */
880 else
881 overflow_p = true;
883 else
884 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
885 &f->data, sat_p);
889 else
891 /* Right shift a to temp based on a->mode. */
892 double_int temp;
893 temp = a->data.lshift (GET_MODE_FBIT (mode) - GET_MODE_FBIT (a->mode),
894 HOST_BITS_PER_DOUBLE_INT,
895 SIGNED_FIXED_POINT_MODE_P (a->mode));
896 f->mode = mode;
897 f->data = temp;
898 if (SIGNED_FIXED_POINT_MODE_P (a->mode) ==
899 SIGNED_FIXED_POINT_MODE_P (f->mode))
900 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
901 else
903 /* Take care of the cases when converting between signed and
904 unsigned. */
905 if (SIGNED_FIXED_POINT_MODE_P (a->mode))
907 /* Signed -> Unsigned. */
908 if (a->data.high < 0)
910 if (sat_p)
912 f->data.low = 0; /* Set to zero. */
913 f->data.high = 0; /* Set to zero. */
915 else
916 overflow_p = true;
918 else
919 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data,
920 sat_p);
922 else
924 /* Unsigned -> Signed. */
925 if (temp.high < 0)
927 if (sat_p)
929 /* Set to maximum. */
930 f->data.low = -1; /* Set to all ones. */
931 f->data.high = -1; /* Set to all ones. */
932 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
933 + GET_MODE_IBIT (f->mode));
934 /* Clear the sign. */
936 else
937 overflow_p = true;
939 else
940 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data,
941 sat_p);
946 f->data = f->data.ext (SIGNED_FIXED_POINT_MODE_P (f->mode)
947 + GET_MODE_FBIT (f->mode)
948 + GET_MODE_IBIT (f->mode),
949 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
950 return overflow_p;
953 /* Convert to a new fixed-point mode from an integer.
954 If UNSIGNED_P, this integer is unsigned.
955 If SAT_P, saturate the result to the max or the min.
956 Return true, if !SAT_P and overflow. */
958 bool
959 fixed_convert_from_int (FIXED_VALUE_TYPE *f, machine_mode mode,
960 double_int a, bool unsigned_p, bool sat_p)
962 bool overflow_p = false;
963 /* Left shift a to temp_high, temp_low. */
964 double_int temp_high, temp_low;
965 int amount = GET_MODE_FBIT (mode);
966 if (amount == HOST_BITS_PER_DOUBLE_INT)
968 temp_high = a;
969 temp_low.low = 0;
970 temp_low.high = 0;
972 else
974 temp_low = a.llshift (amount, HOST_BITS_PER_DOUBLE_INT);
976 /* Logical shift right to temp_high. */
977 temp_high = a.llshift (amount - HOST_BITS_PER_DOUBLE_INT,
978 HOST_BITS_PER_DOUBLE_INT);
980 if (!unsigned_p && a.high < 0) /* Signed-extend temp_high. */
981 temp_high = temp_high.sext (amount);
983 f->mode = mode;
984 f->data = temp_low;
986 if (unsigned_p == UNSIGNED_FIXED_POINT_MODE_P (f->mode))
987 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
988 sat_p);
989 else
991 /* Take care of the cases when converting between signed and unsigned. */
992 if (!unsigned_p)
994 /* Signed -> Unsigned. */
995 if (a.high < 0)
997 if (sat_p)
999 f->data.low = 0; /* Set to zero. */
1000 f->data.high = 0; /* Set to zero. */
1002 else
1003 overflow_p = true;
1005 else
1006 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
1007 &f->data, sat_p);
1009 else
1011 /* Unsigned -> Signed. */
1012 if (temp_high.high < 0)
1014 if (sat_p)
1016 /* Set to maximum. */
1017 f->data.low = -1; /* Set to all ones. */
1018 f->data.high = -1; /* Set to all ones. */
1019 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
1020 + GET_MODE_IBIT (f->mode));
1021 /* Clear the sign. */
1023 else
1024 overflow_p = true;
1026 else
1027 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
1028 &f->data, sat_p);
1031 f->data = f->data.ext (SIGNED_FIXED_POINT_MODE_P (f->mode)
1032 + GET_MODE_FBIT (f->mode)
1033 + GET_MODE_IBIT (f->mode),
1034 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
1035 return overflow_p;
1038 /* Convert to a new fixed-point mode from a real.
1039 If SAT_P, saturate the result to the max or the min.
1040 Return true, if !SAT_P and overflow. */
1042 bool
1043 fixed_convert_from_real (FIXED_VALUE_TYPE *f, machine_mode mode,
1044 const REAL_VALUE_TYPE *a, bool sat_p)
1046 bool overflow_p = false;
1047 REAL_VALUE_TYPE real_value, fixed_value, base_value;
1048 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
1049 int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
1050 unsigned int fbit = GET_MODE_FBIT (mode);
1051 enum fixed_value_range_code temp;
1052 bool fail;
1054 real_value = *a;
1055 f->mode = mode;
1056 real_2expN (&base_value, fbit, mode);
1057 real_arithmetic (&fixed_value, MULT_EXPR, &real_value, &base_value);
1059 wide_int w = real_to_integer (&fixed_value, &fail,
1060 GET_MODE_PRECISION (mode));
1061 f->data.low = w.elt (0);
1062 f->data.high = w.elt (1);
1063 temp = check_real_for_fixed_mode (&real_value, mode);
1064 if (temp == FIXED_UNDERFLOW) /* Minimum. */
1066 if (sat_p)
1068 if (unsigned_p)
1070 f->data.low = 0;
1071 f->data.high = 0;
1073 else
1075 f->data.low = 1;
1076 f->data.high = 0;
1077 f->data = f->data.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
1078 f->data = f->data.sext (1 + i_f_bits);
1081 else
1082 overflow_p = true;
1084 else if (temp == FIXED_GT_MAX_EPS || temp == FIXED_MAX_EPS) /* Maximum. */
1086 if (sat_p)
1088 f->data.low = -1;
1089 f->data.high = -1;
1090 f->data = f->data.zext (i_f_bits);
1092 else
1093 overflow_p = true;
1095 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
1096 return overflow_p;
1099 /* Convert to a new real mode from a fixed-point. */
1101 void
1102 real_convert_from_fixed (REAL_VALUE_TYPE *r, machine_mode mode,
1103 const FIXED_VALUE_TYPE *f)
1105 REAL_VALUE_TYPE base_value, fixed_value, real_value;
1107 signop sgn = UNSIGNED_FIXED_POINT_MODE_P (f->mode) ? UNSIGNED : SIGNED;
1108 real_2expN (&base_value, GET_MODE_FBIT (f->mode), f->mode);
1109 real_from_integer (&fixed_value, VOIDmode,
1110 wide_int::from (f->data, GET_MODE_PRECISION (f->mode),
1111 sgn), sgn);
1112 real_arithmetic (&real_value, RDIV_EXPR, &fixed_value, &base_value);
1113 real_convert (r, mode, &real_value);
1116 /* Determine whether a fixed-point value F is negative. */
1118 bool
1119 fixed_isneg (const FIXED_VALUE_TYPE *f)
1121 if (SIGNED_FIXED_POINT_MODE_P (f->mode))
1123 int i_f_bits = GET_MODE_IBIT (f->mode) + GET_MODE_FBIT (f->mode);
1124 int sign_bit = get_fixed_sign_bit (f->data, i_f_bits);
1125 if (sign_bit == 1)
1126 return true;
1129 return false;