Bump date stamp to 20121110
[official-gcc.git] / gcc / fixed-value.c
blobb74a60e29f25d13afa9ebde617f822688fdfaf64
1 /* Fixed-point arithmetic support.
2 Copyright (C) 2006, 2007, 2008, 2009, 2010 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;
84 /* Initialize from a decimal or hexadecimal string. */
86 void
87 fixed_from_string (FIXED_VALUE_TYPE *f, const char *str, enum machine_mode mode)
89 REAL_VALUE_TYPE real_value, fixed_value, base_value;
90 unsigned int fbit;
91 enum fixed_value_range_code temp;
93 f->mode = mode;
94 fbit = GET_MODE_FBIT (mode);
96 real_from_string (&real_value, str);
97 temp = check_real_for_fixed_mode (&real_value, f->mode);
98 /* We don't want to warn the case when the _Fract value is 1.0. */
99 if (temp == FIXED_UNDERFLOW
100 || temp == FIXED_GT_MAX_EPS
101 || (temp == FIXED_MAX_EPS && ALL_ACCUM_MODE_P (f->mode)))
102 warning (OPT_Woverflow,
103 "large fixed-point constant implicitly truncated to fixed-point type");
104 real_2expN (&base_value, fbit, mode);
105 real_arithmetic (&fixed_value, MULT_EXPR, &real_value, &base_value);
106 real_to_integer2 ((HOST_WIDE_INT *)&f->data.low, &f->data.high,
107 &fixed_value);
109 if (temp == FIXED_MAX_EPS && ALL_FRACT_MODE_P (f->mode))
111 /* From the spec, we need to evaluate 1 to the maximal value. */
112 f->data.low = -1;
113 f->data.high = -1;
114 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
115 + GET_MODE_IBIT (f->mode));
117 else
118 f->data = f->data.ext (SIGNED_FIXED_POINT_MODE_P (f->mode)
119 + GET_MODE_FBIT (f->mode)
120 + GET_MODE_IBIT (f->mode),
121 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
124 /* Render F as a decimal floating point constant. */
126 void
127 fixed_to_decimal (char *str, const FIXED_VALUE_TYPE *f_orig,
128 size_t buf_size)
130 REAL_VALUE_TYPE real_value, base_value, fixed_value;
132 real_2expN (&base_value, GET_MODE_FBIT (f_orig->mode), f_orig->mode);
133 real_from_integer (&real_value, VOIDmode, f_orig->data.low, f_orig->data.high,
134 UNSIGNED_FIXED_POINT_MODE_P (f_orig->mode));
135 real_arithmetic (&fixed_value, RDIV_EXPR, &real_value, &base_value);
136 real_to_decimal (str, &fixed_value, buf_size, 0, 1);
139 /* If SAT_P, saturate A to the maximum or the minimum, and save to *F based on
140 the machine mode MODE.
141 Do not modify *F otherwise.
142 This function assumes the width of double_int is greater than the width
143 of the fixed-point value (the sum of a possible sign bit, possible ibits,
144 and fbits).
145 Return true, if !SAT_P and overflow. */
147 static bool
148 fixed_saturate1 (enum machine_mode mode, double_int a, double_int *f,
149 bool sat_p)
151 bool overflow_p = false;
152 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
153 int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
155 if (unsigned_p) /* Unsigned type. */
157 double_int max;
158 max.low = -1;
159 max.high = -1;
160 max = max.zext (i_f_bits);
161 if (a.ugt (max))
163 if (sat_p)
164 *f = max;
165 else
166 overflow_p = true;
169 else /* Signed type. */
171 double_int max, min;
172 max.high = -1;
173 max.low = -1;
174 max = max.zext (i_f_bits);
175 min.high = 0;
176 min.low = 1;
177 min = min.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
178 min = min.sext (1 + i_f_bits);
179 if (a.sgt (max))
181 if (sat_p)
182 *f = max;
183 else
184 overflow_p = true;
186 else if (a.slt (min))
188 if (sat_p)
189 *f = min;
190 else
191 overflow_p = true;
194 return overflow_p;
197 /* If SAT_P, saturate {A_HIGH, A_LOW} to the maximum or the minimum, and
198 save to *F based on the machine mode MODE.
199 Do not modify *F otherwise.
200 This function assumes the width of two double_int is greater than the width
201 of the fixed-point value (the sum of a possible sign bit, possible ibits,
202 and fbits).
203 Return true, if !SAT_P and overflow. */
205 static bool
206 fixed_saturate2 (enum machine_mode mode, double_int a_high, double_int a_low,
207 double_int *f, bool sat_p)
209 bool overflow_p = false;
210 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
211 int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
213 if (unsigned_p) /* Unsigned type. */
215 double_int max_r, max_s;
216 max_r.high = 0;
217 max_r.low = 0;
218 max_s.high = -1;
219 max_s.low = -1;
220 max_s = max_s.zext (i_f_bits);
221 if (a_high.ugt (max_r)
222 || (a_high == max_r &&
223 a_low.ugt (max_s)))
225 if (sat_p)
226 *f = max_s;
227 else
228 overflow_p = true;
231 else /* Signed type. */
233 double_int max_r, max_s, min_r, min_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 min_r.high = -1;
240 min_r.low = -1;
241 min_s.high = 0;
242 min_s.low = 1;
243 min_s = min_s.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
244 min_s = min_s.sext (1 + i_f_bits);
245 if (a_high.sgt (max_r)
246 || (a_high == max_r &&
247 a_low.ugt (max_s)))
249 if (sat_p)
250 *f = max_s;
251 else
252 overflow_p = true;
254 else if (a_high.slt (min_r)
255 || (a_high == min_r &&
256 a_low.ult (min_s)))
258 if (sat_p)
259 *f = min_s;
260 else
261 overflow_p = true;
264 return overflow_p;
267 /* Return the sign bit based on I_F_BITS. */
269 static inline int
270 get_fixed_sign_bit (double_int a, int i_f_bits)
272 if (i_f_bits < HOST_BITS_PER_WIDE_INT)
273 return (a.low >> i_f_bits) & 1;
274 else
275 return (a.high >> (i_f_bits - HOST_BITS_PER_WIDE_INT)) & 1;
278 /* Calculate F = A + (SUBTRACT_P ? -B : B).
279 If SAT_P, saturate the result to the max or the min.
280 Return true, if !SAT_P and overflow. */
282 static bool
283 do_fixed_add (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
284 const FIXED_VALUE_TYPE *b, bool subtract_p, bool sat_p)
286 bool overflow_p = false;
287 bool unsigned_p;
288 double_int temp;
289 int i_f_bits;
291 /* This was a conditional expression but it triggered a bug in
292 Sun C 5.5. */
293 if (subtract_p)
294 temp = -b->data;
295 else
296 temp = b->data;
298 unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
299 i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
300 f->mode = a->mode;
301 f->data = a->data + temp;
302 if (unsigned_p) /* Unsigned type. */
304 if (subtract_p) /* Unsigned subtraction. */
306 if (a->data.ult (b->data))
308 if (sat_p)
310 f->data.high = 0;
311 f->data.low = 0;
313 else
314 overflow_p = true;
317 else /* Unsigned addition. */
319 f->data = f->data.zext (i_f_bits);
320 if (f->data.ult (a->data)
321 || f->data.ult (b->data))
323 if (sat_p)
325 f->data.high = -1;
326 f->data.low = -1;
328 else
329 overflow_p = true;
333 else /* Signed type. */
335 if ((!subtract_p
336 && (get_fixed_sign_bit (a->data, i_f_bits)
337 == get_fixed_sign_bit (b->data, i_f_bits))
338 && (get_fixed_sign_bit (a->data, i_f_bits)
339 != get_fixed_sign_bit (f->data, i_f_bits)))
340 || (subtract_p
341 && (get_fixed_sign_bit (a->data, i_f_bits)
342 != get_fixed_sign_bit (b->data, i_f_bits))
343 && (get_fixed_sign_bit (a->data, i_f_bits)
344 != get_fixed_sign_bit (f->data, i_f_bits))))
346 if (sat_p)
348 f->data.low = 1;
349 f->data.high = 0;
350 f->data = f->data.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
351 if (get_fixed_sign_bit (a->data, i_f_bits) == 0)
353 --f->data;
356 else
357 overflow_p = true;
360 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
361 return overflow_p;
364 /* Calculate F = A * B.
365 If SAT_P, saturate the result to the max or the min.
366 Return true, if !SAT_P and overflow. */
368 static bool
369 do_fixed_multiply (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
370 const FIXED_VALUE_TYPE *b, bool sat_p)
372 bool overflow_p = false;
373 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
374 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
375 f->mode = a->mode;
376 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT)
378 f->data = a->data * b->data;
379 f->data = f->data.lshift (-GET_MODE_FBIT (f->mode),
380 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
381 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
383 else
385 /* The result of multiplication expands to two double_int. */
386 double_int a_high, a_low, b_high, b_low;
387 double_int high_high, high_low, low_high, low_low;
388 double_int r, s, temp1, temp2;
389 int carry = 0;
391 /* Decompose a and b to four double_int. */
392 a_high.low = a->data.high;
393 a_high.high = 0;
394 a_low.low = a->data.low;
395 a_low.high = 0;
396 b_high.low = b->data.high;
397 b_high.high = 0;
398 b_low.low = b->data.low;
399 b_low.high = 0;
401 /* Perform four multiplications. */
402 low_low = a_low * b_low;
403 low_high = a_low * b_high;
404 high_low = a_high * b_low;
405 high_high = a_high * b_high;
407 /* Accumulate four results to {r, s}. */
408 temp1.high = high_low.low;
409 temp1.low = 0;
410 s = low_low + temp1;
411 if (s.ult (low_low)
412 || s.ult (temp1))
413 carry ++; /* Carry */
414 temp1.high = s.high;
415 temp1.low = s.low;
416 temp2.high = low_high.low;
417 temp2.low = 0;
418 s = temp1 + temp2;
419 if (s.ult (temp1)
420 || s.ult (temp2))
421 carry ++; /* Carry */
423 temp1.low = high_low.high;
424 temp1.high = 0;
425 r = high_high + temp1;
426 temp1.low = low_high.high;
427 temp1.high = 0;
428 r += temp1;
429 temp1.low = carry;
430 temp1.high = 0;
431 r += temp1;
433 /* We need to subtract b from r, if a < 0. */
434 if (!unsigned_p && a->data.high < 0)
435 r -= b->data;
436 /* We need to subtract a from r, if b < 0. */
437 if (!unsigned_p && b->data.high < 0)
438 r -= a->data;
440 /* Shift right the result by FBIT. */
441 if (GET_MODE_FBIT (f->mode) == HOST_BITS_PER_DOUBLE_INT)
443 s.low = r.low;
444 s.high = r.high;
445 if (unsigned_p)
447 r.low = 0;
448 r.high = 0;
450 else
452 r.low = -1;
453 r.high = -1;
455 f->data.low = s.low;
456 f->data.high = s.high;
458 else
460 s = s.llshift ((-GET_MODE_FBIT (f->mode)), HOST_BITS_PER_DOUBLE_INT);
461 f->data = r.llshift ((HOST_BITS_PER_DOUBLE_INT
462 - GET_MODE_FBIT (f->mode)),
463 HOST_BITS_PER_DOUBLE_INT);
464 f->data.low = f->data.low | s.low;
465 f->data.high = f->data.high | s.high;
466 s.low = f->data.low;
467 s.high = f->data.high;
468 r = r.lshift (-GET_MODE_FBIT (f->mode),
469 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
472 overflow_p = fixed_saturate2 (f->mode, r, s, &f->data, sat_p);
475 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
476 return overflow_p;
479 /* Calculate F = A / B.
480 If SAT_P, saturate the result to the max or the min.
481 Return true, if !SAT_P and overflow. */
483 static bool
484 do_fixed_divide (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
485 const FIXED_VALUE_TYPE *b, bool sat_p)
487 bool overflow_p = false;
488 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
489 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
490 f->mode = a->mode;
491 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT)
493 f->data = a->data.lshift (GET_MODE_FBIT (f->mode),
494 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
495 f->data = f->data.div (b->data, unsigned_p, TRUNC_DIV_EXPR);
496 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
498 else
500 double_int pos_a, pos_b, r, s;
501 double_int quo_r, quo_s, mod, temp;
502 int num_of_neg = 0;
503 int i;
505 /* If a < 0, negate a. */
506 if (!unsigned_p && a->data.high < 0)
508 pos_a = -a->data;
509 num_of_neg ++;
511 else
512 pos_a = a->data;
514 /* If b < 0, negate b. */
515 if (!unsigned_p && b->data.high < 0)
517 pos_b = -b->data;
518 num_of_neg ++;
520 else
521 pos_b = b->data;
523 /* Left shift pos_a to {r, s} by FBIT. */
524 if (GET_MODE_FBIT (f->mode) == HOST_BITS_PER_DOUBLE_INT)
526 r = pos_a;
527 s.high = 0;
528 s.low = 0;
530 else
532 s = pos_a.llshift (GET_MODE_FBIT (f->mode), HOST_BITS_PER_DOUBLE_INT);
533 r = pos_a.llshift (- (HOST_BITS_PER_DOUBLE_INT
534 - GET_MODE_FBIT (f->mode)),
535 HOST_BITS_PER_DOUBLE_INT);
538 /* Divide r by pos_b to quo_r. The remainder is in mod. */
539 quo_r = r.divmod (pos_b, 1, TRUNC_DIV_EXPR, &mod);
540 quo_s = double_int_zero;
542 for (i = 0; i < HOST_BITS_PER_DOUBLE_INT; i++)
544 /* Record the leftmost bit of mod. */
545 int leftmost_mod = (mod.high < 0);
547 /* Shift left mod by 1 bit. */
548 mod = mod.llshift (1, HOST_BITS_PER_DOUBLE_INT);
550 /* Test the leftmost bit of s to add to mod. */
551 if (s.high < 0)
552 mod.low += 1;
554 /* Shift left quo_s by 1 bit. */
555 quo_s = quo_s.llshift (1, HOST_BITS_PER_DOUBLE_INT);
557 /* Try to calculate (mod - pos_b). */
558 temp = mod - pos_b;
560 if (leftmost_mod == 1 || mod.ucmp (pos_b) != -1)
562 quo_s.low += 1;
563 mod = temp;
566 /* Shift left s by 1 bit. */
567 s = s.llshift (1, HOST_BITS_PER_DOUBLE_INT);
571 if (num_of_neg == 1)
573 quo_s = -quo_s;
574 if (quo_s.high == 0 && quo_s.low == 0)
575 quo_r = -quo_r;
576 else
578 quo_r.low = ~quo_r.low;
579 quo_r.high = ~quo_r.high;
583 f->data = quo_s;
584 overflow_p = fixed_saturate2 (f->mode, quo_r, quo_s, &f->data, sat_p);
587 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
588 return overflow_p;
591 /* Calculate F = A << B if LEFT_P. Otherwise, F = A >> B.
592 If SAT_P, saturate the result to the max or the min.
593 Return true, if !SAT_P and overflow. */
595 static bool
596 do_fixed_shift (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
597 const FIXED_VALUE_TYPE *b, bool left_p, bool sat_p)
599 bool overflow_p = false;
600 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
601 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
602 f->mode = a->mode;
604 if (b->data.low == 0)
606 f->data = a->data;
607 return overflow_p;
610 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT || (!left_p))
612 f->data = a->data.lshift (left_p ? b->data.low : -b->data.low,
613 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
614 if (left_p) /* Only left shift saturates. */
615 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
617 else /* We need two double_int to store the left-shift result. */
619 double_int temp_high, temp_low;
620 if (b->data.low == HOST_BITS_PER_DOUBLE_INT)
622 temp_high = a->data;
623 temp_low.high = 0;
624 temp_low.low = 0;
626 else
628 temp_low = a->data.lshift (b->data.low,
629 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
630 /* Logical shift right to temp_high. */
631 temp_high = a->data.llshift (b->data.low - HOST_BITS_PER_DOUBLE_INT,
632 HOST_BITS_PER_DOUBLE_INT);
634 if (!unsigned_p && a->data.high < 0) /* Signed-extend temp_high. */
635 temp_high = temp_high.ext (b->data.low, unsigned_p);
636 f->data = temp_low;
637 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
638 sat_p);
640 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
641 return overflow_p;
644 /* Calculate F = -A.
645 If SAT_P, saturate the result to the max or the min.
646 Return true, if !SAT_P and overflow. */
648 static bool
649 do_fixed_neg (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a, bool sat_p)
651 bool overflow_p = false;
652 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
653 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
654 f->mode = a->mode;
655 f->data = -a->data;
656 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
658 if (unsigned_p) /* Unsigned type. */
660 if (f->data.low != 0 || f->data.high != 0)
662 if (sat_p)
664 f->data.low = 0;
665 f->data.high = 0;
667 else
668 overflow_p = true;
671 else /* Signed type. */
673 if (!(f->data.high == 0 && f->data.low == 0)
674 && f->data.high == a->data.high && f->data.low == a->data.low )
676 if (sat_p)
678 /* Saturate to the maximum by subtracting f->data by one. */
679 f->data.low = -1;
680 f->data.high = -1;
681 f->data = f->data.zext (i_f_bits);
683 else
684 overflow_p = true;
687 return overflow_p;
690 /* Perform the binary or unary operation described by CODE.
691 Note that OP0 and OP1 must have the same mode for binary operators.
692 For a unary operation, leave OP1 NULL.
693 Return true, if !SAT_P and overflow. */
695 bool
696 fixed_arithmetic (FIXED_VALUE_TYPE *f, int icode, const FIXED_VALUE_TYPE *op0,
697 const FIXED_VALUE_TYPE *op1, bool sat_p)
699 switch (icode)
701 case NEGATE_EXPR:
702 return do_fixed_neg (f, op0, sat_p);
703 break;
705 case PLUS_EXPR:
706 gcc_assert (op0->mode == op1->mode);
707 return do_fixed_add (f, op0, op1, false, sat_p);
708 break;
710 case MINUS_EXPR:
711 gcc_assert (op0->mode == op1->mode);
712 return do_fixed_add (f, op0, op1, true, sat_p);
713 break;
715 case MULT_EXPR:
716 gcc_assert (op0->mode == op1->mode);
717 return do_fixed_multiply (f, op0, op1, sat_p);
718 break;
720 case TRUNC_DIV_EXPR:
721 gcc_assert (op0->mode == op1->mode);
722 return do_fixed_divide (f, op0, op1, sat_p);
723 break;
725 case LSHIFT_EXPR:
726 return do_fixed_shift (f, op0, op1, true, sat_p);
727 break;
729 case RSHIFT_EXPR:
730 return do_fixed_shift (f, op0, op1, false, sat_p);
731 break;
733 default:
734 gcc_unreachable ();
736 return false;
739 /* Compare fixed-point values by tree_code.
740 Note that OP0 and OP1 must have the same mode. */
742 bool
743 fixed_compare (int icode, const FIXED_VALUE_TYPE *op0,
744 const FIXED_VALUE_TYPE *op1)
746 enum tree_code code = (enum tree_code) icode;
747 gcc_assert (op0->mode == op1->mode);
749 switch (code)
751 case NE_EXPR:
752 return op0->data != op1->data;
754 case EQ_EXPR:
755 return op0->data == op1->data;
757 case LT_EXPR:
758 return op0->data.cmp (op1->data,
759 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) == -1;
761 case LE_EXPR:
762 return op0->data.cmp (op1->data,
763 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) != 1;
765 case GT_EXPR:
766 return op0->data.cmp (op1->data,
767 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) == 1;
769 case GE_EXPR:
770 return op0->data.cmp (op1->data,
771 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) != -1;
773 default:
774 gcc_unreachable ();
778 /* Extend or truncate to a new mode.
779 If SAT_P, saturate the result to the max or the min.
780 Return true, if !SAT_P and overflow. */
782 bool
783 fixed_convert (FIXED_VALUE_TYPE *f, enum machine_mode mode,
784 const FIXED_VALUE_TYPE *a, bool sat_p)
786 bool overflow_p = false;
787 if (mode == a->mode)
789 *f = *a;
790 return overflow_p;
793 if (GET_MODE_FBIT (mode) > GET_MODE_FBIT (a->mode))
795 /* Left shift a to temp_high, temp_low based on a->mode. */
796 double_int temp_high, temp_low;
797 int amount = GET_MODE_FBIT (mode) - GET_MODE_FBIT (a->mode);
798 temp_low = a->data.lshift (amount,
799 HOST_BITS_PER_DOUBLE_INT,
800 SIGNED_FIXED_POINT_MODE_P (a->mode));
801 /* Logical shift right to temp_high. */
802 temp_high = a->data.llshift (amount - HOST_BITS_PER_DOUBLE_INT,
803 HOST_BITS_PER_DOUBLE_INT);
804 if (SIGNED_FIXED_POINT_MODE_P (a->mode)
805 && a->data.high < 0) /* Signed-extend temp_high. */
806 temp_high = temp_high.sext (amount);
807 f->mode = mode;
808 f->data = temp_low;
809 if (SIGNED_FIXED_POINT_MODE_P (a->mode) ==
810 SIGNED_FIXED_POINT_MODE_P (f->mode))
811 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
812 sat_p);
813 else
815 /* Take care of the cases when converting between signed and
816 unsigned. */
817 if (SIGNED_FIXED_POINT_MODE_P (a->mode))
819 /* Signed -> Unsigned. */
820 if (a->data.high < 0)
822 if (sat_p)
824 f->data.low = 0; /* Set to zero. */
825 f->data.high = 0; /* Set to zero. */
827 else
828 overflow_p = true;
830 else
831 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
832 &f->data, sat_p);
834 else
836 /* Unsigned -> Signed. */
837 if (temp_high.high < 0)
839 if (sat_p)
841 /* Set to maximum. */
842 f->data.low = -1; /* Set to all ones. */
843 f->data.high = -1; /* Set to all ones. */
844 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
845 + GET_MODE_IBIT (f->mode));
846 /* Clear the sign. */
848 else
849 overflow_p = true;
851 else
852 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
853 &f->data, sat_p);
857 else
859 /* Right shift a to temp based on a->mode. */
860 double_int temp;
861 temp = a->data.lshift (GET_MODE_FBIT (mode) - GET_MODE_FBIT (a->mode),
862 HOST_BITS_PER_DOUBLE_INT,
863 SIGNED_FIXED_POINT_MODE_P (a->mode));
864 f->mode = mode;
865 f->data = temp;
866 if (SIGNED_FIXED_POINT_MODE_P (a->mode) ==
867 SIGNED_FIXED_POINT_MODE_P (f->mode))
868 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
869 else
871 /* Take care of the cases when converting between signed and
872 unsigned. */
873 if (SIGNED_FIXED_POINT_MODE_P (a->mode))
875 /* Signed -> Unsigned. */
876 if (a->data.high < 0)
878 if (sat_p)
880 f->data.low = 0; /* Set to zero. */
881 f->data.high = 0; /* Set to zero. */
883 else
884 overflow_p = true;
886 else
887 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data,
888 sat_p);
890 else
892 /* Unsigned -> Signed. */
893 if (temp.high < 0)
895 if (sat_p)
897 /* Set to maximum. */
898 f->data.low = -1; /* Set to all ones. */
899 f->data.high = -1; /* Set to all ones. */
900 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
901 + GET_MODE_IBIT (f->mode));
902 /* Clear the sign. */
904 else
905 overflow_p = true;
907 else
908 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data,
909 sat_p);
914 f->data = f->data.ext (SIGNED_FIXED_POINT_MODE_P (f->mode)
915 + GET_MODE_FBIT (f->mode)
916 + GET_MODE_IBIT (f->mode),
917 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
918 return overflow_p;
921 /* Convert to a new fixed-point mode from an integer.
922 If UNSIGNED_P, this integer is unsigned.
923 If SAT_P, saturate the result to the max or the min.
924 Return true, if !SAT_P and overflow. */
926 bool
927 fixed_convert_from_int (FIXED_VALUE_TYPE *f, enum machine_mode mode,
928 double_int a, bool unsigned_p, bool sat_p)
930 bool overflow_p = false;
931 /* Left shift a to temp_high, temp_low. */
932 double_int temp_high, temp_low;
933 int amount = GET_MODE_FBIT (mode);
934 if (amount == HOST_BITS_PER_DOUBLE_INT)
936 temp_high = a;
937 temp_low.low = 0;
938 temp_low.high = 0;
940 else
942 temp_low = a.llshift (amount, HOST_BITS_PER_DOUBLE_INT);
944 /* Logical shift right to temp_high. */
945 temp_high = a.llshift (amount - HOST_BITS_PER_DOUBLE_INT,
946 HOST_BITS_PER_DOUBLE_INT);
948 if (!unsigned_p && a.high < 0) /* Signed-extend temp_high. */
949 temp_high = temp_high.sext (amount);
951 f->mode = mode;
952 f->data = temp_low;
954 if (unsigned_p == UNSIGNED_FIXED_POINT_MODE_P (f->mode))
955 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
956 sat_p);
957 else
959 /* Take care of the cases when converting between signed and unsigned. */
960 if (!unsigned_p)
962 /* Signed -> Unsigned. */
963 if (a.high < 0)
965 if (sat_p)
967 f->data.low = 0; /* Set to zero. */
968 f->data.high = 0; /* Set to zero. */
970 else
971 overflow_p = true;
973 else
974 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
975 &f->data, sat_p);
977 else
979 /* Unsigned -> Signed. */
980 if (temp_high.high < 0)
982 if (sat_p)
984 /* Set to maximum. */
985 f->data.low = -1; /* Set to all ones. */
986 f->data.high = -1; /* Set to all ones. */
987 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
988 + GET_MODE_IBIT (f->mode));
989 /* Clear the sign. */
991 else
992 overflow_p = true;
994 else
995 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
996 &f->data, sat_p);
999 f->data = f->data.ext (SIGNED_FIXED_POINT_MODE_P (f->mode)
1000 + GET_MODE_FBIT (f->mode)
1001 + GET_MODE_IBIT (f->mode),
1002 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
1003 return overflow_p;
1006 /* Convert to a new fixed-point mode from a real.
1007 If SAT_P, saturate the result to the max or the min.
1008 Return true, if !SAT_P and overflow. */
1010 bool
1011 fixed_convert_from_real (FIXED_VALUE_TYPE *f, enum machine_mode mode,
1012 const REAL_VALUE_TYPE *a, bool sat_p)
1014 bool overflow_p = false;
1015 REAL_VALUE_TYPE real_value, fixed_value, base_value;
1016 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
1017 int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
1018 unsigned int fbit = GET_MODE_FBIT (mode);
1019 enum fixed_value_range_code temp;
1021 real_value = *a;
1022 f->mode = mode;
1023 real_2expN (&base_value, fbit, mode);
1024 real_arithmetic (&fixed_value, MULT_EXPR, &real_value, &base_value);
1025 real_to_integer2 ((HOST_WIDE_INT *)&f->data.low, &f->data.high, &fixed_value);
1026 temp = check_real_for_fixed_mode (&real_value, mode);
1027 if (temp == FIXED_UNDERFLOW) /* Minimum. */
1029 if (sat_p)
1031 if (unsigned_p)
1033 f->data.low = 0;
1034 f->data.high = 0;
1036 else
1038 f->data.low = 1;
1039 f->data.high = 0;
1040 f->data = f->data.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
1041 f->data = f->data.sext (1 + i_f_bits);
1044 else
1045 overflow_p = true;
1047 else if (temp == FIXED_GT_MAX_EPS || temp == FIXED_MAX_EPS) /* Maximum. */
1049 if (sat_p)
1051 f->data.low = -1;
1052 f->data.high = -1;
1053 f->data = f->data.zext (i_f_bits);
1055 else
1056 overflow_p = true;
1058 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
1059 return overflow_p;
1062 /* Convert to a new real mode from a fixed-point. */
1064 void
1065 real_convert_from_fixed (REAL_VALUE_TYPE *r, enum machine_mode mode,
1066 const FIXED_VALUE_TYPE *f)
1068 REAL_VALUE_TYPE base_value, fixed_value, real_value;
1070 real_2expN (&base_value, GET_MODE_FBIT (f->mode), f->mode);
1071 real_from_integer (&fixed_value, VOIDmode, f->data.low, f->data.high,
1072 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
1073 real_arithmetic (&real_value, RDIV_EXPR, &fixed_value, &base_value);
1074 real_convert (r, mode, &real_value);
1077 /* Determine whether a fixed-point value F is negative. */
1079 bool
1080 fixed_isneg (const FIXED_VALUE_TYPE *f)
1082 if (SIGNED_FIXED_POINT_MODE_P (f->mode))
1084 int i_f_bits = GET_MODE_IBIT (f->mode) + GET_MODE_FBIT (f->mode);
1085 int sign_bit = get_fixed_sign_bit (f->data, i_f_bits);
1086 if (sign_bit == 1)
1087 return true;
1090 return false;