re PR tree-optimization/56064 (Optimize VIEW_CONVERT_EXPR with FIXED_CST)
[gcc.git] / gcc / fixed-value.c
1 /* Fixed-point arithmetic support.
2 Copyright (C) 2006-2013 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 "diagnostic-core.h"
26
27 /* Compare two fixed objects for bitwise identity. */
28
29 bool
30 fixed_identical (const FIXED_VALUE_TYPE *a, const FIXED_VALUE_TYPE *b)
31 {
32 return (a->mode == b->mode
33 && a->data.high == b->data.high
34 && a->data.low == b->data.low);
35 }
36
37 /* Calculate a hash value. */
38
39 unsigned int
40 fixed_hash (const FIXED_VALUE_TYPE *f)
41 {
42 return (unsigned int) (f->data.low ^ f->data.high);
43 }
44
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. */
52 };
53
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. */
60
61 static enum fixed_value_range_code
62 check_real_for_fixed_mode (REAL_VALUE_TYPE *real_value, enum machine_mode mode)
63 {
64 REAL_VALUE_TYPE max_value, min_value, epsilon_value;
65
66 real_2expN (&max_value, GET_MODE_IBIT (mode), mode);
67 real_2expN (&epsilon_value, -GET_MODE_FBIT (mode), mode);
68
69 if (SIGNED_FIXED_POINT_MODE_P (mode))
70 min_value = real_value_negate (&max_value);
71 else
72 real_from_string (&min_value, "0.0");
73
74 if (real_compare (LT_EXPR, real_value, &min_value))
75 return FIXED_UNDERFLOW;
76 if (real_compare (EQ_EXPR, real_value, &max_value))
77 return FIXED_MAX_EPS;
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;
81 return FIXED_OK;
82 }
83
84
85 /* Construct a CONST_FIXED from a bit payload and machine mode MODE.
86 The bits in PAYLOAD are used verbatim. */
87
88 FIXED_VALUE_TYPE
89 fixed_from_double_int (double_int payload, enum machine_mode mode)
90 {
91 FIXED_VALUE_TYPE value;
92
93 gcc_assert (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_DOUBLE_INT);
94
95 value.data = payload;
96 value.mode = mode;
97
98 return value;
99 }
100
101
102 /* Initialize from a decimal or hexadecimal string. */
103
104 void
105 fixed_from_string (FIXED_VALUE_TYPE *f, const char *str, enum machine_mode mode)
106 {
107 REAL_VALUE_TYPE real_value, fixed_value, base_value;
108 unsigned int fbit;
109 enum fixed_value_range_code temp;
110
111 f->mode = mode;
112 fbit = GET_MODE_FBIT (mode);
113
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,
125 &fixed_value);
126
127 if (temp == FIXED_MAX_EPS && ALL_FRACT_MODE_P (f->mode))
128 {
129 /* From the spec, we need to evaluate 1 to the maximal value. */
130 f->data.low = -1;
131 f->data.high = -1;
132 f->data = f->data.zext (GET_MODE_FBIT (f->mode)
133 + GET_MODE_IBIT (f->mode));
134 }
135 else
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));
140 }
141
142 /* Render F as a decimal floating point constant. */
143
144 void
145 fixed_to_decimal (char *str, const FIXED_VALUE_TYPE *f_orig,
146 size_t buf_size)
147 {
148 REAL_VALUE_TYPE real_value, base_value, fixed_value;
149
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);
155 }
156
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,
162 and fbits).
163 Return true, if !SAT_P and overflow. */
164
165 static bool
166 fixed_saturate1 (enum machine_mode mode, double_int a, double_int *f,
167 bool sat_p)
168 {
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);
172
173 if (unsigned_p) /* Unsigned type. */
174 {
175 double_int max;
176 max.low = -1;
177 max.high = -1;
178 max = max.zext (i_f_bits);
179 if (a.ugt (max))
180 {
181 if (sat_p)
182 *f = max;
183 else
184 overflow_p = true;
185 }
186 }
187 else /* Signed type. */
188 {
189 double_int max, min;
190 max.high = -1;
191 max.low = -1;
192 max = max.zext (i_f_bits);
193 min.high = 0;
194 min.low = 1;
195 min = min.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
196 min = min.sext (1 + i_f_bits);
197 if (a.sgt (max))
198 {
199 if (sat_p)
200 *f = max;
201 else
202 overflow_p = true;
203 }
204 else if (a.slt (min))
205 {
206 if (sat_p)
207 *f = min;
208 else
209 overflow_p = true;
210 }
211 }
212 return overflow_p;
213 }
214
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,
220 and fbits).
221 Return true, if !SAT_P and overflow. */
222
223 static bool
224 fixed_saturate2 (enum machine_mode mode, double_int a_high, double_int a_low,
225 double_int *f, bool sat_p)
226 {
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);
230
231 if (unsigned_p) /* Unsigned type. */
232 {
233 double_int max_r, max_s;
234 max_r.high = 0;
235 max_r.low = 0;
236 max_s.high = -1;
237 max_s.low = -1;
238 max_s = max_s.zext (i_f_bits);
239 if (a_high.ugt (max_r)
240 || (a_high == max_r &&
241 a_low.ugt (max_s)))
242 {
243 if (sat_p)
244 *f = max_s;
245 else
246 overflow_p = true;
247 }
248 }
249 else /* Signed type. */
250 {
251 double_int max_r, max_s, min_r, min_s;
252 max_r.high = 0;
253 max_r.low = 0;
254 max_s.high = -1;
255 max_s.low = -1;
256 max_s = max_s.zext (i_f_bits);
257 min_r.high = -1;
258 min_r.low = -1;
259 min_s.high = 0;
260 min_s.low = 1;
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 &&
265 a_low.ugt (max_s)))
266 {
267 if (sat_p)
268 *f = max_s;
269 else
270 overflow_p = true;
271 }
272 else if (a_high.slt (min_r)
273 || (a_high == min_r &&
274 a_low.ult (min_s)))
275 {
276 if (sat_p)
277 *f = min_s;
278 else
279 overflow_p = true;
280 }
281 }
282 return overflow_p;
283 }
284
285 /* Return the sign bit based on I_F_BITS. */
286
287 static inline int
288 get_fixed_sign_bit (double_int a, int i_f_bits)
289 {
290 if (i_f_bits < HOST_BITS_PER_WIDE_INT)
291 return (a.low >> i_f_bits) & 1;
292 else
293 return (a.high >> (i_f_bits - HOST_BITS_PER_WIDE_INT)) & 1;
294 }
295
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. */
299
300 static bool
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)
303 {
304 bool overflow_p = false;
305 bool unsigned_p;
306 double_int temp;
307 int i_f_bits;
308
309 /* This was a conditional expression but it triggered a bug in
310 Sun C 5.5. */
311 if (subtract_p)
312 temp = -b->data;
313 else
314 temp = b->data;
315
316 unsigned_p = UNSIGNED_FIXED_POINT_MODE_P (a->mode);
317 i_f_bits = GET_MODE_IBIT (a->mode) + GET_MODE_FBIT (a->mode);
318 f->mode = a->mode;
319 f->data = a->data + temp;
320 if (unsigned_p) /* Unsigned type. */
321 {
322 if (subtract_p) /* Unsigned subtraction. */
323 {
324 if (a->data.ult (b->data))
325 {
326 if (sat_p)
327 {
328 f->data.high = 0;
329 f->data.low = 0;
330 }
331 else
332 overflow_p = true;
333 }
334 }
335 else /* Unsigned addition. */
336 {
337 f->data = f->data.zext (i_f_bits);
338 if (f->data.ult (a->data)
339 || f->data.ult (b->data))
340 {
341 if (sat_p)
342 {
343 f->data.high = -1;
344 f->data.low = -1;
345 }
346 else
347 overflow_p = true;
348 }
349 }
350 }
351 else /* Signed type. */
352 {
353 if ((!subtract_p
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)))
358 || (subtract_p
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))))
363 {
364 if (sat_p)
365 {
366 f->data.low = 1;
367 f->data.high = 0;
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)
370 {
371 --f->data;
372 }
373 }
374 else
375 overflow_p = true;
376 }
377 }
378 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
379 return overflow_p;
380 }
381
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. */
385
386 static bool
387 do_fixed_multiply (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
388 const FIXED_VALUE_TYPE *b, bool sat_p)
389 {
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);
393 f->mode = a->mode;
394 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT)
395 {
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);
400 }
401 else
402 {
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;
407 int carry = 0;
408
409 /* Decompose a and b to four double_int. */
410 a_high.low = a->data.high;
411 a_high.high = 0;
412 a_low.low = a->data.low;
413 a_low.high = 0;
414 b_high.low = b->data.high;
415 b_high.high = 0;
416 b_low.low = b->data.low;
417 b_low.high = 0;
418
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;
424
425 /* Accumulate four results to {r, s}. */
426 temp1.high = high_low.low;
427 temp1.low = 0;
428 s = low_low + temp1;
429 if (s.ult (low_low)
430 || s.ult (temp1))
431 carry ++; /* Carry */
432 temp1.high = s.high;
433 temp1.low = s.low;
434 temp2.high = low_high.low;
435 temp2.low = 0;
436 s = temp1 + temp2;
437 if (s.ult (temp1)
438 || s.ult (temp2))
439 carry ++; /* Carry */
440
441 temp1.low = high_low.high;
442 temp1.high = 0;
443 r = high_high + temp1;
444 temp1.low = low_high.high;
445 temp1.high = 0;
446 r += temp1;
447 temp1.low = carry;
448 temp1.high = 0;
449 r += temp1;
450
451 /* We need to subtract b from r, if a < 0. */
452 if (!unsigned_p && a->data.high < 0)
453 r -= b->data;
454 /* We need to subtract a from r, if b < 0. */
455 if (!unsigned_p && b->data.high < 0)
456 r -= a->data;
457
458 /* Shift right the result by FBIT. */
459 if (GET_MODE_FBIT (f->mode) == HOST_BITS_PER_DOUBLE_INT)
460 {
461 s.low = r.low;
462 s.high = r.high;
463 if (unsigned_p)
464 {
465 r.low = 0;
466 r.high = 0;
467 }
468 else
469 {
470 r.low = -1;
471 r.high = -1;
472 }
473 f->data.low = s.low;
474 f->data.high = s.high;
475 }
476 else
477 {
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;
484 s.low = f->data.low;
485 s.high = f->data.high;
486 r = r.lshift (-GET_MODE_FBIT (f->mode),
487 HOST_BITS_PER_DOUBLE_INT, !unsigned_p);
488 }
489
490 overflow_p = fixed_saturate2 (f->mode, r, s, &f->data, sat_p);
491 }
492
493 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
494 return overflow_p;
495 }
496
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. */
500
501 static bool
502 do_fixed_divide (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a,
503 const FIXED_VALUE_TYPE *b, bool sat_p)
504 {
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);
508 f->mode = a->mode;
509 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT)
510 {
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);
515 }
516 else
517 {
518 double_int pos_a, pos_b, r, s;
519 double_int quo_r, quo_s, mod, temp;
520 int num_of_neg = 0;
521 int i;
522
523 /* If a < 0, negate a. */
524 if (!unsigned_p && a->data.high < 0)
525 {
526 pos_a = -a->data;
527 num_of_neg ++;
528 }
529 else
530 pos_a = a->data;
531
532 /* If b < 0, negate b. */
533 if (!unsigned_p && b->data.high < 0)
534 {
535 pos_b = -b->data;
536 num_of_neg ++;
537 }
538 else
539 pos_b = b->data;
540
541 /* Left shift pos_a to {r, s} by FBIT. */
542 if (GET_MODE_FBIT (f->mode) == HOST_BITS_PER_DOUBLE_INT)
543 {
544 r = pos_a;
545 s.high = 0;
546 s.low = 0;
547 }
548 else
549 {
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);
554 }
555
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;
559
560 for (i = 0; i < HOST_BITS_PER_DOUBLE_INT; i++)
561 {
562 /* Record the leftmost bit of mod. */
563 int leftmost_mod = (mod.high < 0);
564
565 /* Shift left mod by 1 bit. */
566 mod = mod.llshift (1, HOST_BITS_PER_DOUBLE_INT);
567
568 /* Test the leftmost bit of s to add to mod. */
569 if (s.high < 0)
570 mod.low += 1;
571
572 /* Shift left quo_s by 1 bit. */
573 quo_s = quo_s.llshift (1, HOST_BITS_PER_DOUBLE_INT);
574
575 /* Try to calculate (mod - pos_b). */
576 temp = mod - pos_b;
577
578 if (leftmost_mod == 1 || mod.ucmp (pos_b) != -1)
579 {
580 quo_s.low += 1;
581 mod = temp;
582 }
583
584 /* Shift left s by 1 bit. */
585 s = s.llshift (1, HOST_BITS_PER_DOUBLE_INT);
586
587 }
588
589 if (num_of_neg == 1)
590 {
591 quo_s = -quo_s;
592 if (quo_s.high == 0 && quo_s.low == 0)
593 quo_r = -quo_r;
594 else
595 {
596 quo_r.low = ~quo_r.low;
597 quo_r.high = ~quo_r.high;
598 }
599 }
600
601 f->data = quo_s;
602 overflow_p = fixed_saturate2 (f->mode, quo_r, quo_s, &f->data, sat_p);
603 }
604
605 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
606 return overflow_p;
607 }
608
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. */
612
613 static bool
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)
616 {
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);
620 f->mode = a->mode;
621
622 if (b->data.low == 0)
623 {
624 f->data = a->data;
625 return overflow_p;
626 }
627
628 if (GET_MODE_PRECISION (f->mode) <= HOST_BITS_PER_WIDE_INT || (!left_p))
629 {
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);
634 }
635 else /* We need two double_int to store the left-shift result. */
636 {
637 double_int temp_high, temp_low;
638 if (b->data.low == HOST_BITS_PER_DOUBLE_INT)
639 {
640 temp_high = a->data;
641 temp_low.high = 0;
642 temp_low.low = 0;
643 }
644 else
645 {
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);
651 }
652 if (!unsigned_p && a->data.high < 0) /* Signed-extend temp_high. */
653 temp_high = temp_high.ext (b->data.low, unsigned_p);
654 f->data = temp_low;
655 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
656 sat_p);
657 }
658 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
659 return overflow_p;
660 }
661
662 /* Calculate F = -A.
663 If SAT_P, saturate the result to the max or the min.
664 Return true, if !SAT_P and overflow. */
665
666 static bool
667 do_fixed_neg (FIXED_VALUE_TYPE *f, const FIXED_VALUE_TYPE *a, bool sat_p)
668 {
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);
672 f->mode = a->mode;
673 f->data = -a->data;
674 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
675
676 if (unsigned_p) /* Unsigned type. */
677 {
678 if (f->data.low != 0 || f->data.high != 0)
679 {
680 if (sat_p)
681 {
682 f->data.low = 0;
683 f->data.high = 0;
684 }
685 else
686 overflow_p = true;
687 }
688 }
689 else /* Signed type. */
690 {
691 if (!(f->data.high == 0 && f->data.low == 0)
692 && f->data.high == a->data.high && f->data.low == a->data.low )
693 {
694 if (sat_p)
695 {
696 /* Saturate to the maximum by subtracting f->data by one. */
697 f->data.low = -1;
698 f->data.high = -1;
699 f->data = f->data.zext (i_f_bits);
700 }
701 else
702 overflow_p = true;
703 }
704 }
705 return overflow_p;
706 }
707
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. */
712
713 bool
714 fixed_arithmetic (FIXED_VALUE_TYPE *f, int icode, const FIXED_VALUE_TYPE *op0,
715 const FIXED_VALUE_TYPE *op1, bool sat_p)
716 {
717 switch (icode)
718 {
719 case NEGATE_EXPR:
720 return do_fixed_neg (f, op0, sat_p);
721 break;
722
723 case PLUS_EXPR:
724 gcc_assert (op0->mode == op1->mode);
725 return do_fixed_add (f, op0, op1, false, sat_p);
726 break;
727
728 case MINUS_EXPR:
729 gcc_assert (op0->mode == op1->mode);
730 return do_fixed_add (f, op0, op1, true, sat_p);
731 break;
732
733 case MULT_EXPR:
734 gcc_assert (op0->mode == op1->mode);
735 return do_fixed_multiply (f, op0, op1, sat_p);
736 break;
737
738 case TRUNC_DIV_EXPR:
739 gcc_assert (op0->mode == op1->mode);
740 return do_fixed_divide (f, op0, op1, sat_p);
741 break;
742
743 case LSHIFT_EXPR:
744 return do_fixed_shift (f, op0, op1, true, sat_p);
745 break;
746
747 case RSHIFT_EXPR:
748 return do_fixed_shift (f, op0, op1, false, sat_p);
749 break;
750
751 default:
752 gcc_unreachable ();
753 }
754 return false;
755 }
756
757 /* Compare fixed-point values by tree_code.
758 Note that OP0 and OP1 must have the same mode. */
759
760 bool
761 fixed_compare (int icode, const FIXED_VALUE_TYPE *op0,
762 const FIXED_VALUE_TYPE *op1)
763 {
764 enum tree_code code = (enum tree_code) icode;
765 gcc_assert (op0->mode == op1->mode);
766
767 switch (code)
768 {
769 case NE_EXPR:
770 return op0->data != op1->data;
771
772 case EQ_EXPR:
773 return op0->data == op1->data;
774
775 case LT_EXPR:
776 return op0->data.cmp (op1->data,
777 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) == -1;
778
779 case LE_EXPR:
780 return op0->data.cmp (op1->data,
781 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) != 1;
782
783 case GT_EXPR:
784 return op0->data.cmp (op1->data,
785 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) == 1;
786
787 case GE_EXPR:
788 return op0->data.cmp (op1->data,
789 UNSIGNED_FIXED_POINT_MODE_P (op0->mode)) != -1;
790
791 default:
792 gcc_unreachable ();
793 }
794 }
795
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. */
799
800 bool
801 fixed_convert (FIXED_VALUE_TYPE *f, enum machine_mode mode,
802 const FIXED_VALUE_TYPE *a, bool sat_p)
803 {
804 bool overflow_p = false;
805 if (mode == a->mode)
806 {
807 *f = *a;
808 return overflow_p;
809 }
810
811 if (GET_MODE_FBIT (mode) > GET_MODE_FBIT (a->mode))
812 {
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);
825 f->mode = mode;
826 f->data = temp_low;
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,
830 sat_p);
831 else
832 {
833 /* Take care of the cases when converting between signed and
834 unsigned. */
835 if (SIGNED_FIXED_POINT_MODE_P (a->mode))
836 {
837 /* Signed -> Unsigned. */
838 if (a->data.high < 0)
839 {
840 if (sat_p)
841 {
842 f->data.low = 0; /* Set to zero. */
843 f->data.high = 0; /* Set to zero. */
844 }
845 else
846 overflow_p = true;
847 }
848 else
849 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
850 &f->data, sat_p);
851 }
852 else
853 {
854 /* Unsigned -> Signed. */
855 if (temp_high.high < 0)
856 {
857 if (sat_p)
858 {
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. */
865 }
866 else
867 overflow_p = true;
868 }
869 else
870 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
871 &f->data, sat_p);
872 }
873 }
874 }
875 else
876 {
877 /* Right shift a to temp based on a->mode. */
878 double_int temp;
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));
882 f->mode = mode;
883 f->data = temp;
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);
887 else
888 {
889 /* Take care of the cases when converting between signed and
890 unsigned. */
891 if (SIGNED_FIXED_POINT_MODE_P (a->mode))
892 {
893 /* Signed -> Unsigned. */
894 if (a->data.high < 0)
895 {
896 if (sat_p)
897 {
898 f->data.low = 0; /* Set to zero. */
899 f->data.high = 0; /* Set to zero. */
900 }
901 else
902 overflow_p = true;
903 }
904 else
905 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data,
906 sat_p);
907 }
908 else
909 {
910 /* Unsigned -> Signed. */
911 if (temp.high < 0)
912 {
913 if (sat_p)
914 {
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. */
921 }
922 else
923 overflow_p = true;
924 }
925 else
926 overflow_p = fixed_saturate1 (f->mode, f->data, &f->data,
927 sat_p);
928 }
929 }
930 }
931
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));
936 return overflow_p;
937 }
938
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. */
943
944 bool
945 fixed_convert_from_int (FIXED_VALUE_TYPE *f, enum machine_mode mode,
946 double_int a, bool unsigned_p, bool sat_p)
947 {
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)
953 {
954 temp_high = a;
955 temp_low.low = 0;
956 temp_low.high = 0;
957 }
958 else
959 {
960 temp_low = a.llshift (amount, HOST_BITS_PER_DOUBLE_INT);
961
962 /* Logical shift right to temp_high. */
963 temp_high = a.llshift (amount - HOST_BITS_PER_DOUBLE_INT,
964 HOST_BITS_PER_DOUBLE_INT);
965 }
966 if (!unsigned_p && a.high < 0) /* Signed-extend temp_high. */
967 temp_high = temp_high.sext (amount);
968
969 f->mode = mode;
970 f->data = temp_low;
971
972 if (unsigned_p == UNSIGNED_FIXED_POINT_MODE_P (f->mode))
973 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low, &f->data,
974 sat_p);
975 else
976 {
977 /* Take care of the cases when converting between signed and unsigned. */
978 if (!unsigned_p)
979 {
980 /* Signed -> Unsigned. */
981 if (a.high < 0)
982 {
983 if (sat_p)
984 {
985 f->data.low = 0; /* Set to zero. */
986 f->data.high = 0; /* Set to zero. */
987 }
988 else
989 overflow_p = true;
990 }
991 else
992 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
993 &f->data, sat_p);
994 }
995 else
996 {
997 /* Unsigned -> Signed. */
998 if (temp_high.high < 0)
999 {
1000 if (sat_p)
1001 {
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. */
1008 }
1009 else
1010 overflow_p = true;
1011 }
1012 else
1013 overflow_p = fixed_saturate2 (f->mode, temp_high, temp_low,
1014 &f->data, sat_p);
1015 }
1016 }
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));
1021 return overflow_p;
1022 }
1023
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. */
1027
1028 bool
1029 fixed_convert_from_real (FIXED_VALUE_TYPE *f, enum machine_mode mode,
1030 const REAL_VALUE_TYPE *a, bool sat_p)
1031 {
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;
1038
1039 real_value = *a;
1040 f->mode = mode;
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. */
1046 {
1047 if (sat_p)
1048 {
1049 if (unsigned_p)
1050 {
1051 f->data.low = 0;
1052 f->data.high = 0;
1053 }
1054 else
1055 {
1056 f->data.low = 1;
1057 f->data.high = 0;
1058 f->data = f->data.alshift (i_f_bits, HOST_BITS_PER_DOUBLE_INT);
1059 f->data = f->data.sext (1 + i_f_bits);
1060 }
1061 }
1062 else
1063 overflow_p = true;
1064 }
1065 else if (temp == FIXED_GT_MAX_EPS || temp == FIXED_MAX_EPS) /* Maximum. */
1066 {
1067 if (sat_p)
1068 {
1069 f->data.low = -1;
1070 f->data.high = -1;
1071 f->data = f->data.zext (i_f_bits);
1072 }
1073 else
1074 overflow_p = true;
1075 }
1076 f->data = f->data.ext ((!unsigned_p) + i_f_bits, unsigned_p);
1077 return overflow_p;
1078 }
1079
1080 /* Convert to a new real mode from a fixed-point. */
1081
1082 void
1083 real_convert_from_fixed (REAL_VALUE_TYPE *r, enum machine_mode mode,
1084 const FIXED_VALUE_TYPE *f)
1085 {
1086 REAL_VALUE_TYPE base_value, fixed_value, real_value;
1087
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);
1093 }
1094
1095 /* Determine whether a fixed-point value F is negative. */
1096
1097 bool
1098 fixed_isneg (const FIXED_VALUE_TYPE *f)
1099 {
1100 if (SIGNED_FIXED_POINT_MODE_P (f->mode))
1101 {
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);
1104 if (sign_bit == 1)
1105 return true;
1106 }
1107
1108 return false;
1109 }