S/390: Fix optimized mem* running on 31 bit kernels.
[platform/upstream/glibc.git] / sysdeps / s390 / s390-32 / multiarch / ifunc-resolve.c
1 /* IFUNC resolver function for CPU specific functions.
2    32 bit S/390 version.
3    Copyright (C) 2012 Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, see
18    <http://www.gnu.org/licenses/>.  */
19
20 #include <unistd.h>
21 #include <dl-procinfo.h>
22
23 #define STFLE_BITS_Z10  34 /* General instructions extension */
24 #define STFLE_BITS_Z196 45 /* Distinct operands, pop ... */
25
26 #ifndef NOT_IN_libc
27
28 #define IFUNC_RESOLVE(FUNC)                                             \
29   asm (".globl " #FUNC "\n\t"                                           \
30        ".type  " #FUNC ",@gnu_indirect_function\n\t"                    \
31        ".set   " #FUNC ",resolve_" #FUNC "\n\t"                         \
32        ".globl __GI_" #FUNC "\n\t"                                      \
33        ".set   __GI_" #FUNC "," #FUNC "\n");                            \
34                                                                         \
35   /* Make the declarations of the optimized functions hidden in order
36      to prevent GOT slots being generated for them. */                  \
37   extern __attribute__((visibility("hidden"))) void *FUNC##_z196;       \
38   extern __attribute__((visibility("hidden"))) void *FUNC##_z10;        \
39   extern __attribute__((visibility("hidden"))) void *FUNC##_g5;         \
40                                                                         \
41   void *resolve_##FUNC (unsigned long int dl_hwcap)                     \
42   {                                                                     \
43     if ((dl_hwcap & HWCAP_S390_STFLE)                                   \
44         && (dl_hwcap & HWCAP_S390_ZARCH)                                \
45         && (dl_hwcap & HWCAP_S390_HIGH_GPRS))                           \
46       {                                                                 \
47         /* We want just 1 double word to be returned.  */               \
48         register unsigned long reg0 asm("0") = 0;                       \
49         unsigned long long stfle_bits;                                  \
50                                                                         \
51         asm volatile(".insn s,0xb2b00000,%0" "\n\t"  /* stfle */        \
52                      : "=QS" (stfle_bits), "+d" (reg0)                  \
53                      : : "cc");                                         \
54                                                                         \
55         if ((stfle_bits & (1ULL << (63 - STFLE_BITS_Z196))) != 0)       \
56           return &FUNC##_z196;                                          \
57         else if ((stfle_bits & (1ULL << (63 - STFLE_BITS_Z10))) != 0)   \
58           return &FUNC##_z10;                                           \
59       }                                                                 \
60     return &FUNC##_g5;                                                  \
61   }
62
63 IFUNC_RESOLVE(memset)
64 IFUNC_RESOLVE(memcmp)
65 asm(".weak bcmp ; bcmp = memcmp");
66
67 /* In the static lib memcpy is needed before the reloc is resolved.  */
68 #ifdef SHARED
69 IFUNC_RESOLVE(memcpy)
70 #endif
71
72 #endif