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