xref: /titanic_41/usr/src/lib/libbc/libc/gen/common/_base_il.c (revision 5d54f3d8999eac1762fe0a8c7177d20f1f201fae)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1988 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include "base_conversion.h"
30 
31 /*	The following should be coded as inline expansion templates.	*/
32 
33 /*
34  * Fundamental utilities that multiply two shorts into a unsigned long, add
35  * carry, compute quotient and remainder in underlying base, and return
36  * quo<<16 | rem as  a unsigned long.
37  */
38 
39 /*
40  * C compilers tend to generate bad code - forcing full unsigned long by
41  * unsigned long multiplies when what is really wanted is the unsigned long
42  * product of half-long operands. Similarly the quotient and remainder are
43  * all half-long. So these functions should really be implemented by inline
44  * expansion templates.
45  */
46 
47 /* p = x * y + c ; return p */
48 unsigned long
_umac(_BIG_FLOAT_DIGIT x,_BIG_FLOAT_DIGIT y,unsigned long c)49 _umac(_BIG_FLOAT_DIGIT x, _BIG_FLOAT_DIGIT y, unsigned long c)
50 {
51 	return (x * (unsigned long) y + c);
52 }
53 
54 /* p = x + c ; return (p/10000 << 16 | p%10000) */
55 unsigned long
_carry_in_b10000(_BIG_FLOAT_DIGIT x,long unsigned c)56 _carry_in_b10000(_BIG_FLOAT_DIGIT x, long unsigned c)
57 {
58 	unsigned long   p = x + c ;
59 
60 	return ((p / 10000) << 16) | (p % 10000);
61 }
62 
63 void
_carry_propagate_two(unsigned long carry,_BIG_FLOAT_DIGIT * psignificand)64 _carry_propagate_two(unsigned long carry, _BIG_FLOAT_DIGIT *psignificand)
65 {
66 	/*
67 	 * Propagate carries in a base-2**16 significand.
68 	 */
69 
70 	long unsigned   p;
71 	int             j;
72 
73 	j = 0;
74 	while (carry != 0) {
75 	p = _carry_in_b65536(psignificand[j],carry);
76 		psignificand[j++] = (_BIG_FLOAT_DIGIT) (p & 0xffff);
77 		carry = p >> 16;
78 	}
79 }
80 
81 void
_carry_propagate_ten(unsigned long carry,_BIG_FLOAT_DIGIT * psignificand)82 _carry_propagate_ten(unsigned long carry, _BIG_FLOAT_DIGIT *psignificand)
83 {
84 	/*
85 	 * Propagate carries in a base-10**4 significand.
86 	 */
87 
88 	int             j;
89 	unsigned long p;
90 
91 	j = 0;
92 	while (carry != 0) {
93 	p = _carry_in_b10000(psignificand[j],carry);
94 		psignificand[j++] = (_BIG_FLOAT_DIGIT) (p & 0xffff);
95 		carry = p >> 16;
96 	}
97 }
98