Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 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 | |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 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); |
| 48 | DEFINE_IFUNC_FOR(memcmp) { |
| 49 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame^] | 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); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 52 | RETURN_FUNC(memcmp_func, memcmp_generic); |
| 53 | } |
| 54 | |
| 55 | typedef void* memset_func(void* __dst, int __ch, size_t __n); |
| 56 | DEFINE_IFUNC_FOR(memset) { |
| 57 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame^] | 58 | if (__builtin_cpu_is("atom")) RETURN_FUNC(memset_func, memset_atom); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 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); |
| 63 | DEFINE_IFUNC_FOR(__memset_chk) { |
| 64 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame^] | 65 | if (__builtin_cpu_is("atom")) RETURN_FUNC(__memset_chk_func, __memset_chk_atom); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 66 | RETURN_FUNC(__memset_chk_func, __memset_chk_generic); |
| 67 | } |
| 68 | |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 69 | typedef void* memmove_func(void* __dst, const void* __src, size_t __n); |
| 70 | DEFINE_IFUNC_FOR(memmove) { |
| 71 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame^] | 72 | if (__builtin_cpu_is("atom")) RETURN_FUNC(memmove_func, memmove_atom); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 73 | RETURN_FUNC(memmove_func, memmove_generic); |
| 74 | } |
| 75 | |
Haibo Huang | e141362 | 2018-11-13 14:20:43 -0800 | [diff] [blame] | 76 | typedef void* memcpy_func(void*, const void*, size_t); |
| 77 | DEFINE_IFUNC_FOR(memcpy) { |
| 78 | return memmove_resolver(); |
| 79 | } |
| 80 | |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 81 | typedef char* strcpy_func(char* __dst, const char* __src); |
| 82 | DEFINE_IFUNC_FOR(strcpy) { |
| 83 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame^] | 84 | if (__builtin_cpu_is("atom")) RETURN_FUNC(strcpy_func, strcpy_atom); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 85 | RETURN_FUNC(strcpy_func, strcpy_generic); |
| 86 | } |
| 87 | |
| 88 | typedef char* strncpy_func(char* __dst, const char* __src, size_t __n); |
| 89 | DEFINE_IFUNC_FOR(strncpy) { |
| 90 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame^] | 91 | if (__builtin_cpu_is("atom")) RETURN_FUNC(strncpy_func, strncpy_atom); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 92 | RETURN_FUNC(strncpy_func, strncpy_generic); |
| 93 | } |
| 94 | |
| 95 | typedef size_t strlen_func(const char* __s); |
| 96 | DEFINE_IFUNC_FOR(strlen) { |
| 97 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame^] | 98 | if (__builtin_cpu_is("atom")) RETURN_FUNC(strlen_func, strlen_atom); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 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); |
| 103 | DEFINE_IFUNC_FOR(wmemcmp) { |
| 104 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame^] | 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); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 107 | RETURN_FUNC(wmemcmp_func, wmemcmp_freebsd); |
| 108 | } |
| 109 | |
| 110 | typedef int strcmp_func(const char* __lhs, const char* __rhs); |
| 111 | DEFINE_IFUNC_FOR(strcmp) { |
| 112 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame^] | 113 | if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(strcmp_func, strcmp_ssse3); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 114 | RETURN_FUNC(strcmp_func, strcmp_generic); |
| 115 | } |
| 116 | |
| 117 | typedef int strncmp_func(const char* __lhs, const char* __rhs, size_t __n); |
| 118 | DEFINE_IFUNC_FOR(strncmp) { |
| 119 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame^] | 120 | if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(strncmp_func, strncmp_ssse3); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 121 | RETURN_FUNC(strncmp_func, strncmp_generic); |
| 122 | } |
| 123 | |
| 124 | typedef char* strcat_func(char* __dst, const char* __src); |
| 125 | DEFINE_IFUNC_FOR(strcat) { |
| 126 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame^] | 127 | if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(strcat_func, strcat_ssse3); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 128 | RETURN_FUNC(strcat_func, strcat_generic); |
| 129 | } |
| 130 | |
| 131 | typedef char* strncat_func(char* __dst, const char* __src, size_t __n); |
| 132 | DEFINE_IFUNC_FOR(strncat) { |
| 133 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame^] | 134 | if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(strncat_func, strncat_ssse3); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 135 | RETURN_FUNC(strncat_func, strncat_openbsd); |
| 136 | } |
| 137 | |
| 138 | typedef size_t strlcat_func(char *dst, const char *src, size_t dsize); |
| 139 | DEFINE_IFUNC_FOR(strlcat) { |
| 140 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame^] | 141 | if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(strlcat_func, strlcat_ssse3); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 142 | RETURN_FUNC(strlcat_func, strlcat_openbsd); |
| 143 | } |
| 144 | |
| 145 | typedef size_t strlcpy_func(char *dst, const char *src, size_t dsize); |
| 146 | DEFINE_IFUNC_FOR(strlcpy) { |
| 147 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame^] | 148 | if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(strlcpy_func, strlcpy_ssse3); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 149 | RETURN_FUNC(strlcpy_func, strlcpy_openbsd); |
| 150 | } |
| 151 | |
| 152 | typedef wchar_t* wcscat_func(wchar_t *s1, const wchar_t *s2); |
| 153 | DEFINE_IFUNC_FOR(wcscat) { |
| 154 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame^] | 155 | if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(wcscat_func, wcscat_ssse3); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 156 | RETURN_FUNC(wcscat_func, wcscat_freebsd); |
| 157 | } |
| 158 | |
| 159 | typedef wchar_t* wcscpy_func(wchar_t *s1, const wchar_t *s2); |
| 160 | DEFINE_IFUNC_FOR(wcscpy) { |
| 161 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame^] | 162 | if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(wcscpy_func, wcscpy_ssse3); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 163 | RETURN_FUNC(wcscpy_func, wcscpy_freebsd); |
| 164 | } |
| 165 | |
| 166 | } // extern "C" |