comparison gcc/double-int.c @ 67:f6334be47118

update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
author nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
date Tue, 22 Mar 2011 17:18:12 +0900
parents b7f97abdc517
children 04ced10e8804
comparison
equal deleted inserted replaced
65:65488c3d617d 67:f6334be47118
18 <http://www.gnu.org/licenses/>. */ 18 <http://www.gnu.org/licenses/>. */
19 19
20 #include "config.h" 20 #include "config.h"
21 #include "system.h" 21 #include "system.h"
22 #include "coretypes.h" 22 #include "coretypes.h"
23 #include "tm.h" 23 #include "tm.h" /* For SHIFT_COUNT_TRUNCATED. */
24 #include "tree.h" 24 #include "tree.h"
25 25
26 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring 26 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
27 overflow. Suppose A, B and SUM have the same respective signs as A1, B1, 27 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
28 and SUM1. Then this yields nonzero if overflow occurred during the 28 and SUM1. Then this yields nonzero if overflow occurred during the
65 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low, 65 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
66 HOST_WIDE_INT *hi) 66 HOST_WIDE_INT *hi)
67 { 67 {
68 *low = words[0] + words[1] * BASE; 68 *low = words[0] + words[1] * BASE;
69 *hi = words[2] + words[3] * BASE; 69 *hi = words[2] + words[3] * BASE;
70 }
71
72 /* Force the double-word integer L1, H1 to be within the range of the
73 integer type TYPE. Stores the properly truncated and sign-extended
74 double-word integer in *LV, *HV. Returns true if the operation
75 overflows, that is, argument and result are different. */
76
77 int
78 fit_double_type (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
79 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, const_tree type)
80 {
81 unsigned HOST_WIDE_INT low0 = l1;
82 HOST_WIDE_INT high0 = h1;
83 unsigned int prec = TYPE_PRECISION (type);
84 int sign_extended_type;
85
86 /* Size types *are* sign extended. */
87 sign_extended_type = (!TYPE_UNSIGNED (type)
88 || (TREE_CODE (type) == INTEGER_TYPE
89 && TYPE_IS_SIZETYPE (type)));
90
91 /* First clear all bits that are beyond the type's precision. */
92 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
93 ;
94 else if (prec > HOST_BITS_PER_WIDE_INT)
95 h1 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
96 else
97 {
98 h1 = 0;
99 if (prec < HOST_BITS_PER_WIDE_INT)
100 l1 &= ~((HOST_WIDE_INT) (-1) << prec);
101 }
102
103 /* Then do sign extension if necessary. */
104 if (!sign_extended_type)
105 /* No sign extension */;
106 else if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
107 /* Correct width already. */;
108 else if (prec > HOST_BITS_PER_WIDE_INT)
109 {
110 /* Sign extend top half? */
111 if (h1 & ((unsigned HOST_WIDE_INT)1
112 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
113 h1 |= (HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT);
114 }
115 else if (prec == HOST_BITS_PER_WIDE_INT)
116 {
117 if ((HOST_WIDE_INT)l1 < 0)
118 h1 = -1;
119 }
120 else
121 {
122 /* Sign extend bottom half? */
123 if (l1 & ((unsigned HOST_WIDE_INT)1 << (prec - 1)))
124 {
125 h1 = -1;
126 l1 |= (HOST_WIDE_INT)(-1) << prec;
127 }
128 }
129
130 *lv = l1;
131 *hv = h1;
132
133 /* If the value didn't fit, signal overflow. */
134 return l1 != low0 || h1 != high0;
135 }
136
137 /* We force the double-int HIGH:LOW to the range of the type TYPE by
138 sign or zero extending it.
139 OVERFLOWABLE indicates if we are interested
140 in overflow of the value, when >0 we are only interested in signed
141 overflow, for <0 we are interested in any overflow. OVERFLOWED
142 indicates whether overflow has already occurred. CONST_OVERFLOWED
143 indicates whether constant overflow has already occurred. We force
144 T's value to be within range of T's type (by setting to 0 or 1 all
145 the bits outside the type's range). We set TREE_OVERFLOWED if,
146 OVERFLOWED is nonzero,
147 or OVERFLOWABLE is >0 and signed overflow occurs
148 or OVERFLOWABLE is <0 and any overflow occurs
149 We return a new tree node for the extended double-int. The node
150 is shared if no overflow flags are set. */
151
152 tree
153 force_fit_type_double (tree type, unsigned HOST_WIDE_INT low,
154 HOST_WIDE_INT high, int overflowable,
155 bool overflowed)
156 {
157 int sign_extended_type;
158 bool overflow;
159
160 /* Size types *are* sign extended. */
161 sign_extended_type = (!TYPE_UNSIGNED (type)
162 || (TREE_CODE (type) == INTEGER_TYPE
163 && TYPE_IS_SIZETYPE (type)));
164
165 overflow = fit_double_type (low, high, &low, &high, type);
166
167 /* If we need to set overflow flags, return a new unshared node. */
168 if (overflowed || overflow)
169 {
170 if (overflowed
171 || overflowable < 0
172 || (overflowable > 0 && sign_extended_type))
173 {
174 tree t = make_node (INTEGER_CST);
175 TREE_INT_CST_LOW (t) = low;
176 TREE_INT_CST_HIGH (t) = high;
177 TREE_TYPE (t) = type;
178 TREE_OVERFLOW (t) = 1;
179 return t;
180 }
181 }
182
183 /* Else build a shared node. */
184 return build_int_cst_wide (type, low, high);
185 } 70 }
186 71
187 /* Add two doubleword integers with doubleword result. 72 /* Add two doubleword integers with doubleword result.
188 Return nonzero if the operation overflows according to UNSIGNED_P. 73 Return nonzero if the operation overflows according to UNSIGNED_P.
189 Each argument is given as two `HOST_WIDE_INT' pieces. 74 Each argument is given as two `HOST_WIDE_INT' pieces.
428 { 313 {
429 *hv = signmask; 314 *hv = signmask;
430 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count)); 315 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
431 *lv |= signmask << (prec - count); 316 *lv |= signmask << (prec - count);
432 } 317 }
433 }
434
435 /* Rotate the doubleword integer in L1, H1 left by COUNT places
436 keeping only PREC bits of result.
437 Rotate right if COUNT is negative.
438 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
439
440 void
441 lrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
442 HOST_WIDE_INT count, unsigned int prec,
443 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
444 {
445 unsigned HOST_WIDE_INT s1l, s2l;
446 HOST_WIDE_INT s1h, s2h;
447
448 count %= prec;
449 if (count < 0)
450 count += prec;
451
452 lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
453 rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
454 *lv = s1l | s2l;
455 *hv = s1h | s2h;
456 }
457
458 /* Rotate the doubleword integer in L1, H1 left by COUNT places
459 keeping only PREC bits of result. COUNT must be positive.
460 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
461
462 void
463 rrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
464 HOST_WIDE_INT count, unsigned int prec,
465 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
466 {
467 unsigned HOST_WIDE_INT s1l, s2l;
468 HOST_WIDE_INT s1h, s2h;
469
470 count %= prec;
471 if (count < 0)
472 count += prec;
473
474 rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
475 lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
476 *lv = s1l | s2l;
477 *hv = s1h | s2h;
478 } 318 }
479 319
480 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN 320 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
481 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM). 321 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
482 CODE is a tree code for a kind of division, one of 322 CODE is a tree code for a kind of division, one of
840 } 680 }
841 681
842 return r; 682 return r;
843 } 683 }
844 684
845 /* Returns true if CST fits in unsigned HOST_WIDE_INT. */
846
847 bool
848 double_int_fits_in_uhwi_p (double_int cst)
849 {
850 return cst.high == 0;
851 }
852
853 /* Returns true if CST fits in signed HOST_WIDE_INT. */ 685 /* Returns true if CST fits in signed HOST_WIDE_INT. */
854 686
855 bool 687 bool
856 double_int_fits_in_shwi_p (double_int cst) 688 double_int_fits_in_shwi_p (double_int cst)
857 { 689 {
873 return double_int_fits_in_uhwi_p (cst); 705 return double_int_fits_in_uhwi_p (cst);
874 else 706 else
875 return double_int_fits_in_shwi_p (cst); 707 return double_int_fits_in_shwi_p (cst);
876 } 708 }
877 709
878 /* Returns value of CST as a signed number. CST must satisfy
879 double_int_fits_in_shwi_p. */
880
881 HOST_WIDE_INT
882 double_int_to_shwi (double_int cst)
883 {
884 return (HOST_WIDE_INT) cst.low;
885 }
886
887 /* Returns value of CST as an unsigned number. CST must satisfy
888 double_int_fits_in_uhwi_p. */
889
890 unsigned HOST_WIDE_INT
891 double_int_to_uhwi (double_int cst)
892 {
893 return cst.low;
894 }
895
896 /* Returns A * B. */ 710 /* Returns A * B. */
897 711
898 double_int 712 double_int
899 double_int_mul (double_int a, double_int b) 713 double_int_mul (double_int a, double_int b)
900 { 714 {
901 double_int ret; 715 double_int ret;
902 mul_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high); 716 mul_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high);
903 return ret; 717 return ret;
904 } 718 }
905 719
720 /* Returns A * B. If the operation overflows according to UNSIGNED_P,
721 *OVERFLOW is set to nonzero. */
722
723 double_int
724 double_int_mul_with_sign (double_int a, double_int b,
725 bool unsigned_p, int *overflow)
726 {
727 double_int ret;
728 *overflow = mul_double_with_sign (a.low, a.high, b.low, b.high,
729 &ret.low, &ret.high, unsigned_p);
730 return ret;
731 }
732
906 /* Returns A + B. */ 733 /* Returns A + B. */
907 734
908 double_int 735 double_int
909 double_int_add (double_int a, double_int b) 736 double_int_add (double_int a, double_int b)
910 { 737 {
911 double_int ret; 738 double_int ret;
739 add_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high);
740 return ret;
741 }
742
743 /* Returns A - B. */
744
745 double_int
746 double_int_sub (double_int a, double_int b)
747 {
748 double_int ret;
749 neg_double (b.low, b.high, &b.low, &b.high);
912 add_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high); 750 add_double (a.low, a.high, b.low, b.high, &ret.low, &ret.high);
913 return ret; 751 return ret;
914 } 752 }
915 753
916 /* Returns -A. */ 754 /* Returns -A. */
1023 a.high |= (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT); 861 a.high |= (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
1024 862
1025 return a; 863 return a;
1026 } 864 }
1027 865
866 /* Count trailing zeros in A. */
867 int
868 double_int_ctz (double_int a)
869 {
870 unsigned HOST_WIDE_INT w = a.low ? a.low : (unsigned HOST_WIDE_INT) a.high;
871 unsigned bits = a.low ? 0 : HOST_BITS_PER_WIDE_INT;
872 if (!w)
873 return HOST_BITS_PER_DOUBLE_INT;
874 bits += ctz_hwi (w);
875 return bits;
876 }
877
1028 /* Shift A left by COUNT places keeping only PREC bits of result. Shift 878 /* Shift A left by COUNT places keeping only PREC bits of result. Shift
1029 right if COUNT is negative. ARITH true specifies arithmetic shifting; 879 right if COUNT is negative. ARITH true specifies arithmetic shifting;
1030 otherwise use logical shift. */ 880 otherwise use logical shift. */
1031 881
1032 double_int 882 double_int
1045 double_int_rshift (double_int a, HOST_WIDE_INT count, unsigned int prec, bool arith) 895 double_int_rshift (double_int a, HOST_WIDE_INT count, unsigned int prec, bool arith)
1046 { 896 {
1047 double_int ret; 897 double_int ret;
1048 rshift_double (a.low, a.high, count, prec, &ret.low, &ret.high, arith); 898 rshift_double (a.low, a.high, count, prec, &ret.low, &ret.high, arith);
1049 return ret; 899 return ret;
900 }
901
902 /* Rotate A left by COUNT places keeping only PREC bits of result.
903 Rotate right if COUNT is negative. */
904
905 double_int
906 double_int_lrotate (double_int a, HOST_WIDE_INT count, unsigned int prec)
907 {
908 double_int t1, t2;
909
910 count %= prec;
911 if (count < 0)
912 count += prec;
913
914 t1 = double_int_lshift (a, count, prec, false);
915 t2 = double_int_rshift (a, prec - count, prec, false);
916
917 return double_int_ior (t1, t2);
918 }
919
920 /* Rotate A rigth by COUNT places keeping only PREC bits of result.
921 Rotate right if COUNT is negative. */
922
923 double_int
924 double_int_rrotate (double_int a, HOST_WIDE_INT count, unsigned int prec)
925 {
926 double_int t1, t2;
927
928 count %= prec;
929 if (count < 0)
930 count += prec;
931
932 t1 = double_int_rshift (a, count, prec, false);
933 t2 = double_int_lshift (a, prec - count, prec, false);
934
935 return double_int_ior (t1, t2);
1050 } 936 }
1051 937
1052 /* Returns -1 if A < B, 0 if A == B and 1 if A > B. Signedness of the 938 /* Returns -1 if A < B, 0 if A == B and 1 if A > B. Signedness of the
1053 comparison is given by UNS. */ 939 comparison is given by UNS. */
1054 940
1093 return -1; 979 return -1;
1094 if (a.low > b.low) 980 if (a.low > b.low)
1095 return 1; 981 return 1;
1096 982
1097 return 0; 983 return 0;
984 }
985
986 /* Compares two values A and B. Returns max value. Signedness of the
987 comparison is given by UNS. */
988
989 double_int
990 double_int_max (double_int a, double_int b, bool uns)
991 {
992 return (double_int_cmp (a, b, uns) == 1) ? a : b;
993 }
994
995 /* Compares two signed values A and B. Returns max value. */
996
997 double_int double_int_smax (double_int a, double_int b)
998 {
999 return (double_int_scmp (a, b) == 1) ? a : b;
1000 }
1001
1002 /* Compares two unsigned values A and B. Returns max value. */
1003
1004 double_int double_int_umax (double_int a, double_int b)
1005 {
1006 return (double_int_ucmp (a, b) == 1) ? a : b;
1007 }
1008
1009 /* Compares two values A and B. Returns mix value. Signedness of the
1010 comparison is given by UNS. */
1011
1012 double_int double_int_min (double_int a, double_int b, bool uns)
1013 {
1014 return (double_int_cmp (a, b, uns) == -1) ? a : b;
1015 }
1016
1017 /* Compares two signed values A and B. Returns min value. */
1018
1019 double_int double_int_smin (double_int a, double_int b)
1020 {
1021 return (double_int_scmp (a, b) == -1) ? a : b;
1022 }
1023
1024 /* Compares two unsigned values A and B. Returns min value. */
1025
1026 double_int double_int_umin (double_int a, double_int b)
1027 {
1028 return (double_int_ucmp (a, b) == -1) ? a : b;
1098 } 1029 }
1099 1030
1100 /* Splits last digit of *CST (taken as unsigned) in BASE and returns it. */ 1031 /* Splits last digit of *CST (taken as unsigned) in BASE and returns it. */
1101 1032
1102 static unsigned 1033 static unsigned