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 "diagnostic-core.h"
29 /* Compare two fixed objects for bitwise identity. */
32 fixed_identical (const FIXED_VALUE_TYPE
*a
, const FIXED_VALUE_TYPE
*b
)
34 return (a
->mode
== b
->mode
35 && a
->data
.high
== b
->data
.high
36 && a
->data
.low
== b
->data
.low
);
39 /* Calculate a hash value. */
42 fixed_hash (const FIXED_VALUE_TYPE
*f
)
44 return (unsigned int) (f
->data
.low
^ f
->data
.high
);
47 /* Define the enum code for the range of the fixed-point value. */
48 enum fixed_value_range_code
{
49 FIXED_OK
, /* The value is within the range. */
50 FIXED_UNDERFLOW
, /* The value is less than the minimum. */
51 FIXED_GT_MAX_EPS
, /* The value is greater than the maximum, but not equal
52 to the maximum plus the epsilon. */
53 FIXED_MAX_EPS
/* The value equals the maximum plus the epsilon. */
56 /* Check REAL_VALUE against the range of the fixed-point mode.
57 Return FIXED_OK, if it is within the range.
58 FIXED_UNDERFLOW, if it is less than the minimum.
59 FIXED_GT_MAX_EPS, if it is greater than the maximum, but not equal to
60 the maximum plus the epsilon.
61 FIXED_MAX_EPS, if it is equal to the maximum plus the epsilon. */
63 static enum fixed_value_range_code
64 check_real_for_fixed_mode (REAL_VALUE_TYPE
*real_value
, machine_mode mode
)
66 REAL_VALUE_TYPE max_value
, min_value
, epsilon_value
;
68 real_2expN (&max_value
, GET_MODE_IBIT (mode
), mode
);
69 real_2expN (&epsilon_value
, -GET_MODE_FBIT (mode
), mode
);
71 if (SIGNED_FIXED_POINT_MODE_P (mode
))
72 min_value
= real_value_negate (&max_value
);
74 real_from_string (&min_value
, "0.0");
76 if (real_compare (LT_EXPR
, real_value
, &min_value
))
77 return FIXED_UNDERFLOW
;
78 if (real_compare (EQ_EXPR
, real_value
, &max_value
))
80 real_arithmetic (&max_value
, MINUS_EXPR
, &max_value
, &epsilon_value
);
81 if (real_compare (GT_EXPR
, real_value
, &max_value
))
82 return FIXED_GT_MAX_EPS
;
87 /* Construct a CONST_FIXED from a bit payload and machine mode MODE.
88 The bits in PAYLOAD are sign-extended/zero-extended according to MODE. */
91 fixed_from_double_int (double_int payload
, machine_mode mode
)
93 FIXED_VALUE_TYPE value
;
95 gcc_assert (GET_MODE_BITSIZE (mode
) <= HOST_BITS_PER_DOUBLE_INT
);
97 if (SIGNED_SCALAR_FIXED_POINT_MODE_P (mode
))
98 value
.data
= payload
.sext (1 + GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
));
99 else if (UNSIGNED_SCALAR_FIXED_POINT_MODE_P (mode
))
100 value
.data
= payload
.zext (GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
));
110 /* Initialize from a decimal or hexadecimal string. */
113 fixed_from_string (FIXED_VALUE_TYPE
*f
, const char *str
, machine_mode mode
)
115 REAL_VALUE_TYPE real_value
, fixed_value
, base_value
;
117 enum fixed_value_range_code temp
;
121 fbit
= GET_MODE_FBIT (mode
);
123 real_from_string (&real_value
, str
);
124 temp
= check_real_for_fixed_mode (&real_value
, f
->mode
);
125 /* We don't want to warn the case when the _Fract value is 1.0. */
126 if (temp
== FIXED_UNDERFLOW
127 || temp
== FIXED_GT_MAX_EPS
128 || (temp
== FIXED_MAX_EPS
&& ALL_ACCUM_MODE_P (f
->mode
)))
129 warning (OPT_Woverflow
,
130 "large fixed-point constant implicitly truncated to fixed-point type");
131 real_2expN (&base_value
, fbit
, mode
);
132 real_arithmetic (&fixed_value
, MULT_EXPR
, &real_value
, &base_value
);
133 wide_int w
= real_to_integer (&fixed_value
, &fail
,
134 GET_MODE_PRECISION (mode
));
135 f
->data
.low
= w
.elt (0);
136 f
->data
.high
= w
.elt (1);
138 if (temp
== FIXED_MAX_EPS
&& ALL_FRACT_MODE_P (f
->mode
))
140 /* From the spec, we need to evaluate 1 to the maximal value. */
143 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
144 + GET_MODE_IBIT (f
->mode
));
147 f
->data
= f
->data
.ext (SIGNED_FIXED_POINT_MODE_P (f
->mode
)
148 + GET_MODE_FBIT (f
->mode
)
149 + GET_MODE_IBIT (f
->mode
),
150 UNSIGNED_FIXED_POINT_MODE_P (f
->mode
));
153 /* Render F as a decimal floating point constant. */
156 fixed_to_decimal (char *str
, const FIXED_VALUE_TYPE
*f_orig
,
159 REAL_VALUE_TYPE real_value
, base_value
, fixed_value
;
161 signop sgn
= UNSIGNED_FIXED_POINT_MODE_P (f_orig
->mode
) ? UNSIGNED
: SIGNED
;
162 real_2expN (&base_value
, GET_MODE_FBIT (f_orig
->mode
), f_orig
->mode
);
163 real_from_integer (&real_value
, VOIDmode
,
164 wide_int::from (f_orig
->data
,
165 GET_MODE_PRECISION (f_orig
->mode
), sgn
),
167 real_arithmetic (&fixed_value
, RDIV_EXPR
, &real_value
, &base_value
);
168 real_to_decimal (str
, &fixed_value
, buf_size
, 0, 1);
171 /* If SAT_P, saturate A to the maximum or the minimum, and save to *F based on
172 the machine mode MODE.
173 Do not modify *F otherwise.
174 This function assumes the width of double_int is greater than the width
175 of the fixed-point value (the sum of a possible sign bit, possible ibits,
177 Return true, if !SAT_P and overflow. */
180 fixed_saturate1 (machine_mode mode
, double_int a
, double_int
*f
,
183 bool overflow_p
= false;
184 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (mode
);
185 int i_f_bits
= GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
);
187 if (unsigned_p
) /* Unsigned type. */
192 max
= max
.zext (i_f_bits
);
201 else /* Signed type. */
206 max
= max
.zext (i_f_bits
);
209 min
= min
.alshift (i_f_bits
, HOST_BITS_PER_DOUBLE_INT
);
210 min
= min
.sext (1 + i_f_bits
);
218 else if (a
.slt (min
))
229 /* If SAT_P, saturate {A_HIGH, A_LOW} to the maximum or the minimum, and
230 save to *F based on the machine mode MODE.
231 Do not modify *F otherwise.
232 This function assumes the width of two double_int is greater than the width
233 of the fixed-point value (the sum of a possible sign bit, possible ibits,
235 Return true, if !SAT_P and overflow. */
238 fixed_saturate2 (machine_mode mode
, double_int a_high
, double_int a_low
,
239 double_int
*f
, bool sat_p
)
241 bool overflow_p
= false;
242 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (mode
);
243 int i_f_bits
= GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
);
245 if (unsigned_p
) /* Unsigned type. */
247 double_int max_r
, max_s
;
252 max_s
= max_s
.zext (i_f_bits
);
253 if (a_high
.ugt (max_r
)
254 || (a_high
== max_r
&&
263 else /* Signed type. */
265 double_int max_r
, max_s
, min_r
, min_s
;
270 max_s
= max_s
.zext (i_f_bits
);
275 min_s
= min_s
.alshift (i_f_bits
, HOST_BITS_PER_DOUBLE_INT
);
276 min_s
= min_s
.sext (1 + i_f_bits
);
277 if (a_high
.sgt (max_r
)
278 || (a_high
== max_r
&&
286 else if (a_high
.slt (min_r
)
287 || (a_high
== min_r
&&
299 /* Return the sign bit based on I_F_BITS. */
302 get_fixed_sign_bit (double_int a
, int i_f_bits
)
304 if (i_f_bits
< HOST_BITS_PER_WIDE_INT
)
305 return (a
.low
>> i_f_bits
) & 1;
307 return (a
.high
>> (i_f_bits
- HOST_BITS_PER_WIDE_INT
)) & 1;
310 /* Calculate F = A + (SUBTRACT_P ? -B : B).
311 If SAT_P, saturate the result to the max or the min.
312 Return true, if !SAT_P and overflow. */
315 do_fixed_add (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
316 const FIXED_VALUE_TYPE
*b
, bool subtract_p
, bool sat_p
)
318 bool overflow_p
= false;
323 /* This was a conditional expression but it triggered a bug in
330 unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
331 i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
333 f
->data
= a
->data
+ temp
;
334 if (unsigned_p
) /* Unsigned type. */
336 if (subtract_p
) /* Unsigned subtraction. */
338 if (a
->data
.ult (b
->data
))
349 else /* Unsigned addition. */
351 f
->data
= f
->data
.zext (i_f_bits
);
352 if (f
->data
.ult (a
->data
)
353 || f
->data
.ult (b
->data
))
365 else /* Signed type. */
368 && (get_fixed_sign_bit (a
->data
, i_f_bits
)
369 == get_fixed_sign_bit (b
->data
, i_f_bits
))
370 && (get_fixed_sign_bit (a
->data
, i_f_bits
)
371 != get_fixed_sign_bit (f
->data
, i_f_bits
)))
373 && (get_fixed_sign_bit (a
->data
, i_f_bits
)
374 != get_fixed_sign_bit (b
->data
, i_f_bits
))
375 && (get_fixed_sign_bit (a
->data
, i_f_bits
)
376 != get_fixed_sign_bit (f
->data
, i_f_bits
))))
382 f
->data
= f
->data
.alshift (i_f_bits
, HOST_BITS_PER_DOUBLE_INT
);
383 if (get_fixed_sign_bit (a
->data
, i_f_bits
) == 0)
392 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
396 /* Calculate F = A * B.
397 If SAT_P, saturate the result to the max or the min.
398 Return true, if !SAT_P and overflow. */
401 do_fixed_multiply (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
402 const FIXED_VALUE_TYPE
*b
, bool sat_p
)
404 bool overflow_p
= false;
405 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
406 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
408 if (GET_MODE_PRECISION (f
->mode
) <= HOST_BITS_PER_WIDE_INT
)
410 f
->data
= a
->data
* b
->data
;
411 f
->data
= f
->data
.lshift (-GET_MODE_FBIT (f
->mode
),
412 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
413 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
417 /* The result of multiplication expands to two double_int. */
418 double_int a_high
, a_low
, b_high
, b_low
;
419 double_int high_high
, high_low
, low_high
, low_low
;
420 double_int r
, s
, temp1
, temp2
;
423 /* Decompose a and b to four double_int. */
424 a_high
.low
= a
->data
.high
;
426 a_low
.low
= a
->data
.low
;
428 b_high
.low
= b
->data
.high
;
430 b_low
.low
= b
->data
.low
;
433 /* Perform four multiplications. */
434 low_low
= a_low
* b_low
;
435 low_high
= a_low
* b_high
;
436 high_low
= a_high
* b_low
;
437 high_high
= a_high
* b_high
;
439 /* Accumulate four results to {r, s}. */
440 temp1
.high
= high_low
.low
;
445 carry
++; /* Carry */
448 temp2
.high
= low_high
.low
;
453 carry
++; /* Carry */
455 temp1
.low
= high_low
.high
;
457 r
= high_high
+ temp1
;
458 temp1
.low
= low_high
.high
;
465 /* We need to subtract b from r, if a < 0. */
466 if (!unsigned_p
&& a
->data
.high
< 0)
468 /* We need to subtract a from r, if b < 0. */
469 if (!unsigned_p
&& b
->data
.high
< 0)
472 /* Shift right the result by FBIT. */
473 if (GET_MODE_FBIT (f
->mode
) == HOST_BITS_PER_DOUBLE_INT
)
488 f
->data
.high
= s
.high
;
492 s
= s
.llshift ((-GET_MODE_FBIT (f
->mode
)), HOST_BITS_PER_DOUBLE_INT
);
493 f
->data
= r
.llshift ((HOST_BITS_PER_DOUBLE_INT
494 - GET_MODE_FBIT (f
->mode
)),
495 HOST_BITS_PER_DOUBLE_INT
);
496 f
->data
.low
= f
->data
.low
| s
.low
;
497 f
->data
.high
= f
->data
.high
| s
.high
;
499 s
.high
= f
->data
.high
;
500 r
= r
.lshift (-GET_MODE_FBIT (f
->mode
),
501 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
504 overflow_p
= fixed_saturate2 (f
->mode
, r
, s
, &f
->data
, sat_p
);
507 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
511 /* Calculate F = A / B.
512 If SAT_P, saturate the result to the max or the min.
513 Return true, if !SAT_P and overflow. */
516 do_fixed_divide (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
517 const FIXED_VALUE_TYPE
*b
, bool sat_p
)
519 bool overflow_p
= false;
520 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
521 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
523 if (GET_MODE_PRECISION (f
->mode
) <= HOST_BITS_PER_WIDE_INT
)
525 f
->data
= a
->data
.lshift (GET_MODE_FBIT (f
->mode
),
526 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
527 f
->data
= f
->data
.div (b
->data
, unsigned_p
, TRUNC_DIV_EXPR
);
528 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
532 double_int pos_a
, pos_b
, r
, s
;
533 double_int quo_r
, quo_s
, mod
, temp
;
537 /* If a < 0, negate a. */
538 if (!unsigned_p
&& a
->data
.high
< 0)
546 /* If b < 0, negate b. */
547 if (!unsigned_p
&& b
->data
.high
< 0)
555 /* Left shift pos_a to {r, s} by FBIT. */
556 if (GET_MODE_FBIT (f
->mode
) == HOST_BITS_PER_DOUBLE_INT
)
564 s
= pos_a
.llshift (GET_MODE_FBIT (f
->mode
), HOST_BITS_PER_DOUBLE_INT
);
565 r
= pos_a
.llshift (- (HOST_BITS_PER_DOUBLE_INT
566 - GET_MODE_FBIT (f
->mode
)),
567 HOST_BITS_PER_DOUBLE_INT
);
570 /* Divide r by pos_b to quo_r. The remainder is in mod. */
571 quo_r
= r
.divmod (pos_b
, 1, TRUNC_DIV_EXPR
, &mod
);
572 quo_s
= double_int_zero
;
574 for (i
= 0; i
< HOST_BITS_PER_DOUBLE_INT
; i
++)
576 /* Record the leftmost bit of mod. */
577 int leftmost_mod
= (mod
.high
< 0);
579 /* Shift left mod by 1 bit. */
580 mod
= mod
.lshift (1);
582 /* Test the leftmost bit of s to add to mod. */
586 /* Shift left quo_s by 1 bit. */
587 quo_s
= quo_s
.lshift (1);
589 /* Try to calculate (mod - pos_b). */
592 if (leftmost_mod
== 1 || mod
.ucmp (pos_b
) != -1)
598 /* Shift left s by 1 bit. */
606 if (quo_s
.high
== 0 && quo_s
.low
== 0)
610 quo_r
.low
= ~quo_r
.low
;
611 quo_r
.high
= ~quo_r
.high
;
616 overflow_p
= fixed_saturate2 (f
->mode
, quo_r
, quo_s
, &f
->data
, sat_p
);
619 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
623 /* Calculate F = A << B if LEFT_P. Otherwise, F = A >> B.
624 If SAT_P, saturate the result to the max or the min.
625 Return true, if !SAT_P and overflow. */
628 do_fixed_shift (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
629 const FIXED_VALUE_TYPE
*b
, bool left_p
, bool sat_p
)
631 bool overflow_p
= false;
632 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
633 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
636 if (b
->data
.low
== 0)
642 if (GET_MODE_PRECISION (f
->mode
) <= HOST_BITS_PER_WIDE_INT
|| (!left_p
))
644 f
->data
= a
->data
.lshift (left_p
? b
->data
.low
: -b
->data
.low
,
645 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
646 if (left_p
) /* Only left shift saturates. */
647 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
649 else /* We need two double_int to store the left-shift result. */
651 double_int temp_high
, temp_low
;
652 if (b
->data
.low
== HOST_BITS_PER_DOUBLE_INT
)
660 temp_low
= a
->data
.lshift (b
->data
.low
,
661 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
662 /* Logical shift right to temp_high. */
663 temp_high
= a
->data
.llshift (b
->data
.low
- HOST_BITS_PER_DOUBLE_INT
,
664 HOST_BITS_PER_DOUBLE_INT
);
666 if (!unsigned_p
&& a
->data
.high
< 0) /* Signed-extend temp_high. */
667 temp_high
= temp_high
.ext (b
->data
.low
, unsigned_p
);
669 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
, &f
->data
,
672 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
677 If SAT_P, saturate the result to the max or the min.
678 Return true, if !SAT_P and overflow. */
681 do_fixed_neg (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
, bool sat_p
)
683 bool overflow_p
= false;
684 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
685 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
688 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
690 if (unsigned_p
) /* Unsigned type. */
692 if (f
->data
.low
!= 0 || f
->data
.high
!= 0)
703 else /* Signed type. */
705 if (!(f
->data
.high
== 0 && f
->data
.low
== 0)
706 && f
->data
.high
== a
->data
.high
&& f
->data
.low
== a
->data
.low
)
710 /* Saturate to the maximum by subtracting f->data by one. */
713 f
->data
= f
->data
.zext (i_f_bits
);
722 /* Perform the binary or unary operation described by CODE.
723 Note that OP0 and OP1 must have the same mode for binary operators.
724 For a unary operation, leave OP1 NULL.
725 Return true, if !SAT_P and overflow. */
728 fixed_arithmetic (FIXED_VALUE_TYPE
*f
, int icode
, const FIXED_VALUE_TYPE
*op0
,
729 const FIXED_VALUE_TYPE
*op1
, bool sat_p
)
734 return do_fixed_neg (f
, op0
, sat_p
);
738 gcc_assert (op0
->mode
== op1
->mode
);
739 return do_fixed_add (f
, op0
, op1
, false, sat_p
);
743 gcc_assert (op0
->mode
== op1
->mode
);
744 return do_fixed_add (f
, op0
, op1
, true, sat_p
);
748 gcc_assert (op0
->mode
== op1
->mode
);
749 return do_fixed_multiply (f
, op0
, op1
, sat_p
);
753 gcc_assert (op0
->mode
== op1
->mode
);
754 return do_fixed_divide (f
, op0
, op1
, sat_p
);
758 return do_fixed_shift (f
, op0
, op1
, true, sat_p
);
762 return do_fixed_shift (f
, op0
, op1
, false, sat_p
);
771 /* Compare fixed-point values by tree_code.
772 Note that OP0 and OP1 must have the same mode. */
775 fixed_compare (int icode
, const FIXED_VALUE_TYPE
*op0
,
776 const FIXED_VALUE_TYPE
*op1
)
778 enum tree_code code
= (enum tree_code
) icode
;
779 gcc_assert (op0
->mode
== op1
->mode
);
784 return op0
->data
!= op1
->data
;
787 return op0
->data
== op1
->data
;
790 return op0
->data
.cmp (op1
->data
,
791 UNSIGNED_FIXED_POINT_MODE_P (op0
->mode
)) == -1;
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;
810 /* Extend or truncate to a new mode.
811 If SAT_P, saturate the result to the max or the min.
812 Return true, if !SAT_P and overflow. */
815 fixed_convert (FIXED_VALUE_TYPE
*f
, machine_mode mode
,
816 const FIXED_VALUE_TYPE
*a
, bool sat_p
)
818 bool overflow_p
= false;
825 if (GET_MODE_FBIT (mode
) > GET_MODE_FBIT (a
->mode
))
827 /* Left shift a to temp_high, temp_low based on a->mode. */
828 double_int temp_high
, temp_low
;
829 int amount
= GET_MODE_FBIT (mode
) - GET_MODE_FBIT (a
->mode
);
830 temp_low
= a
->data
.lshift (amount
,
831 HOST_BITS_PER_DOUBLE_INT
,
832 SIGNED_FIXED_POINT_MODE_P (a
->mode
));
833 /* Logical shift right to temp_high. */
834 temp_high
= a
->data
.llshift (amount
- HOST_BITS_PER_DOUBLE_INT
,
835 HOST_BITS_PER_DOUBLE_INT
);
836 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
)
837 && a
->data
.high
< 0) /* Signed-extend temp_high. */
838 temp_high
= temp_high
.sext (amount
);
841 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
) ==
842 SIGNED_FIXED_POINT_MODE_P (f
->mode
))
843 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
, &f
->data
,
847 /* Take care of the cases when converting between signed and
849 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
))
851 /* Signed -> Unsigned. */
852 if (a
->data
.high
< 0)
856 f
->data
.low
= 0; /* Set to zero. */
857 f
->data
.high
= 0; /* Set to zero. */
863 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
868 /* Unsigned -> Signed. */
869 if (temp_high
.high
< 0)
873 /* Set to maximum. */
874 f
->data
.low
= -1; /* Set to all ones. */
875 f
->data
.high
= -1; /* Set to all ones. */
876 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
877 + GET_MODE_IBIT (f
->mode
));
878 /* Clear the sign. */
884 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
891 /* Right shift a to temp based on a->mode. */
893 temp
= a
->data
.lshift (GET_MODE_FBIT (mode
) - GET_MODE_FBIT (a
->mode
),
894 HOST_BITS_PER_DOUBLE_INT
,
895 SIGNED_FIXED_POINT_MODE_P (a
->mode
));
898 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
) ==
899 SIGNED_FIXED_POINT_MODE_P (f
->mode
))
900 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
903 /* Take care of the cases when converting between signed and
905 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
))
907 /* Signed -> Unsigned. */
908 if (a
->data
.high
< 0)
912 f
->data
.low
= 0; /* Set to zero. */
913 f
->data
.high
= 0; /* Set to zero. */
919 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
,
924 /* Unsigned -> Signed. */
929 /* Set to maximum. */
930 f
->data
.low
= -1; /* Set to all ones. */
931 f
->data
.high
= -1; /* Set to all ones. */
932 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
933 + GET_MODE_IBIT (f
->mode
));
934 /* Clear the sign. */
940 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
,
946 f
->data
= f
->data
.ext (SIGNED_FIXED_POINT_MODE_P (f
->mode
)
947 + GET_MODE_FBIT (f
->mode
)
948 + GET_MODE_IBIT (f
->mode
),
949 UNSIGNED_FIXED_POINT_MODE_P (f
->mode
));
953 /* Convert to a new fixed-point mode from an integer.
954 If UNSIGNED_P, this integer is unsigned.
955 If SAT_P, saturate the result to the max or the min.
956 Return true, if !SAT_P and overflow. */
959 fixed_convert_from_int (FIXED_VALUE_TYPE
*f
, machine_mode mode
,
960 double_int a
, bool unsigned_p
, bool sat_p
)
962 bool overflow_p
= false;
963 /* Left shift a to temp_high, temp_low. */
964 double_int temp_high
, temp_low
;
965 int amount
= GET_MODE_FBIT (mode
);
966 if (amount
== HOST_BITS_PER_DOUBLE_INT
)
974 temp_low
= a
.llshift (amount
, HOST_BITS_PER_DOUBLE_INT
);
976 /* Logical shift right to temp_high. */
977 temp_high
= a
.llshift (amount
- HOST_BITS_PER_DOUBLE_INT
,
978 HOST_BITS_PER_DOUBLE_INT
);
980 if (!unsigned_p
&& a
.high
< 0) /* Signed-extend temp_high. */
981 temp_high
= temp_high
.sext (amount
);
986 if (unsigned_p
== UNSIGNED_FIXED_POINT_MODE_P (f
->mode
))
987 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
, &f
->data
,
991 /* Take care of the cases when converting between signed and unsigned. */
994 /* Signed -> Unsigned. */
999 f
->data
.low
= 0; /* Set to zero. */
1000 f
->data
.high
= 0; /* Set to zero. */
1006 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
1011 /* Unsigned -> Signed. */
1012 if (temp_high
.high
< 0)
1016 /* Set to maximum. */
1017 f
->data
.low
= -1; /* Set to all ones. */
1018 f
->data
.high
= -1; /* Set to all ones. */
1019 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
1020 + GET_MODE_IBIT (f
->mode
));
1021 /* Clear the sign. */
1027 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
1031 f
->data
= f
->data
.ext (SIGNED_FIXED_POINT_MODE_P (f
->mode
)
1032 + GET_MODE_FBIT (f
->mode
)
1033 + GET_MODE_IBIT (f
->mode
),
1034 UNSIGNED_FIXED_POINT_MODE_P (f
->mode
));
1038 /* Convert to a new fixed-point mode from a real.
1039 If SAT_P, saturate the result to the max or the min.
1040 Return true, if !SAT_P and overflow. */
1043 fixed_convert_from_real (FIXED_VALUE_TYPE
*f
, machine_mode mode
,
1044 const REAL_VALUE_TYPE
*a
, bool sat_p
)
1046 bool overflow_p
= false;
1047 REAL_VALUE_TYPE real_value
, fixed_value
, base_value
;
1048 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (mode
);
1049 int i_f_bits
= GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
);
1050 unsigned int fbit
= GET_MODE_FBIT (mode
);
1051 enum fixed_value_range_code temp
;
1056 real_2expN (&base_value
, fbit
, mode
);
1057 real_arithmetic (&fixed_value
, MULT_EXPR
, &real_value
, &base_value
);
1059 wide_int w
= real_to_integer (&fixed_value
, &fail
,
1060 GET_MODE_PRECISION (mode
));
1061 f
->data
.low
= w
.elt (0);
1062 f
->data
.high
= w
.elt (1);
1063 temp
= check_real_for_fixed_mode (&real_value
, mode
);
1064 if (temp
== FIXED_UNDERFLOW
) /* Minimum. */
1077 f
->data
= f
->data
.alshift (i_f_bits
, HOST_BITS_PER_DOUBLE_INT
);
1078 f
->data
= f
->data
.sext (1 + i_f_bits
);
1084 else if (temp
== FIXED_GT_MAX_EPS
|| temp
== FIXED_MAX_EPS
) /* Maximum. */
1090 f
->data
= f
->data
.zext (i_f_bits
);
1095 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
1099 /* Convert to a new real mode from a fixed-point. */
1102 real_convert_from_fixed (REAL_VALUE_TYPE
*r
, machine_mode mode
,
1103 const FIXED_VALUE_TYPE
*f
)
1105 REAL_VALUE_TYPE base_value
, fixed_value
, real_value
;
1107 signop sgn
= UNSIGNED_FIXED_POINT_MODE_P (f
->mode
) ? UNSIGNED
: SIGNED
;
1108 real_2expN (&base_value
, GET_MODE_FBIT (f
->mode
), f
->mode
);
1109 real_from_integer (&fixed_value
, VOIDmode
,
1110 wide_int::from (f
->data
, GET_MODE_PRECISION (f
->mode
),
1112 real_arithmetic (&real_value
, RDIV_EXPR
, &fixed_value
, &base_value
);
1113 real_convert (r
, mode
, &real_value
);
1116 /* Determine whether a fixed-point value F is negative. */
1119 fixed_isneg (const FIXED_VALUE_TYPE
*f
)
1121 if (SIGNED_FIXED_POINT_MODE_P (f
->mode
))
1123 int i_f_bits
= GET_MODE_IBIT (f
->mode
) + GET_MODE_FBIT (f
->mode
);
1124 int sign_bit
= get_fixed_sign_bit (f
->data
, i_f_bits
);