blob: a9251902737c6cf4f680d349c75547b757b1818b [file] [log] [blame]
Haibo Huangb9244ff2018-08-11 10:12:13 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <stddef.h>
30
31extern "C" {
32
33struct __processor_model {
34 unsigned int __cpu_vendor;
35 unsigned int __cpu_type;
36 unsigned int __cpu_subtype;
37 unsigned int __cpu_features[1];
38};
39
40__attribute__((visibility("hidden")))
41extern struct __processor_model __cpu_model;
42
43// These definitions have to match the values in
44// llvm/include/llvm/Support/X86TargetParser.def
45static constexpr int SSSE3 = 6;
46static constexpr int SSE4_1 = 7;
47static constexpr int ATOM = 1;
48
49// __builtin_cpu_supports and __builtin_cpu_is can not be used here. They
50// don't access __cpu_model directly but use GOT.
51// See https://reviews.llvm.org/D53850
52static bool cpu_supports(unsigned int feature) {
53 return (__cpu_model.__cpu_features[0] & (1U << feature)) != 0;
54}
55
56static bool cpu_is(unsigned int type) {
57 return (__cpu_model.__cpu_type == type);
58}
59
60#define DEFINE_IFUNC_FOR(name) \
61 name##_func name __attribute__((ifunc(#name "_resolver"))); \
62 __attribute__((visibility("hidden"))) \
63 name##_func* name##_resolver()
64
65#define DECLARE_FUNC(type, name) \
66 __attribute__((visibility("hidden"))) \
67 type name
68
69#define RETURN_FUNC(type, name) { \
70 DECLARE_FUNC(type, name); \
71 return name; \
72 }
73
74typedef int memcmp_func(const void* __lhs, const void* __rhs, size_t __n);
75DEFINE_IFUNC_FOR(memcmp) {
76 __builtin_cpu_init();
77 if (cpu_is(ATOM)) RETURN_FUNC(memcmp_func, memcmp_atom);
78 if (cpu_supports(SSE4_1)) RETURN_FUNC(memcmp_func, memcmp_sse4);
79 RETURN_FUNC(memcmp_func, memcmp_generic);
80}
81
82typedef void* memset_func(void* __dst, int __ch, size_t __n);
83DEFINE_IFUNC_FOR(memset) {
84 __builtin_cpu_init();
85 if (cpu_is(ATOM)) RETURN_FUNC(memset_func, memset_atom);
86 RETURN_FUNC(memset_func, memset_generic);
87}
88
89typedef void* __memset_chk_func(void *s, int c, size_t n, size_t n2);
90DEFINE_IFUNC_FOR(__memset_chk) {
91 __builtin_cpu_init();
92 if (cpu_is(ATOM)) RETURN_FUNC(__memset_chk_func, __memset_chk_atom);
93 RETURN_FUNC(__memset_chk_func, __memset_chk_generic);
94}
95
96typedef void* memcpy_func(void*, const void*, size_t);
97DEFINE_IFUNC_FOR(memcpy) {
98 __builtin_cpu_init();
99 if (cpu_is(ATOM)) RETURN_FUNC(memcpy_func, memcpy_atom);
100 RETURN_FUNC(memcpy_func, memcpy_generic);
101}
102
103typedef void* memmove_func(void* __dst, const void* __src, size_t __n);
104DEFINE_IFUNC_FOR(memmove) {
105 __builtin_cpu_init();
106 if (cpu_is(ATOM)) RETURN_FUNC(memmove_func, memmove_atom);
107 RETURN_FUNC(memmove_func, memmove_generic);
108}
109
110typedef char* strcpy_func(char* __dst, const char* __src);
111DEFINE_IFUNC_FOR(strcpy) {
112 __builtin_cpu_init();
113 if (cpu_is(ATOM)) RETURN_FUNC(strcpy_func, strcpy_atom);
114 RETURN_FUNC(strcpy_func, strcpy_generic);
115}
116
117typedef char* strncpy_func(char* __dst, const char* __src, size_t __n);
118DEFINE_IFUNC_FOR(strncpy) {
119 __builtin_cpu_init();
120 if (cpu_is(ATOM)) RETURN_FUNC(strncpy_func, strncpy_atom);
121 RETURN_FUNC(strncpy_func, strncpy_generic);
122}
123
124typedef size_t strlen_func(const char* __s);
125DEFINE_IFUNC_FOR(strlen) {
126 __builtin_cpu_init();
127 if (cpu_is(ATOM)) RETURN_FUNC(strlen_func, strlen_atom);
128 RETURN_FUNC(strlen_func, strlen_generic);
129}
130
131typedef int wmemcmp_func(const wchar_t* __lhs, const wchar_t* __rhs, size_t __n);
132DEFINE_IFUNC_FOR(wmemcmp) {
133 __builtin_cpu_init();
134 if (cpu_supports(SSE4_1)) RETURN_FUNC(wmemcmp_func, wmemcmp_sse4);
135 if (cpu_is(ATOM)) RETURN_FUNC(wmemcmp_func, wmemcmp_atom);
136 RETURN_FUNC(wmemcmp_func, wmemcmp_freebsd);
137}
138
139typedef int strcmp_func(const char* __lhs, const char* __rhs);
140DEFINE_IFUNC_FOR(strcmp) {
141 __builtin_cpu_init();
142 if (cpu_supports(SSSE3)) RETURN_FUNC(strcmp_func, strcmp_ssse3);
143 RETURN_FUNC(strcmp_func, strcmp_generic);
144}
145
146typedef int strncmp_func(const char* __lhs, const char* __rhs, size_t __n);
147DEFINE_IFUNC_FOR(strncmp) {
148 __builtin_cpu_init();
149 if (cpu_supports(SSSE3)) RETURN_FUNC(strncmp_func, strncmp_ssse3);
150 RETURN_FUNC(strncmp_func, strncmp_generic);
151}
152
153typedef char* strcat_func(char* __dst, const char* __src);
154DEFINE_IFUNC_FOR(strcat) {
155 __builtin_cpu_init();
156 if (cpu_supports(SSSE3)) RETURN_FUNC(strcat_func, strcat_ssse3);
157 RETURN_FUNC(strcat_func, strcat_generic);
158}
159
160typedef char* strncat_func(char* __dst, const char* __src, size_t __n);
161DEFINE_IFUNC_FOR(strncat) {
162 __builtin_cpu_init();
163 if (cpu_supports(SSSE3)) RETURN_FUNC(strncat_func, strncat_ssse3);
164 RETURN_FUNC(strncat_func, strncat_openbsd);
165}
166
167typedef size_t strlcat_func(char *dst, const char *src, size_t dsize);
168DEFINE_IFUNC_FOR(strlcat) {
169 __builtin_cpu_init();
170 if (cpu_supports(SSSE3)) RETURN_FUNC(strlcat_func, strlcat_ssse3);
171 RETURN_FUNC(strlcat_func, strlcat_openbsd);
172}
173
174typedef size_t strlcpy_func(char *dst, const char *src, size_t dsize);
175DEFINE_IFUNC_FOR(strlcpy) {
176 __builtin_cpu_init();
177 if (cpu_supports(SSSE3)) RETURN_FUNC(strlcpy_func, strlcpy_ssse3);
178 RETURN_FUNC(strlcpy_func, strlcpy_openbsd);
179}
180
181typedef wchar_t* wcscat_func(wchar_t *s1, const wchar_t *s2);
182DEFINE_IFUNC_FOR(wcscat) {
183 __builtin_cpu_init();
184 if (cpu_supports(SSSE3)) RETURN_FUNC(wcscat_func, wcscat_ssse3);
185 RETURN_FUNC(wcscat_func, wcscat_freebsd);
186}
187
188typedef wchar_t* wcscpy_func(wchar_t *s1, const wchar_t *s2);
189DEFINE_IFUNC_FOR(wcscpy) {
190 __builtin_cpu_init();
191 if (cpu_supports(SSSE3)) RETURN_FUNC(wcscpy_func, wcscpy_ssse3);
192 RETURN_FUNC(wcscpy_func, wcscpy_freebsd);
193}
194
195} // extern "C"