lto-streamer-out.c (hash_tree): Use cl_optimization_hash.
[official-gcc.git] / gcc / fixed-value.c
blobcd65b9cb56d68ce09d05fbf6bc13f3ed5aba0c6d
1 /* Fixed-point arithmetic support.
2 Copyright (C) 2006-2014 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"
26 #include "wide-int.h"
28 /* Compare two fixed objects for bitwise identity. */
30 bool
31 fixed_identical (const FIXED_VALUE_TYPE *a, const FIXED_VALUE_TYPE *b)
33 return (a->mode == b->mode
34 && a->data.high == b->data.high
35 && a->data.low == b->data.low);
38 /* Calculate a hash value. */
40 unsigned int
41 fixed_hash (const FIXED_VALUE_TYPE *f)
43 return (unsigned int) (f->data.low ^ f->data.high);
46 /* Define the enum code for the range of the fixed-point value. */
47 enum fixed_value_range_code {
48 FIXED_OK, /* The value is within the range. */
49 FIXED_UNDERFLOW, /* The value is less than the minimum. */
50 FIXED_GT_MAX_EPS, /* The value is greater than the maximum, but not equal
51 to the maximum plus the epsilon. */
52 FIXED_MAX_EPS /* The value equals the maximum plus the epsilon. */
55 /* Check REAL_VALUE against the range of the fixed-point mode.
56 Return FIXED_OK, if it is within the range.
57 FIXED_UNDERFLOW, if it is less than the minimum.
58 FIXED_GT_MAX_EPS, if it is greater than the maximum, but not equal to
59 the maximum plus the epsilon.
60 FIXED_MAX_EPS, if it is equal to the maximum plus the epsilon. */
62 static enum fixed_value_range_code
63 check_real_for_fixed_mode (REAL_VALUE_TYPE *real_value, machine_mode mode)
65 REAL_VALUE_TYPE max_value, min_value, epsilon_value;
67 real_2expN (&max_value, GET_MODE_IBIT (mode), mode);
68 real_2expN (&epsilon_value, -GET_MODE_FBIT (mode), mode);
70 if (SIGNED_FIXED_POINT_MODE_P (mode))
71 min_value = real_value_negate (&max_value);
72 else
73 real_from_string (&min_value, "0.0");
75 if (real_compare (LT_EXPR, real_value, &min_value))
76 return FIXED_UNDERFLOW;
77 if (real_compare (EQ_EXPR, real_value, &max_value))
78 return FIXED_MAX_EPS;
79 real_arithmetic (&max_value, MINUS_EXPR, &max_value, &epsilon_value);
80 if (real_compare (GT_EXPR, real_value, &max_value))
81 return FIXED_GT_MAX_EPS;
82 return FIXED_OK;
86 /* Construct a CONST_FIXED from a bit payload and machine mode MODE.
87 The bits in PAYLOAD are sign-extended/zero-extended according to MODE. */
89 FIXED_VALUE_TYPE
90 fixed_from_double_int (double_int payload, machine_mode mode)
92 FIXED_VALUE_TYPE value;
94 gcc_assert (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_DOUBLE_INT);
96 if (SIGNED_SCALAR_FIXED_POINT_MODE_P (mode))
97 value.data = payload.sext (1 + GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode));
98 else if (UNSIGNED_SCALAR_FIXED_POINT_MODE_P (mode))
99 value.data = payload.zext (GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode));
100 else
101 gcc_unreachable ();
103 value.mode = mode;
105 return value;
109 /* Initialize from a decimal or hexadecimal string. */
111 void
112 fixed_from_string (FIXED_VALUE_TYPE *f, const char *str, machine_mode mode)
114 REAL_VALUE_TYPE real_value, fixed_value, base_value;
115 unsigned int fbit;
116 enum fixed_value_range_code temp;
117 bool fail;
119 f->mode = mode;
120 fbit = GET_MODE_FBIT (mode);
122 real_from_string (&real_value, str);
123 temp = check_real_for_fixed_mode (&real_value, f->mode);
124 /* We don't want to warn the case when the _Fract value is 1.0. */
125 if (temp == FIXED_UNDERFLOW
126 || temp == FIXED_GT_MAX_EPS
127 || (temp == FIXED_MAX_EPS && ALL_ACCUM_MODE_P (f->mode)))
128 warning (OPT_Woverflow,
129 "large fixed-point constant implicitly truncated to fixed-point type");
130 real_2expN (&base_value, fbit, mode);
131 real_arithmetic (&fixed_value, MULT_EXPR, &real_value, &base_value);
132 wide_int w = real_to_integer (&fixed_value, &fail,
133 GET_MODE_PRECISION (mode));
134 f->data.low = w.elt (0);
135 f->data.high = w.elt (1);
137 if (temp == FIXED_MAX_EPS && ALL_FRACT_MODE_P (f->mode))
139 /* From the spec, we need to evaluate 1 to the maximal value. */
140 f->data.low = -1;
141 f->data.high = -1;
142 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
143 + GET_MODE_IBIT (f->mode));
145 else
146 f->data = f->data.ext (SIGNED_FIXED_POINT_MODE_P (f->mode)
147 + GET_MODE_FBIT (f->mode)
148 + GET_MODE_IBIT (f->mode),
149 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
152 /* Render F as a decimal floating point constant. */
154 void
155 fixed_to_decimal (char *str, const FIXED_VALUE_TYPE *f_orig,
156 size_t buf_size)
158 REAL_VALUE_TYPE real_value, base_value, fixed_value;
160 signop sgn = UNSIGNED_FIXED_POINT_MODE_P (f_orig->mode) ? UNSIGNED : SIGNED;
161 real_2expN (&base_value, GET_MODE_FBIT (f_orig->mode), f_orig->mode);
162 real_from_integer (&real_value, VOIDmode,
163 wide_int::from (f_orig->data,
164 GET_MODE_PRECISION (f_orig->mode), sgn),
165 sgn);
166 real_arithmetic (&fixed_value, RDIV_EXPR, &real_value, &base_value);
167 real_to_decimal (str, &fixed_value, buf_size, 0, 1);
170 /* If SAT_P, saturate A to the maximum or the minimum, and save to *F based on
171 the machine mode MODE.
172 Do not modify *F otherwise.
173 This function assumes the width of double_int is greater than the width
174 of the fixed-point value (the sum of a possible sign bit, possible ibits,
175 and fbits).
176 Return true, if !SAT_P and overflow. */
178 static bool
179 fixed_saturate1 (machine_mode mode, double_int a, double_int *f,
180 bool sat_p)
182 bool overflow_p = false;
183 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
184 int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
186 if (unsigned_p) /* Unsigned type. */
188 double_int max;
189 max.low = -1;
190 max.high = -1;
191 max = max.zext (i_f_bits);
192 if (a.ugt (max))
194 if (sat_p)
195 *f = max;
196 else
197 overflow_p = true;
200 else /* Signed type. */
202 double_int max, min;
203 max.high = -1;
204 max.low = -1;
205 max = max.zext (i_f_bits);
206 min.high = 0;
207 min.low = 1;
208 min = min.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
209 min = min.sext (1 + i_f_bits);
210 if (a.sgt (max))
212 if (sat_p)
213 *f = max;
214 else
215 overflow_p = true;
217 else if (a.slt (min))
219 if (sat_p)
220 *f = min;
221 else
222 overflow_p = true;
225 return overflow_p;
228 /* If SAT_P, saturate {A_HIGH, A_LOW} to the maximum or the minimum, and
229 save to *F based on the machine mode MODE.
230 Do not modify *F otherwise.
231 This function assumes the width of two double_int is greater than the width
232 of the fixed-point value (the sum of a possible sign bit, possible ibits,
233 and fbits).
234 Return true, if !SAT_P and overflow. */
236 static bool
237 fixed_saturate2 (machine_mode mode, double_int a_high, double_int a_low,
238 double_int *f, bool sat_p)
240 bool overflow_p = false;
241 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
242 int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
244 if (unsigned_p) /* Unsigned type. */
246 double_int max_r, max_s;
247 max_r.high = 0;
248 max_r.low = 0;
249 max_s.high = -1;
250 max_s.low = -1;
251 max_s = max_s.zext (i_f_bits);
252 if (a_high.ugt (max_r)
253 || (a_high == max_r &&
254 a_low.ugt (max_s)))
256 if (sat_p)
257 *f = max_s;
258 else
259 overflow_p = true;
262 else /* Signed type. */
264 double_int max_r, max_s, min_r, min_s;
265 max_r.high = 0;
266 max_r.low = 0;
267 max_s.high = -1;
268 max_s.low = -1;
269 max_s = max_s.zext (i_f_bits);
270 min_r.high = -1;
271 min_r.low = -1;
272 min_s.high = 0;
273 min_s.low = 1;
274 min_s = min_s.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
275 min_s = min_s.sext (1 + i_f_bits);
276 if (a_high.sgt (max_r)
277 || (a_high == max_r &&
278 a_low.ugt (max_s)))
280 if (sat_p)
281 *f = max_s;
282 else
283 overflow_p = true;
285 else if (a_high.slt (min_r)
286 || (a_high == min_r &&
287 a_low.ult (min_s)))
289 if (sat_p)
290 *f = min_s;
291 else
292 overflow_p = true;
295 return overflow_p;
298 /* Return the sign bit based on I_F_BITS. */
300 static inline int
301 get_fixed_sign_bit (double_int a, int i_f_bits)
303 if (i_f_bits < HOST_BITS_PER_WIDE_INT)
304 return (a.low >> i_f_bits) & 1;
305 else
306 return (a.high >> (i_f_bits - HOST_BITS_PER_WIDE_INT)) & 1;
309 /* Calculate F = A + (SUBTRACT_P ? -B : B).
310 If SAT_P, saturate the result to the max or the min.
311 Return true, if !SAT_P and overflow. */
313 static bool
314 do_fixed_add (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
315 const FIXED_VALUE_TYPE *b, bool subtract_p, bool sat_p)
317 bool overflow_p = false;
318 bool unsigned_p;
319 double_int temp;
320 int i_f_bits;
322 /* This was a conditional expression but it triggered a bug in
323 Sun C 5.5. */
324 if (subtract_p)
325 temp = -b->data;
326 else
327 temp = b->data;
329 unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
330 i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
331 f->mode = a->mode;
332 f->data = a->data + temp;
333 if (unsigned_p) /* Unsigned type. */
335 if (subtract_p) /* Unsigned subtraction. */
337 if (a->data.ult (b->data))
339 if (sat_p)
341 f->data.high = 0;
342 f->data.low = 0;
344 else
345 overflow_p = true;
348 else /* Unsigned addition. */
350 f->data = f->data.zext (i_f_bits);
351 if (f->data.ult (a->data)
352 || f->data.ult (b->data))
354 if (sat_p)
356 f->data.high = -1;
357 f->data.low = -1;
359 else
360 overflow_p = true;
364 else /* Signed type. */
366 if ((!subtract_p
367 && (get_fixed_sign_bit (a->data, i_f_bits)
368 == get_fixed_sign_bit (b->data, i_f_bits))
369 && (get_fixed_sign_bit (a->data, i_f_bits)
370 != get_fixed_sign_bit (f->data, i_f_bits)))
371 || (subtract_p
372 && (get_fixed_sign_bit (a->data, i_f_bits)
373 != get_fixed_sign_bit (b->data, i_f_bits))
374 && (get_fixed_sign_bit (a->data, i_f_bits)
375 != get_fixed_sign_bit (f->data, i_f_bits))))
377 if (sat_p)
379 f->data.low = 1;
380 f->data.high = 0;
381 f->data = f->data.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
382 if (get_fixed_sign_bit (a->data, i_f_bits) == 0)
384 --f->data;
387 else
388 overflow_p = true;
391 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
392 return overflow_p;
395 /* Calculate F = A * B.
396 If SAT_P, saturate the result to the max or the min.
397 Return true, if !SAT_P and overflow. */
399 static bool
400 do_fixed_multiply (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
401 const FIXED_VALUE_TYPE *b, bool sat_p)
403 bool overflow_p = false;
404 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
405 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
406 f->mode = a->mode;
407 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT)
409 f->data = a->data * b->data;
410 f->data = f->data.lshift (-GET_MODE_FBIT (f->mode),
411 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
412 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
414 else
416 /* The result of multiplication expands to two double_int. */
417 double_int a_high, a_low, b_high, b_low;
418 double_int high_high, high_low, low_high, low_low;
419 double_int r, s, temp1, temp2;
420 int carry = 0;
422 /* Decompose a and b to four double_int. */
423 a_high.low = a->data.high;
424 a_high.high = 0;
425 a_low.low = a->data.low;
426 a_low.high = 0;
427 b_high.low = b->data.high;
428 b_high.high = 0;
429 b_low.low = b->data.low;
430 b_low.high = 0;
432 /* Perform four multiplications. */
433 low_low = a_low * b_low;
434 low_high = a_low * b_high;
435 high_low = a_high * b_low;
436 high_high = a_high * b_high;
438 /* Accumulate four results to {r, s}. */
439 temp1.high = high_low.low;
440 temp1.low = 0;
441 s = low_low + temp1;
442 if (s.ult (low_low)
443 || s.ult (temp1))
444 carry ++; /* Carry */
445 temp1.high = s.high;
446 temp1.low = s.low;
447 temp2.high = low_high.low;
448 temp2.low = 0;
449 s = temp1 + temp2;
450 if (s.ult (temp1)
451 || s.ult (temp2))
452 carry ++; /* Carry */
454 temp1.low = high_low.high;
455 temp1.high = 0;
456 r = high_high + temp1;
457 temp1.low = low_high.high;
458 temp1.high = 0;
459 r += temp1;
460 temp1.low = carry;
461 temp1.high = 0;
462 r += temp1;
464 /* We need to subtract b from r, if a < 0. */
465 if (!unsigned_p && a->data.high < 0)
466 r -= b->data;
467 /* We need to subtract a from r, if b < 0. */
468 if (!unsigned_p && b->data.high < 0)
469 r -= a->data;
471 /* Shift right the result by FBIT. */
472 if (GET_MODE_FBIT (f->mode) == HOST_BITS_PER_DOUBLE_INT)
474 s.low = r.low;
475 s.high = r.high;
476 if (unsigned_p)
478 r.low = 0;
479 r.high = 0;
481 else
483 r.low = -1;
484 r.high = -1;
486 f->data.low = s.low;
487 f->data.high = s.high;
489 else
491 s = s.llshift ((-GET_MODE_FBIT (f->mode)), HOST_BITS_PER_DOUBLE_INT);
492 f->data = r.llshift ((HOST_BITS_PER_DOUBLE_INT
493 - GET_MODE_FBIT (f->mode)),
494 HOST_BITS_PER_DOUBLE_INT);
495 f->data.low = f->data.low | s.low;
496 f->data.high = f->data.high | s.high;
497 s.low = f->data.low;
498 s.high = f->data.high;
499 r = r.lshift (-GET_MODE_FBIT (f->mode),
500 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
503 overflow_p = fixed_saturate2 (f->mode, r, s, &f->data, sat_p);
506 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
507 return overflow_p;
510 /* Calculate F = A / B.
511 If SAT_P, saturate the result to the max or the min.
512 Return true, if !SAT_P and overflow. */
514 static bool
515 do_fixed_divide (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
516 const FIXED_VALUE_TYPE *b, bool sat_p)
518 bool overflow_p = false;
519 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
520 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
521 f->mode = a->mode;
522 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT)
524 f->data = a->data.lshift (GET_MODE_FBIT (f->mode),
525 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
526 f->data = f->data.div (b->data, unsigned_p, TRUNC_DIV_EXPR);
527 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
529 else
531 double_int pos_a, pos_b, r, s;
532 double_int quo_r, quo_s, mod, temp;
533 int num_of_neg = 0;
534 int i;
536 /* If a < 0, negate a. */
537 if (!unsigned_p && a->data.high < 0)
539 pos_a = -a->data;
540 num_of_neg ++;
542 else
543 pos_a = a->data;
545 /* If b < 0, negate b. */
546 if (!unsigned_p && b->data.high < 0)
548 pos_b = -b->data;
549 num_of_neg ++;
551 else
552 pos_b = b->data;
554 /* Left shift pos_a to {r, s} by FBIT. */
555 if (GET_MODE_FBIT (f->mode) == HOST_BITS_PER_DOUBLE_INT)
557 r = pos_a;
558 s.high = 0;
559 s.low = 0;
561 else
563 s = pos_a.llshift (GET_MODE_FBIT (f->mode), HOST_BITS_PER_DOUBLE_INT);
564 r = pos_a.llshift (- (HOST_BITS_PER_DOUBLE_INT
565 - GET_MODE_FBIT (f->mode)),
566 HOST_BITS_PER_DOUBLE_INT);
569 /* Divide r by pos_b to quo_r. The remainder is in mod. */
570 quo_r = r.divmod (pos_b, 1, TRUNC_DIV_EXPR, &mod);
571 quo_s = double_int_zero;
573 for (i = 0; i < HOST_BITS_PER_DOUBLE_INT; i++)
575 /* Record the leftmost bit of mod. */
576 int leftmost_mod = (mod.high < 0);
578 /* Shift left mod by 1 bit. */
579 mod = mod.lshift (1);
581 /* Test the leftmost bit of s to add to mod. */
582 if (s.high < 0)
583 mod.low += 1;
585 /* Shift left quo_s by 1 bit. */
586 quo_s = quo_s.lshift (1);
588 /* Try to calculate (mod - pos_b). */
589 temp = mod - pos_b;
591 if (leftmost_mod == 1 || mod.ucmp (pos_b) != -1)
593 quo_s.low += 1;
594 mod = temp;
597 /* Shift left s by 1 bit. */
598 s = s.lshift (1);
602 if (num_of_neg == 1)
604 quo_s = -quo_s;
605 if (quo_s.high == 0 && quo_s.low == 0)
606 quo_r = -quo_r;
607 else
609 quo_r.low = ~quo_r.low;
610 quo_r.high = ~quo_r.high;
614 f->data = quo_s;
615 overflow_p = fixed_saturate2 (f->mode, quo_r, quo_s, &f->data, sat_p);
618 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
619 return overflow_p;
622 /* Calculate F = A << B if LEFT_P. Otherwise, F = A >> B.
623 If SAT_P, saturate the result to the max or the min.
624 Return true, if !SAT_P and overflow. */
626 static bool
627 do_fixed_shift (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
628 const FIXED_VALUE_TYPE *b, bool left_p, bool sat_p)
630 bool overflow_p = false;
631 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
632 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
633 f->mode = a->mode;
635 if (b->data.low == 0)
637 f->data = a->data;
638 return overflow_p;
641 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT || (!left_p))
643 f->data = a->data.lshift (left_p ? b->data.low : -b->data.low,
644 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
645 if (left_p) /* Only left shift saturates. */
646 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
648 else /* We need two double_int to store the left-shift result. */
650 double_int temp_high, temp_low;
651 if (b->data.low == HOST_BITS_PER_DOUBLE_INT)
653 temp_high = a->data;
654 temp_low.high = 0;
655 temp_low.low = 0;
657 else
659 temp_low = a->data.lshift (b->data.low,
660 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
661 /* Logical shift right to temp_high. */
662 temp_high = a->data.llshift (b->data.low - HOST_BITS_PER_DOUBLE_INT,
663 HOST_BITS_PER_DOUBLE_INT);
665 if (!unsigned_p && a->data.high < 0) /* Signed-extend temp_high. */
666 temp_high = temp_high.ext (b->data.low, unsigned_p);
667 f->data = temp_low;
668 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
669 sat_p);
671 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
672 return overflow_p;
675 /* Calculate F = -A.
676 If SAT_P, saturate the result to the max or the min.
677 Return true, if !SAT_P and overflow. */
679 static bool
680 do_fixed_neg (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a, bool sat_p)
682 bool overflow_p = false;
683 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
684 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
685 f->mode = a->mode;
686 f->data = -a->data;
687 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
689 if (unsigned_p) /* Unsigned type. */
691 if (f->data.low != 0 || f->data.high != 0)
693 if (sat_p)
695 f->data.low = 0;
696 f->data.high = 0;
698 else
699 overflow_p = true;
702 else /* Signed type. */
704 if (!(f->data.high == 0 && f->data.low == 0)
705 && f->data.high == a->data.high && f->data.low == a->data.low )
707 if (sat_p)
709 /* Saturate to the maximum by subtracting f->data by one. */
710 f->data.low = -1;
711 f->data.high = -1;
712 f->data = f->data.zext (i_f_bits);
714 else
715 overflow_p = true;
718 return overflow_p;
721 /* Perform the binary or unary operation described by CODE.
722 Note that OP0 and OP1 must have the same mode for binary operators.
723 For a unary operation, leave OP1 NULL.
724 Return true, if !SAT_P and overflow. */
726 bool
727 fixed_arithmetic (FIXED_VALUE_TYPE *f, int icode, const FIXED_VALUE_TYPE *op0,
728 const FIXED_VALUE_TYPE *op1, bool sat_p)
730 switch (icode)
732 case NEGATE_EXPR:
733 return do_fixed_neg (f, op0, sat_p);
734 break;
736 case PLUS_EXPR:
737 gcc_assert (op0->mode == op1->mode);
738 return do_fixed_add (f, op0, op1, false, sat_p);
739 break;
741 case MINUS_EXPR:
742 gcc_assert (op0->mode == op1->mode);
743 return do_fixed_add (f, op0, op1, true, sat_p);
744 break;
746 case MULT_EXPR:
747 gcc_assert (op0->mode == op1->mode);
748 return do_fixed_multiply (f, op0, op1, sat_p);
749 break;
751 case TRUNC_DIV_EXPR:
752 gcc_assert (op0->mode == op1->mode);
753 return do_fixed_divide (f, op0, op1, sat_p);
754 break;
756 case LSHIFT_EXPR:
757 return do_fixed_shift (f, op0, op1, true, sat_p);
758 break;
760 case RSHIFT_EXPR:
761 return do_fixed_shift (f, op0, op1, false, sat_p);
762 break;
764 default:
765 gcc_unreachable ();
767 return false;
770 /* Compare fixed-point values by tree_code.
771 Note that OP0 and OP1 must have the same mode. */
773 bool
774 fixed_compare (int icode, const FIXED_VALUE_TYPE *op0,
775 const FIXED_VALUE_TYPE *op1)
777 enum tree_code code = (enum tree_code) icode;
778 gcc_assert (op0->mode == op1->mode);
780 switch (code)
782 case NE_EXPR:
783 return op0->data != op1->data;
785 case EQ_EXPR:
786 return op0->data == op1->data;
788 case LT_EXPR:
789 return op0->data.cmp (op1->data,
790 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) == -1;
792 case LE_EXPR:
793 return op0->data.cmp (op1->data,
794 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) != 1;
796 case GT_EXPR:
797 return op0->data.cmp (op1->data,
798 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) == 1;
800 case GE_EXPR:
801 return op0->data.cmp (op1->data,
802 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) != -1;
804 default:
805 gcc_unreachable ();
809 /* Extend or truncate to a new mode.
810 If SAT_P, saturate the result to the max or the min.
811 Return true, if !SAT_P and overflow. */
813 bool
814 fixed_convert (FIXED_VALUE_TYPE *f, machine_mode mode,
815 const FIXED_VALUE_TYPE *a, bool sat_p)
817 bool overflow_p = false;
818 if (mode == a->mode)
820 *f = *a;
821 return overflow_p;
824 if (GET_MODE_FBIT (mode) > GET_MODE_FBIT (a->mode))
826 /* Left shift a to temp_high, temp_low based on a->mode. */
827 double_int temp_high, temp_low;
828 int amount = GET_MODE_FBIT (mode) - GET_MODE_FBIT (a->mode);
829 temp_low = a->data.lshift (amount,
830 HOST_BITS_PER_DOUBLE_INT,
831 SIGNED_FIXED_POINT_MODE_P (a->mode));
832 /* Logical shift right to temp_high. */
833 temp_high = a->data.llshift (amount - HOST_BITS_PER_DOUBLE_INT,
834 HOST_BITS_PER_DOUBLE_INT);
835 if (SIGNED_FIXED_POINT_MODE_P (a->mode)
836 && a->data.high < 0) /* Signed-extend temp_high. */
837 temp_high = temp_high.sext (amount);
838 f->mode = mode;
839 f->data = temp_low;
840 if (SIGNED_FIXED_POINT_MODE_P (a->mode) ==
841 SIGNED_FIXED_POINT_MODE_P (f->mode))
842 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
843 sat_p);
844 else
846 /* Take care of the cases when converting between signed and
847 unsigned. */
848 if (SIGNED_FIXED_POINT_MODE_P (a->mode))
850 /* Signed -> Unsigned. */
851 if (a->data.high < 0)
853 if (sat_p)
855 f->data.low = 0; /* Set to zero. */
856 f->data.high = 0; /* Set to zero. */
858 else
859 overflow_p = true;
861 else
862 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
863 &f->data, sat_p);
865 else
867 /* Unsigned -> Signed. */
868 if (temp_high.high < 0)
870 if (sat_p)
872 /* Set to maximum. */
873 f->data.low = -1; /* Set to all ones. */
874 f->data.high = -1; /* Set to all ones. */
875 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
876 + GET_MODE_IBIT (f->mode));
877 /* Clear the sign. */
879 else
880 overflow_p = true;
882 else
883 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
884 &f->data, sat_p);
888 else
890 /* Right shift a to temp based on a->mode. */
891 double_int temp;
892 temp = a->data.lshift (GET_MODE_FBIT (mode) - GET_MODE_FBIT (a->mode),
893 HOST_BITS_PER_DOUBLE_INT,
894 SIGNED_FIXED_POINT_MODE_P (a->mode));
895 f->mode = mode;
896 f->data = temp;
897 if (SIGNED_FIXED_POINT_MODE_P (a->mode) ==
898 SIGNED_FIXED_POINT_MODE_P (f->mode))
899 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
900 else
902 /* Take care of the cases when converting between signed and
903 unsigned. */
904 if (SIGNED_FIXED_POINT_MODE_P (a->mode))
906 /* Signed -> Unsigned. */
907 if (a->data.high < 0)
909 if (sat_p)
911 f->data.low = 0; /* Set to zero. */
912 f->data.high = 0; /* Set to zero. */
914 else
915 overflow_p = true;
917 else
918 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data,
919 sat_p);
921 else
923 /* Unsigned -> Signed. */
924 if (temp.high < 0)
926 if (sat_p)
928 /* Set to maximum. */
929 f->data.low = -1; /* Set to all ones. */
930 f->data.high = -1; /* Set to all ones. */
931 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
932 + GET_MODE_IBIT (f->mode));
933 /* Clear the sign. */
935 else
936 overflow_p = true;
938 else
939 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data,
940 sat_p);
945 f->data = f->data.ext (SIGNED_FIXED_POINT_MODE_P (f->mode)
946 + GET_MODE_FBIT (f->mode)
947 + GET_MODE_IBIT (f->mode),
948 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
949 return overflow_p;
952 /* Convert to a new fixed-point mode from an integer.
953 If UNSIGNED_P, this integer is unsigned.
954 If SAT_P, saturate the result to the max or the min.
955 Return true, if !SAT_P and overflow. */
957 bool
958 fixed_convert_from_int (FIXED_VALUE_TYPE *f, machine_mode mode,
959 double_int a, bool unsigned_p, bool sat_p)
961 bool overflow_p = false;
962 /* Left shift a to temp_high, temp_low. */
963 double_int temp_high, temp_low;
964 int amount = GET_MODE_FBIT (mode);
965 if (amount == HOST_BITS_PER_DOUBLE_INT)
967 temp_high = a;
968 temp_low.low = 0;
969 temp_low.high = 0;
971 else
973 temp_low = a.llshift (amount, HOST_BITS_PER_DOUBLE_INT);
975 /* Logical shift right to temp_high. */
976 temp_high = a.llshift (amount - HOST_BITS_PER_DOUBLE_INT,
977 HOST_BITS_PER_DOUBLE_INT);
979 if (!unsigned_p && a.high < 0) /* Signed-extend temp_high. */
980 temp_high = temp_high.sext (amount);
982 f->mode = mode;
983 f->data = temp_low;
985 if (unsigned_p == UNSIGNED_FIXED_POINT_MODE_P (f->mode))
986 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
987 sat_p);
988 else
990 /* Take care of the cases when converting between signed and unsigned. */
991 if (!unsigned_p)
993 /* Signed -> Unsigned. */
994 if (a.high < 0)
996 if (sat_p)
998 f->data.low = 0; /* Set to zero. */
999 f->data.high = 0; /* Set to zero. */
1001 else
1002 overflow_p = true;
1004 else
1005 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
1006 &f->data, sat_p);
1008 else
1010 /* Unsigned -> Signed. */
1011 if (temp_high.high < 0)
1013 if (sat_p)
1015 /* Set to maximum. */
1016 f->data.low = -1; /* Set to all ones. */
1017 f->data.high = -1; /* Set to all ones. */
1018 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
1019 + GET_MODE_IBIT (f->mode));
1020 /* Clear the sign. */
1022 else
1023 overflow_p = true;
1025 else
1026 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
1027 &f->data, sat_p);
1030 f->data = f->data.ext (SIGNED_FIXED_POINT_MODE_P (f->mode)
1031 + GET_MODE_FBIT (f->mode)
1032 + GET_MODE_IBIT (f->mode),
1033 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
1034 return overflow_p;
1037 /* Convert to a new fixed-point mode from a real.
1038 If SAT_P, saturate the result to the max or the min.
1039 Return true, if !SAT_P and overflow. */
1041 bool
1042 fixed_convert_from_real (FIXED_VALUE_TYPE *f, machine_mode mode,
1043 const REAL_VALUE_TYPE *a, bool sat_p)
1045 bool overflow_p = false;
1046 REAL_VALUE_TYPE real_value, fixed_value, base_value;
1047 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
1048 int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
1049 unsigned int fbit = GET_MODE_FBIT (mode);
1050 enum fixed_value_range_code temp;
1051 bool fail;
1053 real_value = *a;
1054 f->mode = mode;
1055 real_2expN (&base_value, fbit, mode);
1056 real_arithmetic (&fixed_value, MULT_EXPR, &real_value, &base_value);
1058 wide_int w = real_to_integer (&fixed_value, &fail,
1059 GET_MODE_PRECISION (mode));
1060 f->data.low = w.elt (0);
1061 f->data.high = w.elt (1);
1062 temp = check_real_for_fixed_mode (&real_value, mode);
1063 if (temp == FIXED_UNDERFLOW) /* Minimum. */
1065 if (sat_p)
1067 if (unsigned_p)
1069 f->data.low = 0;
1070 f->data.high = 0;
1072 else
1074 f->data.low = 1;
1075 f->data.high = 0;
1076 f->data = f->data.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
1077 f->data = f->data.sext (1 + i_f_bits);
1080 else
1081 overflow_p = true;
1083 else if (temp == FIXED_GT_MAX_EPS || temp == FIXED_MAX_EPS) /* Maximum. */
1085 if (sat_p)
1087 f->data.low = -1;
1088 f->data.high = -1;
1089 f->data = f->data.zext (i_f_bits);
1091 else
1092 overflow_p = true;
1094 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
1095 return overflow_p;
1098 /* Convert to a new real mode from a fixed-point. */
1100 void
1101 real_convert_from_fixed (REAL_VALUE_TYPE *r, machine_mode mode,
1102 const FIXED_VALUE_TYPE *f)
1104 REAL_VALUE_TYPE base_value, fixed_value, real_value;
1106 signop sgn = UNSIGNED_FIXED_POINT_MODE_P (f->mode) ? UNSIGNED : SIGNED;
1107 real_2expN (&base_value, GET_MODE_FBIT (f->mode), f->mode);
1108 real_from_integer (&fixed_value, VOIDmode,
1109 wide_int::from (f->data, GET_MODE_PRECISION (f->mode),
1110 sgn), sgn);
1111 real_arithmetic (&real_value, RDIV_EXPR, &fixed_value, &base_value);
1112 real_convert (r, mode, &real_value);
1115 /* Determine whether a fixed-point value F is negative. */
1117 bool
1118 fixed_isneg (const FIXED_VALUE_TYPE *f)
1120 if (SIGNED_FIXED_POINT_MODE_P (f->mode))
1122 int i_f_bits = GET_MODE_IBIT (f->mode) + GET_MODE_FBIT (f->mode);
1123 int sign_bit = get_fixed_sign_bit (f->data, i_f_bits);
1124 if (sign_bit == 1)
1125 return true;
1128 return false;