1 /* Fixed-point arithmetic support.
2 Copyright (C) 2006-2016 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"
25 #include "diagnostic-core.h"
27 /* Compare two fixed objects for bitwise identity. */
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. */
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, machine_mode mode)
64 REAL_VALUE_TYPE max_value, min_value, epsilon_value;
66 real_2expN (&max_value, GET_MODE_IBIT (mode), VOIDmode);
67 real_2expN (&epsilon_value, -GET_MODE_FBIT (mode), VOIDmode);
69 if (SIGNED_FIXED_POINT_MODE_P (mode))
70 min_value = real_value_negate (&max_value);
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))
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;
85 /* Construct a CONST_FIXED from a bit payload and machine mode MODE.
86 The bits in PAYLOAD are sign-extended/zero-extended according to MODE. */
89 fixed_from_double_int (double_int payload, machine_mode mode)
91 FIXED_VALUE_TYPE value;
93 gcc_assert (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_DOUBLE_INT);
95 if (SIGNED_SCALAR_FIXED_POINT_MODE_P (mode))
96 value.data = payload.sext (1 + GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode));
97 else if (UNSIGNED_SCALAR_FIXED_POINT_MODE_P (mode))
98 value.data = payload.zext (GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode));
108 /* Initialize from a decimal or hexadecimal string. */
111 fixed_from_string (FIXED_VALUE_TYPE *f, const char *str, machine_mode mode)
113 REAL_VALUE_TYPE real_value, fixed_value, base_value;
115 enum fixed_value_range_code temp;
119 fbit = GET_MODE_FBIT (mode);
121 real_from_string (&real_value, str);
122 temp = check_real_for_fixed_mode (&real_value, f->mode);
123 /* We don't want to warn the case when the _Fract value is 1.0. */
124 if (temp == FIXED_UNDERFLOW
125 || temp == FIXED_GT_MAX_EPS
126 || (temp == FIXED_MAX_EPS && ALL_ACCUM_MODE_P (f->mode)))
127 warning (OPT_Woverflow,
128 "large fixed-point constant implicitly truncated to fixed-point type");
129 real_2expN (&base_value, fbit, VOIDmode);
130 real_arithmetic (&fixed_value, MULT_EXPR, &real_value, &base_value);
131 wide_int w = real_to_integer (&fixed_value, &fail,
132 GET_MODE_PRECISION (mode));
133 f->data.low = w.elt (0);
134 f->data.high = w.elt (1);
136 if (temp == FIXED_MAX_EPS && ALL_FRACT_MODE_P (f->mode))
138 /* From the spec, we need to evaluate 1 to the maximal value. */
141 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
142 + GET_MODE_IBIT (f->mode));
145 f->data = f->data.ext (SIGNED_FIXED_POINT_MODE_P (f->mode)
146 + GET_MODE_FBIT (f->mode)
147 + GET_MODE_IBIT (f->mode),
148 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
151 /* Render F as a decimal floating point constant. */
154 fixed_to_decimal (char *str, const FIXED_VALUE_TYPE *f_orig,
157 REAL_VALUE_TYPE real_value, base_value, fixed_value;
159 signop sgn = UNSIGNED_FIXED_POINT_MODE_P (f_orig->mode) ? UNSIGNED : SIGNED;
160 real_2expN (&base_value, GET_MODE_FBIT (f_orig->mode), VOIDmode);
161 real_from_integer (&real_value, VOIDmode,
162 wide_int::from (f_orig->data,
163 GET_MODE_PRECISION (f_orig->mode), sgn),
165 real_arithmetic (&fixed_value, RDIV_EXPR, &real_value, &base_value);
166 real_to_decimal (str, &fixed_value, buf_size, 0, 1);
169 /* If SAT_P, saturate A to the maximum or the minimum, and save to *F based on
170 the machine mode MODE.
171 Do not modify *F otherwise.
172 This function assumes the width of double_int is greater than the width
173 of the fixed-point value (the sum of a possible sign bit, possible ibits,
175 Return true, if !SAT_P and overflow. */
178 fixed_saturate1 (machine_mode mode, double_int a, double_int *f,
181 bool overflow_p = false;
182 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
183 int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
185 if (unsigned_p) /* Unsigned type. */
190 max = max.zext (i_f_bits);
199 else /* Signed type. */
204 max = max.zext (i_f_bits);
207 min = min.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
208 min = min.sext (1 + i_f_bits);
216 else if (a.slt (min))
227 /* If SAT_P, saturate {A_HIGH, A_LOW} to the maximum or the minimum, and
228 save to *F based on the machine mode MODE.
229 Do not modify *F otherwise.
230 This function assumes the width of two double_int is greater than the width
231 of the fixed-point value (the sum of a possible sign bit, possible ibits,
233 Return true, if !SAT_P and overflow. */
236 fixed_saturate2 (machine_mode mode, double_int a_high, double_int a_low,
237 double_int *f, bool sat_p)
239 bool overflow_p = false;
240 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
241 int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
243 if (unsigned_p) /* Unsigned type. */
245 double_int max_r, max_s;
250 max_s = max_s.zext (i_f_bits);
251 if (a_high.ugt (max_r)
252 || (a_high == max_r &&
261 else /* Signed type. */
263 double_int max_r, max_s, min_r, min_s;
268 max_s = max_s.zext (i_f_bits);
273 min_s = min_s.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
274 min_s = min_s.sext (1 + i_f_bits);
275 if (a_high.sgt (max_r)
276 || (a_high == max_r &&
284 else if (a_high.slt (min_r)
285 || (a_high == min_r &&
297 /* Return the sign bit based on I_F_BITS. */
300 get_fixed_sign_bit (double_int a, int i_f_bits)
302 if (i_f_bits < HOST_BITS_PER_WIDE_INT)
303 return (a.low >> i_f_bits) & 1;
305 return (a.high >> (i_f_bits - HOST_BITS_PER_WIDE_INT)) & 1;
308 /* Calculate F = A + (SUBTRACT_P ? -B : B).
309 If SAT_P, saturate the result to the max or the min.
310 Return true, if !SAT_P and overflow. */
313 do_fixed_add (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
314 const FIXED_VALUE_TYPE *b, bool subtract_p, bool sat_p)
316 bool overflow_p = false;
321 /* This was a conditional expression but it triggered a bug in
328 unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
329 i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
331 f->data = a->data + temp;
332 if (unsigned_p) /* Unsigned type. */
334 if (subtract_p) /* Unsigned subtraction. */
336 if (a->data.ult (b->data))
347 else /* Unsigned addition. */
349 f->data = f->data.zext (i_f_bits);
350 if (f->data.ult (a->data)
351 || f->data.ult (b->data))
363 else /* Signed type. */
366 && (get_fixed_sign_bit (a->data, i_f_bits)
367 == get_fixed_sign_bit (b->data, i_f_bits))
368 && (get_fixed_sign_bit (a->data, i_f_bits)
369 != get_fixed_sign_bit (f->data, i_f_bits)))
371 && (get_fixed_sign_bit (a->data, i_f_bits)
372 != get_fixed_sign_bit (b->data, i_f_bits))
373 && (get_fixed_sign_bit (a->data, i_f_bits)
374 != get_fixed_sign_bit (f->data, i_f_bits))))
380 f->data = f->data.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
381 if (get_fixed_sign_bit (a->data, i_f_bits) == 0)
390 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
394 /* Calculate F = A * B.
395 If SAT_P, saturate the result to the max or the min.
396 Return true, if !SAT_P and overflow. */
399 do_fixed_multiply (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
400 const FIXED_VALUE_TYPE *b, bool sat_p)
402 bool overflow_p = false;
403 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
404 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
406 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT)
408 f->data = a->data * b->data;
409 f->data = f->data.lshift (-GET_MODE_FBIT (f->mode),
410 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
411 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
415 /* The result of multiplication expands to two double_int. */
416 double_int a_high, a_low, b_high, b_low;
417 double_int high_high, high_low, low_high, low_low;
418 double_int r, s, temp1, temp2;
421 /* Decompose a and b to four double_int. */
422 a_high.low = a->data.high;
424 a_low.low = a->data.low;
426 b_high.low = b->data.high;
428 b_low.low = b->data.low;
431 /* Perform four multiplications. */
432 low_low = a_low * b_low;
433 low_high = a_low * b_high;
434 high_low = a_high * b_low;
435 high_high = a_high * b_high;
437 /* Accumulate four results to {r, s}. */
438 temp1.high = high_low.low;
443 carry ++; /* Carry */
446 temp2.high = low_high.low;
451 carry ++; /* Carry */
453 temp1.low = high_low.high;
455 r = high_high + temp1;
456 temp1.low = low_high.high;
463 /* We need to subtract b from r, if a < 0. */
464 if (!unsigned_p && a->data.high < 0)
466 /* We need to subtract a from r, if b < 0. */
467 if (!unsigned_p && b->data.high < 0)
470 /* Shift right the result by FBIT. */
471 if (GET_MODE_FBIT (f->mode) == HOST_BITS_PER_DOUBLE_INT)
486 f->data.high = s.high;
490 s = s.llshift ((-GET_MODE_FBIT (f->mode)), HOST_BITS_PER_DOUBLE_INT);
491 f->data = r.llshift ((HOST_BITS_PER_DOUBLE_INT
492 - GET_MODE_FBIT (f->mode)),
493 HOST_BITS_PER_DOUBLE_INT);
494 f->data.low = f->data.low | s.low;
495 f->data.high = f->data.high | s.high;
497 s.high = f->data.high;
498 r = r.lshift (-GET_MODE_FBIT (f->mode),
499 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
502 overflow_p = fixed_saturate2 (f->mode, r, s, &f->data, sat_p);
505 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
509 /* Calculate F = A / B.
510 If SAT_P, saturate the result to the max or the min.
511 Return true, if !SAT_P and overflow. */
514 do_fixed_divide (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
515 const FIXED_VALUE_TYPE *b, bool sat_p)
517 bool overflow_p = false;
518 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
519 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
521 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT)
523 f->data = a->data.lshift (GET_MODE_FBIT (f->mode),
524 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
525 f->data = f->data.div (b->data, unsigned_p, TRUNC_DIV_EXPR);
526 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
530 double_int pos_a, pos_b, r, s;
531 double_int quo_r, quo_s, mod, temp;
535 /* If a < 0, negate a. */
536 if (!unsigned_p && a->data.high < 0)
544 /* If b < 0, negate b. */
545 if (!unsigned_p && b->data.high < 0)
553 /* Left shift pos_a to {r, s} by FBIT. */
554 if (GET_MODE_FBIT (f->mode) == HOST_BITS_PER_DOUBLE_INT)
562 s = pos_a.llshift (GET_MODE_FBIT (f->mode), HOST_BITS_PER_DOUBLE_INT);
563 r = pos_a.llshift (- (HOST_BITS_PER_DOUBLE_INT
564 - GET_MODE_FBIT (f->mode)),
565 HOST_BITS_PER_DOUBLE_INT);
568 /* Divide r by pos_b to quo_r. The remainder is in mod. */
569 quo_r = r.divmod (pos_b, 1, TRUNC_DIV_EXPR, &mod);
570 quo_s = double_int_zero;
572 for (i = 0; i < HOST_BITS_PER_DOUBLE_INT; i++)
574 /* Record the leftmost bit of mod. */
575 int leftmost_mod = (mod.high < 0);
577 /* Shift left mod by 1 bit. */
578 mod = mod.lshift (1);
580 /* Test the leftmost bit of s to add to mod. */
584 /* Shift left quo_s by 1 bit. */
585 quo_s = quo_s.lshift (1);
587 /* Try to calculate (mod - pos_b). */
590 if (leftmost_mod == 1 || mod.ucmp (pos_b) != -1)
596 /* Shift left s by 1 bit. */
604 if (quo_s.high == 0 && quo_s.low == 0)
608 quo_r.low = ~quo_r.low;
609 quo_r.high = ~quo_r.high;
614 overflow_p = fixed_saturate2 (f->mode, quo_r, quo_s, &f->data, sat_p);
617 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
621 /* Calculate F = A << B if LEFT_P. Otherwise, F = A >> B.
622 If SAT_P, saturate the result to the max or the min.
623 Return true, if !SAT_P and overflow. */
626 do_fixed_shift (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
627 const FIXED_VALUE_TYPE *b, bool left_p, bool sat_p)
629 bool overflow_p = false;
630 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
631 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
634 if (b->data.low == 0)
640 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT || (!left_p))
642 f->data = a->data.lshift (left_p ? b->data.low : -b->data.low,
643 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
644 if (left_p) /* Only left shift saturates. */
645 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
647 else /* We need two double_int to store the left-shift result. */
649 double_int temp_high, temp_low;
650 if (b->data.low == HOST_BITS_PER_DOUBLE_INT)
658 temp_low = a->data.lshift (b->data.low,
659 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
660 /* Logical shift right to temp_high. */
661 temp_high = a->data.llshift (b->data.low - HOST_BITS_PER_DOUBLE_INT,
662 HOST_BITS_PER_DOUBLE_INT);
664 if (!unsigned_p && a->data.high < 0) /* Signed-extend temp_high. */
665 temp_high = temp_high.ext (b->data.low, unsigned_p);
667 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
670 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
675 If SAT_P, saturate the result to the max or the min.
676 Return true, if !SAT_P and overflow. */
679 do_fixed_neg (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a, bool sat_p)
681 bool overflow_p = false;
682 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
683 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
686 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
688 if (unsigned_p) /* Unsigned type. */
690 if (f->data.low != 0 || f->data.high != 0)
701 else /* Signed type. */
703 if (!(f->data.high == 0 && f->data.low == 0)
704 && f->data.high == a->data.high && f->data.low == a->data.low )
708 /* Saturate to the maximum by subtracting f->data by one. */
711 f->data = f->data.zext (i_f_bits);
720 /* Perform the binary or unary operation described by CODE.
721 Note that OP0 and OP1 must have the same mode for binary operators.
722 For a unary operation, leave OP1 NULL.
723 Return true, if !SAT_P and overflow. */
726 fixed_arithmetic (FIXED_VALUE_TYPE *f, int icode, const FIXED_VALUE_TYPE *op0,
727 const FIXED_VALUE_TYPE *op1, bool sat_p)
732 return do_fixed_neg (f, op0, sat_p);
736 gcc_assert (op0->mode == op1->mode);
737 return do_fixed_add (f, op0, op1, false, sat_p);
741 gcc_assert (op0->mode == op1->mode);
742 return do_fixed_add (f, op0, op1, true, sat_p);
746 gcc_assert (op0->mode == op1->mode);
747 return do_fixed_multiply (f, op0, op1, sat_p);
751 gcc_assert (op0->mode == op1->mode);
752 return do_fixed_divide (f, op0, op1, sat_p);
756 return do_fixed_shift (f, op0, op1, true, sat_p);
760 return do_fixed_shift (f, op0, op1, false, sat_p);
769 /* Compare fixed-point values by tree_code.
770 Note that OP0 and OP1 must have the same mode. */
773 fixed_compare (int icode, const FIXED_VALUE_TYPE *op0,
774 const FIXED_VALUE_TYPE *op1)
776 enum tree_code code = (enum tree_code) icode;
777 gcc_assert (op0->mode == op1->mode);
782 return op0->data != op1->data;
785 return op0->data == op1->data;
788 return op0->data.cmp (op1->data,
789 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) == -1;
792 return op0->data.cmp (op1->data,
793 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) != 1;
796 return op0->data.cmp (op1->data,
797 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) == 1;
800 return op0->data.cmp (op1->data,
801 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) != -1;
808 /* Extend or truncate to a new mode.
809 If SAT_P, saturate the result to the max or the min.
810 Return true, if !SAT_P and overflow. */
813 fixed_convert (FIXED_VALUE_TYPE *f, machine_mode mode,
814 const FIXED_VALUE_TYPE *a, bool sat_p)
816 bool overflow_p = false;
823 if (GET_MODE_FBIT (mode) > GET_MODE_FBIT (a->mode))
825 /* Left shift a to temp_high, temp_low based on a->mode. */
826 double_int temp_high, temp_low;
827 int amount = GET_MODE_FBIT (mode) - GET_MODE_FBIT (a->mode);
828 temp_low = a->data.lshift (amount,
829 HOST_BITS_PER_DOUBLE_INT,
830 SIGNED_FIXED_POINT_MODE_P (a->mode));
831 /* Logical shift right to temp_high. */
832 temp_high = a->data.llshift (amount - HOST_BITS_PER_DOUBLE_INT,
833 HOST_BITS_PER_DOUBLE_INT);
834 if (SIGNED_FIXED_POINT_MODE_P (a->mode)
835 && a->data.high < 0) /* Signed-extend temp_high. */
836 temp_high = temp_high.sext (amount);
839 if (SIGNED_FIXED_POINT_MODE_P (a->mode) ==
840 SIGNED_FIXED_POINT_MODE_P (f->mode))
841 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
845 /* Take care of the cases when converting between signed and
847 if (SIGNED_FIXED_POINT_MODE_P (a->mode))
849 /* Signed -> Unsigned. */
850 if (a->data.high < 0)
854 f->data.low = 0; /* Set to zero. */
855 f->data.high = 0; /* Set to zero. */
861 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
866 /* Unsigned -> Signed. */
867 if (temp_high.high < 0)
871 /* Set to maximum. */
872 f->data.low = -1; /* Set to all ones. */
873 f->data.high = -1; /* Set to all ones. */
874 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
875 + GET_MODE_IBIT (f->mode));
876 /* Clear the sign. */
882 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
889 /* Right shift a to temp based on a->mode. */
891 temp = a->data.lshift (GET_MODE_FBIT (mode) - GET_MODE_FBIT (a->mode),
892 HOST_BITS_PER_DOUBLE_INT,
893 SIGNED_FIXED_POINT_MODE_P (a->mode));
896 if (SIGNED_FIXED_POINT_MODE_P (a->mode) ==
897 SIGNED_FIXED_POINT_MODE_P (f->mode))
898 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
901 /* Take care of the cases when converting between signed and
903 if (SIGNED_FIXED_POINT_MODE_P (a->mode))
905 /* Signed -> Unsigned. */
906 if (a->data.high < 0)
910 f->data.low = 0; /* Set to zero. */
911 f->data.high = 0; /* Set to zero. */
917 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data,
922 /* Unsigned -> Signed. */
927 /* Set to maximum. */
928 f->data.low = -1; /* Set to all ones. */
929 f->data.high = -1; /* Set to all ones. */
930 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
931 + GET_MODE_IBIT (f->mode));
932 /* Clear the sign. */
938 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data,
944 f->data = f->data.ext (SIGNED_FIXED_POINT_MODE_P (f->mode)
945 + GET_MODE_FBIT (f->mode)
946 + GET_MODE_IBIT (f->mode),
947 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
951 /* Convert to a new fixed-point mode from an integer.
952 If UNSIGNED_P, this integer is unsigned.
953 If SAT_P, saturate the result to the max or the min.
954 Return true, if !SAT_P and overflow. */
957 fixed_convert_from_int (FIXED_VALUE_TYPE *f, machine_mode mode,
958 double_int a, bool unsigned_p, bool sat_p)
960 bool overflow_p = false;
961 /* Left shift a to temp_high, temp_low. */
962 double_int temp_high, temp_low;
963 int amount = GET_MODE_FBIT (mode);
964 if (amount == HOST_BITS_PER_DOUBLE_INT)
972 temp_low = a.llshift (amount, HOST_BITS_PER_DOUBLE_INT);
974 /* Logical shift right to temp_high. */
975 temp_high = a.llshift (amount - HOST_BITS_PER_DOUBLE_INT,
976 HOST_BITS_PER_DOUBLE_INT);
978 if (!unsigned_p && a.high < 0) /* Signed-extend temp_high. */
979 temp_high = temp_high.sext (amount);
984 if (unsigned_p == UNSIGNED_FIXED_POINT_MODE_P (f->mode))
985 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
989 /* Take care of the cases when converting between signed and unsigned. */
992 /* Signed -> Unsigned. */
997 f->data.low = 0; /* Set to zero. */
998 f->data.high = 0; /* Set to zero. */
1004 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
1009 /* Unsigned -> Signed. */
1010 if (temp_high.high < 0)
1014 /* Set to maximum. */
1015 f->data.low = -1; /* Set to all ones. */
1016 f->data.high = -1; /* Set to all ones. */
1017 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
1018 + GET_MODE_IBIT (f->mode));
1019 /* Clear the sign. */
1025 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
1029 f->data = f->data.ext (SIGNED_FIXED_POINT_MODE_P (f->mode)
1030 + GET_MODE_FBIT (f->mode)
1031 + GET_MODE_IBIT (f->mode),
1032 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
1036 /* Convert to a new fixed-point mode from a real.
1037 If SAT_P, saturate the result to the max or the min.
1038 Return true, if !SAT_P and overflow. */
1041 fixed_convert_from_real (FIXED_VALUE_TYPE *f, machine_mode mode,
1042 const REAL_VALUE_TYPE *a, bool sat_p)
1044 bool overflow_p = false;
1045 REAL_VALUE_TYPE real_value, fixed_value, base_value;
1046 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
1047 int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
1048 unsigned int fbit = GET_MODE_FBIT (mode);
1049 enum fixed_value_range_code temp;
1054 real_2expN (&base_value, fbit, VOIDmode);
1055 real_arithmetic (&fixed_value, MULT_EXPR, &real_value, &base_value);
1057 wide_int w = real_to_integer (&fixed_value, &fail,
1058 GET_MODE_PRECISION (mode));
1059 f->data.low = w.elt (0);
1060 f->data.high = w.elt (1);
1061 temp = check_real_for_fixed_mode (&real_value, mode);
1062 if (temp == FIXED_UNDERFLOW) /* Minimum. */
1075 f->data = f->data.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
1076 f->data = f->data.sext (1 + i_f_bits);
1082 else if (temp == FIXED_GT_MAX_EPS || temp == FIXED_MAX_EPS) /* Maximum. */
1088 f->data = f->data.zext (i_f_bits);
1093 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
1097 /* Convert to a new real mode from a fixed-point. */
1100 real_convert_from_fixed (REAL_VALUE_TYPE *r, machine_mode mode,
1101 const FIXED_VALUE_TYPE *f)
1103 REAL_VALUE_TYPE base_value, fixed_value, real_value;
1105 signop sgn = UNSIGNED_FIXED_POINT_MODE_P (f->mode) ? UNSIGNED : SIGNED;
1106 real_2expN (&base_value, GET_MODE_FBIT (f->mode), VOIDmode);
1107 real_from_integer (&fixed_value, VOIDmode,
1108 wide_int::from (f->data, GET_MODE_PRECISION (f->mode),
1110 real_arithmetic (&real_value, RDIV_EXPR, &fixed_value, &base_value);
1111 real_convert (r, mode, &real_value);
1114 /* Determine whether a fixed-point value F is negative. */
1117 fixed_isneg (const FIXED_VALUE_TYPE *f)
1119 if (SIGNED_FIXED_POINT_MODE_P (f->mode))
1121 int i_f_bits = GET_MODE_IBIT (f->mode) + GET_MODE_FBIT (f->mode);
1122 int sign_bit = get_fixed_sign_bit (f->data, i_f_bits);