Skip to content

Commit

Permalink
Merge pull request OpenMathLib#4340 from yinshiyou/la-dev
Browse files Browse the repository at this point in the history
Add some refines and optimizations for LoongArch.
  • Loading branch information
martin-frbg authored Nov 29, 2023
2 parents 42b5e08 + 9fe07d8 commit 39bf8ec
Show file tree
Hide file tree
Showing 10 changed files with 786 additions and 26 deletions.
2 changes: 1 addition & 1 deletion benchmark/trsv.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ int main(int argc, char *argv[]){
long long muls = n*(n+1)/2.0;
long long adds = (n - 1.0)*n/2.0;

fprintf(stderr, "%10d %10.2f MFlops %10.6f sec\n", n,(muls+adds) / timeg * 1.e-6, timeg);
fprintf(stderr, "%10d : %10.2f MFlops %10.6f sec\n", n,(muls+adds) / timeg * 1.e-6, timeg);
if(a != NULL){
free(a);
}
Expand Down
6 changes: 2 additions & 4 deletions c_check
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,7 @@ if [ "$architecture" = "loongarch64" ]; then
tmpd="$(mktemp -d)"
tmplsx="$tmpd/lsx.c"
codelsx='"vadd.b $vr0, $vr0, $vr0"'
lsx_flags='-march=loongarch64 -mlsx'
printf "#include <lsxintrin.h>\n\n" >> "$tmplsx"
lsx_flags='-march=loongarch64'
printf "void main(void){ __asm__ volatile(%s);}\n" "$codelsx" >> "$tmplsx"
args="$lsx_flags -o $tmplsx.o $tmplsx"
{
Expand All @@ -211,8 +210,7 @@ if [ "$architecture" = "loongarch64" ]; then

tmplasx="$tmpd/lasx.c"
codelasx='"xvadd.b $xr0, $xr0, $xr0"'
lasx_flags='-march=loongarch64 -mlasx'
printf "#include <lasxintrin.h>\n\n" >> "$tmplasx"
lasx_flags='-march=loongarch64'
printf "void main(void){ __asm__ volatile(%s);}\n" "$codelasx" >> "$tmplasx"
args="$lasx_flags -o $tmplasx.o $tmplasx"
{
Expand Down
6 changes: 2 additions & 4 deletions c_check.pl
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,7 @@
} else {
$tmplsx = new File::Temp( SUFFIX => '.c' , UNLINK => 1 );
$codelsx = '"vadd.b $vr0, $vr0, $vr0"';
$lsx_flags = "-march=loongarch64 -mlsx";
print $tmplsx "#include <lsxintrin.h>\n\n";
$lsx_flags = "-march=loongarch64";
print $tmplsx "void main(void){ __asm__ volatile($codelsx); }\n";

$args = "$lsx_flags -o $tmplsx.o $tmplsx";
Expand All @@ -257,8 +256,7 @@

$tmplasx = new File::Temp( SUFFIX => '.c' , UNLINK => 1 );
$codelasx = '"xvadd.b $xr0, $xr0, $xr0"';
$lasx_flags = "-march=loongarch64 -mlasx";
print $tmplasx "#include <lasxintrin.h>\n\n";
$lasx_flags = "-march=loongarch64";
print $tmplasx "void main(void){ __asm__ volatile($codelasx); }\n";

$args = "$lasx_flags -o $tmplasx.o $tmplasx";
Expand Down
19 changes: 19 additions & 0 deletions common_loongarch64.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,17 @@ static inline int WhereAmI(void){
#define CMPLE fcmp.cle.d
#define CMPLT fcmp.clt.d
#define NEG fneg.d

#define XVFSUB xvfsub.d
#define XVFADD xvfadd.d
#define XVFMADD xvfmadd.d

#define VFSUB vfsub.d
#define VFADD vfadd.d
#define VFMADD vfmadd.d

#else

#define LD fld.s
#define ST fst.s
#define MADD fmadd.s
Expand All @@ -142,6 +152,15 @@ static inline int WhereAmI(void){
#define CMPLE fcmp.cle.s
#define CMPLT fcmp.clt.s
#define NEG fneg.s

#define XVFSUB xvfsub.s
#define XVFADD xvfadd.s
#define XVFMADD xvfmadd.s

#define VFSUB vfsub.s
#define VFADD vfadd.s
#define VFMADD vfmadd.s

#endif /* defined(DOUBLE) */

#if defined(__64BIT__) && defined(USE64BITINT)
Expand Down
16 changes: 11 additions & 5 deletions cpuid_loongarch64.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define CPU_LOONGSON3R5 1
#define CPU_LOONGSON2K1000 2

#define LA_HWCAP_LSX (1<<4)
#define LA_HWCAP_LASX (1<<5)
#define LA_HWCAP_LSX (1U << 4)
#define LA_HWCAP_LASX (1U << 5)

static char *cpuname[] = {
"LOONGSONGENERIC",
Expand All @@ -64,11 +64,11 @@ static char *cpuname_lower[] = {

int detect(void) {
#ifdef __linux
int flag = (int)getauxval(AT_HWCAP);
int hwcap = (int)getauxval(AT_HWCAP);

if (flag & LA_HWCAP_LASX)
if (hwcap & LA_HWCAP_LASX)
return CPU_LOONGSON3R5;
else if (flag & LA_HWCAP_LSX)
else if (hwcap & LA_HWCAP_LSX)
return CPU_LOONGSON2K1000;
else
return CPU_GENERIC;
Expand All @@ -94,7 +94,9 @@ void get_subdirname(void) {
}

void get_cpuconfig(void) {
uint32_t hwcaps = 0;
int d = detect();

switch (d) {
case CPU_LOONGSON3R5:
printf("#define LOONGSON3R5\n");
Expand Down Expand Up @@ -129,6 +131,10 @@ void get_cpuconfig(void) {
printf("#define L2_ASSOCIATIVE 16\n");
break;
}

hwcaps = (uint32_t)getauxval( AT_HWCAP );
if (hwcaps & LA_HWCAP_LSX) printf("#define HAVE_LSX\n");
if (hwcaps & LA_HWCAP_LASX) printf("#define HAVE_LASX\n");
}

void get_libname(void){
Expand Down
19 changes: 7 additions & 12 deletions driver/others/dynamic_loongarch64.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************/

#include <sys/auxv.h>
#include "common.h"

extern gotoblas_t gotoblas_LOONGSON3R5;
Expand Down Expand Up @@ -74,21 +75,15 @@ static gotoblas_t *force_coretype(char *coretype) {
return NULL;
}

#define LASX_MASK 1<<7
#define LSX_MASK 1<<6
#define LOONGARCH_CFG2 0x02
#define LA_HWCAP_LSX (1U << 4)
#define LA_HWCAP_LASX (1U << 5)

static gotoblas_t *get_coretype(void) {
int ret = 0;
__asm__ volatile (
"cpucfg %0, %1 \n\t"
: "+&r"(ret)
: "r"(LOONGARCH_CFG2)
);

if (ret & LASX_MASK)
int hwcap = (int)getauxval(AT_HWCAP);

if (hwcap & LA_HWCAP_LASX)
return &gotoblas_LOONGSON3R5;
else if (ret & LSX_MASK)
else if (hwcap & LA_HWCAP_LSX)
return &gotoblas_LOONGSON2K1000;
else
return &gotoblas_LOONGSONGENERIC;
Expand Down
7 changes: 7 additions & 0 deletions kernel/loongarch64/KERNEL.LOONGSON2K1000
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ifndef NO_LSX

SDOTKERNEL = dot_lsx.S
DSDOTKERNEL = dot_lsx.S
DDOTKERNEL = dot_lsx.S

endif
5 changes: 5 additions & 0 deletions kernel/loongarch64/KERNEL.LOONGSON3R5
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
ifndef NO_LASX

SDOTKERNEL = dot_lasx.S
DSDOTKERNEL = dot_lasx.S
DDOTKERNEL = dot_lasx.S

DGEMMKERNEL = dgemm_kernel_16x4.S
DGEMMINCOPY = dgemm_ncopy_16.S
DGEMMITCOPY = dgemm_tcopy_16.S
Expand Down
Loading

0 comments on commit 39bf8ec

Please sign in to comment.