| 1 |
/* Multiplication two double word integers for RISC-V. |
| 2 |
|
| 3 |
Copyright (C) 2016-2025 Free Software Foundation, Inc. |
| 4 |
|
| 5 |
This file is part of GCC. |
| 6 |
|
| 7 |
GCC is free software; you can redistribute it and/or modify it under |
| 8 |
the terms of the GNU General Public License as published by the Free |
| 9 |
Software Foundation; either version 3, or (at your option) any later |
| 10 |
version. |
| 11 |
|
| 12 |
GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 |
for more details. |
| 16 |
|
| 17 |
Under Section 7 of GPL version 3, you are granted additional |
| 18 |
permissions described in the GCC Runtime Library Exception, version |
| 19 |
3.1, as published by the Free Software Foundation. |
| 20 |
|
| 21 |
You should have received a copy of the GNU General Public License and |
| 22 |
a copy of the GCC Runtime Library Exception along with this program; |
| 23 |
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 24 |
<http://www.gnu.org/licenses/>. */ |
| 25 |
|
| 26 |
#include "tconfig.h" |
| 27 |
#include "tsystem.h" |
| 28 |
#include "coretypes.h" |
| 29 |
#include "tm.h" |
| 30 |
#include "libgcc_tm.h" |
| 31 |
#define LIBGCC2_UNITS_PER_WORD (__riscv_xlen / 8) |
| 32 |
|
| 33 |
#include "libgcc2.h" |
| 34 |
|
| 35 |
#if __riscv_xlen == 32 |
| 36 |
/* Our RV64 64-bit routines are equivalent to our RV32 32-bit routines. */ |
| 37 |
# define __multi3 __muldi3 |
| 38 |
#endif |
| 39 |
|
| 40 |
DWtype |
| 41 |
__multi3 (DWtype u, DWtype v) |
| 42 |
{ |
| 43 |
const DWunion uu = {.ll = u}; |
| 44 |
const DWunion vv = {.ll = v}; |
| 45 |
DWunion w; |
| 46 |
UWtype u_low = uu.s.low; |
| 47 |
UWtype v_low = vv.s.low; |
| 48 |
UWtype u_low_msb; |
| 49 |
UWtype w_low = 0; |
| 50 |
UWtype new_w_low; |
| 51 |
UWtype w_high = 0; |
| 52 |
UWtype w_high_tmp = 0; |
| 53 |
UWtype w_high_tmp2x; |
| 54 |
UWtype carry; |
| 55 |
|
| 56 |
/* Calculate low half part of u and v, and get a UDWtype result just like |
| 57 |
what __umulsidi3 do. */ |
| 58 |
do |
| 59 |
{ |
| 60 |
new_w_low = w_low + u_low; |
| 61 |
w_high_tmp2x = w_high_tmp << 1; |
| 62 |
w_high_tmp += w_high; |
| 63 |
if (v_low & 1) |
| 64 |
{ |
| 65 |
carry = new_w_low < w_low; |
| 66 |
w_low = new_w_low; |
| 67 |
w_high = carry + w_high_tmp; |
| 68 |
} |
| 69 |
u_low_msb = (u_low >> ((sizeof (UWtype) * 8) - 1)); |
| 70 |
v_low >>= 1; |
| 71 |
u_low <<= 1; |
| 72 |
w_high_tmp = u_low_msb | w_high_tmp2x; |
| 73 |
} |
| 74 |
while (v_low); |
| 75 |
|
| 76 |
w.s.low = w_low; |
| 77 |
w.s.high = w_high; |
| 78 |
|
| 79 |
if (uu.s.high) |
| 80 |
w.s.high = w.s.high + __muluw3(vv.s.low, uu.s.high); |
| 81 |
|
| 82 |
if (vv.s.high) |
| 83 |
w.s.high += __muluw3(uu.s.low, vv.s.high); |
| 84 |
|
| 85 |
return w.ll; |
| 86 |
} |