aboutsummaryrefslogtreecommitdiff
path: root/sysdeps/x86/tst-ifunc-isa.h
blob: dc79d260ca0292e33e0e5608f850eb9e895a967e (plain)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* IFUNC resolver with CPU_FEATURE_ACTIVE.
   Copyright (C) 2021-2024 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   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
   <https://www.gnu.org/licenses/>.  */

#include <sys/platform/x86.h>

enum isa
{
  none,
  sse2,
  sse4_2,
  avx,
  avx2,
  avx512f
};

enum isa
__attribute__ ((__optimize__ ("-fno-stack-protector")))
get_isa (void)
{
  if (CPU_FEATURE_ACTIVE (AVX512F))
    return avx512f;
  if (CPU_FEATURE_ACTIVE (AVX2))
    return avx2;
  if (CPU_FEATURE_ACTIVE (AVX))
    return avx;
  if (CPU_FEATURE_ACTIVE (SSE4_2))
    return sse4_2;
  if (CPU_FEATURE_ACTIVE (SSE2))
    return sse2;
  return none;
}

static int
isa_sse2 (void)
{
  return sse2;
}

static int
isa_sse4_2 (void)
{
  return sse4_2;
}

static int
isa_avx (void)
{
  return avx;
}

static int
isa_avx2 (void)
{
  return avx2;
}

static int
isa_avx512f (void)
{
  return avx512f;
}

static int
isa_none (void)
{
  return none;
}

int foo (void) __attribute__ ((ifunc ("foo_ifunc")));

void *
__attribute__ ((__optimize__ ("-fno-stack-protector")))
foo_ifunc (void)
{
  switch (get_isa ())
    {
    case avx512f:
      return isa_avx512f;
    case avx2:
      return isa_avx2;
    case avx:
      return isa_avx;
    case sse4_2:
      return isa_sse4_2;
    case sse2:
      return isa_sse2;
    default:
      break;
    }
  return isa_none;
}