* g++.dg/cpp0x/constexpr-53094-2.C: Ignore non-standard ABI
[official-gcc.git] / gcc / fixed-value.c
blob2e97a4926193d2c5b2fb39b4e122009854b7955c
1 /* Fixed-point arithmetic support.
2 Copyright (C) 2006-2013 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 "tree.h"
25 #include "diagnostic-core.h"
27 /* Compare two fixed objects for bitwise identity. */
29 bool
30 fixed_identical (const FIXED_VALUE_TYPE *a, const FIXED_VALUE_TYPE *b)
32 return (a->mode == b->mode
33 && a->data.high == b->data.high
34 && a->data.low == b->data.low);
37 /* Calculate a hash value. */
39 unsigned int
40 fixed_hash (const FIXED_VALUE_TYPE *f)
42 return (unsigned int) (f->data.low ^ f->data.high);
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. */
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. */
61 static enum fixed_value_range_code
62 check_real_for_fixed_mode (REAL_VALUE_TYPE *real_value, enum machine_mode mode)
64 REAL_VALUE_TYPE max_value, min_value, epsilon_value;
66 real_2expN (&max_value, GET_MODE_IBIT (mode), mode);
67 real_2expN (&epsilon_value, -GET_MODE_FBIT (mode), mode);
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");
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;
85 /* Construct a CONST_FIXED from a bit payload and machine mode MODE.
86 The bits in PAYLOAD are used verbatim. */
88 FIXED_VALUE_TYPE
89 fixed_from_double_int (double_int payload, enum machine_mode mode)
91 FIXED_VALUE_TYPE value;
93 gcc_assert (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_DOUBLE_INT);
95 value.data = payload;
96 value.mode = mode;
98 return value;
102 /* Initialize from a decimal or hexadecimal string. */
104 void
105 fixed_from_string (FIXED_VALUE_TYPE *f, const char *str, enum machine_mode mode)
107 REAL_VALUE_TYPE real_value, fixed_value, base_value;
108 unsigned int fbit;
109 enum fixed_value_range_code temp;
111 f->mode = mode;
112 fbit = GET_MODE_FBIT (mode);
114 real_from_string (&real_value, str);
115 temp = check_real_for_fixed_mode (&real_value, f->mode);
116 /* We don't want to warn the case when the _Fract value is 1.0. */
117 if (temp == FIXED_UNDERFLOW
118 || temp == FIXED_GT_MAX_EPS
119 || (temp == FIXED_MAX_EPS && ALL_ACCUM_MODE_P (f->mode)))
120 warning (OPT_Woverflow,
121 "large fixed-point constant implicitly truncated to fixed-point type");
122 real_2expN (&base_value, fbit, mode);
123 real_arithmetic (&fixed_value, MULT_EXPR, &real_value, &base_value);
124 real_to_integer2 ((HOST_WIDE_INT *)&f->data.low, &f->data.high,
125 &fixed_value);
127 if (temp == FIXED_MAX_EPS && ALL_FRACT_MODE_P (f->mode))
129 /* From the spec, we need to evaluate 1 to the maximal value. */
130 f->data.low = -1;
131 f->data.high = -1;
132 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
133 + GET_MODE_IBIT (f->mode));
135 else
136 f->data = f->data.ext (SIGNED_FIXED_POINT_MODE_P (f->mode)
137 + GET_MODE_FBIT (f->mode)
138 + GET_MODE_IBIT (f->mode),
139 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
142 /* Render F as a decimal floating point constant. */
144 void
145 fixed_to_decimal (char *str, const FIXED_VALUE_TYPE *f_orig,
146 size_t buf_size)
148 REAL_VALUE_TYPE real_value, base_value, fixed_value;
150 real_2expN (&base_value, GET_MODE_FBIT (f_orig->mode), f_orig->mode);
151 real_from_integer (&real_value, VOIDmode, f_orig->data.low, f_orig->data.high,
152 UNSIGNED_FIXED_POINT_MODE_P (f_orig->mode));
153 real_arithmetic (&fixed_value, RDIV_EXPR, &real_value, &base_value);
154 real_to_decimal (str, &fixed_value, buf_size, 0, 1);
157 /* If SAT_P, saturate A to the maximum or the minimum, and save to *F based on
158 the machine mode MODE.
159 Do not modify *F otherwise.
160 This function assumes the width of double_int is greater than the width
161 of the fixed-point value (the sum of a possible sign bit, possible ibits,
162 and fbits).
163 Return true, if !SAT_P and overflow. */
165 static bool
166 fixed_saturate1 (enum machine_mode mode, double_int a, double_int *f,
167 bool sat_p)
169 bool overflow_p = false;
170 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
171 int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
173 if (unsigned_p) /* Unsigned type. */
175 double_int max;
176 max.low = -1;
177 max.high = -1;
178 max = max.zext (i_f_bits);
179 if (a.ugt (max))
181 if (sat_p)
182 *f = max;
183 else
184 overflow_p = true;
187 else /* Signed type. */
189 double_int max, min;
190 max.high = -1;
191 max.low = -1;
192 max = max.zext (i_f_bits);
193 min.high = 0;
194 min.low = 1;
195 min = min.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
196 min = min.sext (1 + i_f_bits);
197 if (a.sgt (max))
199 if (sat_p)
200 *f = max;
201 else
202 overflow_p = true;
204 else if (a.slt (min))
206 if (sat_p)
207 *f = min;
208 else
209 overflow_p = true;
212 return overflow_p;
215 /* If SAT_P, saturate {A_HIGH, A_LOW} to the maximum or the minimum, and
216 save to *F based on the machine mode MODE.
217 Do not modify *F otherwise.
218 This function assumes the width of two double_int is greater than the width
219 of the fixed-point value (the sum of a possible sign bit, possible ibits,
220 and fbits).
221 Return true, if !SAT_P and overflow. */
223 static bool
224 fixed_saturate2 (enum machine_mode mode, double_int a_high, double_int a_low,
225 double_int *f, bool sat_p)
227 bool overflow_p = false;
228 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
229 int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
231 if (unsigned_p) /* Unsigned type. */
233 double_int max_r, max_s;
234 max_r.high = 0;
235 max_r.low = 0;
236 max_s.high = -1;
237 max_s.low = -1;
238 max_s = max_s.zext (i_f_bits);
239 if (a_high.ugt (max_r)
240 || (a_high == max_r &&
241 a_low.ugt (max_s)))
243 if (sat_p)
244 *f = max_s;
245 else
246 overflow_p = true;
249 else /* Signed type. */
251 double_int max_r, max_s, min_r, min_s;
252 max_r.high = 0;
253 max_r.low = 0;
254 max_s.high = -1;
255 max_s.low = -1;
256 max_s = max_s.zext (i_f_bits);
257 min_r.high = -1;
258 min_r.low = -1;
259 min_s.high = 0;
260 min_s.low = 1;
261 min_s = min_s.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
262 min_s = min_s.sext (1 + i_f_bits);
263 if (a_high.sgt (max_r)
264 || (a_high == max_r &&
265 a_low.ugt (max_s)))
267 if (sat_p)
268 *f = max_s;
269 else
270 overflow_p = true;
272 else if (a_high.slt (min_r)
273 || (a_high == min_r &&
274 a_low.ult (min_s)))
276 if (sat_p)
277 *f = min_s;
278 else
279 overflow_p = true;
282 return overflow_p;
285 /* Return the sign bit based on I_F_BITS. */
287 static inline int
288 get_fixed_sign_bit (double_int a, int i_f_bits)
290 if (i_f_bits < HOST_BITS_PER_WIDE_INT)
291 return (a.low >> i_f_bits) & 1;
292 else
293 return (a.high >> (i_f_bits - HOST_BITS_PER_WIDE_INT)) & 1;
296 /* Calculate F = A + (SUBTRACT_P ? -B : B).
297 If SAT_P, saturate the result to the max or the min.
298 Return true, if !SAT_P and overflow. */
300 static bool
301 do_fixed_add (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
302 const FIXED_VALUE_TYPE *b, bool subtract_p, bool sat_p)
304 bool overflow_p = false;
305 bool unsigned_p;
306 double_int temp;
307 int i_f_bits;
309 /* This was a conditional expression but it triggered a bug in
310 Sun C 5.5. */
311 if (subtract_p)
312 temp = -b->data;
313 else
314 temp = b->data;
316 unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
317 i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
318 f->mode = a->mode;
319 f->data = a->data + temp;
320 if (unsigned_p) /* Unsigned type. */
322 if (subtract_p) /* Unsigned subtraction. */
324 if (a->data.ult (b->data))
326 if (sat_p)
328 f->data.high = 0;
329 f->data.low = 0;
331 else
332 overflow_p = true;
335 else /* Unsigned addition. */
337 f->data = f->data.zext (i_f_bits);
338 if (f->data.ult (a->data)
339 || f->data.ult (b->data))
341 if (sat_p)
343 f->data.high = -1;
344 f->data.low = -1;
346 else
347 overflow_p = true;
351 else /* Signed type. */
353 if ((!subtract_p
354 && (get_fixed_sign_bit (a->data, i_f_bits)
355 == get_fixed_sign_bit (b->data, i_f_bits))
356 && (get_fixed_sign_bit (a->data, i_f_bits)
357 != get_fixed_sign_bit (f->data, i_f_bits)))
358 || (subtract_p
359 && (get_fixed_sign_bit (a->data, i_f_bits)
360 != get_fixed_sign_bit (b->data, i_f_bits))
361 && (get_fixed_sign_bit (a->data, i_f_bits)
362 != get_fixed_sign_bit (f->data, i_f_bits))))
364 if (sat_p)
366 f->data.low = 1;
367 f->data.high = 0;
368 f->data = f->data.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
369 if (get_fixed_sign_bit (a->data, i_f_bits) == 0)
371 --f->data;
374 else
375 overflow_p = true;
378 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
379 return overflow_p;
382 /* Calculate F = A * B.
383 If SAT_P, saturate the result to the max or the min.
384 Return true, if !SAT_P and overflow. */
386 static bool
387 do_fixed_multiply (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
388 const FIXED_VALUE_TYPE *b, bool sat_p)
390 bool overflow_p = false;
391 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
392 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
393 f->mode = a->mode;
394 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT)
396 f->data = a->data * b->data;
397 f->data = f->data.lshift (-GET_MODE_FBIT (f->mode),
398 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
399 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
401 else
403 /* The result of multiplication expands to two double_int. */
404 double_int a_high, a_low, b_high, b_low;
405 double_int high_high, high_low, low_high, low_low;
406 double_int r, s, temp1, temp2;
407 int carry = 0;
409 /* Decompose a and b to four double_int. */
410 a_high.low = a->data.high;
411 a_high.high = 0;
412 a_low.low = a->data.low;
413 a_low.high = 0;
414 b_high.low = b->data.high;
415 b_high.high = 0;
416 b_low.low = b->data.low;
417 b_low.high = 0;
419 /* Perform four multiplications. */
420 low_low = a_low * b_low;
421 low_high = a_low * b_high;
422 high_low = a_high * b_low;
423 high_high = a_high * b_high;
425 /* Accumulate four results to {r, s}. */
426 temp1.high = high_low.low;
427 temp1.low = 0;
428 s = low_low + temp1;
429 if (s.ult (low_low)
430 || s.ult (temp1))
431 carry ++; /* Carry */
432 temp1.high = s.high;
433 temp1.low = s.low;
434 temp2.high = low_high.low;
435 temp2.low = 0;
436 s = temp1 + temp2;
437 if (s.ult (temp1)
438 || s.ult (temp2))
439 carry ++; /* Carry */
441 temp1.low = high_low.high;
442 temp1.high = 0;
443 r = high_high + temp1;
444 temp1.low = low_high.high;
445 temp1.high = 0;
446 r += temp1;
447 temp1.low = carry;
448 temp1.high = 0;
449 r += temp1;
451 /* We need to subtract b from r, if a < 0. */
452 if (!unsigned_p && a->data.high < 0)
453 r -= b->data;
454 /* We need to subtract a from r, if b < 0. */
455 if (!unsigned_p && b->data.high < 0)
456 r -= a->data;
458 /* Shift right the result by FBIT. */
459 if (GET_MODE_FBIT (f->mode) == HOST_BITS_PER_DOUBLE_INT)
461 s.low = r.low;
462 s.high = r.high;
463 if (unsigned_p)
465 r.low = 0;
466 r.high = 0;
468 else
470 r.low = -1;
471 r.high = -1;
473 f->data.low = s.low;
474 f->data.high = s.high;
476 else
478 s = s.llshift ((-GET_MODE_FBIT (f->mode)), HOST_BITS_PER_DOUBLE_INT);
479 f->data = r.llshift ((HOST_BITS_PER_DOUBLE_INT
480 - GET_MODE_FBIT (f->mode)),
481 HOST_BITS_PER_DOUBLE_INT);
482 f->data.low = f->data.low | s.low;
483 f->data.high = f->data.high | s.high;
484 s.low = f->data.low;
485 s.high = f->data.high;
486 r = r.lshift (-GET_MODE_FBIT (f->mode),
487 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
490 overflow_p = fixed_saturate2 (f->mode, r, s, &f->data, sat_p);
493 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
494 return overflow_p;
497 /* Calculate F = A / B.
498 If SAT_P, saturate the result to the max or the min.
499 Return true, if !SAT_P and overflow. */
501 static bool
502 do_fixed_divide (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
503 const FIXED_VALUE_TYPE *b, bool sat_p)
505 bool overflow_p = false;
506 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
507 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
508 f->mode = a->mode;
509 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT)
511 f->data = a->data.lshift (GET_MODE_FBIT (f->mode),
512 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
513 f->data = f->data.div (b->data, unsigned_p, TRUNC_DIV_EXPR);
514 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
516 else
518 double_int pos_a, pos_b, r, s;
519 double_int quo_r, quo_s, mod, temp;
520 int num_of_neg = 0;
521 int i;
523 /* If a < 0, negate a. */
524 if (!unsigned_p && a->data.high < 0)
526 pos_a = -a->data;
527 num_of_neg ++;
529 else
530 pos_a = a->data;
532 /* If b < 0, negate b. */
533 if (!unsigned_p && b->data.high < 0)
535 pos_b = -b->data;
536 num_of_neg ++;
538 else
539 pos_b = b->data;
541 /* Left shift pos_a to {r, s} by FBIT. */
542 if (GET_MODE_FBIT (f->mode) == HOST_BITS_PER_DOUBLE_INT)
544 r = pos_a;
545 s.high = 0;
546 s.low = 0;
548 else
550 s = pos_a.llshift (GET_MODE_FBIT (f->mode), HOST_BITS_PER_DOUBLE_INT);
551 r = pos_a.llshift (- (HOST_BITS_PER_DOUBLE_INT
552 - GET_MODE_FBIT (f->mode)),
553 HOST_BITS_PER_DOUBLE_INT);
556 /* Divide r by pos_b to quo_r. The remainder is in mod. */
557 quo_r = r.divmod (pos_b, 1, TRUNC_DIV_EXPR, &mod);
558 quo_s = double_int_zero;
560 for (i = 0; i < HOST_BITS_PER_DOUBLE_INT; i++)
562 /* Record the leftmost bit of mod. */
563 int leftmost_mod = (mod.high < 0);
565 /* Shift left mod by 1 bit. */
566 mod = mod.llshift (1, HOST_BITS_PER_DOUBLE_INT);
568 /* Test the leftmost bit of s to add to mod. */
569 if (s.high < 0)
570 mod.low += 1;
572 /* Shift left quo_s by 1 bit. */
573 quo_s = quo_s.llshift (1, HOST_BITS_PER_DOUBLE_INT);
575 /* Try to calculate (mod - pos_b). */
576 temp = mod - pos_b;
578 if (leftmost_mod == 1 || mod.ucmp (pos_b) != -1)
580 quo_s.low += 1;
581 mod = temp;
584 /* Shift left s by 1 bit. */
585 s = s.llshift (1, HOST_BITS_PER_DOUBLE_INT);
589 if (num_of_neg == 1)
591 quo_s = -quo_s;
592 if (quo_s.high == 0 && quo_s.low == 0)
593 quo_r = -quo_r;
594 else
596 quo_r.low = ~quo_r.low;
597 quo_r.high = ~quo_r.high;
601 f->data = quo_s;
602 overflow_p = fixed_saturate2 (f->mode, quo_r, quo_s, &f->data, sat_p);
605 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
606 return overflow_p;
609 /* Calculate F = A << B if LEFT_P. Otherwise, F = A >> B.
610 If SAT_P, saturate the result to the max or the min.
611 Return true, if !SAT_P and overflow. */
613 static bool
614 do_fixed_shift (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
615 const FIXED_VALUE_TYPE *b, bool left_p, bool sat_p)
617 bool overflow_p = false;
618 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
619 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
620 f->mode = a->mode;
622 if (b->data.low == 0)
624 f->data = a->data;
625 return overflow_p;
628 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT || (!left_p))
630 f->data = a->data.lshift (left_p ? b->data.low : -b->data.low,
631 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
632 if (left_p) /* Only left shift saturates. */
633 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
635 else /* We need two double_int to store the left-shift result. */
637 double_int temp_high, temp_low;
638 if (b->data.low == HOST_BITS_PER_DOUBLE_INT)
640 temp_high = a->data;
641 temp_low.high = 0;
642 temp_low.low = 0;
644 else
646 temp_low = a->data.lshift (b->data.low,
647 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
648 /* Logical shift right to temp_high. */
649 temp_high = a->data.llshift (b->data.low - HOST_BITS_PER_DOUBLE_INT,
650 HOST_BITS_PER_DOUBLE_INT);
652 if (!unsigned_p && a->data.high < 0) /* Signed-extend temp_high. */
653 temp_high = temp_high.ext (b->data.low, unsigned_p);
654 f->data = temp_low;
655 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
656 sat_p);
658 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
659 return overflow_p;
662 /* Calculate F = -A.
663 If SAT_P, saturate the result to the max or the min.
664 Return true, if !SAT_P and overflow. */
666 static bool
667 do_fixed_neg (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a, bool sat_p)
669 bool overflow_p = false;
670 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
671 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
672 f->mode = a->mode;
673 f->data = -a->data;
674 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
676 if (unsigned_p) /* Unsigned type. */
678 if (f->data.low != 0 || f->data.high != 0)
680 if (sat_p)
682 f->data.low = 0;
683 f->data.high = 0;
685 else
686 overflow_p = true;
689 else /* Signed type. */
691 if (!(f->data.high == 0 && f->data.low == 0)
692 && f->data.high == a->data.high && f->data.low == a->data.low )
694 if (sat_p)
696 /* Saturate to the maximum by subtracting f->data by one. */
697 f->data.low = -1;
698 f->data.high = -1;
699 f->data = f->data.zext (i_f_bits);
701 else
702 overflow_p = true;
705 return overflow_p;
708 /* Perform the binary or unary operation described by CODE.
709 Note that OP0 and OP1 must have the same mode for binary operators.
710 For a unary operation, leave OP1 NULL.
711 Return true, if !SAT_P and overflow. */
713 bool
714 fixed_arithmetic (FIXED_VALUE_TYPE *f, int icode, const FIXED_VALUE_TYPE *op0,
715 const FIXED_VALUE_TYPE *op1, bool sat_p)
717 switch (icode)
719 case NEGATE_EXPR:
720 return do_fixed_neg (f, op0, sat_p);
721 break;
723 case PLUS_EXPR:
724 gcc_assert (op0->mode == op1->mode);
725 return do_fixed_add (f, op0, op1, false, sat_p);
726 break;
728 case MINUS_EXPR:
729 gcc_assert (op0->mode == op1->mode);
730 return do_fixed_add (f, op0, op1, true, sat_p);
731 break;
733 case MULT_EXPR:
734 gcc_assert (op0->mode == op1->mode);
735 return do_fixed_multiply (f, op0, op1, sat_p);
736 break;
738 case TRUNC_DIV_EXPR:
739 gcc_assert (op0->mode == op1->mode);
740 return do_fixed_divide (f, op0, op1, sat_p);
741 break;
743 case LSHIFT_EXPR:
744 return do_fixed_shift (f, op0, op1, true, sat_p);
745 break;
747 case RSHIFT_EXPR:
748 return do_fixed_shift (f, op0, op1, false, sat_p);
749 break;
751 default:
752 gcc_unreachable ();
754 return false;
757 /* Compare fixed-point values by tree_code.
758 Note that OP0 and OP1 must have the same mode. */
760 bool
761 fixed_compare (int icode, const FIXED_VALUE_TYPE *op0,
762 const FIXED_VALUE_TYPE *op1)
764 enum tree_code code = (enum tree_code) icode;
765 gcc_assert (op0->mode == op1->mode);
767 switch (code)
769 case NE_EXPR:
770 return op0->data != op1->data;
772 case EQ_EXPR:
773 return op0->data == op1->data;
775 case LT_EXPR:
776 return op0->data.cmp (op1->data,
777 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) == -1;
779 case LE_EXPR:
780 return op0->data.cmp (op1->data,
781 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) != 1;
783 case GT_EXPR:
784 return op0->data.cmp (op1->data,
785 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) == 1;
787 case GE_EXPR:
788 return op0->data.cmp (op1->data,
789 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) != -1;
791 default:
792 gcc_unreachable ();
796 /* Extend or truncate to a new mode.
797 If SAT_P, saturate the result to the max or the min.
798 Return true, if !SAT_P and overflow. */
800 bool
801 fixed_convert (FIXED_VALUE_TYPE *f, enum machine_mode mode,
802 const FIXED_VALUE_TYPE *a, bool sat_p)
804 bool overflow_p = false;
805 if (mode == a->mode)
807 *f = *a;
808 return overflow_p;
811 if (GET_MODE_FBIT (mode) > GET_MODE_FBIT (a->mode))
813 /* Left shift a to temp_high, temp_low based on a->mode. */
814 double_int temp_high, temp_low;
815 int amount = GET_MODE_FBIT (mode) - GET_MODE_FBIT (a->mode);
816 temp_low = a->data.lshift (amount,
817 HOST_BITS_PER_DOUBLE_INT,
818 SIGNED_FIXED_POINT_MODE_P (a->mode));
819 /* Logical shift right to temp_high. */
820 temp_high = a->data.llshift (amount - HOST_BITS_PER_DOUBLE_INT,
821 HOST_BITS_PER_DOUBLE_INT);
822 if (SIGNED_FIXED_POINT_MODE_P (a->mode)
823 && a->data.high < 0) /* Signed-extend temp_high. */
824 temp_high = temp_high.sext (amount);
825 f->mode = mode;
826 f->data = temp_low;
827 if (SIGNED_FIXED_POINT_MODE_P (a->mode) ==
828 SIGNED_FIXED_POINT_MODE_P (f->mode))
829 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
830 sat_p);
831 else
833 /* Take care of the cases when converting between signed and
834 unsigned. */
835 if (SIGNED_FIXED_POINT_MODE_P (a->mode))
837 /* Signed -> Unsigned. */
838 if (a->data.high < 0)
840 if (sat_p)
842 f->data.low = 0; /* Set to zero. */
843 f->data.high = 0; /* Set to zero. */
845 else
846 overflow_p = true;
848 else
849 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
850 &f->data, sat_p);
852 else
854 /* Unsigned -> Signed. */
855 if (temp_high.high < 0)
857 if (sat_p)
859 /* Set to maximum. */
860 f->data.low = -1; /* Set to all ones. */
861 f->data.high = -1; /* Set to all ones. */
862 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
863 + GET_MODE_IBIT (f->mode));
864 /* Clear the sign. */
866 else
867 overflow_p = true;
869 else
870 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
871 &f->data, sat_p);
875 else
877 /* Right shift a to temp based on a->mode. */
878 double_int temp;
879 temp = a->data.lshift (GET_MODE_FBIT (mode) - GET_MODE_FBIT (a->mode),
880 HOST_BITS_PER_DOUBLE_INT,
881 SIGNED_FIXED_POINT_MODE_P (a->mode));
882 f->mode = mode;
883 f->data = temp;
884 if (SIGNED_FIXED_POINT_MODE_P (a->mode) ==
885 SIGNED_FIXED_POINT_MODE_P (f->mode))
886 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
887 else
889 /* Take care of the cases when converting between signed and
890 unsigned. */
891 if (SIGNED_FIXED_POINT_MODE_P (a->mode))
893 /* Signed -> Unsigned. */
894 if (a->data.high < 0)
896 if (sat_p)
898 f->data.low = 0; /* Set to zero. */
899 f->data.high = 0; /* Set to zero. */
901 else
902 overflow_p = true;
904 else
905 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data,
906 sat_p);
908 else
910 /* Unsigned -> Signed. */
911 if (temp.high < 0)
913 if (sat_p)
915 /* Set to maximum. */
916 f->data.low = -1; /* Set to all ones. */
917 f->data.high = -1; /* Set to all ones. */
918 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
919 + GET_MODE_IBIT (f->mode));
920 /* Clear the sign. */
922 else
923 overflow_p = true;
925 else
926 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data,
927 sat_p);
932 f->data = f->data.ext (SIGNED_FIXED_POINT_MODE_P (f->mode)
933 + GET_MODE_FBIT (f->mode)
934 + GET_MODE_IBIT (f->mode),
935 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
936 return overflow_p;
939 /* Convert to a new fixed-point mode from an integer.
940 If UNSIGNED_P, this integer is unsigned.
941 If SAT_P, saturate the result to the max or the min.
942 Return true, if !SAT_P and overflow. */
944 bool
945 fixed_convert_from_int (FIXED_VALUE_TYPE *f, enum machine_mode mode,
946 double_int a, bool unsigned_p, bool sat_p)
948 bool overflow_p = false;
949 /* Left shift a to temp_high, temp_low. */
950 double_int temp_high, temp_low;
951 int amount = GET_MODE_FBIT (mode);
952 if (amount == HOST_BITS_PER_DOUBLE_INT)
954 temp_high = a;
955 temp_low.low = 0;
956 temp_low.high = 0;
958 else
960 temp_low = a.llshift (amount, HOST_BITS_PER_DOUBLE_INT);
962 /* Logical shift right to temp_high. */
963 temp_high = a.llshift (amount - HOST_BITS_PER_DOUBLE_INT,
964 HOST_BITS_PER_DOUBLE_INT);
966 if (!unsigned_p && a.high < 0) /* Signed-extend temp_high. */
967 temp_high = temp_high.sext (amount);
969 f->mode = mode;
970 f->data = temp_low;
972 if (unsigned_p == UNSIGNED_FIXED_POINT_MODE_P (f->mode))
973 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
974 sat_p);
975 else
977 /* Take care of the cases when converting between signed and unsigned. */
978 if (!unsigned_p)
980 /* Signed -> Unsigned. */
981 if (a.high < 0)
983 if (sat_p)
985 f->data.low = 0; /* Set to zero. */
986 f->data.high = 0; /* Set to zero. */
988 else
989 overflow_p = true;
991 else
992 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
993 &f->data, sat_p);
995 else
997 /* Unsigned -> Signed. */
998 if (temp_high.high < 0)
1000 if (sat_p)
1002 /* Set to maximum. */
1003 f->data.low = -1; /* Set to all ones. */
1004 f->data.high = -1; /* Set to all ones. */
1005 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
1006 + GET_MODE_IBIT (f->mode));
1007 /* Clear the sign. */
1009 else
1010 overflow_p = true;
1012 else
1013 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
1014 &f->data, sat_p);
1017 f->data = f->data.ext (SIGNED_FIXED_POINT_MODE_P (f->mode)
1018 + GET_MODE_FBIT (f->mode)
1019 + GET_MODE_IBIT (f->mode),
1020 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
1021 return overflow_p;
1024 /* Convert to a new fixed-point mode from a real.
1025 If SAT_P, saturate the result to the max or the min.
1026 Return true, if !SAT_P and overflow. */
1028 bool
1029 fixed_convert_from_real (FIXED_VALUE_TYPE *f, enum machine_mode mode,
1030 const REAL_VALUE_TYPE *a, bool sat_p)
1032 bool overflow_p = false;
1033 REAL_VALUE_TYPE real_value, fixed_value, base_value;
1034 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
1035 int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
1036 unsigned int fbit = GET_MODE_FBIT (mode);
1037 enum fixed_value_range_code temp;
1039 real_value = *a;
1040 f->mode = mode;
1041 real_2expN (&base_value, fbit, mode);
1042 real_arithmetic (&fixed_value, MULT_EXPR, &real_value, &base_value);
1043 real_to_integer2 ((HOST_WIDE_INT *)&f->data.low, &f->data.high, &fixed_value);
1044 temp = check_real_for_fixed_mode (&real_value, mode);
1045 if (temp == FIXED_UNDERFLOW) /* Minimum. */
1047 if (sat_p)
1049 if (unsigned_p)
1051 f->data.low = 0;
1052 f->data.high = 0;
1054 else
1056 f->data.low = 1;
1057 f->data.high = 0;
1058 f->data = f->data.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
1059 f->data = f->data.sext (1 + i_f_bits);
1062 else
1063 overflow_p = true;
1065 else if (temp == FIXED_GT_MAX_EPS || temp == FIXED_MAX_EPS) /* Maximum. */
1067 if (sat_p)
1069 f->data.low = -1;
1070 f->data.high = -1;
1071 f->data = f->data.zext (i_f_bits);
1073 else
1074 overflow_p = true;
1076 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
1077 return overflow_p;
1080 /* Convert to a new real mode from a fixed-point. */
1082 void
1083 real_convert_from_fixed (REAL_VALUE_TYPE *r, enum machine_mode mode,
1084 const FIXED_VALUE_TYPE *f)
1086 REAL_VALUE_TYPE base_value, fixed_value, real_value;
1088 real_2expN (&base_value, GET_MODE_FBIT (f->mode), f->mode);
1089 real_from_integer (&fixed_value, VOIDmode, f->data.low, f->data.high,
1090 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
1091 real_arithmetic (&real_value, RDIV_EXPR, &fixed_value, &base_value);
1092 real_convert (r, mode, &real_value);
1095 /* Determine whether a fixed-point value F is negative. */
1097 bool
1098 fixed_isneg (const FIXED_VALUE_TYPE *f)
1100 if (SIGNED_FIXED_POINT_MODE_P (f->mode))
1102 int i_f_bits = GET_MODE_IBIT (f->mode) + GET_MODE_FBIT (f->mode);
1103 int sign_bit = get_fixed_sign_bit (f->data, i_f_bits);
1104 if (sign_bit == 1)
1105 return true;
1108 return false;