aboutsummaryrefslogtreecommitdiff
path: root/lib/libmpa/mpa_mul.c
blob: e1da76a6a859ea223cce30a6a221b3e3cecf18a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// SPDX-License-Identifier: BSD-2-Clause
/*
 * Copyright (c) 2014, STMicroelectronics International N.V.
 */
#include "mpa.h"

/*************************************************************
 *
 *   HELPERS
 *
 *************************************************************/

/*------------------------------------------------------------
 *
 *  These functions have ARM assembler implementations
 *
 */
#if !defined(USE_ARM_ASM)

/*  --------------------------------------------------------------------
 *  Function:   __mpa_mul_add_word
 *
 *  Multiplies a and b and adds the incoming carry to produce the product p
 *  outgoing carry is stored in *carry.
 */
void __mpa_mul_add_word(mpa_word_t a,
			mpa_word_t b, mpa_word_t *p, mpa_word_t *carry)
{
#if defined(MPA_SUPPORT_DWORD_T)
	mpa_dword_t prod;

	prod = (mpa_dword_t) (a) * (mpa_dword_t) (b) + (mpa_dword_t) (*carry);
	*p = (mpa_word_t) prod;
	*carry = (mpa_word_t) (prod >> MPA_WORD_SIZE);
#else
#error "error, write non-dword_t code for __mpa_mul_add_word"
#endif
}

/*  --------------------------------------------------------------------
 *  Function:   __mpa_mul_add_word_cum
 *
 *  Multiplies a and b and adds the incoming carry and the cumulative
 *  product stored in *p.
 *  Outgoing carry is stored in *carry.
 */
void __mpa_mul_add_word_cum(mpa_word_t a,
			    mpa_word_t b, mpa_word_t *p, mpa_word_t *carry)
{
#if defined(MPA_SUPPORT_DWORD_T)
	mpa_dword_t prod;

	prod =
	    (mpa_dword_t) (a) * (mpa_dword_t) (b) + (mpa_dword_t) (*p) +
	    (mpa_dword_t) (*carry);
	*p = (mpa_word_t) prod;
	*carry = (mpa_word_t) (prod >> MPA_WORD_SIZE);
#else
#error "error: write non-dword_t code for __mpa_mul_add_word_cum"
#endif
}

#endif /* USE_ARM_ASM */

/*  --------------------------------------------------------------------
 *  Function:   __mpa_abs_mul_word
 *
 *  Simpler multiplication when one operand is known to be a word.
 *  Calculates |op1| * op2, op2 is always positive (larger than zero).
 *  Dest needs to be distinct from op1.
 */
void __mpa_abs_mul_word(mpanum dest, const mpanum op1, mpa_word_t op2)
{
	mpa_word_t i;
	mpa_word_t carry;
	mpa_word_t *prod;
	const mpa_word_t *a;

	/* clear dest digits */
	mpa_memset(dest->d, 0, dest->alloc * BYTES_PER_WORD);

	a = op1->d;
	prod = dest->d;
	carry = 0;
	for (i = 0; i < __mpanum_size(op1); i++) {
		__mpa_mul_add_word(*a, op2, prod + i, &carry);
		a++;
	}
	dest->size = i;
	if (carry) {
		*(prod + i) = carry;
		dest->size++;
	}
}

/*  --------------------------------------------------------------------
 *  Function:   __mpa_abs_mul
 *
 *  Calculates |op1| * |op2| and puts result in dest.
 *  dest must be big enough to hold result and cannot be
 *  the same as op1 or op2.
 */
void __mpa_abs_mul(mpanum dest, const mpanum op1, const mpanum op2)
{
	mpa_word_t i = 0;
	mpa_word_t j = 0;
	mpa_word_t carry = 0;
	mpa_word_t *prod;
	const mpa_word_t *a;
	const mpa_word_t *b;

	/* clear dest digits */
	mpa_memset(dest->d, 0, dest->alloc * BYTES_PER_WORD);

	a = op1->d;
	prod = dest->d;
	for (i = 0; i < __mpanum_size(op1); i++) {
		b = op2->d;
		carry = 0;
		for (j = 0; j < __mpanum_size(op2); j++) {
			__mpa_mul_add_word_cum(*a, *b, prod + j, &carry);
			b++;
		}
		if (carry)
			*(prod + j) = carry;
		a++;
		prod++;
	}
	dest->size = i + j - 1;
	if (carry)
		dest->size++;
}

/*************************************************************
 *
 *   LIB FUNCTIONS
 *
 *************************************************************/

/*  --------------------------------------------------------------------
 *  Function:   mpa_mul
 *
 *  dest = op1 * op2
 */
void mpa_mul(mpanum dest,
	     const mpanum op1, const mpanum op2, mpa_scratch_mem pool)
{
	mpanum tmp_dest;
	char mem_marker;

	if (__mpanum_is_zero(op1) || __mpanum_is_zero(op2)) {
		mpa_set_word(dest, 0);
		return;
	}

	/* handle the case when dest is one of the operands */
	mem_marker = (dest == op1 || dest == op2);
	if (mem_marker)
		mpa_alloc_static_temp_var(&tmp_dest, pool);
	else
		tmp_dest = dest;

	__mpa_abs_mul(tmp_dest, op1, op2);

	if (__mpanum_sign(op1) != __mpanum_sign(op2))
		__mpanum_neg(tmp_dest);

	mpa_copy(dest, tmp_dest);
	if (mem_marker)
		mpa_free_static_temp_var(&tmp_dest, pool);
}

/*  --------------------------------------------------------------------
 *  Function:   mpa_mul_word
 *
 *  Calculates op1 * op2, where op2 is a word, puts result in dest.
 */
void mpa_mul_word(mpanum dest,
		 const mpanum op1, mpa_word_t op2, mpa_scratch_mem pool)
{
	int sign_1;
	mpanum tmp_dest;
	char mem_marker;

	if (__mpanum_is_zero(op1) || op2 == 0) {
		mpa_set_word(dest, 0);
		return;
	}

	sign_1 = __mpanum_sign(op1);

	/* handle the case when dest is the operand */
	mem_marker = (dest == op1);
	if (mem_marker)
		mpa_alloc_static_temp_var(&tmp_dest, pool);
	else
		tmp_dest = dest;

	__mpa_abs_mul_word(tmp_dest, op1, op2);

	if (sign_1 == MPA_NEG_SIGN)
		__mpanum_neg(tmp_dest);
	mpa_copy(dest, tmp_dest);
	if (mem_marker)
		mpa_free_static_temp_var(&tmp_dest, pool);
}