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"
31 #include "diagnostic-core.h"
33 /* Compare two fixed objects for bitwise identity. */
36 fixed_identical (const FIXED_VALUE_TYPE
*a
, const FIXED_VALUE_TYPE
*b
)
38 return (a
->mode
== b
->mode
39 && a
->data
.high
== b
->data
.high
40 && a
->data
.low
== b
->data
.low
);
43 /* Calculate a hash value. */
46 fixed_hash (const FIXED_VALUE_TYPE
*f
)
48 return (unsigned int) (f
->data
.low
^ f
->data
.high
);
51 /* Define the enum code for the range of the fixed-point value. */
52 enum fixed_value_range_code
{
53 FIXED_OK
, /* The value is within the range. */
54 FIXED_UNDERFLOW
, /* The value is less than the minimum. */
55 FIXED_GT_MAX_EPS
, /* The value is greater than the maximum, but not equal
56 to the maximum plus the epsilon. */
57 FIXED_MAX_EPS
/* The value equals the maximum plus the epsilon. */
60 /* Check REAL_VALUE against the range of the fixed-point mode.
61 Return FIXED_OK, if it is within the range.
62 FIXED_UNDERFLOW, if it is less than the minimum.
63 FIXED_GT_MAX_EPS, if it is greater than the maximum, but not equal to
64 the maximum plus the epsilon.
65 FIXED_MAX_EPS, if it is equal to the maximum plus the epsilon. */
67 static enum fixed_value_range_code
68 check_real_for_fixed_mode (REAL_VALUE_TYPE
*real_value
, machine_mode mode
)
70 REAL_VALUE_TYPE max_value
, min_value
, epsilon_value
;
72 real_2expN (&max_value
, GET_MODE_IBIT (mode
), mode
);
73 real_2expN (&epsilon_value
, -GET_MODE_FBIT (mode
), mode
);
75 if (SIGNED_FIXED_POINT_MODE_P (mode
))
76 min_value
= real_value_negate (&max_value
);
78 real_from_string (&min_value
, "0.0");
80 if (real_compare (LT_EXPR
, real_value
, &min_value
))
81 return FIXED_UNDERFLOW
;
82 if (real_compare (EQ_EXPR
, real_value
, &max_value
))
84 real_arithmetic (&max_value
, MINUS_EXPR
, &max_value
, &epsilon_value
);
85 if (real_compare (GT_EXPR
, real_value
, &max_value
))
86 return FIXED_GT_MAX_EPS
;
91 /* Construct a CONST_FIXED from a bit payload and machine mode MODE.
92 The bits in PAYLOAD are sign-extended/zero-extended according to MODE. */
95 fixed_from_double_int (double_int payload
, machine_mode mode
)
97 FIXED_VALUE_TYPE value
;
99 gcc_assert (GET_MODE_BITSIZE (mode
) <= HOST_BITS_PER_DOUBLE_INT
);
101 if (SIGNED_SCALAR_FIXED_POINT_MODE_P (mode
))
102 value
.data
= payload
.sext (1 + GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
));
103 else if (UNSIGNED_SCALAR_FIXED_POINT_MODE_P (mode
))
104 value
.data
= payload
.zext (GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
));
114 /* Initialize from a decimal or hexadecimal string. */
117 fixed_from_string (FIXED_VALUE_TYPE
*f
, const char *str
, machine_mode mode
)
119 REAL_VALUE_TYPE real_value
, fixed_value
, base_value
;
121 enum fixed_value_range_code temp
;
125 fbit
= GET_MODE_FBIT (mode
);
127 real_from_string (&real_value
, str
);
128 temp
= check_real_for_fixed_mode (&real_value
, f
->mode
);
129 /* We don't want to warn the case when the _Fract value is 1.0. */
130 if (temp
== FIXED_UNDERFLOW
131 || temp
== FIXED_GT_MAX_EPS
132 || (temp
== FIXED_MAX_EPS
&& ALL_ACCUM_MODE_P (f
->mode
)))
133 warning (OPT_Woverflow
,
134 "large fixed-point constant implicitly truncated to fixed-point type");
135 real_2expN (&base_value
, fbit
, mode
);
136 real_arithmetic (&fixed_value
, MULT_EXPR
, &real_value
, &base_value
);
137 wide_int w
= real_to_integer (&fixed_value
, &fail
,
138 GET_MODE_PRECISION (mode
));
139 f
->data
.low
= w
.elt (0);
140 f
->data
.high
= w
.elt (1);
142 if (temp
== FIXED_MAX_EPS
&& ALL_FRACT_MODE_P (f
->mode
))
144 /* From the spec, we need to evaluate 1 to the maximal value. */
147 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
148 + GET_MODE_IBIT (f
->mode
));
151 f
->data
= f
->data
.ext (SIGNED_FIXED_POINT_MODE_P (f
->mode
)
152 + GET_MODE_FBIT (f
->mode
)
153 + GET_MODE_IBIT (f
->mode
),
154 UNSIGNED_FIXED_POINT_MODE_P (f
->mode
));
157 /* Render F as a decimal floating point constant. */
160 fixed_to_decimal (char *str
, const FIXED_VALUE_TYPE
*f_orig
,
163 REAL_VALUE_TYPE real_value
, base_value
, fixed_value
;
165 signop sgn
= UNSIGNED_FIXED_POINT_MODE_P (f_orig
->mode
) ? UNSIGNED
: SIGNED
;
166 real_2expN (&base_value
, GET_MODE_FBIT (f_orig
->mode
), f_orig
->mode
);
167 real_from_integer (&real_value
, VOIDmode
,
168 wide_int::from (f_orig
->data
,
169 GET_MODE_PRECISION (f_orig
->mode
), sgn
),
171 real_arithmetic (&fixed_value
, RDIV_EXPR
, &real_value
, &base_value
);
172 real_to_decimal (str
, &fixed_value
, buf_size
, 0, 1);
175 /* If SAT_P, saturate A to the maximum or the minimum, and save to *F based on
176 the machine mode MODE.
177 Do not modify *F otherwise.
178 This function assumes the width of double_int is greater than the width
179 of the fixed-point value (the sum of a possible sign bit, possible ibits,
181 Return true, if !SAT_P and overflow. */
184 fixed_saturate1 (machine_mode mode
, double_int a
, double_int
*f
,
187 bool overflow_p
= false;
188 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (mode
);
189 int i_f_bits
= GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
);
191 if (unsigned_p
) /* Unsigned type. */
196 max
= max
.zext (i_f_bits
);
205 else /* Signed type. */
210 max
= max
.zext (i_f_bits
);
213 min
= min
.alshift (i_f_bits
, HOST_BITS_PER_DOUBLE_INT
);
214 min
= min
.sext (1 + i_f_bits
);
222 else if (a
.slt (min
))
233 /* If SAT_P, saturate {A_HIGH, A_LOW} to the maximum or the minimum, and
234 save to *F based on the machine mode MODE.
235 Do not modify *F otherwise.
236 This function assumes the width of two double_int is greater than the width
237 of the fixed-point value (the sum of a possible sign bit, possible ibits,
239 Return true, if !SAT_P and overflow. */
242 fixed_saturate2 (machine_mode mode
, double_int a_high
, double_int a_low
,
243 double_int
*f
, bool sat_p
)
245 bool overflow_p
= false;
246 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (mode
);
247 int i_f_bits
= GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
);
249 if (unsigned_p
) /* Unsigned type. */
251 double_int max_r
, max_s
;
256 max_s
= max_s
.zext (i_f_bits
);
257 if (a_high
.ugt (max_r
)
258 || (a_high
== max_r
&&
267 else /* Signed type. */
269 double_int max_r
, max_s
, min_r
, min_s
;
274 max_s
= max_s
.zext (i_f_bits
);
279 min_s
= min_s
.alshift (i_f_bits
, HOST_BITS_PER_DOUBLE_INT
);
280 min_s
= min_s
.sext (1 + i_f_bits
);
281 if (a_high
.sgt (max_r
)
282 || (a_high
== max_r
&&
290 else if (a_high
.slt (min_r
)
291 || (a_high
== min_r
&&
303 /* Return the sign bit based on I_F_BITS. */
306 get_fixed_sign_bit (double_int a
, int i_f_bits
)
308 if (i_f_bits
< HOST_BITS_PER_WIDE_INT
)
309 return (a
.low
>> i_f_bits
) & 1;
311 return (a
.high
>> (i_f_bits
- HOST_BITS_PER_WIDE_INT
)) & 1;
314 /* Calculate F = A + (SUBTRACT_P ? -B : B).
315 If SAT_P, saturate the result to the max or the min.
316 Return true, if !SAT_P and overflow. */
319 do_fixed_add (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
320 const FIXED_VALUE_TYPE
*b
, bool subtract_p
, bool sat_p
)
322 bool overflow_p
= false;
327 /* This was a conditional expression but it triggered a bug in
334 unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
335 i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
337 f
->data
= a
->data
+ temp
;
338 if (unsigned_p
) /* Unsigned type. */
340 if (subtract_p
) /* Unsigned subtraction. */
342 if (a
->data
.ult (b
->data
))
353 else /* Unsigned addition. */
355 f
->data
= f
->data
.zext (i_f_bits
);
356 if (f
->data
.ult (a
->data
)
357 || f
->data
.ult (b
->data
))
369 else /* Signed type. */
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
)))
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
))))
386 f
->data
= f
->data
.alshift (i_f_bits
, HOST_BITS_PER_DOUBLE_INT
);
387 if (get_fixed_sign_bit (a
->data
, i_f_bits
) == 0)
396 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
400 /* Calculate F = A * B.
401 If SAT_P, saturate the result to the max or the min.
402 Return true, if !SAT_P and overflow. */
405 do_fixed_multiply (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
406 const FIXED_VALUE_TYPE
*b
, bool sat_p
)
408 bool overflow_p
= false;
409 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
410 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
412 if (GET_MODE_PRECISION (f
->mode
) <= HOST_BITS_PER_WIDE_INT
)
414 f
->data
= a
->data
* b
->data
;
415 f
->data
= f
->data
.lshift (-GET_MODE_FBIT (f
->mode
),
416 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
417 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
421 /* The result of multiplication expands to two double_int. */
422 double_int a_high
, a_low
, b_high
, b_low
;
423 double_int high_high
, high_low
, low_high
, low_low
;
424 double_int r
, s
, temp1
, temp2
;
427 /* Decompose a and b to four double_int. */
428 a_high
.low
= a
->data
.high
;
430 a_low
.low
= a
->data
.low
;
432 b_high
.low
= b
->data
.high
;
434 b_low
.low
= b
->data
.low
;
437 /* Perform four multiplications. */
438 low_low
= a_low
* b_low
;
439 low_high
= a_low
* b_high
;
440 high_low
= a_high
* b_low
;
441 high_high
= a_high
* b_high
;
443 /* Accumulate four results to {r, s}. */
444 temp1
.high
= high_low
.low
;
449 carry
++; /* Carry */
452 temp2
.high
= low_high
.low
;
457 carry
++; /* Carry */
459 temp1
.low
= high_low
.high
;
461 r
= high_high
+ temp1
;
462 temp1
.low
= low_high
.high
;
469 /* We need to subtract b from r, if a < 0. */
470 if (!unsigned_p
&& a
->data
.high
< 0)
472 /* We need to subtract a from r, if b < 0. */
473 if (!unsigned_p
&& b
->data
.high
< 0)
476 /* Shift right the result by FBIT. */
477 if (GET_MODE_FBIT (f
->mode
) == HOST_BITS_PER_DOUBLE_INT
)
492 f
->data
.high
= s
.high
;
496 s
= s
.llshift ((-GET_MODE_FBIT (f
->mode
)), HOST_BITS_PER_DOUBLE_INT
);
497 f
->data
= r
.llshift ((HOST_BITS_PER_DOUBLE_INT
498 - GET_MODE_FBIT (f
->mode
)),
499 HOST_BITS_PER_DOUBLE_INT
);
500 f
->data
.low
= f
->data
.low
| s
.low
;
501 f
->data
.high
= f
->data
.high
| s
.high
;
503 s
.high
= f
->data
.high
;
504 r
= r
.lshift (-GET_MODE_FBIT (f
->mode
),
505 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
508 overflow_p
= fixed_saturate2 (f
->mode
, r
, s
, &f
->data
, sat_p
);
511 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
515 /* Calculate F = A / B.
516 If SAT_P, saturate the result to the max or the min.
517 Return true, if !SAT_P and overflow. */
520 do_fixed_divide (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
521 const FIXED_VALUE_TYPE
*b
, bool sat_p
)
523 bool overflow_p
= false;
524 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
525 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
527 if (GET_MODE_PRECISION (f
->mode
) <= HOST_BITS_PER_WIDE_INT
)
529 f
->data
= a
->data
.lshift (GET_MODE_FBIT (f
->mode
),
530 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
531 f
->data
= f
->data
.div (b
->data
, unsigned_p
, TRUNC_DIV_EXPR
);
532 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
536 double_int pos_a
, pos_b
, r
, s
;
537 double_int quo_r
, quo_s
, mod
, temp
;
541 /* If a < 0, negate a. */
542 if (!unsigned_p
&& a
->data
.high
< 0)
550 /* If b < 0, negate b. */
551 if (!unsigned_p
&& b
->data
.high
< 0)
559 /* Left shift pos_a to {r, s} by FBIT. */
560 if (GET_MODE_FBIT (f
->mode
) == HOST_BITS_PER_DOUBLE_INT
)
568 s
= pos_a
.llshift (GET_MODE_FBIT (f
->mode
), HOST_BITS_PER_DOUBLE_INT
);
569 r
= pos_a
.llshift (- (HOST_BITS_PER_DOUBLE_INT
570 - GET_MODE_FBIT (f
->mode
)),
571 HOST_BITS_PER_DOUBLE_INT
);
574 /* Divide r by pos_b to quo_r. The remainder is in mod. */
575 quo_r
= r
.divmod (pos_b
, 1, TRUNC_DIV_EXPR
, &mod
);
576 quo_s
= double_int_zero
;
578 for (i
= 0; i
< HOST_BITS_PER_DOUBLE_INT
; i
++)
580 /* Record the leftmost bit of mod. */
581 int leftmost_mod
= (mod
.high
< 0);
583 /* Shift left mod by 1 bit. */
584 mod
= mod
.lshift (1);
586 /* Test the leftmost bit of s to add to mod. */
590 /* Shift left quo_s by 1 bit. */
591 quo_s
= quo_s
.lshift (1);
593 /* Try to calculate (mod - pos_b). */
596 if (leftmost_mod
== 1 || mod
.ucmp (pos_b
) != -1)
602 /* Shift left s by 1 bit. */
610 if (quo_s
.high
== 0 && quo_s
.low
== 0)
614 quo_r
.low
= ~quo_r
.low
;
615 quo_r
.high
= ~quo_r
.high
;
620 overflow_p
= fixed_saturate2 (f
->mode
, quo_r
, quo_s
, &f
->data
, sat_p
);
623 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
627 /* Calculate F = A << B if LEFT_P. Otherwise, F = A >> B.
628 If SAT_P, saturate the result to the max or the min.
629 Return true, if !SAT_P and overflow. */
632 do_fixed_shift (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
633 const FIXED_VALUE_TYPE
*b
, bool left_p
, bool sat_p
)
635 bool overflow_p
= false;
636 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
637 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
640 if (b
->data
.low
== 0)
646 if (GET_MODE_PRECISION (f
->mode
) <= HOST_BITS_PER_WIDE_INT
|| (!left_p
))
648 f
->data
= a
->data
.lshift (left_p
? b
->data
.low
: -b
->data
.low
,
649 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
650 if (left_p
) /* Only left shift saturates. */
651 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
653 else /* We need two double_int to store the left-shift result. */
655 double_int temp_high
, temp_low
;
656 if (b
->data
.low
== HOST_BITS_PER_DOUBLE_INT
)
664 temp_low
= a
->data
.lshift (b
->data
.low
,
665 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
666 /* Logical shift right to temp_high. */
667 temp_high
= a
->data
.llshift (b
->data
.low
- HOST_BITS_PER_DOUBLE_INT
,
668 HOST_BITS_PER_DOUBLE_INT
);
670 if (!unsigned_p
&& a
->data
.high
< 0) /* Signed-extend temp_high. */
671 temp_high
= temp_high
.ext (b
->data
.low
, unsigned_p
);
673 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
, &f
->data
,
676 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
681 If SAT_P, saturate the result to the max or the min.
682 Return true, if !SAT_P and overflow. */
685 do_fixed_neg (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
, bool sat_p
)
687 bool overflow_p
= false;
688 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
689 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
692 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
694 if (unsigned_p
) /* Unsigned type. */
696 if (f
->data
.low
!= 0 || f
->data
.high
!= 0)
707 else /* Signed type. */
709 if (!(f
->data
.high
== 0 && f
->data
.low
== 0)
710 && f
->data
.high
== a
->data
.high
&& f
->data
.low
== a
->data
.low
)
714 /* Saturate to the maximum by subtracting f->data by one. */
717 f
->data
= f
->data
.zext (i_f_bits
);
726 /* Perform the binary or unary operation described by CODE.
727 Note that OP0 and OP1 must have the same mode for binary operators.
728 For a unary operation, leave OP1 NULL.
729 Return true, if !SAT_P and overflow. */
732 fixed_arithmetic (FIXED_VALUE_TYPE
*f
, int icode
, const FIXED_VALUE_TYPE
*op0
,
733 const FIXED_VALUE_TYPE
*op1
, bool sat_p
)
738 return do_fixed_neg (f
, op0
, sat_p
);
742 gcc_assert (op0
->mode
== op1
->mode
);
743 return do_fixed_add (f
, op0
, op1
, false, sat_p
);
747 gcc_assert (op0
->mode
== op1
->mode
);
748 return do_fixed_add (f
, op0
, op1
, true, sat_p
);
752 gcc_assert (op0
->mode
== op1
->mode
);
753 return do_fixed_multiply (f
, op0
, op1
, sat_p
);
757 gcc_assert (op0
->mode
== op1
->mode
);
758 return do_fixed_divide (f
, op0
, op1
, sat_p
);
762 return do_fixed_shift (f
, op0
, op1
, true, sat_p
);
766 return do_fixed_shift (f
, op0
, op1
, false, sat_p
);
775 /* Compare fixed-point values by tree_code.
776 Note that OP0 and OP1 must have the same mode. */
779 fixed_compare (int icode
, const FIXED_VALUE_TYPE
*op0
,
780 const FIXED_VALUE_TYPE
*op1
)
782 enum tree_code code
= (enum tree_code
) icode
;
783 gcc_assert (op0
->mode
== op1
->mode
);
788 return op0
->data
!= op1
->data
;
791 return op0
->data
== op1
->data
;
794 return op0
->data
.cmp (op1
->data
,
795 UNSIGNED_FIXED_POINT_MODE_P (op0
->mode
)) == -1;
798 return op0
->data
.cmp (op1
->data
,
799 UNSIGNED_FIXED_POINT_MODE_P (op0
->mode
)) != 1;
802 return op0
->data
.cmp (op1
->data
,
803 UNSIGNED_FIXED_POINT_MODE_P (op0
->mode
)) == 1;
806 return op0
->data
.cmp (op1
->data
,
807 UNSIGNED_FIXED_POINT_MODE_P (op0
->mode
)) != -1;
814 /* Extend or truncate to a new mode.
815 If SAT_P, saturate the result to the max or the min.
816 Return true, if !SAT_P and overflow. */
819 fixed_convert (FIXED_VALUE_TYPE
*f
, machine_mode mode
,
820 const FIXED_VALUE_TYPE
*a
, bool sat_p
)
822 bool overflow_p
= false;
829 if (GET_MODE_FBIT (mode
) > GET_MODE_FBIT (a
->mode
))
831 /* Left shift a to temp_high, temp_low based on a->mode. */
832 double_int temp_high
, temp_low
;
833 int amount
= GET_MODE_FBIT (mode
) - GET_MODE_FBIT (a
->mode
);
834 temp_low
= a
->data
.lshift (amount
,
835 HOST_BITS_PER_DOUBLE_INT
,
836 SIGNED_FIXED_POINT_MODE_P (a
->mode
));
837 /* Logical shift right to temp_high. */
838 temp_high
= a
->data
.llshift (amount
- HOST_BITS_PER_DOUBLE_INT
,
839 HOST_BITS_PER_DOUBLE_INT
);
840 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
)
841 && a
->data
.high
< 0) /* Signed-extend temp_high. */
842 temp_high
= temp_high
.sext (amount
);
845 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
) ==
846 SIGNED_FIXED_POINT_MODE_P (f
->mode
))
847 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
, &f
->data
,
851 /* Take care of the cases when converting between signed and
853 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
))
855 /* Signed -> Unsigned. */
856 if (a
->data
.high
< 0)
860 f
->data
.low
= 0; /* Set to zero. */
861 f
->data
.high
= 0; /* Set to zero. */
867 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
872 /* Unsigned -> Signed. */
873 if (temp_high
.high
< 0)
877 /* Set to maximum. */
878 f
->data
.low
= -1; /* Set to all ones. */
879 f
->data
.high
= -1; /* Set to all ones. */
880 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
881 + GET_MODE_IBIT (f
->mode
));
882 /* Clear the sign. */
888 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
895 /* Right shift a to temp based on a->mode. */
897 temp
= a
->data
.lshift (GET_MODE_FBIT (mode
) - GET_MODE_FBIT (a
->mode
),
898 HOST_BITS_PER_DOUBLE_INT
,
899 SIGNED_FIXED_POINT_MODE_P (a
->mode
));
902 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
) ==
903 SIGNED_FIXED_POINT_MODE_P (f
->mode
))
904 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
907 /* Take care of the cases when converting between signed and
909 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
))
911 /* Signed -> Unsigned. */
912 if (a
->data
.high
< 0)
916 f
->data
.low
= 0; /* Set to zero. */
917 f
->data
.high
= 0; /* Set to zero. */
923 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
,
928 /* Unsigned -> Signed. */
933 /* Set to maximum. */
934 f
->data
.low
= -1; /* Set to all ones. */
935 f
->data
.high
= -1; /* Set to all ones. */
936 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
937 + GET_MODE_IBIT (f
->mode
));
938 /* Clear the sign. */
944 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
,
950 f
->data
= f
->data
.ext (SIGNED_FIXED_POINT_MODE_P (f
->mode
)
951 + GET_MODE_FBIT (f
->mode
)
952 + GET_MODE_IBIT (f
->mode
),
953 UNSIGNED_FIXED_POINT_MODE_P (f
->mode
));
957 /* Convert to a new fixed-point mode from an integer.
958 If UNSIGNED_P, this integer is unsigned.
959 If SAT_P, saturate the result to the max or the min.
960 Return true, if !SAT_P and overflow. */
963 fixed_convert_from_int (FIXED_VALUE_TYPE
*f
, machine_mode mode
,
964 double_int a
, bool unsigned_p
, bool sat_p
)
966 bool overflow_p
= false;
967 /* Left shift a to temp_high, temp_low. */
968 double_int temp_high
, temp_low
;
969 int amount
= GET_MODE_FBIT (mode
);
970 if (amount
== HOST_BITS_PER_DOUBLE_INT
)
978 temp_low
= a
.llshift (amount
, HOST_BITS_PER_DOUBLE_INT
);
980 /* Logical shift right to temp_high. */
981 temp_high
= a
.llshift (amount
- HOST_BITS_PER_DOUBLE_INT
,
982 HOST_BITS_PER_DOUBLE_INT
);
984 if (!unsigned_p
&& a
.high
< 0) /* Signed-extend temp_high. */
985 temp_high
= temp_high
.sext (amount
);
990 if (unsigned_p
== UNSIGNED_FIXED_POINT_MODE_P (f
->mode
))
991 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
, &f
->data
,
995 /* Take care of the cases when converting between signed and unsigned. */
998 /* Signed -> Unsigned. */
1003 f
->data
.low
= 0; /* Set to zero. */
1004 f
->data
.high
= 0; /* Set to zero. */
1010 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
1015 /* Unsigned -> Signed. */
1016 if (temp_high
.high
< 0)
1020 /* Set to maximum. */
1021 f
->data
.low
= -1; /* Set to all ones. */
1022 f
->data
.high
= -1; /* Set to all ones. */
1023 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
1024 + GET_MODE_IBIT (f
->mode
));
1025 /* Clear the sign. */
1031 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
1035 f
->data
= f
->data
.ext (SIGNED_FIXED_POINT_MODE_P (f
->mode
)
1036 + GET_MODE_FBIT (f
->mode
)
1037 + GET_MODE_IBIT (f
->mode
),
1038 UNSIGNED_FIXED_POINT_MODE_P (f
->mode
));
1042 /* Convert to a new fixed-point mode from a real.
1043 If SAT_P, saturate the result to the max or the min.
1044 Return true, if !SAT_P and overflow. */
1047 fixed_convert_from_real (FIXED_VALUE_TYPE
*f
, machine_mode mode
,
1048 const REAL_VALUE_TYPE
*a
, bool sat_p
)
1050 bool overflow_p
= false;
1051 REAL_VALUE_TYPE real_value
, fixed_value
, base_value
;
1052 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (mode
);
1053 int i_f_bits
= GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
);
1054 unsigned int fbit
= GET_MODE_FBIT (mode
);
1055 enum fixed_value_range_code temp
;
1060 real_2expN (&base_value
, fbit
, mode
);
1061 real_arithmetic (&fixed_value
, MULT_EXPR
, &real_value
, &base_value
);
1063 wide_int w
= real_to_integer (&fixed_value
, &fail
,
1064 GET_MODE_PRECISION (mode
));
1065 f
->data
.low
= w
.elt (0);
1066 f
->data
.high
= w
.elt (1);
1067 temp
= check_real_for_fixed_mode (&real_value
, mode
);
1068 if (temp
== FIXED_UNDERFLOW
) /* Minimum. */
1081 f
->data
= f
->data
.alshift (i_f_bits
, HOST_BITS_PER_DOUBLE_INT
);
1082 f
->data
= f
->data
.sext (1 + i_f_bits
);
1088 else if (temp
== FIXED_GT_MAX_EPS
|| temp
== FIXED_MAX_EPS
) /* Maximum. */
1094 f
->data
= f
->data
.zext (i_f_bits
);
1099 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
1103 /* Convert to a new real mode from a fixed-point. */
1106 real_convert_from_fixed (REAL_VALUE_TYPE
*r
, machine_mode mode
,
1107 const FIXED_VALUE_TYPE
*f
)
1109 REAL_VALUE_TYPE base_value
, fixed_value
, real_value
;
1111 signop sgn
= UNSIGNED_FIXED_POINT_MODE_P (f
->mode
) ? UNSIGNED
: SIGNED
;
1112 real_2expN (&base_value
, GET_MODE_FBIT (f
->mode
), f
->mode
);
1113 real_from_integer (&fixed_value
, VOIDmode
,
1114 wide_int::from (f
->data
, GET_MODE_PRECISION (f
->mode
),
1116 real_arithmetic (&real_value
, RDIV_EXPR
, &fixed_value
, &base_value
);
1117 real_convert (r
, mode
, &real_value
);
1120 /* Determine whether a fixed-point value F is negative. */
1123 fixed_isneg (const FIXED_VALUE_TYPE
*f
)
1125 if (SIGNED_FIXED_POINT_MODE_P (f
->mode
))
1127 int i_f_bits
= GET_MODE_IBIT (f
->mode
) + GET_MODE_FBIT (f
->mode
);
1128 int sign_bit
= get_fixed_sign_bit (f
->data
, i_f_bits
);