2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / fixed-value.c
blob6c15ede9d74e3520d11a34966d4105f836dead0e
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 "input.h"
25 #include "alias.h"
26 #include "symtab.h"
27 #include "tree.h"
28 #include "diagnostic-core.h"
30 /* Compare two fixed objects for bitwise identity. */
32 bool
33 fixed_identical (const FIXED_VALUE_TYPE *a, const FIXED_VALUE_TYPE *b)
35 return (a->mode == b->mode
36 && a->data.high == b->data.high
37 && a->data.low == b->data.low);
40 /* Calculate a hash value. */
42 unsigned int
43 fixed_hash (const FIXED_VALUE_TYPE *f)
45 return (unsigned int) (f->data.low ^ f->data.high);
48 /* Define the enum code for the range of the fixed-point value. */
49 enum fixed_value_range_code {
50 FIXED_OK, /* The value is within the range. */
51 FIXED_UNDERFLOW, /* The value is less than the minimum. */
52 FIXED_GT_MAX_EPS, /* The value is greater than the maximum, but not equal
53 to the maximum plus the epsilon. */
54 FIXED_MAX_EPS /* The value equals the maximum plus the epsilon. */
57 /* Check REAL_VALUE against the range of the fixed-point mode.
58 Return FIXED_OK, if it is within the range.
59 FIXED_UNDERFLOW, if it is less than the minimum.
60 FIXED_GT_MAX_EPS, if it is greater than the maximum, but not equal to
61 the maximum plus the epsilon.
62 FIXED_MAX_EPS, if it is equal to the maximum plus the epsilon. */
64 static enum fixed_value_range_code
65 check_real_for_fixed_mode (REAL_VALUE_TYPE *real_value, machine_mode mode)
67 REAL_VALUE_TYPE max_value, min_value, epsilon_value;
69 real_2expN (&max_value, GET_MODE_IBIT (mode), mode);
70 real_2expN (&epsilon_value, -GET_MODE_FBIT (mode), mode);
72 if (SIGNED_FIXED_POINT_MODE_P (mode))
73 min_value = real_value_negate (&max_value);
74 else
75 real_from_string (&min_value, "0.0");
77 if (real_compare (LT_EXPR, real_value, &min_value))
78 return FIXED_UNDERFLOW;
79 if (real_compare (EQ_EXPR, real_value, &max_value))
80 return FIXED_MAX_EPS;
81 real_arithmetic (&max_value, MINUS_EXPR, &max_value, &epsilon_value);
82 if (real_compare (GT_EXPR, real_value, &max_value))
83 return FIXED_GT_MAX_EPS;
84 return FIXED_OK;
88 /* Construct a CONST_FIXED from a bit payload and machine mode MODE.
89 The bits in PAYLOAD are sign-extended/zero-extended according to MODE. */
91 FIXED_VALUE_TYPE
92 fixed_from_double_int (double_int payload, machine_mode mode)
94 FIXED_VALUE_TYPE value;
96 gcc_assert (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_DOUBLE_INT);
98 if (SIGNED_SCALAR_FIXED_POINT_MODE_P (mode))
99 value.data = payload.sext (1 + GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode));
100 else if (UNSIGNED_SCALAR_FIXED_POINT_MODE_P (mode))
101 value.data = payload.zext (GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode));
102 else
103 gcc_unreachable ();
105 value.mode = mode;
107 return value;
111 /* Initialize from a decimal or hexadecimal string. */
113 void
114 fixed_from_string (FIXED_VALUE_TYPE *f, const char *str, machine_mode mode)
116 REAL_VALUE_TYPE real_value, fixed_value, base_value;
117 unsigned int fbit;
118 enum fixed_value_range_code temp;
119 bool fail;
121 f->mode = mode;
122 fbit = GET_MODE_FBIT (mode);
124 real_from_string (&real_value, str);
125 temp = check_real_for_fixed_mode (&real_value, f->mode);
126 /* We don't want to warn the case when the _Fract value is 1.0. */
127 if (temp == FIXED_UNDERFLOW
128 || temp == FIXED_GT_MAX_EPS
129 || (temp == FIXED_MAX_EPS && ALL_ACCUM_MODE_P (f->mode)))
130 warning (OPT_Woverflow,
131 "large fixed-point constant implicitly truncated to fixed-point type");
132 real_2expN (&base_value, fbit, mode);
133 real_arithmetic (&fixed_value, MULT_EXPR, &real_value, &base_value);
134 wide_int w = real_to_integer (&fixed_value, &fail,
135 GET_MODE_PRECISION (mode));
136 f->data.low = w.elt (0);
137 f->data.high = w.elt (1);
139 if (temp == FIXED_MAX_EPS && ALL_FRACT_MODE_P (f->mode))
141 /* From the spec, we need to evaluate 1 to the maximal value. */
142 f->data.low = -1;
143 f->data.high = -1;
144 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
145 + GET_MODE_IBIT (f->mode));
147 else
148 f->data = f->data.ext (SIGNED_FIXED_POINT_MODE_P (f->mode)
149 + GET_MODE_FBIT (f->mode)
150 + GET_MODE_IBIT (f->mode),
151 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
154 /* Render F as a decimal floating point constant. */
156 void
157 fixed_to_decimal (char *str, const FIXED_VALUE_TYPE *f_orig,
158 size_t buf_size)
160 REAL_VALUE_TYPE real_value, base_value, fixed_value;
162 signop sgn = UNSIGNED_FIXED_POINT_MODE_P (f_orig->mode) ? UNSIGNED : SIGNED;
163 real_2expN (&base_value, GET_MODE_FBIT (f_orig->mode), f_orig->mode);
164 real_from_integer (&real_value, VOIDmode,
165 wide_int::from (f_orig->data,
166 GET_MODE_PRECISION (f_orig->mode), sgn),
167 sgn);
168 real_arithmetic (&fixed_value, RDIV_EXPR, &real_value, &base_value);
169 real_to_decimal (str, &fixed_value, buf_size, 0, 1);
172 /* If SAT_P, saturate A to the maximum or the minimum, and save to *F based on
173 the machine mode MODE.
174 Do not modify *F otherwise.
175 This function assumes the width of double_int is greater than the width
176 of the fixed-point value (the sum of a possible sign bit, possible ibits,
177 and fbits).
178 Return true, if !SAT_P and overflow. */
180 static bool
181 fixed_saturate1 (machine_mode mode, double_int a, double_int *f,
182 bool sat_p)
184 bool overflow_p = false;
185 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
186 int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
188 if (unsigned_p) /* Unsigned type. */
190 double_int max;
191 max.low = -1;
192 max.high = -1;
193 max = max.zext (i_f_bits);
194 if (a.ugt (max))
196 if (sat_p)
197 *f = max;
198 else
199 overflow_p = true;
202 else /* Signed type. */
204 double_int max, min;
205 max.high = -1;
206 max.low = -1;
207 max = max.zext (i_f_bits);
208 min.high = 0;
209 min.low = 1;
210 min = min.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
211 min = min.sext (1 + i_f_bits);
212 if (a.sgt (max))
214 if (sat_p)
215 *f = max;
216 else
217 overflow_p = true;
219 else if (a.slt (min))
221 if (sat_p)
222 *f = min;
223 else
224 overflow_p = true;
227 return overflow_p;
230 /* If SAT_P, saturate {A_HIGH, A_LOW} to the maximum or the minimum, and
231 save to *F based on the machine mode MODE.
232 Do not modify *F otherwise.
233 This function assumes the width of two double_int is greater than the width
234 of the fixed-point value (the sum of a possible sign bit, possible ibits,
235 and fbits).
236 Return true, if !SAT_P and overflow. */
238 static bool
239 fixed_saturate2 (machine_mode mode, double_int a_high, double_int a_low,
240 double_int *f, bool sat_p)
242 bool overflow_p = false;
243 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
244 int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
246 if (unsigned_p) /* Unsigned type. */
248 double_int max_r, max_s;
249 max_r.high = 0;
250 max_r.low = 0;
251 max_s.high = -1;
252 max_s.low = -1;
253 max_s = max_s.zext (i_f_bits);
254 if (a_high.ugt (max_r)
255 || (a_high == max_r &&
256 a_low.ugt (max_s)))
258 if (sat_p)
259 *f = max_s;
260 else
261 overflow_p = true;
264 else /* Signed type. */
266 double_int max_r, max_s, min_r, min_s;
267 max_r.high = 0;
268 max_r.low = 0;
269 max_s.high = -1;
270 max_s.low = -1;
271 max_s = max_s.zext (i_f_bits);
272 min_r.high = -1;
273 min_r.low = -1;
274 min_s.high = 0;
275 min_s.low = 1;
276 min_s = min_s.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
277 min_s = min_s.sext (1 + i_f_bits);
278 if (a_high.sgt (max_r)
279 || (a_high == max_r &&
280 a_low.ugt (max_s)))
282 if (sat_p)
283 *f = max_s;
284 else
285 overflow_p = true;
287 else if (a_high.slt (min_r)
288 || (a_high == min_r &&
289 a_low.ult (min_s)))
291 if (sat_p)
292 *f = min_s;
293 else
294 overflow_p = true;
297 return overflow_p;
300 /* Return the sign bit based on I_F_BITS. */
302 static inline int
303 get_fixed_sign_bit (double_int a, int i_f_bits)
305 if (i_f_bits < HOST_BITS_PER_WIDE_INT)
306 return (a.low >> i_f_bits) & 1;
307 else
308 return (a.high >> (i_f_bits - HOST_BITS_PER_WIDE_INT)) & 1;
311 /* Calculate F = A + (SUBTRACT_P ? -B : B).
312 If SAT_P, saturate the result to the max or the min.
313 Return true, if !SAT_P and overflow. */
315 static bool
316 do_fixed_add (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
317 const FIXED_VALUE_TYPE *b, bool subtract_p, bool sat_p)
319 bool overflow_p = false;
320 bool unsigned_p;
321 double_int temp;
322 int i_f_bits;
324 /* This was a conditional expression but it triggered a bug in
325 Sun C 5.5. */
326 if (subtract_p)
327 temp = -b->data;
328 else
329 temp = b->data;
331 unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
332 i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
333 f->mode = a->mode;
334 f->data = a->data + temp;
335 if (unsigned_p) /* Unsigned type. */
337 if (subtract_p) /* Unsigned subtraction. */
339 if (a->data.ult (b->data))
341 if (sat_p)
343 f->data.high = 0;
344 f->data.low = 0;
346 else
347 overflow_p = true;
350 else /* Unsigned addition. */
352 f->data = f->data.zext (i_f_bits);
353 if (f->data.ult (a->data)
354 || f->data.ult (b->data))
356 if (sat_p)
358 f->data.high = -1;
359 f->data.low = -1;
361 else
362 overflow_p = true;
366 else /* Signed type. */
368 if ((!subtract_p
369 && (get_fixed_sign_bit (a->data, i_f_bits)
370 == get_fixed_sign_bit (b->data, i_f_bits))
371 && (get_fixed_sign_bit (a->data, i_f_bits)
372 != get_fixed_sign_bit (f->data, i_f_bits)))
373 || (subtract_p
374 && (get_fixed_sign_bit (a->data, i_f_bits)
375 != get_fixed_sign_bit (b->data, i_f_bits))
376 && (get_fixed_sign_bit (a->data, i_f_bits)
377 != get_fixed_sign_bit (f->data, i_f_bits))))
379 if (sat_p)
381 f->data.low = 1;
382 f->data.high = 0;
383 f->data = f->data.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
384 if (get_fixed_sign_bit (a->data, i_f_bits) == 0)
386 --f->data;
389 else
390 overflow_p = true;
393 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
394 return overflow_p;
397 /* Calculate F = A * B.
398 If SAT_P, saturate the result to the max or the min.
399 Return true, if !SAT_P and overflow. */
401 static bool
402 do_fixed_multiply (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
403 const FIXED_VALUE_TYPE *b, bool sat_p)
405 bool overflow_p = false;
406 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
407 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
408 f->mode = a->mode;
409 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT)
411 f->data = a->data * b->data;
412 f->data = f->data.lshift (-GET_MODE_FBIT (f->mode),
413 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
414 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
416 else
418 /* The result of multiplication expands to two double_int. */
419 double_int a_high, a_low, b_high, b_low;
420 double_int high_high, high_low, low_high, low_low;
421 double_int r, s, temp1, temp2;
422 int carry = 0;
424 /* Decompose a and b to four double_int. */
425 a_high.low = a->data.high;
426 a_high.high = 0;
427 a_low.low = a->data.low;
428 a_low.high = 0;
429 b_high.low = b->data.high;
430 b_high.high = 0;
431 b_low.low = b->data.low;
432 b_low.high = 0;
434 /* Perform four multiplications. */
435 low_low = a_low * b_low;
436 low_high = a_low * b_high;
437 high_low = a_high * b_low;
438 high_high = a_high * b_high;
440 /* Accumulate four results to {r, s}. */
441 temp1.high = high_low.low;
442 temp1.low = 0;
443 s = low_low + temp1;
444 if (s.ult (low_low)
445 || s.ult (temp1))
446 carry ++; /* Carry */
447 temp1.high = s.high;
448 temp1.low = s.low;
449 temp2.high = low_high.low;
450 temp2.low = 0;
451 s = temp1 + temp2;
452 if (s.ult (temp1)
453 || s.ult (temp2))
454 carry ++; /* Carry */
456 temp1.low = high_low.high;
457 temp1.high = 0;
458 r = high_high + temp1;
459 temp1.low = low_high.high;
460 temp1.high = 0;
461 r += temp1;
462 temp1.low = carry;
463 temp1.high = 0;
464 r += temp1;
466 /* We need to subtract b from r, if a < 0. */
467 if (!unsigned_p && a->data.high < 0)
468 r -= b->data;
469 /* We need to subtract a from r, if b < 0. */
470 if (!unsigned_p && b->data.high < 0)
471 r -= a->data;
473 /* Shift right the result by FBIT. */
474 if (GET_MODE_FBIT (f->mode) == HOST_BITS_PER_DOUBLE_INT)
476 s.low = r.low;
477 s.high = r.high;
478 if (unsigned_p)
480 r.low = 0;
481 r.high = 0;
483 else
485 r.low = -1;
486 r.high = -1;
488 f->data.low = s.low;
489 f->data.high = s.high;
491 else
493 s = s.llshift ((-GET_MODE_FBIT (f->mode)), HOST_BITS_PER_DOUBLE_INT);
494 f->data = r.llshift ((HOST_BITS_PER_DOUBLE_INT
495 - GET_MODE_FBIT (f->mode)),
496 HOST_BITS_PER_DOUBLE_INT);
497 f->data.low = f->data.low | s.low;
498 f->data.high = f->data.high | s.high;
499 s.low = f->data.low;
500 s.high = f->data.high;
501 r = r.lshift (-GET_MODE_FBIT (f->mode),
502 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
505 overflow_p = fixed_saturate2 (f->mode, r, s, &f->data, sat_p);
508 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
509 return overflow_p;
512 /* Calculate F = A / B.
513 If SAT_P, saturate the result to the max or the min.
514 Return true, if !SAT_P and overflow. */
516 static bool
517 do_fixed_divide (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
518 const FIXED_VALUE_TYPE *b, bool sat_p)
520 bool overflow_p = false;
521 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
522 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
523 f->mode = a->mode;
524 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT)
526 f->data = a->data.lshift (GET_MODE_FBIT (f->mode),
527 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
528 f->data = f->data.div (b->data, unsigned_p, TRUNC_DIV_EXPR);
529 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
531 else
533 double_int pos_a, pos_b, r, s;
534 double_int quo_r, quo_s, mod, temp;
535 int num_of_neg = 0;
536 int i;
538 /* If a < 0, negate a. */
539 if (!unsigned_p && a->data.high < 0)
541 pos_a = -a->data;
542 num_of_neg ++;
544 else
545 pos_a = a->data;
547 /* If b < 0, negate b. */
548 if (!unsigned_p && b->data.high < 0)
550 pos_b = -b->data;
551 num_of_neg ++;
553 else
554 pos_b = b->data;
556 /* Left shift pos_a to {r, s} by FBIT. */
557 if (GET_MODE_FBIT (f->mode) == HOST_BITS_PER_DOUBLE_INT)
559 r = pos_a;
560 s.high = 0;
561 s.low = 0;
563 else
565 s = pos_a.llshift (GET_MODE_FBIT (f->mode), HOST_BITS_PER_DOUBLE_INT);
566 r = pos_a.llshift (- (HOST_BITS_PER_DOUBLE_INT
567 - GET_MODE_FBIT (f->mode)),
568 HOST_BITS_PER_DOUBLE_INT);
571 /* Divide r by pos_b to quo_r. The remainder is in mod. */
572 quo_r = r.divmod (pos_b, 1, TRUNC_DIV_EXPR, &mod);
573 quo_s = double_int_zero;
575 for (i = 0; i < HOST_BITS_PER_DOUBLE_INT; i++)
577 /* Record the leftmost bit of mod. */
578 int leftmost_mod = (mod.high < 0);
580 /* Shift left mod by 1 bit. */
581 mod = mod.lshift (1);
583 /* Test the leftmost bit of s to add to mod. */
584 if (s.high < 0)
585 mod.low += 1;
587 /* Shift left quo_s by 1 bit. */
588 quo_s = quo_s.lshift (1);
590 /* Try to calculate (mod - pos_b). */
591 temp = mod - pos_b;
593 if (leftmost_mod == 1 || mod.ucmp (pos_b) != -1)
595 quo_s.low += 1;
596 mod = temp;
599 /* Shift left s by 1 bit. */
600 s = s.lshift (1);
604 if (num_of_neg == 1)
606 quo_s = -quo_s;
607 if (quo_s.high == 0 && quo_s.low == 0)
608 quo_r = -quo_r;
609 else
611 quo_r.low = ~quo_r.low;
612 quo_r.high = ~quo_r.high;
616 f->data = quo_s;
617 overflow_p = fixed_saturate2 (f->mode, quo_r, quo_s, &f->data, sat_p);
620 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
621 return overflow_p;
624 /* Calculate F = A << B if LEFT_P. Otherwise, F = A >> B.
625 If SAT_P, saturate the result to the max or the min.
626 Return true, if !SAT_P and overflow. */
628 static bool
629 do_fixed_shift (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
630 const FIXED_VALUE_TYPE *b, bool left_p, bool sat_p)
632 bool overflow_p = false;
633 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
634 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
635 f->mode = a->mode;
637 if (b->data.low == 0)
639 f->data = a->data;
640 return overflow_p;
643 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT || (!left_p))
645 f->data = a->data.lshift (left_p ? b->data.low : -b->data.low,
646 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
647 if (left_p) /* Only left shift saturates. */
648 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
650 else /* We need two double_int to store the left-shift result. */
652 double_int temp_high, temp_low;
653 if (b->data.low == HOST_BITS_PER_DOUBLE_INT)
655 temp_high = a->data;
656 temp_low.high = 0;
657 temp_low.low = 0;
659 else
661 temp_low = a->data.lshift (b->data.low,
662 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
663 /* Logical shift right to temp_high. */
664 temp_high = a->data.llshift (b->data.low - HOST_BITS_PER_DOUBLE_INT,
665 HOST_BITS_PER_DOUBLE_INT);
667 if (!unsigned_p && a->data.high < 0) /* Signed-extend temp_high. */
668 temp_high = temp_high.ext (b->data.low, unsigned_p);
669 f->data = temp_low;
670 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
671 sat_p);
673 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
674 return overflow_p;
677 /* Calculate F = -A.
678 If SAT_P, saturate the result to the max or the min.
679 Return true, if !SAT_P and overflow. */
681 static bool
682 do_fixed_neg (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a, bool sat_p)
684 bool overflow_p = false;
685 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
686 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
687 f->mode = a->mode;
688 f->data = -a->data;
689 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
691 if (unsigned_p) /* Unsigned type. */
693 if (f->data.low != 0 || f->data.high != 0)
695 if (sat_p)
697 f->data.low = 0;
698 f->data.high = 0;
700 else
701 overflow_p = true;
704 else /* Signed type. */
706 if (!(f->data.high == 0 && f->data.low == 0)
707 && f->data.high == a->data.high && f->data.low == a->data.low )
709 if (sat_p)
711 /* Saturate to the maximum by subtracting f->data by one. */
712 f->data.low = -1;
713 f->data.high = -1;
714 f->data = f->data.zext (i_f_bits);
716 else
717 overflow_p = true;
720 return overflow_p;
723 /* Perform the binary or unary operation described by CODE.
724 Note that OP0 and OP1 must have the same mode for binary operators.
725 For a unary operation, leave OP1 NULL.
726 Return true, if !SAT_P and overflow. */
728 bool
729 fixed_arithmetic (FIXED_VALUE_TYPE *f, int icode, const FIXED_VALUE_TYPE *op0,
730 const FIXED_VALUE_TYPE *op1, bool sat_p)
732 switch (icode)
734 case NEGATE_EXPR:
735 return do_fixed_neg (f, op0, sat_p);
736 break;
738 case PLUS_EXPR:
739 gcc_assert (op0->mode == op1->mode);
740 return do_fixed_add (f, op0, op1, false, sat_p);
741 break;
743 case MINUS_EXPR:
744 gcc_assert (op0->mode == op1->mode);
745 return do_fixed_add (f, op0, op1, true, sat_p);
746 break;
748 case MULT_EXPR:
749 gcc_assert (op0->mode == op1->mode);
750 return do_fixed_multiply (f, op0, op1, sat_p);
751 break;
753 case TRUNC_DIV_EXPR:
754 gcc_assert (op0->mode == op1->mode);
755 return do_fixed_divide (f, op0, op1, sat_p);
756 break;
758 case LSHIFT_EXPR:
759 return do_fixed_shift (f, op0, op1, true, sat_p);
760 break;
762 case RSHIFT_EXPR:
763 return do_fixed_shift (f, op0, op1, false, sat_p);
764 break;
766 default:
767 gcc_unreachable ();
769 return false;
772 /* Compare fixed-point values by tree_code.
773 Note that OP0 and OP1 must have the same mode. */
775 bool
776 fixed_compare (int icode, const FIXED_VALUE_TYPE *op0,
777 const FIXED_VALUE_TYPE *op1)
779 enum tree_code code = (enum tree_code) icode;
780 gcc_assert (op0->mode == op1->mode);
782 switch (code)
784 case NE_EXPR:
785 return op0->data != op1->data;
787 case EQ_EXPR:
788 return op0->data == op1->data;
790 case LT_EXPR:
791 return op0->data.cmp (op1->data,
792 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) == -1;
794 case LE_EXPR:
795 return op0->data.cmp (op1->data,
796 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) != 1;
798 case GT_EXPR:
799 return op0->data.cmp (op1->data,
800 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) == 1;
802 case GE_EXPR:
803 return op0->data.cmp (op1->data,
804 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) != -1;
806 default:
807 gcc_unreachable ();
811 /* Extend or truncate to a new mode.
812 If SAT_P, saturate the result to the max or the min.
813 Return true, if !SAT_P and overflow. */
815 bool
816 fixed_convert (FIXED_VALUE_TYPE *f, machine_mode mode,
817 const FIXED_VALUE_TYPE *a, bool sat_p)
819 bool overflow_p = false;
820 if (mode == a->mode)
822 *f = *a;
823 return overflow_p;
826 if (GET_MODE_FBIT (mode) > GET_MODE_FBIT (a->mode))
828 /* Left shift a to temp_high, temp_low based on a->mode. */
829 double_int temp_high, temp_low;
830 int amount = GET_MODE_FBIT (mode) - GET_MODE_FBIT (a->mode);
831 temp_low = a->data.lshift (amount,
832 HOST_BITS_PER_DOUBLE_INT,
833 SIGNED_FIXED_POINT_MODE_P (a->mode));
834 /* Logical shift right to temp_high. */
835 temp_high = a->data.llshift (amount - HOST_BITS_PER_DOUBLE_INT,
836 HOST_BITS_PER_DOUBLE_INT);
837 if (SIGNED_FIXED_POINT_MODE_P (a->mode)
838 && a->data.high < 0) /* Signed-extend temp_high. */
839 temp_high = temp_high.sext (amount);
840 f->mode = mode;
841 f->data = temp_low;
842 if (SIGNED_FIXED_POINT_MODE_P (a->mode) ==
843 SIGNED_FIXED_POINT_MODE_P (f->mode))
844 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
845 sat_p);
846 else
848 /* Take care of the cases when converting between signed and
849 unsigned. */
850 if (SIGNED_FIXED_POINT_MODE_P (a->mode))
852 /* Signed -> Unsigned. */
853 if (a->data.high < 0)
855 if (sat_p)
857 f->data.low = 0; /* Set to zero. */
858 f->data.high = 0; /* Set to zero. */
860 else
861 overflow_p = true;
863 else
864 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
865 &f->data, sat_p);
867 else
869 /* Unsigned -> Signed. */
870 if (temp_high.high < 0)
872 if (sat_p)
874 /* Set to maximum. */
875 f->data.low = -1; /* Set to all ones. */
876 f->data.high = -1; /* Set to all ones. */
877 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
878 + GET_MODE_IBIT (f->mode));
879 /* Clear the sign. */
881 else
882 overflow_p = true;
884 else
885 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
886 &f->data, sat_p);
890 else
892 /* Right shift a to temp based on a->mode. */
893 double_int temp;
894 temp = a->data.lshift (GET_MODE_FBIT (mode) - GET_MODE_FBIT (a->mode),
895 HOST_BITS_PER_DOUBLE_INT,
896 SIGNED_FIXED_POINT_MODE_P (a->mode));
897 f->mode = mode;
898 f->data = temp;
899 if (SIGNED_FIXED_POINT_MODE_P (a->mode) ==
900 SIGNED_FIXED_POINT_MODE_P (f->mode))
901 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
902 else
904 /* Take care of the cases when converting between signed and
905 unsigned. */
906 if (SIGNED_FIXED_POINT_MODE_P (a->mode))
908 /* Signed -> Unsigned. */
909 if (a->data.high < 0)
911 if (sat_p)
913 f->data.low = 0; /* Set to zero. */
914 f->data.high = 0; /* Set to zero. */
916 else
917 overflow_p = true;
919 else
920 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data,
921 sat_p);
923 else
925 /* Unsigned -> Signed. */
926 if (temp.high < 0)
928 if (sat_p)
930 /* Set to maximum. */
931 f->data.low = -1; /* Set to all ones. */
932 f->data.high = -1; /* Set to all ones. */
933 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
934 + GET_MODE_IBIT (f->mode));
935 /* Clear the sign. */
937 else
938 overflow_p = true;
940 else
941 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data,
942 sat_p);
947 f->data = f->data.ext (SIGNED_FIXED_POINT_MODE_P (f->mode)
948 + GET_MODE_FBIT (f->mode)
949 + GET_MODE_IBIT (f->mode),
950 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
951 return overflow_p;
954 /* Convert to a new fixed-point mode from an integer.
955 If UNSIGNED_P, this integer is unsigned.
956 If SAT_P, saturate the result to the max or the min.
957 Return true, if !SAT_P and overflow. */
959 bool
960 fixed_convert_from_int (FIXED_VALUE_TYPE *f, machine_mode mode,
961 double_int a, bool unsigned_p, bool sat_p)
963 bool overflow_p = false;
964 /* Left shift a to temp_high, temp_low. */
965 double_int temp_high, temp_low;
966 int amount = GET_MODE_FBIT (mode);
967 if (amount == HOST_BITS_PER_DOUBLE_INT)
969 temp_high = a;
970 temp_low.low = 0;
971 temp_low.high = 0;
973 else
975 temp_low = a.llshift (amount, HOST_BITS_PER_DOUBLE_INT);
977 /* Logical shift right to temp_high. */
978 temp_high = a.llshift (amount - HOST_BITS_PER_DOUBLE_INT,
979 HOST_BITS_PER_DOUBLE_INT);
981 if (!unsigned_p && a.high < 0) /* Signed-extend temp_high. */
982 temp_high = temp_high.sext (amount);
984 f->mode = mode;
985 f->data = temp_low;
987 if (unsigned_p == UNSIGNED_FIXED_POINT_MODE_P (f->mode))
988 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
989 sat_p);
990 else
992 /* Take care of the cases when converting between signed and unsigned. */
993 if (!unsigned_p)
995 /* Signed -> Unsigned. */
996 if (a.high < 0)
998 if (sat_p)
1000 f->data.low = 0; /* Set to zero. */
1001 f->data.high = 0; /* Set to zero. */
1003 else
1004 overflow_p = true;
1006 else
1007 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
1008 &f->data, sat_p);
1010 else
1012 /* Unsigned -> Signed. */
1013 if (temp_high.high < 0)
1015 if (sat_p)
1017 /* Set to maximum. */
1018 f->data.low = -1; /* Set to all ones. */
1019 f->data.high = -1; /* Set to all ones. */
1020 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
1021 + GET_MODE_IBIT (f->mode));
1022 /* Clear the sign. */
1024 else
1025 overflow_p = true;
1027 else
1028 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
1029 &f->data, sat_p);
1032 f->data = f->data.ext (SIGNED_FIXED_POINT_MODE_P (f->mode)
1033 + GET_MODE_FBIT (f->mode)
1034 + GET_MODE_IBIT (f->mode),
1035 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
1036 return overflow_p;
1039 /* Convert to a new fixed-point mode from a real.
1040 If SAT_P, saturate the result to the max or the min.
1041 Return true, if !SAT_P and overflow. */
1043 bool
1044 fixed_convert_from_real (FIXED_VALUE_TYPE *f, machine_mode mode,
1045 const REAL_VALUE_TYPE *a, bool sat_p)
1047 bool overflow_p = false;
1048 REAL_VALUE_TYPE real_value, fixed_value, base_value;
1049 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
1050 int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
1051 unsigned int fbit = GET_MODE_FBIT (mode);
1052 enum fixed_value_range_code temp;
1053 bool fail;
1055 real_value = *a;
1056 f->mode = mode;
1057 real_2expN (&base_value, fbit, mode);
1058 real_arithmetic (&fixed_value, MULT_EXPR, &real_value, &base_value);
1060 wide_int w = real_to_integer (&fixed_value, &fail,
1061 GET_MODE_PRECISION (mode));
1062 f->data.low = w.elt (0);
1063 f->data.high = w.elt (1);
1064 temp = check_real_for_fixed_mode (&real_value, mode);
1065 if (temp == FIXED_UNDERFLOW) /* Minimum. */
1067 if (sat_p)
1069 if (unsigned_p)
1071 f->data.low = 0;
1072 f->data.high = 0;
1074 else
1076 f->data.low = 1;
1077 f->data.high = 0;
1078 f->data = f->data.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
1079 f->data = f->data.sext (1 + i_f_bits);
1082 else
1083 overflow_p = true;
1085 else if (temp == FIXED_GT_MAX_EPS || temp == FIXED_MAX_EPS) /* Maximum. */
1087 if (sat_p)
1089 f->data.low = -1;
1090 f->data.high = -1;
1091 f->data = f->data.zext (i_f_bits);
1093 else
1094 overflow_p = true;
1096 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
1097 return overflow_p;
1100 /* Convert to a new real mode from a fixed-point. */
1102 void
1103 real_convert_from_fixed (REAL_VALUE_TYPE *r, machine_mode mode,
1104 const FIXED_VALUE_TYPE *f)
1106 REAL_VALUE_TYPE base_value, fixed_value, real_value;
1108 signop sgn = UNSIGNED_FIXED_POINT_MODE_P (f->mode) ? UNSIGNED : SIGNED;
1109 real_2expN (&base_value, GET_MODE_FBIT (f->mode), f->mode);
1110 real_from_integer (&fixed_value, VOIDmode,
1111 wide_int::from (f->data, GET_MODE_PRECISION (f->mode),
1112 sgn), sgn);
1113 real_arithmetic (&real_value, RDIV_EXPR, &fixed_value, &base_value);
1114 real_convert (r, mode, &real_value);
1117 /* Determine whether a fixed-point value F is negative. */
1119 bool
1120 fixed_isneg (const FIXED_VALUE_TYPE *f)
1122 if (SIGNED_FIXED_POINT_MODE_P (f->mode))
1124 int i_f_bits = GET_MODE_IBIT (f->mode) + GET_MODE_FBIT (f->mode);
1125 int sign_bit = get_fixed_sign_bit (f->data, i_f_bits);
1126 if (sign_bit == 1)
1127 return true;
1130 return false;