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