glibc/sysdeps/loongarch/cpu-tunables.c
caiyinyu a53451559d LoongArch: Add glibc.cpu.hwcap support.
Key Points:
1. On lasx & lsx platforms, We must use _dl_runtime_{profile, resolve}_{lsx, lasx}
   to save vector registers.
2. Via "tunables", users can choose str/mem_{lasx,lsx,unaligned} functions with
   `export GLIBC_TUNABLES=glibc.cpu.hwcaps=LASX,...`.
   Note: glibc.cpu.hwcaps doesn't affect _dl_runtime_{profile, resolve}_{lsx, lasx}
   selection.

Usage Notes:
1. Only valid inputs: LASX, LSX, UAL. Case-sensitive, comma-separated, no spaces.
2. Example: `export GLIBC_TUNABLES=glibc.cpu.hwcaps=LASX,UAL` turns on LASX & UAL.
   Unmentioned features turn off. With default ifunc: lasx > lsx > unaligned >
   aligned > generic, effect is: lasx > unaligned > aligned > generic; lsx off.
3. Incorrect GLIBC_TUNABLES settings will show error messages.
   For example: On lsx platforms, you cannot enable lasx features. If you do
   that, you will get error messages.
4. Valid input examples:
   - GLIBC_TUNABLES=glibc.cpu.hwcaps=LASX: lasx > aligned > generic.
   - GLIBC_TUNABLES=glibc.cpu.hwcaps=LSX,UAL: lsx > unaligned > aligned > generic.
   - GLIBC_TUNABLES=glibc.cpu.hwcaps=LASX,UAL,LASX,UAL,LSX,LASX,UAL: Repetitions
     allowed but not recommended. Results in: lasx > lsx > unaligned > aligned >
     generic.
2023-09-19 09:11:49 +08:00

89 lines
2.5 KiB
C

/* LoongArch CPU feature tuning.
This file is part of the GNU C Library.
Copyright (C) 2023 Free Software Foundation, Inc.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
# include <stdbool.h>
# include <stdint.h>
# include <unistd.h> /* Get STDOUT_FILENO for _dl_printf. */
# include <elf/dl-tunables.h>
# include <string.h>
# include <cpu-features.h>
# include <ldsodefs.h>
# include <sys/auxv.h>
# define HWCAP_LOONGARCH_IFUNC \
(HWCAP_LOONGARCH_UAL | HWCAP_LOONGARCH_LSX | HWCAP_LOONGARCH_LASX)
# define CHECK_GLIBC_IFUNC_CPU_OFF(f, name, len) \
_Static_assert (sizeof (#name) - 1 == len, #name " != " #len); \
if (!memcmp (f, #name, len) && \
(GLRO (dl_hwcap) & HWCAP_LOONGARCH_##name)) \
{ \
hwcap |= (HWCAP_LOONGARCH_##name | (~HWCAP_LOONGARCH_IFUNC)); \
break; \
} \
attribute_hidden
void
TUNABLE_CALLBACK (set_hwcaps) (tunable_val_t *valp)
{
const char *p = valp->strval;
size_t len;
unsigned long hwcap = 0;
const char *c;
do {
for (c = p; *c != ','; c++)
if (*c == '\0')
break;
len = c - p;
switch(len)
{
default:
_dl_fatal_printf (
"The valid values of glibc.cpu.hwcaps is UAL, LASX, LSX!!\n"
);
break;
case 3:
{
CHECK_GLIBC_IFUNC_CPU_OFF (p, LSX, 3);
CHECK_GLIBC_IFUNC_CPU_OFF (p, UAL, 3);
_dl_fatal_printf (
"Some features are invalid or not supported on this machine!!\n"
"The valid values of glibc.cpu.hwcaps is UAL, LASX, LSX!!\n"
);
}
break;
case 4:
{
CHECK_GLIBC_IFUNC_CPU_OFF (p, LASX, 4);
_dl_fatal_printf (
"Some features are invalid or not supported on this machine!!\n"
"The valid values of glibc.cpu.hwcaps is UAL, LASX, LSX!!\n"
);
}
break;
}
p += len + 1;
}
while (*c != '\0');
GLRO (dl_larch_cpu_features).hwcap &= hwcap;
}