URL
https://opencores.org/ocsvn/forwardcom/forwardcom/trunk
[/] [forwardcom/] [libraries/] [multiply_int_light.as] - Blame information for rev 149
Go to most recent revision |
Details |
Compare with Previous |
View Log
| Line No. |
Rev |
Author |
Line |
| 1 |
95 |
Agner |
/**************************** multiply_int_light.as **************************
|
| 2 |
|
|
* Author: Agner Fog
|
| 3 |
|
|
* date created: 2021-05-26
|
| 4 |
|
|
* Last modified: 2021-05-26
|
| 5 |
|
|
* Version: 1.11
|
| 6 |
|
|
* Project: ForwardCom library libc_light.li
|
| 7 |
|
|
* Description: multiply_int: multiply two 32-bit signed integers
|
| 8 |
|
|
* This function is for small CPUs with limited capabilities and no multiplication instruction
|
| 9 |
|
|
*
|
| 10 |
|
|
* Copyright 2021 GNU General Public License http://www.gnu.org/licenses
|
| 11 |
|
|
*****************************************************************************/
|
| 12 |
|
|
|
| 13 |
|
|
|
| 14 |
|
|
code section execute
|
| 15 |
|
|
|
| 16 |
|
|
_multiply_int function public reguse=1,0
|
| 17 |
|
|
// multiplies two 32 bit signed integers. no overflow check
|
| 18 |
|
|
// input r0, r1: factors
|
| 19 |
|
|
|
| 20 |
|
|
// save r1, r2, r3
|
| 21 |
|
|
int64 sp -= 3*8
|
| 22 |
|
|
int64 [sp] = r1
|
| 23 |
|
|
int64 [sp+8] = r2
|
| 24 |
|
|
int64 [sp+0x10] = r3
|
| 25 |
|
|
|
| 26 |
|
|
// get signs
|
| 27 |
|
|
int32 r2 = r0 < 0 // sign of r0
|
| 28 |
|
|
int32 r0 = -r0, mask = r2 // abs(r0)
|
| 29 |
|
|
int32 r3 = r1 < 0 // sign of r1
|
| 30 |
|
|
int32 r1 = -r1, mask = r3 // abs(r1)
|
| 31 |
|
|
int r3 ^= r2 // sign of product
|
| 32 |
|
|
// get the smallest factor in r1
|
| 33 |
|
|
if (uint32 r1 > r0) {
|
| 34 |
|
|
int32 r2 = r1
|
| 35 |
|
|
int32 r1 = r0
|
| 36 |
|
|
int32 r0 = r2
|
| 37 |
|
|
}
|
| 38 |
|
|
int32 r2 = 0 // summation of r0 multiplied by each bit in r1
|
| 39 |
|
|
while (int32 r1 != 0) { // multiplication loop
|
| 40 |
|
|
uint32 r2 += r0, mask = r1 // add r0 if bit 0 of r1
|
| 41 |
|
|
uint32 r1 >>= 1
|
| 42 |
|
|
uint32 r0 <<= 1
|
| 43 |
|
|
}
|
| 44 |
|
|
int32 r0 = r3 ? -r2 : r2 // apply sign
|
| 45 |
|
|
|
| 46 |
|
|
// restore r2, r3
|
| 47 |
|
|
int64 r1 = [sp]
|
| 48 |
|
|
int64 r2 = [sp+8]
|
| 49 |
|
|
int64 r3 = [sp+0x10]
|
| 50 |
|
|
int64 sp += 3*8
|
| 51 |
|
|
return
|
| 52 |
|
|
|
| 53 |
|
|
code end
|
© copyright 1999-2025
OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.