1 /* Fixed-point arithmetic support.
2 Copyright (C) 2006-2014 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"
28 /* Compare two fixed objects for bitwise identity. */
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. */
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
, 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
);
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
))
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
;
86 /* Construct a CONST_FIXED from a bit payload and machine mode MODE.
87 The bits in PAYLOAD are sign-extended/zero-extended according to MODE. */
90 fixed_from_double_int (double_int payload
, machine_mode mode
)
92 FIXED_VALUE_TYPE value
;
94 gcc_assert (GET_MODE_BITSIZE (mode
) <= HOST_BITS_PER_DOUBLE_INT
);
96 if (SIGNED_SCALAR_FIXED_POINT_MODE_P (mode
))
97 value
.data
= payload
.sext (1 + GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
));
98 else if (UNSIGNED_SCALAR_FIXED_POINT_MODE_P (mode
))
99 value
.data
= payload
.zext (GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
));
109 /* Initialize from a decimal or hexadecimal string. */
112 fixed_from_string (FIXED_VALUE_TYPE
*f
, const char *str
, machine_mode mode
)
114 REAL_VALUE_TYPE real_value
, fixed_value
, base_value
;
116 enum fixed_value_range_code temp
;
120 fbit
= GET_MODE_FBIT (mode
);
122 real_from_string (&real_value
, str
);
123 temp
= check_real_for_fixed_mode (&real_value
, f
->mode
);
124 /* We don't want to warn the case when the _Fract value is 1.0. */
125 if (temp
== FIXED_UNDERFLOW
126 || temp
== FIXED_GT_MAX_EPS
127 || (temp
== FIXED_MAX_EPS
&& ALL_ACCUM_MODE_P (f
->mode
)))
128 warning (OPT_Woverflow
,
129 "large fixed-point constant implicitly truncated to fixed-point type");
130 real_2expN (&base_value
, fbit
, mode
);
131 real_arithmetic (&fixed_value
, MULT_EXPR
, &real_value
, &base_value
);
132 wide_int w
= real_to_integer (&fixed_value
, &fail
,
133 GET_MODE_PRECISION (mode
));
134 f
->data
.low
= w
.elt (0);
135 f
->data
.high
= w
.elt (1);
137 if (temp
== FIXED_MAX_EPS
&& ALL_FRACT_MODE_P (f
->mode
))
139 /* From the spec, we need to evaluate 1 to the maximal value. */
142 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
143 + GET_MODE_IBIT (f
->mode
));
146 f
->data
= f
->data
.ext (SIGNED_FIXED_POINT_MODE_P (f
->mode
)
147 + GET_MODE_FBIT (f
->mode
)
148 + GET_MODE_IBIT (f
->mode
),
149 UNSIGNED_FIXED_POINT_MODE_P (f
->mode
));
152 /* Render F as a decimal floating point constant. */
155 fixed_to_decimal (char *str
, const FIXED_VALUE_TYPE
*f_orig
,
158 REAL_VALUE_TYPE real_value
, base_value
, fixed_value
;
160 signop sgn
= UNSIGNED_FIXED_POINT_MODE_P (f_orig
->mode
) ? UNSIGNED
: SIGNED
;
161 real_2expN (&base_value
, GET_MODE_FBIT (f_orig
->mode
), f_orig
->mode
);
162 real_from_integer (&real_value
, VOIDmode
,
163 wide_int::from (f_orig
->data
,
164 GET_MODE_PRECISION (f_orig
->mode
), sgn
),
166 real_arithmetic (&fixed_value
, RDIV_EXPR
, &real_value
, &base_value
);
167 real_to_decimal (str
, &fixed_value
, buf_size
, 0, 1);
170 /* If SAT_P, saturate A to the maximum or the minimum, and save to *F based on
171 the machine mode MODE.
172 Do not modify *F otherwise.
173 This function assumes the width of double_int is greater than the width
174 of the fixed-point value (the sum of a possible sign bit, possible ibits,
176 Return true, if !SAT_P and overflow. */
179 fixed_saturate1 (machine_mode mode
, double_int a
, double_int
*f
,
182 bool overflow_p
= false;
183 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (mode
);
184 int i_f_bits
= GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
);
186 if (unsigned_p
) /* Unsigned type. */
191 max
= max
.zext (i_f_bits
);
200 else /* Signed type. */
205 max
= max
.zext (i_f_bits
);
208 min
= min
.alshift (i_f_bits
, HOST_BITS_PER_DOUBLE_INT
);
209 min
= min
.sext (1 + i_f_bits
);
217 else if (a
.slt (min
))
228 /* If SAT_P, saturate {A_HIGH, A_LOW} to the maximum or the minimum, and
229 save to *F based on the machine mode MODE.
230 Do not modify *F otherwise.
231 This function assumes the width of two double_int is greater than the width
232 of the fixed-point value (the sum of a possible sign bit, possible ibits,
234 Return true, if !SAT_P and overflow. */
237 fixed_saturate2 (machine_mode mode
, double_int a_high
, double_int a_low
,
238 double_int
*f
, bool sat_p
)
240 bool overflow_p
= false;
241 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (mode
);
242 int i_f_bits
= GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
);
244 if (unsigned_p
) /* Unsigned type. */
246 double_int max_r
, max_s
;
251 max_s
= max_s
.zext (i_f_bits
);
252 if (a_high
.ugt (max_r
)
253 || (a_high
== max_r
&&
262 else /* Signed type. */
264 double_int max_r
, max_s
, min_r
, min_s
;
269 max_s
= max_s
.zext (i_f_bits
);
274 min_s
= min_s
.alshift (i_f_bits
, HOST_BITS_PER_DOUBLE_INT
);
275 min_s
= min_s
.sext (1 + i_f_bits
);
276 if (a_high
.sgt (max_r
)
277 || (a_high
== max_r
&&
285 else if (a_high
.slt (min_r
)
286 || (a_high
== min_r
&&
298 /* Return the sign bit based on I_F_BITS. */
301 get_fixed_sign_bit (double_int a
, int i_f_bits
)
303 if (i_f_bits
< HOST_BITS_PER_WIDE_INT
)
304 return (a
.low
>> i_f_bits
) & 1;
306 return (a
.high
>> (i_f_bits
- HOST_BITS_PER_WIDE_INT
)) & 1;
309 /* Calculate F = A + (SUBTRACT_P ? -B : B).
310 If SAT_P, saturate the result to the max or the min.
311 Return true, if !SAT_P and overflow. */
314 do_fixed_add (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
315 const FIXED_VALUE_TYPE
*b
, bool subtract_p
, bool sat_p
)
317 bool overflow_p
= false;
322 /* This was a conditional expression but it triggered a bug in
329 unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
330 i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
332 f
->data
= a
->data
+ temp
;
333 if (unsigned_p
) /* Unsigned type. */
335 if (subtract_p
) /* Unsigned subtraction. */
337 if (a
->data
.ult (b
->data
))
348 else /* Unsigned addition. */
350 f
->data
= f
->data
.zext (i_f_bits
);
351 if (f
->data
.ult (a
->data
)
352 || f
->data
.ult (b
->data
))
364 else /* Signed type. */
367 && (get_fixed_sign_bit (a
->data
, i_f_bits
)
368 == get_fixed_sign_bit (b
->data
, i_f_bits
))
369 && (get_fixed_sign_bit (a
->data
, i_f_bits
)
370 != get_fixed_sign_bit (f
->data
, i_f_bits
)))
372 && (get_fixed_sign_bit (a
->data
, i_f_bits
)
373 != get_fixed_sign_bit (b
->data
, i_f_bits
))
374 && (get_fixed_sign_bit (a
->data
, i_f_bits
)
375 != get_fixed_sign_bit (f
->data
, i_f_bits
))))
381 f
->data
= f
->data
.alshift (i_f_bits
, HOST_BITS_PER_DOUBLE_INT
);
382 if (get_fixed_sign_bit (a
->data
, i_f_bits
) == 0)
391 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
395 /* Calculate F = A * B.
396 If SAT_P, saturate the result to the max or the min.
397 Return true, if !SAT_P and overflow. */
400 do_fixed_multiply (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
401 const FIXED_VALUE_TYPE
*b
, bool sat_p
)
403 bool overflow_p
= false;
404 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
405 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
407 if (GET_MODE_PRECISION (f
->mode
) <= HOST_BITS_PER_WIDE_INT
)
409 f
->data
= a
->data
* b
->data
;
410 f
->data
= f
->data
.lshift (-GET_MODE_FBIT (f
->mode
),
411 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
412 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
416 /* The result of multiplication expands to two double_int. */
417 double_int a_high
, a_low
, b_high
, b_low
;
418 double_int high_high
, high_low
, low_high
, low_low
;
419 double_int r
, s
, temp1
, temp2
;
422 /* Decompose a and b to four double_int. */
423 a_high
.low
= a
->data
.high
;
425 a_low
.low
= a
->data
.low
;
427 b_high
.low
= b
->data
.high
;
429 b_low
.low
= b
->data
.low
;
432 /* Perform four multiplications. */
433 low_low
= a_low
* b_low
;
434 low_high
= a_low
* b_high
;
435 high_low
= a_high
* b_low
;
436 high_high
= a_high
* b_high
;
438 /* Accumulate four results to {r, s}. */
439 temp1
.high
= high_low
.low
;
444 carry
++; /* Carry */
447 temp2
.high
= low_high
.low
;
452 carry
++; /* Carry */
454 temp1
.low
= high_low
.high
;
456 r
= high_high
+ temp1
;
457 temp1
.low
= low_high
.high
;
464 /* We need to subtract b from r, if a < 0. */
465 if (!unsigned_p
&& a
->data
.high
< 0)
467 /* We need to subtract a from r, if b < 0. */
468 if (!unsigned_p
&& b
->data
.high
< 0)
471 /* Shift right the result by FBIT. */
472 if (GET_MODE_FBIT (f
->mode
) == HOST_BITS_PER_DOUBLE_INT
)
487 f
->data
.high
= s
.high
;
491 s
= s
.llshift ((-GET_MODE_FBIT (f
->mode
)), HOST_BITS_PER_DOUBLE_INT
);
492 f
->data
= r
.llshift ((HOST_BITS_PER_DOUBLE_INT
493 - GET_MODE_FBIT (f
->mode
)),
494 HOST_BITS_PER_DOUBLE_INT
);
495 f
->data
.low
= f
->data
.low
| s
.low
;
496 f
->data
.high
= f
->data
.high
| s
.high
;
498 s
.high
= f
->data
.high
;
499 r
= r
.lshift (-GET_MODE_FBIT (f
->mode
),
500 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
503 overflow_p
= fixed_saturate2 (f
->mode
, r
, s
, &f
->data
, sat_p
);
506 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
510 /* Calculate F = A / B.
511 If SAT_P, saturate the result to the max or the min.
512 Return true, if !SAT_P and overflow. */
515 do_fixed_divide (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
516 const FIXED_VALUE_TYPE
*b
, bool sat_p
)
518 bool overflow_p
= false;
519 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
520 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
522 if (GET_MODE_PRECISION (f
->mode
) <= HOST_BITS_PER_WIDE_INT
)
524 f
->data
= a
->data
.lshift (GET_MODE_FBIT (f
->mode
),
525 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
526 f
->data
= f
->data
.div (b
->data
, unsigned_p
, TRUNC_DIV_EXPR
);
527 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
531 double_int pos_a
, pos_b
, r
, s
;
532 double_int quo_r
, quo_s
, mod
, temp
;
536 /* If a < 0, negate a. */
537 if (!unsigned_p
&& a
->data
.high
< 0)
545 /* If b < 0, negate b. */
546 if (!unsigned_p
&& b
->data
.high
< 0)
554 /* Left shift pos_a to {r, s} by FBIT. */
555 if (GET_MODE_FBIT (f
->mode
) == HOST_BITS_PER_DOUBLE_INT
)
563 s
= pos_a
.llshift (GET_MODE_FBIT (f
->mode
), HOST_BITS_PER_DOUBLE_INT
);
564 r
= pos_a
.llshift (- (HOST_BITS_PER_DOUBLE_INT
565 - GET_MODE_FBIT (f
->mode
)),
566 HOST_BITS_PER_DOUBLE_INT
);
569 /* Divide r by pos_b to quo_r. The remainder is in mod. */
570 quo_r
= r
.divmod (pos_b
, 1, TRUNC_DIV_EXPR
, &mod
);
571 quo_s
= double_int_zero
;
573 for (i
= 0; i
< HOST_BITS_PER_DOUBLE_INT
; i
++)
575 /* Record the leftmost bit of mod. */
576 int leftmost_mod
= (mod
.high
< 0);
578 /* Shift left mod by 1 bit. */
579 mod
= mod
.lshift (1);
581 /* Test the leftmost bit of s to add to mod. */
585 /* Shift left quo_s by 1 bit. */
586 quo_s
= quo_s
.lshift (1);
588 /* Try to calculate (mod - pos_b). */
591 if (leftmost_mod
== 1 || mod
.ucmp (pos_b
) != -1)
597 /* Shift left s by 1 bit. */
605 if (quo_s
.high
== 0 && quo_s
.low
== 0)
609 quo_r
.low
= ~quo_r
.low
;
610 quo_r
.high
= ~quo_r
.high
;
615 overflow_p
= fixed_saturate2 (f
->mode
, quo_r
, quo_s
, &f
->data
, sat_p
);
618 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
622 /* Calculate F = A << B if LEFT_P. Otherwise, F = A >> B.
623 If SAT_P, saturate the result to the max or the min.
624 Return true, if !SAT_P and overflow. */
627 do_fixed_shift (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
628 const FIXED_VALUE_TYPE
*b
, bool left_p
, bool sat_p
)
630 bool overflow_p
= false;
631 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
632 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
635 if (b
->data
.low
== 0)
641 if (GET_MODE_PRECISION (f
->mode
) <= HOST_BITS_PER_WIDE_INT
|| (!left_p
))
643 f
->data
= a
->data
.lshift (left_p
? b
->data
.low
: -b
->data
.low
,
644 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
645 if (left_p
) /* Only left shift saturates. */
646 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
648 else /* We need two double_int to store the left-shift result. */
650 double_int temp_high
, temp_low
;
651 if (b
->data
.low
== HOST_BITS_PER_DOUBLE_INT
)
659 temp_low
= a
->data
.lshift (b
->data
.low
,
660 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
661 /* Logical shift right to temp_high. */
662 temp_high
= a
->data
.llshift (b
->data
.low
- HOST_BITS_PER_DOUBLE_INT
,
663 HOST_BITS_PER_DOUBLE_INT
);
665 if (!unsigned_p
&& a
->data
.high
< 0) /* Signed-extend temp_high. */
666 temp_high
= temp_high
.ext (b
->data
.low
, unsigned_p
);
668 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
, &f
->data
,
671 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
676 If SAT_P, saturate the result to the max or the min.
677 Return true, if !SAT_P and overflow. */
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
);
687 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
689 if (unsigned_p
) /* Unsigned type. */
691 if (f
->data
.low
!= 0 || f
->data
.high
!= 0)
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
)
709 /* Saturate to the maximum by subtracting f->data by one. */
712 f
->data
= f
->data
.zext (i_f_bits
);
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. */
727 fixed_arithmetic (FIXED_VALUE_TYPE
*f
, int icode
, const FIXED_VALUE_TYPE
*op0
,
728 const FIXED_VALUE_TYPE
*op1
, bool sat_p
)
733 return do_fixed_neg (f
, op0
, sat_p
);
737 gcc_assert (op0
->mode
== op1
->mode
);
738 return do_fixed_add (f
, op0
, op1
, false, sat_p
);
742 gcc_assert (op0
->mode
== op1
->mode
);
743 return do_fixed_add (f
, op0
, op1
, true, sat_p
);
747 gcc_assert (op0
->mode
== op1
->mode
);
748 return do_fixed_multiply (f
, op0
, op1
, sat_p
);
752 gcc_assert (op0
->mode
== op1
->mode
);
753 return do_fixed_divide (f
, op0
, op1
, sat_p
);
757 return do_fixed_shift (f
, op0
, op1
, true, sat_p
);
761 return do_fixed_shift (f
, op0
, op1
, false, sat_p
);
770 /* Compare fixed-point values by tree_code.
771 Note that OP0 and OP1 must have the same mode. */
774 fixed_compare (int icode
, const FIXED_VALUE_TYPE
*op0
,
775 const FIXED_VALUE_TYPE
*op1
)
777 enum tree_code code
= (enum tree_code
) icode
;
778 gcc_assert (op0
->mode
== op1
->mode
);
783 return op0
->data
!= op1
->data
;
786 return op0
->data
== op1
->data
;
789 return op0
->data
.cmp (op1
->data
,
790 UNSIGNED_FIXED_POINT_MODE_P (op0
->mode
)) == -1;
793 return op0
->data
.cmp (op1
->data
,
794 UNSIGNED_FIXED_POINT_MODE_P (op0
->mode
)) != 1;
797 return op0
->data
.cmp (op1
->data
,
798 UNSIGNED_FIXED_POINT_MODE_P (op0
->mode
)) == 1;
801 return op0
->data
.cmp (op1
->data
,
802 UNSIGNED_FIXED_POINT_MODE_P (op0
->mode
)) != -1;
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. */
814 fixed_convert (FIXED_VALUE_TYPE
*f
, machine_mode mode
,
815 const FIXED_VALUE_TYPE
*a
, bool sat_p
)
817 bool overflow_p
= false;
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 temp_low
= a
->data
.lshift (amount
,
830 HOST_BITS_PER_DOUBLE_INT
,
831 SIGNED_FIXED_POINT_MODE_P (a
->mode
));
832 /* Logical shift right to temp_high. */
833 temp_high
= a
->data
.llshift (amount
- HOST_BITS_PER_DOUBLE_INT
,
834 HOST_BITS_PER_DOUBLE_INT
);
835 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
)
836 && a
->data
.high
< 0) /* Signed-extend temp_high. */
837 temp_high
= temp_high
.sext (amount
);
840 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
) ==
841 SIGNED_FIXED_POINT_MODE_P (f
->mode
))
842 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
, &f
->data
,
846 /* Take care of the cases when converting between signed and
848 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
))
850 /* Signed -> Unsigned. */
851 if (a
->data
.high
< 0)
855 f
->data
.low
= 0; /* Set to zero. */
856 f
->data
.high
= 0; /* Set to zero. */
862 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
867 /* Unsigned -> Signed. */
868 if (temp_high
.high
< 0)
872 /* Set to maximum. */
873 f
->data
.low
= -1; /* Set to all ones. */
874 f
->data
.high
= -1; /* Set to all ones. */
875 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
876 + GET_MODE_IBIT (f
->mode
));
877 /* Clear the sign. */
883 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
890 /* Right shift a to temp based on a->mode. */
892 temp
= a
->data
.lshift (GET_MODE_FBIT (mode
) - GET_MODE_FBIT (a
->mode
),
893 HOST_BITS_PER_DOUBLE_INT
,
894 SIGNED_FIXED_POINT_MODE_P (a
->mode
));
897 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
) ==
898 SIGNED_FIXED_POINT_MODE_P (f
->mode
))
899 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
902 /* Take care of the cases when converting between signed and
904 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
))
906 /* Signed -> Unsigned. */
907 if (a
->data
.high
< 0)
911 f
->data
.low
= 0; /* Set to zero. */
912 f
->data
.high
= 0; /* Set to zero. */
918 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
,
923 /* Unsigned -> Signed. */
928 /* Set to maximum. */
929 f
->data
.low
= -1; /* Set to all ones. */
930 f
->data
.high
= -1; /* Set to all ones. */
931 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
932 + GET_MODE_IBIT (f
->mode
));
933 /* Clear the sign. */
939 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
,
945 f
->data
= f
->data
.ext (SIGNED_FIXED_POINT_MODE_P (f
->mode
)
946 + GET_MODE_FBIT (f
->mode
)
947 + GET_MODE_IBIT (f
->mode
),
948 UNSIGNED_FIXED_POINT_MODE_P (f
->mode
));
952 /* Convert to a new fixed-point mode from an integer.
953 If UNSIGNED_P, this integer is unsigned.
954 If SAT_P, saturate the result to the max or the min.
955 Return true, if !SAT_P and overflow. */
958 fixed_convert_from_int (FIXED_VALUE_TYPE
*f
, machine_mode mode
,
959 double_int a
, bool unsigned_p
, bool sat_p
)
961 bool overflow_p
= false;
962 /* Left shift a to temp_high, temp_low. */
963 double_int temp_high
, temp_low
;
964 int amount
= GET_MODE_FBIT (mode
);
965 if (amount
== HOST_BITS_PER_DOUBLE_INT
)
973 temp_low
= a
.llshift (amount
, HOST_BITS_PER_DOUBLE_INT
);
975 /* Logical shift right to temp_high. */
976 temp_high
= a
.llshift (amount
- HOST_BITS_PER_DOUBLE_INT
,
977 HOST_BITS_PER_DOUBLE_INT
);
979 if (!unsigned_p
&& a
.high
< 0) /* Signed-extend temp_high. */
980 temp_high
= temp_high
.sext (amount
);
985 if (unsigned_p
== UNSIGNED_FIXED_POINT_MODE_P (f
->mode
))
986 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
, &f
->data
,
990 /* Take care of the cases when converting between signed and unsigned. */
993 /* Signed -> Unsigned. */
998 f
->data
.low
= 0; /* Set to zero. */
999 f
->data
.high
= 0; /* Set to zero. */
1005 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
1010 /* Unsigned -> Signed. */
1011 if (temp_high
.high
< 0)
1015 /* Set to maximum. */
1016 f
->data
.low
= -1; /* Set to all ones. */
1017 f
->data
.high
= -1; /* Set to all ones. */
1018 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
1019 + GET_MODE_IBIT (f
->mode
));
1020 /* Clear the sign. */
1026 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
1030 f
->data
= f
->data
.ext (SIGNED_FIXED_POINT_MODE_P (f
->mode
)
1031 + GET_MODE_FBIT (f
->mode
)
1032 + GET_MODE_IBIT (f
->mode
),
1033 UNSIGNED_FIXED_POINT_MODE_P (f
->mode
));
1037 /* Convert to a new fixed-point mode from a real.
1038 If SAT_P, saturate the result to the max or the min.
1039 Return true, if !SAT_P and overflow. */
1042 fixed_convert_from_real (FIXED_VALUE_TYPE
*f
, machine_mode mode
,
1043 const REAL_VALUE_TYPE
*a
, bool sat_p
)
1045 bool overflow_p
= false;
1046 REAL_VALUE_TYPE real_value
, fixed_value
, base_value
;
1047 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (mode
);
1048 int i_f_bits
= GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
);
1049 unsigned int fbit
= GET_MODE_FBIT (mode
);
1050 enum fixed_value_range_code temp
;
1055 real_2expN (&base_value
, fbit
, mode
);
1056 real_arithmetic (&fixed_value
, MULT_EXPR
, &real_value
, &base_value
);
1058 wide_int w
= real_to_integer (&fixed_value
, &fail
,
1059 GET_MODE_PRECISION (mode
));
1060 f
->data
.low
= w
.elt (0);
1061 f
->data
.high
= w
.elt (1);
1062 temp
= check_real_for_fixed_mode (&real_value
, mode
);
1063 if (temp
== FIXED_UNDERFLOW
) /* Minimum. */
1076 f
->data
= f
->data
.alshift (i_f_bits
, HOST_BITS_PER_DOUBLE_INT
);
1077 f
->data
= f
->data
.sext (1 + i_f_bits
);
1083 else if (temp
== FIXED_GT_MAX_EPS
|| temp
== FIXED_MAX_EPS
) /* Maximum. */
1089 f
->data
= f
->data
.zext (i_f_bits
);
1094 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
1098 /* Convert to a new real mode from a fixed-point. */
1101 real_convert_from_fixed (REAL_VALUE_TYPE
*r
, machine_mode mode
,
1102 const FIXED_VALUE_TYPE
*f
)
1104 REAL_VALUE_TYPE base_value
, fixed_value
, real_value
;
1106 signop sgn
= UNSIGNED_FIXED_POINT_MODE_P (f
->mode
) ? UNSIGNED
: SIGNED
;
1107 real_2expN (&base_value
, GET_MODE_FBIT (f
->mode
), f
->mode
);
1108 real_from_integer (&fixed_value
, VOIDmode
,
1109 wide_int::from (f
->data
, GET_MODE_PRECISION (f
->mode
),
1111 real_arithmetic (&real_value
, RDIV_EXPR
, &fixed_value
, &base_value
);
1112 real_convert (r
, mode
, &real_value
);
1115 /* Determine whether a fixed-point value F is negative. */
1118 fixed_isneg (const FIXED_VALUE_TYPE
*f
)
1120 if (SIGNED_FIXED_POINT_MODE_P (f
->mode
))
1122 int i_f_bits
= GET_MODE_IBIT (f
->mode
) + GET_MODE_FBIT (f
->mode
);
1123 int sign_bit
= get_fixed_sign_bit (f
->data
, i_f_bits
);