| 1 |
// Optimizations for random number extensions, aarch64 version -*- C++ -*- |
| 2 |
|
| 3 |
// Copyright (C) 2017-2025 Free Software Foundation, Inc. |
| 4 |
// |
| 5 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 6 |
// software; you can redistribute it and/or modify it under the |
| 7 |
// terms of the GNU General Public License as published by the |
| 8 |
// Free Software Foundation; either version 3, or (at your option) |
| 9 |
// any later version. |
| 10 |
|
| 11 |
// This 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 |
| 14 |
// GNU General Public License for more details. |
| 15 |
|
| 16 |
// Under Section 7 of GPL version 3, you are granted additional |
| 17 |
// permissions described in the GCC Runtime Library Exception, version |
| 18 |
// 3.1, as published by the Free Software Foundation. |
| 19 |
|
| 20 |
// You should have received a copy of the GNU General Public License and |
| 21 |
// a copy of the GCC Runtime Library Exception along with this program; |
| 22 |
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 |
// <http://www.gnu.org/licenses/>. |
| 24 |
|
| 25 |
/** @file ext/opt_random.h |
| 26 |
* This is an internal header file, included by other library headers. |
| 27 |
* Do not attempt to use it directly. @headername{ext/random} |
| 28 |
*/ |
| 29 |
|
| 30 |
#ifndef _EXT_OPT_RANDOM_H |
| 31 |
#define _EXT_OPT_RANDOM_H 1 |
| 32 |
|
| 33 |
#pragma GCC system_header |
| 34 |
|
| 35 |
#ifdef __ARM_NEON |
| 36 |
|
| 37 |
#ifdef __ARM_BIG_ENDIAN |
| 38 |
# define __VEXT(_A,_B,_C) __builtin_shufflevector (_A, _B, \ |
| 39 |
16-_C, 17-_C, 18-_C, 19-_C, 20-_C, 21-_C, 22-_C, 23-_C, \ |
| 40 |
24-_C, 25-_C, 26-_C, 27-_C, 28-_C, 29-_C, 30-_C, 31-_C) |
| 41 |
#else |
| 42 |
# define __VEXT(_A,_B,_C) __builtin_shufflevector (_B, _A, \ |
| 43 |
_C, _C+1, _C+2, _C+3, _C+4, _C+5, _C+6, _C+7, \ |
| 44 |
_C+8, _C+9, _C+10, _C+11, _C+12, _C+13, _C+14, _C+15) |
| 45 |
#endif |
| 46 |
|
| 47 |
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 48 |
namespace __gnu_cxx _GLIBCXX_VISIBILITY (default) |
| 49 |
{ |
| 50 |
_GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 51 |
|
| 52 |
namespace { |
| 53 |
// Logical Shift right 128-bits by c * 8 bits |
| 54 |
|
| 55 |
__extension__ |
| 56 |
template<int __c> |
| 57 |
extern __inline __Uint32x4_t |
| 58 |
__attribute__ ((__always_inline__, __gnu_inline__, __artificial__)) |
| 59 |
__aarch64_lsr_128 (__Uint8x16_t __a) |
| 60 |
{ |
| 61 |
const __Uint8x16_t __zero = {0, 0, 0, 0, 0, 0, 0, 0, |
| 62 |
0, 0, 0, 0, 0, 0, 0, 0}; |
| 63 |
|
| 64 |
return (__Uint32x4_t) __VEXT (__zero, __a, __c); |
| 65 |
} |
| 66 |
|
| 67 |
// Logical Shift left 128-bits by c * 8 bits |
| 68 |
|
| 69 |
__extension__ |
| 70 |
template<int __c> |
| 71 |
extern __inline __Uint32x4_t |
| 72 |
__attribute__ ((__always_inline__, __gnu_inline__, __artificial__)) |
| 73 |
__aarch64_lsl_128 (__Uint8x16_t __a) |
| 74 |
{ |
| 75 |
const __Uint8x16_t __zero = {0, 0, 0, 0, 0, 0, 0, 0, |
| 76 |
0, 0, 0, 0, 0, 0, 0, 0}; |
| 77 |
|
| 78 |
return (__Uint32x4_t) __VEXT (__a, __zero, 16 - __c); |
| 79 |
} |
| 80 |
|
| 81 |
template<size_t __sl1, size_t __sl2, size_t __sr1, size_t __sr2> |
| 82 |
inline __Uint32x4_t __aarch64_recursion (__Uint32x4_t __a, |
| 83 |
__Uint32x4_t __b, |
| 84 |
__Uint32x4_t __c, |
| 85 |
__Uint32x4_t __d, |
| 86 |
__Uint32x4_t __e) |
| 87 |
{ |
| 88 |
__Uint32x4_t __y = (__b >> __sr1); |
| 89 |
__Uint32x4_t __z = __aarch64_lsr_128<__sr2> ((__Uint8x16_t) __c); |
| 90 |
|
| 91 |
__Uint32x4_t __v = __d << __sl1; |
| 92 |
|
| 93 |
__z = __z ^ __a; |
| 94 |
__z = __z ^ __v; |
| 95 |
|
| 96 |
__Uint32x4_t __x = __aarch64_lsl_128<__sl2> ((__Uint8x16_t) __a); |
| 97 |
|
| 98 |
__y = __y & __e; |
| 99 |
__z = __z ^ __x; |
| 100 |
return __z ^ __y; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
#define _GLIBCXX_OPT_HAVE_RANDOM_SFMT_GEN_READ 1 |
| 105 |
template<typename _UIntType, size_t __m, |
| 106 |
size_t __pos1, size_t __sl1, size_t __sl2, |
| 107 |
size_t __sr1, size_t __sr2, |
| 108 |
uint32_t __msk1, uint32_t __msk2, |
| 109 |
uint32_t __msk3, uint32_t __msk4, |
| 110 |
uint32_t __parity1, uint32_t __parity2, |
| 111 |
uint32_t __parity3, uint32_t __parity4> |
| 112 |
void simd_fast_mersenne_twister_engine<_UIntType, __m, |
| 113 |
__pos1, __sl1, __sl2, __sr1, __sr2, |
| 114 |
__msk1, __msk2, __msk3, __msk4, |
| 115 |
__parity1, __parity2, __parity3, |
| 116 |
__parity4>:: |
| 117 |
_M_gen_rand (void) |
| 118 |
{ |
| 119 |
__Uint32x4_t __r1 = _M_state[_M_nstate - 2]; |
| 120 |
__Uint32x4_t __r2 = _M_state[_M_nstate - 1]; |
| 121 |
|
| 122 |
__Uint32x4_t __aData = {__msk1, __msk2, __msk3, __msk4}; |
| 123 |
|
| 124 |
size_t __i; |
| 125 |
for (__i = 0; __i < _M_nstate - __pos1; ++__i) |
| 126 |
{ |
| 127 |
__Uint32x4_t __r = __aarch64_recursion<__sl1, __sl2, __sr1, __sr2> |
| 128 |
(_M_state[__i], _M_state[__i + __pos1], __r1, __r2, __aData); |
| 129 |
|
| 130 |
_M_state[__i] = __r; |
| 131 |
|
| 132 |
__r1 = __r2; |
| 133 |
__r2 = __r; |
| 134 |
} |
| 135 |
for (; __i < _M_nstate; ++__i) |
| 136 |
{ |
| 137 |
__Uint32x4_t __r = __aarch64_recursion<__sl1, __sl2, __sr1, __sr2> |
| 138 |
(_M_state[__i], _M_state[__i + __pos1 - _M_nstate], __r1, __r2, |
| 139 |
__aData); |
| 140 |
|
| 141 |
_M_state[__i] = __r; |
| 142 |
|
| 143 |
__r1 = __r2; |
| 144 |
__r2 = __r; |
| 145 |
} |
| 146 |
|
| 147 |
_M_pos = 0; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
#define _GLIBCXX_OPT_HAVE_RANDOM_SFMT_OPERATOREQUAL 1 |
| 152 |
template<typename _UIntType, size_t __m, |
| 153 |
size_t __pos1, size_t __sl1, size_t __sl2, |
| 154 |
size_t __sr1, size_t __sr2, |
| 155 |
uint32_t __msk1, uint32_t __msk2, |
| 156 |
uint32_t __msk3, uint32_t __msk4, |
| 157 |
uint32_t __parity1, uint32_t __parity2, |
| 158 |
uint32_t __parity3, uint32_t __parity4> |
| 159 |
bool |
| 160 |
operator==(const __gnu_cxx::simd_fast_mersenne_twister_engine<_UIntType, |
| 161 |
__m, __pos1, __sl1, __sl2, __sr1, __sr2, |
| 162 |
__msk1, __msk2, __msk3, __msk4, |
| 163 |
__parity1, __parity2, __parity3, __parity4>& __lhs, |
| 164 |
const __gnu_cxx::simd_fast_mersenne_twister_engine<_UIntType, |
| 165 |
__m, __pos1, __sl1, __sl2, __sr1, __sr2, |
| 166 |
__msk1, __msk2, __msk3, __msk4, |
| 167 |
__parity1, __parity2, __parity3, __parity4>& __rhs) |
| 168 |
{ |
| 169 |
if (__lhs._M_pos != __rhs._M_pos) |
| 170 |
return false; |
| 171 |
|
| 172 |
__Uint32x4_t __res = __lhs._M_state[0] ^ __rhs._M_state[0]; |
| 173 |
|
| 174 |
for (size_t __i = 1; __i < __lhs._M_nstate; ++__i) |
| 175 |
__res |= __lhs._M_state[__i] ^ __rhs._M_state[__i]; |
| 176 |
|
| 177 |
return (__int128) __res == 0; |
| 178 |
} |
| 179 |
|
| 180 |
_GLIBCXX_END_NAMESPACE_VERSION |
| 181 |
} // namespace |
| 182 |
|
| 183 |
#endif // __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 184 |
#endif // __ARM_NEON |
| 185 |
|
| 186 |
#endif // _EXT_OPT_RANDOM_H |