1 /**
2  * This file has no copyright assigned and is placed in the Public Domain.
3  * This file is part of the mingw-w64 runtime package.
4  * No warranty is given; refer to the file DISCLAIMER.PD within this package.
5  */
6 #include "cephes_emath.h"
7 
8 /*
9  * The constants are for 64 bit precision.
10  */
11 
12 
13 /* Move in external format number,
14  * converting it to internal format.
15  */
__emovi(const short unsigned int * __restrict__ a,short unsigned int * __restrict__ b)16 void __emovi(const short unsigned int * __restrict__ a,
17 	     short unsigned int * __restrict__ b)
18 {
19 	register const unsigned short *p;
20 	register unsigned short *q;
21 	int i;
22 
23 	q = b;
24 	p = a + (NE-1);	/* point to last word of external number */
25 	/* get the sign bit */
26 	if (*p & 0x8000)
27 		*q++ = 0xffff;
28 	else
29 		*q++ = 0;
30 	/* get the exponent */
31 	*q = *p--;
32 	*q++ &= 0x7fff;	/* delete the sign bit */
33 #ifdef INFINITY
34 	if ((*(q - 1) & 0x7fff) == 0x7fff)
35 	{
36 #ifdef NANS
37 		if (__eisnan(a))
38 		{
39 			*q++ = 0;
40 			for (i = 3; i < NI; i++ )
41 				*q++ = *p--;
42 			return;
43 		}
44 #endif
45 		for (i = 2; i < NI; i++)
46 			*q++ = 0;
47 		return;
48 	}
49 #endif
50 	/* clear high guard word */
51 	*q++ = 0;
52 	/* move in the significand */
53 	for (i = 0; i < NE - 1; i++ )
54 		*q++ = *p--;
55 	/* clear low guard word */
56 	*q = 0;
57 }
58 
59 
60 /*
61 ;	Add significands
62 ;	x + y replaces y
63 */
64 
__eaddm(const short unsigned int * __restrict__ x,short unsigned int * __restrict__ y)65 void __eaddm(const short unsigned int * __restrict__ x,
66 		  short unsigned int * __restrict__ y)
67 {
68 	register unsigned long a;
69 	int i;
70 	unsigned int carry;
71 
72 	x += NI - 1;
73 	y += NI - 1;
74 	carry = 0;
75 	for (i = M; i < NI; i++)
76 	{
77 		a = (unsigned long)(*x) + (unsigned long)(*y) + carry;
78 		if (a & 0x10000)
79 			carry = 1;
80 		else
81 			carry = 0;
82 		*y = (unsigned short)a;
83 		--x;
84 		--y;
85 	}
86 }
87 
88 /*
89 ;	Subtract significands
90 ;	y - x replaces y
91 */
92 
__esubm(const short unsigned int * __restrict__ x,short unsigned int * __restrict__ y)93 void __esubm(const short unsigned int * __restrict__ x,
94 		  short unsigned int * __restrict__ y)
95 {
96 	unsigned long a;
97 	int i;
98 	unsigned int carry;
99 
100 	x += NI - 1;
101 	y += NI - 1;
102 	carry = 0;
103 	for (i = M; i < NI; i++)
104 	{
105 		a = (unsigned long)(*y) - (unsigned long)(*x) - carry;
106 		if (a & 0x10000)
107 			carry = 1;
108 		else
109 			carry = 0;
110 		*y = (unsigned short)a;
111 		--x;
112 		--y;
113 	}
114 }
115 
116 
117 /* Multiply significand of e-type number b
118 by 16-bit quantity a, e-type result to c. */
119 
__m16m(short unsigned int a,short unsigned int * __restrict__ b,short unsigned int * __restrict__ c)120 static void __m16m(short unsigned int a,
121 		   short unsigned int *  __restrict__ b,
122 		   short unsigned int *  __restrict__ c)
123 {
124 	register unsigned short *pp;
125 	register unsigned long carry;
126 	unsigned short *ps;
127 	unsigned short p[NI];
128 	unsigned long aa, m;
129 	int i;
130 
131 	aa = a;
132 	pp = &p[NI - 2];
133 	*pp++ = 0;
134 	*pp = 0;
135 	ps = &b[NI - 1];
136 
137 	for(i = M + 1; i < NI; i++)
138 	{
139 		if (*ps == 0)
140 		{
141 			--ps;
142 			--pp;
143 			*(pp - 1) = 0;
144 		}
145 		else
146 		{
147 			m = (unsigned long) aa * *ps--;
148 			carry = (m & 0xffff) + *pp;
149 			*pp-- = (unsigned short)carry;
150 			carry = (carry >> 16) + (m >> 16) + *pp;
151 			*pp = (unsigned short)carry;
152 			*(pp - 1) = carry >> 16;
153 		}
154 	}
155 	for (i = M; i < NI; i++)
156 	c[i] = p[i];
157 }
158 
159 
160 /* Divide significands. Neither the numerator nor the denominator
161 is permitted to have its high guard word nonzero.  */
162 
__edivm(short unsigned int * __restrict__ den,short unsigned int * __restrict__ num)163 int __edivm(short unsigned int * __restrict__ den,
164 		 short unsigned int * __restrict__ num)
165 {
166 	int i;
167 	register unsigned short *p;
168 	unsigned long tnum;
169 	unsigned short j, tdenm, tquot;
170 	unsigned short tprod[NI + 1];
171 	unsigned short equot[NI];
172 
173 	p = &equot[0];
174 	*p++ = num[0];
175 	*p++ = num[1];
176 
177 	for (i = M; i < NI; i++)
178 	{
179 		*p++ = 0;
180 	}
181 	__eshdn1(num);
182 	tdenm = den[M + 1];
183 	for (i = M; i < NI; i++)
184 	{
185 		/* Find trial quotient digit (the radix is 65536). */
186 		tnum = (((unsigned long) num[M]) << 16) + num[M + 1];
187 
188 		/* Do not execute the divide instruction if it will overflow. */
189 		if ((tdenm * 0xffffUL) < tnum)
190 			tquot = 0xffff;
191 		else
192 			tquot = tnum / tdenm;
193 
194 		/* Prove that the divide worked. */
195 		/*
196 		tcheck = (unsigned long)tquot * tdenm;
197 		if (tnum - tcheck > tdenm)
198 			tquot = 0xffff;
199 		*/
200 		/* Multiply denominator by trial quotient digit. */
201 		__m16m(tquot, den, tprod);
202 		/* The quotient digit may have been overestimated. */
203 		if (__ecmpm(tprod, num) > 0)
204 		{
205 			tquot -= 1;
206 			__esubm(den, tprod);
207 			if(__ecmpm(tprod, num) > 0)
208 			{
209 				tquot -= 1;
210 				__esubm(den, tprod);
211 			}
212 		}
213 		__esubm(tprod, num);
214 		equot[i] = tquot;
215 		__eshup6(num);
216 	}
217 	/* test for nonzero remainder after roundoff bit */
218 	p = &num[M];
219 	j = 0;
220 	for (i = M; i < NI; i++)
221 	{
222 		j |= *p++;
223 	}
224 	if (j)
225 		j = 1;
226 
227 	for (i = 0; i < NI; i++)
228 		num[i] = equot[i];
229 
230 	return ( (int)j );
231 }
232 
233 
234 /* Multiply significands */
__emulm(const short unsigned int * __restrict__ a,short unsigned int * __restrict__ b)235 int __emulm(const short unsigned int * __restrict__ a,
236 		 short unsigned int * __restrict__ b)
237 {
238 	const unsigned short *p;
239 	unsigned short *q;
240 	unsigned short pprod[NI];
241 	unsigned short equot[NI];
242 	unsigned short j;
243 	int i;
244 
245 	equot[0] = b[0];
246 	equot[1] = b[1];
247 	for (i = M; i < NI; i++)
248 		equot[i] = 0;
249 
250 	j = 0;
251 	p = &a[NI - 1];
252 	q = &equot[NI - 1];
253 	for (i = M + 1; i < NI; i++)
254 	{
255 		if (*p == 0)
256 		{
257 			--p;
258 		}
259 		else
260 		{
261 			__m16m(*p--, b, pprod);
262 			__eaddm(pprod, equot);
263 		}
264 		j |= *q;
265 		__eshdn6(equot);
266 	}
267 
268 	for (i = 0; i < NI; i++)
269 		b[i] = equot[i];
270 
271 	/* return flag for lost nonzero bits */
272 	return ( (int)j );
273 }
274 
275 
276 /*
277  * Normalize and round off.
278  *
279  * The internal format number to be rounded is "s".
280  * Input "lost" indicates whether the number is exact.
281  * This is the so-called sticky bit.
282  *
283  * Input "subflg" indicates whether the number was obtained
284  * by a subtraction operation.  In that case if lost is nonzero
285  * then the number is slightly smaller than indicated.
286  *
287  * Input "expo" is the biased exponent, which may be negative.
288  * the exponent field of "s" is ignored but is replaced by
289  * "expo" as adjusted by normalization and rounding.
290  *
291  * Input "rcntrl" is the rounding control.
292  *
293  * Input "rnprc" is precison control (64 or NBITS).
294  */
295 
__emdnorm(short unsigned int * s,int lost,int subflg,int expo,int rcntrl,int rndprc)296 void __emdnorm(short unsigned int *s, int lost, int subflg, int expo, int rcntrl, int rndprc)
297 {
298 	int i, j;
299 	unsigned short r;
300 	int rw = NI-1; /* low guard word */
301 	int re = NI-2;
302 	const unsigned short rmsk = 0xffff;
303 	const unsigned short rmbit = 0x8000;
304 #if NE == 6
305 	unsigned short rbit[NI] = {0,0,0,0,0,0,0,1,0};
306 #else
307 	unsigned short rbit[NI] = {0,0,0,0,0,0,0,0,0,0,0,1,0};
308 #endif
309 
310 	/* Normalize */
311 	j = __enormlz(s);
312 
313 	/* a blank significand could mean either zero or infinity. */
314 #ifndef INFINITY
315 	if (j > NBITS)
316 	{
317 		__ecleazs(s);
318 		return;
319 	}
320 #endif
321 	expo -= j;
322 #ifndef INFINITY
323 	if (expo >= 32767L)
324 		goto overf;
325 #else
326 	if ((j > NBITS) && (expo < 32767L))
327 	{
328 		__ecleazs(s);
329 		return;
330 	}
331 #endif
332 	if (expo < 0L)
333 	{
334 		if (expo > (long)(-NBITS - 1))
335 		{
336 			j = (int)expo;
337 			i = __eshift(s, j);
338 			if (i)
339 				lost = 1;
340 		}
341 		else
342 		{
343 			__ecleazs(s);
344 			return;
345 		}
346 	}
347 	/* Round off, unless told not to by rcntrl. */
348 	if (rcntrl == 0)
349 		goto mdfin;
350 	if (rndprc == 64)
351 	{
352 		rw = 7;
353 		re = 6;
354 		rbit[NI - 2] = 0;
355 		rbit[6] = 1;
356 	}
357 
358 	/* Shift down 1 temporarily if the data structure has an implied
359 	 * most significant bit and the number is denormal.
360 	 * For rndprc = 64 or NBITS, there is no implied bit.
361 	 * But Intel long double denormals lose one bit of significance even so.
362 	 */
363 #if IBMPC
364 	if ((expo <= 0) && (rndprc != NBITS))
365 #else
366 	if ((expo <= 0) && (rndprc != 64) && (rndprc != NBITS))
367 #endif
368 	{
369 		lost |= s[NI - 1] & 1;
370 		__eshdn1(s);
371 	}
372 	/* Clear out all bits below the rounding bit,
373 	 * remembering in r if any were nonzero.
374 	 */
375 	r = s[rw] & rmsk;
376 	if (rndprc < NBITS)
377 	{
378 		i = rw + 1;
379 		while (i < NI)
380 		{
381 			if( s[i] )
382 				r |= 1;
383 			s[i] = 0;
384 			++i;
385 		}
386 	}
387 	s[rw] &= (rmsk ^ 0xffff);
388 	if ((r & rmbit) != 0)
389 	{
390 		if (r == rmbit)
391 		{
392 			if (lost == 0)
393 			{ /* round to even */
394 				if ((s[re] & 1) == 0)
395 					goto mddone;
396 			}
397 			else
398 			{
399 				if (subflg != 0)
400 					goto mddone;
401 			}
402 		}
403 		__eaddm(rbit, s);
404 	}
405 mddone:
406 #if IBMPC
407 	if ((expo <= 0) && (rndprc != NBITS))
408 #else
409 	if ((expo <= 0) && (rndprc != 64) && (rndprc != NBITS))
410 #endif
411 	{
412 		__eshup1(s);
413 	}
414 	if (s[2] != 0)
415 	{ /* overflow on roundoff */
416 		__eshdn1(s);
417 		expo += 1;
418 	}
419 mdfin:
420 	s[NI - 1] = 0;
421 	if (expo >= 32767L)
422 	{
423 #ifndef INFINITY
424 overf:
425 #endif
426 #ifdef INFINITY
427 		s[1] = 32767;
428 		for (i = 2; i < NI - 1; i++ )
429 			s[i] = 0;
430 #else
431 		s[1] = 32766;
432 		s[2] = 0;
433 		for (i = M + 1; i < NI - 1; i++)
434 			s[i] = 0xffff;
435 		s[NI - 1] = 0;
436 		if ((rndprc < 64) || (rndprc == 113))
437 			s[rw] &= (rmsk ^ 0xffff);
438 #endif
439 		return;
440 	}
441 	if (expo < 0)
442 		s[1] = 0;
443 	else
444 		s[1] = (unsigned short)expo;
445 }
446 
447 
448 /*
449 ;	Multiply.
450 ;
451 ;	unsigned short a[NE], b[NE], c[NE];
452 ;	emul( a, b, c );	c = b * a
453 */
__emul(const short unsigned int * a,const short unsigned int * b,short unsigned int * c)454 void __emul(const short unsigned int *a,
455 		 const short unsigned int *b,
456 		 short unsigned int *c)
457 {
458 	unsigned short ai[NI], bi[NI];
459 	int i, j;
460 	long lt, lta, ltb;
461 
462 #ifdef NANS
463 	/* NaN times anything is the same NaN. */
464 	if (__eisnan(a))
465 	{
466 		__emov(a, c);
467 		return;
468 	}
469 	if (__eisnan(b))
470 	{
471 		__emov(b, c);
472 		return;
473 	}
474 	/* Zero times infinity is a NaN. */
475 	if ((__eisinf(a) && __eiiszero(b))
476 	 || (__eisinf(b) && __eiiszero(a)))
477 	{
478 		mtherr( "emul", DOMAIN);
479 		__enan_NBITS(c);
480 		return;
481 	}
482 #endif
483 /* Infinity times anything else is infinity. */
484 #ifdef INFINITY
485 	if (__eisinf(a) || __eisinf(b))
486 	{
487 		if (__eisneg(a) ^ __eisneg(b))
488 			*(c + (NE-1)) = 0x8000;
489 		else
490 			*(c + (NE-1)) = 0;
491 		__einfin(c);
492 		return;
493 	}
494 #endif
495 	__emovi(a, ai);
496 	__emovi(b, bi);
497 	lta = ai[E];
498 	ltb = bi[E];
499 	if (ai[E] == 0)
500 	{
501 		for (i = 1; i < NI - 1; i++)
502 		{
503 			if (ai[i] != 0)
504 			{
505 				lta -= __enormlz( ai );
506 				goto mnzer1;
507 			}
508 		}
509 		__eclear(c);
510 		return;
511 	}
512 mnzer1:
513 
514 	if (bi[E] == 0)
515 	{
516 		for (i = 1; i < NI - 1; i++)
517 		{
518 			if (bi[i] != 0)
519 			{
520 				ltb -= __enormlz(bi);
521 				goto mnzer2;
522 			}
523 		}
524 		__eclear(c);
525 		return;
526 	}
527 mnzer2:
528 
529 	/* Multiply significands */
530 	j = __emulm(ai, bi);
531 	/* calculate exponent */
532 	lt = lta + ltb - (EXONE - 1);
533 	__emdnorm(bi, j, 0, lt, 64, NBITS);
534 	/* calculate sign of product */
535 	if (ai[0] == bi[0])
536 		bi[0] = 0;
537 	else
538 		bi[0] = 0xffff;
539 	__emovo(bi, c);
540 }
541 
542 
543 /* move out internal format to ieee long double */
__toe64(short unsigned int * a,short unsigned int * b)544 void __toe64(short unsigned int *a, short unsigned int *b)
545 {
546 	register unsigned short *p, *q;
547 	unsigned short i;
548 
549 #ifdef NANS
550 	if (__eiisnan(a))
551 	{
552 		__enan_64(b);
553 		return;
554 	}
555 #endif
556 #ifdef IBMPC
557 	/* Shift Intel denormal significand down 1.  */
558 	if (a[E] == 0)
559 		__eshdn1(a);
560 #endif
561 	p = a;
562 #ifdef MIEEE
563 	q = b;
564 #else
565 	q = b + 4; /* point to output exponent */
566 #if 1
567 	/* NOTE: if data type is 96 bits wide, clear the last word here. */
568 	*(q + 1)= 0;
569 #endif
570 #endif
571 
572 	/* combine sign and exponent */
573 	i = *p++;
574 #ifdef MIEEE
575 	if (i)
576 		*q++ = *p++ | 0x8000;
577 	else
578 		*q++ = *p++;
579 	*q++ = 0;
580 #else
581 	if (i)
582 		*q-- = *p++ | 0x8000;
583 	else
584 		*q-- = *p++;
585 #endif
586 	/* skip over guard word */
587 	++p;
588 	/* move the significand */
589 #ifdef MIEEE
590 	for (i = 0; i < 4; i++)
591 		*q++ = *p++;
592 #else
593 #ifdef INFINITY
594 	if (__eiisinf(a))
595         {
596 	/* Intel long double infinity.  */
597 		*q-- = 0x8000;
598 		*q-- = 0;
599 		*q-- = 0;
600 		*q = 0;
601 		return;
602 	}
603 #endif
604 	for (i = 0; i < 4; i++)
605 		*q-- = *p++;
606 #endif
607 }
608 
609 
610 /* Compare two e type numbers.
611  *
612  * unsigned short a[NE], b[NE];
613  * ecmp( a, b );
614  *
615  *  returns +1 if a > b
616  *           0 if a == b
617  *          -1 if a < b
618  *          -2 if either a or b is a NaN.
619  */
__ecmp(const short unsigned int * __restrict__ a,const short unsigned int * __restrict__ b)620 int __ecmp(const short unsigned int * __restrict__ a,
621 		const short unsigned int *  __restrict__ b)
622 {
623 	unsigned short ai[NI], bi[NI];
624 	register unsigned short *p, *q;
625 	register int i;
626 	int msign;
627 
628 #ifdef NANS
629 	if (__eisnan (a) || __eisnan (b))
630 		return (-2);
631 #endif
632 	__emovi(a, ai);
633 	p = ai;
634 	__emovi(b, bi);
635 	q = bi;
636 
637 	if (*p != *q)
638 	{ /* the signs are different */
639 		/* -0 equals + 0 */
640 		for (i = 1; i < NI - 1; i++)
641 		{
642 			if (ai[i] != 0)
643 				goto nzro;
644 			if (bi[i] != 0)
645 				goto nzro;
646 		}
647 		return (0);
648 nzro:
649 		if (*p == 0)
650 			return (1);
651 		else
652 			return (-1);
653 	}
654 	/* both are the same sign */
655 	if (*p == 0)
656 		msign = 1;
657 	else
658 		msign = -1;
659 	i = NI - 1;
660 	do
661 	{
662 		if (*p++ != *q++)
663 		{
664 			goto diff;
665 		}
666 	}
667 	while (--i > 0);
668 
669 	return (0);	/* equality */
670 
671 diff:
672 	if ( *(--p) > *(--q) )
673 		return (msign);		/* p is bigger */
674 	else
675 		return (-msign);	/* p is littler */
676 }
677 
678 /*
679 ;	Shift significand
680 ;
681 ;	Shifts significand area up or down by the number of bits
682 ;	given by the variable sc.
683 */
__eshift(short unsigned int * x,int sc)684 int __eshift(short unsigned int *x, int sc)
685 {
686 	unsigned short lost;
687 	unsigned short *p;
688 
689 	if (sc == 0)
690 		return (0);
691 
692 	lost = 0;
693 	p = x + NI - 1;
694 
695 	if (sc < 0)
696 	{
697 		sc = -sc;
698 		while (sc >= 16)
699 		{
700 			lost |= *p;	/* remember lost bits */
701 			__eshdn6(x);
702 			sc -= 16;
703 		}
704 
705 		while (sc >= 8)
706 		{
707 			lost |= *p & 0xff;
708 			__eshdn8(x);
709 			sc -= 8;
710 		}
711 
712 		while (sc > 0)
713 		{
714 			lost |= *p & 1;
715 			__eshdn1(x);
716 			sc -= 1;
717 		}
718 	}
719 	else
720 	{
721 		while (sc >= 16)
722 		{
723 			__eshup6(x);
724 			sc -= 16;
725 		}
726 
727 		while (sc >= 8)
728 		{
729 			__eshup8(x);
730 			sc -= 8;
731 		}
732 
733 		while (sc > 0)
734 		{
735 			__eshup1(x);
736 			sc -= 1;
737 		}
738 	}
739 	if (lost)
740 		lost = 1;
741 	return ( (int)lost );
742 }
743 
744 
745 /*
746 ;	normalize
747 ;
748 ; Shift normalizes the significand area pointed to by argument
749 ; shift count (up = positive) is returned.
750 */
__enormlz(short unsigned int * x)751 int __enormlz(short unsigned int *x)
752 {
753 	register unsigned short *p;
754 	int sc;
755 
756 	sc = 0;
757 	p = &x[M];
758 	if (*p != 0)
759 		goto normdn;
760 	++p;
761 	if (*p & 0x8000)
762 		return (0);	/* already normalized */
763 	while (*p == 0)
764 	{
765 		__eshup6(x);
766 		sc += 16;
767 		/* With guard word, there are NBITS+16 bits available.
768 		 * return true if all are zero.
769 		 */
770 		if (sc > NBITS)
771 			return (sc);
772 	}
773 	/* see if high byte is zero */
774 	while ((*p & 0xff00) == 0)
775 	{
776 		__eshup8(x);
777 		sc += 8;
778 	}
779 	/* now shift 1 bit at a time */
780 	while ((*p  & 0x8000) == 0)
781 	{
782 		__eshup1(x);
783 		sc += 1;
784 		if (sc > (NBITS + 16))
785 		{
786 			mtherr( "enormlz", UNDERFLOW);
787 			return (sc);
788 		}
789 	}
790 	return (sc);
791 
792 	/* Normalize by shifting down out of the high guard word
793 	   of the significand */
794 normdn:
795 	if (*p & 0xff00)
796 	{
797 		__eshdn8(x);
798 		sc -= 8;
799 	}
800 	while (*p != 0)
801 	{
802 		__eshdn1(x);
803 		sc -= 1;
804 
805 		if (sc < -NBITS)
806 		{
807 			mtherr("enormlz", OVERFLOW);
808 			return (sc);
809 		}
810 	}
811 	return (sc);
812 }
813 
814 
815 /* Move internal format number out,
816  * converting it to external format.
817  */
__emovo(const short unsigned int * __restrict__ a,short unsigned int * __restrict__ b)818 void __emovo(const short unsigned int * __restrict__ a,
819 		  short unsigned int * __restrict__ b)
820 {
821 	register const unsigned short *p;
822 	register unsigned short *q;
823 	unsigned short i;
824 
825 	p = a;
826 	q = b + (NE - 1); /* point to output exponent */
827 	/* combine sign and exponent */
828 	i = *p++;
829 	if (i)
830 		*q-- = *p++ | 0x8000;
831 	else
832 		*q-- = *p++;
833 #ifdef INFINITY
834 	if (*(p - 1) == 0x7fff)
835 	{
836 #ifdef NANS
837 		if (__eiisnan(a))
838 		{
839 			__enan_NBITS(b);
840 			return;
841 		}
842 #endif
843 		__einfin(b);
844 		return;
845 	}
846 #endif
847 	/* skip over guard word */
848 	++p;
849 	/* move the significand */
850 	for (i = 0; i < NE - 1; i++)
851 		*q-- = *p++;
852 }
853 
854 
855 #if USE_LDTOA
856 
__eiremain(short unsigned int * den,short unsigned int * num,short unsigned int * equot)857 void __eiremain(short unsigned int *den, short unsigned int *num,
858 	 short unsigned int *equot )
859 {
860 	long ld, ln;
861 	unsigned short j;
862 
863 	ld = den[E];
864 	ld -= __enormlz(den);
865 	ln = num[E];
866 	ln -= __enormlz(num);
867 	__ecleaz(equot);
868 	while (ln >= ld)
869 	{
870 		if(__ecmpm(den,num) <= 0)
871 		{
872 			__esubm(den, num);
873 			j = 1;
874 		}
875 		else
876 		{
877 			j = 0;
878 		}
879 		__eshup1(equot);
880 		equot[NI - 1] |= j;
881 		__eshup1(num);
882 		ln -= 1;
883 	}
884 	__emdnorm( num, 0, 0, ln, 0, NBITS );
885 }
886 
887 
__eadd1(const short unsigned int * __restrict__ a,const short unsigned int * __restrict__ b,short unsigned int * __restrict__ c,int subflg)888 void __eadd1(const short unsigned int *  __restrict__ a,
889 		  const short unsigned int *  __restrict__ b,
890 		  short unsigned int *  __restrict__ c,
891 		  int subflg)
892 {
893 	unsigned short ai[NI], bi[NI], ci[NI];
894 	int i, lost, j, k;
895 	long lt, lta, ltb;
896 
897 #ifdef INFINITY
898 	if (__eisinf(a))
899 	{
900 		__emov(a, c);
901 		if( subflg )
902 			__eneg(c);
903 		return;
904 	}
905 	if (__eisinf(b))
906 	{
907 		__emov(b, c);
908 		return;
909 	}
910 #endif
911 	__emovi(a, ai);
912 	__emovi(b, bi);
913 	if (sub)
914 		ai[0] = ~ai[0];
915 
916 	/* compare exponents */
917 	lta = ai[E];
918 	ltb = bi[E];
919 	lt = lta - ltb;
920 	if (lt > 0L)
921 	{	/* put the larger number in bi */
922 		__emovz(bi, ci);
923 		__emovz(ai, bi);
924 		__emovz(ci, ai);
925 		ltb = bi[E];
926 		lt = -lt;
927 	}
928 	lost = 0;
929 	if (lt != 0L)
930 	{
931 		if (lt < (long)(-NBITS - 1))
932 			goto done;	/* answer same as larger addend */
933 		k = (int)lt;
934 		lost = __eshift(ai, k); /* shift the smaller number down */
935 	}
936 	else
937 	{
938 		/* exponents were the same, so must compare significands */
939 		i = __ecmpm(ai, bi);
940 		if (i == 0)
941 		{ /* the numbers are identical in magnitude */
942 			/* if different signs, result is zero */
943 			if (ai[0] != bi[0])
944 			{
945 				__eclear(c);
946 				return;
947 			}
948 			/* if same sign, result is double */
949 			/* double denomalized tiny number */
950 			if ((bi[E] == 0) && ((bi[3] & 0x8000) == 0))
951 			{
952 				__eshup1( bi );
953 				goto done;
954 			}
955 			/* add 1 to exponent unless both are zero! */
956 			for (j = 1; j < NI - 1; j++)
957 			{
958 				if (bi[j] != 0)
959 				{
960 				/* This could overflow, but let emovo take care of that. */
961 					ltb += 1;
962 					break;
963 				}
964 			}
965 			bi[E] = (unsigned short )ltb;
966 			goto done;
967 		}
968 		if (i > 0)
969 		{	/* put the larger number in bi */
970 			__emovz(bi, ci);
971 			__emovz(ai, bi);
972 			__emovz(ci, ai);
973 		}
974 	}
975 	if (ai[0] == bi[0])
976 	{
977 		__eaddm(ai, bi);
978 		subflg = 0;
979 	}
980 	else
981 	{
982 		__esubm(ai, bi);
983 		subflg = 1;
984 	}
985 	__emdnorm(bi, lost, subflg, ltb, 64, NBITS);
986 
987 done:
988 	__emovo(bi, c);
989 }
990 
991 
992 /* y = largest integer not greater than x
993  * (truncated toward minus infinity)
994  *
995  * unsigned short x[NE], y[NE]
996  *
997  * efloor( x, y );
998  */
999 
1000 
__efloor(short unsigned int * x,short unsigned int * y)1001 void __efloor(short unsigned int *x, short unsigned int *y)
1002 {
1003 	register unsigned short *p;
1004 	int e, expon, i;
1005 	unsigned short f[NE];
1006 	const unsigned short bmask[] = {
1007 				0xffff,
1008 				0xfffe,
1009 				0xfffc,
1010 				0xfff8,
1011 				0xfff0,
1012 				0xffe0,
1013 				0xffc0,
1014 				0xff80,
1015 				0xff00,
1016 				0xfe00,
1017 				0xfc00,
1018 				0xf800,
1019 				0xf000,
1020 				0xe000,
1021 				0xc000,
1022 				0x8000,
1023 				0x0000,
1024 	};
1025 
1026 	__emov(x, f); /* leave in external format */
1027 	expon = (int) f[NE - 1];
1028 	e = (expon & 0x7fff) - (EXONE - 1);
1029 	if (e <= 0)
1030 	{
1031 		__eclear(y);
1032 		goto isitneg;
1033 	}
1034 	/* number of bits to clear out */
1035 	e = NBITS - e;
1036 	__emov(f, y);
1037 	if (e <= 0)
1038 		return;
1039 
1040 	p = &y[0];
1041 	while (e >= 16)
1042 	{
1043 		*p++ = 0;
1044 		e -= 16;
1045 	}
1046 	/* clear the remaining bits */
1047 	*p &= bmask[e];
1048 	/* truncate negatives toward minus infinity */
1049 isitneg:
1050 
1051 	if ((unsigned short)expon & (unsigned short)0x8000)
1052 	{
1053 		for (i = 0; i < NE - 1; i++)
1054 		{
1055 			if (f[i] != y[i])
1056 			{
1057 				__esub( __eone, y, y );
1058 				break;
1059 			}
1060 		}
1061 	}
1062 }
1063 
1064 /*
1065 ;	Subtract external format numbers.
1066 ;
1067 ;	unsigned short a[NE], b[NE], c[NE];
1068 ;	esub( a, b, c );	 c = b - a
1069 */
1070 
__esub(const short unsigned int * a,const short unsigned int * b,short unsigned int * c)1071 void __esub(const short unsigned int *  a,
1072 		 const short unsigned int *  b,
1073 		 short unsigned int *  c)
1074 {
1075 #ifdef NANS
1076 	if (__eisnan(a))
1077 	{
1078 		__emov (a, c);
1079 		return;
1080 	}
1081 	if ( __eisnan(b))
1082 	{
1083 		__emov(b, c);
1084 		return;
1085 	}
1086 	/* Infinity minus infinity is a NaN.
1087 	 * Test for subtracting infinities of the same sign.
1088 	 */
1089 	if (__eisinf(a) && __eisinf(b) && ((__eisneg (a) ^ __eisneg (b)) == 0))
1090 	{
1091 		mtherr("esub", DOMAIN);
1092 		__enan_NBITS( c );
1093 		return;
1094 	}
1095 #endif
1096 	__eadd1(a, b, c, 1);
1097 }
1098 
1099 
1100 /*
1101 ;	Divide.
1102 ;
1103 ;	unsigned short a[NI], b[NI], c[NI];
1104 ;	ediv( a, b, c );	c = b / a
1105 */
1106 
__ediv(const short unsigned int * a,const short unsigned int * b,short unsigned int * c)1107 void __ediv(const short unsigned int *a,
1108 		 const short unsigned int *b,
1109 		 short unsigned int *c)
1110 {
1111 	unsigned short ai[NI], bi[NI];
1112 	int i;
1113 	long lt, lta, ltb;
1114 
1115 #ifdef NANS
1116 	/* Return any NaN input. */
1117 	if (__eisnan(a))
1118 	{
1119 		__emov(a, c);
1120 		return;
1121 	}
1122 	if (__eisnan(b))
1123 	{
1124 		__emov(b, c);
1125 		return;
1126 	}
1127 	/* Zero over zero, or infinity over infinity, is a NaN. */
1128 	if ((__eiszero(a) && __eiszero(b))
1129 	 || (__eisinf (a) && __eisinf (b)))
1130 	{
1131 		mtherr("ediv", DOMAIN);
1132 		__enan_NBITS( c );
1133 		return;
1134 	}
1135 #endif
1136 /* Infinity over anything else is infinity. */
1137 #ifdef INFINITY
1138 	if (__eisinf(b))
1139 	{
1140 		if (__eisneg(a) ^ __eisneg(b))
1141 			*(c + (NE - 1)) = 0x8000;
1142 		else
1143 			*(c + (NE - 1)) = 0;
1144 		__einfin(c);
1145 		return;
1146 	}
1147 	if (__eisinf(a))
1148 	{
1149 		__eclear(c);
1150 		return;
1151 	}
1152 #endif
1153 	__emovi(a, ai);
1154 	__emovi(b, bi);
1155 	lta = ai[E];
1156 	ltb = bi[E];
1157 	if (bi[E] == 0)
1158 	{ /* See if numerator is zero. */
1159 		for (i = 1; i < NI - 1; i++)
1160 		{
1161 			if (bi[i] != 0)
1162 			{
1163 				ltb -= __enormlz(bi);
1164 				goto dnzro1;
1165 			}
1166 		}
1167 		__eclear(c);
1168 		return;
1169 	}
1170 dnzro1:
1171 
1172 	if (ai[E] == 0)
1173 	{	/* possible divide by zero */
1174 		for (i = 1; i < NI - 1; i++)
1175 		{
1176 			if (ai[i] != 0)
1177 			{
1178 				lta -= __enormlz(ai);
1179 				goto dnzro2;
1180 			}
1181 		}
1182 		if (ai[0] == bi[0])
1183 			*(c + (NE - 1)) = 0;
1184 		else
1185 			*(c + (NE - 1)) = 0x8000;
1186 		__einfin(c);
1187 		mtherr("ediv", SING);
1188 		return;
1189 	}
1190 dnzro2:
1191 
1192 	i = __edivm(ai, bi);
1193 	/* calculate exponent */
1194 	lt = ltb - lta + EXONE;
1195 	__emdnorm(bi, i, 0, lt, 64, NBITS);
1196 	/* set the sign */
1197 	if (ai[0] == bi[0])
1198 		bi[0] = 0;
1199 	else
1200 		bi[0] = 0Xffff;
1201 	__emovo(bi, c);
1202 }
1203 
__e64toe(short unsigned int * pe,short unsigned int * y)1204 void __e64toe(short unsigned int *pe, short unsigned int *y)
1205 {
1206 	unsigned short yy[NI];
1207 	unsigned short *p, *q, *e;
1208 	int i;
1209 
1210 	e = pe;
1211 	p = yy;
1212 	for (i = 0; i < NE - 5; i++)
1213 		*p++ = 0;
1214 #ifdef IBMPC
1215 	for (i = 0; i < 5; i++)
1216 		*p++ = *e++;
1217 #endif
1218 #ifdef DEC
1219 	for (i = 0; i < 5; i++)
1220 		*p++ = *e++;
1221 #endif
1222 #ifdef MIEEE
1223 	p = &yy[0] + (NE - 1);
1224 	*p-- = *e++;
1225 	++e;
1226 	for (i = 0; i < 4; i++)
1227 		*p-- = *e++;
1228 #endif
1229 
1230 #ifdef IBMPC
1231 	/* For Intel long double, shift denormal significand up 1
1232 	   -- but only if the top significand bit is zero.  */
1233 	if ((yy[NE - 1] & 0x7fff) == 0 && (yy[NE - 2] & 0x8000) == 0)
1234 	{
1235 		unsigned short temp[NI + 1];
1236 		__emovi(yy, temp);
1237 		__eshup1(temp);
1238 		__emovo(temp,y);
1239 		return;
1240 	}
1241 #endif
1242 #ifdef INFINITY
1243 	/* Point to the exponent field.  */
1244 	p = &yy[NE - 1];
1245 	if (*p == 0x7fff)
1246 	{
1247 #ifdef NANS
1248 #ifdef IBMPC
1249 		for (i = 0; i < 4; i++)
1250 		{
1251 			if ((i != 3 && pe[i] != 0)
1252 			  /* Check for Intel long double infinity pattern.  */
1253 			  || (i == 3 && pe[i] != 0x8000))
1254 			{
1255 				__enan_NBITS(y);
1256 				return;
1257 			}
1258 		}
1259 #else
1260 		for (i = 1; i <= 4; i++)
1261 		{
1262 			if (pe[i] != 0)
1263 			{
1264 				__enan_NBITS(y);
1265 				return;
1266 			}
1267 		}
1268 #endif
1269 #endif /* NANS */
1270 		__eclear(y);
1271 		__einfin(y);
1272 		if (*p & 0x8000)
1273 			__eneg(y);
1274 		return;
1275 	}
1276 #endif
1277 	p = yy;
1278 	q = y;
1279 	for (i = 0; i < NE; i++)
1280 		*q++ = *p++;
1281 }
1282 
1283 #endif /* USE_LDTOA */
1284