xref: /onnv-gate/usr/src/common/bignum/i386/bignum_i386.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * This file contains bignum implementation code that
31*0Sstevel@tonic-gate  * is specific to x86, but which is still more appropriate
32*0Sstevel@tonic-gate  * to write in C, rather than assembly language.
33*0Sstevel@tonic-gate  * bignum_i386_asm.s does all the assembly language code
34*0Sstevel@tonic-gate  * for x86 specific bignum support.  The assembly language
35*0Sstevel@tonic-gate  * source file has pure code, no data.  Let the C compiler
36*0Sstevel@tonic-gate  * generate what is needed to handle the variations in
37*0Sstevel@tonic-gate  * data representation and addressing, for example,
38*0Sstevel@tonic-gate  * statically linked vs PIC.
39*0Sstevel@tonic-gate  */
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #include "bignum.h"
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate #if defined(_KERNEL)
44*0Sstevel@tonic-gate #include <sys/cpuvar.h>
45*0Sstevel@tonic-gate #include <sys/disp.h>
46*0Sstevel@tonic-gate #endif
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate extern uint32_t big_mul_set_vec_sse2(uint32_t *, uint32_t *, int, uint32_t);
49*0Sstevel@tonic-gate extern uint32_t big_mul_add_vec_sse2(uint32_t *, uint32_t *, int, uint32_t);
50*0Sstevel@tonic-gate extern void big_mul_vec_sse2(uint32_t *, uint32_t *, int, uint32_t *, int);
51*0Sstevel@tonic-gate extern void big_sqr_vec_sse2(uint32_t *, uint32_t *, int);
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate #if defined(MMX_MANAGE)
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate extern uint32_t big_mul_set_vec_sse2_nsv(uint32_t *, uint32_t *, int, uint32_t);
56*0Sstevel@tonic-gate extern uint32_t big_mul_add_vec_sse2_nsv(uint32_t *, uint32_t *, int, uint32_t);
57*0Sstevel@tonic-gate extern void big_mul_vec_sse2_nsv(uint32_t *, uint32_t *, int, uint32_t *, int);
58*0Sstevel@tonic-gate extern void big_sqr_vec_sse2_nsv(uint32_t *, uint32_t *, int);
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate #endif
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate extern uint32_t big_mul_set_vec_umul(uint32_t *, uint32_t *, int, uint32_t);
63*0Sstevel@tonic-gate extern uint32_t big_mul_add_vec_umul(uint32_t *, uint32_t *, int, uint32_t);
64*0Sstevel@tonic-gate extern void big_mul_vec_umul(uint32_t *, uint32_t *, int, uint32_t *, int);
65*0Sstevel@tonic-gate extern void big_sqr_vec_umul(uint32_t *, uint32_t *, int);
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate extern uint32_t bignum_use_sse2();
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate static void bignum_i386_init();
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate static uint32_t big_mul_set_vec_init(uint32_t *, uint32_t *, int, uint32_t);
72*0Sstevel@tonic-gate static uint32_t big_mul_add_vec_init(uint32_t *, uint32_t *, int, uint32_t);
73*0Sstevel@tonic-gate static void big_mul_vec_init(uint32_t *, uint32_t *, int, uint32_t *, int);
74*0Sstevel@tonic-gate static void big_sqr_vec_init(uint32_t *, uint32_t *, int);
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate uint32_t
77*0Sstevel@tonic-gate (*big_mul_set_vec_impl)(uint32_t *r, uint32_t *a, int len, uint32_t digit)
78*0Sstevel@tonic-gate 	= &big_mul_set_vec_init;
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate uint32_t
81*0Sstevel@tonic-gate (*big_mul_add_vec_impl)(uint32_t *r, uint32_t *a, int len, uint32_t digit)
82*0Sstevel@tonic-gate 	= &big_mul_add_vec_init;
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate void
85*0Sstevel@tonic-gate (*big_mul_vec_impl)(uint32_t *r, uint32_t *a, int alen, uint32_t *b, int blen)
86*0Sstevel@tonic-gate 	= &big_mul_vec_init;
87*0Sstevel@tonic-gate void
88*0Sstevel@tonic-gate (*big_sqr_vec_impl)(uint32_t *r, uint32_t *a, int alen)
89*0Sstevel@tonic-gate 	= &big_sqr_vec_init;
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate static uint32_t
big_mul_set_vec_init(uint32_t * r,uint32_t * a,int len,uint32_t digit)92*0Sstevel@tonic-gate big_mul_set_vec_init(uint32_t *r, uint32_t *a, int len, uint32_t digit)
93*0Sstevel@tonic-gate {
94*0Sstevel@tonic-gate 	bignum_i386_init();
95*0Sstevel@tonic-gate 	return ((*big_mul_set_vec_impl)(r, a, len, digit));
96*0Sstevel@tonic-gate }
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate static uint32_t
big_mul_add_vec_init(uint32_t * r,uint32_t * a,int len,uint32_t digit)99*0Sstevel@tonic-gate big_mul_add_vec_init(uint32_t *r, uint32_t *a, int len, uint32_t digit)
100*0Sstevel@tonic-gate {
101*0Sstevel@tonic-gate 	bignum_i386_init();
102*0Sstevel@tonic-gate 	return ((*big_mul_add_vec_impl)(r, a, len, digit));
103*0Sstevel@tonic-gate }
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate static void
big_mul_vec_init(uint32_t * r,uint32_t * a,int alen,uint32_t * b,int blen)106*0Sstevel@tonic-gate big_mul_vec_init(uint32_t *r, uint32_t *a, int alen, uint32_t *b, int blen)
107*0Sstevel@tonic-gate {
108*0Sstevel@tonic-gate 	bignum_i386_init();
109*0Sstevel@tonic-gate 	(*big_mul_vec_impl)(r, a, alen, b, blen);
110*0Sstevel@tonic-gate }
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate static void
big_sqr_vec_init(uint32_t * r,uint32_t * a,int alen)113*0Sstevel@tonic-gate big_sqr_vec_init(uint32_t *r, uint32_t *a, int alen)
114*0Sstevel@tonic-gate {
115*0Sstevel@tonic-gate 	bignum_i386_init();
116*0Sstevel@tonic-gate 	(*big_sqr_vec_impl)(r, a, alen);
117*0Sstevel@tonic-gate }
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate static void
bignum_i386_init()120*0Sstevel@tonic-gate bignum_i386_init()
121*0Sstevel@tonic-gate {
122*0Sstevel@tonic-gate 	if (bignum_use_sse2() != 0) {
123*0Sstevel@tonic-gate 		big_mul_set_vec_impl = &big_mul_set_vec_sse2;
124*0Sstevel@tonic-gate 		big_mul_add_vec_impl = &big_mul_add_vec_sse2;
125*0Sstevel@tonic-gate 		big_mul_vec_impl = &big_mul_vec_sse2;
126*0Sstevel@tonic-gate 		big_sqr_vec_impl = &big_sqr_vec_sse2;
127*0Sstevel@tonic-gate 	} else {
128*0Sstevel@tonic-gate 		big_mul_set_vec_impl = &big_mul_set_vec_umul;
129*0Sstevel@tonic-gate 		big_mul_add_vec_impl = &big_mul_add_vec_umul;
130*0Sstevel@tonic-gate 		big_mul_vec_impl = &big_mul_vec_umul;
131*0Sstevel@tonic-gate 		big_sqr_vec_impl = &big_sqr_vec_umul;
132*0Sstevel@tonic-gate 	}
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate void
big_mul_vec_umul(uint32_t * r,uint32_t * a,int alen,uint32_t * b,int blen)136*0Sstevel@tonic-gate big_mul_vec_umul(uint32_t *r, uint32_t *a, int alen, uint32_t *b, int blen)
137*0Sstevel@tonic-gate {
138*0Sstevel@tonic-gate 	int i;
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 	r[alen] = big_mul_set_vec_umul(r, a, alen, b[0]);
141*0Sstevel@tonic-gate 	for (i = 1; i < blen; ++i)
142*0Sstevel@tonic-gate 		r[alen + i] = big_mul_add_vec_umul(r+i, a, alen, b[i]);
143*0Sstevel@tonic-gate }
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate void
big_sqr_vec_umul(uint32_t * r,uint32_t * a,int alen)146*0Sstevel@tonic-gate big_sqr_vec_umul(uint32_t *r, uint32_t *a, int alen)
147*0Sstevel@tonic-gate {
148*0Sstevel@tonic-gate 	int i;
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	r[alen] = big_mul_set_vec_umul(r, a, alen, a[0]);
151*0Sstevel@tonic-gate 	for (i = 1; i < alen; ++i)
152*0Sstevel@tonic-gate 		r[alen + i] = big_mul_add_vec_umul(r+i, a, alen, a[i]);
153*0Sstevel@tonic-gate }
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate #if defined(_KERNEL)
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate void
kpr_disable()158*0Sstevel@tonic-gate kpr_disable()
159*0Sstevel@tonic-gate {
160*0Sstevel@tonic-gate 	kpreempt_disable();
161*0Sstevel@tonic-gate }
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate void
kpr_enable()164*0Sstevel@tonic-gate kpr_enable()
165*0Sstevel@tonic-gate {
166*0Sstevel@tonic-gate 	kpreempt_enable();
167*0Sstevel@tonic-gate }
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate #endif
170