]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/fixed-value.c
Reapply reverted change:
[thirdparty/gcc.git] / gcc / fixed-value.c
1 /* Fixed-point arithmetic support.
2 Copyright (C) 2006, 2007 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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
9 version.
10
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
14 for more details.
15
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/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "toplev.h"
26 #include "fixed-value.h"
27
28 /* Compare two fixed objects for bitwise identity. */
29
30 bool
31 fixed_identical (const FIXED_VALUE_TYPE *a, const FIXED_VALUE_TYPE *b)
32 {
33 return (a->mode == b->mode
34 && a->data.high == b->data.high
35 && a->data.low == b->data.low);
36 }
37
38 /* Calculate a hash value. */
39
40 unsigned int
41 fixed_hash (const FIXED_VALUE_TYPE *f)
42 {
43 return (unsigned int) (f->data.low ^ f->data.high);
44 }
45
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. */
53 };
54
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. */
61
62 static enum fixed_value_range_code
63 check_real_for_fixed_mode (REAL_VALUE_TYPE *real_value, enum machine_mode mode)
64 {
65 REAL_VALUE_TYPE max_value, min_value, epsilon_value;
66
67 real_2expN (&max_value, GET_MODE_IBIT (mode), mode);
68 real_2expN (&epsilon_value, -GET_MODE_FBIT (mode), mode);
69
70 if (SIGNED_FIXED_POINT_MODE_P (mode))
71 min_value = REAL_VALUE_NEGATE (max_value);
72 else
73 real_from_string (&min_value, "0.0");
74
75 if (real_compare (LT_EXPR, real_value, &min_value))
76 return FIXED_UNDERFLOW;
77 if (real_compare (EQ_EXPR, real_value, &max_value))
78 return FIXED_MAX_EPS;
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;
82 return FIXED_OK;
83 }
84
85 /* Initialize from a decimal or hexadecimal string. */
86
87 void
88 fixed_from_string (FIXED_VALUE_TYPE *f, const char *str, enum machine_mode mode)
89 {
90 REAL_VALUE_TYPE real_value, fixed_value, base_value;
91 unsigned int fbit;
92 enum fixed_value_range_code temp;
93
94 f->mode = mode;
95 fbit = GET_MODE_FBIT (mode);
96
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,
108 &fixed_value);
109
110 if (temp == FIXED_MAX_EPS && ALL_FRACT_MODE_P (f->mode))
111 {
112 /* From the spec, we need to evaluate 1 to the maximal value. */
113 f->data.low = -1;
114 f->data.high = -1;
115 f->data = double_int_ext (f->data,
116 GET_MODE_FBIT (f->mode)
117 + GET_MODE_IBIT (f->mode), 1);
118 }
119 else
120 f->data = double_int_ext (f->data,
121 SIGNED_FIXED_POINT_MODE_P (f->mode)
122 + GET_MODE_FBIT (f->mode)
123 + GET_MODE_IBIT (f->mode),
124 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
125 }
126
127 /* Render F as a decimal floating point constant. */
128
129 void
130 fixed_to_decimal (char *str, const FIXED_VALUE_TYPE *f_orig,
131 size_t buf_size)
132 {
133 REAL_VALUE_TYPE real_value, base_value, fixed_value;
134
135 real_2expN (&base_value, GET_MODE_FBIT (f_orig->mode), f_orig->mode);
136 real_from_integer (&real_value, VOIDmode, f_orig->data.low, f_orig->data.high,
137 UNSIGNED_FIXED_POINT_MODE_P (f_orig->mode));
138 real_arithmetic (&fixed_value, RDIV_EXPR, &real_value, &base_value);
139 real_to_decimal (str, &fixed_value, buf_size, 0, 1);
140 }
141
142 /* If SAT_P, saturate A to the maximum or the minimum, and save to *F based on
143 the machine mode MODE.
144 Do not modify *F otherwise.
145 This function assumes the width of double_int is greater than the width
146 of the fixed-point value (the sum of a possible sign bit, possible ibits,
147 and fbits).
148 Return true, if !SAT_P and overflow. */
149
150 static bool
151 fixed_saturate1 (enum machine_mode mode, double_int a, double_int *f,
152 bool sat_p)
153 {
154 bool overflow_p = false;
155 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
156 int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
157
158 if (unsigned_p) /* Unsigned type. */
159 {
160 double_int max;
161 max.low = -1;
162 max.high = -1;
163 max = double_int_ext (max, i_f_bits, 1);
164 if (double_int_cmp (a, max, 1) == 1)
165 {
166 if (sat_p)
167 *f = max;
168 else
169 overflow_p = true;
170 }
171 }
172 else /* Signed type. */
173 {
174 double_int max, min;
175 max.high = -1;
176 max.low = -1;
177 max = double_int_ext (max, i_f_bits, 1);
178 min.high = 0;
179 min.low = 1;
180 lshift_double (min.low, min.high, i_f_bits,
181 2 * HOST_BITS_PER_WIDE_INT,
182 &min.low, &min.high, 1);
183 min = double_int_ext (min, 1 + i_f_bits, 0);
184 if (double_int_cmp (a, max, 0) == 1)
185 {
186 if (sat_p)
187 *f = max;
188 else
189 overflow_p = true;
190 }
191 else if (double_int_cmp (a, min, 0) == -1)
192 {
193 if (sat_p)
194 *f = min;
195 else
196 overflow_p = true;
197 }
198 }
199 return overflow_p;
200 }
201
202 /* If SAT_P, saturate {A_HIGH, A_LOW} to the maximum or the minimum, and
203 save to *F based on the machine mode MODE.
204 Do not modify *F otherwise.
205 This function assumes the width of two double_int is greater than the width
206 of the fixed-point value (the sum of a possible sign bit, possible ibits,
207 and fbits).
208 Return true, if !SAT_P and overflow. */
209
210 static bool
211 fixed_saturate2 (enum machine_mode mode, double_int a_high, double_int a_low,
212 double_int *f, bool sat_p)
213 {
214 bool overflow_p = false;
215 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
216 int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
217
218 if (unsigned_p) /* Unsigned type. */
219 {
220 double_int max_r, max_s;
221 max_r.high = 0;
222 max_r.low = 0;
223 max_s.high = -1;
224 max_s.low = -1;
225 max_s = double_int_ext (max_s, i_f_bits, 1);
226 if (double_int_cmp (a_high, max_r, 1) == 1
227 || (double_int_equal_p (a_high, max_r) &&
228 double_int_cmp (a_low, max_s, 1) == 1))
229 {
230 if (sat_p)
231 *f = max_s;
232 else
233 overflow_p = true;
234 }
235 }
236 else /* Signed type. */
237 {
238 double_int max_r, max_s, min_r, min_s;
239 max_r.high = 0;
240 max_r.low = 0;
241 max_s.high = -1;
242 max_s.low = -1;
243 max_s = double_int_ext (max_s, i_f_bits, 1);
244 min_r.high = -1;
245 min_r.low = -1;
246 min_s.high = 0;
247 min_s.low = 1;
248 lshift_double (min_s.low, min_s.high, i_f_bits,
249 2 * HOST_BITS_PER_WIDE_INT,
250 &min_s.low, &min_s.high, 1);
251 min_s = double_int_ext (min_s, 1 + i_f_bits, 0);
252 if (double_int_cmp (a_high, max_r, 0) == 1
253 || (double_int_equal_p (a_high, max_r) &&
254 double_int_cmp (a_low, max_s, 1) == 1))
255 {
256 if (sat_p)
257 *f = max_s;
258 else
259 overflow_p = true;
260 }
261 else if (double_int_cmp (a_high, min_r, 0) == -1
262 || (double_int_equal_p (a_high, min_r) &&
263 double_int_cmp (a_low, min_s, 1) == -1))
264 {
265 if (sat_p)
266 *f = min_s;
267 else
268 overflow_p = true;
269 }
270 }
271 return overflow_p;
272 }
273
274 /* Return the sign bit based on I_F_BITS. */
275
276 static inline int
277 get_fixed_sign_bit (double_int a, int i_f_bits)
278 {
279 if (i_f_bits < HOST_BITS_PER_WIDE_INT)
280 return (a.low >> i_f_bits) & 1;
281 else
282 return (a.high >> (i_f_bits - HOST_BITS_PER_WIDE_INT)) & 1;
283 }
284
285 /* Calculate F = A + (SUBTRACT_P ? -B : B).
286 If SAT_P, saturate the result to the max or the min.
287 Return true, if !SAT_P and overflow. */
288
289 static bool
290 do_fixed_add (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
291 const FIXED_VALUE_TYPE *b, bool subtract_p, bool sat_p)
292 {
293 bool overflow_p = false;
294 double_int temp = subtract_p ? double_int_neg (b->data) : b->data;
295 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
296 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
297 f->mode = a->mode;
298 f->data = double_int_add (a->data, temp);
299 if (unsigned_p) /* Unsigned type. */
300 {
301 if (subtract_p) /* Unsigned subtraction. */
302 {
303 if (double_int_cmp (a->data, b->data, 1) == -1)
304 {
305 if (sat_p)
306 {
307 f->data.high = 0;
308 f->data.low = 0;
309 }
310 else
311 overflow_p = true;
312 }
313 }
314 else /* Unsigned addition. */
315 {
316 f->data = double_int_ext (f->data, i_f_bits, 1);
317 if (double_int_cmp (f->data, a->data, 1) == -1
318 || double_int_cmp (f->data, b->data, 1) == -1)
319 {
320 if (sat_p)
321 {
322 f->data.high = -1;
323 f->data.low = -1;
324 }
325 else
326 overflow_p = true;
327 }
328 }
329 }
330 else /* Signed type. */
331 {
332 if ((!subtract_p
333 && (get_fixed_sign_bit (a->data, i_f_bits)
334 == get_fixed_sign_bit (b->data, i_f_bits))
335 && (get_fixed_sign_bit (a->data, i_f_bits)
336 != get_fixed_sign_bit (f->data, i_f_bits)))
337 || (subtract_p
338 && (get_fixed_sign_bit (a->data, i_f_bits)
339 != get_fixed_sign_bit (b->data, i_f_bits))
340 && (get_fixed_sign_bit (a->data, i_f_bits)
341 != get_fixed_sign_bit (f->data, i_f_bits))))
342 {
343 if (sat_p)
344 {
345 f->data.low = 1;
346 f->data.high = 0;
347 lshift_double (f->data.low, f->data.high, i_f_bits,
348 2 * HOST_BITS_PER_WIDE_INT,
349 &f->data.low, &f->data.high, 1);
350 if (get_fixed_sign_bit (a->data, i_f_bits) == 0)
351 {
352 double_int one;
353 one.low = 1;
354 one.high = 0;
355 f->data = double_int_add (f->data, double_int_neg (one));
356 }
357 }
358 else
359 overflow_p = true;
360 }
361 }
362 f->data = double_int_ext (f->data, (!unsigned_p) + i_f_bits, unsigned_p);
363 return overflow_p;
364 }
365
366 /* Calculate F = A * B.
367 If SAT_P, saturate the result to the max or the min.
368 Return true, if !SAT_P and overflow. */
369
370 static bool
371 do_fixed_multiply (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
372 const FIXED_VALUE_TYPE *b, bool sat_p)
373 {
374 bool overflow_p = false;
375 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
376 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
377 f->mode = a->mode;
378 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT)
379 {
380 f->data = double_int_mul (a->data, b->data);
381 lshift_double (f->data.low, f->data.high,
382 (-GET_MODE_FBIT (f->mode)),
383 2 * HOST_BITS_PER_WIDE_INT,
384 &f->data.low, &f->data.high, !unsigned_p);
385 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
386 }
387 else
388 {
389 /* The result of multiplication expands to two double_int. */
390 double_int a_high, a_low, b_high, b_low;
391 double_int high_high, high_low, low_high, low_low;
392 double_int r, s, temp1, temp2;
393 int carry = 0;
394
395 /* Decompose a and b to four double_int. */
396 a_high.low = a->data.high;
397 a_high.high = 0;
398 a_low.low = a->data.low;
399 a_low.high = 0;
400 b_high.low = b->data.high;
401 b_high.high = 0;
402 b_low.low = b->data.low;
403 b_low.high = 0;
404
405 /* Perform four multiplications. */
406 low_low = double_int_mul (a_low, b_low);
407 low_high = double_int_mul (a_low, b_high);
408 high_low = double_int_mul (a_high, b_low);
409 high_high = double_int_mul (a_high, b_high);
410
411 /* Accumulate four results to {r, s}. */
412 temp1.high = high_low.low;
413 temp1.low = 0;
414 s = double_int_add (low_low, temp1);
415 if (double_int_cmp (s, low_low, 1) == -1
416 || double_int_cmp (s, temp1, 1) == -1)
417 carry ++; /* Carry */
418 temp1.high = s.high;
419 temp1.low = s.low;
420 temp2.high = low_high.low;
421 temp2.low = 0;
422 s = double_int_add (temp1, temp2);
423 if (double_int_cmp (s, temp1, 1) == -1
424 || double_int_cmp (s, temp2, 1) == -1)
425 carry ++; /* Carry */
426
427 temp1.low = high_low.high;
428 temp1.high = 0;
429 r = double_int_add (high_high, temp1);
430 temp1.low = low_high.high;
431 temp1.high = 0;
432 r = double_int_add (r, temp1);
433 temp1.low = carry;
434 temp1.high = 0;
435 r = double_int_add (r, temp1);
436
437 /* We need to add neg(b) to r, if a < 0. */
438 if (!unsigned_p && a->data.high < 0)
439 r = double_int_add (r, double_int_neg (b->data));
440 /* We need to add neg(a) to r, if b < 0. */
441 if (!unsigned_p && b->data.high < 0)
442 r = double_int_add (r, double_int_neg (a->data));
443
444 /* Shift right the result by FBIT. */
445 if (GET_MODE_FBIT (f->mode) == 2 * HOST_BITS_PER_WIDE_INT)
446 {
447 s.low = r.low;
448 s.high = r.high;
449 if (unsigned_p)
450 {
451 r.low = 0;
452 r.high = 0;
453 }
454 else
455 {
456 r.low = -1;
457 r.high = -1;
458 }
459 f->data.low = s.low;
460 f->data.high = s.high;
461 }
462 else
463 {
464 lshift_double (s.low, s.high,
465 (-GET_MODE_FBIT (f->mode)),
466 2 * HOST_BITS_PER_WIDE_INT,
467 &s.low, &s.high, 0);
468 lshift_double (r.low, r.high,
469 (2 * HOST_BITS_PER_WIDE_INT
470 - GET_MODE_FBIT (f->mode)),
471 2 * HOST_BITS_PER_WIDE_INT,
472 &f->data.low, &f->data.high, 0);
473 f->data.low = f->data.low | s.low;
474 f->data.high = f->data.high | s.high;
475 s.low = f->data.low;
476 s.high = f->data.high;
477 lshift_double (r.low, r.high,
478 (-GET_MODE_FBIT (f->mode)),
479 2 * HOST_BITS_PER_WIDE_INT,
480 &r.low, &r.high, !unsigned_p);
481 }
482
483 overflow_p = fixed_saturate2 (f->mode, r, s, &f->data, sat_p);
484 }
485
486 f->data = double_int_ext (f->data, (!unsigned_p) + i_f_bits, unsigned_p);
487 return overflow_p;
488 }
489
490 /* Calculate F = A / B.
491 If SAT_P, saturate the result to the max or the min.
492 Return true, if !SAT_P and overflow. */
493
494 static bool
495 do_fixed_divide (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
496 const FIXED_VALUE_TYPE *b, bool sat_p)
497 {
498 bool overflow_p = false;
499 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
500 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
501 f->mode = a->mode;
502 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT)
503 {
504 lshift_double (a->data.low, a->data.high,
505 GET_MODE_FBIT (f->mode),
506 2 * HOST_BITS_PER_WIDE_INT,
507 &f->data.low, &f->data.high, !unsigned_p);
508 f->data = double_int_div (f->data, b->data, unsigned_p, TRUNC_DIV_EXPR);
509 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
510 }
511 else
512 {
513 double_int pos_a, pos_b, r, s;
514 double_int quo_r, quo_s, mod, temp;
515 int num_of_neg = 0;
516 int i;
517
518 /* If a < 0, negate a. */
519 if (!unsigned_p && a->data.high < 0)
520 {
521 pos_a = double_int_neg (a->data);
522 num_of_neg ++;
523 }
524 else
525 pos_a = a->data;
526
527 /* If b < 0, negate b. */
528 if (!unsigned_p && b->data.high < 0)
529 {
530 pos_b = double_int_neg (b->data);
531 num_of_neg ++;
532 }
533 else
534 pos_b = b->data;
535
536 /* Left shift pos_a to {r, s} by FBIT. */
537 if (GET_MODE_FBIT (f->mode) == 2 * HOST_BITS_PER_WIDE_INT)
538 {
539 r = pos_a;
540 s.high = 0;
541 s.low = 0;
542 }
543 else
544 {
545 lshift_double (pos_a.low, pos_a.high,
546 GET_MODE_FBIT (f->mode),
547 2 * HOST_BITS_PER_WIDE_INT,
548 &s.low, &s.high, 0);
549 lshift_double (pos_a.low, pos_a.high,
550 - (2 * HOST_BITS_PER_WIDE_INT
551 - GET_MODE_FBIT (f->mode)),
552 2 * HOST_BITS_PER_WIDE_INT,
553 &r.low, &r.high, 0);
554 }
555
556 /* Divide r by pos_b to quo_r. The remainder is in mod. */
557 div_and_round_double (TRUNC_DIV_EXPR, 1, r.low, r.high, pos_b.low,
558 pos_b.high, &quo_r.low, &quo_r.high, &mod.low,
559 &mod.high);
560
561 quo_s.high = 0;
562 quo_s.low = 0;
563
564 for (i = 0; i < 2 * HOST_BITS_PER_WIDE_INT; i++)
565 {
566 /* Record the leftmost bit of mod. */
567 int leftmost_mod = (mod.high < 0);
568
569 /* Shift left mod by 1 bit. */
570 lshift_double (mod.low, mod.high, 1, 2 * HOST_BITS_PER_WIDE_INT,
571 &mod.low, &mod.high, 0);
572
573 /* Test the leftmost bit of s to add to mod. */
574 if (s.high < 0)
575 mod.low += 1;
576
577 /* Shift left quo_s by 1 bit. */
578 lshift_double (quo_s.low, quo_s.high, 1, 2 * HOST_BITS_PER_WIDE_INT,
579 &quo_s.low, &quo_s.high, 0);
580
581 /* Try to calculate (mod - pos_b). */
582 temp = double_int_add (mod, double_int_neg (pos_b));
583
584 if (leftmost_mod == 1 || double_int_cmp (mod, pos_b, 1) != -1)
585 {
586 quo_s.low += 1;
587 mod = temp;
588 }
589
590 /* Shift left s by 1 bit. */
591 lshift_double (s.low, s.high, 1, 2 * HOST_BITS_PER_WIDE_INT,
592 &s.low, &s.high, 0);
593
594 }
595
596 if (num_of_neg == 1)
597 {
598 quo_s = double_int_neg (quo_s);
599 if (quo_s.high == 0 && quo_s.low == 0)
600 quo_r = double_int_neg (quo_r);
601 else
602 {
603 quo_r.low = ~quo_r.low;
604 quo_r.high = ~quo_r.high;
605 }
606 }
607
608 f->data = quo_s;
609 overflow_p = fixed_saturate2 (f->mode, quo_r, quo_s, &f->data, sat_p);
610 }
611
612 f->data = double_int_ext (f->data, (!unsigned_p) + i_f_bits, unsigned_p);
613 return overflow_p;
614 }
615
616 /* Calculate F = A << B if LEFT_P. Otherwise, F = A >> B.
617 If SAT_P, saturate the result to the max or the min.
618 Return true, if !SAT_P and overflow. */
619
620 static bool
621 do_fixed_shift (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
622 const FIXED_VALUE_TYPE *b, bool left_p, bool sat_p)
623 {
624 bool overflow_p = false;
625 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
626 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
627 f->mode = a->mode;
628
629 if (b->data.low == 0)
630 {
631 f->data = a->data;
632 return overflow_p;
633 }
634
635 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT || (!left_p))
636 {
637 lshift_double (a->data.low, a->data.high,
638 left_p ? b->data.low : (-b->data.low),
639 2 * HOST_BITS_PER_WIDE_INT,
640 &f->data.low, &f->data.high, !unsigned_p);
641 if (left_p) /* Only left shift saturates. */
642 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
643 }
644 else /* We need two double_int to store the left-shift result. */
645 {
646 double_int temp_high, temp_low;
647 if (b->data.low == 2 * HOST_BITS_PER_WIDE_INT)
648 {
649 temp_high = a->data;
650 temp_low.high = 0;
651 temp_low.low = 0;
652 }
653 else
654 {
655 lshift_double (a->data.low, a->data.high,
656 b->data.low,
657 2 * HOST_BITS_PER_WIDE_INT,
658 &temp_low.low, &temp_low.high, !unsigned_p);
659 /* Logical shift right to temp_high. */
660 lshift_double (a->data.low, a->data.high,
661 b->data.low - 2 * HOST_BITS_PER_WIDE_INT,
662 2 * HOST_BITS_PER_WIDE_INT,
663 &temp_high.low, &temp_high.high, 0);
664 }
665 if (!unsigned_p && a->data.high < 0) /* Signed-extend temp_high. */
666 temp_high = double_int_ext (temp_high, b->data.low, unsigned_p);
667 f->data = temp_low;
668 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
669 sat_p);
670 }
671 f->data = double_int_ext (f->data, (!unsigned_p) + i_f_bits, unsigned_p);
672 return overflow_p;
673 }
674
675 /* Calculate F = -A.
676 If SAT_P, saturate the result to the max or the min.
677 Return true, if !SAT_P and overflow. */
678
679 static bool
680 do_fixed_neg (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a, bool sat_p)
681 {
682 bool overflow_p = false;
683 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
684 int i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
685 f->mode = a->mode;
686 f->data = double_int_neg (a->data);
687 f->data = double_int_ext (f->data, (!unsigned_p) + i_f_bits, unsigned_p);
688
689 if (unsigned_p) /* Unsigned type. */
690 {
691 if (f->data.low != 0 || f->data.high != 0)
692 {
693 if (sat_p)
694 {
695 f->data.low = 0;
696 f->data.high = 0;
697 }
698 else
699 overflow_p = true;
700 }
701 }
702 else /* Signed type. */
703 {
704 if (!(f->data.high == 0 && f->data.low == 0)
705 && f->data.high == a->data.high && f->data.low == a->data.low )
706 {
707 if (sat_p)
708 {
709 /* Saturate to the maximum by subtracting f->data by one. */
710 f->data.low = -1;
711 f->data.high = -1;
712 f->data = double_int_ext (f->data, i_f_bits, 1);
713 }
714 else
715 overflow_p = true;
716 }
717 }
718 return overflow_p;
719 }
720
721 /* Perform the binary or unary operation described by CODE.
722 Note that OP0 and OP1 must have the same mode for binary operators.
723 For a unary operation, leave OP1 NULL.
724 Return true, if !SAT_P and overflow. */
725
726 bool
727 fixed_arithmetic (FIXED_VALUE_TYPE *f, int icode, const FIXED_VALUE_TYPE *op0,
728 const FIXED_VALUE_TYPE *op1, bool sat_p)
729 {
730 switch (icode)
731 {
732 case NEGATE_EXPR:
733 return do_fixed_neg (f, op0, sat_p);
734 break;
735
736 case PLUS_EXPR:
737 gcc_assert (op0->mode == op1->mode);
738 return do_fixed_add (f, op0, op1, false, sat_p);
739 break;
740
741 case MINUS_EXPR:
742 gcc_assert (op0->mode == op1->mode);
743 return do_fixed_add (f, op0, op1, true, sat_p);
744 break;
745
746 case MULT_EXPR:
747 gcc_assert (op0->mode == op1->mode);
748 return do_fixed_multiply (f, op0, op1, sat_p);
749 break;
750
751 case TRUNC_DIV_EXPR:
752 gcc_assert (op0->mode == op1->mode);
753 return do_fixed_divide (f, op0, op1, sat_p);
754 break;
755
756 case LSHIFT_EXPR:
757 return do_fixed_shift (f, op0, op1, true, sat_p);
758 break;
759
760 case RSHIFT_EXPR:
761 return do_fixed_shift (f, op0, op1, false, sat_p);
762 break;
763
764 default:
765 gcc_unreachable ();
766 }
767 return false;
768 }
769
770 /* Compare fixed-point values by tree_code.
771 Note that OP0 and OP1 must have the same mode. */
772
773 bool
774 fixed_compare (int icode, const FIXED_VALUE_TYPE *op0,
775 const FIXED_VALUE_TYPE *op1)
776 {
777 enum tree_code code = icode;
778 gcc_assert (op0->mode == op1->mode);
779
780 switch (code)
781 {
782 case NE_EXPR:
783 return !double_int_equal_p (op0->data, op1->data);
784
785 case EQ_EXPR:
786 return double_int_equal_p (op0->data, op1->data);
787
788 case LT_EXPR:
789 return double_int_cmp (op0->data, op1->data,
790 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) == -1;
791
792 case LE_EXPR:
793 return double_int_cmp (op0->data, op1->data,
794 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) != 1;
795
796 case GT_EXPR:
797 return double_int_cmp (op0->data, op1->data,
798 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) == 1;
799
800 case GE_EXPR:
801 return double_int_cmp (op0->data, op1->data,
802 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) != -1;
803
804 default:
805 gcc_unreachable ();
806 }
807 }
808
809 /* Extend or truncate to a new mode.
810 If SAT_P, saturate the result to the max or the min.
811 Return true, if !SAT_P and overflow. */
812
813 bool
814 fixed_convert (FIXED_VALUE_TYPE *f, enum machine_mode mode,
815 const FIXED_VALUE_TYPE *a, bool sat_p)
816 {
817 bool overflow_p = false;
818 if (mode == a->mode)
819 {
820 *f = *a;
821 return overflow_p;
822 }
823
824 if (GET_MODE_FBIT (mode) > GET_MODE_FBIT (a->mode))
825 {
826 /* Left shift a to temp_high, temp_low based on a->mode. */
827 double_int temp_high, temp_low;
828 int amount = GET_MODE_FBIT (mode) - GET_MODE_FBIT (a->mode);
829 lshift_double (a->data.low, a->data.high,
830 amount,
831 2 * HOST_BITS_PER_WIDE_INT,
832 &temp_low.low, &temp_low.high,
833 SIGNED_FIXED_POINT_MODE_P (a->mode));
834 /* Logical shift right to temp_high. */
835 lshift_double (a->data.low, a->data.high,
836 amount - 2 * HOST_BITS_PER_WIDE_INT,
837 2 * HOST_BITS_PER_WIDE_INT,
838 &temp_high.low, &temp_high.high, 0);
839 if (SIGNED_FIXED_POINT_MODE_P (a->mode)
840 && a->data.high < 0) /* Signed-extend temp_high. */
841 temp_high = double_int_ext (temp_high, amount, 0);
842 f->mode = mode;
843 f->data = temp_low;
844 if (SIGNED_FIXED_POINT_MODE_P (a->mode) ==
845 SIGNED_FIXED_POINT_MODE_P (f->mode))
846 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
847 sat_p);
848 else
849 {
850 /* Take care of the cases when converting between signed and
851 unsigned. */
852 if (SIGNED_FIXED_POINT_MODE_P (a->mode))
853 {
854 /* Signed -> Unsigned. */
855 if (a->data.high < 0)
856 {
857 if (sat_p)
858 {
859 f->data.low = 0; /* Set to zero. */
860 f->data.high = 0; /* Set to zero. */
861 }
862 else
863 overflow_p = true;
864 }
865 else
866 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
867 &f->data, sat_p);
868 }
869 else
870 {
871 /* Unsigned -> Signed. */
872 if (temp_high.high < 0)
873 {
874 if (sat_p)
875 {
876 /* Set to maximum. */
877 f->data.low = -1; /* Set to all ones. */
878 f->data.high = -1; /* Set to all ones. */
879 f->data = double_int_ext (f->data,
880 GET_MODE_FBIT (f->mode)
881 + GET_MODE_IBIT (f->mode),
882 1); /* Clear the sign. */
883 }
884 else
885 overflow_p = true;
886 }
887 else
888 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
889 &f->data, sat_p);
890 }
891 }
892 }
893 else
894 {
895 /* Right shift a to temp based on a->mode. */
896 double_int temp;
897 lshift_double (a->data.low, a->data.high,
898 GET_MODE_FBIT (mode) - GET_MODE_FBIT (a->mode),
899 2 * HOST_BITS_PER_WIDE_INT,
900 &temp.low, &temp.high,
901 SIGNED_FIXED_POINT_MODE_P (a->mode));
902 f->mode = mode;
903 f->data = temp;
904 if (SIGNED_FIXED_POINT_MODE_P (a->mode) ==
905 SIGNED_FIXED_POINT_MODE_P (f->mode))
906 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data, sat_p);
907 else
908 {
909 /* Take care of the cases when converting between signed and
910 unsigned. */
911 if (SIGNED_FIXED_POINT_MODE_P (a->mode))
912 {
913 /* Signed -> Unsigned. */
914 if (a->data.high < 0)
915 {
916 if (sat_p)
917 {
918 f->data.low = 0; /* Set to zero. */
919 f->data.high = 0; /* Set to zero. */
920 }
921 else
922 overflow_p = true;
923 }
924 else
925 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data,
926 sat_p);
927 }
928 else
929 {
930 /* Unsigned -> Signed. */
931 if (temp.high < 0)
932 {
933 if (sat_p)
934 {
935 /* Set to maximum. */
936 f->data.low = -1; /* Set to all ones. */
937 f->data.high = -1; /* Set to all ones. */
938 f->data = double_int_ext (f->data,
939 GET_MODE_FBIT (f->mode)
940 + GET_MODE_IBIT (f->mode),
941 1); /* Clear the sign. */
942 }
943 else
944 overflow_p = true;
945 }
946 else
947 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data,
948 sat_p);
949 }
950 }
951 }
952
953 f->data = double_int_ext (f->data,
954 SIGNED_FIXED_POINT_MODE_P (f->mode)
955 + GET_MODE_FBIT (f->mode)
956 + GET_MODE_IBIT (f->mode),
957 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
958 return overflow_p;
959 }
960
961 /* Convert to a new fixed-point mode from an integer.
962 If UNSIGNED_P, this integer is unsigned.
963 If SAT_P, saturate the result to the max or the min.
964 Return true, if !SAT_P and overflow. */
965
966 bool
967 fixed_convert_from_int (FIXED_VALUE_TYPE *f, enum machine_mode mode,
968 double_int a, bool unsigned_p, bool sat_p)
969 {
970 bool overflow_p = false;
971 /* Left shift a to temp_high, temp_low. */
972 double_int temp_high, temp_low;
973 int amount = GET_MODE_FBIT (mode);
974 if (amount == 2 * HOST_BITS_PER_WIDE_INT)
975 {
976 temp_high = a;
977 temp_low.low = 0;
978 temp_low.high = 0;
979 }
980 else
981 {
982 lshift_double (a.low, a.high,
983 amount,
984 2 * HOST_BITS_PER_WIDE_INT,
985 &temp_low.low, &temp_low.high, 0);
986
987 /* Logical shift right to temp_high. */
988 lshift_double (a.low, a.high,
989 amount - 2 * HOST_BITS_PER_WIDE_INT,
990 2 * HOST_BITS_PER_WIDE_INT,
991 &temp_high.low, &temp_high.high, 0);
992 }
993 if (!unsigned_p && a.high < 0) /* Signed-extend temp_high. */
994 temp_high = double_int_ext (temp_high, amount, 0);
995
996 f->mode = mode;
997 f->data = temp_low;
998
999 if (unsigned_p == UNSIGNED_FIXED_POINT_MODE_P (f->mode))
1000 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
1001 sat_p);
1002 else
1003 {
1004 /* Take care of the cases when converting between signed and unsigned. */
1005 if (!unsigned_p)
1006 {
1007 /* Signed -> Unsigned. */
1008 if (a.high < 0)
1009 {
1010 if (sat_p)
1011 {
1012 f->data.low = 0; /* Set to zero. */
1013 f->data.high = 0; /* Set to zero. */
1014 }
1015 else
1016 overflow_p = true;
1017 }
1018 else
1019 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
1020 &f->data, sat_p);
1021 }
1022 else
1023 {
1024 /* Unsigned -> Signed. */
1025 if (temp_high.high < 0)
1026 {
1027 if (sat_p)
1028 {
1029 /* Set to maximum. */
1030 f->data.low = -1; /* Set to all ones. */
1031 f->data.high = -1; /* Set to all ones. */
1032 f->data = double_int_ext (f->data,
1033 GET_MODE_FBIT (f->mode)
1034 + GET_MODE_IBIT (f->mode),
1035 1); /* Clear the sign. */
1036 }
1037 else
1038 overflow_p = true;
1039 }
1040 else
1041 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
1042 &f->data, sat_p);
1043 }
1044 }
1045 f->data = double_int_ext (f->data,
1046 SIGNED_FIXED_POINT_MODE_P (f->mode)
1047 + GET_MODE_FBIT (f->mode)
1048 + GET_MODE_IBIT (f->mode),
1049 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
1050 return overflow_p;
1051 }
1052
1053 /* Convert to a new fixed-point mode from a real.
1054 If SAT_P, saturate the result to the max or the min.
1055 Return true, if !SAT_P and overflow. */
1056
1057 bool
1058 fixed_convert_from_real (FIXED_VALUE_TYPE *f, enum machine_mode mode,
1059 const REAL_VALUE_TYPE *a, bool sat_p)
1060 {
1061 bool overflow_p = false;
1062 REAL_VALUE_TYPE real_value, fixed_value, base_value;
1063 bool unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (mode);
1064 int i_f_bits = GET_MODE_IBIT (mode) + GET_MODE_FBIT (mode);
1065 unsigned int fbit = GET_MODE_FBIT (mode);
1066 enum fixed_value_range_code temp;
1067
1068 real_value = *a;
1069 f->mode = mode;
1070 real_2expN (&base_value, fbit, mode);
1071 real_arithmetic (&fixed_value, MULT_EXPR, &real_value, &base_value);
1072 real_to_integer2 ((HOST_WIDE_INT *)&f->data.low, &f->data.high, &fixed_value);
1073 temp = check_real_for_fixed_mode (&real_value, mode);
1074 if (temp == FIXED_UNDERFLOW) /* Minimum. */
1075 {
1076 if (sat_p)
1077 {
1078 if (unsigned_p)
1079 {
1080 f->data.low = 0;
1081 f->data.high = 0;
1082 }
1083 else
1084 {
1085 f->data.low = 1;
1086 f->data.high = 0;
1087 lshift_double (f->data.low, f->data.high, i_f_bits,
1088 2 * HOST_BITS_PER_WIDE_INT,
1089 &f->data.low, &f->data.high, 1);
1090 f->data = double_int_ext (f->data, 1 + i_f_bits, 0);
1091 }
1092 }
1093 else
1094 overflow_p = true;
1095 }
1096 else if (temp == FIXED_GT_MAX_EPS || temp == FIXED_MAX_EPS) /* Maximum. */
1097 {
1098 if (sat_p)
1099 {
1100 f->data.low = -1;
1101 f->data.high = -1;
1102 f->data = double_int_ext (f->data, i_f_bits, 1);
1103 }
1104 else
1105 overflow_p = true;
1106 }
1107 f->data = double_int_ext (f->data, (!unsigned_p) + i_f_bits, unsigned_p);
1108 return overflow_p;
1109 }
1110
1111 /* Convert to a new real mode from a fixed-point. */
1112
1113 void
1114 real_convert_from_fixed (REAL_VALUE_TYPE *r, enum machine_mode mode,
1115 const FIXED_VALUE_TYPE *f)
1116 {
1117 REAL_VALUE_TYPE base_value, fixed_value, real_value;
1118
1119 real_2expN (&base_value, GET_MODE_FBIT (f->mode), f->mode);
1120 real_from_integer (&fixed_value, VOIDmode, f->data.low, f->data.high,
1121 UNSIGNED_FIXED_POINT_MODE_P (f->mode));
1122 real_arithmetic (&real_value, RDIV_EXPR, &fixed_value, &base_value);
1123 real_convert (r, mode, &real_value);
1124 }
1125
1126 /* Determine whether a fixed-point value F is negative. */
1127
1128 bool
1129 fixed_isneg (const FIXED_VALUE_TYPE *f)
1130 {
1131 if (SIGNED_FIXED_POINT_MODE_P (f->mode))
1132 {
1133 int i_f_bits = GET_MODE_IBIT (f->mode) + GET_MODE_FBIT (f->mode);
1134 int sign_bit = get_fixed_sign_bit (f->data, i_f_bits);
1135 if (sign_bit == 1)
1136 return true;
1137 }
1138
1139 return false;
1140 }