blob: 9d7a4c98c788cfed2f65ccf008ef2b21ec11aca2 [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
Elliott Hughesa4c78762019-09-17 09:53:14 -070029#include <private/bionic_ifuncs.h>
Elliott Hughesadc41712024-08-16 13:08:11 +000030#include <stddef.h>
Elliott Hughesa4c78762019-09-17 09:53:14 -070031
Haibo Huangb9244ff2018-08-11 10:12:13 -070032extern "C" {
33
Haibo Huangb9244ff2018-08-11 10:12:13 -070034DEFINE_IFUNC_FOR(memcmp) {
Elliott Hughesadc41712024-08-16 13:08:11 +000035 __builtin_cpu_init();
36 if (__builtin_cpu_is("atom")) RETURN_FUNC(memcmp_func_t, memcmp_atom);
37 if (__builtin_cpu_supports("sse4.1")) RETURN_FUNC(memcmp_func_t, memcmp_sse4);
38 RETURN_FUNC(memcmp_func_t, memcmp_generic);
Haibo Huangb9244ff2018-08-11 10:12:13 -070039}
Elliott Hughesadc41712024-08-16 13:08:11 +000040MEMCMP_SHIM()
Haibo Huangb9244ff2018-08-11 10:12:13 -070041
Haibo Huangb9244ff2018-08-11 10:12:13 -070042DEFINE_IFUNC_FOR(memset) {
Elliott Hughesadc41712024-08-16 13:08:11 +000043 __builtin_cpu_init();
44 if (__builtin_cpu_is("atom")) RETURN_FUNC(memset_func_t, memset_atom);
45 RETURN_FUNC(memset_func_t, memset_generic);
Haibo Huangb9244ff2018-08-11 10:12:13 -070046}
Elliott Hughesadc41712024-08-16 13:08:11 +000047MEMSET_SHIM()
Haibo Huangb9244ff2018-08-11 10:12:13 -070048
Haibo Huangb9244ff2018-08-11 10:12:13 -070049DEFINE_IFUNC_FOR(__memset_chk) {
Elliott Hughesadc41712024-08-16 13:08:11 +000050 __builtin_cpu_init();
51 if (__builtin_cpu_is("atom")) RETURN_FUNC(__memset_chk_func_t, __memset_chk_atom);
52 RETURN_FUNC(__memset_chk_func_t, __memset_chk_generic);
Haibo Huangb9244ff2018-08-11 10:12:13 -070053}
Elliott Hughesadc41712024-08-16 13:08:11 +000054__MEMSET_CHK_SHIM()
Haibo Huangb9244ff2018-08-11 10:12:13 -070055
Haibo Huangb9244ff2018-08-11 10:12:13 -070056DEFINE_IFUNC_FOR(memmove) {
Elliott Hughesadc41712024-08-16 13:08:11 +000057 __builtin_cpu_init();
58 if (__builtin_cpu_is("atom")) RETURN_FUNC(memmove_func_t, memmove_atom);
59 RETURN_FUNC(memmove_func_t, memmove_generic);
Haibo Huangb9244ff2018-08-11 10:12:13 -070060}
Elliott Hughesadc41712024-08-16 13:08:11 +000061MEMMOVE_SHIM()
Haibo Huangb9244ff2018-08-11 10:12:13 -070062
Elliott Hughesadc41712024-08-16 13:08:11 +000063DEFINE_IFUNC_FOR(memcpy) { return memmove_resolver(); }
64MEMCPY_SHIM()
Haibo Huange1413622018-11-13 14:20:43 -080065
Haibo Huangb9244ff2018-08-11 10:12:13 -070066DEFINE_IFUNC_FOR(strcpy) {
Elliott Hughesadc41712024-08-16 13:08:11 +000067 __builtin_cpu_init();
68 if (__builtin_cpu_is("atom")) RETURN_FUNC(strcpy_func_t, strcpy_atom);
69 RETURN_FUNC(strcpy_func_t, strcpy_generic);
Haibo Huangb9244ff2018-08-11 10:12:13 -070070}
Elliott Hughesadc41712024-08-16 13:08:11 +000071STRCPY_SHIM()
Haibo Huangb9244ff2018-08-11 10:12:13 -070072
Haibo Huangb9244ff2018-08-11 10:12:13 -070073DEFINE_IFUNC_FOR(strncpy) {
Elliott Hughesadc41712024-08-16 13:08:11 +000074 __builtin_cpu_init();
75 if (__builtin_cpu_is("atom")) RETURN_FUNC(strncpy_func_t, strncpy_atom);
76 RETURN_FUNC(strncpy_func_t, strncpy_generic);
Haibo Huangb9244ff2018-08-11 10:12:13 -070077}
Elliott Hughesadc41712024-08-16 13:08:11 +000078STRNCPY_SHIM()
Haibo Huangb9244ff2018-08-11 10:12:13 -070079
Haibo Huangb9244ff2018-08-11 10:12:13 -070080DEFINE_IFUNC_FOR(strlen) {
Elliott Hughesadc41712024-08-16 13:08:11 +000081 __builtin_cpu_init();
82 if (__builtin_cpu_is("atom")) RETURN_FUNC(strlen_func_t, strlen_atom);
83 RETURN_FUNC(strlen_func_t, strlen_generic);
Haibo Huangb9244ff2018-08-11 10:12:13 -070084}
Elliott Hughesadc41712024-08-16 13:08:11 +000085STRLEN_SHIM()
Haibo Huangb9244ff2018-08-11 10:12:13 -070086
Haibo Huangb9244ff2018-08-11 10:12:13 -070087DEFINE_IFUNC_FOR(strcmp) {
Elliott Hughesadc41712024-08-16 13:08:11 +000088 __builtin_cpu_init();
89 // TODO: ssse3 is required by our x86 abi!
90 if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(strcmp_func_t, strcmp_ssse3);
91 RETURN_FUNC(strcmp_func_t, strcmp_generic);
Haibo Huangb9244ff2018-08-11 10:12:13 -070092}
Elliott Hughesadc41712024-08-16 13:08:11 +000093STRCMP_SHIM()
Haibo Huangb9244ff2018-08-11 10:12:13 -070094
Haibo Huangb9244ff2018-08-11 10:12:13 -070095DEFINE_IFUNC_FOR(strncmp) {
Elliott Hughesadc41712024-08-16 13:08:11 +000096 __builtin_cpu_init();
97 // TODO: ssse3 is required by our x86 abi!
98 if (__builtin_cpu_supports("ssse3"))
99 RETURN_FUNC(strncmp_func_t, strncmp_ssse3);
100 RETURN_FUNC(strncmp_func_t, strncmp_generic);
Haibo Huangb9244ff2018-08-11 10:12:13 -0700101}
Elliott Hughesadc41712024-08-16 13:08:11 +0000102STRNCMP_SHIM()
Haibo Huangb9244ff2018-08-11 10:12:13 -0700103
Haibo Huangb9244ff2018-08-11 10:12:13 -0700104DEFINE_IFUNC_FOR(strcat) {
Elliott Hughesadc41712024-08-16 13:08:11 +0000105 __builtin_cpu_init();
106 // TODO: ssse3 is required by our x86 abi!
107 if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(strcat_func_t, strcat_ssse3);
108 RETURN_FUNC(strcat_func_t, strcat_generic);
Haibo Huangb9244ff2018-08-11 10:12:13 -0700109}
Elliott Hughesadc41712024-08-16 13:08:11 +0000110STRCAT_SHIM()
Haibo Huangb9244ff2018-08-11 10:12:13 -0700111
Haibo Huangb9244ff2018-08-11 10:12:13 -0700112DEFINE_IFUNC_FOR(strncat) {
Elliott Hughesadc41712024-08-16 13:08:11 +0000113 __builtin_cpu_init();
114 // TODO: ssse3 is required by our x86 abi!
115 if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(strncat_func_t, strncat_ssse3);
116 RETURN_FUNC(strncat_func_t, strncat_openbsd);
Haibo Huangb9244ff2018-08-11 10:12:13 -0700117}
Elliott Hughesadc41712024-08-16 13:08:11 +0000118STRNCAT_SHIM()
Haibo Huangb9244ff2018-08-11 10:12:13 -0700119
Elliott Hughesadc41712024-08-16 13:08:11 +0000120typedef size_t strlcat_func_t(char*, const char*, size_t);
Haibo Huangb9244ff2018-08-11 10:12:13 -0700121DEFINE_IFUNC_FOR(strlcat) {
Elliott Hughesadc41712024-08-16 13:08:11 +0000122 __builtin_cpu_init();
123 // TODO: ssse3 is required by our x86 abi!
124 if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(strlcat_func_t, strlcat_ssse3);
125 RETURN_FUNC(strlcat_func_t, strlcat_openbsd);
Haibo Huangb9244ff2018-08-11 10:12:13 -0700126}
Elliott Hughesadc41712024-08-16 13:08:11 +0000127DEFINE_STATIC_SHIM(size_t strlcat(char* dst, const char* src, size_t n) {
128 FORWARD(strlcat)(dst, src, n);
129})
Haibo Huangb9244ff2018-08-11 10:12:13 -0700130
Elliott Hughesadc41712024-08-16 13:08:11 +0000131typedef size_t strlcpy_func_t(char*, const char*, size_t);
Haibo Huangb9244ff2018-08-11 10:12:13 -0700132DEFINE_IFUNC_FOR(strlcpy) {
Elliott Hughesadc41712024-08-16 13:08:11 +0000133 __builtin_cpu_init();
134 // TODO: ssse3 is required by our x86 abi!
135 if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(strlcpy_func_t, strlcpy_ssse3);
136 RETURN_FUNC(strlcpy_func_t, strlcpy_openbsd);
Haibo Huangb9244ff2018-08-11 10:12:13 -0700137}
Elliott Hughesadc41712024-08-16 13:08:11 +0000138DEFINE_STATIC_SHIM(size_t strlcpy(char* dst, const char* src, size_t n) {
139 FORWARD(strlcpy)(dst, src, n);
140})
Haibo Huangb9244ff2018-08-11 10:12:13 -0700141
Elliott Hughesadc41712024-08-16 13:08:11 +0000142typedef wchar_t* wcscat_func_t(wchar_t*, const wchar_t*);
Haibo Huangb9244ff2018-08-11 10:12:13 -0700143DEFINE_IFUNC_FOR(wcscat) {
Elliott Hughesadc41712024-08-16 13:08:11 +0000144 __builtin_cpu_init();
145 // TODO: ssse3 is required by our x86 abi!
146 if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(wcscat_func_t, wcscat_ssse3);
147 RETURN_FUNC(wcscat_func_t, wcscat_freebsd);
Haibo Huangb9244ff2018-08-11 10:12:13 -0700148}
Elliott Hughesadc41712024-08-16 13:08:11 +0000149DEFINE_STATIC_SHIM(wchar_t* wcscat(wchar_t* dst, const wchar_t* src) {
150 FORWARD(wcscat)(dst, src);
151})
Haibo Huangb9244ff2018-08-11 10:12:13 -0700152
Elliott Hughesadc41712024-08-16 13:08:11 +0000153typedef wchar_t* wcscpy_func_t(wchar_t*, const wchar_t*);
Haibo Huangb9244ff2018-08-11 10:12:13 -0700154DEFINE_IFUNC_FOR(wcscpy) {
Elliott Hughesadc41712024-08-16 13:08:11 +0000155 __builtin_cpu_init();
156 // TODO: ssse3 is required by our x86 abi!
157 if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(wcscpy_func_t, wcscpy_ssse3);
158 RETURN_FUNC(wcscpy_func_t, wcscpy_freebsd);
Haibo Huangb9244ff2018-08-11 10:12:13 -0700159}
Elliott Hughesadc41712024-08-16 13:08:11 +0000160DEFINE_STATIC_SHIM(wchar_t* wcscpy(wchar_t* dst, const wchar_t* src) {
161 FORWARD(wcscpy)(dst, src);
162})
163
164typedef int wmemcmp_func_t(const wchar_t*, const wchar_t*, size_t);
165DEFINE_IFUNC_FOR(wmemcmp) {
166 __builtin_cpu_init();
167 if (__builtin_cpu_supports("sse4.1")) RETURN_FUNC(wmemcmp_func_t, wmemcmp_sse4);
168 if (__builtin_cpu_is("atom")) RETURN_FUNC(wmemcmp_func_t, wmemcmp_atom);
169 RETURN_FUNC(wmemcmp_func_t, wmemcmp_freebsd);
170}
171DEFINE_STATIC_SHIM(int wmemcmp(const wchar_t* lhs, const wchar_t* rhs, size_t n) {
172 FORWARD(wmemcmp)(lhs, rhs, n);
173})
Haibo Huangb9244ff2018-08-11 10:12:13 -0700174
175} // extern "C"