xref: /onnv-gate/usr/src/common/bignum/mont_mulf.c (revision 8933:16480dbef03d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*8933Sopensolaris@drydog.com  * Common Development and Distribution License (the "License").
6*8933Sopensolaris@drydog.com  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*8933Sopensolaris@drydog.com  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * If compiled without -DRF_INLINE_MACROS then needs -lm at link time
280Sstevel@tonic-gate  * If compiled with -DRF_INLINE_MACROS then needs conv.il at compile time
29*8933Sopensolaris@drydog.com  * (i.e. cc <compiler_flags> -DRF_INLINE_MACROS conv.il mont_mulf.c )
300Sstevel@tonic-gate  */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <math.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate static const double TwoTo16 = 65536.0;
360Sstevel@tonic-gate static const double TwoToMinus16 = 1.0/65536.0;
370Sstevel@tonic-gate static const double Zero = 0.0;
380Sstevel@tonic-gate static const double TwoTo32 = 65536.0 * 65536.0;
390Sstevel@tonic-gate static const double TwoToMinus32 = 1.0 / (65536.0 * 65536.0);
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #ifdef RF_INLINE_MACROS
420Sstevel@tonic-gate 
430Sstevel@tonic-gate double upper32(double);
440Sstevel@tonic-gate double lower32(double, double);
450Sstevel@tonic-gate double mod(double, double, double);
460Sstevel@tonic-gate 
470Sstevel@tonic-gate #else
480Sstevel@tonic-gate 
490Sstevel@tonic-gate static double
upper32(double x)500Sstevel@tonic-gate upper32(double x)
510Sstevel@tonic-gate {
520Sstevel@tonic-gate 	return (floor(x * TwoToMinus32));
530Sstevel@tonic-gate }
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 
560Sstevel@tonic-gate static double
lower32(double x,double y)570Sstevel@tonic-gate lower32(double x, double y)
580Sstevel@tonic-gate {
590Sstevel@tonic-gate 	return (x - TwoTo32 * floor(x * TwoToMinus32));
600Sstevel@tonic-gate }
610Sstevel@tonic-gate 
620Sstevel@tonic-gate static double
mod(double x,double oneoverm,double m)630Sstevel@tonic-gate mod(double x, double oneoverm, double m)
640Sstevel@tonic-gate {
650Sstevel@tonic-gate 	return (x - m * floor(x * oneoverm));
660Sstevel@tonic-gate }
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #endif
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 
710Sstevel@tonic-gate static void
cleanup(double * dt,int from,int tlen)720Sstevel@tonic-gate cleanup(double *dt, int from, int tlen)
730Sstevel@tonic-gate {
740Sstevel@tonic-gate 	int i;
750Sstevel@tonic-gate 	double tmp, tmp1, x, x1;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	tmp = tmp1 = Zero;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate 	for (i = 2 * from; i < 2 * tlen; i += 2) {
800Sstevel@tonic-gate 		x = dt[i];
810Sstevel@tonic-gate 		x1 = dt[i + 1];
820Sstevel@tonic-gate 		dt[i] = lower32(x, Zero) + tmp;
830Sstevel@tonic-gate 		dt[i + 1] = lower32(x1, Zero) + tmp1;
840Sstevel@tonic-gate 		tmp = upper32(x);
850Sstevel@tonic-gate 		tmp1 = upper32(x1);
860Sstevel@tonic-gate 	}
870Sstevel@tonic-gate }
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 
900Sstevel@tonic-gate void
conv_d16_to_i32(uint32_t * i32,double * d16,int64_t * tmp,int ilen)910Sstevel@tonic-gate conv_d16_to_i32(uint32_t *i32, double *d16, int64_t *tmp, int ilen)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate 	int i;
94*8933Sopensolaris@drydog.com 	int64_t t, t1,		/* Using int64_t and not uint64_t */
95*8933Sopensolaris@drydog.com 	    a, b, c, d;		/* because more efficient code is */
960Sstevel@tonic-gate 				/* generated this way, and there  */
97*8933Sopensolaris@drydog.com 				/* is no overflow.  */
980Sstevel@tonic-gate 	t1 = 0;
990Sstevel@tonic-gate 	a = (int64_t)d16[0];
1000Sstevel@tonic-gate 	b = (int64_t)d16[1];
1010Sstevel@tonic-gate 	for (i = 0; i < ilen - 1; i++) {
1020Sstevel@tonic-gate 		c = (int64_t)d16[2 * i + 2];
1030Sstevel@tonic-gate 		t1 += a & 0xffffffff;
1040Sstevel@tonic-gate 		t = (a >> 32);
1050Sstevel@tonic-gate 		d = (int64_t)d16[2 * i + 3];
1060Sstevel@tonic-gate 		t1 += (b & 0xffff) << 16;
1070Sstevel@tonic-gate 		t += (b >> 16) + (t1 >> 32);
1080Sstevel@tonic-gate 		i32[i] = t1 & 0xffffffff;
1090Sstevel@tonic-gate 		t1 = t;
1100Sstevel@tonic-gate 		a = c;
1110Sstevel@tonic-gate 		b = d;
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate 	t1 += a & 0xffffffff;
1140Sstevel@tonic-gate 	t = (a >> 32);
1150Sstevel@tonic-gate 	t1 += (b & 0xffff) << 16;
1160Sstevel@tonic-gate 	i32[i] = t1 & 0xffffffff;
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate void
conv_i32_to_d32(double * d32,uint32_t * i32,int len)1200Sstevel@tonic-gate conv_i32_to_d32(double *d32, uint32_t *i32, int len)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate 	int i;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate #pragma pipeloop(0)
1250Sstevel@tonic-gate 	for (i = 0; i < len; i++)
1260Sstevel@tonic-gate 		d32[i] = (double)(i32[i]);
1270Sstevel@tonic-gate }
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate void
conv_i32_to_d16(double * d16,uint32_t * i32,int len)1310Sstevel@tonic-gate conv_i32_to_d16(double *d16, uint32_t *i32, int len)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate 	int i;
1340Sstevel@tonic-gate 	uint32_t a;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate #pragma pipeloop(0)
1370Sstevel@tonic-gate 	for (i = 0; i < len; i++) {
1380Sstevel@tonic-gate 		a = i32[i];
1390Sstevel@tonic-gate 		d16[2 * i] = (double)(a & 0xffff);
1400Sstevel@tonic-gate 		d16[2 * i + 1] = (double)(a >> 16);
1410Sstevel@tonic-gate 	}
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate #ifdef RF_INLINE_MACROS
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate void
1470Sstevel@tonic-gate i16_to_d16_and_d32x4(const double *,	/* 1/(2^16) */
1480Sstevel@tonic-gate 			const double *,	/* 2^16 */
1490Sstevel@tonic-gate 			const double *,	/* 0 */
1500Sstevel@tonic-gate 			double *,	/* result16 */
1510Sstevel@tonic-gate 			double *,	/* result32 */
1520Sstevel@tonic-gate 			float *);	/* source - should be unsigned int* */
1530Sstevel@tonic-gate 					/* converted to float* */
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate #else
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate static void
i16_to_d16_and_d32x4(const double * dummy1,const double * dummy2,const double * dummy3,double * result16,double * result32,float * src)1590Sstevel@tonic-gate i16_to_d16_and_d32x4(const double *dummy1,	/* 1/(2^16) */
1600Sstevel@tonic-gate 			const double *dummy2,	/* 2^16 */
1610Sstevel@tonic-gate 			const double *dummy3,	/* 0 */
1620Sstevel@tonic-gate 			double *result16,
1630Sstevel@tonic-gate 			double *result32,
1640Sstevel@tonic-gate 			float *src)	/* source - should be unsigned int* */
1650Sstevel@tonic-gate 					/* converted to float* */
1660Sstevel@tonic-gate {
1670Sstevel@tonic-gate 	uint32_t *i32;
1680Sstevel@tonic-gate 	uint32_t a, b, c, d;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	i32 = (uint32_t *)src;
1710Sstevel@tonic-gate 	a = i32[0];
1720Sstevel@tonic-gate 	b = i32[1];
1730Sstevel@tonic-gate 	c = i32[2];
1740Sstevel@tonic-gate 	d = i32[3];
1750Sstevel@tonic-gate 	result16[0] = (double)(a & 0xffff);
1760Sstevel@tonic-gate 	result16[1] = (double)(a >> 16);
1770Sstevel@tonic-gate 	result32[0] = (double)a;
1780Sstevel@tonic-gate 	result16[2] = (double)(b & 0xffff);
1790Sstevel@tonic-gate 	result16[3] = (double)(b >> 16);
1800Sstevel@tonic-gate 	result32[1] = (double)b;
1810Sstevel@tonic-gate 	result16[4] = (double)(c & 0xffff);
1820Sstevel@tonic-gate 	result16[5] = (double)(c >> 16);
1830Sstevel@tonic-gate 	result32[2] = (double)c;
1840Sstevel@tonic-gate 	result16[6] = (double)(d & 0xffff);
1850Sstevel@tonic-gate 	result16[7] = (double)(d >> 16);
1860Sstevel@tonic-gate 	result32[3] = (double)d;
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate #endif
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate void
conv_i32_to_d32_and_d16(double * d32,double * d16,uint32_t * i32,int len)1930Sstevel@tonic-gate conv_i32_to_d32_and_d16(double *d32, double *d16, uint32_t *i32, int len)
1940Sstevel@tonic-gate {
1950Sstevel@tonic-gate 	int i;
1960Sstevel@tonic-gate 	uint32_t a;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate #pragma pipeloop(0)
1990Sstevel@tonic-gate 	for (i = 0; i < len - 3; i += 4) {
2000Sstevel@tonic-gate 		i16_to_d16_and_d32x4(&TwoToMinus16, &TwoTo16, &Zero,
201*8933Sopensolaris@drydog.com 		    &(d16[2*i]), &(d32[i]), (float *)(&(i32[i])));
2020Sstevel@tonic-gate 	}
2030Sstevel@tonic-gate 	for (; i < len; i++) {
2040Sstevel@tonic-gate 		a = i32[i];
2050Sstevel@tonic-gate 		d32[i] = (double)(i32[i]);
2060Sstevel@tonic-gate 		d16[2 * i] = (double)(a & 0xffff);
2070Sstevel@tonic-gate 		d16[2 * i + 1] = (double)(a >> 16);
2080Sstevel@tonic-gate 	}
2090Sstevel@tonic-gate }
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate static void
adjust_montf_result(uint32_t * i32,uint32_t * nint,int len)2130Sstevel@tonic-gate adjust_montf_result(uint32_t *i32, uint32_t *nint, int len)
2140Sstevel@tonic-gate {
2150Sstevel@tonic-gate 	int64_t acc;
2160Sstevel@tonic-gate 	int i;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	if (i32[len] > 0)
2190Sstevel@tonic-gate 		i = -1;
2200Sstevel@tonic-gate 	else {
2210Sstevel@tonic-gate 		for (i = len - 1; i >= 0; i--) {
2220Sstevel@tonic-gate 			if (i32[i] != nint[i]) break;
2230Sstevel@tonic-gate 		}
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate 	if ((i < 0) || (i32[i] > nint[i])) {
2260Sstevel@tonic-gate 		acc = 0;
2270Sstevel@tonic-gate 		for (i = 0; i < len; i++) {
2280Sstevel@tonic-gate 			acc = acc + (uint64_t)(i32[i]) - (uint64_t)(nint[i]);
2290Sstevel@tonic-gate 			i32[i] = acc & 0xffffffff;
2300Sstevel@tonic-gate 			acc = acc >> 32;
2310Sstevel@tonic-gate 		}
2320Sstevel@tonic-gate 	}
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate /*
2370Sstevel@tonic-gate  * the lengths of the input arrays should be at least the following:
2380Sstevel@tonic-gate  * result[nlen+1], dm1[nlen], dm2[2*nlen+1], dt[4*nlen+2], dn[nlen], nint[nlen]
2390Sstevel@tonic-gate  * all of them should be different from one another
2400Sstevel@tonic-gate  */
mont_mulf_noconv(uint32_t * result,double * dm1,double * dm2,double * dt,double * dn,uint32_t * nint,int nlen,double dn0)2410Sstevel@tonic-gate void mont_mulf_noconv(uint32_t *result,
2420Sstevel@tonic-gate 			double *dm1, double *dm2, double *dt,
2430Sstevel@tonic-gate 			double *dn, uint32_t *nint,
2440Sstevel@tonic-gate 			int nlen, double dn0)
2450Sstevel@tonic-gate {
2460Sstevel@tonic-gate 	int i, j, jj;
2470Sstevel@tonic-gate 	double digit, m2j, a, b;
2480Sstevel@tonic-gate 	double *pdm1, *pdm2, *pdn, *pdtj, pdn_0, pdm1_0;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	pdm1 = &(dm1[0]);
2510Sstevel@tonic-gate 	pdm2 = &(dm2[0]);
2520Sstevel@tonic-gate 	pdn = &(dn[0]);
2530Sstevel@tonic-gate 	pdm2[2 * nlen] = Zero;
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	if (nlen != 16) {
2560Sstevel@tonic-gate 		for (i = 0; i < 4 * nlen + 2; i++)
2570Sstevel@tonic-gate 			dt[i] = Zero;
2580Sstevel@tonic-gate 		a = dt[0] = pdm1[0] * pdm2[0];
2590Sstevel@tonic-gate 		digit = mod(lower32(a, Zero) * dn0, TwoToMinus16, TwoTo16);
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 		pdtj = &(dt[0]);
2620Sstevel@tonic-gate 		for (j = jj = 0; j < 2 * nlen; j++, jj++, pdtj++) {
2630Sstevel@tonic-gate 			m2j = pdm2[j];
2640Sstevel@tonic-gate 			a = pdtj[0] + pdn[0] * digit;
2650Sstevel@tonic-gate 			b = pdtj[1] + pdm1[0] * pdm2[j + 1] + a * TwoToMinus16;
2660Sstevel@tonic-gate 			pdtj[1] = b;
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate #pragma pipeloop(0)
2690Sstevel@tonic-gate 			for (i = 1; i < nlen; i++) {
2700Sstevel@tonic-gate 				pdtj[2 * i] += pdm1[i] * m2j + pdn[i] * digit;
2710Sstevel@tonic-gate 			}
2720Sstevel@tonic-gate 			if (jj == 30) {
2730Sstevel@tonic-gate 				cleanup(dt, j / 2 + 1, 2 * nlen + 1);
2740Sstevel@tonic-gate 				jj = 0;
2750Sstevel@tonic-gate 			}
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 			digit = mod(lower32(b, Zero) * dn0,
278*8933Sopensolaris@drydog.com 			    TwoToMinus16, TwoTo16);
2790Sstevel@tonic-gate 		}
2800Sstevel@tonic-gate 	} else {
2810Sstevel@tonic-gate 		a = dt[0] = pdm1[0] * pdm2[0];
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 		dt[65] = dt[64] = dt[63] = dt[62] = dt[61] = dt[60] =
284*8933Sopensolaris@drydog.com 		    dt[59] = dt[58] = dt[57] = dt[56] = dt[55] =
285*8933Sopensolaris@drydog.com 		    dt[54] = dt[53] = dt[52] = dt[51] = dt[50] =
286*8933Sopensolaris@drydog.com 		    dt[49] = dt[48] = dt[47] = dt[46] = dt[45] =
287*8933Sopensolaris@drydog.com 		    dt[44] = dt[43] = dt[42] = dt[41] = dt[40] =
288*8933Sopensolaris@drydog.com 		    dt[39] = dt[38] = dt[37] = dt[36] = dt[35] =
289*8933Sopensolaris@drydog.com 		    dt[34] = dt[33] = dt[32] = dt[31] = dt[30] =
290*8933Sopensolaris@drydog.com 		    dt[29] = dt[28] = dt[27] = dt[26] = dt[25] =
291*8933Sopensolaris@drydog.com 		    dt[24] = dt[23] = dt[22] = dt[21] = dt[20] =
292*8933Sopensolaris@drydog.com 		    dt[19] = dt[18] = dt[17] = dt[16] = dt[15] =
293*8933Sopensolaris@drydog.com 		    dt[14] = dt[13] = dt[12] = dt[11] = dt[10] =
294*8933Sopensolaris@drydog.com 		    dt[9] = dt[8] = dt[7] = dt[6] = dt[5] = dt[4] =
295*8933Sopensolaris@drydog.com 		    dt[3] = dt[2] = dt[1] = Zero;
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 		pdn_0 = pdn[0];
2980Sstevel@tonic-gate 		pdm1_0 = pdm1[0];
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 		digit = mod(lower32(a, Zero) * dn0, TwoToMinus16, TwoTo16);
3010Sstevel@tonic-gate 		pdtj = &(dt[0]);
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 		for (j = 0; j < 32; j++, pdtj++) {
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 			m2j = pdm2[j];
3060Sstevel@tonic-gate 			a = pdtj[0] + pdn_0 * digit;
3070Sstevel@tonic-gate 			b = pdtj[1] + pdm1_0 * pdm2[j + 1] + a * TwoToMinus16;
3080Sstevel@tonic-gate 			pdtj[1] = b;
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 			pdtj[2] += pdm1[1] *m2j + pdn[1] * digit;
3110Sstevel@tonic-gate 			pdtj[4] += pdm1[2] *m2j + pdn[2] * digit;
3120Sstevel@tonic-gate 			pdtj[6] += pdm1[3] *m2j + pdn[3] * digit;
3130Sstevel@tonic-gate 			pdtj[8] += pdm1[4] *m2j + pdn[4] * digit;
3140Sstevel@tonic-gate 			pdtj[10] += pdm1[5] *m2j + pdn[5] * digit;
3150Sstevel@tonic-gate 			pdtj[12] += pdm1[6] *m2j + pdn[6] * digit;
3160Sstevel@tonic-gate 			pdtj[14] += pdm1[7] *m2j + pdn[7] * digit;
3170Sstevel@tonic-gate 			pdtj[16] += pdm1[8] *m2j + pdn[8] * digit;
3180Sstevel@tonic-gate 			pdtj[18] += pdm1[9] *m2j + pdn[9] * digit;
3190Sstevel@tonic-gate 			pdtj[20] += pdm1[10] *m2j + pdn[10] * digit;
3200Sstevel@tonic-gate 			pdtj[22] += pdm1[11] *m2j + pdn[11] * digit;
3210Sstevel@tonic-gate 			pdtj[24] += pdm1[12] *m2j + pdn[12] * digit;
3220Sstevel@tonic-gate 			pdtj[26] += pdm1[13] *m2j + pdn[13] * digit;
3230Sstevel@tonic-gate 			pdtj[28] += pdm1[14] *m2j + pdn[14] * digit;
3240Sstevel@tonic-gate 			pdtj[30] += pdm1[15] *m2j + pdn[15] * digit;
325*8933Sopensolaris@drydog.com 			/* no need for cleanup, cannot overflow */
3260Sstevel@tonic-gate 			digit = mod(lower32(b, Zero) * dn0,
327*8933Sopensolaris@drydog.com 			    TwoToMinus16, TwoTo16);
3280Sstevel@tonic-gate 		}
3290Sstevel@tonic-gate 	}
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	conv_d16_to_i32(result, dt + 2 * nlen, (int64_t *)dt, nlen + 1);
3320Sstevel@tonic-gate 	adjust_montf_result(result, nint, nlen);
3330Sstevel@tonic-gate }
334