target-supports.exp (check_effective_target_mips_soft_float): Return true for MIPS16...
[official-gcc.git] / gcc / fixed-value.c
blob8c8d37190441bf3d3b8629e97f401c3c26958e49
1 /* Fixed-point arithmetic support.
2 Copyright (C) 2006, 2007 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 "toplev.h"
26 #include "fixed-value.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, enum 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;
85 /* Initialize from a decimal or hexadecimal string. */
87 void
88 fixed_from_string (FIXED_VALUE_TYPE *f, const char *str, enum machine_mode mode)
90 REAL_VALUE_TYPE real_value, fixed_value, base_value;
91 unsigned int fbit;
92 enum fixed_value_range_code temp;
94 f->mode = mode;
95 fbit = GET_MODE_FBIT (mode);
97 real_from_string (&real_value, str);
98 temp = check_real_for_fixed_mode (&real_value, f->mode);
99 /* We don't want to warn the case when the _Fract value is 1.0. */
100 if (temp == FIXED_UNDERFLOW
101 || temp == FIXED_GT_MAX_EPS
102 || (temp == FIXED_MAX_EPS && ALL_ACCUM_MODE_P (f->mode)))
103 warning (OPT_Woverflow,
104 "large fixed-point constant implicitly truncated to fixed-point type");
105 real_2expN (&base_value, fbit, mode);
106 real_arithmetic (&fixed_value, MULT_EXPR, &real_value, &base_value);
107 real_to_integer2 ((HOST_WIDE_INT *)&f->data.low, &f->data.high,
108 &fixed_value);
110 if (temp == FIXED_MAX_EPS && ALL_FRACT_MODE_P (f->mode))
112 /* From the spec, we need to evaluate 1 to the maximal value. */
113 f->data.low = -1;
114 f->data.high = -1;
115 f->data = double_int_ext (f->data,
116 GET_MODE_FBIT (f->mode)
117 + GET_MODE_IBIT (f->mode), 1);
119 else
120 f->data = double_int_ext (f->data,
121 SIGNED_FIXED_POINT_MODE_P (f->mode)
122 + GET_MODE_FBIT (f->mode)
123 + GET_MODE_IBIT (f->mode),
124 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
127 /* Render F as a decimal floating point constant. */
129 void
130 fixed_to_decimal (char *str, const FIXED_VALUE_TYPE *f_orig,
131 size_t buf_size)
133 REAL_VALUE_TYPE real_value, base_value, fixed_value;
135 real_2expN (&base_value, GET_MODE_FBIT (f_orig->mode), f_orig->mode);
136 real_from_integer (&real_value, VOIDmode, f_orig->data.low, f_orig->data.high,
137 UNSIGNED_FIXED_POINT_MODE_P (f_orig->mode));
138 real_arithmetic (&fixed_value, RDIV_EXPR, &real_value, &base_value);
139 real_to_decimal (str, &fixed_value, buf_size, 0, 1);
142 /* If SAT_P, saturate A to the maximum or the minimum, and save to *F based on
143 the machine mode MODE.
144 Do not modify *F otherwise.
145 This function assumes the width of double_int is greater than the width
146 of the fixed-point value (the sum of a possible sign bit, possible ibits,
147 and fbits).
148 Return true, if !SAT_P and overflow. */
150 static bool
151 fixed_saturate1 (enum machine_mode mode, double_int a, double_int *f,
152 bool sat_p)
154 bool overflow_p = false;
155 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
156 int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
158 if (unsigned_p) /* Unsigned type. */
160 double_int max;
161 max.low = -1;
162 max.high = -1;
163 max = double_int_ext (max, i_f_bits, 1);
164 if (double_int_cmp (a, max, 1) == 1)
166 if (sat_p)
167 *f = max;
168 else
169 overflow_p = true;
172 else /* Signed type. */
174 double_int max, min;
175 max.high = -1;
176 max.low = -1;
177 max = double_int_ext (max, i_f_bits, 1);
178 min.high = 0;
179 min.low = 1;
180 lshift_double (min.low, min.high, i_f_bits,
181 2 * HOST_BITS_PER_WIDE_INT,
182 &min.low, &min.high, 1);
183 min = double_int_ext (min, 1 + i_f_bits, 0);
184 if (double_int_cmp (a, max, 0) == 1)
186 if (sat_p)
187 *f = max;
188 else
189 overflow_p = true;
191 else if (double_int_cmp (a, min, 0) == -1)
193 if (sat_p)
194 *f = min;
195 else
196 overflow_p = true;
199 return overflow_p;
202 /* If SAT_P, saturate {A_HIGH, A_LOW} to the maximum or the minimum, and
203 save to *F based on the machine mode MODE.
204 Do not modify *F otherwise.
205 This function assumes the width of two double_int is greater than the width
206 of the fixed-point value (the sum of a possible sign bit, possible ibits,
207 and fbits).
208 Return true, if !SAT_P and overflow. */
210 static bool
211 fixed_saturate2 (enum machine_mode mode, double_int a_high, double_int a_low,
212 double_int *f, bool sat_p)
214 bool overflow_p = false;
215 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
216 int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
218 if (unsigned_p) /* Unsigned type. */
220 double_int max_r, max_s;
221 max_r.high = 0;
222 max_r.low = 0;
223 max_s.high = -1;
224 max_s.low = -1;
225 max_s = double_int_ext (max_s, i_f_bits, 1);
226 if (double_int_cmp (a_high, max_r, 1) == 1
227 || (double_int_equal_p (a_high, max_r) &&
228 double_int_cmp (a_low, max_s, 1) == 1))
230 if (sat_p)
231 *f = max_s;
232 else
233 overflow_p = true;
236 else /* Signed type. */
238 double_int max_r, max_s, min_r, min_s;
239 max_r.high = 0;
240 max_r.low = 0;
241 max_s.high = -1;
242 max_s.low = -1;
243 max_s = double_int_ext (max_s, i_f_bits, 1);
244 min_r.high = -1;
245 min_r.low = -1;
246 min_s.high = 0;
247 min_s.low = 1;
248 lshift_double (min_s.low, min_s.high, i_f_bits,
249 2 * HOST_BITS_PER_WIDE_INT,
250 &min_s.low, &min_s.high, 1);
251 min_s = double_int_ext (min_s, 1 + i_f_bits, 0);
252 if (double_int_cmp (a_high, max_r, 0) == 1
253 || (double_int_equal_p (a_high, max_r) &&
254 double_int_cmp (a_low, max_s, 1) == 1))
256 if (sat_p)
257 *f = max_s;
258 else
259 overflow_p = true;
261 else if (double_int_cmp (a_high, min_r, 0) == -1
262 || (double_int_equal_p (a_high, min_r) &&
263 double_int_cmp (a_low, min_s, 1) == -1))
265 if (sat_p)
266 *f = min_s;
267 else
268 overflow_p = true;
271 return overflow_p;
274 /* Return the sign bit based on I_F_BITS. */
276 static inline int
277 get_fixed_sign_bit (double_int a, int i_f_bits)
279 if (i_f_bits < HOST_BITS_PER_WIDE_INT)
280 return (a.low >> i_f_bits) & 1;
281 else
282 return (a.high >> (i_f_bits - HOST_BITS_PER_WIDE_INT)) & 1;
285 /* Calculate F = A + (SUBTRACT_P ? -B : B).
286 If SAT_P, saturate the result to the max or the min.
287 Return true, if !SAT_P and overflow. */
289 static bool
290 do_fixed_add (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
291 const FIXED_VALUE_TYPE *b, bool subtract_p, bool sat_p)
293 bool overflow_p = false;
294 double_int temp = subtract_p ? double_int_neg (b->data) : b->data;
295 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
296 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
297 f->mode = a->mode;
298 f->data = double_int_add (a->data, temp);
299 if (unsigned_p) /* Unsigned type. */
301 if (subtract_p) /* Unsigned subtraction. */
303 if (double_int_cmp (a->data, b->data, 1) == -1)
305 if (sat_p)
307 f->data.high = 0;
308 f->data.low = 0;
310 else
311 overflow_p = true;
314 else /* Unsigned addition. */
316 f->data = double_int_ext (f->data, i_f_bits, 1);
317 if (double_int_cmp (f->data, a->data, 1) == -1
318 || double_int_cmp (f->data, b->data, 1) == -1)
320 if (sat_p)
322 f->data.high = -1;
323 f->data.low = -1;
325 else
326 overflow_p = true;
330 else /* Signed type. */
332 if ((!subtract_p
333 && (get_fixed_sign_bit (a->data, i_f_bits)
334 == get_fixed_sign_bit (b->data, i_f_bits))
335 && (get_fixed_sign_bit (a->data, i_f_bits)
336 != get_fixed_sign_bit (f->data, i_f_bits)))
337 || (subtract_p
338 && (get_fixed_sign_bit (a->data, i_f_bits)
339 != get_fixed_sign_bit (b->data, i_f_bits))
340 && (get_fixed_sign_bit (a->data, i_f_bits)
341 != get_fixed_sign_bit (f->data, i_f_bits))))
343 if (sat_p)
345 f->data.low = 1;
346 f->data.high = 0;
347 lshift_double (f->data.low, f->data.high, i_f_bits,
348 2 * HOST_BITS_PER_WIDE_INT,
349 &f->data.low, &f->data.high, 1);
350 if (get_fixed_sign_bit (a->data, i_f_bits) == 0)
352 double_int one;
353 one.low = 1;
354 one.high = 0;
355 f->data = double_int_add (f->data, double_int_neg (one));
358 else
359 overflow_p = true;
362 f->data = double_int_ext (f->data, (!unsigned_p) + i_f_bits, unsigned_p);
363 return overflow_p;
366 /* Calculate F = A * B.
367 If SAT_P, saturate the result to the max or the min.
368 Return true, if !SAT_P and overflow. */
370 static bool
371 do_fixed_multiply (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
372 const FIXED_VALUE_TYPE *b, bool sat_p)
374 bool overflow_p = false;
375 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
376 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
377 f->mode = a->mode;
378 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT)
380 f->data = double_int_mul (a->data, b->data);
381 lshift_double (f->data.low, f->data.high,
382 (-GET_MODE_FBIT (f->mode)),
383 2 * HOST_BITS_PER_WIDE_INT,
384 &f->data.low, &f->data.high, !unsigned_p);
385 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
387 else
389 /* The result of multiplication expands to two double_int. */
390 double_int a_high, a_low, b_high, b_low;
391 double_int high_high, high_low, low_high, low_low;
392 double_int r, s, temp1, temp2;
393 int carry = 0;
395 /* Decompose a and b to four double_int. */
396 a_high.low = a->data.high;
397 a_high.high = 0;
398 a_low.low = a->data.low;
399 a_low.high = 0;
400 b_high.low = b->data.high;
401 b_high.high = 0;
402 b_low.low = b->data.low;
403 b_low.high = 0;
405 /* Perform four multiplications. */
406 low_low = double_int_mul (a_low, b_low);
407 low_high = double_int_mul (a_low, b_high);
408 high_low = double_int_mul (a_high, b_low);
409 high_high = double_int_mul (a_high, b_high);
411 /* Accumulate four results to {r, s}. */
412 temp1.high = high_low.low;
413 temp1.low = 0;
414 s = double_int_add (low_low, temp1);
415 if (double_int_cmp (s, low_low, 1) == -1
416 || double_int_cmp (s, temp1, 1) == -1)
417 carry ++; /* Carry */
418 temp1.high = s.high;
419 temp1.low = s.low;
420 temp2.high = low_high.low;
421 temp2.low = 0;
422 s = double_int_add (temp1, temp2);
423 if (double_int_cmp (s, temp1, 1) == -1
424 || double_int_cmp (s, temp2, 1) == -1)
425 carry ++; /* Carry */
427 temp1.low = high_low.high;
428 temp1.high = 0;
429 r = double_int_add (high_high, temp1);
430 temp1.low = low_high.high;
431 temp1.high = 0;
432 r = double_int_add (r, temp1);
433 temp1.low = carry;
434 temp1.high = 0;
435 r = double_int_add (r, temp1);
437 /* We need to add neg(b) to r, if a < 0. */
438 if (!unsigned_p && a->data.high < 0)
439 r = double_int_add (r, double_int_neg (b->data));
440 /* We need to add neg(a) to r, if b < 0. */
441 if (!unsigned_p && b->data.high < 0)
442 r = double_int_add (r, double_int_neg (a->data));
444 /* Shift right the result by FBIT. */
445 if (GET_MODE_FBIT (f->mode) == 2 * HOST_BITS_PER_WIDE_INT)
447 s.low = r.low;
448 s.high = r.high;
449 if (unsigned_p)
451 r.low = 0;
452 r.high = 0;
454 else
456 r.low = -1;
457 r.high = -1;
459 f->data.low = s.low;
460 f->data.high = s.high;
462 else
464 lshift_double (s.low, s.high,
465 (-GET_MODE_FBIT (f->mode)),
466 2 * HOST_BITS_PER_WIDE_INT,
467 &s.low, &s.high, 0);
468 lshift_double (r.low, r.high,
469 (2 * HOST_BITS_PER_WIDE_INT
470 - GET_MODE_FBIT (f->mode)),
471 2 * HOST_BITS_PER_WIDE_INT,
472 &f->data.low, &f->data.high, 0);
473 f->data.low = f->data.low | s.low;
474 f->data.high = f->data.high | s.high;
475 s.low = f->data.low;
476 s.high = f->data.high;
477 lshift_double (r.low, r.high,
478 (-GET_MODE_FBIT (f->mode)),
479 2 * HOST_BITS_PER_WIDE_INT,
480 &r.low, &r.high, !unsigned_p);
483 overflow_p = fixed_saturate2 (f->mode, r, s, &f->data, sat_p);
486 f->data = double_int_ext (f->data, (!unsigned_p) + i_f_bits, unsigned_p);
487 return overflow_p;
490 /* Calculate F = A / B.
491 If SAT_P, saturate the result to the max or the min.
492 Return true, if !SAT_P and overflow. */
494 static bool
495 do_fixed_divide (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
496 const FIXED_VALUE_TYPE *b, bool sat_p)
498 bool overflow_p = false;
499 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
500 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
501 f->mode = a->mode;
502 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT)
504 lshift_double (a->data.low, a->data.high,
505 GET_MODE_FBIT (f->mode),
506 2 * HOST_BITS_PER_WIDE_INT,
507 &f->data.low, &f->data.high, !unsigned_p);
508 f->data = double_int_div (f->data, b->data, unsigned_p, TRUNC_DIV_EXPR);
509 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
511 else
513 double_int pos_a, pos_b, r, s;
514 double_int quo_r, quo_s, mod, temp;
515 int num_of_neg = 0;
516 int i;
518 /* If a < 0, negate a. */
519 if (!unsigned_p && a->data.high < 0)
521 pos_a = double_int_neg (a->data);
522 num_of_neg ++;
524 else
525 pos_a = a->data;
527 /* If b < 0, negate b. */
528 if (!unsigned_p && b->data.high < 0)
530 pos_b = double_int_neg (b->data);
531 num_of_neg ++;
533 else
534 pos_b = b->data;
536 /* Left shift pos_a to {r, s} by FBIT. */
537 if (GET_MODE_FBIT (f->mode) == 2 * HOST_BITS_PER_WIDE_INT)
539 r = pos_a;
540 s.high = 0;
541 s.low = 0;
543 else
545 lshift_double (pos_a.low, pos_a.high,
546 GET_MODE_FBIT (f->mode),
547 2 * HOST_BITS_PER_WIDE_INT,
548 &s.low, &s.high, 0);
549 lshift_double (pos_a.low, pos_a.high,
550 - (2 * HOST_BITS_PER_WIDE_INT
551 - GET_MODE_FBIT (f->mode)),
552 2 * HOST_BITS_PER_WIDE_INT,
553 &r.low, &r.high, 0);
556 /* Divide r by pos_b to quo_r. The remainder is in mod. */
557 div_and_round_double (TRUNC_DIV_EXPR, 1, r.low, r.high, pos_b.low,
558 pos_b.high, &quo_r.low, &quo_r.high, &mod.low,
559 &mod.high);
561 quo_s.high = 0;
562 quo_s.low = 0;
564 for (i = 0; i < 2 * HOST_BITS_PER_WIDE_INT; i++)
566 /* Record the leftmost bit of mod. */
567 int leftmost_mod = (mod.high < 0);
569 /* Shift left mod by 1 bit. */
570 lshift_double (mod.low, mod.high, 1, 2 * HOST_BITS_PER_WIDE_INT,
571 &mod.low, &mod.high, 0);
573 /* Test the leftmost bit of s to add to mod. */
574 if (s.high < 0)
575 mod.low += 1;
577 /* Shift left quo_s by 1 bit. */
578 lshift_double (quo_s.low, quo_s.high, 1, 2 * HOST_BITS_PER_WIDE_INT,
579 &quo_s.low, &quo_s.high, 0);
581 /* Try to calculate (mod - pos_b). */
582 temp = double_int_add (mod, double_int_neg (pos_b));
584 if (leftmost_mod == 1 || double_int_cmp (mod, pos_b, 1) != -1)
586 quo_s.low += 1;
587 mod = temp;
590 /* Shift left s by 1 bit. */
591 lshift_double (s.low, s.high, 1, 2 * HOST_BITS_PER_WIDE_INT,
592 &s.low, &s.high, 0);
596 if (num_of_neg == 1)
598 quo_s = double_int_neg (quo_s);
599 if (quo_s.high == 0 && quo_s.low == 0)
600 quo_r = double_int_neg (quo_r);
601 else
603 quo_r.low = ~quo_r.low;
604 quo_r.high = ~quo_r.high;
608 f->data = quo_s;
609 overflow_p = fixed_saturate2 (f->mode, quo_r, quo_s, &f->data, sat_p);
612 f->data = double_int_ext (f->data, (!unsigned_p) + i_f_bits, unsigned_p);
613 return overflow_p;
616 /* Calculate F = A << B if LEFT_P. Otherwise, F = A >> B.
617 If SAT_P, saturate the result to the max or the min.
618 Return true, if !SAT_P and overflow. */
620 static bool
621 do_fixed_shift (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
622 const FIXED_VALUE_TYPE *b, bool left_p, bool sat_p)
624 bool overflow_p = false;
625 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
626 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
627 f->mode = a->mode;
629 if (b->data.low == 0)
631 f->data = a->data;
632 return overflow_p;
635 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT || (!left_p))
637 lshift_double (a->data.low, a->data.high,
638 left_p ? b->data.low : (-b->data.low),
639 2 * HOST_BITS_PER_WIDE_INT,
640 &f->data.low, &f->data.high, !unsigned_p);
641 if (left_p) /* Only left shift saturates. */
642 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
644 else /* We need two double_int to store the left-shift result. */
646 double_int temp_high, temp_low;
647 if (b->data.low == 2 * HOST_BITS_PER_WIDE_INT)
649 temp_high = a->data;
650 temp_low.high = 0;
651 temp_low.low = 0;
653 else
655 lshift_double (a->data.low, a->data.high,
656 b->data.low,
657 2 * HOST_BITS_PER_WIDE_INT,
658 &temp_low.low, &temp_low.high, !unsigned_p);
659 /* Logical shift right to temp_high. */
660 lshift_double (a->data.low, a->data.high,
661 b->data.low - 2 * HOST_BITS_PER_WIDE_INT,
662 2 * HOST_BITS_PER_WIDE_INT,
663 &temp_high.low, &temp_high.high, 0);
665 if (!unsigned_p && a->data.high < 0) /* Signed-extend temp_high. */
666 temp_high = double_int_ext (temp_high, 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 = double_int_ext (f->data, (!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 = double_int_neg (a->data);
687 f->data = double_int_ext (f->data, (!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 = double_int_ext (f->data, i_f_bits, 1);
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 = icode;
778 gcc_assert (op0->mode == op1->mode);
780 switch (code)
782 case NE_EXPR:
783 return !double_int_equal_p (op0->data, op1->data);
785 case EQ_EXPR:
786 return double_int_equal_p (op0->data, op1->data);
788 case LT_EXPR:
789 return double_int_cmp (op0->data, op1->data,
790 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) == -1;
792 case LE_EXPR:
793 return double_int_cmp (op0->data, op1->data,
794 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) != 1;
796 case GT_EXPR:
797 return double_int_cmp (op0->data, op1->data,
798 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) == 1;
800 case GE_EXPR:
801 return double_int_cmp (op0->data, 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, enum 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 lshift_double (a->data.low, a->data.high,
830 amount,
831 2 * HOST_BITS_PER_WIDE_INT,
832 &temp_low.low, &temp_low.high,
833 SIGNED_FIXED_POINT_MODE_P (a->mode));
834 /* Logical shift right to temp_high. */
835 lshift_double (a->data.low, a->data.high,
836 amount - 2 * HOST_BITS_PER_WIDE_INT,
837 2 * HOST_BITS_PER_WIDE_INT,
838 &temp_high.low, &temp_high.high, 0);
839 if (SIGNED_FIXED_POINT_MODE_P (a->mode)
840 && a->data.high < 0) /* Signed-extend temp_high. */
841 temp_high = double_int_ext (temp_high, amount, 0);
842 f->mode = mode;
843 f->data = temp_low;
844 if (SIGNED_FIXED_POINT_MODE_P (a->mode) ==
845 SIGNED_FIXED_POINT_MODE_P (f->mode))
846 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
847 sat_p);
848 else
850 /* Take care of the cases when converting between signed and
851 unsigned. */
852 if (SIGNED_FIXED_POINT_MODE_P (a->mode))
854 /* Signed -> Unsigned. */
855 if (a->data.high < 0)
857 if (sat_p)
859 f->data.low = 0; /* Set to zero. */
860 f->data.high = 0; /* Set to zero. */
862 else
863 overflow_p = true;
865 else
866 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
867 &f->data, sat_p);
869 else
871 /* Unsigned -> Signed. */
872 if (temp_high.high < 0)
874 if (sat_p)
876 /* Set to maximum. */
877 f->data.low = -1; /* Set to all ones. */
878 f->data.high = -1; /* Set to all ones. */
879 f->data = double_int_ext (f->data,
880 GET_MODE_FBIT (f->mode)
881 + GET_MODE_IBIT (f->mode),
882 1); /* Clear the sign. */
884 else
885 overflow_p = true;
887 else
888 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
889 &f->data, sat_p);
893 else
895 /* Right shift a to temp based on a->mode. */
896 double_int temp;
897 lshift_double (a->data.low, a->data.high,
898 GET_MODE_FBIT (mode) - GET_MODE_FBIT (a->mode),
899 2 * HOST_BITS_PER_WIDE_INT,
900 &temp.low, &temp.high,
901 SIGNED_FIXED_POINT_MODE_P (a->mode));
902 f->mode = mode;
903 f->data = temp;
904 if (SIGNED_FIXED_POINT_MODE_P (a->mode) ==
905 SIGNED_FIXED_POINT_MODE_P (f->mode))
906 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
907 else
909 /* Take care of the cases when converting between signed and
910 unsigned. */
911 if (SIGNED_FIXED_POINT_MODE_P (a->mode))
913 /* Signed -> Unsigned. */
914 if (a->data.high < 0)
916 if (sat_p)
918 f->data.low = 0; /* Set to zero. */
919 f->data.high = 0; /* Set to zero. */
921 else
922 overflow_p = true;
924 else
925 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data,
926 sat_p);
928 else
930 /* Unsigned -> Signed. */
931 if (temp.high < 0)
933 if (sat_p)
935 /* Set to maximum. */
936 f->data.low = -1; /* Set to all ones. */
937 f->data.high = -1; /* Set to all ones. */
938 f->data = double_int_ext (f->data,
939 GET_MODE_FBIT (f->mode)
940 + GET_MODE_IBIT (f->mode),
941 1); /* Clear the sign. */
943 else
944 overflow_p = true;
946 else
947 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data,
948 sat_p);
953 f->data = double_int_ext (f->data,
954 SIGNED_FIXED_POINT_MODE_P (f->mode)
955 + GET_MODE_FBIT (f->mode)
956 + GET_MODE_IBIT (f->mode),
957 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
958 return overflow_p;
961 /* Convert to a new fixed-point mode from an integer.
962 If UNSIGNED_P, this integer is unsigned.
963 If SAT_P, saturate the result to the max or the min.
964 Return true, if !SAT_P and overflow. */
966 bool
967 fixed_convert_from_int (FIXED_VALUE_TYPE *f, enum machine_mode mode,
968 double_int a, bool unsigned_p, bool sat_p)
970 bool overflow_p = false;
971 /* Left shift a to temp_high, temp_low. */
972 double_int temp_high, temp_low;
973 int amount = GET_MODE_FBIT (mode);
974 if (amount == 2 * HOST_BITS_PER_WIDE_INT)
976 temp_high = a;
977 temp_low.low = 0;
978 temp_low.high = 0;
980 else
982 lshift_double (a.low, a.high,
983 amount,
984 2 * HOST_BITS_PER_WIDE_INT,
985 &temp_low.low, &temp_low.high, 0);
987 /* Logical shift right to temp_high. */
988 lshift_double (a.low, a.high,
989 amount - 2 * HOST_BITS_PER_WIDE_INT,
990 2 * HOST_BITS_PER_WIDE_INT,
991 &temp_high.low, &temp_high.high, 0);
993 if (!unsigned_p && a.high < 0) /* Signed-extend temp_high. */
994 temp_high = double_int_ext (temp_high, amount, 0);
996 f->mode = mode;
997 f->data = temp_low;
999 if (unsigned_p == UNSIGNED_FIXED_POINT_MODE_P (f->mode))
1000 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
1001 sat_p);
1002 else
1004 /* Take care of the cases when converting between signed and unsigned. */
1005 if (!unsigned_p)
1007 /* Signed -> Unsigned. */
1008 if (a.high < 0)
1010 if (sat_p)
1012 f->data.low = 0; /* Set to zero. */
1013 f->data.high = 0; /* Set to zero. */
1015 else
1016 overflow_p = true;
1018 else
1019 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
1020 &f->data, sat_p);
1022 else
1024 /* Unsigned -> Signed. */
1025 if (temp_high.high < 0)
1027 if (sat_p)
1029 /* Set to maximum. */
1030 f->data.low = -1; /* Set to all ones. */
1031 f->data.high = -1; /* Set to all ones. */
1032 f->data = double_int_ext (f->data,
1033 GET_MODE_FBIT (f->mode)
1034 + GET_MODE_IBIT (f->mode),
1035 1); /* Clear the sign. */
1037 else
1038 overflow_p = true;
1040 else
1041 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
1042 &f->data, sat_p);
1045 f->data = double_int_ext (f->data,
1046 SIGNED_FIXED_POINT_MODE_P (f->mode)
1047 + GET_MODE_FBIT (f->mode)
1048 + GET_MODE_IBIT (f->mode),
1049 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
1050 return overflow_p;
1053 /* Convert to a new fixed-point mode from a real.
1054 If SAT_P, saturate the result to the max or the min.
1055 Return true, if !SAT_P and overflow. */
1057 bool
1058 fixed_convert_from_real (FIXED_VALUE_TYPE *f, enum machine_mode mode,
1059 const REAL_VALUE_TYPE *a, bool sat_p)
1061 bool overflow_p = false;
1062 REAL_VALUE_TYPE real_value, fixed_value, base_value;
1063 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
1064 int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
1065 unsigned int fbit = GET_MODE_FBIT (mode);
1066 enum fixed_value_range_code temp;
1068 real_value = *a;
1069 f->mode = mode;
1070 real_2expN (&base_value, fbit, mode);
1071 real_arithmetic (&fixed_value, MULT_EXPR, &real_value, &base_value);
1072 real_to_integer2 ((HOST_WIDE_INT *)&f->data.low, &f->data.high, &fixed_value);
1073 temp = check_real_for_fixed_mode (&real_value, mode);
1074 if (temp == FIXED_UNDERFLOW) /* Minimum. */
1076 if (sat_p)
1078 if (unsigned_p)
1080 f->data.low = 0;
1081 f->data.high = 0;
1083 else
1085 f->data.low = 1;
1086 f->data.high = 0;
1087 lshift_double (f->data.low, f->data.high, i_f_bits,
1088 2 * HOST_BITS_PER_WIDE_INT,
1089 &f->data.low, &f->data.high, 1);
1090 f->data = double_int_ext (f->data, 1 + i_f_bits, 0);
1093 else
1094 overflow_p = true;
1096 else if (temp == FIXED_GT_MAX_EPS || temp == FIXED_MAX_EPS) /* Maximum. */
1098 if (sat_p)
1100 f->data.low = -1;
1101 f->data.high = -1;
1102 f->data = double_int_ext (f->data, i_f_bits, 1);
1104 else
1105 overflow_p = true;
1107 f->data = double_int_ext (f->data, (!unsigned_p) + i_f_bits, unsigned_p);
1108 return overflow_p;
1111 /* Convert to a new real mode from a fixed-point. */
1113 void
1114 real_convert_from_fixed (REAL_VALUE_TYPE *r, enum machine_mode mode,
1115 const FIXED_VALUE_TYPE *f)
1117 REAL_VALUE_TYPE base_value, fixed_value, real_value;
1119 real_2expN (&base_value, GET_MODE_FBIT (f->mode), f->mode);
1120 real_from_integer (&fixed_value, VOIDmode, f->data.low, f->data.high,
1121 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
1122 real_arithmetic (&real_value, RDIV_EXPR, &fixed_value, &base_value);
1123 real_convert (r, mode, &real_value);
1126 /* Determine whether a fixed-point value F is negative. */
1128 bool
1129 fixed_isneg (const FIXED_VALUE_TYPE *f)
1131 if (SIGNED_FIXED_POINT_MODE_P (f->mode))
1133 int i_f_bits = GET_MODE_IBIT (f->mode) + GET_MODE_FBIT (f->mode);
1134 int sign_bit = get_fixed_sign_bit (f->data, i_f_bits);
1135 if (sign_bit == 1)
1136 return true;
1139 return false;