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 | |
Elliott Hughes | a4c7876 | 2019-09-17 09:53:14 -0700 | [diff] [blame^] | 31 | #include <private/bionic_ifuncs.h> |
| 32 | |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 33 | extern "C" { |
| 34 | |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 35 | typedef int memcmp_func(const void* __lhs, const void* __rhs, size_t __n); |
| 36 | DEFINE_IFUNC_FOR(memcmp) { |
| 37 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame] | 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); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 40 | RETURN_FUNC(memcmp_func, memcmp_generic); |
| 41 | } |
| 42 | |
| 43 | typedef void* memset_func(void* __dst, int __ch, size_t __n); |
| 44 | DEFINE_IFUNC_FOR(memset) { |
| 45 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame] | 46 | if (__builtin_cpu_is("atom")) RETURN_FUNC(memset_func, memset_atom); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 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); |
| 51 | DEFINE_IFUNC_FOR(__memset_chk) { |
| 52 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame] | 53 | 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] | 54 | RETURN_FUNC(__memset_chk_func, __memset_chk_generic); |
| 55 | } |
| 56 | |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 57 | typedef void* memmove_func(void* __dst, const void* __src, size_t __n); |
| 58 | DEFINE_IFUNC_FOR(memmove) { |
| 59 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame] | 60 | if (__builtin_cpu_is("atom")) RETURN_FUNC(memmove_func, memmove_atom); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 61 | RETURN_FUNC(memmove_func, memmove_generic); |
| 62 | } |
| 63 | |
Haibo Huang | e141362 | 2018-11-13 14:20:43 -0800 | [diff] [blame] | 64 | typedef void* memcpy_func(void*, const void*, size_t); |
| 65 | DEFINE_IFUNC_FOR(memcpy) { |
| 66 | return memmove_resolver(); |
| 67 | } |
| 68 | |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 69 | typedef char* strcpy_func(char* __dst, const char* __src); |
| 70 | DEFINE_IFUNC_FOR(strcpy) { |
| 71 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame] | 72 | if (__builtin_cpu_is("atom")) RETURN_FUNC(strcpy_func, strcpy_atom); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 73 | RETURN_FUNC(strcpy_func, strcpy_generic); |
| 74 | } |
| 75 | |
| 76 | typedef char* strncpy_func(char* __dst, const char* __src, size_t __n); |
| 77 | DEFINE_IFUNC_FOR(strncpy) { |
| 78 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame] | 79 | if (__builtin_cpu_is("atom")) RETURN_FUNC(strncpy_func, strncpy_atom); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 80 | RETURN_FUNC(strncpy_func, strncpy_generic); |
| 81 | } |
| 82 | |
| 83 | typedef size_t strlen_func(const char* __s); |
| 84 | DEFINE_IFUNC_FOR(strlen) { |
| 85 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame] | 86 | if (__builtin_cpu_is("atom")) RETURN_FUNC(strlen_func, strlen_atom); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 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); |
| 91 | DEFINE_IFUNC_FOR(wmemcmp) { |
| 92 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame] | 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); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 95 | RETURN_FUNC(wmemcmp_func, wmemcmp_freebsd); |
| 96 | } |
| 97 | |
Shalini Salomi Bodapati | 4ed2f47 | 2019-06-13 09:54:08 +0530 | [diff] [blame] | 98 | typedef int wmemset_func(const wchar_t* __lhs, const wchar_t* __rhs, size_t __n); |
| 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 | |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 105 | typedef int strcmp_func(const char* __lhs, const char* __rhs); |
| 106 | DEFINE_IFUNC_FOR(strcmp) { |
| 107 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame] | 108 | if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(strcmp_func, strcmp_ssse3); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 109 | RETURN_FUNC(strcmp_func, strcmp_generic); |
| 110 | } |
| 111 | |
| 112 | typedef int strncmp_func(const char* __lhs, const char* __rhs, size_t __n); |
| 113 | DEFINE_IFUNC_FOR(strncmp) { |
| 114 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame] | 115 | if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(strncmp_func, strncmp_ssse3); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 116 | RETURN_FUNC(strncmp_func, strncmp_generic); |
| 117 | } |
| 118 | |
| 119 | typedef char* strcat_func(char* __dst, const char* __src); |
| 120 | DEFINE_IFUNC_FOR(strcat) { |
| 121 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame] | 122 | if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(strcat_func, strcat_ssse3); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 123 | RETURN_FUNC(strcat_func, strcat_generic); |
| 124 | } |
| 125 | |
| 126 | typedef char* strncat_func(char* __dst, const char* __src, size_t __n); |
| 127 | DEFINE_IFUNC_FOR(strncat) { |
| 128 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame] | 129 | if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(strncat_func, strncat_ssse3); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 130 | RETURN_FUNC(strncat_func, strncat_openbsd); |
| 131 | } |
| 132 | |
| 133 | typedef size_t strlcat_func(char *dst, const char *src, size_t dsize); |
| 134 | DEFINE_IFUNC_FOR(strlcat) { |
| 135 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame] | 136 | if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(strlcat_func, strlcat_ssse3); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 137 | RETURN_FUNC(strlcat_func, strlcat_openbsd); |
| 138 | } |
| 139 | |
| 140 | typedef size_t strlcpy_func(char *dst, const char *src, size_t dsize); |
| 141 | DEFINE_IFUNC_FOR(strlcpy) { |
| 142 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame] | 143 | if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(strlcpy_func, strlcpy_ssse3); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 144 | RETURN_FUNC(strlcpy_func, strlcpy_openbsd); |
| 145 | } |
| 146 | |
| 147 | typedef wchar_t* wcscat_func(wchar_t *s1, const wchar_t *s2); |
| 148 | DEFINE_IFUNC_FOR(wcscat) { |
| 149 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame] | 150 | if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(wcscat_func, wcscat_ssse3); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 151 | RETURN_FUNC(wcscat_func, wcscat_freebsd); |
| 152 | } |
| 153 | |
| 154 | typedef wchar_t* wcscpy_func(wchar_t *s1, const wchar_t *s2); |
| 155 | DEFINE_IFUNC_FOR(wcscpy) { |
| 156 | __builtin_cpu_init(); |
Haibo Huang | 021d522 | 2018-12-21 14:54:47 -0800 | [diff] [blame] | 157 | if (__builtin_cpu_supports("ssse3")) RETURN_FUNC(wcscpy_func, wcscpy_ssse3); |
Haibo Huang | b9244ff | 2018-08-11 10:12:13 -0700 | [diff] [blame] | 158 | RETURN_FUNC(wcscpy_func, wcscpy_freebsd); |
| 159 | } |
| 160 | |
| 161 | } // extern "C" |