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
,
382 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
386 /* The result of multiplication expands to two double_int. */
387 double_int a_high
, a_low
, b_high
, b_low
;
388 double_int high_high
, high_low
, low_high
, low_low
;
389 double_int r
, s
, temp1
, temp2
;
392 /* Decompose a and b to four double_int. */
393 a_high
.low
= a
->data
.high
;
395 a_low
.low
= a
->data
.low
;
397 b_high
.low
= b
->data
.high
;
399 b_low
.low
= b
->data
.low
;
402 /* Perform four multiplications. */
403 low_low
= a_low
* b_low
;
404 low_high
= a_low
* b_high
;
405 high_low
= a_high
* b_low
;
406 high_high
= a_high
* b_high
;
408 /* Accumulate four results to {r, s}. */
409 temp1
.high
= high_low
.low
;
414 carry
++; /* Carry */
417 temp2
.high
= low_high
.low
;
422 carry
++; /* Carry */
424 temp1
.low
= high_low
.high
;
426 r
= high_high
+ temp1
;
427 temp1
.low
= low_high
.high
;
434 /* We need to subtract b from r, if a < 0. */
435 if (!unsigned_p
&& a
->data
.high
< 0)
437 /* We need to subtract a from r, if b < 0. */
438 if (!unsigned_p
&& b
->data
.high
< 0)
441 /* Shift right the result by FBIT. */
442 if (GET_MODE_FBIT (f
->mode
) == HOST_BITS_PER_DOUBLE_INT
)
457 f
->data
.high
= s
.high
;
461 s
= s
.llshift ((-GET_MODE_FBIT (f
->mode
)), HOST_BITS_PER_DOUBLE_INT
);
462 f
->data
= r
.llshift ((HOST_BITS_PER_DOUBLE_INT
463 - GET_MODE_FBIT (f
->mode
)),
464 HOST_BITS_PER_DOUBLE_INT
);
465 f
->data
.low
= f
->data
.low
| s
.low
;
466 f
->data
.high
= f
->data
.high
| s
.high
;
468 s
.high
= f
->data
.high
;
469 r
= r
.lshift ((-GET_MODE_FBIT (f
->mode
)),
470 HOST_BITS_PER_DOUBLE_INT
,
474 overflow_p
= fixed_saturate2 (f
->mode
, r
, s
, &f
->data
, sat_p
);
477 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
481 /* Calculate F = A / B.
482 If SAT_P, saturate the result to the max or the min.
483 Return true, if !SAT_P and overflow. */
486 do_fixed_divide (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
487 const FIXED_VALUE_TYPE
*b
, bool sat_p
)
489 bool overflow_p
= false;
490 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
491 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
493 if (GET_MODE_PRECISION (f
->mode
) <= HOST_BITS_PER_WIDE_INT
)
495 f
->data
= a
->data
.lshift (GET_MODE_FBIT (f
->mode
),
496 HOST_BITS_PER_DOUBLE_INT
,
498 f
->data
= f
->data
.div (b
->data
, unsigned_p
, TRUNC_DIV_EXPR
);
499 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
503 double_int pos_a
, pos_b
, r
, s
;
504 double_int quo_r
, quo_s
, mod
, temp
;
508 /* If a < 0, negate a. */
509 if (!unsigned_p
&& a
->data
.high
< 0)
517 /* If b < 0, negate b. */
518 if (!unsigned_p
&& b
->data
.high
< 0)
526 /* Left shift pos_a to {r, s} by FBIT. */
527 if (GET_MODE_FBIT (f
->mode
) == HOST_BITS_PER_DOUBLE_INT
)
535 s
= pos_a
.llshift (GET_MODE_FBIT (f
->mode
), HOST_BITS_PER_DOUBLE_INT
);
536 r
= pos_a
.llshift (- (HOST_BITS_PER_DOUBLE_INT
537 - GET_MODE_FBIT (f
->mode
)),
538 HOST_BITS_PER_DOUBLE_INT
);
541 /* Divide r by pos_b to quo_r. The remainder is in mod. */
542 quo_r
= r
.divmod (pos_b
, 1, TRUNC_DIV_EXPR
, &mod
);
543 quo_s
= double_int_zero
;
545 for (i
= 0; i
< HOST_BITS_PER_DOUBLE_INT
; i
++)
547 /* Record the leftmost bit of mod. */
548 int leftmost_mod
= (mod
.high
< 0);
550 /* Shift left mod by 1 bit. */
551 mod
= mod
.llshift (1, HOST_BITS_PER_DOUBLE_INT
);
553 /* Test the leftmost bit of s to add to mod. */
557 /* Shift left quo_s by 1 bit. */
558 quo_s
= quo_s
.llshift (1, HOST_BITS_PER_DOUBLE_INT
);
560 /* Try to calculate (mod - pos_b). */
563 if (leftmost_mod
== 1 || mod
.ucmp (pos_b
) != -1)
569 /* Shift left s by 1 bit. */
570 s
= s
.llshift (1, HOST_BITS_PER_DOUBLE_INT
);
577 if (quo_s
.high
== 0 && quo_s
.low
== 0)
581 quo_r
.low
= ~quo_r
.low
;
582 quo_r
.high
= ~quo_r
.high
;
587 overflow_p
= fixed_saturate2 (f
->mode
, quo_r
, quo_s
, &f
->data
, sat_p
);
590 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
594 /* Calculate F = A << B if LEFT_P. Otherwise, F = A >> B.
595 If SAT_P, saturate the result to the max or the min.
596 Return true, if !SAT_P and overflow. */
599 do_fixed_shift (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
600 const FIXED_VALUE_TYPE
*b
, bool left_p
, bool sat_p
)
602 bool overflow_p
= false;
603 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
604 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
607 if (b
->data
.low
== 0)
613 if (GET_MODE_PRECISION (f
->mode
) <= HOST_BITS_PER_WIDE_INT
|| (!left_p
))
615 f
->data
= a
->data
.lshift (left_p
? b
->data
.low
: (-b
->data
.low
),
616 HOST_BITS_PER_DOUBLE_INT
,
618 if (left_p
) /* Only left shift saturates. */
619 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
621 else /* We need two double_int to store the left-shift result. */
623 double_int temp_high
, temp_low
;
624 if (b
->data
.low
== HOST_BITS_PER_DOUBLE_INT
)
632 temp_low
= a
->data
.lshift (b
->data
.low
,
633 HOST_BITS_PER_DOUBLE_INT
,
635 /* Logical shift right to temp_high. */
636 temp_high
= a
->data
.llshift (b
->data
.low
- HOST_BITS_PER_DOUBLE_INT
,
637 HOST_BITS_PER_DOUBLE_INT
);
639 if (!unsigned_p
&& a
->data
.high
< 0) /* Signed-extend temp_high. */
640 temp_high
= temp_high
.ext (b
->data
.low
, unsigned_p
);
642 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
, &f
->data
,
645 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
650 If SAT_P, saturate the result to the max or the min.
651 Return true, if !SAT_P and overflow. */
654 do_fixed_neg (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
, bool sat_p
)
656 bool overflow_p
= false;
657 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
658 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
661 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
663 if (unsigned_p
) /* Unsigned type. */
665 if (f
->data
.low
!= 0 || f
->data
.high
!= 0)
676 else /* Signed type. */
678 if (!(f
->data
.high
== 0 && f
->data
.low
== 0)
679 && f
->data
.high
== a
->data
.high
&& f
->data
.low
== a
->data
.low
)
683 /* Saturate to the maximum by subtracting f->data by one. */
686 f
->data
= f
->data
.zext (i_f_bits
);
695 /* Perform the binary or unary operation described by CODE.
696 Note that OP0 and OP1 must have the same mode for binary operators.
697 For a unary operation, leave OP1 NULL.
698 Return true, if !SAT_P and overflow. */
701 fixed_arithmetic (FIXED_VALUE_TYPE
*f
, int icode
, const FIXED_VALUE_TYPE
*op0
,
702 const FIXED_VALUE_TYPE
*op1
, bool sat_p
)
707 return do_fixed_neg (f
, op0
, sat_p
);
711 gcc_assert (op0
->mode
== op1
->mode
);
712 return do_fixed_add (f
, op0
, op1
, false, sat_p
);
716 gcc_assert (op0
->mode
== op1
->mode
);
717 return do_fixed_add (f
, op0
, op1
, true, sat_p
);
721 gcc_assert (op0
->mode
== op1
->mode
);
722 return do_fixed_multiply (f
, op0
, op1
, sat_p
);
726 gcc_assert (op0
->mode
== op1
->mode
);
727 return do_fixed_divide (f
, op0
, op1
, sat_p
);
731 return do_fixed_shift (f
, op0
, op1
, true, sat_p
);
735 return do_fixed_shift (f
, op0
, op1
, false, sat_p
);
744 /* Compare fixed-point values by tree_code.
745 Note that OP0 and OP1 must have the same mode. */
748 fixed_compare (int icode
, const FIXED_VALUE_TYPE
*op0
,
749 const FIXED_VALUE_TYPE
*op1
)
751 enum tree_code code
= (enum tree_code
) icode
;
752 gcc_assert (op0
->mode
== op1
->mode
);
757 return op0
->data
!= op1
->data
;
760 return op0
->data
== op1
->data
;
763 return op0
->data
.cmp (op1
->data
,
764 UNSIGNED_FIXED_POINT_MODE_P (op0
->mode
)) == -1;
767 return op0
->data
.cmp (op1
->data
,
768 UNSIGNED_FIXED_POINT_MODE_P (op0
->mode
)) != 1;
771 return op0
->data
.cmp (op1
->data
,
772 UNSIGNED_FIXED_POINT_MODE_P (op0
->mode
)) == 1;
775 return op0
->data
.cmp (op1
->data
,
776 UNSIGNED_FIXED_POINT_MODE_P (op0
->mode
)) != -1;
783 /* Extend or truncate to a new mode.
784 If SAT_P, saturate the result to the max or the min.
785 Return true, if !SAT_P and overflow. */
788 fixed_convert (FIXED_VALUE_TYPE
*f
, enum machine_mode mode
,
789 const FIXED_VALUE_TYPE
*a
, bool sat_p
)
791 bool overflow_p
= false;
798 if (GET_MODE_FBIT (mode
) > GET_MODE_FBIT (a
->mode
))
800 /* Left shift a to temp_high, temp_low based on a->mode. */
801 double_int temp_high
, temp_low
;
802 int amount
= GET_MODE_FBIT (mode
) - GET_MODE_FBIT (a
->mode
);
803 temp_low
= a
->data
.lshift (amount
,
804 HOST_BITS_PER_DOUBLE_INT
,
805 SIGNED_FIXED_POINT_MODE_P (a
->mode
));
806 /* Logical shift right to temp_high. */
807 temp_high
= a
->data
.llshift (amount
- HOST_BITS_PER_DOUBLE_INT
,
808 HOST_BITS_PER_DOUBLE_INT
);
809 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
)
810 && a
->data
.high
< 0) /* Signed-extend temp_high. */
811 temp_high
= temp_high
.sext (amount
);
814 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
) ==
815 SIGNED_FIXED_POINT_MODE_P (f
->mode
))
816 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
, &f
->data
,
820 /* Take care of the cases when converting between signed and
822 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
))
824 /* Signed -> Unsigned. */
825 if (a
->data
.high
< 0)
829 f
->data
.low
= 0; /* Set to zero. */
830 f
->data
.high
= 0; /* Set to zero. */
836 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
841 /* Unsigned -> Signed. */
842 if (temp_high
.high
< 0)
846 /* Set to maximum. */
847 f
->data
.low
= -1; /* Set to all ones. */
848 f
->data
.high
= -1; /* Set to all ones. */
849 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
850 + GET_MODE_IBIT (f
->mode
));
851 /* Clear the sign. */
857 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
864 /* Right shift a to temp based on a->mode. */
866 temp
= a
->data
.lshift (GET_MODE_FBIT (mode
) - GET_MODE_FBIT (a
->mode
),
867 HOST_BITS_PER_DOUBLE_INT
,
868 SIGNED_FIXED_POINT_MODE_P (a
->mode
));
871 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
) ==
872 SIGNED_FIXED_POINT_MODE_P (f
->mode
))
873 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
876 /* Take care of the cases when converting between signed and
878 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
))
880 /* Signed -> Unsigned. */
881 if (a
->data
.high
< 0)
885 f
->data
.low
= 0; /* Set to zero. */
886 f
->data
.high
= 0; /* Set to zero. */
892 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
,
897 /* Unsigned -> Signed. */
902 /* Set to maximum. */
903 f
->data
.low
= -1; /* Set to all ones. */
904 f
->data
.high
= -1; /* Set to all ones. */
905 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
906 + GET_MODE_IBIT (f
->mode
));
907 /* Clear the sign. */
913 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
,
919 f
->data
= f
->data
.ext (SIGNED_FIXED_POINT_MODE_P (f
->mode
)
920 + GET_MODE_FBIT (f
->mode
)
921 + GET_MODE_IBIT (f
->mode
),
922 UNSIGNED_FIXED_POINT_MODE_P (f
->mode
));
926 /* Convert to a new fixed-point mode from an integer.
927 If UNSIGNED_P, this integer is unsigned.
928 If SAT_P, saturate the result to the max or the min.
929 Return true, if !SAT_P and overflow. */
932 fixed_convert_from_int (FIXED_VALUE_TYPE
*f
, enum machine_mode mode
,
933 double_int a
, bool unsigned_p
, bool sat_p
)
935 bool overflow_p
= false;
936 /* Left shift a to temp_high, temp_low. */
937 double_int temp_high
, temp_low
;
938 int amount
= GET_MODE_FBIT (mode
);
939 if (amount
== HOST_BITS_PER_DOUBLE_INT
)
947 temp_low
= a
.llshift (amount
, HOST_BITS_PER_DOUBLE_INT
);
949 /* Logical shift right to temp_high. */
950 temp_high
= a
.llshift (amount
- HOST_BITS_PER_DOUBLE_INT
,
951 HOST_BITS_PER_DOUBLE_INT
);
953 if (!unsigned_p
&& a
.high
< 0) /* Signed-extend temp_high. */
954 temp_high
= temp_high
.sext (amount
);
959 if (unsigned_p
== UNSIGNED_FIXED_POINT_MODE_P (f
->mode
))
960 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
, &f
->data
,
964 /* Take care of the cases when converting between signed and unsigned. */
967 /* Signed -> Unsigned. */
972 f
->data
.low
= 0; /* Set to zero. */
973 f
->data
.high
= 0; /* Set to zero. */
979 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
984 /* Unsigned -> Signed. */
985 if (temp_high
.high
< 0)
989 /* Set to maximum. */
990 f
->data
.low
= -1; /* Set to all ones. */
991 f
->data
.high
= -1; /* Set to all ones. */
992 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
993 + GET_MODE_IBIT (f
->mode
));
994 /* Clear the sign. */
1000 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
1004 f
->data
= f
->data
.ext (SIGNED_FIXED_POINT_MODE_P (f
->mode
)
1005 + GET_MODE_FBIT (f
->mode
)
1006 + GET_MODE_IBIT (f
->mode
),
1007 UNSIGNED_FIXED_POINT_MODE_P (f
->mode
));
1011 /* Convert to a new fixed-point mode from a real.
1012 If SAT_P, saturate the result to the max or the min.
1013 Return true, if !SAT_P and overflow. */
1016 fixed_convert_from_real (FIXED_VALUE_TYPE
*f
, enum machine_mode mode
,
1017 const REAL_VALUE_TYPE
*a
, bool sat_p
)
1019 bool overflow_p
= false;
1020 REAL_VALUE_TYPE real_value
, fixed_value
, base_value
;
1021 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (mode
);
1022 int i_f_bits
= GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
);
1023 unsigned int fbit
= GET_MODE_FBIT (mode
);
1024 enum fixed_value_range_code temp
;
1028 real_2expN (&base_value
, fbit
, mode
);
1029 real_arithmetic (&fixed_value
, MULT_EXPR
, &real_value
, &base_value
);
1030 real_to_integer2 ((HOST_WIDE_INT
*)&f
->data
.low
, &f
->data
.high
, &fixed_value
);
1031 temp
= check_real_for_fixed_mode (&real_value
, mode
);
1032 if (temp
== FIXED_UNDERFLOW
) /* Minimum. */
1045 f
->data
= f
->data
.alshift (i_f_bits
, HOST_BITS_PER_DOUBLE_INT
);
1046 f
->data
= f
->data
.sext (1 + i_f_bits
);
1052 else if (temp
== FIXED_GT_MAX_EPS
|| temp
== FIXED_MAX_EPS
) /* Maximum. */
1058 f
->data
= f
->data
.zext (i_f_bits
);
1063 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
1067 /* Convert to a new real mode from a fixed-point. */
1070 real_convert_from_fixed (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
1071 const FIXED_VALUE_TYPE
*f
)
1073 REAL_VALUE_TYPE base_value
, fixed_value
, real_value
;
1075 real_2expN (&base_value
, GET_MODE_FBIT (f
->mode
), f
->mode
);
1076 real_from_integer (&fixed_value
, VOIDmode
, f
->data
.low
, f
->data
.high
,
1077 UNSIGNED_FIXED_POINT_MODE_P (f
->mode
));
1078 real_arithmetic (&real_value
, RDIV_EXPR
, &fixed_value
, &base_value
);
1079 real_convert (r
, mode
, &real_value
);
1082 /* Determine whether a fixed-point value F is negative. */
1085 fixed_isneg (const FIXED_VALUE_TYPE
*f
)
1087 if (SIGNED_FIXED_POINT_MODE_P (f
->mode
))
1089 int i_f_bits
= GET_MODE_IBIT (f
->mode
) + GET_MODE_FBIT (f
->mode
);
1090 int sign_bit
= get_fixed_sign_bit (f
->data
, i_f_bits
);