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
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
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/>. */
22 #include "coretypes.h"
27 #include "double-int.h"
33 #include "fixed-value.h"
35 #include "diagnostic-core.h"
38 /* Compare two fixed objects for bitwise identity. */
41 fixed_identical (const FIXED_VALUE_TYPE *a, const FIXED_VALUE_TYPE *b)
43 return (a->mode == b->mode
44 && a->data.high == b->data.high
45 && a->data.low == b->data.low);
48 /* Calculate a hash value. */
51 fixed_hash (const FIXED_VALUE_TYPE *f)
53 return (unsigned int) (f->data.low ^ f->data.high);
56 /* Define the enum code for the range of the fixed-point value. */
57 enum fixed_value_range_code {
58 FIXED_OK, /* The value is within the range. */
59 FIXED_UNDERFLOW, /* The value is less than the minimum. */
60 FIXED_GT_MAX_EPS, /* The value is greater than the maximum, but not equal
61 to the maximum plus the epsilon. */
62 FIXED_MAX_EPS /* The value equals the maximum plus the epsilon. */
65 /* Check REAL_VALUE against the range of the fixed-point mode.
66 Return FIXED_OK, if it is within the range.
67 FIXED_UNDERFLOW, if it is less than the minimum.
68 FIXED_GT_MAX_EPS, if it is greater than the maximum, but not equal to
69 the maximum plus the epsilon.
70 FIXED_MAX_EPS, if it is equal to the maximum plus the epsilon. */
72 static enum fixed_value_range_code
73 check_real_for_fixed_mode (REAL_VALUE_TYPE *real_value, machine_mode mode)
75 REAL_VALUE_TYPE max_value, min_value, epsilon_value;
77 real_2expN (&max_value, GET_MODE_IBIT (mode), mode);
78 real_2expN (&epsilon_value, -GET_MODE_FBIT (mode), mode);
80 if (SIGNED_FIXED_POINT_MODE_P (mode))
81 min_value = real_value_negate (&max_value);
83 real_from_string (&min_value, "0.0");
85 if (real_compare (LT_EXPR, real_value, &min_value))
86 return FIXED_UNDERFLOW;
87 if (real_compare (EQ_EXPR, real_value, &max_value))
89 real_arithmetic (&max_value, MINUS_EXPR, &max_value, &epsilon_value);
90 if (real_compare (GT_EXPR, real_value, &max_value))
91 return FIXED_GT_MAX_EPS;
96 /* Construct a CONST_FIXED from a bit payload and machine mode MODE.
97 The bits in PAYLOAD are sign-extended/zero-extended according to MODE. */
100 fixed_from_double_int (double_int payload, machine_mode mode)
102 FIXED_VALUE_TYPE value;
104 gcc_assert (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_DOUBLE_INT);
106 if (SIGNED_SCALAR_FIXED_POINT_MODE_P (mode))
107 value.data = payload.sext (1 + GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode));
108 else if (UNSIGNED_SCALAR_FIXED_POINT_MODE_P (mode))
109 value.data = payload.zext (GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode));
119 /* Initialize from a decimal or hexadecimal string. */
122 fixed_from_string (FIXED_VALUE_TYPE *f, const char *str, machine_mode mode)
124 REAL_VALUE_TYPE real_value, fixed_value, base_value;
126 enum fixed_value_range_code temp;
130 fbit = GET_MODE_FBIT (mode);
132 real_from_string (&real_value, str);
133 temp = check_real_for_fixed_mode (&real_value, f->mode);
134 /* We don't want to warn the case when the _Fract value is 1.0. */
135 if (temp == FIXED_UNDERFLOW
136 || temp == FIXED_GT_MAX_EPS
137 || (temp == FIXED_MAX_EPS && ALL_ACCUM_MODE_P (f->mode)))
138 warning (OPT_Woverflow,
139 "large fixed-point constant implicitly truncated to fixed-point type");
140 real_2expN (&base_value, fbit, mode);
141 real_arithmetic (&fixed_value, MULT_EXPR, &real_value, &base_value);
142 wide_int w = real_to_integer (&fixed_value, &fail,
143 GET_MODE_PRECISION (mode));
144 f->data.low = w.elt (0);
145 f->data.high = w.elt (1);
147 if (temp == FIXED_MAX_EPS && ALL_FRACT_MODE_P (f->mode))
149 /* From the spec, we need to evaluate 1 to the maximal value. */
152 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
153 + GET_MODE_IBIT (f->mode));
156 f->data = f->data.ext (SIGNED_FIXED_POINT_MODE_P (f->mode)
157 + GET_MODE_FBIT (f->mode)
158 + GET_MODE_IBIT (f->mode),
159 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
162 /* Render F as a decimal floating point constant. */
165 fixed_to_decimal (char *str, const FIXED_VALUE_TYPE *f_orig,
168 REAL_VALUE_TYPE real_value, base_value, fixed_value;
170 signop sgn = UNSIGNED_FIXED_POINT_MODE_P (f_orig->mode) ? UNSIGNED : SIGNED;
171 real_2expN (&base_value, GET_MODE_FBIT (f_orig->mode), f_orig->mode);
172 real_from_integer (&real_value, VOIDmode,
173 wide_int::from (f_orig->data,
174 GET_MODE_PRECISION (f_orig->mode), sgn),
176 real_arithmetic (&fixed_value, RDIV_EXPR, &real_value, &base_value);
177 real_to_decimal (str, &fixed_value, buf_size, 0, 1);
180 /* If SAT_P, saturate A to the maximum or the minimum, and save to *F based on
181 the machine mode MODE.
182 Do not modify *F otherwise.
183 This function assumes the width of double_int is greater than the width
184 of the fixed-point value (the sum of a possible sign bit, possible ibits,
186 Return true, if !SAT_P and overflow. */
189 fixed_saturate1 (machine_mode mode, double_int a, double_int *f,
192 bool overflow_p = false;
193 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
194 int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
196 if (unsigned_p) /* Unsigned type. */
201 max = max.zext (i_f_bits);
210 else /* Signed type. */
215 max = max.zext (i_f_bits);
218 min = min.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
219 min = min.sext (1 + i_f_bits);
227 else if (a.slt (min))
238 /* If SAT_P, saturate {A_HIGH, A_LOW} to the maximum or the minimum, and
239 save to *F based on the machine mode MODE.
240 Do not modify *F otherwise.
241 This function assumes the width of two double_int is greater than the width
242 of the fixed-point value (the sum of a possible sign bit, possible ibits,
244 Return true, if !SAT_P and overflow. */
247 fixed_saturate2 (machine_mode mode, double_int a_high, double_int a_low,
248 double_int *f, bool sat_p)
250 bool overflow_p = false;
251 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
252 int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
254 if (unsigned_p) /* Unsigned type. */
256 double_int max_r, max_s;
261 max_s = max_s.zext (i_f_bits);
262 if (a_high.ugt (max_r)
263 || (a_high == max_r &&
272 else /* Signed type. */
274 double_int max_r, max_s, min_r, min_s;
279 max_s = max_s.zext (i_f_bits);
284 min_s = min_s.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
285 min_s = min_s.sext (1 + i_f_bits);
286 if (a_high.sgt (max_r)
287 || (a_high == max_r &&
295 else if (a_high.slt (min_r)
296 || (a_high == min_r &&
308 /* Return the sign bit based on I_F_BITS. */
311 get_fixed_sign_bit (double_int a, int i_f_bits)
313 if (i_f_bits < HOST_BITS_PER_WIDE_INT)
314 return (a.low >> i_f_bits) & 1;
316 return (a.high >> (i_f_bits - HOST_BITS_PER_WIDE_INT)) & 1;
319 /* Calculate F = A + (SUBTRACT_P ? -B : B).
320 If SAT_P, saturate the result to the max or the min.
321 Return true, if !SAT_P and overflow. */
324 do_fixed_add (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
325 const FIXED_VALUE_TYPE *b, bool subtract_p, bool sat_p)
327 bool overflow_p = false;
332 /* This was a conditional expression but it triggered a bug in
339 unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
340 i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
342 f->data = a->data + temp;
343 if (unsigned_p) /* Unsigned type. */
345 if (subtract_p) /* Unsigned subtraction. */
347 if (a->data.ult (b->data))
358 else /* Unsigned addition. */
360 f->data = f->data.zext (i_f_bits);
361 if (f->data.ult (a->data)
362 || f->data.ult (b->data))
374 else /* Signed type. */
377 && (get_fixed_sign_bit (a->data, i_f_bits)
378 == get_fixed_sign_bit (b->data, i_f_bits))
379 && (get_fixed_sign_bit (a->data, i_f_bits)
380 != get_fixed_sign_bit (f->data, i_f_bits)))
382 && (get_fixed_sign_bit (a->data, i_f_bits)
383 != get_fixed_sign_bit (b->data, i_f_bits))
384 && (get_fixed_sign_bit (a->data, i_f_bits)
385 != get_fixed_sign_bit (f->data, i_f_bits))))
391 f->data = f->data.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
392 if (get_fixed_sign_bit (a->data, i_f_bits) == 0)
401 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
405 /* Calculate F = A * B.
406 If SAT_P, saturate the result to the max or the min.
407 Return true, if !SAT_P and overflow. */
410 do_fixed_multiply (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
411 const FIXED_VALUE_TYPE *b, bool sat_p)
413 bool overflow_p = false;
414 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
415 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
417 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT)
419 f->data = a->data * b->data;
420 f->data = f->data.lshift (-GET_MODE_FBIT (f->mode),
421 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
422 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
426 /* The result of multiplication expands to two double_int. */
427 double_int a_high, a_low, b_high, b_low;
428 double_int high_high, high_low, low_high, low_low;
429 double_int r, s, temp1, temp2;
432 /* Decompose a and b to four double_int. */
433 a_high.low = a->data.high;
435 a_low.low = a->data.low;
437 b_high.low = b->data.high;
439 b_low.low = b->data.low;
442 /* Perform four multiplications. */
443 low_low = a_low * b_low;
444 low_high = a_low * b_high;
445 high_low = a_high * b_low;
446 high_high = a_high * b_high;
448 /* Accumulate four results to {r, s}. */
449 temp1.high = high_low.low;
454 carry ++; /* Carry */
457 temp2.high = low_high.low;
462 carry ++; /* Carry */
464 temp1.low = high_low.high;
466 r = high_high + temp1;
467 temp1.low = low_high.high;
474 /* We need to subtract b from r, if a < 0. */
475 if (!unsigned_p && a->data.high < 0)
477 /* We need to subtract a from r, if b < 0. */
478 if (!unsigned_p && b->data.high < 0)
481 /* Shift right the result by FBIT. */
482 if (GET_MODE_FBIT (f->mode) == HOST_BITS_PER_DOUBLE_INT)
497 f->data.high = s.high;
501 s = s.llshift ((-GET_MODE_FBIT (f->mode)), HOST_BITS_PER_DOUBLE_INT);
502 f->data = r.llshift ((HOST_BITS_PER_DOUBLE_INT
503 - GET_MODE_FBIT (f->mode)),
504 HOST_BITS_PER_DOUBLE_INT);
505 f->data.low = f->data.low | s.low;
506 f->data.high = f->data.high | s.high;
508 s.high = f->data.high;
509 r = r.lshift (-GET_MODE_FBIT (f->mode),
510 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
513 overflow_p = fixed_saturate2 (f->mode, r, s, &f->data, sat_p);
516 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
520 /* Calculate F = A / B.
521 If SAT_P, saturate the result to the max or the min.
522 Return true, if !SAT_P and overflow. */
525 do_fixed_divide (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
526 const FIXED_VALUE_TYPE *b, bool sat_p)
528 bool overflow_p = false;
529 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
530 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
532 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT)
534 f->data = a->data.lshift (GET_MODE_FBIT (f->mode),
535 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
536 f->data = f->data.div (b->data, unsigned_p, TRUNC_DIV_EXPR);
537 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
541 double_int pos_a, pos_b, r, s;
542 double_int quo_r, quo_s, mod, temp;
546 /* If a < 0, negate a. */
547 if (!unsigned_p && a->data.high < 0)
555 /* If b < 0, negate b. */
556 if (!unsigned_p && b->data.high < 0)
564 /* Left shift pos_a to {r, s} by FBIT. */
565 if (GET_MODE_FBIT (f->mode) == HOST_BITS_PER_DOUBLE_INT)
573 s = pos_a.llshift (GET_MODE_FBIT (f->mode), HOST_BITS_PER_DOUBLE_INT);
574 r = pos_a.llshift (- (HOST_BITS_PER_DOUBLE_INT
575 - GET_MODE_FBIT (f->mode)),
576 HOST_BITS_PER_DOUBLE_INT);
579 /* Divide r by pos_b to quo_r. The remainder is in mod. */
580 quo_r = r.divmod (pos_b, 1, TRUNC_DIV_EXPR, &mod);
581 quo_s = double_int_zero;
583 for (i = 0; i < HOST_BITS_PER_DOUBLE_INT; i++)
585 /* Record the leftmost bit of mod. */
586 int leftmost_mod = (mod.high < 0);
588 /* Shift left mod by 1 bit. */
589 mod = mod.lshift (1);
591 /* Test the leftmost bit of s to add to mod. */
595 /* Shift left quo_s by 1 bit. */
596 quo_s = quo_s.lshift (1);
598 /* Try to calculate (mod - pos_b). */
601 if (leftmost_mod == 1 || mod.ucmp (pos_b) != -1)
607 /* Shift left s by 1 bit. */
615 if (quo_s.high == 0 && quo_s.low == 0)
619 quo_r.low = ~quo_r.low;
620 quo_r.high = ~quo_r.high;
625 overflow_p = fixed_saturate2 (f->mode, quo_r, quo_s, &f->data, sat_p);
628 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
632 /* Calculate F = A << B if LEFT_P. Otherwise, F = A >> B.
633 If SAT_P, saturate the result to the max or the min.
634 Return true, if !SAT_P and overflow. */
637 do_fixed_shift (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
638 const FIXED_VALUE_TYPE *b, bool left_p, bool sat_p)
640 bool overflow_p = false;
641 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
642 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
645 if (b->data.low == 0)
651 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT || (!left_p))
653 f->data = a->data.lshift (left_p ? b->data.low : -b->data.low,
654 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
655 if (left_p) /* Only left shift saturates. */
656 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
658 else /* We need two double_int to store the left-shift result. */
660 double_int temp_high, temp_low;
661 if (b->data.low == HOST_BITS_PER_DOUBLE_INT)
669 temp_low = a->data.lshift (b->data.low,
670 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
671 /* Logical shift right to temp_high. */
672 temp_high = a->data.llshift (b->data.low - HOST_BITS_PER_DOUBLE_INT,
673 HOST_BITS_PER_DOUBLE_INT);
675 if (!unsigned_p && a->data.high < 0) /* Signed-extend temp_high. */
676 temp_high = temp_high.ext (b->data.low, unsigned_p);
678 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
681 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
686 If SAT_P, saturate the result to the max or the min.
687 Return true, if !SAT_P and overflow. */
690 do_fixed_neg (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a, bool sat_p)
692 bool overflow_p = false;
693 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
694 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
697 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
699 if (unsigned_p) /* Unsigned type. */
701 if (f->data.low != 0 || f->data.high != 0)
712 else /* Signed type. */
714 if (!(f->data.high == 0 && f->data.low == 0)
715 && f->data.high == a->data.high && f->data.low == a->data.low )
719 /* Saturate to the maximum by subtracting f->data by one. */
722 f->data = f->data.zext (i_f_bits);
731 /* Perform the binary or unary operation described by CODE.
732 Note that OP0 and OP1 must have the same mode for binary operators.
733 For a unary operation, leave OP1 NULL.
734 Return true, if !SAT_P and overflow. */
737 fixed_arithmetic (FIXED_VALUE_TYPE *f, int icode, const FIXED_VALUE_TYPE *op0,
738 const FIXED_VALUE_TYPE *op1, bool sat_p)
743 return do_fixed_neg (f, op0, sat_p);
747 gcc_assert (op0->mode == op1->mode);
748 return do_fixed_add (f, op0, op1, false, sat_p);
752 gcc_assert (op0->mode == op1->mode);
753 return do_fixed_add (f, op0, op1, true, sat_p);
757 gcc_assert (op0->mode == op1->mode);
758 return do_fixed_multiply (f, op0, op1, sat_p);
762 gcc_assert (op0->mode == op1->mode);
763 return do_fixed_divide (f, op0, op1, sat_p);
767 return do_fixed_shift (f, op0, op1, true, sat_p);
771 return do_fixed_shift (f, op0, op1, false, sat_p);
780 /* Compare fixed-point values by tree_code.
781 Note that OP0 and OP1 must have the same mode. */
784 fixed_compare (int icode, const FIXED_VALUE_TYPE *op0,
785 const FIXED_VALUE_TYPE *op1)
787 enum tree_code code = (enum tree_code) icode;
788 gcc_assert (op0->mode == op1->mode);
793 return op0->data != op1->data;
796 return op0->data == op1->data;
799 return op0->data.cmp (op1->data,
800 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) == -1;
803 return op0->data.cmp (op1->data,
804 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) != 1;
807 return op0->data.cmp (op1->data,
808 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) == 1;
811 return op0->data.cmp (op1->data,
812 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) != -1;
819 /* Extend or truncate to a new mode.
820 If SAT_P, saturate the result to the max or the min.
821 Return true, if !SAT_P and overflow. */
824 fixed_convert (FIXED_VALUE_TYPE *f, machine_mode mode,
825 const FIXED_VALUE_TYPE *a, bool sat_p)
827 bool overflow_p = false;
834 if (GET_MODE_FBIT (mode) > GET_MODE_FBIT (a->mode))
836 /* Left shift a to temp_high, temp_low based on a->mode. */
837 double_int temp_high, temp_low;
838 int amount = GET_MODE_FBIT (mode) - GET_MODE_FBIT (a->mode);
839 temp_low = a->data.lshift (amount,
840 HOST_BITS_PER_DOUBLE_INT,
841 SIGNED_FIXED_POINT_MODE_P (a->mode));
842 /* Logical shift right to temp_high. */
843 temp_high = a->data.llshift (amount - HOST_BITS_PER_DOUBLE_INT,
844 HOST_BITS_PER_DOUBLE_INT);
845 if (SIGNED_FIXED_POINT_MODE_P (a->mode)
846 && a->data.high < 0) /* Signed-extend temp_high. */
847 temp_high = temp_high.sext (amount);
850 if (SIGNED_FIXED_POINT_MODE_P (a->mode) ==
851 SIGNED_FIXED_POINT_MODE_P (f->mode))
852 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
856 /* Take care of the cases when converting between signed and
858 if (SIGNED_FIXED_POINT_MODE_P (a->mode))
860 /* Signed -> Unsigned. */
861 if (a->data.high < 0)
865 f->data.low = 0; /* Set to zero. */
866 f->data.high = 0; /* Set to zero. */
872 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
877 /* Unsigned -> Signed. */
878 if (temp_high.high < 0)
882 /* Set to maximum. */
883 f->data.low = -1; /* Set to all ones. */
884 f->data.high = -1; /* Set to all ones. */
885 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
886 + GET_MODE_IBIT (f->mode));
887 /* Clear the sign. */
893 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
900 /* Right shift a to temp based on a->mode. */
902 temp = a->data.lshift (GET_MODE_FBIT (mode) - GET_MODE_FBIT (a->mode),
903 HOST_BITS_PER_DOUBLE_INT,
904 SIGNED_FIXED_POINT_MODE_P (a->mode));
907 if (SIGNED_FIXED_POINT_MODE_P (a->mode) ==
908 SIGNED_FIXED_POINT_MODE_P (f->mode))
909 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
912 /* Take care of the cases when converting between signed and
914 if (SIGNED_FIXED_POINT_MODE_P (a->mode))
916 /* Signed -> Unsigned. */
917 if (a->data.high < 0)
921 f->data.low = 0; /* Set to zero. */
922 f->data.high = 0; /* Set to zero. */
928 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data,
933 /* Unsigned -> Signed. */
938 /* Set to maximum. */
939 f->data.low = -1; /* Set to all ones. */
940 f->data.high = -1; /* Set to all ones. */
941 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
942 + GET_MODE_IBIT (f->mode));
943 /* Clear the sign. */
949 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data,
955 f->data = f->data.ext (SIGNED_FIXED_POINT_MODE_P (f->mode)
956 + GET_MODE_FBIT (f->mode)
957 + GET_MODE_IBIT (f->mode),
958 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
962 /* Convert to a new fixed-point mode from an integer.
963 If UNSIGNED_P, this integer is unsigned.
964 If SAT_P, saturate the result to the max or the min.
965 Return true, if !SAT_P and overflow. */
968 fixed_convert_from_int (FIXED_VALUE_TYPE *f, machine_mode mode,
969 double_int a, bool unsigned_p, bool sat_p)
971 bool overflow_p = false;
972 /* Left shift a to temp_high, temp_low. */
973 double_int temp_high, temp_low;
974 int amount = GET_MODE_FBIT (mode);
975 if (amount == HOST_BITS_PER_DOUBLE_INT)
983 temp_low = a.llshift (amount, HOST_BITS_PER_DOUBLE_INT);
985 /* Logical shift right to temp_high. */
986 temp_high = a.llshift (amount - HOST_BITS_PER_DOUBLE_INT,
987 HOST_BITS_PER_DOUBLE_INT);
989 if (!unsigned_p && a.high < 0) /* Signed-extend temp_high. */
990 temp_high = temp_high.sext (amount);
995 if (unsigned_p == UNSIGNED_FIXED_POINT_MODE_P (f->mode))
996 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
1000 /* Take care of the cases when converting between signed and unsigned. */
1003 /* Signed -> Unsigned. */
1008 f->data.low = 0; /* Set to zero. */
1009 f->data.high = 0; /* Set to zero. */
1015 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
1020 /* Unsigned -> Signed. */
1021 if (temp_high.high < 0)
1025 /* Set to maximum. */
1026 f->data.low = -1; /* Set to all ones. */
1027 f->data.high = -1; /* Set to all ones. */
1028 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
1029 + GET_MODE_IBIT (f->mode));
1030 /* Clear the sign. */
1036 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
1040 f->data = f->data.ext (SIGNED_FIXED_POINT_MODE_P (f->mode)
1041 + GET_MODE_FBIT (f->mode)
1042 + GET_MODE_IBIT (f->mode),
1043 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
1047 /* Convert to a new fixed-point mode from a real.
1048 If SAT_P, saturate the result to the max or the min.
1049 Return true, if !SAT_P and overflow. */
1052 fixed_convert_from_real (FIXED_VALUE_TYPE *f, machine_mode mode,
1053 const REAL_VALUE_TYPE *a, bool sat_p)
1055 bool overflow_p = false;
1056 REAL_VALUE_TYPE real_value, fixed_value, base_value;
1057 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
1058 int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
1059 unsigned int fbit = GET_MODE_FBIT (mode);
1060 enum fixed_value_range_code temp;
1065 real_2expN (&base_value, fbit, mode);
1066 real_arithmetic (&fixed_value, MULT_EXPR, &real_value, &base_value);
1068 wide_int w = real_to_integer (&fixed_value, &fail,
1069 GET_MODE_PRECISION (mode));
1070 f->data.low = w.elt (0);
1071 f->data.high = w.elt (1);
1072 temp = check_real_for_fixed_mode (&real_value, mode);
1073 if (temp == FIXED_UNDERFLOW) /* Minimum. */
1086 f->data = f->data.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
1087 f->data = f->data.sext (1 + i_f_bits);
1093 else if (temp == FIXED_GT_MAX_EPS || temp == FIXED_MAX_EPS) /* Maximum. */
1099 f->data = f->data.zext (i_f_bits);
1104 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
1108 /* Convert to a new real mode from a fixed-point. */
1111 real_convert_from_fixed (REAL_VALUE_TYPE *r, machine_mode mode,
1112 const FIXED_VALUE_TYPE *f)
1114 REAL_VALUE_TYPE base_value, fixed_value, real_value;
1116 signop sgn = UNSIGNED_FIXED_POINT_MODE_P (f->mode) ? UNSIGNED : SIGNED;
1117 real_2expN (&base_value, GET_MODE_FBIT (f->mode), f->mode);
1118 real_from_integer (&fixed_value, VOIDmode,
1119 wide_int::from (f->data, GET_MODE_PRECISION (f->mode),
1121 real_arithmetic (&real_value, RDIV_EXPR, &fixed_value, &base_value);
1122 real_convert (r, mode, &real_value);
1125 /* Determine whether a fixed-point value F is negative. */
1128 fixed_isneg (const FIXED_VALUE_TYPE *f)
1130 if (SIGNED_FIXED_POINT_MODE_P (f->mode))
1132 int i_f_bits = GET_MODE_IBIT (f->mode) + GET_MODE_FBIT (f->mode);
1133 int sign_bit = get_fixed_sign_bit (f->data, i_f_bits);