1 /*
2  * Copyright 2013 The Emscripten Authors.  All rights reserved.
3  * Emscripten is available under two separate licenses, the MIT license and the
4  * University of Illinois/NCSA Open Source License.  Both these licenses can be
5  * found in the LICENSE file.
6  */
7 
8 /*
9  *  Licensed to the Apache Software Foundation (ASF) under one or more
10  *  contributor license agreements.  See the NOTICE file distributed with
11  *  this work for additional information regarding copyright ownership.
12  *  The ASF licenses this file to You under the Apache License, Version 2.0
13  *  (the "License"); you may not use this file except in compliance with
14  *  the License.  You may obtain a copy of the License at
15  *
16  *     http://www.apache.org/licenses/LICENSE-2.0
17  *
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  *
24  *  cbigint.c has been adapted for xmlvm
25  */
26 #include "xmlvm-number.h"
27 
28 
29 U_32
simpleMultiplyHighPrecision(U_64 * arg1,IDATA length,U_64 arg2)30 simpleMultiplyHighPrecision (U_64 * arg1, IDATA length, U_64 arg2)
31 {
32 	/* assumes arg2 only holds 32 bits of information */
33 	U_64 product;
34 	IDATA index;
35 
36 	index = 0;
37 	product = 0;
38 
39 	do
40     {
41 		product =
42         HIGH_IN_U64 (product) + arg2 * LOW_U32_FROM_PTR (arg1 + index);
43 		LOW_U32_FROM_PTR (arg1 + index) = LOW_U32_FROM_VAR (product);
44 		product =
45         HIGH_IN_U64 (product) + arg2 * HIGH_U32_FROM_PTR (arg1 + index);
46 		HIGH_U32_FROM_PTR (arg1 + index) = LOW_U32_FROM_VAR (product);
47     }
48 	while (++index < length);
49 
50 	return HIGH_U32_FROM_VAR (product);
51 }
52 
simpleShiftLeftHighPrecision(U_64 * arg1,IDATA length,IDATA arg2)53 void simpleShiftLeftHighPrecision (U_64 * arg1, IDATA length, IDATA arg2)
54 {
55 	/* assumes length > 0 */
56 	IDATA index, offset;
57 	if (arg2 >= 64)
58     {
59 		offset = arg2 >> 6;
60 		index = length;
61 
62 		while (--index - offset >= 0)
63 			arg1[index] = arg1[index - offset];
64 		do
65         {
66 			arg1[index] = 0;
67         }
68 		while (--index >= 0);
69 
70 		arg2 &= 0x3F;
71     }
72 
73 	if (arg2 == 0)
74 		return;
75 	while (--length > 0)
76     {
77 		arg1[length] = arg1[length] << arg2 | arg1[length - 1] >> (64 - arg2);
78     }
79 	*arg1 <<= arg2;
80 }
81 
82 
simpleMultiplyHighPrecision64(U_64 * arg1,IDATA length,U_64 arg2)83 U_64 simpleMultiplyHighPrecision64 (U_64 * arg1, IDATA length, U_64 arg2)
84 {
85 	U_64 intermediate, *pArg1, carry1, carry2, prod1, prod2, sum;
86 	IDATA index;
87 	U_32 buf32;
88 
89 	index = 0;
90 	intermediate = 0;
91 	pArg1 = arg1 + index;
92 	carry1 = carry2 = 0;
93 
94 	do
95     {
96 		if ((*pArg1 != 0) || (intermediate != 0))
97         {
98 			prod1 =
99             (U_64) LOW_U32_FROM_VAR (arg2) * (U_64) LOW_U32_FROM_PTR (pArg1);
100 			sum = intermediate + prod1;
101 			if ((sum < prod1) || (sum < intermediate))
102             {
103 				carry1 = 1;
104             }
105 			else
106             {
107 				carry1 = 0;
108             }
109 			prod1 =
110             (U_64) LOW_U32_FROM_VAR (arg2) * (U_64) HIGH_U32_FROM_PTR (pArg1);
111 			prod2 =
112             (U_64) HIGH_U32_FROM_VAR (arg2) * (U_64) LOW_U32_FROM_PTR (pArg1);
113 			intermediate = carry2 + HIGH_IN_U64 (sum) + prod1 + prod2;
114 			if ((intermediate < prod1) || (intermediate < prod2))
115             {
116 				carry2 = 1;
117             }
118 			else
119             {
120 				carry2 = 0;
121             }
122 			LOW_U32_FROM_PTR (pArg1) = LOW_U32_FROM_VAR (sum);
123 			buf32 = HIGH_U32_FROM_PTR (pArg1);
124 			HIGH_U32_FROM_PTR (pArg1) = LOW_U32_FROM_VAR (intermediate);
125 			intermediate = carry1 + HIGH_IN_U64 (intermediate)
126             + (U_64) HIGH_U32_FROM_VAR (arg2) * (U_64) buf32;
127         }
128 		pArg1++;
129     }
130 	while (++index < length);
131 	return intermediate;
132 }
133 
simpleAppendDecimalDigitHighPrecision(U_64 * arg1,IDATA length,U_64 digit)134 U_32 simpleAppendDecimalDigitHighPrecision (U_64 * arg1, IDATA length, U_64 digit)
135 {
136 	/* assumes digit is less than 32 bits */
137 	U_64 arg;
138 	IDATA index = 0;
139 
140 	digit <<= 32;
141 	do
142     {
143 		arg = LOW_IN_U64 (arg1[index]);
144 		digit = HIGH_IN_U64 (digit) + TIMES_TEN (arg);
145 		LOW_U32_FROM_PTR (arg1 + index) = LOW_U32_FROM_VAR (digit);
146 
147 		arg = HIGH_IN_U64 (arg1[index]);
148 		digit = HIGH_IN_U64 (digit) + TIMES_TEN (arg);
149 		HIGH_U32_FROM_PTR (arg1 + index) = LOW_U32_FROM_VAR (digit);
150     }
151 	while (++index < length);
152 
153 	return HIGH_U32_FROM_VAR (digit);
154 }
155 
timesTenToTheEHighPrecision(U_64 * result,IDATA length,JAVA_INT e)156 IDATA timesTenToTheEHighPrecision (U_64 * result, IDATA length, JAVA_INT e)
157 {
158 	/* assumes result can hold value */
159 	U_64 overflow;
160 	int exp10 = e;
161 
162 	if (e == 0)
163 		return length;
164 
165 	while (exp10 >= 19)
166     {
167 		overflow = simpleMultiplyHighPrecision64 (result, length, TEN_E19);
168 		if (overflow)
169 			result[length++] = overflow;
170 		exp10 -= 19;
171     }
172 	while (exp10 >= 9)
173     {
174 		overflow = simpleMultiplyHighPrecision (result, length, TEN_E9);
175 		if (overflow)
176 			result[length++] = overflow;
177 		exp10 -= 9;
178     }
179 	if (exp10 == 0)
180 		return length;
181 	else if (exp10 == 1)
182     {
183 		overflow = simpleAppendDecimalDigitHighPrecision (result, length, 0);
184 		if (overflow)
185 			result[length++] = overflow;
186     }
187 	else if (exp10 == 2)
188     {
189 		overflow = simpleAppendDecimalDigitHighPrecision (result, length, 0);
190 		if (overflow)
191 			result[length++] = overflow;
192 		overflow = simpleAppendDecimalDigitHighPrecision (result, length, 0);
193 		if (overflow)
194 			result[length++] = overflow;
195     }
196 	else if (exp10 == 3)
197     {
198 		overflow = simpleMultiplyHighPrecision (result, length, TEN_E3);
199 		if (overflow)
200 			result[length++] = overflow;
201     }
202 	else if (exp10 == 4)
203     {
204 		overflow = simpleMultiplyHighPrecision (result, length, TEN_E4);
205 		if (overflow)
206 			result[length++] = overflow;
207     }
208 	else if (exp10 == 5)
209     {
210 		overflow = simpleMultiplyHighPrecision (result, length, TEN_E5);
211 		if (overflow)
212 			result[length++] = overflow;
213     }
214 	else if (exp10 == 6)
215     {
216 		overflow = simpleMultiplyHighPrecision (result, length, TEN_E6);
217 		if (overflow)
218 			result[length++] = overflow;
219     }
220 	else if (exp10 == 7)
221     {
222 		overflow = simpleMultiplyHighPrecision (result, length, TEN_E7);
223 		if (overflow)
224 			result[length++] = overflow;
225     }
226 	else if (exp10 == 8)
227     {
228 		overflow = simpleMultiplyHighPrecision (result, length, TEN_E8);
229 		if (overflow)
230 			result[length++] = overflow;
231     }
232 	return length;
233 }
234 
addHighPrecision(U_64 * arg1,IDATA length1,U_64 * arg2,IDATA length2)235 IDATA addHighPrecision (U_64 * arg1, IDATA length1, U_64 * arg2, IDATA length2)
236 {
237 
238 	U_64 temp1, temp2, temp3;     /* temporary variables to help the SH-4, and gcc */
239 	U_64 carry;
240 	IDATA index;
241 
242 	if (length1 == 0 || length2 == 0)
243     {
244 		return 0;
245     }
246 	else if (length1 < length2)
247     {
248 		length2 = length1;
249     }
250 
251 	carry = 0;
252 	index = 0;
253 	do
254     {
255 		temp1 = arg1[index];
256 		temp2 = arg2[index];
257 		temp3 = temp1 + temp2;
258 		arg1[index] = temp3 + carry;
259 		if (arg2[index] < arg1[index])
260 			carry = 0;
261 		else if (arg2[index] != arg1[index])
262 			carry = 1;
263     }
264 	while (++index < length2);
265 	if (!carry)
266 		return 0;
267 	else if (index == length1)
268 		return 1;
269 
270 	while (++arg1[index] == 0 && ++index < length1);
271 
272 	return (IDATA) index == length1;
273 }
274 
275 IDATA
compareHighPrecision(U_64 * arg1,IDATA length1,U_64 * arg2,IDATA length2)276 compareHighPrecision (U_64 * arg1, IDATA length1, U_64 * arg2, IDATA length2)
277 {
278 	while (--length1 >= 0 && arg1[length1] == 0);
279 	while (--length2 >= 0 && arg2[length2] == 0);
280 
281 	if (length1 > length2)
282 		return 1;
283 	else if (length1 < length2)
284 		return -1;
285 	else if (length1 > -1)
286     {
287 		do
288         {
289 			if (arg1[length1] > arg2[length1])
290 				return 1;
291 			else if (arg1[length1] < arg2[length1])
292 				return -1;
293         }
294 		while (--length1 >= 0);
295     }
296 
297 	return 0;
298 }
299 
300 IDATA
simpleAddHighPrecision(U_64 * arg1,IDATA length,U_64 arg2)301 simpleAddHighPrecision (U_64 * arg1, IDATA length, U_64 arg2)
302 {
303 	/* assumes length > 0 */
304 	IDATA index = 1;
305 
306 	*arg1 += arg2;
307 	if (arg2 <= *arg1)
308 		return 0;
309 	else if (length == 1)
310 		return 1;
311 
312 	while (++arg1[index] == 0 && ++index < length);
313 
314 	return (IDATA) index == length;
315 }
316 
317 void
subtractHighPrecision(U_64 * arg1,IDATA length1,U_64 * arg2,IDATA length2)318 subtractHighPrecision (U_64 * arg1, IDATA length1, U_64 * arg2, IDATA length2)
319 {
320 	/* assumes arg1 > arg2 */
321 	IDATA index;
322 	for (index = 0; index < length1; ++index)
323 		arg1[index] = ~arg1[index];
324 	simpleAddHighPrecision (arg1, length1, 1);
325 
326 	while (length2 > 0 && arg2[length2 - 1] == 0)
327 		--length2;
328 
329 	addHighPrecision (arg1, length1, arg2, length2);
330 
331 	for (index = 0; index < length1; ++index)
332 		arg1[index] = ~arg1[index];
333 	simpleAddHighPrecision (arg1, length1, 1);
334 }
335