2012-09-04 Janus Weil <janus@gcc.gnu.org>
[official-gcc.git] / gcc / fixed-value.c
blob9a34bc53de1784a53d763dfefd99c6271cdb89cc
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,
381 !unsigned_p);
382 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
384 else
386 /* The result of multiplication expands to two double_int. */
387 double_int a_high, a_low, b_high, b_low;
388 double_int high_high, high_low, low_high, low_low;
389 double_int r, s, temp1, temp2;
390 int carry = 0;
392 /* Decompose a and b to four double_int. */
393 a_high.low = a->data.high;
394 a_high.high = 0;
395 a_low.low = a->data.low;
396 a_low.high = 0;
397 b_high.low = b->data.high;
398 b_high.high = 0;
399 b_low.low = b->data.low;
400 b_low.high = 0;
402 /* Perform four multiplications. */
403 low_low = a_low * b_low;
404 low_high = a_low * b_high;
405 high_low = a_high * b_low;
406 high_high = a_high * b_high;
408 /* Accumulate four results to {r, s}. */
409 temp1.high = high_low.low;
410 temp1.low = 0;
411 s = low_low + temp1;
412 if (s.ult (low_low)
413 || s.ult (temp1))
414 carry ++; /* Carry */
415 temp1.high = s.high;
416 temp1.low = s.low;
417 temp2.high = low_high.low;
418 temp2.low = 0;
419 s = temp1 + temp2;
420 if (s.ult (temp1)
421 || s.ult (temp2))
422 carry ++; /* Carry */
424 temp1.low = high_low.high;
425 temp1.high = 0;
426 r = high_high + temp1;
427 temp1.low = low_high.high;
428 temp1.high = 0;
429 r += temp1;
430 temp1.low = carry;
431 temp1.high = 0;
432 r += temp1;
434 /* We need to subtract b from r, if a < 0. */
435 if (!unsigned_p && a->data.high < 0)
436 r -= b->data;
437 /* We need to subtract a from r, if b < 0. */
438 if (!unsigned_p && b->data.high < 0)
439 r -= a->data;
441 /* Shift right the result by FBIT. */
442 if (GET_MODE_FBIT (f->mode) == HOST_BITS_PER_DOUBLE_INT)
444 s.low = r.low;
445 s.high = r.high;
446 if (unsigned_p)
448 r.low = 0;
449 r.high = 0;
451 else
453 r.low = -1;
454 r.high = -1;
456 f->data.low = s.low;
457 f->data.high = s.high;
459 else
461 s = s.llshift ((-GET_MODE_FBIT (f->mode)), HOST_BITS_PER_DOUBLE_INT);
462 f->data = r.llshift ((HOST_BITS_PER_DOUBLE_INT
463 - GET_MODE_FBIT (f->mode)),
464 HOST_BITS_PER_DOUBLE_INT);
465 f->data.low = f->data.low | s.low;
466 f->data.high = f->data.high | s.high;
467 s.low = f->data.low;
468 s.high = f->data.high;
469 r = r.lshift ((-GET_MODE_FBIT (f->mode)),
470 HOST_BITS_PER_DOUBLE_INT,
471 !unsigned_p);
474 overflow_p = fixed_saturate2 (f->mode, r, s, &f->data, sat_p);
477 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
478 return overflow_p;
481 /* Calculate F = A / B.
482 If SAT_P, saturate the result to the max or the min.
483 Return true, if !SAT_P and overflow. */
485 static bool
486 do_fixed_divide (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
487 const FIXED_VALUE_TYPE *b, bool sat_p)
489 bool overflow_p = false;
490 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
491 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
492 f->mode = a->mode;
493 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT)
495 f->data = a->data.lshift (GET_MODE_FBIT (f->mode),
496 HOST_BITS_PER_DOUBLE_INT,
497 !unsigned_p);
498 f->data = f->data.div (b->data, unsigned_p, TRUNC_DIV_EXPR);
499 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
501 else
503 double_int pos_a, pos_b, r, s;
504 double_int quo_r, quo_s, mod, temp;
505 int num_of_neg = 0;
506 int i;
508 /* If a < 0, negate a. */
509 if (!unsigned_p && a->data.high < 0)
511 pos_a = -a->data;
512 num_of_neg ++;
514 else
515 pos_a = a->data;
517 /* If b < 0, negate b. */
518 if (!unsigned_p && b->data.high < 0)
520 pos_b = -b->data;
521 num_of_neg ++;
523 else
524 pos_b = b->data;
526 /* Left shift pos_a to {r, s} by FBIT. */
527 if (GET_MODE_FBIT (f->mode) == HOST_BITS_PER_DOUBLE_INT)
529 r = pos_a;
530 s.high = 0;
531 s.low = 0;
533 else
535 s = pos_a.llshift (GET_MODE_FBIT (f->mode), HOST_BITS_PER_DOUBLE_INT);
536 r = pos_a.llshift (- (HOST_BITS_PER_DOUBLE_INT
537 - GET_MODE_FBIT (f->mode)),
538 HOST_BITS_PER_DOUBLE_INT);
541 /* Divide r by pos_b to quo_r. The remainder is in mod. */
542 quo_r = r.divmod (pos_b, 1, TRUNC_DIV_EXPR, &mod);
543 quo_s = double_int_zero;
545 for (i = 0; i < HOST_BITS_PER_DOUBLE_INT; i++)
547 /* Record the leftmost bit of mod. */
548 int leftmost_mod = (mod.high < 0);
550 /* Shift left mod by 1 bit. */
551 mod = mod.llshift (1, HOST_BITS_PER_DOUBLE_INT);
553 /* Test the leftmost bit of s to add to mod. */
554 if (s.high < 0)
555 mod.low += 1;
557 /* Shift left quo_s by 1 bit. */
558 quo_s = quo_s.llshift (1, HOST_BITS_PER_DOUBLE_INT);
560 /* Try to calculate (mod - pos_b). */
561 temp = mod - pos_b;
563 if (leftmost_mod == 1 || mod.ucmp (pos_b) != -1)
565 quo_s.low += 1;
566 mod = temp;
569 /* Shift left s by 1 bit. */
570 s = s.llshift (1, HOST_BITS_PER_DOUBLE_INT);
574 if (num_of_neg == 1)
576 quo_s = -quo_s;
577 if (quo_s.high == 0 && quo_s.low == 0)
578 quo_r = -quo_r;
579 else
581 quo_r.low = ~quo_r.low;
582 quo_r.high = ~quo_r.high;
586 f->data = quo_s;
587 overflow_p = fixed_saturate2 (f->mode, quo_r, quo_s, &f->data, sat_p);
590 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
591 return overflow_p;
594 /* Calculate F = A << B if LEFT_P. Otherwise, F = A >> B.
595 If SAT_P, saturate the result to the max or the min.
596 Return true, if !SAT_P and overflow. */
598 static bool
599 do_fixed_shift (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
600 const FIXED_VALUE_TYPE *b, bool left_p, bool sat_p)
602 bool overflow_p = false;
603 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
604 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
605 f->mode = a->mode;
607 if (b->data.low == 0)
609 f->data = a->data;
610 return overflow_p;
613 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT || (!left_p))
615 f->data = a->data.lshift (left_p ? b->data.low : (-b->data.low),
616 HOST_BITS_PER_DOUBLE_INT,
617 !unsigned_p);
618 if (left_p) /* Only left shift saturates. */
619 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
621 else /* We need two double_int to store the left-shift result. */
623 double_int temp_high, temp_low;
624 if (b->data.low == HOST_BITS_PER_DOUBLE_INT)
626 temp_high = a->data;
627 temp_low.high = 0;
628 temp_low.low = 0;
630 else
632 temp_low = a->data.lshift (b->data.low,
633 HOST_BITS_PER_DOUBLE_INT,
634 !unsigned_p);
635 /* Logical shift right to temp_high. */
636 temp_high = a->data.llshift (b->data.low - HOST_BITS_PER_DOUBLE_INT,
637 HOST_BITS_PER_DOUBLE_INT);
639 if (!unsigned_p && a->data.high < 0) /* Signed-extend temp_high. */
640 temp_high = temp_high.ext (b->data.low, unsigned_p);
641 f->data = temp_low;
642 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
643 sat_p);
645 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
646 return overflow_p;
649 /* Calculate F = -A.
650 If SAT_P, saturate the result to the max or the min.
651 Return true, if !SAT_P and overflow. */
653 static bool
654 do_fixed_neg (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a, bool sat_p)
656 bool overflow_p = false;
657 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
658 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
659 f->mode = a->mode;
660 f->data = -a->data;
661 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
663 if (unsigned_p) /* Unsigned type. */
665 if (f->data.low != 0 || f->data.high != 0)
667 if (sat_p)
669 f->data.low = 0;
670 f->data.high = 0;
672 else
673 overflow_p = true;
676 else /* Signed type. */
678 if (!(f->data.high == 0 && f->data.low == 0)
679 && f->data.high == a->data.high && f->data.low == a->data.low )
681 if (sat_p)
683 /* Saturate to the maximum by subtracting f->data by one. */
684 f->data.low = -1;
685 f->data.high = -1;
686 f->data = f->data.zext (i_f_bits);
688 else
689 overflow_p = true;
692 return overflow_p;
695 /* Perform the binary or unary operation described by CODE.
696 Note that OP0 and OP1 must have the same mode for binary operators.
697 For a unary operation, leave OP1 NULL.
698 Return true, if !SAT_P and overflow. */
700 bool
701 fixed_arithmetic (FIXED_VALUE_TYPE *f, int icode, const FIXED_VALUE_TYPE *op0,
702 const FIXED_VALUE_TYPE *op1, bool sat_p)
704 switch (icode)
706 case NEGATE_EXPR:
707 return do_fixed_neg (f, op0, sat_p);
708 break;
710 case PLUS_EXPR:
711 gcc_assert (op0->mode == op1->mode);
712 return do_fixed_add (f, op0, op1, false, sat_p);
713 break;
715 case MINUS_EXPR:
716 gcc_assert (op0->mode == op1->mode);
717 return do_fixed_add (f, op0, op1, true, sat_p);
718 break;
720 case MULT_EXPR:
721 gcc_assert (op0->mode == op1->mode);
722 return do_fixed_multiply (f, op0, op1, sat_p);
723 break;
725 case TRUNC_DIV_EXPR:
726 gcc_assert (op0->mode == op1->mode);
727 return do_fixed_divide (f, op0, op1, sat_p);
728 break;
730 case LSHIFT_EXPR:
731 return do_fixed_shift (f, op0, op1, true, sat_p);
732 break;
734 case RSHIFT_EXPR:
735 return do_fixed_shift (f, op0, op1, false, sat_p);
736 break;
738 default:
739 gcc_unreachable ();
741 return false;
744 /* Compare fixed-point values by tree_code.
745 Note that OP0 and OP1 must have the same mode. */
747 bool
748 fixed_compare (int icode, const FIXED_VALUE_TYPE *op0,
749 const FIXED_VALUE_TYPE *op1)
751 enum tree_code code = (enum tree_code) icode;
752 gcc_assert (op0->mode == op1->mode);
754 switch (code)
756 case NE_EXPR:
757 return op0->data != op1->data;
759 case EQ_EXPR:
760 return op0->data == op1->data;
762 case LT_EXPR:
763 return op0->data.cmp (op1->data,
764 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) == -1;
766 case LE_EXPR:
767 return op0->data.cmp (op1->data,
768 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) != 1;
770 case GT_EXPR:
771 return op0->data.cmp (op1->data,
772 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) == 1;
774 case GE_EXPR:
775 return op0->data.cmp (op1->data,
776 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) != -1;
778 default:
779 gcc_unreachable ();
783 /* Extend or truncate to a new mode.
784 If SAT_P, saturate the result to the max or the min.
785 Return true, if !SAT_P and overflow. */
787 bool
788 fixed_convert (FIXED_VALUE_TYPE *f, enum machine_mode mode,
789 const FIXED_VALUE_TYPE *a, bool sat_p)
791 bool overflow_p = false;
792 if (mode == a->mode)
794 *f = *a;
795 return overflow_p;
798 if (GET_MODE_FBIT (mode) > GET_MODE_FBIT (a->mode))
800 /* Left shift a to temp_high, temp_low based on a->mode. */
801 double_int temp_high, temp_low;
802 int amount = GET_MODE_FBIT (mode) - GET_MODE_FBIT (a->mode);
803 temp_low = a->data.lshift (amount,
804 HOST_BITS_PER_DOUBLE_INT,
805 SIGNED_FIXED_POINT_MODE_P (a->mode));
806 /* Logical shift right to temp_high. */
807 temp_high = a->data.llshift (amount - HOST_BITS_PER_DOUBLE_INT,
808 HOST_BITS_PER_DOUBLE_INT);
809 if (SIGNED_FIXED_POINT_MODE_P (a->mode)
810 && a->data.high < 0) /* Signed-extend temp_high. */
811 temp_high = temp_high.sext (amount);
812 f->mode = mode;
813 f->data = temp_low;
814 if (SIGNED_FIXED_POINT_MODE_P (a->mode) ==
815 SIGNED_FIXED_POINT_MODE_P (f->mode))
816 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
817 sat_p);
818 else
820 /* Take care of the cases when converting between signed and
821 unsigned. */
822 if (SIGNED_FIXED_POINT_MODE_P (a->mode))
824 /* Signed -> Unsigned. */
825 if (a->data.high < 0)
827 if (sat_p)
829 f->data.low = 0; /* Set to zero. */
830 f->data.high = 0; /* Set to zero. */
832 else
833 overflow_p = true;
835 else
836 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
837 &f->data, sat_p);
839 else
841 /* Unsigned -> Signed. */
842 if (temp_high.high < 0)
844 if (sat_p)
846 /* Set to maximum. */
847 f->data.low = -1; /* Set to all ones. */
848 f->data.high = -1; /* Set to all ones. */
849 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
850 + GET_MODE_IBIT (f->mode));
851 /* Clear the sign. */
853 else
854 overflow_p = true;
856 else
857 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
858 &f->data, sat_p);
862 else
864 /* Right shift a to temp based on a->mode. */
865 double_int temp;
866 temp = a->data.lshift (GET_MODE_FBIT (mode) - GET_MODE_FBIT (a->mode),
867 HOST_BITS_PER_DOUBLE_INT,
868 SIGNED_FIXED_POINT_MODE_P (a->mode));
869 f->mode = mode;
870 f->data = temp;
871 if (SIGNED_FIXED_POINT_MODE_P (a->mode) ==
872 SIGNED_FIXED_POINT_MODE_P (f->mode))
873 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
874 else
876 /* Take care of the cases when converting between signed and
877 unsigned. */
878 if (SIGNED_FIXED_POINT_MODE_P (a->mode))
880 /* Signed -> Unsigned. */
881 if (a->data.high < 0)
883 if (sat_p)
885 f->data.low = 0; /* Set to zero. */
886 f->data.high = 0; /* Set to zero. */
888 else
889 overflow_p = true;
891 else
892 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data,
893 sat_p);
895 else
897 /* Unsigned -> Signed. */
898 if (temp.high < 0)
900 if (sat_p)
902 /* Set to maximum. */
903 f->data.low = -1; /* Set to all ones. */
904 f->data.high = -1; /* Set to all ones. */
905 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
906 + GET_MODE_IBIT (f->mode));
907 /* Clear the sign. */
909 else
910 overflow_p = true;
912 else
913 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data,
914 sat_p);
919 f->data = f->data.ext (SIGNED_FIXED_POINT_MODE_P (f->mode)
920 + GET_MODE_FBIT (f->mode)
921 + GET_MODE_IBIT (f->mode),
922 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
923 return overflow_p;
926 /* Convert to a new fixed-point mode from an integer.
927 If UNSIGNED_P, this integer is unsigned.
928 If SAT_P, saturate the result to the max or the min.
929 Return true, if !SAT_P and overflow. */
931 bool
932 fixed_convert_from_int (FIXED_VALUE_TYPE *f, enum machine_mode mode,
933 double_int a, bool unsigned_p, bool sat_p)
935 bool overflow_p = false;
936 /* Left shift a to temp_high, temp_low. */
937 double_int temp_high, temp_low;
938 int amount = GET_MODE_FBIT (mode);
939 if (amount == HOST_BITS_PER_DOUBLE_INT)
941 temp_high = a;
942 temp_low.low = 0;
943 temp_low.high = 0;
945 else
947 temp_low = a.llshift (amount, HOST_BITS_PER_DOUBLE_INT);
949 /* Logical shift right to temp_high. */
950 temp_high = a.llshift (amount - HOST_BITS_PER_DOUBLE_INT,
951 HOST_BITS_PER_DOUBLE_INT);
953 if (!unsigned_p && a.high < 0) /* Signed-extend temp_high. */
954 temp_high = temp_high.sext (amount);
956 f->mode = mode;
957 f->data = temp_low;
959 if (unsigned_p == UNSIGNED_FIXED_POINT_MODE_P (f->mode))
960 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
961 sat_p);
962 else
964 /* Take care of the cases when converting between signed and unsigned. */
965 if (!unsigned_p)
967 /* Signed -> Unsigned. */
968 if (a.high < 0)
970 if (sat_p)
972 f->data.low = 0; /* Set to zero. */
973 f->data.high = 0; /* Set to zero. */
975 else
976 overflow_p = true;
978 else
979 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
980 &f->data, sat_p);
982 else
984 /* Unsigned -> Signed. */
985 if (temp_high.high < 0)
987 if (sat_p)
989 /* Set to maximum. */
990 f->data.low = -1; /* Set to all ones. */
991 f->data.high = -1; /* Set to all ones. */
992 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
993 + GET_MODE_IBIT (f->mode));
994 /* Clear the sign. */
996 else
997 overflow_p = true;
999 else
1000 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
1001 &f->data, sat_p);
1004 f->data = f->data.ext (SIGNED_FIXED_POINT_MODE_P (f->mode)
1005 + GET_MODE_FBIT (f->mode)
1006 + GET_MODE_IBIT (f->mode),
1007 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
1008 return overflow_p;
1011 /* Convert to a new fixed-point mode from a real.
1012 If SAT_P, saturate the result to the max or the min.
1013 Return true, if !SAT_P and overflow. */
1015 bool
1016 fixed_convert_from_real (FIXED_VALUE_TYPE *f, enum machine_mode mode,
1017 const REAL_VALUE_TYPE *a, bool sat_p)
1019 bool overflow_p = false;
1020 REAL_VALUE_TYPE real_value, fixed_value, base_value;
1021 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
1022 int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
1023 unsigned int fbit = GET_MODE_FBIT (mode);
1024 enum fixed_value_range_code temp;
1026 real_value = *a;
1027 f->mode = mode;
1028 real_2expN (&base_value, fbit, mode);
1029 real_arithmetic (&fixed_value, MULT_EXPR, &real_value, &base_value);
1030 real_to_integer2 ((HOST_WIDE_INT *)&f->data.low, &f->data.high, &fixed_value);
1031 temp = check_real_for_fixed_mode (&real_value, mode);
1032 if (temp == FIXED_UNDERFLOW) /* Minimum. */
1034 if (sat_p)
1036 if (unsigned_p)
1038 f->data.low = 0;
1039 f->data.high = 0;
1041 else
1043 f->data.low = 1;
1044 f->data.high = 0;
1045 f->data = f->data.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
1046 f->data = f->data.sext (1 + i_f_bits);
1049 else
1050 overflow_p = true;
1052 else if (temp == FIXED_GT_MAX_EPS || temp == FIXED_MAX_EPS) /* Maximum. */
1054 if (sat_p)
1056 f->data.low = -1;
1057 f->data.high = -1;
1058 f->data = f->data.zext (i_f_bits);
1060 else
1061 overflow_p = true;
1063 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
1064 return overflow_p;
1067 /* Convert to a new real mode from a fixed-point. */
1069 void
1070 real_convert_from_fixed (REAL_VALUE_TYPE *r, enum machine_mode mode,
1071 const FIXED_VALUE_TYPE *f)
1073 REAL_VALUE_TYPE base_value, fixed_value, real_value;
1075 real_2expN (&base_value, GET_MODE_FBIT (f->mode), f->mode);
1076 real_from_integer (&fixed_value, VOIDmode, f->data.low, f->data.high,
1077 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
1078 real_arithmetic (&real_value, RDIV_EXPR, &fixed_value, &base_value);
1079 real_convert (r, mode, &real_value);
1082 /* Determine whether a fixed-point value F is negative. */
1084 bool
1085 fixed_isneg (const FIXED_VALUE_TYPE *f)
1087 if (SIGNED_FIXED_POINT_MODE_P (f->mode))
1089 int i_f_bits = GET_MODE_IBIT (f->mode) + GET_MODE_FBIT (f->mode);
1090 int sign_bit = get_fixed_sign_bit (f->data, i_f_bits);
1091 if (sign_bit == 1)
1092 return true;
1095 return false;