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