about summary refs log tree commit diff
path: root/sysdeps/loongarch/cpu-tunables.c
blob: 8e9fab938b8aa5c8901f0af1c80000479a6e5724 (plain) (blame)
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
/* 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;
}