1 /* Fixed-point arithmetic support.
2 Copyright (C) 2006-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
28 #include "diagnostic-core.h"
30 /* Compare two fixed objects for bitwise identity. */
33 fixed_identical (const FIXED_VALUE_TYPE
*a
, const FIXED_VALUE_TYPE
*b
)
35 return (a
->mode
== b
->mode
36 && a
->data
.high
== b
->data
.high
37 && a
->data
.low
== b
->data
.low
);
40 /* Calculate a hash value. */
43 fixed_hash (const FIXED_VALUE_TYPE
*f
)
45 return (unsigned int) (f
->data
.low
^ f
->data
.high
);
48 /* Define the enum code for the range of the fixed-point value. */
49 enum fixed_value_range_code
{
50 FIXED_OK
, /* The value is within the range. */
51 FIXED_UNDERFLOW
, /* The value is less than the minimum. */
52 FIXED_GT_MAX_EPS
, /* The value is greater than the maximum, but not equal
53 to the maximum plus the epsilon. */
54 FIXED_MAX_EPS
/* The value equals the maximum plus the epsilon. */
57 /* Check REAL_VALUE against the range of the fixed-point mode.
58 Return FIXED_OK, if it is within the range.
59 FIXED_UNDERFLOW, if it is less than the minimum.
60 FIXED_GT_MAX_EPS, if it is greater than the maximum, but not equal to
61 the maximum plus the epsilon.
62 FIXED_MAX_EPS, if it is equal to the maximum plus the epsilon. */
64 static enum fixed_value_range_code
65 check_real_for_fixed_mode (REAL_VALUE_TYPE
*real_value
, machine_mode mode
)
67 REAL_VALUE_TYPE max_value
, min_value
, epsilon_value
;
69 real_2expN (&max_value
, GET_MODE_IBIT (mode
), mode
);
70 real_2expN (&epsilon_value
, -GET_MODE_FBIT (mode
), mode
);
72 if (SIGNED_FIXED_POINT_MODE_P (mode
))
73 min_value
= real_value_negate (&max_value
);
75 real_from_string (&min_value
, "0.0");
77 if (real_compare (LT_EXPR
, real_value
, &min_value
))
78 return FIXED_UNDERFLOW
;
79 if (real_compare (EQ_EXPR
, real_value
, &max_value
))
81 real_arithmetic (&max_value
, MINUS_EXPR
, &max_value
, &epsilon_value
);
82 if (real_compare (GT_EXPR
, real_value
, &max_value
))
83 return FIXED_GT_MAX_EPS
;
88 /* Construct a CONST_FIXED from a bit payload and machine mode MODE.
89 The bits in PAYLOAD are sign-extended/zero-extended according to MODE. */
92 fixed_from_double_int (double_int payload
, machine_mode mode
)
94 FIXED_VALUE_TYPE value
;
96 gcc_assert (GET_MODE_BITSIZE (mode
) <= HOST_BITS_PER_DOUBLE_INT
);
98 if (SIGNED_SCALAR_FIXED_POINT_MODE_P (mode
))
99 value
.data
= payload
.sext (1 + GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
));
100 else if (UNSIGNED_SCALAR_FIXED_POINT_MODE_P (mode
))
101 value
.data
= payload
.zext (GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
));
111 /* Initialize from a decimal or hexadecimal string. */
114 fixed_from_string (FIXED_VALUE_TYPE
*f
, const char *str
, machine_mode mode
)
116 REAL_VALUE_TYPE real_value
, fixed_value
, base_value
;
118 enum fixed_value_range_code temp
;
122 fbit
= GET_MODE_FBIT (mode
);
124 real_from_string (&real_value
, str
);
125 temp
= check_real_for_fixed_mode (&real_value
, f
->mode
);
126 /* We don't want to warn the case when the _Fract value is 1.0. */
127 if (temp
== FIXED_UNDERFLOW
128 || temp
== FIXED_GT_MAX_EPS
129 || (temp
== FIXED_MAX_EPS
&& ALL_ACCUM_MODE_P (f
->mode
)))
130 warning (OPT_Woverflow
,
131 "large fixed-point constant implicitly truncated to fixed-point type");
132 real_2expN (&base_value
, fbit
, mode
);
133 real_arithmetic (&fixed_value
, MULT_EXPR
, &real_value
, &base_value
);
134 wide_int w
= real_to_integer (&fixed_value
, &fail
,
135 GET_MODE_PRECISION (mode
));
136 f
->data
.low
= w
.elt (0);
137 f
->data
.high
= w
.elt (1);
139 if (temp
== FIXED_MAX_EPS
&& ALL_FRACT_MODE_P (f
->mode
))
141 /* From the spec, we need to evaluate 1 to the maximal value. */
144 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
145 + GET_MODE_IBIT (f
->mode
));
148 f
->data
= f
->data
.ext (SIGNED_FIXED_POINT_MODE_P (f
->mode
)
149 + GET_MODE_FBIT (f
->mode
)
150 + GET_MODE_IBIT (f
->mode
),
151 UNSIGNED_FIXED_POINT_MODE_P (f
->mode
));
154 /* Render F as a decimal floating point constant. */
157 fixed_to_decimal (char *str
, const FIXED_VALUE_TYPE
*f_orig
,
160 REAL_VALUE_TYPE real_value
, base_value
, fixed_value
;
162 signop sgn
= UNSIGNED_FIXED_POINT_MODE_P (f_orig
->mode
) ? UNSIGNED
: SIGNED
;
163 real_2expN (&base_value
, GET_MODE_FBIT (f_orig
->mode
), f_orig
->mode
);
164 real_from_integer (&real_value
, VOIDmode
,
165 wide_int::from (f_orig
->data
,
166 GET_MODE_PRECISION (f_orig
->mode
), sgn
),
168 real_arithmetic (&fixed_value
, RDIV_EXPR
, &real_value
, &base_value
);
169 real_to_decimal (str
, &fixed_value
, buf_size
, 0, 1);
172 /* If SAT_P, saturate A to the maximum or the minimum, and save to *F based on
173 the machine mode MODE.
174 Do not modify *F otherwise.
175 This function assumes the width of double_int is greater than the width
176 of the fixed-point value (the sum of a possible sign bit, possible ibits,
178 Return true, if !SAT_P and overflow. */
181 fixed_saturate1 (machine_mode mode
, double_int a
, double_int
*f
,
184 bool overflow_p
= false;
185 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (mode
);
186 int i_f_bits
= GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
);
188 if (unsigned_p
) /* Unsigned type. */
193 max
= max
.zext (i_f_bits
);
202 else /* Signed type. */
207 max
= max
.zext (i_f_bits
);
210 min
= min
.alshift (i_f_bits
, HOST_BITS_PER_DOUBLE_INT
);
211 min
= min
.sext (1 + i_f_bits
);
219 else if (a
.slt (min
))
230 /* If SAT_P, saturate {A_HIGH, A_LOW} to the maximum or the minimum, and
231 save to *F based on the machine mode MODE.
232 Do not modify *F otherwise.
233 This function assumes the width of two double_int is greater than the width
234 of the fixed-point value (the sum of a possible sign bit, possible ibits,
236 Return true, if !SAT_P and overflow. */
239 fixed_saturate2 (machine_mode mode
, double_int a_high
, double_int a_low
,
240 double_int
*f
, bool sat_p
)
242 bool overflow_p
= false;
243 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (mode
);
244 int i_f_bits
= GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
);
246 if (unsigned_p
) /* Unsigned type. */
248 double_int max_r
, max_s
;
253 max_s
= max_s
.zext (i_f_bits
);
254 if (a_high
.ugt (max_r
)
255 || (a_high
== max_r
&&
264 else /* Signed type. */
266 double_int max_r
, max_s
, min_r
, min_s
;
271 max_s
= max_s
.zext (i_f_bits
);
276 min_s
= min_s
.alshift (i_f_bits
, HOST_BITS_PER_DOUBLE_INT
);
277 min_s
= min_s
.sext (1 + i_f_bits
);
278 if (a_high
.sgt (max_r
)
279 || (a_high
== max_r
&&
287 else if (a_high
.slt (min_r
)
288 || (a_high
== min_r
&&
300 /* Return the sign bit based on I_F_BITS. */
303 get_fixed_sign_bit (double_int a
, int i_f_bits
)
305 if (i_f_bits
< HOST_BITS_PER_WIDE_INT
)
306 return (a
.low
>> i_f_bits
) & 1;
308 return (a
.high
>> (i_f_bits
- HOST_BITS_PER_WIDE_INT
)) & 1;
311 /* Calculate F = A + (SUBTRACT_P ? -B : B).
312 If SAT_P, saturate the result to the max or the min.
313 Return true, if !SAT_P and overflow. */
316 do_fixed_add (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
317 const FIXED_VALUE_TYPE
*b
, bool subtract_p
, bool sat_p
)
319 bool overflow_p
= false;
324 /* This was a conditional expression but it triggered a bug in
331 unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
332 i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
334 f
->data
= a
->data
+ temp
;
335 if (unsigned_p
) /* Unsigned type. */
337 if (subtract_p
) /* Unsigned subtraction. */
339 if (a
->data
.ult (b
->data
))
350 else /* Unsigned addition. */
352 f
->data
= f
->data
.zext (i_f_bits
);
353 if (f
->data
.ult (a
->data
)
354 || f
->data
.ult (b
->data
))
366 else /* Signed type. */
369 && (get_fixed_sign_bit (a
->data
, i_f_bits
)
370 == get_fixed_sign_bit (b
->data
, i_f_bits
))
371 && (get_fixed_sign_bit (a
->data
, i_f_bits
)
372 != get_fixed_sign_bit (f
->data
, i_f_bits
)))
374 && (get_fixed_sign_bit (a
->data
, i_f_bits
)
375 != get_fixed_sign_bit (b
->data
, i_f_bits
))
376 && (get_fixed_sign_bit (a
->data
, i_f_bits
)
377 != get_fixed_sign_bit (f
->data
, i_f_bits
))))
383 f
->data
= f
->data
.alshift (i_f_bits
, HOST_BITS_PER_DOUBLE_INT
);
384 if (get_fixed_sign_bit (a
->data
, i_f_bits
) == 0)
393 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
397 /* Calculate F = A * B.
398 If SAT_P, saturate the result to the max or the min.
399 Return true, if !SAT_P and overflow. */
402 do_fixed_multiply (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
403 const FIXED_VALUE_TYPE
*b
, bool sat_p
)
405 bool overflow_p
= false;
406 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
407 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
409 if (GET_MODE_PRECISION (f
->mode
) <= HOST_BITS_PER_WIDE_INT
)
411 f
->data
= a
->data
* b
->data
;
412 f
->data
= f
->data
.lshift (-GET_MODE_FBIT (f
->mode
),
413 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
414 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
418 /* The result of multiplication expands to two double_int. */
419 double_int a_high
, a_low
, b_high
, b_low
;
420 double_int high_high
, high_low
, low_high
, low_low
;
421 double_int r
, s
, temp1
, temp2
;
424 /* Decompose a and b to four double_int. */
425 a_high
.low
= a
->data
.high
;
427 a_low
.low
= a
->data
.low
;
429 b_high
.low
= b
->data
.high
;
431 b_low
.low
= b
->data
.low
;
434 /* Perform four multiplications. */
435 low_low
= a_low
* b_low
;
436 low_high
= a_low
* b_high
;
437 high_low
= a_high
* b_low
;
438 high_high
= a_high
* b_high
;
440 /* Accumulate four results to {r, s}. */
441 temp1
.high
= high_low
.low
;
446 carry
++; /* Carry */
449 temp2
.high
= low_high
.low
;
454 carry
++; /* Carry */
456 temp1
.low
= high_low
.high
;
458 r
= high_high
+ temp1
;
459 temp1
.low
= low_high
.high
;
466 /* We need to subtract b from r, if a < 0. */
467 if (!unsigned_p
&& a
->data
.high
< 0)
469 /* We need to subtract a from r, if b < 0. */
470 if (!unsigned_p
&& b
->data
.high
< 0)
473 /* Shift right the result by FBIT. */
474 if (GET_MODE_FBIT (f
->mode
) == HOST_BITS_PER_DOUBLE_INT
)
489 f
->data
.high
= s
.high
;
493 s
= s
.llshift ((-GET_MODE_FBIT (f
->mode
)), HOST_BITS_PER_DOUBLE_INT
);
494 f
->data
= r
.llshift ((HOST_BITS_PER_DOUBLE_INT
495 - GET_MODE_FBIT (f
->mode
)),
496 HOST_BITS_PER_DOUBLE_INT
);
497 f
->data
.low
= f
->data
.low
| s
.low
;
498 f
->data
.high
= f
->data
.high
| s
.high
;
500 s
.high
= f
->data
.high
;
501 r
= r
.lshift (-GET_MODE_FBIT (f
->mode
),
502 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
505 overflow_p
= fixed_saturate2 (f
->mode
, r
, s
, &f
->data
, sat_p
);
508 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
512 /* Calculate F = A / B.
513 If SAT_P, saturate the result to the max or the min.
514 Return true, if !SAT_P and overflow. */
517 do_fixed_divide (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
518 const FIXED_VALUE_TYPE
*b
, bool sat_p
)
520 bool overflow_p
= false;
521 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
522 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
524 if (GET_MODE_PRECISION (f
->mode
) <= HOST_BITS_PER_WIDE_INT
)
526 f
->data
= a
->data
.lshift (GET_MODE_FBIT (f
->mode
),
527 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
528 f
->data
= f
->data
.div (b
->data
, unsigned_p
, TRUNC_DIV_EXPR
);
529 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
533 double_int pos_a
, pos_b
, r
, s
;
534 double_int quo_r
, quo_s
, mod
, temp
;
538 /* If a < 0, negate a. */
539 if (!unsigned_p
&& a
->data
.high
< 0)
547 /* If b < 0, negate b. */
548 if (!unsigned_p
&& b
->data
.high
< 0)
556 /* Left shift pos_a to {r, s} by FBIT. */
557 if (GET_MODE_FBIT (f
->mode
) == HOST_BITS_PER_DOUBLE_INT
)
565 s
= pos_a
.llshift (GET_MODE_FBIT (f
->mode
), HOST_BITS_PER_DOUBLE_INT
);
566 r
= pos_a
.llshift (- (HOST_BITS_PER_DOUBLE_INT
567 - GET_MODE_FBIT (f
->mode
)),
568 HOST_BITS_PER_DOUBLE_INT
);
571 /* Divide r by pos_b to quo_r. The remainder is in mod. */
572 quo_r
= r
.divmod (pos_b
, 1, TRUNC_DIV_EXPR
, &mod
);
573 quo_s
= double_int_zero
;
575 for (i
= 0; i
< HOST_BITS_PER_DOUBLE_INT
; i
++)
577 /* Record the leftmost bit of mod. */
578 int leftmost_mod
= (mod
.high
< 0);
580 /* Shift left mod by 1 bit. */
581 mod
= mod
.lshift (1);
583 /* Test the leftmost bit of s to add to mod. */
587 /* Shift left quo_s by 1 bit. */
588 quo_s
= quo_s
.lshift (1);
590 /* Try to calculate (mod - pos_b). */
593 if (leftmost_mod
== 1 || mod
.ucmp (pos_b
) != -1)
599 /* Shift left s by 1 bit. */
607 if (quo_s
.high
== 0 && quo_s
.low
== 0)
611 quo_r
.low
= ~quo_r
.low
;
612 quo_r
.high
= ~quo_r
.high
;
617 overflow_p
= fixed_saturate2 (f
->mode
, quo_r
, quo_s
, &f
->data
, sat_p
);
620 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
624 /* Calculate F = A << B if LEFT_P. Otherwise, F = A >> B.
625 If SAT_P, saturate the result to the max or the min.
626 Return true, if !SAT_P and overflow. */
629 do_fixed_shift (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
,
630 const FIXED_VALUE_TYPE
*b
, bool left_p
, bool sat_p
)
632 bool overflow_p
= false;
633 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
634 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
637 if (b
->data
.low
== 0)
643 if (GET_MODE_PRECISION (f
->mode
) <= HOST_BITS_PER_WIDE_INT
|| (!left_p
))
645 f
->data
= a
->data
.lshift (left_p
? b
->data
.low
: -b
->data
.low
,
646 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
647 if (left_p
) /* Only left shift saturates. */
648 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
650 else /* We need two double_int to store the left-shift result. */
652 double_int temp_high
, temp_low
;
653 if (b
->data
.low
== HOST_BITS_PER_DOUBLE_INT
)
661 temp_low
= a
->data
.lshift (b
->data
.low
,
662 HOST_BITS_PER_DOUBLE_INT
, !unsigned_p
);
663 /* Logical shift right to temp_high. */
664 temp_high
= a
->data
.llshift (b
->data
.low
- HOST_BITS_PER_DOUBLE_INT
,
665 HOST_BITS_PER_DOUBLE_INT
);
667 if (!unsigned_p
&& a
->data
.high
< 0) /* Signed-extend temp_high. */
668 temp_high
= temp_high
.ext (b
->data
.low
, unsigned_p
);
670 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
, &f
->data
,
673 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
678 If SAT_P, saturate the result to the max or the min.
679 Return true, if !SAT_P and overflow. */
682 do_fixed_neg (FIXED_VALUE_TYPE
*f
, const FIXED_VALUE_TYPE
*a
, bool sat_p
)
684 bool overflow_p
= false;
685 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (a
->mode
);
686 int i_f_bits
= GET_MODE_IBIT (a
->mode
) + GET_MODE_FBIT (a
->mode
);
689 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
691 if (unsigned_p
) /* Unsigned type. */
693 if (f
->data
.low
!= 0 || f
->data
.high
!= 0)
704 else /* Signed type. */
706 if (!(f
->data
.high
== 0 && f
->data
.low
== 0)
707 && f
->data
.high
== a
->data
.high
&& f
->data
.low
== a
->data
.low
)
711 /* Saturate to the maximum by subtracting f->data by one. */
714 f
->data
= f
->data
.zext (i_f_bits
);
723 /* Perform the binary or unary operation described by CODE.
724 Note that OP0 and OP1 must have the same mode for binary operators.
725 For a unary operation, leave OP1 NULL.
726 Return true, if !SAT_P and overflow. */
729 fixed_arithmetic (FIXED_VALUE_TYPE
*f
, int icode
, const FIXED_VALUE_TYPE
*op0
,
730 const FIXED_VALUE_TYPE
*op1
, bool sat_p
)
735 return do_fixed_neg (f
, op0
, sat_p
);
739 gcc_assert (op0
->mode
== op1
->mode
);
740 return do_fixed_add (f
, op0
, op1
, false, sat_p
);
744 gcc_assert (op0
->mode
== op1
->mode
);
745 return do_fixed_add (f
, op0
, op1
, true, sat_p
);
749 gcc_assert (op0
->mode
== op1
->mode
);
750 return do_fixed_multiply (f
, op0
, op1
, sat_p
);
754 gcc_assert (op0
->mode
== op1
->mode
);
755 return do_fixed_divide (f
, op0
, op1
, sat_p
);
759 return do_fixed_shift (f
, op0
, op1
, true, sat_p
);
763 return do_fixed_shift (f
, op0
, op1
, false, sat_p
);
772 /* Compare fixed-point values by tree_code.
773 Note that OP0 and OP1 must have the same mode. */
776 fixed_compare (int icode
, const FIXED_VALUE_TYPE
*op0
,
777 const FIXED_VALUE_TYPE
*op1
)
779 enum tree_code code
= (enum tree_code
) icode
;
780 gcc_assert (op0
->mode
== op1
->mode
);
785 return op0
->data
!= op1
->data
;
788 return op0
->data
== op1
->data
;
791 return op0
->data
.cmp (op1
->data
,
792 UNSIGNED_FIXED_POINT_MODE_P (op0
->mode
)) == -1;
795 return op0
->data
.cmp (op1
->data
,
796 UNSIGNED_FIXED_POINT_MODE_P (op0
->mode
)) != 1;
799 return op0
->data
.cmp (op1
->data
,
800 UNSIGNED_FIXED_POINT_MODE_P (op0
->mode
)) == 1;
803 return op0
->data
.cmp (op1
->data
,
804 UNSIGNED_FIXED_POINT_MODE_P (op0
->mode
)) != -1;
811 /* Extend or truncate to a new mode.
812 If SAT_P, saturate the result to the max or the min.
813 Return true, if !SAT_P and overflow. */
816 fixed_convert (FIXED_VALUE_TYPE
*f
, machine_mode mode
,
817 const FIXED_VALUE_TYPE
*a
, bool sat_p
)
819 bool overflow_p
= false;
826 if (GET_MODE_FBIT (mode
) > GET_MODE_FBIT (a
->mode
))
828 /* Left shift a to temp_high, temp_low based on a->mode. */
829 double_int temp_high
, temp_low
;
830 int amount
= GET_MODE_FBIT (mode
) - GET_MODE_FBIT (a
->mode
);
831 temp_low
= a
->data
.lshift (amount
,
832 HOST_BITS_PER_DOUBLE_INT
,
833 SIGNED_FIXED_POINT_MODE_P (a
->mode
));
834 /* Logical shift right to temp_high. */
835 temp_high
= a
->data
.llshift (amount
- HOST_BITS_PER_DOUBLE_INT
,
836 HOST_BITS_PER_DOUBLE_INT
);
837 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
)
838 && a
->data
.high
< 0) /* Signed-extend temp_high. */
839 temp_high
= temp_high
.sext (amount
);
842 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
) ==
843 SIGNED_FIXED_POINT_MODE_P (f
->mode
))
844 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
, &f
->data
,
848 /* Take care of the cases when converting between signed and
850 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
))
852 /* Signed -> Unsigned. */
853 if (a
->data
.high
< 0)
857 f
->data
.low
= 0; /* Set to zero. */
858 f
->data
.high
= 0; /* Set to zero. */
864 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
869 /* Unsigned -> Signed. */
870 if (temp_high
.high
< 0)
874 /* Set to maximum. */
875 f
->data
.low
= -1; /* Set to all ones. */
876 f
->data
.high
= -1; /* Set to all ones. */
877 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
878 + GET_MODE_IBIT (f
->mode
));
879 /* Clear the sign. */
885 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
892 /* Right shift a to temp based on a->mode. */
894 temp
= a
->data
.lshift (GET_MODE_FBIT (mode
) - GET_MODE_FBIT (a
->mode
),
895 HOST_BITS_PER_DOUBLE_INT
,
896 SIGNED_FIXED_POINT_MODE_P (a
->mode
));
899 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
) ==
900 SIGNED_FIXED_POINT_MODE_P (f
->mode
))
901 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
, sat_p
);
904 /* Take care of the cases when converting between signed and
906 if (SIGNED_FIXED_POINT_MODE_P (a
->mode
))
908 /* Signed -> Unsigned. */
909 if (a
->data
.high
< 0)
913 f
->data
.low
= 0; /* Set to zero. */
914 f
->data
.high
= 0; /* Set to zero. */
920 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
,
925 /* Unsigned -> Signed. */
930 /* Set to maximum. */
931 f
->data
.low
= -1; /* Set to all ones. */
932 f
->data
.high
= -1; /* Set to all ones. */
933 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
934 + GET_MODE_IBIT (f
->mode
));
935 /* Clear the sign. */
941 overflow_p
= fixed_saturate1 (f
->mode
, f
->data
, &f
->data
,
947 f
->data
= f
->data
.ext (SIGNED_FIXED_POINT_MODE_P (f
->mode
)
948 + GET_MODE_FBIT (f
->mode
)
949 + GET_MODE_IBIT (f
->mode
),
950 UNSIGNED_FIXED_POINT_MODE_P (f
->mode
));
954 /* Convert to a new fixed-point mode from an integer.
955 If UNSIGNED_P, this integer is unsigned.
956 If SAT_P, saturate the result to the max or the min.
957 Return true, if !SAT_P and overflow. */
960 fixed_convert_from_int (FIXED_VALUE_TYPE
*f
, machine_mode mode
,
961 double_int a
, bool unsigned_p
, bool sat_p
)
963 bool overflow_p
= false;
964 /* Left shift a to temp_high, temp_low. */
965 double_int temp_high
, temp_low
;
966 int amount
= GET_MODE_FBIT (mode
);
967 if (amount
== HOST_BITS_PER_DOUBLE_INT
)
975 temp_low
= a
.llshift (amount
, HOST_BITS_PER_DOUBLE_INT
);
977 /* Logical shift right to temp_high. */
978 temp_high
= a
.llshift (amount
- HOST_BITS_PER_DOUBLE_INT
,
979 HOST_BITS_PER_DOUBLE_INT
);
981 if (!unsigned_p
&& a
.high
< 0) /* Signed-extend temp_high. */
982 temp_high
= temp_high
.sext (amount
);
987 if (unsigned_p
== UNSIGNED_FIXED_POINT_MODE_P (f
->mode
))
988 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
, &f
->data
,
992 /* Take care of the cases when converting between signed and unsigned. */
995 /* Signed -> Unsigned. */
1000 f
->data
.low
= 0; /* Set to zero. */
1001 f
->data
.high
= 0; /* Set to zero. */
1007 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
1012 /* Unsigned -> Signed. */
1013 if (temp_high
.high
< 0)
1017 /* Set to maximum. */
1018 f
->data
.low
= -1; /* Set to all ones. */
1019 f
->data
.high
= -1; /* Set to all ones. */
1020 f
->data
= f
->data
.zext (GET_MODE_FBIT (f
->mode
)
1021 + GET_MODE_IBIT (f
->mode
));
1022 /* Clear the sign. */
1028 overflow_p
= fixed_saturate2 (f
->mode
, temp_high
, temp_low
,
1032 f
->data
= f
->data
.ext (SIGNED_FIXED_POINT_MODE_P (f
->mode
)
1033 + GET_MODE_FBIT (f
->mode
)
1034 + GET_MODE_IBIT (f
->mode
),
1035 UNSIGNED_FIXED_POINT_MODE_P (f
->mode
));
1039 /* Convert to a new fixed-point mode from a real.
1040 If SAT_P, saturate the result to the max or the min.
1041 Return true, if !SAT_P and overflow. */
1044 fixed_convert_from_real (FIXED_VALUE_TYPE
*f
, machine_mode mode
,
1045 const REAL_VALUE_TYPE
*a
, bool sat_p
)
1047 bool overflow_p
= false;
1048 REAL_VALUE_TYPE real_value
, fixed_value
, base_value
;
1049 bool unsigned_p
= UNSIGNED_FIXED_POINT_MODE_P (mode
);
1050 int i_f_bits
= GET_MODE_IBIT (mode
) + GET_MODE_FBIT (mode
);
1051 unsigned int fbit
= GET_MODE_FBIT (mode
);
1052 enum fixed_value_range_code temp
;
1057 real_2expN (&base_value
, fbit
, mode
);
1058 real_arithmetic (&fixed_value
, MULT_EXPR
, &real_value
, &base_value
);
1060 wide_int w
= real_to_integer (&fixed_value
, &fail
,
1061 GET_MODE_PRECISION (mode
));
1062 f
->data
.low
= w
.elt (0);
1063 f
->data
.high
= w
.elt (1);
1064 temp
= check_real_for_fixed_mode (&real_value
, mode
);
1065 if (temp
== FIXED_UNDERFLOW
) /* Minimum. */
1078 f
->data
= f
->data
.alshift (i_f_bits
, HOST_BITS_PER_DOUBLE_INT
);
1079 f
->data
= f
->data
.sext (1 + i_f_bits
);
1085 else if (temp
== FIXED_GT_MAX_EPS
|| temp
== FIXED_MAX_EPS
) /* Maximum. */
1091 f
->data
= f
->data
.zext (i_f_bits
);
1096 f
->data
= f
->data
.ext ((!unsigned_p
) + i_f_bits
, unsigned_p
);
1100 /* Convert to a new real mode from a fixed-point. */
1103 real_convert_from_fixed (REAL_VALUE_TYPE
*r
, machine_mode mode
,
1104 const FIXED_VALUE_TYPE
*f
)
1106 REAL_VALUE_TYPE base_value
, fixed_value
, real_value
;
1108 signop sgn
= UNSIGNED_FIXED_POINT_MODE_P (f
->mode
) ? UNSIGNED
: SIGNED
;
1109 real_2expN (&base_value
, GET_MODE_FBIT (f
->mode
), f
->mode
);
1110 real_from_integer (&fixed_value
, VOIDmode
,
1111 wide_int::from (f
->data
, GET_MODE_PRECISION (f
->mode
),
1113 real_arithmetic (&real_value
, RDIV_EXPR
, &fixed_value
, &base_value
);
1114 real_convert (r
, mode
, &real_value
);
1117 /* Determine whether a fixed-point value F is negative. */
1120 fixed_isneg (const FIXED_VALUE_TYPE
*f
)
1122 if (SIGNED_FIXED_POINT_MODE_P (f
->mode
))
1124 int i_f_bits
= GET_MODE_IBIT (f
->mode
) + GET_MODE_FBIT (f
->mode
);
1125 int sign_bit
= get_fixed_sign_bit (f
->data
, i_f_bits
);