1 /* Fixed-point arithmetic support.
2 Copyright (C) 2006, 2007, 2008, 2009, 2010 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
, enum machine_mode mode
)
64 REAL_VALUE_TYPE max_value
, min_value
, epsilon_value
;
66 real_2expN (&max_value
, GET_MODE_IBIT (mode
), mode
);
67 real_2expN (&epsilon_value
, -GET_MODE_FBIT (mode
), mode
);
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
;
84 /* Initialize from a decimal or hexadecimal string. */
87 fixed_from_string (FIXED_VALUE_TYPE
*f
, const char *str
, enum machine_mode mode
)
89 REAL_VALUE_TYPE real_value
, fixed_value
, base_value
;
91 enum fixed_value_range_code temp
;
94 fbit
= GET_MODE_FBIT (mode
);
96 real_from_string (&real_value
, str
);
97 temp
= check_real_for_fixed_mode (&real_value
, f
->mode
);
98 /* We don't want to warn the case when the _Fract value is 1.0. */
99 if (temp
== FIXED_UNDERFLOW
100 || temp
== FIXED_GT_MAX_EPS
101 || (temp
== FIXED_MAX_EPS
&& ALL_ACCUM_MODE_P (f
->mode
)))
102 warning (OPT_Woverflow
,
103 "large fixed-point constant implicitly truncated to fixed-point type");
104 real_2expN (&base_value
, fbit
, mode
);
105 real_arithmetic (&fixed_value
, MULT_EXPR
, &real_value
, &base_value
);
106 real_to_integer2 ((HOST_WIDE_INT
*)&f
->data
.low
, &f
->data
.high
,
109 if (temp
== FIXED_MAX_EPS
&& ALL_FRACT_MODE_P (f
->mode
))
111 /* From the spec, we need to evaluate 1 to the maximal value. */
114 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
115 + GET_MODE_IBIT (f
->mode
));
118 f
->data
= f
->data
.ext (SIGNED_FIXED_POINT_MODE_P (f
->mode
)
119 + GET_MODE_FBIT (f
->mode
)
120 + GET_MODE_IBIT (f
->mode
),
121 UNSIGNED_FIXED_POINT_MODE_P (f
->mode
));
124 /* Render F as a decimal floating point constant. */
127 fixed_to_decimal (char *str
, const FIXED_VALUE_TYPE
*f_orig
,
130 REAL_VALUE_TYPE real_value
, base_value
, fixed_value
;
132 real_2expN (&base_value
, GET_MODE_FBIT (f_orig
->mode
), f_orig
->mode
);
133 real_from_integer (&real_value
, VOIDmode
, f_orig
->data
.low
, f_orig
->data
.high
,
134 UNSIGNED_FIXED_POINT_MODE_P (f_orig
->mode
));
135 real_arithmetic (&fixed_value
, RDIV_EXPR
, &real_value
, &base_value
);
136 real_to_decimal (str
, &fixed_value
, buf_size
, 0, 1);
139 /* If SAT_P, saturate A to the maximum or the minimum, and save to *F based on
140 the machine mode MODE.
141 Do not modify *F otherwise.
142 This function assumes the width of double_int is greater than the width
143 of the fixed-point value (the sum of a possible sign bit, possible ibits,
145 Return true, if !SAT_P and overflow. */
148 fixed_saturate1 (enum machine_mode mode
, double_int a
, double_int
*f
,
151 bool overflow_p
= false;
152 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (mode
);
153 int i_f_bits
= GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
);
155 if (unsigned_p
) /* Unsigned type. */
160 max
= max
.zext (i_f_bits
);
169 else /* Signed type. */
174 max
= max
.zext (i_f_bits
);
177 min
= min
.alshift (i_f_bits
, HOST_BITS_PER_DOUBLE_INT
);
178 min
= min
.sext (1 + i_f_bits
);
186 else if (a
.slt (min
))
197 /* If SAT_P, saturate {A_HIGH, A_LOW} to the maximum or the minimum, and
198 save to *F based on the machine mode MODE.
199 Do not modify *F otherwise.
200 This function assumes the width of two double_int is greater than the width
201 of the fixed-point value (the sum of a possible sign bit, possible ibits,
203 Return true, if !SAT_P and overflow. */
206 fixed_saturate2 (enum machine_mode mode
, double_int a_high
, double_int a_low
,
207 double_int
*f
, bool sat_p
)
209 bool overflow_p
= false;
210 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (mode
);
211 int i_f_bits
= GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
);
213 if (unsigned_p
) /* Unsigned type. */
215 double_int max_r
, max_s
;
220 max_s
= max_s
.zext (i_f_bits
);
221 if (a_high
.ugt (max_r
)
222 || (a_high
== max_r
&&
231 else /* Signed type. */
233 double_int max_r
, max_s
, min_r
, min_s
;
238 max_s
= max_s
.zext (i_f_bits
);
243 min_s
= min_s
.alshift (i_f_bits
, HOST_BITS_PER_DOUBLE_INT
);
244 min_s
= min_s
.sext (1 + i_f_bits
);
245 if (a_high
.sgt (max_r
)
246 || (a_high
== max_r
&&
254 else if (a_high
.slt (min_r
)
255 || (a_high
== min_r
&&
267 /* Return the sign bit based on I_F_BITS. */
270 get_fixed_sign_bit (double_int a
, int i_f_bits
)
272 if (i_f_bits
< HOST_BITS_PER_WIDE_INT
)
273 return (a
.low
>> i_f_bits
) & 1;
275 return (a
.high
>> (i_f_bits
- HOST_BITS_PER_WIDE_INT
)) & 1;
278 /* Calculate F = A + (SUBTRACT_P ? -B : B).
279 If SAT_P, saturate the result to the max or the min.
280 Return true, if !SAT_P and overflow. */
283 do_fixed_add (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
284 const FIXED_VALUE_TYPE
*b
, bool subtract_p
, bool sat_p
)
286 bool overflow_p
= false;
291 /* This was a conditional expression but it triggered a bug in
298 unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
299 i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
301 f
->data
= a
->data
+ temp
;
302 if (unsigned_p
) /* Unsigned type. */
304 if (subtract_p
) /* Unsigned subtraction. */
306 if (a
->data
.ult (b
->data
))
317 else /* Unsigned addition. */
319 f
->data
= f
->data
.zext (i_f_bits
);
320 if (f
->data
.ult (a
->data
)
321 || f
->data
.ult (b
->data
))
333 else /* Signed type. */
336 && (get_fixed_sign_bit (a
->data
, i_f_bits
)
337 == get_fixed_sign_bit (b
->data
, i_f_bits
))
338 && (get_fixed_sign_bit (a
->data
, i_f_bits
)
339 != get_fixed_sign_bit (f
->data
, i_f_bits
)))
341 && (get_fixed_sign_bit (a
->data
, i_f_bits
)
342 != get_fixed_sign_bit (b
->data
, i_f_bits
))
343 && (get_fixed_sign_bit (a
->data
, i_f_bits
)
344 != get_fixed_sign_bit (f
->data
, i_f_bits
))))
350 f
->data
= f
->data
.alshift (i_f_bits
, HOST_BITS_PER_DOUBLE_INT
);
351 if (get_fixed_sign_bit (a
->data
, i_f_bits
) == 0)
360 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
364 /* Calculate F = A * B.
365 If SAT_P, saturate the result to the max or the min.
366 Return true, if !SAT_P and overflow. */
369 do_fixed_multiply (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
370 const FIXED_VALUE_TYPE
*b
, bool sat_p
)
372 bool overflow_p
= false;
373 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
374 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
376 if (GET_MODE_PRECISION (f
->mode
) <= HOST_BITS_PER_WIDE_INT
)
378 f
->data
= a
->data
* b
->data
;
379 f
->data
= f
->data
.lshift (-GET_MODE_FBIT (f
->mode
),
380 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
381 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
385 /* The result of multiplication expands to two double_int. */
386 double_int a_high
, a_low
, b_high
, b_low
;
387 double_int high_high
, high_low
, low_high
, low_low
;
388 double_int r
, s
, temp1
, temp2
;
391 /* Decompose a and b to four double_int. */
392 a_high
.low
= a
->data
.high
;
394 a_low
.low
= a
->data
.low
;
396 b_high
.low
= b
->data
.high
;
398 b_low
.low
= b
->data
.low
;
401 /* Perform four multiplications. */
402 low_low
= a_low
* b_low
;
403 low_high
= a_low
* b_high
;
404 high_low
= a_high
* b_low
;
405 high_high
= a_high
* b_high
;
407 /* Accumulate four results to {r, s}. */
408 temp1
.high
= high_low
.low
;
413 carry
++; /* Carry */
416 temp2
.high
= low_high
.low
;
421 carry
++; /* Carry */
423 temp1
.low
= high_low
.high
;
425 r
= high_high
+ temp1
;
426 temp1
.low
= low_high
.high
;
433 /* We need to subtract b from r, if a < 0. */
434 if (!unsigned_p
&& a
->data
.high
< 0)
436 /* We need to subtract a from r, if b < 0. */
437 if (!unsigned_p
&& b
->data
.high
< 0)
440 /* Shift right the result by FBIT. */
441 if (GET_MODE_FBIT (f
->mode
) == HOST_BITS_PER_DOUBLE_INT
)
456 f
->data
.high
= s
.high
;
460 s
= s
.llshift ((-GET_MODE_FBIT (f
->mode
)), HOST_BITS_PER_DOUBLE_INT
);
461 f
->data
= r
.llshift ((HOST_BITS_PER_DOUBLE_INT
462 - GET_MODE_FBIT (f
->mode
)),
463 HOST_BITS_PER_DOUBLE_INT
);
464 f
->data
.low
= f
->data
.low
| s
.low
;
465 f
->data
.high
= f
->data
.high
| s
.high
;
467 s
.high
= f
->data
.high
;
468 r
= r
.lshift (-GET_MODE_FBIT (f
->mode
),
469 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
472 overflow_p
= fixed_saturate2 (f
->mode
, r
, s
, &f
->data
, sat_p
);
475 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
479 /* Calculate F = A / B.
480 If SAT_P, saturate the result to the max or the min.
481 Return true, if !SAT_P and overflow. */
484 do_fixed_divide (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
485 const FIXED_VALUE_TYPE
*b
, bool sat_p
)
487 bool overflow_p
= false;
488 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
489 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
491 if (GET_MODE_PRECISION (f
->mode
) <= HOST_BITS_PER_WIDE_INT
)
493 f
->data
= a
->data
.lshift (GET_MODE_FBIT (f
->mode
),
494 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
495 f
->data
= f
->data
.div (b
->data
, unsigned_p
, TRUNC_DIV_EXPR
);
496 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
500 double_int pos_a
, pos_b
, r
, s
;
501 double_int quo_r
, quo_s
, mod
, temp
;
505 /* If a < 0, negate a. */
506 if (!unsigned_p
&& a
->data
.high
< 0)
514 /* If b < 0, negate b. */
515 if (!unsigned_p
&& b
->data
.high
< 0)
523 /* Left shift pos_a to {r, s} by FBIT. */
524 if (GET_MODE_FBIT (f
->mode
) == HOST_BITS_PER_DOUBLE_INT
)
532 s
= pos_a
.llshift (GET_MODE_FBIT (f
->mode
), HOST_BITS_PER_DOUBLE_INT
);
533 r
= pos_a
.llshift (- (HOST_BITS_PER_DOUBLE_INT
534 - GET_MODE_FBIT (f
->mode
)),
535 HOST_BITS_PER_DOUBLE_INT
);
538 /* Divide r by pos_b to quo_r. The remainder is in mod. */
539 quo_r
= r
.divmod (pos_b
, 1, TRUNC_DIV_EXPR
, &mod
);
540 quo_s
= double_int_zero
;
542 for (i
= 0; i
< HOST_BITS_PER_DOUBLE_INT
; i
++)
544 /* Record the leftmost bit of mod. */
545 int leftmost_mod
= (mod
.high
< 0);
547 /* Shift left mod by 1 bit. */
548 mod
= mod
.llshift (1, HOST_BITS_PER_DOUBLE_INT
);
550 /* Test the leftmost bit of s to add to mod. */
554 /* Shift left quo_s by 1 bit. */
555 quo_s
= quo_s
.llshift (1, HOST_BITS_PER_DOUBLE_INT
);
557 /* Try to calculate (mod - pos_b). */
560 if (leftmost_mod
== 1 || mod
.ucmp (pos_b
) != -1)
566 /* Shift left s by 1 bit. */
567 s
= s
.llshift (1, HOST_BITS_PER_DOUBLE_INT
);
574 if (quo_s
.high
== 0 && quo_s
.low
== 0)
578 quo_r
.low
= ~quo_r
.low
;
579 quo_r
.high
= ~quo_r
.high
;
584 overflow_p
= fixed_saturate2 (f
->mode
, quo_r
, quo_s
, &f
->data
, sat_p
);
587 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
591 /* Calculate F = A << B if LEFT_P. Otherwise, F = A >> B.
592 If SAT_P, saturate the result to the max or the min.
593 Return true, if !SAT_P and overflow. */
596 do_fixed_shift (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
597 const FIXED_VALUE_TYPE
*b
, bool left_p
, bool sat_p
)
599 bool overflow_p
= false;
600 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
601 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
604 if (b
->data
.low
== 0)
610 if (GET_MODE_PRECISION (f
->mode
) <= HOST_BITS_PER_WIDE_INT
|| (!left_p
))
612 f
->data
= a
->data
.lshift (left_p
? b
->data
.low
: -b
->data
.low
,
613 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
614 if (left_p
) /* Only left shift saturates. */
615 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
617 else /* We need two double_int to store the left-shift result. */
619 double_int temp_high
, temp_low
;
620 if (b
->data
.low
== HOST_BITS_PER_DOUBLE_INT
)
628 temp_low
= a
->data
.lshift (b
->data
.low
,
629 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
630 /* Logical shift right to temp_high. */
631 temp_high
= a
->data
.llshift (b
->data
.low
- HOST_BITS_PER_DOUBLE_INT
,
632 HOST_BITS_PER_DOUBLE_INT
);
634 if (!unsigned_p
&& a
->data
.high
< 0) /* Signed-extend temp_high. */
635 temp_high
= temp_high
.ext (b
->data
.low
, unsigned_p
);
637 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
, &f
->data
,
640 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
645 If SAT_P, saturate the result to the max or the min.
646 Return true, if !SAT_P and overflow. */
649 do_fixed_neg (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
, bool sat_p
)
651 bool overflow_p
= false;
652 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
653 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
656 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
658 if (unsigned_p
) /* Unsigned type. */
660 if (f
->data
.low
!= 0 || f
->data
.high
!= 0)
671 else /* Signed type. */
673 if (!(f
->data
.high
== 0 && f
->data
.low
== 0)
674 && f
->data
.high
== a
->data
.high
&& f
->data
.low
== a
->data
.low
)
678 /* Saturate to the maximum by subtracting f->data by one. */
681 f
->data
= f
->data
.zext (i_f_bits
);
690 /* Perform the binary or unary operation described by CODE.
691 Note that OP0 and OP1 must have the same mode for binary operators.
692 For a unary operation, leave OP1 NULL.
693 Return true, if !SAT_P and overflow. */
696 fixed_arithmetic (FIXED_VALUE_TYPE
*f
, int icode
, const FIXED_VALUE_TYPE
*op0
,
697 const FIXED_VALUE_TYPE
*op1
, bool sat_p
)
702 return do_fixed_neg (f
, op0
, sat_p
);
706 gcc_assert (op0
->mode
== op1
->mode
);
707 return do_fixed_add (f
, op0
, op1
, false, sat_p
);
711 gcc_assert (op0
->mode
== op1
->mode
);
712 return do_fixed_add (f
, op0
, op1
, true, sat_p
);
716 gcc_assert (op0
->mode
== op1
->mode
);
717 return do_fixed_multiply (f
, op0
, op1
, sat_p
);
721 gcc_assert (op0
->mode
== op1
->mode
);
722 return do_fixed_divide (f
, op0
, op1
, sat_p
);
726 return do_fixed_shift (f
, op0
, op1
, true, sat_p
);
730 return do_fixed_shift (f
, op0
, op1
, false, sat_p
);
739 /* Compare fixed-point values by tree_code.
740 Note that OP0 and OP1 must have the same mode. */
743 fixed_compare (int icode
, const FIXED_VALUE_TYPE
*op0
,
744 const FIXED_VALUE_TYPE
*op1
)
746 enum tree_code code
= (enum tree_code
) icode
;
747 gcc_assert (op0
->mode
== op1
->mode
);
752 return op0
->data
!= op1
->data
;
755 return op0
->data
== op1
->data
;
758 return op0
->data
.cmp (op1
->data
,
759 UNSIGNED_FIXED_POINT_MODE_P (op0
->mode
)) == -1;
762 return op0
->data
.cmp (op1
->data
,
763 UNSIGNED_FIXED_POINT_MODE_P (op0
->mode
)) != 1;
766 return op0
->data
.cmp (op1
->data
,
767 UNSIGNED_FIXED_POINT_MODE_P (op0
->mode
)) == 1;
770 return op0
->data
.cmp (op1
->data
,
771 UNSIGNED_FIXED_POINT_MODE_P (op0
->mode
)) != -1;
778 /* Extend or truncate to a new mode.
779 If SAT_P, saturate the result to the max or the min.
780 Return true, if !SAT_P and overflow. */
783 fixed_convert (FIXED_VALUE_TYPE
*f
, enum machine_mode mode
,
784 const FIXED_VALUE_TYPE
*a
, bool sat_p
)
786 bool overflow_p
= false;
793 if (GET_MODE_FBIT (mode
) > GET_MODE_FBIT (a
->mode
))
795 /* Left shift a to temp_high, temp_low based on a->mode. */
796 double_int temp_high
, temp_low
;
797 int amount
= GET_MODE_FBIT (mode
) - GET_MODE_FBIT (a
->mode
);
798 temp_low
= a
->data
.lshift (amount
,
799 HOST_BITS_PER_DOUBLE_INT
,
800 SIGNED_FIXED_POINT_MODE_P (a
->mode
));
801 /* Logical shift right to temp_high. */
802 temp_high
= a
->data
.llshift (amount
- HOST_BITS_PER_DOUBLE_INT
,
803 HOST_BITS_PER_DOUBLE_INT
);
804 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
)
805 && a
->data
.high
< 0) /* Signed-extend temp_high. */
806 temp_high
= temp_high
.sext (amount
);
809 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
) ==
810 SIGNED_FIXED_POINT_MODE_P (f
->mode
))
811 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
, &f
->data
,
815 /* Take care of the cases when converting between signed and
817 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
))
819 /* Signed -> Unsigned. */
820 if (a
->data
.high
< 0)
824 f
->data
.low
= 0; /* Set to zero. */
825 f
->data
.high
= 0; /* Set to zero. */
831 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
836 /* Unsigned -> Signed. */
837 if (temp_high
.high
< 0)
841 /* Set to maximum. */
842 f
->data
.low
= -1; /* Set to all ones. */
843 f
->data
.high
= -1; /* Set to all ones. */
844 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
845 + GET_MODE_IBIT (f
->mode
));
846 /* Clear the sign. */
852 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
859 /* Right shift a to temp based on a->mode. */
861 temp
= a
->data
.lshift (GET_MODE_FBIT (mode
) - GET_MODE_FBIT (a
->mode
),
862 HOST_BITS_PER_DOUBLE_INT
,
863 SIGNED_FIXED_POINT_MODE_P (a
->mode
));
866 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
) ==
867 SIGNED_FIXED_POINT_MODE_P (f
->mode
))
868 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
871 /* Take care of the cases when converting between signed and
873 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
))
875 /* Signed -> Unsigned. */
876 if (a
->data
.high
< 0)
880 f
->data
.low
= 0; /* Set to zero. */
881 f
->data
.high
= 0; /* Set to zero. */
887 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
,
892 /* Unsigned -> Signed. */
897 /* Set to maximum. */
898 f
->data
.low
= -1; /* Set to all ones. */
899 f
->data
.high
= -1; /* Set to all ones. */
900 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
901 + GET_MODE_IBIT (f
->mode
));
902 /* Clear the sign. */
908 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
,
914 f
->data
= f
->data
.ext (SIGNED_FIXED_POINT_MODE_P (f
->mode
)
915 + GET_MODE_FBIT (f
->mode
)
916 + GET_MODE_IBIT (f
->mode
),
917 UNSIGNED_FIXED_POINT_MODE_P (f
->mode
));
921 /* Convert to a new fixed-point mode from an integer.
922 If UNSIGNED_P, this integer is unsigned.
923 If SAT_P, saturate the result to the max or the min.
924 Return true, if !SAT_P and overflow. */
927 fixed_convert_from_int (FIXED_VALUE_TYPE
*f
, enum machine_mode mode
,
928 double_int a
, bool unsigned_p
, bool sat_p
)
930 bool overflow_p
= false;
931 /* Left shift a to temp_high, temp_low. */
932 double_int temp_high
, temp_low
;
933 int amount
= GET_MODE_FBIT (mode
);
934 if (amount
== HOST_BITS_PER_DOUBLE_INT
)
942 temp_low
= a
.llshift (amount
, HOST_BITS_PER_DOUBLE_INT
);
944 /* Logical shift right to temp_high. */
945 temp_high
= a
.llshift (amount
- HOST_BITS_PER_DOUBLE_INT
,
946 HOST_BITS_PER_DOUBLE_INT
);
948 if (!unsigned_p
&& a
.high
< 0) /* Signed-extend temp_high. */
949 temp_high
= temp_high
.sext (amount
);
954 if (unsigned_p
== UNSIGNED_FIXED_POINT_MODE_P (f
->mode
))
955 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
, &f
->data
,
959 /* Take care of the cases when converting between signed and unsigned. */
962 /* Signed -> Unsigned. */
967 f
->data
.low
= 0; /* Set to zero. */
968 f
->data
.high
= 0; /* Set to zero. */
974 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
979 /* Unsigned -> Signed. */
980 if (temp_high
.high
< 0)
984 /* Set to maximum. */
985 f
->data
.low
= -1; /* Set to all ones. */
986 f
->data
.high
= -1; /* Set to all ones. */
987 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
988 + GET_MODE_IBIT (f
->mode
));
989 /* Clear the sign. */
995 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
999 f
->data
= f
->data
.ext (SIGNED_FIXED_POINT_MODE_P (f
->mode
)
1000 + GET_MODE_FBIT (f
->mode
)
1001 + GET_MODE_IBIT (f
->mode
),
1002 UNSIGNED_FIXED_POINT_MODE_P (f
->mode
));
1006 /* Convert to a new fixed-point mode from a real.
1007 If SAT_P, saturate the result to the max or the min.
1008 Return true, if !SAT_P and overflow. */
1011 fixed_convert_from_real (FIXED_VALUE_TYPE
*f
, enum machine_mode mode
,
1012 const REAL_VALUE_TYPE
*a
, bool sat_p
)
1014 bool overflow_p
= false;
1015 REAL_VALUE_TYPE real_value
, fixed_value
, base_value
;
1016 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (mode
);
1017 int i_f_bits
= GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
);
1018 unsigned int fbit
= GET_MODE_FBIT (mode
);
1019 enum fixed_value_range_code temp
;
1023 real_2expN (&base_value
, fbit
, mode
);
1024 real_arithmetic (&fixed_value
, MULT_EXPR
, &real_value
, &base_value
);
1025 real_to_integer2 ((HOST_WIDE_INT
*)&f
->data
.low
, &f
->data
.high
, &fixed_value
);
1026 temp
= check_real_for_fixed_mode (&real_value
, mode
);
1027 if (temp
== FIXED_UNDERFLOW
) /* Minimum. */
1040 f
->data
= f
->data
.alshift (i_f_bits
, HOST_BITS_PER_DOUBLE_INT
);
1041 f
->data
= f
->data
.sext (1 + i_f_bits
);
1047 else if (temp
== FIXED_GT_MAX_EPS
|| temp
== FIXED_MAX_EPS
) /* Maximum. */
1053 f
->data
= f
->data
.zext (i_f_bits
);
1058 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
1062 /* Convert to a new real mode from a fixed-point. */
1065 real_convert_from_fixed (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
1066 const FIXED_VALUE_TYPE
*f
)
1068 REAL_VALUE_TYPE base_value
, fixed_value
, real_value
;
1070 real_2expN (&base_value
, GET_MODE_FBIT (f
->mode
), f
->mode
);
1071 real_from_integer (&fixed_value
, VOIDmode
, f
->data
.low
, f
->data
.high
,
1072 UNSIGNED_FIXED_POINT_MODE_P (f
->mode
));
1073 real_arithmetic (&real_value
, RDIV_EXPR
, &fixed_value
, &base_value
);
1074 real_convert (r
, mode
, &real_value
);
1077 /* Determine whether a fixed-point value F is negative. */
1080 fixed_isneg (const FIXED_VALUE_TYPE
*f
)
1082 if (SIGNED_FIXED_POINT_MODE_P (f
->mode
))
1084 int i_f_bits
= GET_MODE_IBIT (f
->mode
) + GET_MODE_FBIT (f
->mode
);
1085 int sign_bit
= get_fixed_sign_bit (f
->data
, i_f_bits
);