1 /* Fixed-point arithmetic support.
2 Copyright (C) 2006-2013 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
;
85 /* Construct a CONST_FIXED from a bit payload and machine mode MODE.
86 The bits in PAYLOAD are used verbatim. */
89 fixed_from_double_int (double_int payload
, enum machine_mode mode
)
91 FIXED_VALUE_TYPE value
;
93 gcc_assert (GET_MODE_BITSIZE (mode
) <= HOST_BITS_PER_DOUBLE_INT
);
102 /* Initialize from a decimal or hexadecimal string. */
105 fixed_from_string (FIXED_VALUE_TYPE
*f
, const char *str
, enum machine_mode mode
)
107 REAL_VALUE_TYPE real_value
, fixed_value
, base_value
;
109 enum fixed_value_range_code temp
;
112 fbit
= GET_MODE_FBIT (mode
);
114 real_from_string (&real_value
, str
);
115 temp
= check_real_for_fixed_mode (&real_value
, f
->mode
);
116 /* We don't want to warn the case when the _Fract value is 1.0. */
117 if (temp
== FIXED_UNDERFLOW
118 || temp
== FIXED_GT_MAX_EPS
119 || (temp
== FIXED_MAX_EPS
&& ALL_ACCUM_MODE_P (f
->mode
)))
120 warning (OPT_Woverflow
,
121 "large fixed-point constant implicitly truncated to fixed-point type");
122 real_2expN (&base_value
, fbit
, mode
);
123 real_arithmetic (&fixed_value
, MULT_EXPR
, &real_value
, &base_value
);
124 real_to_integer2 ((HOST_WIDE_INT
*)&f
->data
.low
, &f
->data
.high
,
127 if (temp
== FIXED_MAX_EPS
&& ALL_FRACT_MODE_P (f
->mode
))
129 /* From the spec, we need to evaluate 1 to the maximal value. */
132 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
133 + GET_MODE_IBIT (f
->mode
));
136 f
->data
= f
->data
.ext (SIGNED_FIXED_POINT_MODE_P (f
->mode
)
137 + GET_MODE_FBIT (f
->mode
)
138 + GET_MODE_IBIT (f
->mode
),
139 UNSIGNED_FIXED_POINT_MODE_P (f
->mode
));
142 /* Render F as a decimal floating point constant. */
145 fixed_to_decimal (char *str
, const FIXED_VALUE_TYPE
*f_orig
,
148 REAL_VALUE_TYPE real_value
, base_value
, fixed_value
;
150 real_2expN (&base_value
, GET_MODE_FBIT (f_orig
->mode
), f_orig
->mode
);
151 real_from_integer (&real_value
, VOIDmode
, f_orig
->data
.low
, f_orig
->data
.high
,
152 UNSIGNED_FIXED_POINT_MODE_P (f_orig
->mode
));
153 real_arithmetic (&fixed_value
, RDIV_EXPR
, &real_value
, &base_value
);
154 real_to_decimal (str
, &fixed_value
, buf_size
, 0, 1);
157 /* If SAT_P, saturate A to the maximum or the minimum, and save to *F based on
158 the machine mode MODE.
159 Do not modify *F otherwise.
160 This function assumes the width of double_int is greater than the width
161 of the fixed-point value (the sum of a possible sign bit, possible ibits,
163 Return true, if !SAT_P and overflow. */
166 fixed_saturate1 (enum machine_mode mode
, double_int a
, double_int
*f
,
169 bool overflow_p
= false;
170 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (mode
);
171 int i_f_bits
= GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
);
173 if (unsigned_p
) /* Unsigned type. */
178 max
= max
.zext (i_f_bits
);
187 else /* Signed type. */
192 max
= max
.zext (i_f_bits
);
195 min
= min
.alshift (i_f_bits
, HOST_BITS_PER_DOUBLE_INT
);
196 min
= min
.sext (1 + i_f_bits
);
204 else if (a
.slt (min
))
215 /* If SAT_P, saturate {A_HIGH, A_LOW} to the maximum or the minimum, and
216 save to *F based on the machine mode MODE.
217 Do not modify *F otherwise.
218 This function assumes the width of two double_int is greater than the width
219 of the fixed-point value (the sum of a possible sign bit, possible ibits,
221 Return true, if !SAT_P and overflow. */
224 fixed_saturate2 (enum machine_mode mode
, double_int a_high
, double_int a_low
,
225 double_int
*f
, bool sat_p
)
227 bool overflow_p
= false;
228 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (mode
);
229 int i_f_bits
= GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
);
231 if (unsigned_p
) /* Unsigned type. */
233 double_int max_r
, max_s
;
238 max_s
= max_s
.zext (i_f_bits
);
239 if (a_high
.ugt (max_r
)
240 || (a_high
== max_r
&&
249 else /* Signed type. */
251 double_int max_r
, max_s
, min_r
, min_s
;
256 max_s
= max_s
.zext (i_f_bits
);
261 min_s
= min_s
.alshift (i_f_bits
, HOST_BITS_PER_DOUBLE_INT
);
262 min_s
= min_s
.sext (1 + i_f_bits
);
263 if (a_high
.sgt (max_r
)
264 || (a_high
== max_r
&&
272 else if (a_high
.slt (min_r
)
273 || (a_high
== min_r
&&
285 /* Return the sign bit based on I_F_BITS. */
288 get_fixed_sign_bit (double_int a
, int i_f_bits
)
290 if (i_f_bits
< HOST_BITS_PER_WIDE_INT
)
291 return (a
.low
>> i_f_bits
) & 1;
293 return (a
.high
>> (i_f_bits
- HOST_BITS_PER_WIDE_INT
)) & 1;
296 /* Calculate F = A + (SUBTRACT_P ? -B : B).
297 If SAT_P, saturate the result to the max or the min.
298 Return true, if !SAT_P and overflow. */
301 do_fixed_add (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
302 const FIXED_VALUE_TYPE
*b
, bool subtract_p
, bool sat_p
)
304 bool overflow_p
= false;
309 /* This was a conditional expression but it triggered a bug in
316 unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
317 i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
319 f
->data
= a
->data
+ temp
;
320 if (unsigned_p
) /* Unsigned type. */
322 if (subtract_p
) /* Unsigned subtraction. */
324 if (a
->data
.ult (b
->data
))
335 else /* Unsigned addition. */
337 f
->data
= f
->data
.zext (i_f_bits
);
338 if (f
->data
.ult (a
->data
)
339 || f
->data
.ult (b
->data
))
351 else /* Signed type. */
354 && (get_fixed_sign_bit (a
->data
, i_f_bits
)
355 == get_fixed_sign_bit (b
->data
, i_f_bits
))
356 && (get_fixed_sign_bit (a
->data
, i_f_bits
)
357 != get_fixed_sign_bit (f
->data
, i_f_bits
)))
359 && (get_fixed_sign_bit (a
->data
, i_f_bits
)
360 != get_fixed_sign_bit (b
->data
, i_f_bits
))
361 && (get_fixed_sign_bit (a
->data
, i_f_bits
)
362 != get_fixed_sign_bit (f
->data
, i_f_bits
))))
368 f
->data
= f
->data
.alshift (i_f_bits
, HOST_BITS_PER_DOUBLE_INT
);
369 if (get_fixed_sign_bit (a
->data
, i_f_bits
) == 0)
378 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
382 /* Calculate F = A * B.
383 If SAT_P, saturate the result to the max or the min.
384 Return true, if !SAT_P and overflow. */
387 do_fixed_multiply (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
388 const FIXED_VALUE_TYPE
*b
, bool sat_p
)
390 bool overflow_p
= false;
391 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
392 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
394 if (GET_MODE_PRECISION (f
->mode
) <= HOST_BITS_PER_WIDE_INT
)
396 f
->data
= a
->data
* b
->data
;
397 f
->data
= f
->data
.lshift (-GET_MODE_FBIT (f
->mode
),
398 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
399 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
403 /* The result of multiplication expands to two double_int. */
404 double_int a_high
, a_low
, b_high
, b_low
;
405 double_int high_high
, high_low
, low_high
, low_low
;
406 double_int r
, s
, temp1
, temp2
;
409 /* Decompose a and b to four double_int. */
410 a_high
.low
= a
->data
.high
;
412 a_low
.low
= a
->data
.low
;
414 b_high
.low
= b
->data
.high
;
416 b_low
.low
= b
->data
.low
;
419 /* Perform four multiplications. */
420 low_low
= a_low
* b_low
;
421 low_high
= a_low
* b_high
;
422 high_low
= a_high
* b_low
;
423 high_high
= a_high
* b_high
;
425 /* Accumulate four results to {r, s}. */
426 temp1
.high
= high_low
.low
;
431 carry
++; /* Carry */
434 temp2
.high
= low_high
.low
;
439 carry
++; /* Carry */
441 temp1
.low
= high_low
.high
;
443 r
= high_high
+ temp1
;
444 temp1
.low
= low_high
.high
;
451 /* We need to subtract b from r, if a < 0. */
452 if (!unsigned_p
&& a
->data
.high
< 0)
454 /* We need to subtract a from r, if b < 0. */
455 if (!unsigned_p
&& b
->data
.high
< 0)
458 /* Shift right the result by FBIT. */
459 if (GET_MODE_FBIT (f
->mode
) == HOST_BITS_PER_DOUBLE_INT
)
474 f
->data
.high
= s
.high
;
478 s
= s
.llshift ((-GET_MODE_FBIT (f
->mode
)), HOST_BITS_PER_DOUBLE_INT
);
479 f
->data
= r
.llshift ((HOST_BITS_PER_DOUBLE_INT
480 - GET_MODE_FBIT (f
->mode
)),
481 HOST_BITS_PER_DOUBLE_INT
);
482 f
->data
.low
= f
->data
.low
| s
.low
;
483 f
->data
.high
= f
->data
.high
| s
.high
;
485 s
.high
= f
->data
.high
;
486 r
= r
.lshift (-GET_MODE_FBIT (f
->mode
),
487 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
490 overflow_p
= fixed_saturate2 (f
->mode
, r
, s
, &f
->data
, sat_p
);
493 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
497 /* Calculate F = A / B.
498 If SAT_P, saturate the result to the max or the min.
499 Return true, if !SAT_P and overflow. */
502 do_fixed_divide (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
503 const FIXED_VALUE_TYPE
*b
, bool sat_p
)
505 bool overflow_p
= false;
506 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
507 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
509 if (GET_MODE_PRECISION (f
->mode
) <= HOST_BITS_PER_WIDE_INT
)
511 f
->data
= a
->data
.lshift (GET_MODE_FBIT (f
->mode
),
512 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
513 f
->data
= f
->data
.div (b
->data
, unsigned_p
, TRUNC_DIV_EXPR
);
514 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
518 double_int pos_a
, pos_b
, r
, s
;
519 double_int quo_r
, quo_s
, mod
, temp
;
523 /* If a < 0, negate a. */
524 if (!unsigned_p
&& a
->data
.high
< 0)
532 /* If b < 0, negate b. */
533 if (!unsigned_p
&& b
->data
.high
< 0)
541 /* Left shift pos_a to {r, s} by FBIT. */
542 if (GET_MODE_FBIT (f
->mode
) == HOST_BITS_PER_DOUBLE_INT
)
550 s
= pos_a
.llshift (GET_MODE_FBIT (f
->mode
), HOST_BITS_PER_DOUBLE_INT
);
551 r
= pos_a
.llshift (- (HOST_BITS_PER_DOUBLE_INT
552 - GET_MODE_FBIT (f
->mode
)),
553 HOST_BITS_PER_DOUBLE_INT
);
556 /* Divide r by pos_b to quo_r. The remainder is in mod. */
557 quo_r
= r
.divmod (pos_b
, 1, TRUNC_DIV_EXPR
, &mod
);
558 quo_s
= double_int_zero
;
560 for (i
= 0; i
< HOST_BITS_PER_DOUBLE_INT
; i
++)
562 /* Record the leftmost bit of mod. */
563 int leftmost_mod
= (mod
.high
< 0);
565 /* Shift left mod by 1 bit. */
566 mod
= mod
.llshift (1, HOST_BITS_PER_DOUBLE_INT
);
568 /* Test the leftmost bit of s to add to mod. */
572 /* Shift left quo_s by 1 bit. */
573 quo_s
= quo_s
.llshift (1, HOST_BITS_PER_DOUBLE_INT
);
575 /* Try to calculate (mod - pos_b). */
578 if (leftmost_mod
== 1 || mod
.ucmp (pos_b
) != -1)
584 /* Shift left s by 1 bit. */
585 s
= s
.llshift (1, HOST_BITS_PER_DOUBLE_INT
);
592 if (quo_s
.high
== 0 && quo_s
.low
== 0)
596 quo_r
.low
= ~quo_r
.low
;
597 quo_r
.high
= ~quo_r
.high
;
602 overflow_p
= fixed_saturate2 (f
->mode
, quo_r
, quo_s
, &f
->data
, sat_p
);
605 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
609 /* Calculate F = A << B if LEFT_P. Otherwise, F = A >> B.
610 If SAT_P, saturate the result to the max or the min.
611 Return true, if !SAT_P and overflow. */
614 do_fixed_shift (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
615 const FIXED_VALUE_TYPE
*b
, bool left_p
, bool sat_p
)
617 bool overflow_p
= false;
618 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
619 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
622 if (b
->data
.low
== 0)
628 if (GET_MODE_PRECISION (f
->mode
) <= HOST_BITS_PER_WIDE_INT
|| (!left_p
))
630 f
->data
= a
->data
.lshift (left_p
? b
->data
.low
: -b
->data
.low
,
631 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
632 if (left_p
) /* Only left shift saturates. */
633 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
635 else /* We need two double_int to store the left-shift result. */
637 double_int temp_high
, temp_low
;
638 if (b
->data
.low
== HOST_BITS_PER_DOUBLE_INT
)
646 temp_low
= a
->data
.lshift (b
->data
.low
,
647 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
648 /* Logical shift right to temp_high. */
649 temp_high
= a
->data
.llshift (b
->data
.low
- HOST_BITS_PER_DOUBLE_INT
,
650 HOST_BITS_PER_DOUBLE_INT
);
652 if (!unsigned_p
&& a
->data
.high
< 0) /* Signed-extend temp_high. */
653 temp_high
= temp_high
.ext (b
->data
.low
, unsigned_p
);
655 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
, &f
->data
,
658 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
663 If SAT_P, saturate the result to the max or the min.
664 Return true, if !SAT_P and overflow. */
667 do_fixed_neg (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
, bool sat_p
)
669 bool overflow_p
= false;
670 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
671 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
674 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
676 if (unsigned_p
) /* Unsigned type. */
678 if (f
->data
.low
!= 0 || f
->data
.high
!= 0)
689 else /* Signed type. */
691 if (!(f
->data
.high
== 0 && f
->data
.low
== 0)
692 && f
->data
.high
== a
->data
.high
&& f
->data
.low
== a
->data
.low
)
696 /* Saturate to the maximum by subtracting f->data by one. */
699 f
->data
= f
->data
.zext (i_f_bits
);
708 /* Perform the binary or unary operation described by CODE.
709 Note that OP0 and OP1 must have the same mode for binary operators.
710 For a unary operation, leave OP1 NULL.
711 Return true, if !SAT_P and overflow. */
714 fixed_arithmetic (FIXED_VALUE_TYPE
*f
, int icode
, const FIXED_VALUE_TYPE
*op0
,
715 const FIXED_VALUE_TYPE
*op1
, bool sat_p
)
720 return do_fixed_neg (f
, op0
, sat_p
);
724 gcc_assert (op0
->mode
== op1
->mode
);
725 return do_fixed_add (f
, op0
, op1
, false, sat_p
);
729 gcc_assert (op0
->mode
== op1
->mode
);
730 return do_fixed_add (f
, op0
, op1
, true, sat_p
);
734 gcc_assert (op0
->mode
== op1
->mode
);
735 return do_fixed_multiply (f
, op0
, op1
, sat_p
);
739 gcc_assert (op0
->mode
== op1
->mode
);
740 return do_fixed_divide (f
, op0
, op1
, sat_p
);
744 return do_fixed_shift (f
, op0
, op1
, true, sat_p
);
748 return do_fixed_shift (f
, op0
, op1
, false, sat_p
);
757 /* Compare fixed-point values by tree_code.
758 Note that OP0 and OP1 must have the same mode. */
761 fixed_compare (int icode
, const FIXED_VALUE_TYPE
*op0
,
762 const FIXED_VALUE_TYPE
*op1
)
764 enum tree_code code
= (enum tree_code
) icode
;
765 gcc_assert (op0
->mode
== op1
->mode
);
770 return op0
->data
!= op1
->data
;
773 return op0
->data
== op1
->data
;
776 return op0
->data
.cmp (op1
->data
,
777 UNSIGNED_FIXED_POINT_MODE_P (op0
->mode
)) == -1;
780 return op0
->data
.cmp (op1
->data
,
781 UNSIGNED_FIXED_POINT_MODE_P (op0
->mode
)) != 1;
784 return op0
->data
.cmp (op1
->data
,
785 UNSIGNED_FIXED_POINT_MODE_P (op0
->mode
)) == 1;
788 return op0
->data
.cmp (op1
->data
,
789 UNSIGNED_FIXED_POINT_MODE_P (op0
->mode
)) != -1;
796 /* Extend or truncate to a new mode.
797 If SAT_P, saturate the result to the max or the min.
798 Return true, if !SAT_P and overflow. */
801 fixed_convert (FIXED_VALUE_TYPE
*f
, enum machine_mode mode
,
802 const FIXED_VALUE_TYPE
*a
, bool sat_p
)
804 bool overflow_p
= false;
811 if (GET_MODE_FBIT (mode
) > GET_MODE_FBIT (a
->mode
))
813 /* Left shift a to temp_high, temp_low based on a->mode. */
814 double_int temp_high
, temp_low
;
815 int amount
= GET_MODE_FBIT (mode
) - GET_MODE_FBIT (a
->mode
);
816 temp_low
= a
->data
.lshift (amount
,
817 HOST_BITS_PER_DOUBLE_INT
,
818 SIGNED_FIXED_POINT_MODE_P (a
->mode
));
819 /* Logical shift right to temp_high. */
820 temp_high
= a
->data
.llshift (amount
- HOST_BITS_PER_DOUBLE_INT
,
821 HOST_BITS_PER_DOUBLE_INT
);
822 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
)
823 && a
->data
.high
< 0) /* Signed-extend temp_high. */
824 temp_high
= temp_high
.sext (amount
);
827 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
) ==
828 SIGNED_FIXED_POINT_MODE_P (f
->mode
))
829 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
, &f
->data
,
833 /* Take care of the cases when converting between signed and
835 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
))
837 /* Signed -> Unsigned. */
838 if (a
->data
.high
< 0)
842 f
->data
.low
= 0; /* Set to zero. */
843 f
->data
.high
= 0; /* Set to zero. */
849 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
854 /* Unsigned -> Signed. */
855 if (temp_high
.high
< 0)
859 /* Set to maximum. */
860 f
->data
.low
= -1; /* Set to all ones. */
861 f
->data
.high
= -1; /* Set to all ones. */
862 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
863 + GET_MODE_IBIT (f
->mode
));
864 /* Clear the sign. */
870 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
877 /* Right shift a to temp based on a->mode. */
879 temp
= a
->data
.lshift (GET_MODE_FBIT (mode
) - GET_MODE_FBIT (a
->mode
),
880 HOST_BITS_PER_DOUBLE_INT
,
881 SIGNED_FIXED_POINT_MODE_P (a
->mode
));
884 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
) ==
885 SIGNED_FIXED_POINT_MODE_P (f
->mode
))
886 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
889 /* Take care of the cases when converting between signed and
891 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
))
893 /* Signed -> Unsigned. */
894 if (a
->data
.high
< 0)
898 f
->data
.low
= 0; /* Set to zero. */
899 f
->data
.high
= 0; /* Set to zero. */
905 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
,
910 /* Unsigned -> Signed. */
915 /* Set to maximum. */
916 f
->data
.low
= -1; /* Set to all ones. */
917 f
->data
.high
= -1; /* Set to all ones. */
918 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
919 + GET_MODE_IBIT (f
->mode
));
920 /* Clear the sign. */
926 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
,
932 f
->data
= f
->data
.ext (SIGNED_FIXED_POINT_MODE_P (f
->mode
)
933 + GET_MODE_FBIT (f
->mode
)
934 + GET_MODE_IBIT (f
->mode
),
935 UNSIGNED_FIXED_POINT_MODE_P (f
->mode
));
939 /* Convert to a new fixed-point mode from an integer.
940 If UNSIGNED_P, this integer is unsigned.
941 If SAT_P, saturate the result to the max or the min.
942 Return true, if !SAT_P and overflow. */
945 fixed_convert_from_int (FIXED_VALUE_TYPE
*f
, enum machine_mode mode
,
946 double_int a
, bool unsigned_p
, bool sat_p
)
948 bool overflow_p
= false;
949 /* Left shift a to temp_high, temp_low. */
950 double_int temp_high
, temp_low
;
951 int amount
= GET_MODE_FBIT (mode
);
952 if (amount
== HOST_BITS_PER_DOUBLE_INT
)
960 temp_low
= a
.llshift (amount
, HOST_BITS_PER_DOUBLE_INT
);
962 /* Logical shift right to temp_high. */
963 temp_high
= a
.llshift (amount
- HOST_BITS_PER_DOUBLE_INT
,
964 HOST_BITS_PER_DOUBLE_INT
);
966 if (!unsigned_p
&& a
.high
< 0) /* Signed-extend temp_high. */
967 temp_high
= temp_high
.sext (amount
);
972 if (unsigned_p
== UNSIGNED_FIXED_POINT_MODE_P (f
->mode
))
973 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
, &f
->data
,
977 /* Take care of the cases when converting between signed and unsigned. */
980 /* Signed -> Unsigned. */
985 f
->data
.low
= 0; /* Set to zero. */
986 f
->data
.high
= 0; /* Set to zero. */
992 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
997 /* Unsigned -> Signed. */
998 if (temp_high
.high
< 0)
1002 /* Set to maximum. */
1003 f
->data
.low
= -1; /* Set to all ones. */
1004 f
->data
.high
= -1; /* Set to all ones. */
1005 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
1006 + GET_MODE_IBIT (f
->mode
));
1007 /* Clear the sign. */
1013 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
1017 f
->data
= f
->data
.ext (SIGNED_FIXED_POINT_MODE_P (f
->mode
)
1018 + GET_MODE_FBIT (f
->mode
)
1019 + GET_MODE_IBIT (f
->mode
),
1020 UNSIGNED_FIXED_POINT_MODE_P (f
->mode
));
1024 /* Convert to a new fixed-point mode from a real.
1025 If SAT_P, saturate the result to the max or the min.
1026 Return true, if !SAT_P and overflow. */
1029 fixed_convert_from_real (FIXED_VALUE_TYPE
*f
, enum machine_mode mode
,
1030 const REAL_VALUE_TYPE
*a
, bool sat_p
)
1032 bool overflow_p
= false;
1033 REAL_VALUE_TYPE real_value
, fixed_value
, base_value
;
1034 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (mode
);
1035 int i_f_bits
= GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
);
1036 unsigned int fbit
= GET_MODE_FBIT (mode
);
1037 enum fixed_value_range_code temp
;
1041 real_2expN (&base_value
, fbit
, mode
);
1042 real_arithmetic (&fixed_value
, MULT_EXPR
, &real_value
, &base_value
);
1043 real_to_integer2 ((HOST_WIDE_INT
*)&f
->data
.low
, &f
->data
.high
, &fixed_value
);
1044 temp
= check_real_for_fixed_mode (&real_value
, mode
);
1045 if (temp
== FIXED_UNDERFLOW
) /* Minimum. */
1058 f
->data
= f
->data
.alshift (i_f_bits
, HOST_BITS_PER_DOUBLE_INT
);
1059 f
->data
= f
->data
.sext (1 + i_f_bits
);
1065 else if (temp
== FIXED_GT_MAX_EPS
|| temp
== FIXED_MAX_EPS
) /* Maximum. */
1071 f
->data
= f
->data
.zext (i_f_bits
);
1076 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
1080 /* Convert to a new real mode from a fixed-point. */
1083 real_convert_from_fixed (REAL_VALUE_TYPE
*r
, enum machine_mode mode
,
1084 const FIXED_VALUE_TYPE
*f
)
1086 REAL_VALUE_TYPE base_value
, fixed_value
, real_value
;
1088 real_2expN (&base_value
, GET_MODE_FBIT (f
->mode
), f
->mode
);
1089 real_from_integer (&fixed_value
, VOIDmode
, f
->data
.low
, f
->data
.high
,
1090 UNSIGNED_FIXED_POINT_MODE_P (f
->mode
));
1091 real_arithmetic (&real_value
, RDIV_EXPR
, &fixed_value
, &base_value
);
1092 real_convert (r
, mode
, &real_value
);
1095 /* Determine whether a fixed-point value F is negative. */
1098 fixed_isneg (const FIXED_VALUE_TYPE
*f
)
1100 if (SIGNED_FIXED_POINT_MODE_P (f
->mode
))
1102 int i_f_bits
= GET_MODE_IBIT (f
->mode
) + GET_MODE_FBIT (f
->mode
);
1103 int sign_bit
= get_fixed_sign_bit (f
->data
, i_f_bits
);