1 /*
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 
31 #include <private/bionic_ifuncs.h>
32 
33 extern "C" {
34 
35 typedef int memcmp_func(const void* __lhs, const void* __rhs, size_t __n);
DEFINE_IFUNC_FOR(memcmp)36 DEFINE_IFUNC_FOR(memcmp) {
37     __builtin_cpu_init();
38     if (__builtin_cpu_is("atom")) RETURN_FUNC(memcmp_func, memcmp_atom);
39     if (__builtin_cpu_supports("sse4.1")) RETURN_FUNC(memcmp_func, memcmp_sse4);
40     RETURN_FUNC(memcmp_func, memcmp_generic);
41 }
42 
43 typedef void* memset_func(void* __dst, int __ch, size_t __n);
DEFINE_IFUNC_FOR(memset)44 DEFINE_IFUNC_FOR(memset) {
45     __builtin_cpu_init();
46     if (__builtin_cpu_is("atom")) RETURN_FUNC(memset_func, memset_atom);
47     RETURN_FUNC(memset_func, memset_generic);
48 }
49 
50 typedef void* __memset_chk_func(void *s, int c, size_t n, size_t n2);
DEFINE_IFUNC_FOR(__memset_chk)51 DEFINE_IFUNC_FOR(__memset_chk) {
52     __builtin_cpu_init();
53     if (__builtin_cpu_is("atom")) RETURN_FUNC(__memset_chk_func, __memset_chk_atom);
54     RETURN_FUNC(__memset_chk_func, __memset_chk_generic);
55 }
56 
57 typedef void* memmove_func(void* __dst, const void* __src, size_t __n);
DEFINE_IFUNC_FOR(memmove)58 DEFINE_IFUNC_FOR(memmove) {
59     __builtin_cpu_init();
60     if (__builtin_cpu_is("atom")) RETURN_FUNC(memmove_func, memmove_atom);
61     RETURN_FUNC(memmove_func, memmove_generic);
62 }
63 
64 typedef void* memcpy_func(void*, const void*, size_t);
DEFINE_IFUNC_FOR(memcpy)65 DEFINE_IFUNC_FOR(memcpy) {
66     return memmove_resolver();
67 }
68 
69 typedef char* strcpy_func(char* __dst, const char* __src);
DEFINE_IFUNC_FOR(strcpy)70 DEFINE_IFUNC_FOR(strcpy) {
71     __builtin_cpu_init();
72     if (__builtin_cpu_is("atom")) RETURN_FUNC(strcpy_func, strcpy_atom);
73     RETURN_FUNC(strcpy_func, strcpy_generic);
74 }
75 
76 typedef char* strncpy_func(char* __dst, const char* __src, size_t __n);
DEFINE_IFUNC_FOR(strncpy)77 DEFINE_IFUNC_FOR(strncpy) {
78     __builtin_cpu_init();
79     if (__builtin_cpu_is("atom")) RETURN_FUNC(strncpy_func, strncpy_atom);
80     RETURN_FUNC(strncpy_func, strncpy_generic);
81 }
82 
83 typedef size_t strlen_func(const char* __s);
DEFINE_IFUNC_FOR(strlen)84 DEFINE_IFUNC_FOR(strlen) {
85     __builtin_cpu_init();
86     if (__builtin_cpu_is("atom")) RETURN_FUNC(strlen_func, strlen_atom);
87     RETURN_FUNC(strlen_func, strlen_generic);
88 }
89 
90 typedef int wmemcmp_func(const wchar_t* __lhs, const wchar_t* __rhs, size_t __n);
DEFINE_IFUNC_FOR(wmemcmp)91 DEFINE_IFUNC_FOR(wmemcmp) {
92     __builtin_cpu_init();
93     if (__builtin_cpu_supports("sse4.1")) RETURN_FUNC(wmemcmp_func, wmemcmp_sse4);
94     if (__builtin_cpu_is("atom")) RETURN_FUNC(wmemcmp_func, wmemcmp_atom);
95     RETURN_FUNC(wmemcmp_func, wmemcmp_freebsd);
96 }
97 
98 typedef int wmemset_func(const wchar_t* __lhs, const wchar_t* __rhs, size_t __n);
DEFINE_IFUNC_FOR(wmemset)99 DEFINE_IFUNC_FOR(wmemset) {
100     __builtin_cpu_init();
101     if (__builtin_cpu_supports("avx2")) RETURN_FUNC(wmemset_func, wmemset_avx2);
102     RETURN_FUNC(wmemset_func, wmemset_freebsd);
103 }
104 
105 typedef int strcmp_func(const char* __lhs, const char* __rhs);
DEFINE_IFUNC_FOR(strcmp)106 DEFINE_IFUNC_FOR(strcmp) {
107     __builtin_cpu_init();
108     if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(strcmp_func, strcmp_ssse3);
109     RETURN_FUNC(strcmp_func, strcmp_generic);
110 }
111 
112 typedef int strncmp_func(const char* __lhs, const char* __rhs, size_t __n);
DEFINE_IFUNC_FOR(strncmp)113 DEFINE_IFUNC_FOR(strncmp) {
114     __builtin_cpu_init();
115     if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(strncmp_func, strncmp_ssse3);
116     RETURN_FUNC(strncmp_func, strncmp_generic);
117 }
118 
119 typedef char* strcat_func(char* __dst, const char* __src);
DEFINE_IFUNC_FOR(strcat)120 DEFINE_IFUNC_FOR(strcat) {
121     __builtin_cpu_init();
122     if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(strcat_func, strcat_ssse3);
123     RETURN_FUNC(strcat_func, strcat_generic);
124 }
125 
126 typedef char* strncat_func(char* __dst, const char* __src, size_t __n);
DEFINE_IFUNC_FOR(strncat)127 DEFINE_IFUNC_FOR(strncat) {
128     __builtin_cpu_init();
129     if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(strncat_func, strncat_ssse3);
130     RETURN_FUNC(strncat_func, strncat_openbsd);
131 }
132 
133 typedef size_t strlcat_func(char *dst, const char *src, size_t dsize);
DEFINE_IFUNC_FOR(strlcat)134 DEFINE_IFUNC_FOR(strlcat) {
135     __builtin_cpu_init();
136     if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(strlcat_func, strlcat_ssse3);
137     RETURN_FUNC(strlcat_func, strlcat_openbsd);
138 }
139 
140 typedef size_t strlcpy_func(char *dst, const char *src, size_t dsize);
DEFINE_IFUNC_FOR(strlcpy)141 DEFINE_IFUNC_FOR(strlcpy) {
142     __builtin_cpu_init();
143     if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(strlcpy_func, strlcpy_ssse3);
144     RETURN_FUNC(strlcpy_func, strlcpy_openbsd);
145 }
146 
147 typedef wchar_t* wcscat_func(wchar_t *s1, const wchar_t *s2);
DEFINE_IFUNC_FOR(wcscat)148 DEFINE_IFUNC_FOR(wcscat) {
149     __builtin_cpu_init();
150     if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(wcscat_func, wcscat_ssse3);
151     RETURN_FUNC(wcscat_func, wcscat_freebsd);
152 }
153 
154 typedef wchar_t* wcscpy_func(wchar_t *s1, const wchar_t *s2);
DEFINE_IFUNC_FOR(wcscpy)155 DEFINE_IFUNC_FOR(wcscpy) {
156     __builtin_cpu_init();
157     if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(wcscpy_func, wcscpy_ssse3);
158     RETURN_FUNC(wcscpy_func, wcscpy_freebsd);
159 }
160 
161 }  // extern "C"
162