blob: 70f4b3e70e32e4804d7239492cfe34cd8717c05d [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
Haibo Huangb9244ff2018-08-11 10:12:13 -070033#define DEFINE_IFUNC_FOR(name) \
34 name##_func name __attribute__((ifunc(#name "_resolver"))); \
35 __attribute__((visibility("hidden"))) \
36 name##_func* name##_resolver()
37
38#define DECLARE_FUNC(type, name) \
39 __attribute__((visibility("hidden"))) \
40 type name
41
42#define RETURN_FUNC(type, name) { \
43 DECLARE_FUNC(type, name); \
44 return name; \
45 }
46
47typedef int memcmp_func(const void* __lhs, const void* __rhs, size_t __n);
48DEFINE_IFUNC_FOR(memcmp) {
49 __builtin_cpu_init();
Haibo Huang021d5222018-12-21 14:54:47 -080050 if (__builtin_cpu_is("atom")) RETURN_FUNC(memcmp_func, memcmp_atom);
51 if (__builtin_cpu_supports("sse4.1")) RETURN_FUNC(memcmp_func, memcmp_sse4);
Haibo Huangb9244ff2018-08-11 10:12:13 -070052 RETURN_FUNC(memcmp_func, memcmp_generic);
53}
54
55typedef void* memset_func(void* __dst, int __ch, size_t __n);
56DEFINE_IFUNC_FOR(memset) {
57 __builtin_cpu_init();
Haibo Huang021d5222018-12-21 14:54:47 -080058 if (__builtin_cpu_is("atom")) RETURN_FUNC(memset_func, memset_atom);
Haibo Huangb9244ff2018-08-11 10:12:13 -070059 RETURN_FUNC(memset_func, memset_generic);
60}
61
62typedef void* __memset_chk_func(void *s, int c, size_t n, size_t n2);
63DEFINE_IFUNC_FOR(__memset_chk) {
64 __builtin_cpu_init();
Haibo Huang021d5222018-12-21 14:54:47 -080065 if (__builtin_cpu_is("atom")) RETURN_FUNC(__memset_chk_func, __memset_chk_atom);
Haibo Huangb9244ff2018-08-11 10:12:13 -070066 RETURN_FUNC(__memset_chk_func, __memset_chk_generic);
67}
68
Haibo Huangb9244ff2018-08-11 10:12:13 -070069typedef void* memmove_func(void* __dst, const void* __src, size_t __n);
70DEFINE_IFUNC_FOR(memmove) {
71 __builtin_cpu_init();
Haibo Huang021d5222018-12-21 14:54:47 -080072 if (__builtin_cpu_is("atom")) RETURN_FUNC(memmove_func, memmove_atom);
Haibo Huangb9244ff2018-08-11 10:12:13 -070073 RETURN_FUNC(memmove_func, memmove_generic);
74}
75
Haibo Huange1413622018-11-13 14:20:43 -080076typedef void* memcpy_func(void*, const void*, size_t);
77DEFINE_IFUNC_FOR(memcpy) {
78 return memmove_resolver();
79}
80
Haibo Huangb9244ff2018-08-11 10:12:13 -070081typedef char* strcpy_func(char* __dst, const char* __src);
82DEFINE_IFUNC_FOR(strcpy) {
83 __builtin_cpu_init();
Haibo Huang021d5222018-12-21 14:54:47 -080084 if (__builtin_cpu_is("atom")) RETURN_FUNC(strcpy_func, strcpy_atom);
Haibo Huangb9244ff2018-08-11 10:12:13 -070085 RETURN_FUNC(strcpy_func, strcpy_generic);
86}
87
88typedef char* strncpy_func(char* __dst, const char* __src, size_t __n);
89DEFINE_IFUNC_FOR(strncpy) {
90 __builtin_cpu_init();
Haibo Huang021d5222018-12-21 14:54:47 -080091 if (__builtin_cpu_is("atom")) RETURN_FUNC(strncpy_func, strncpy_atom);
Haibo Huangb9244ff2018-08-11 10:12:13 -070092 RETURN_FUNC(strncpy_func, strncpy_generic);
93}
94
95typedef size_t strlen_func(const char* __s);
96DEFINE_IFUNC_FOR(strlen) {
97 __builtin_cpu_init();
Haibo Huang021d5222018-12-21 14:54:47 -080098 if (__builtin_cpu_is("atom")) RETURN_FUNC(strlen_func, strlen_atom);
Haibo Huangb9244ff2018-08-11 10:12:13 -070099 RETURN_FUNC(strlen_func, strlen_generic);
100}
101
102typedef int wmemcmp_func(const wchar_t* __lhs, const wchar_t* __rhs, size_t __n);
103DEFINE_IFUNC_FOR(wmemcmp) {
104 __builtin_cpu_init();
Haibo Huang021d5222018-12-21 14:54:47 -0800105 if (__builtin_cpu_supports("sse4.1")) RETURN_FUNC(wmemcmp_func, wmemcmp_sse4);
106 if (__builtin_cpu_is("atom")) RETURN_FUNC(wmemcmp_func, wmemcmp_atom);
Haibo Huangb9244ff2018-08-11 10:12:13 -0700107 RETURN_FUNC(wmemcmp_func, wmemcmp_freebsd);
108}
109
110typedef int strcmp_func(const char* __lhs, const char* __rhs);
111DEFINE_IFUNC_FOR(strcmp) {
112 __builtin_cpu_init();
Haibo Huang021d5222018-12-21 14:54:47 -0800113 if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(strcmp_func, strcmp_ssse3);
Haibo Huangb9244ff2018-08-11 10:12:13 -0700114 RETURN_FUNC(strcmp_func, strcmp_generic);
115}
116
117typedef int strncmp_func(const char* __lhs, const char* __rhs, size_t __n);
118DEFINE_IFUNC_FOR(strncmp) {
119 __builtin_cpu_init();
Haibo Huang021d5222018-12-21 14:54:47 -0800120 if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(strncmp_func, strncmp_ssse3);
Haibo Huangb9244ff2018-08-11 10:12:13 -0700121 RETURN_FUNC(strncmp_func, strncmp_generic);
122}
123
124typedef char* strcat_func(char* __dst, const char* __src);
125DEFINE_IFUNC_FOR(strcat) {
126 __builtin_cpu_init();
Haibo Huang021d5222018-12-21 14:54:47 -0800127 if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(strcat_func, strcat_ssse3);
Haibo Huangb9244ff2018-08-11 10:12:13 -0700128 RETURN_FUNC(strcat_func, strcat_generic);
129}
130
131typedef char* strncat_func(char* __dst, const char* __src, size_t __n);
132DEFINE_IFUNC_FOR(strncat) {
133 __builtin_cpu_init();
Haibo Huang021d5222018-12-21 14:54:47 -0800134 if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(strncat_func, strncat_ssse3);
Haibo Huangb9244ff2018-08-11 10:12:13 -0700135 RETURN_FUNC(strncat_func, strncat_openbsd);
136}
137
138typedef size_t strlcat_func(char *dst, const char *src, size_t dsize);
139DEFINE_IFUNC_FOR(strlcat) {
140 __builtin_cpu_init();
Haibo Huang021d5222018-12-21 14:54:47 -0800141 if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(strlcat_func, strlcat_ssse3);
Haibo Huangb9244ff2018-08-11 10:12:13 -0700142 RETURN_FUNC(strlcat_func, strlcat_openbsd);
143}
144
145typedef size_t strlcpy_func(char *dst, const char *src, size_t dsize);
146DEFINE_IFUNC_FOR(strlcpy) {
147 __builtin_cpu_init();
Haibo Huang021d5222018-12-21 14:54:47 -0800148 if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(strlcpy_func, strlcpy_ssse3);
Haibo Huangb9244ff2018-08-11 10:12:13 -0700149 RETURN_FUNC(strlcpy_func, strlcpy_openbsd);
150}
151
152typedef wchar_t* wcscat_func(wchar_t *s1, const wchar_t *s2);
153DEFINE_IFUNC_FOR(wcscat) {
154 __builtin_cpu_init();
Haibo Huang021d5222018-12-21 14:54:47 -0800155 if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(wcscat_func, wcscat_ssse3);
Haibo Huangb9244ff2018-08-11 10:12:13 -0700156 RETURN_FUNC(wcscat_func, wcscat_freebsd);
157}
158
159typedef wchar_t* wcscpy_func(wchar_t *s1, const wchar_t *s2);
160DEFINE_IFUNC_FOR(wcscpy) {
161 __builtin_cpu_init();
Haibo Huang021d5222018-12-21 14:54:47 -0800162 if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(wcscpy_func, wcscpy_ssse3);
Haibo Huangb9244ff2018-08-11 10:12:13 -0700163 RETURN_FUNC(wcscpy_func, wcscpy_freebsd);
164}
165
166} // extern "C"