00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #define BITS_PER_UNIT 8
00030
00031 typedef int HItype __attribute__ ((mode (HI)));
00032 typedef unsigned int UHItype __attribute__ ((mode (HI)));
00033
00034 typedef int SItype __attribute__ ((mode (SI)));
00035 typedef unsigned int USItype __attribute__ ((mode (SI)));
00036
00037 typedef int word_type __attribute__ ((mode (__word__)));
00038
00039 struct SIstruct {HItype low, high;};
00040
00041 typedef union
00042 {
00043 struct SIstruct s;
00044 SItype ll;
00045 } SIunion;
00046
00047 SItype
00048 __lshrsi3 (SItype u, word_type b)
00049 {
00050 SIunion w;
00051 word_type bm;
00052 SIunion uu;
00053
00054 if (b == 0)
00055 return u;
00056
00057 uu.ll = u;
00058
00059 bm = (sizeof (HItype) * BITS_PER_UNIT) - b;
00060 if (bm <= 0)
00061 {
00062 w.s.high = 0;
00063 w.s.low = (UHItype)uu.s.high >> -bm;
00064 }
00065 else
00066 {
00067 UHItype carries = (UHItype)uu.s.high << bm;
00068 w.s.high = (UHItype)uu.s.high >> b;
00069 w.s.low = ((UHItype)uu.s.low >> b) | carries;
00070 }
00071
00072 return w.ll;
00073 }
00074
00075 SItype
00076 __ashlsi3 (SItype u, word_type b)
00077 {
00078 SIunion w;
00079 word_type bm;
00080 SIunion uu;
00081
00082 if (b == 0)
00083 return u;
00084
00085 uu.ll = u;
00086
00087 bm = (sizeof (HItype) * BITS_PER_UNIT) - b;
00088 if (bm <= 0)
00089 {
00090 w.s.low = 0;
00091 w.s.high = (UHItype)uu.s.low << -bm;
00092 }
00093 else
00094 {
00095 UHItype carries = (UHItype)uu.s.low >> bm;
00096 w.s.low = (UHItype)uu.s.low << b;
00097 w.s.high = ((UHItype)uu.s.high << b) | carries;
00098 }
00099
00100 return w.ll;
00101 }
00102
00103 SItype
00104 __ashrsi3 (SItype u, word_type b)
00105 {
00106 SIunion w;
00107 word_type bm;
00108 SIunion uu;
00109
00110 if (b == 0)
00111 return u;
00112
00113 uu.ll = u;
00114
00115 bm = (sizeof (HItype) * BITS_PER_UNIT) - b;
00116 if (bm <= 0)
00117 {
00118
00119 w.s.high = uu.s.high >> (sizeof (HItype) * BITS_PER_UNIT - 1);
00120 w.s.low = uu.s.high >> -bm;
00121 }
00122 else
00123 {
00124 UHItype carries = (UHItype)uu.s.high << bm;
00125 w.s.high = uu.s.high >> b;
00126 w.s.low = ((UHItype)uu.s.low >> b) | carries;
00127 }
00128
00129 return w.ll;
00130 }
00131
00132 USItype
00133 __mulsi3 (USItype a, USItype b)
00134 {
00135 USItype c = 0;
00136
00137 while (a != 0)
00138 {
00139 if (a & 1)
00140 c += b;
00141 a >>= 1;
00142 b <<= 1;
00143 }
00144
00145 return c;
00146 }
00147
00148 USItype
00149 udivmodsi4(USItype num, USItype den, word_type modwanted)
00150 {
00151 USItype bit = 1;
00152 USItype res = 0;
00153
00154 while (den < num && bit && !(den & (1L<<31)))
00155 {
00156 den <<=1;
00157 bit <<=1;
00158 }
00159 while (bit)
00160 {
00161 if (num >= den)
00162 {
00163 num -= den;
00164 res |= bit;
00165 }
00166 bit >>=1;
00167 den >>=1;
00168 }
00169 if (modwanted) return num;
00170 return res;
00171 }
00172
00173 SItype
00174 __divsi3 (SItype a, SItype b)
00175 {
00176 word_type neg = 0;
00177 SItype res;
00178
00179 if (a < 0)
00180 {
00181 a = -a;
00182 neg = !neg;
00183 }
00184
00185 if (b < 0)
00186 {
00187 b = -b;
00188 neg = !neg;
00189 }
00190
00191 res = udivmodsi4 (a, b, 0);
00192
00193 if (neg)
00194 res = -res;
00195
00196 return res;
00197 }
00198
00199 SItype
00200 __modsi3 (SItype a, SItype b)
00201 {
00202 word_type neg = 0;
00203 SItype res;
00204
00205 if (a < 0)
00206 {
00207 a = -a;
00208 neg = 1;
00209 }
00210
00211 if (b < 0)
00212 b = -b;
00213
00214 res = udivmodsi4 (a, b, 1);
00215
00216 if (neg)
00217 res = -res;
00218
00219 return res;
00220 }
00221
00222 SItype
00223 __udivsi3 (SItype a, SItype b)
00224 {
00225 return udivmodsi4 (a, b, 0);
00226 }
00227
00228 SItype
00229 __umodsi3 (SItype a, SItype b)
00230 {
00231 return udivmodsi4 (a, b, 1);
00232 }