1ea906c41SOllivier Robert /*
2ea906c41SOllivier Robert * /src/NTP/ntp4-dev/libparse/mfp_mul.c,v 4.9 2005/07/17 20:34:40 kardel RELEASE_20050717_A
3ea906c41SOllivier Robert *
4ea906c41SOllivier Robert * mfp_mul.c,v 4.9 2005/07/17 20:34:40 kardel RELEASE_20050717_A
5ea906c41SOllivier Robert *
6ea906c41SOllivier Robert * $Created: Sat Aug 16 20:35:08 1997 $
7ea906c41SOllivier Robert *
8ea906c41SOllivier Robert * Copyright (c) 1997-2005 by Frank Kardel <kardel <AT> ntp.org>
9ea906c41SOllivier Robert *
10ea906c41SOllivier Robert * Redistribution and use in source and binary forms, with or without
11ea906c41SOllivier Robert * modification, are permitted provided that the following conditions
12ea906c41SOllivier Robert * are met:
13ea906c41SOllivier Robert * 1. Redistributions of source code must retain the above copyright
14ea906c41SOllivier Robert * notice, this list of conditions and the following disclaimer.
15ea906c41SOllivier Robert * 2. Redistributions in binary form must reproduce the above copyright
16ea906c41SOllivier Robert * notice, this list of conditions and the following disclaimer in the
17ea906c41SOllivier Robert * documentation and/or other materials provided with the distribution.
18ea906c41SOllivier Robert * 3. Neither the name of the author nor the names of its contributors
19ea906c41SOllivier Robert * may be used to endorse or promote products derived from this software
20ea906c41SOllivier Robert * without specific prior written permission.
21ea906c41SOllivier Robert *
22ea906c41SOllivier Robert * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23ea906c41SOllivier Robert * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24ea906c41SOllivier Robert * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25ea906c41SOllivier Robert * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26ea906c41SOllivier Robert * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27ea906c41SOllivier Robert * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28ea906c41SOllivier Robert * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29ea906c41SOllivier Robert * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30ea906c41SOllivier Robert * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31ea906c41SOllivier Robert * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32ea906c41SOllivier Robert * SUCH DAMAGE.
33ea906c41SOllivier Robert *
34ea906c41SOllivier Robert */
35*2b15cb3dSCy Schubert #include <config.h>
36ea906c41SOllivier Robert #include <stdio.h>
37ea906c41SOllivier Robert #include "ntp_stdlib.h"
38ea906c41SOllivier Robert #include "ntp_types.h"
39ea906c41SOllivier Robert #include "ntp_fp.h"
40ea906c41SOllivier Robert
41ea906c41SOllivier Robert #define LOW_MASK (u_int32)((1<<(FRACTION_PREC/2))-1)
42ea906c41SOllivier Robert #define HIGH_MASK (u_int32)(LOW_MASK << (FRACTION_PREC/2))
43ea906c41SOllivier Robert
44ea906c41SOllivier Robert /*
45ea906c41SOllivier Robert * for those who worry about overflows (possibly triggered by static analysis tools):
46ea906c41SOllivier Robert *
47ea906c41SOllivier Robert * Largest value of a 2^n bit number is 2^n-1.
48ea906c41SOllivier Robert * Thus the result is: (2^n-1)*(2^n-1) = 2^2n - 2^n - 2^n + 1 < 2^2n
49ea906c41SOllivier Robert * Here overflow can not happen for 2 reasons:
50ea906c41SOllivier Robert * 1) the code actually multiplies the absolute values of two signed
51ea906c41SOllivier Robert * 64bit quantities.thus effectively multiplying 2 63bit quantities.
52ea906c41SOllivier Robert * 2) Carry propagation is from low to high, building principle is
53ea906c41SOllivier Robert * addition, so no storage for the 2^2n term from above is needed.
54ea906c41SOllivier Robert */
55ea906c41SOllivier Robert
56ea906c41SOllivier Robert void
mfp_mul(int32 * o_i,u_int32 * o_f,int32 a_i,u_int32 a_f,int32 b_i,u_int32 b_f)57ea906c41SOllivier Robert mfp_mul(
58ea906c41SOllivier Robert int32 *o_i,
59ea906c41SOllivier Robert u_int32 *o_f,
60ea906c41SOllivier Robert int32 a_i,
61ea906c41SOllivier Robert u_int32 a_f,
62ea906c41SOllivier Robert int32 b_i,
63ea906c41SOllivier Robert u_int32 b_f
64ea906c41SOllivier Robert )
65ea906c41SOllivier Robert {
66ea906c41SOllivier Robert int32 i, j;
67ea906c41SOllivier Robert u_int32 f;
68ea906c41SOllivier Robert u_long a[4]; /* operand a */
69ea906c41SOllivier Robert u_long b[4]; /* operand b */
70ea906c41SOllivier Robert u_long c[5]; /* result c - 5 items for performance - see below */
71ea906c41SOllivier Robert u_long carry;
72ea906c41SOllivier Robert
73ea906c41SOllivier Robert int neg = 0;
74ea906c41SOllivier Robert
75ea906c41SOllivier Robert if (a_i < 0) /* examine sign situation */
76ea906c41SOllivier Robert {
77ea906c41SOllivier Robert neg = 1;
78ea906c41SOllivier Robert M_NEG(a_i, a_f);
79ea906c41SOllivier Robert }
80ea906c41SOllivier Robert
81ea906c41SOllivier Robert if (b_i < 0) /* examine sign situation */
82ea906c41SOllivier Robert {
83ea906c41SOllivier Robert neg = !neg;
84ea906c41SOllivier Robert M_NEG(b_i, b_f);
85ea906c41SOllivier Robert }
86ea906c41SOllivier Robert
87ea906c41SOllivier Robert a[0] = a_f & LOW_MASK; /* prepare a operand */
88ea906c41SOllivier Robert a[1] = (a_f & HIGH_MASK) >> (FRACTION_PREC/2);
89ea906c41SOllivier Robert a[2] = a_i & LOW_MASK;
90ea906c41SOllivier Robert a[3] = (a_i & HIGH_MASK) >> (FRACTION_PREC/2);
91ea906c41SOllivier Robert
92ea906c41SOllivier Robert b[0] = b_f & LOW_MASK; /* prepare b operand */
93ea906c41SOllivier Robert b[1] = (b_f & HIGH_MASK) >> (FRACTION_PREC/2);
94ea906c41SOllivier Robert b[2] = b_i & LOW_MASK;
95ea906c41SOllivier Robert b[3] = (b_i & HIGH_MASK) >> (FRACTION_PREC/2);
96ea906c41SOllivier Robert
97ea906c41SOllivier Robert c[0] = c[1] = c[2] = c[3] = c[4] = 0;
98ea906c41SOllivier Robert
99ea906c41SOllivier Robert for (i = 0; i < 4; i++) /* we do assume 32 * 32 = 64 bit multiplication */
100ea906c41SOllivier Robert for (j = 0; j < 4; j++)
101ea906c41SOllivier Robert {
102ea906c41SOllivier Robert u_long result_low, result_high;
103ea906c41SOllivier Robert int low_index = (i+j)/2; /* formal [0..3] - index for low long word */
104ea906c41SOllivier Robert int mid_index = 1+low_index; /* formal [1..4]! - index for high long word
105ea906c41SOllivier Robert will generate unecessary add of 0 to c[4]
106ea906c41SOllivier Robert but save 15 'if (result_high) expressions' */
107ea906c41SOllivier Robert int high_index = 1+mid_index; /* formal [2..5]! - index for high word overflow
108ea906c41SOllivier Robert - only assigned on overflow (limits range to 2..3) */
109ea906c41SOllivier Robert
110ea906c41SOllivier Robert result_low = (u_long)a[i] * (u_long)b[j]; /* partial product */
111ea906c41SOllivier Robert
112ea906c41SOllivier Robert if ((i+j) & 1) /* splits across two result registers */
113ea906c41SOllivier Robert {
114ea906c41SOllivier Robert result_high = result_low >> (FRACTION_PREC/2);
115ea906c41SOllivier Robert result_low <<= FRACTION_PREC/2;
116ea906c41SOllivier Robert carry = (unsigned)1<<(FRACTION_PREC/2);
117ea906c41SOllivier Robert }
118ea906c41SOllivier Robert else
119ea906c41SOllivier Robert { /* stays in a result register - except for overflows */
120ea906c41SOllivier Robert result_high = 0;
121ea906c41SOllivier Robert carry = 1;
122ea906c41SOllivier Robert }
123ea906c41SOllivier Robert
124ea906c41SOllivier Robert if (((c[low_index] >> 1) + (result_low >> 1) + ((c[low_index] & result_low & carry) != 0)) &
125ea906c41SOllivier Robert (u_int32)((unsigned)1<<(FRACTION_PREC - 1))) {
126ea906c41SOllivier Robert result_high++; /* propagate overflows */
127ea906c41SOllivier Robert }
128ea906c41SOllivier Robert
129ea906c41SOllivier Robert c[low_index] += result_low; /* add up partial products */
130ea906c41SOllivier Robert
131ea906c41SOllivier Robert if (((c[mid_index] >> 1) + (result_high >> 1) + ((c[mid_index] & result_high & 1) != 0)) &
132ea906c41SOllivier Robert (u_int32)((unsigned)1<<(FRACTION_PREC - 1))) {
133ea906c41SOllivier Robert c[high_index]++; /* propagate overflows of high word sum */
134ea906c41SOllivier Robert }
135ea906c41SOllivier Robert
136ea906c41SOllivier Robert c[mid_index] += result_high; /* will add a 0 to c[4] once but saves 15 if conditions */
137ea906c41SOllivier Robert }
138ea906c41SOllivier Robert
139ea906c41SOllivier Robert #ifdef DEBUG
140ea906c41SOllivier Robert if (debug > 6)
141ea906c41SOllivier Robert printf("mfp_mul: 0x%04lx%04lx%04lx%04lx * 0x%04lx%04lx%04lx%04lx = 0x%08lx%08lx%08lx%08lx\n",
142ea906c41SOllivier Robert a[3], a[2], a[1], a[0], b[3], b[2], b[1], b[0], c[3], c[2], c[1], c[0]);
143ea906c41SOllivier Robert #endif
144ea906c41SOllivier Robert
145ea906c41SOllivier Robert if (c[3]) /* overflow */
146ea906c41SOllivier Robert {
147ea906c41SOllivier Robert i = ((unsigned)1 << (FRACTION_PREC-1)) - 1;
148ea906c41SOllivier Robert f = ~(unsigned)0;
149ea906c41SOllivier Robert }
150ea906c41SOllivier Robert else
151ea906c41SOllivier Robert { /* take produkt - discarding extra precision */
152ea906c41SOllivier Robert i = c[2];
153ea906c41SOllivier Robert f = c[1];
154ea906c41SOllivier Robert }
155ea906c41SOllivier Robert
156ea906c41SOllivier Robert if (neg) /* recover sign */
157ea906c41SOllivier Robert {
158ea906c41SOllivier Robert M_NEG(i, f);
159ea906c41SOllivier Robert }
160ea906c41SOllivier Robert
161ea906c41SOllivier Robert *o_i = i;
162ea906c41SOllivier Robert *o_f = f;
163ea906c41SOllivier Robert
164ea906c41SOllivier Robert #ifdef DEBUG
165ea906c41SOllivier Robert if (debug > 6)
166ea906c41SOllivier Robert printf("mfp_mul: %s * %s => %s\n",
167ea906c41SOllivier Robert mfptoa((u_long)a_i, a_f, 6),
168ea906c41SOllivier Robert mfptoa((u_long)b_i, b_f, 6),
169ea906c41SOllivier Robert mfptoa((u_long)i, f, 6));
170ea906c41SOllivier Robert #endif
171ea906c41SOllivier Robert }
172ea906c41SOllivier Robert
173ea906c41SOllivier Robert /*
174ea906c41SOllivier Robert * History:
175ea906c41SOllivier Robert *
176ea906c41SOllivier Robert * mfp_mul.c,v
177ea906c41SOllivier Robert * Revision 4.9 2005/07/17 20:34:40 kardel
178ea906c41SOllivier Robert * correct carry propagation implementation
179ea906c41SOllivier Robert *
180ea906c41SOllivier Robert * Revision 4.8 2005/07/12 16:17:26 kardel
181ea906c41SOllivier Robert * add explanation why we do not write into c[4]
182ea906c41SOllivier Robert *
183ea906c41SOllivier Robert * Revision 4.7 2005/04/16 17:32:10 kardel
184ea906c41SOllivier Robert * update copyright
185ea906c41SOllivier Robert *
186ea906c41SOllivier Robert * Revision 4.6 2004/11/14 15:29:41 kardel
187ea906c41SOllivier Robert * support PPSAPI, upgrade Copyright to Berkeley style
188ea906c41SOllivier Robert *
189ea906c41SOllivier Robert * Revision 4.3 1999/02/21 12:17:37 kardel
190ea906c41SOllivier Robert * 4.91f reconcilation
191ea906c41SOllivier Robert *
192ea906c41SOllivier Robert * Revision 4.2 1998/12/20 23:45:28 kardel
193ea906c41SOllivier Robert * fix types and warnings
194ea906c41SOllivier Robert *
195ea906c41SOllivier Robert * Revision 4.1 1998/05/24 07:59:57 kardel
196ea906c41SOllivier Robert * conditional debug support
197ea906c41SOllivier Robert *
198ea906c41SOllivier Robert * Revision 4.0 1998/04/10 19:46:38 kardel
199ea906c41SOllivier Robert * Start 4.0 release version numbering
200ea906c41SOllivier Robert *
201ea906c41SOllivier Robert * Revision 1.1 1998/04/10 19:27:47 kardel
202ea906c41SOllivier Robert * initial NTP VERSION 4 integration of PARSE with GPS166 binary support
203ea906c41SOllivier Robert *
204ea906c41SOllivier Robert * Revision 1.1 1997/10/06 21:05:46 kardel
205ea906c41SOllivier Robert * new parse structure
206ea906c41SOllivier Robert *
207ea906c41SOllivier Robert */
208