1 /* Fixed-point arithmetic support.
2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2012
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
26 #include "diagnostic-core.h"
28 /* Compare two fixed objects for bitwise identity. */
31 fixed_identical (const FIXED_VALUE_TYPE
*a
, const FIXED_VALUE_TYPE
*b
)
33 return (a
->mode
== b
->mode
34 && a
->data
.high
== b
->data
.high
35 && a
->data
.low
== b
->data
.low
);
38 /* Calculate a hash value. */
41 fixed_hash (const FIXED_VALUE_TYPE
*f
)
43 return (unsigned int) (f
->data
.low
^ f
->data
.high
);
46 /* Define the enum code for the range of the fixed-point value. */
47 enum fixed_value_range_code
{
48 FIXED_OK
, /* The value is within the range. */
49 FIXED_UNDERFLOW
, /* The value is less than the minimum. */
50 FIXED_GT_MAX_EPS
, /* The value is greater than the maximum, but not equal
51 to the maximum plus the epsilon. */
52 FIXED_MAX_EPS
/* The value equals the maximum plus the epsilon. */
55 /* Check REAL_VALUE against the range of the fixed-point mode.
56 Return FIXED_OK, if it is within the range.
57 FIXED_UNDERFLOW, if it is less than the minimum.
58 FIXED_GT_MAX_EPS, if it is greater than the maximum, but not equal to
59 the maximum plus the epsilon.
60 FIXED_MAX_EPS, if it is equal to the maximum plus the epsilon. */
62 static enum fixed_value_range_code
63 check_real_for_fixed_mode (REAL_VALUE_TYPE
*real_value
, enum machine_mode mode
)
65 REAL_VALUE_TYPE max_value
, min_value
, epsilon_value
;
67 real_2expN (&max_value
, GET_MODE_IBIT (mode
), mode
);
68 real_2expN (&epsilon_value
, -GET_MODE_FBIT (mode
), mode
);
70 if (SIGNED_FIXED_POINT_MODE_P (mode
))
71 min_value
= real_value_negate (&max_value
);
73 real_from_string (&min_value
, "0.0");
75 if (real_compare (LT_EXPR
, real_value
, &min_value
))
76 return FIXED_UNDERFLOW
;
77 if (real_compare (EQ_EXPR
, real_value
, &max_value
))
79 real_arithmetic (&max_value
, MINUS_EXPR
, &max_value
, &epsilon_value
);
80 if (real_compare (GT_EXPR
, real_value
, &max_value
))
81 return FIXED_GT_MAX_EPS
;
85 /* Initialize from a decimal or hexadecimal string. */
88 fixed_from_string (FIXED_VALUE_TYPE
*f
, const char *str
, enum machine_mode mode
)
90 REAL_VALUE_TYPE real_value
, fixed_value
, base_value
;
92 enum fixed_value_range_code temp
;
95 fbit
= GET_MODE_FBIT (mode
);
97 real_from_string (&real_value
, str
);
98 temp
= check_real_for_fixed_mode (&real_value
, f
->mode
);
99 /* We don't want to warn the case when the _Fract value is 1.0. */
100 if (temp
== FIXED_UNDERFLOW
101 || temp
== FIXED_GT_MAX_EPS
102 || (temp
== FIXED_MAX_EPS
&& ALL_ACCUM_MODE_P (f
->mode
)))
103 warning (OPT_Woverflow
,
104 "large fixed-point constant implicitly truncated to fixed-point type");
105 real_2expN (&base_value
, fbit
, mode
);
106 real_arithmetic (&fixed_value
, MULT_EXPR
, &real_value
, &base_value
);
107 real_to_integer2 ((HOST_WIDE_INT
*)&f
->data
.low
, &f
->data
.high
,
110 if (temp
== FIXED_MAX_EPS
&& ALL_FRACT_MODE_P (f
->mode
))
112 /* From the spec, we need to evaluate 1 to the maximal value. */
115 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
116 + GET_MODE_IBIT (f
->mode
));
119 f
->data
= f
->data
.ext (SIGNED_FIXED_POINT_MODE_P (f
->mode
)
120 + GET_MODE_FBIT (f
->mode
)
121 + GET_MODE_IBIT (f
->mode
),
122 UNSIGNED_FIXED_POINT_MODE_P (f
->mode
));
125 /* Render F as a decimal floating point constant. */
128 fixed_to_decimal (char *str
, const FIXED_VALUE_TYPE
*f_orig
,
131 REAL_VALUE_TYPE real_value
, base_value
, fixed_value
;
133 real_2expN (&base_value
, GET_MODE_FBIT (f_orig
->mode
), f_orig
->mode
);
134 real_from_integer (&real_value
, VOIDmode
, f_orig
->data
.low
, f_orig
->data
.high
,
135 UNSIGNED_FIXED_POINT_MODE_P (f_orig
->mode
));
136 real_arithmetic (&fixed_value
, RDIV_EXPR
, &real_value
, &base_value
);
137 real_to_decimal (str
, &fixed_value
, buf_size
, 0, 1);
140 /* If SAT_P, saturate A to the maximum or the minimum, and save to *F based on
141 the machine mode MODE.
142 Do not modify *F otherwise.
143 This function assumes the width of double_int is greater than the width
144 of the fixed-point value (the sum of a possible sign bit, possible ibits,
146 Return true, if !SAT_P and overflow. */
149 fixed_saturate1 (enum machine_mode mode
, double_int a
, double_int
*f
,
152 bool overflow_p
= false;
153 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (mode
);
154 int i_f_bits
= GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
);
156 if (unsigned_p
) /* Unsigned type. */
161 max
= max
.zext (i_f_bits
);
170 else /* Signed type. */
175 max
= max
.zext (i_f_bits
);
178 min
= min
.alshift (i_f_bits
, HOST_BITS_PER_DOUBLE_INT
);
179 min
= min
.sext (1 + i_f_bits
);
187 else if (a
.slt (min
))
198 /* If SAT_P, saturate {A_HIGH, A_LOW} to the maximum or the minimum, and
199 save to *F based on the machine mode MODE.
200 Do not modify *F otherwise.
201 This function assumes the width of two double_int is greater than the width
202 of the fixed-point value (the sum of a possible sign bit, possible ibits,
204 Return true, if !SAT_P and overflow. */
207 fixed_saturate2 (enum machine_mode mode
, double_int a_high
, double_int a_low
,
208 double_int
*f
, bool sat_p
)
210 bool overflow_p
= false;
211 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (mode
);
212 int i_f_bits
= GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
);
214 if (unsigned_p
) /* Unsigned type. */
216 double_int max_r
, max_s
;
221 max_s
= max_s
.zext (i_f_bits
);
222 if (a_high
.ugt (max_r
)
223 || (a_high
== max_r
&&
232 else /* Signed type. */
234 double_int max_r
, max_s
, min_r
, min_s
;
239 max_s
= max_s
.zext (i_f_bits
);
244 min_s
= min_s
.alshift (i_f_bits
, HOST_BITS_PER_DOUBLE_INT
);
245 min_s
= min_s
.sext (1 + i_f_bits
);
246 if (a_high
.sgt (max_r
)
247 || (a_high
== max_r
&&
255 else if (a_high
.slt (min_r
)
256 || (a_high
== min_r
&&
268 /* Return the sign bit based on I_F_BITS. */
271 get_fixed_sign_bit (double_int a
, int i_f_bits
)
273 if (i_f_bits
< HOST_BITS_PER_WIDE_INT
)
274 return (a
.low
>> i_f_bits
) & 1;
276 return (a
.high
>> (i_f_bits
- HOST_BITS_PER_WIDE_INT
)) & 1;
279 /* Calculate F = A + (SUBTRACT_P ? -B : B).
280 If SAT_P, saturate the result to the max or the min.
281 Return true, if !SAT_P and overflow. */
284 do_fixed_add (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
285 const FIXED_VALUE_TYPE
*b
, bool subtract_p
, bool sat_p
)
287 bool overflow_p
= false;
292 /* This was a conditional expression but it triggered a bug in
299 unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
300 i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
302 f
->data
= a
->data
+ temp
;
303 if (unsigned_p
) /* Unsigned type. */
305 if (subtract_p
) /* Unsigned subtraction. */
307 if (a
->data
.ult (b
->data
))
318 else /* Unsigned addition. */
320 f
->data
= f
->data
.zext (i_f_bits
);
321 if (f
->data
.ult (a
->data
)
322 || f
->data
.ult (b
->data
))
334 else /* Signed type. */
337 && (get_fixed_sign_bit (a
->data
, i_f_bits
)
338 == get_fixed_sign_bit (b
->data
, i_f_bits
))
339 && (get_fixed_sign_bit (a
->data
, i_f_bits
)
340 != get_fixed_sign_bit (f
->data
, i_f_bits
)))
342 && (get_fixed_sign_bit (a
->data
, i_f_bits
)
343 != get_fixed_sign_bit (b
->data
, i_f_bits
))
344 && (get_fixed_sign_bit (a
->data
, i_f_bits
)
345 != get_fixed_sign_bit (f
->data
, i_f_bits
))))
351 f
->data
= f
->data
.alshift (i_f_bits
, HOST_BITS_PER_DOUBLE_INT
);
352 if (get_fixed_sign_bit (a
->data
, i_f_bits
) == 0)
361 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
365 /* Calculate F = A * B.
366 If SAT_P, saturate the result to the max or the min.
367 Return true, if !SAT_P and overflow. */
370 do_fixed_multiply (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
371 const FIXED_VALUE_TYPE
*b
, bool sat_p
)
373 bool overflow_p
= false;
374 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
375 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
377 if (GET_MODE_PRECISION (f
->mode
) <= HOST_BITS_PER_WIDE_INT
)
379 f
->data
= a
->data
* b
->data
;
380 f
->data
= f
->data
.lshift (-GET_MODE_FBIT (f
->mode
),
381 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
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
, !unsigned_p
);
473 overflow_p
= fixed_saturate2 (f
->mode
, r
, s
, &f
->data
, sat_p
);
476 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
480 /* Calculate F = A / B.
481 If SAT_P, saturate the result to the max or the min.
482 Return true, if !SAT_P and overflow. */
485 do_fixed_divide (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
486 const FIXED_VALUE_TYPE
*b
, bool sat_p
)
488 bool overflow_p
= false;
489 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
490 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
492 if (GET_MODE_PRECISION (f
->mode
) <= HOST_BITS_PER_WIDE_INT
)
494 f
->data
= a
->data
.lshift (GET_MODE_FBIT (f
->mode
),
495 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
496 f
->data
= f
->data
.div (b
->data
, unsigned_p
, TRUNC_DIV_EXPR
);
497 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
501 double_int pos_a
, pos_b
, r
, s
;
502 double_int quo_r
, quo_s
, mod
, temp
;
506 /* If a < 0, negate a. */
507 if (!unsigned_p
&& a
->data
.high
< 0)
515 /* If b < 0, negate b. */
516 if (!unsigned_p
&& b
->data
.high
< 0)
524 /* Left shift pos_a to {r, s} by FBIT. */
525 if (GET_MODE_FBIT (f
->mode
) == HOST_BITS_PER_DOUBLE_INT
)
533 s
= pos_a
.llshift (GET_MODE_FBIT (f
->mode
), HOST_BITS_PER_DOUBLE_INT
);
534 r
= pos_a
.llshift (- (HOST_BITS_PER_DOUBLE_INT
535 - GET_MODE_FBIT (f
->mode
)),
536 HOST_BITS_PER_DOUBLE_INT
);
539 /* Divide r by pos_b to quo_r. The remainder is in mod. */
540 quo_r
= r
.divmod (pos_b
, 1, TRUNC_DIV_EXPR
, &mod
);
541 quo_s
= double_int_zero
;
543 for (i
= 0; i
< HOST_BITS_PER_DOUBLE_INT
; i
++)
545 /* Record the leftmost bit of mod. */
546 int leftmost_mod
= (mod
.high
< 0);
548 /* Shift left mod by 1 bit. */
549 mod
= mod
.llshift (1, HOST_BITS_PER_DOUBLE_INT
);
551 /* Test the leftmost bit of s to add to mod. */
555 /* Shift left quo_s by 1 bit. */
556 quo_s
= quo_s
.llshift (1, HOST_BITS_PER_DOUBLE_INT
);
558 /* Try to calculate (mod - pos_b). */
561 if (leftmost_mod
== 1 || mod
.ucmp (pos_b
) != -1)
567 /* Shift left s by 1 bit. */
568 s
= s
.llshift (1, HOST_BITS_PER_DOUBLE_INT
);
575 if (quo_s
.high
== 0 && quo_s
.low
== 0)
579 quo_r
.low
= ~quo_r
.low
;
580 quo_r
.high
= ~quo_r
.high
;
585 overflow_p
= fixed_saturate2 (f
->mode
, quo_r
, quo_s
, &f
->data
, sat_p
);
588 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
592 /* Calculate F = A << B if LEFT_P. Otherwise, F = A >> B.
593 If SAT_P, saturate the result to the max or the min.
594 Return true, if !SAT_P and overflow. */
597 do_fixed_shift (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
598 const FIXED_VALUE_TYPE
*b
, bool left_p
, bool sat_p
)
600 bool overflow_p
= false;
601 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
602 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
605 if (b
->data
.low
== 0)
611 if (GET_MODE_PRECISION (f
->mode
) <= HOST_BITS_PER_WIDE_INT
|| (!left_p
))
613 f
->data
= a
->data
.lshift (left_p
? b
->data
.low
: -b
->data
.low
,
614 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
615 if (left_p
) /* Only left shift saturates. */
616 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
618 else /* We need two double_int to store the left-shift result. */
620 double_int temp_high
, temp_low
;
621 if (b
->data
.low
== HOST_BITS_PER_DOUBLE_INT
)
629 temp_low
= a
->data
.lshift (b
->data
.low
,
630 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
631 /* Logical shift right to temp_high. */
632 temp_high
= a
->data
.llshift (b
->data
.low
- HOST_BITS_PER_DOUBLE_INT
,
633 HOST_BITS_PER_DOUBLE_INT
);
635 if (!unsigned_p
&& a
->data
.high
< 0) /* Signed-extend temp_high. */
636 temp_high
= temp_high
.ext (b
->data
.low
, unsigned_p
);
638 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
, &f
->data
,
641 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
646 If SAT_P, saturate the result to the max or the min.
647 Return true, if !SAT_P and overflow. */
650 do_fixed_neg (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
, bool sat_p
)
652 bool overflow_p
= false;
653 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
654 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
657 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
659 if (unsigned_p
) /* Unsigned type. */
661 if (f
->data
.low
!= 0 || f
->data
.high
!= 0)
672 else /* Signed type. */
674 if (!(f
->data
.high
== 0 && f
->data
.low
== 0)
675 && f
->data
.high
== a
->data
.high
&& f
->data
.low
== a
->data
.low
)
679 /* Saturate to the maximum by subtracting f->data by one. */
682 f
->data
= f
->data
.zext (i_f_bits
);
691 /* Perform the binary or unary operation described by CODE.
692 Note that OP0 and OP1 must have the same mode for binary operators.
693 For a unary operation, leave OP1 NULL.
694 Return true, if !SAT_P and overflow. */
697 fixed_arithmetic (FIXED_VALUE_TYPE
*f
, int icode
, const FIXED_VALUE_TYPE
*op0
,
698 const FIXED_VALUE_TYPE
*op1
, bool sat_p
)
703 return do_fixed_neg (f
, op0
, sat_p
);
707 gcc_assert (op0
->mode
== op1
->mode
);
708 return do_fixed_add (f
, op0
, op1
, false, sat_p
);
712 gcc_assert (op0
->mode
== op1
->mode
);
713 return do_fixed_add (f
, op0
, op1
, true, sat_p
);
717 gcc_assert (op0
->mode
== op1
->mode
);
718 return do_fixed_multiply (f
, op0
, op1
, sat_p
);
722 gcc_assert (op0
->mode
== op1
->mode
);
723 return do_fixed_divide (f
, op0
, op1
, sat_p
);
727 return do_fixed_shift (f
, op0
, op1
, true, sat_p
);
731 return do_fixed_shift (f
, op0
, op1
, false, sat_p
);
740 /* Compare fixed-point values by tree_code.
741 Note that OP0 and OP1 must have the same mode. */
744 fixed_compare (int icode
, const FIXED_VALUE_TYPE
*op0
,
745 const FIXED_VALUE_TYPE
*op1
)
747 enum tree_code code
= (enum tree_code
) icode
;
748 gcc_assert (op0
->mode
== op1
->mode
);
753 return op0
->data
!= op1
->data
;
756 return op0
->data
== op1
->data
;
759 return op0
->data
.cmp (op1
->data
,
760 UNSIGNED_FIXED_POINT_MODE_P (op0
->mode
)) == -1;
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;
779 /* Extend or truncate to a new mode.
780 If SAT_P, saturate the result to the max or the min.
781 Return true, if !SAT_P and overflow. */
784 fixed_convert (FIXED_VALUE_TYPE
*f
, enum machine_mode mode
,
785 const FIXED_VALUE_TYPE
*a
, bool sat_p
)
787 bool overflow_p
= false;
794 if (GET_MODE_FBIT (mode
) > GET_MODE_FBIT (a
->mode
))
796 /* Left shift a to temp_high, temp_low based on a->mode. */
797 double_int temp_high
, temp_low
;
798 int amount
= GET_MODE_FBIT (mode
) - GET_MODE_FBIT (a
->mode
);
799 temp_low
= a
->data
.lshift (amount
,
800 HOST_BITS_PER_DOUBLE_INT
,
801 SIGNED_FIXED_POINT_MODE_P (a
->mode
));
802 /* Logical shift right to temp_high. */
803 temp_high
= a
->data
.llshift (amount
- HOST_BITS_PER_DOUBLE_INT
,
804 HOST_BITS_PER_DOUBLE_INT
);
805 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
)
806 && a
->data
.high
< 0) /* Signed-extend temp_high. */
807 temp_high
= temp_high
.sext (amount
);
810 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
) ==
811 SIGNED_FIXED_POINT_MODE_P (f
->mode
))
812 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
, &f
->data
,
816 /* Take care of the cases when converting between signed and
818 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
))
820 /* Signed -> Unsigned. */
821 if (a
->data
.high
< 0)
825 f
->data
.low
= 0; /* Set to zero. */
826 f
->data
.high
= 0; /* Set to zero. */
832 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
837 /* Unsigned -> Signed. */
838 if (temp_high
.high
< 0)
842 /* Set to maximum. */
843 f
->data
.low
= -1; /* Set to all ones. */
844 f
->data
.high
= -1; /* Set to all ones. */
845 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
846 + GET_MODE_IBIT (f
->mode
));
847 /* Clear the sign. */
853 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
860 /* Right shift a to temp based on a->mode. */
862 temp
= a
->data
.lshift (GET_MODE_FBIT (mode
) - GET_MODE_FBIT (a
->mode
),
863 HOST_BITS_PER_DOUBLE_INT
,
864 SIGNED_FIXED_POINT_MODE_P (a
->mode
));
867 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
) ==
868 SIGNED_FIXED_POINT_MODE_P (f
->mode
))
869 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
872 /* Take care of the cases when converting between signed and
874 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
))
876 /* Signed -> Unsigned. */
877 if (a
->data
.high
< 0)
881 f
->data
.low
= 0; /* Set to zero. */
882 f
->data
.high
= 0; /* Set to zero. */
888 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
,
893 /* Unsigned -> Signed. */
898 /* Set to maximum. */
899 f
->data
.low
= -1; /* Set to all ones. */
900 f
->data
.high
= -1; /* Set to all ones. */
901 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
902 + GET_MODE_IBIT (f
->mode
));
903 /* Clear the sign. */
909 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
,
915 f
->data
= f
->data
.ext (SIGNED_FIXED_POINT_MODE_P (f
->mode
)
916 + GET_MODE_FBIT (f
->mode
)
917 + GET_MODE_IBIT (f
->mode
),
918 UNSIGNED_FIXED_POINT_MODE_P (f
->mode
));
922 /* Convert to a new fixed-point mode from an integer.
923 If UNSIGNED_P, this integer is unsigned.
924 If SAT_P, saturate the result to the max or the min.
925 Return true, if !SAT_P and overflow. */
928 fixed_convert_from_int (FIXED_VALUE_TYPE
*f
, enum machine_mode mode
,
929 double_int a
, bool unsigned_p
, bool sat_p
)
931 bool overflow_p
= false;
932 /* Left shift a to temp_high, temp_low. */
933 double_int temp_high
, temp_low
;
934 int amount
= GET_MODE_FBIT (mode
);
935 if (amount
== HOST_BITS_PER_DOUBLE_INT
)
943 temp_low
= a
.llshift (amount
, HOST_BITS_PER_DOUBLE_INT
);
945 /* Logical shift right to temp_high. */
946 temp_high
= a
.llshift (amount
- HOST_BITS_PER_DOUBLE_INT
,
947 HOST_BITS_PER_DOUBLE_INT
);
949 if (!unsigned_p
&& a
.high
< 0) /* Signed-extend temp_high. */
950 temp_high
= temp_high
.sext (amount
);
955 if (unsigned_p
== UNSIGNED_FIXED_POINT_MODE_P (f
->mode
))
956 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
, &f
->data
,
960 /* Take care of the cases when converting between signed and unsigned. */
963 /* Signed -> Unsigned. */
968 f
->data
.low
= 0; /* Set to zero. */
969 f
->data
.high
= 0; /* Set to zero. */
975 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
980 /* Unsigned -> Signed. */
981 if (temp_high
.high
< 0)
985 /* Set to maximum. */
986 f
->data
.low
= -1; /* Set to all ones. */
987 f
->data
.high
= -1; /* Set to all ones. */
988 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
989 + GET_MODE_IBIT (f
->mode
));
990 /* Clear the sign. */
996 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
1000 f
->data
= f
->data
.ext (SIGNED_FIXED_POINT_MODE_P (f
->mode
)
1001 + GET_MODE_FBIT (f
->mode
)
1002 + GET_MODE_IBIT (f
->mode
),
1003 UNSIGNED_FIXED_POINT_MODE_P (f
->mode
));
1007 /* Convert to a new fixed-point mode from a real.
1008 If SAT_P, saturate the result to the max or the min.
1009 Return true, if !SAT_P and overflow. */
1012 fixed_convert_from_real (FIXED_VALUE_TYPE
*f
, enum machine_mode mode
,
1013 const REAL_VALUE_TYPE
*a
, bool sat_p
)
1015 bool overflow_p
= false;
1016 REAL_VALUE_TYPE real_value
, fixed_value
, base_value
;
1017 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (mode
);
1018 int i_f_bits
= GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
);
1019 unsigned int fbit
= GET_MODE_FBIT (mode
);
1020 enum fixed_value_range_code temp
;
1024 real_2expN (&base_value
, fbit
, mode
);
1025 real_arithmetic (&fixed_value
, MULT_EXPR
, &real_value
, &base_value
);
1026 real_to_integer2 ((HOST_WIDE_INT
*)&f
->data
.low
, &f
->data
.high
, &fixed_value
);
1027 temp
= check_real_for_fixed_mode (&real_value
, mode
);
1028 if (temp
== FIXED_UNDERFLOW
) /* Minimum. */
1041 f
->data
= f
->data
.alshift (i_f_bits
, HOST_BITS_PER_DOUBLE_INT
);
1042 f
->data
= f
->data
.sext (1 + i_f_bits
);
1048 else if (temp
== FIXED_GT_MAX_EPS
|| temp
== FIXED_MAX_EPS
) /* Maximum. */
1054 f
->data
= f
->data
.zext (i_f_bits
);
1059 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
1063 /* Convert to a new real mode from a fixed-point. */
1066 real_convert_from_fixed (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
1067 const FIXED_VALUE_TYPE
*f
)
1069 REAL_VALUE_TYPE base_value
, fixed_value
, real_value
;
1071 real_2expN (&base_value
, GET_MODE_FBIT (f
->mode
), f
->mode
);
1072 real_from_integer (&fixed_value
, VOIDmode
, f
->data
.low
, f
->data
.high
,
1073 UNSIGNED_FIXED_POINT_MODE_P (f
->mode
));
1074 real_arithmetic (&real_value
, RDIV_EXPR
, &fixed_value
, &base_value
);
1075 real_convert (r
, mode
, &real_value
);
1078 /* Determine whether a fixed-point value F is negative. */
1081 fixed_isneg (const FIXED_VALUE_TYPE
*f
)
1083 if (SIGNED_FIXED_POINT_MODE_P (f
->mode
))
1085 int i_f_bits
= GET_MODE_IBIT (f
->mode
) + GET_MODE_FBIT (f
->mode
);
1086 int sign_bit
= get_fixed_sign_bit (f
->data
, i_f_bits
);