|
| 1 | +// Copyright (C) 2020 Matthew "strager" Glazar |
| 2 | +// Copyright (c) 2014-2020, Arm Limited. |
| 3 | +// See end of file for extended copyright information. |
| 4 | + |
| 5 | +#ifndef QUICK_LINT_JS_SIMD_NEON_ARM_H |
| 6 | +#define QUICK_LINT_JS_SIMD_NEON_ARM_H |
| 7 | + |
| 8 | +#include <cstdint> |
| 9 | +#include <quick-lint-js/bit.h> |
| 10 | +#include <quick-lint-js/force-inline.h> |
| 11 | +#include <quick-lint-js/simd.h> |
| 12 | + |
| 13 | +#if QLJS_HAVE_ARM_NEON |
| 14 | +#include <arm_neon.h> |
| 15 | +#endif |
| 16 | + |
| 17 | +// Some routines have a different copyright than the rest of quick-lint-js, thus |
| 18 | +// are in this separate file. |
| 19 | + |
| 20 | +namespace quick_lint_js { |
| 21 | +#if QLJS_HAVE_ARM_NEON |
| 22 | +QLJS_FORCE_INLINE inline int bool_vector_16_neon::find_first_false() const |
| 23 | + noexcept { |
| 24 | + // You might expect a magic pattern to look like the following: |
| 25 | + // |
| 26 | + // { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x08, [repeat] } |
| 27 | + // |
| 28 | + // However, the above magic pattern requires mixing cells 3 times |
| 29 | + // (16x8 -> 8x16 -> 4x32 -> 2x64). Our magic pattern requires mixing cells |
| 30 | + // only 2 times, but creates an unusual mask (see |
| 31 | + // NOTE[find_first_false NEON mask]). |
| 32 | + ::uint8x16_t magic = { |
| 33 | + 0x01, 0x04, 0x10, 0x40, 0x01, 0x04, 0x10, 0x40, // |
| 34 | + 0x01, 0x04, 0x10, 0x40, 0x01, 0x04, 0x10, 0x40, // |
| 35 | + }; |
| 36 | + |
| 37 | + // It doesn't matter what 'garbage' is. Could be zeros or ones or anything. If |
| 38 | + // we ever extend this algorithm to uint8x32_t inputs, garbage would be the |
| 39 | + // upper 128 bits. |
| 40 | + ::uint8x16_t garbage = this->data_; |
| 41 | + |
| 42 | + // We invert the input so that we can use countr_zero instead of countr_one. |
| 43 | + // countr_one can't be used because of the zero bits in our mask (see |
| 44 | + // NOTE[find_first_false NEON mask]). |
| 45 | + ::uint8x16_t mixed_0 = ::vbicq_u8(magic, this->data_); |
| 46 | + |
| 47 | + // Mix bits to create a mask. Note that arithmetic ADD is effectively |
| 48 | + // bitwise OR. |
| 49 | + // |
| 50 | + // mixed_0: { a b c d e f g h i j k l m n o p } |
| 51 | + // mixed_1: { a+b c+d e+f g+h i+j k+l m+n o+p (64 bits unused...) } |
| 52 | + // mixed_2: { a+b+c+d e+f+g+h i+j+k+l m+n+o+p (96 bits unused...) } |
| 53 | + ::uint8x16_t mixed_1 = ::vpaddq_u8(mixed_0, garbage); |
| 54 | + ::uint8x16_t mixed_2 = ::vpaddq_u8(mixed_1, mixed_1); |
| 55 | + std::uint32_t mask = vgetq_lane_u32(::vreinterpretq_u32_u8(mixed_2), 0); |
| 56 | + |
| 57 | + // NOTE[find_first_false NEON mask]: After mixing bits, an ideal mask looks |
| 58 | + // like this: |
| 59 | + // |
| 60 | + // 0b0000000000000000ABCDEFGHIJKLMNOP |
| 61 | + // |
| 62 | + // But our mask looks like this: |
| 63 | + // |
| 64 | + // 0b0A0B0C0D0E0F0G0H0I0J0K0L0M0N0O0P |
| 65 | + // |
| 66 | + // To deal with the extra zeros, we to divide our countr_zero result by 2. |
| 67 | + return countr_zero(mask) / 2; |
| 68 | +} |
| 69 | +#endif |
| 70 | +} |
| 71 | + |
| 72 | +#endif |
| 73 | + |
| 74 | +// quick-lint-js finds bugs in JavaScript programs. |
| 75 | +// Copyright (C) 2020 Matthew "strager" Glazar |
| 76 | +// |
| 77 | +// This file is part of quick-lint-js. |
| 78 | +// |
| 79 | +// quick-lint-js is free software: you can redistribute it and/or modify |
| 80 | +// it under the terms of the GNU General Public License as published by |
| 81 | +// the Free Software Foundation, either version 3 of the License, or |
| 82 | +// (at your option) any later version. |
| 83 | +// |
| 84 | +// quick-lint-js is distributed in the hope that it will be useful, |
| 85 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 86 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 87 | +// GNU General Public License for more details. |
| 88 | +// |
| 89 | +// You should have received a copy of the GNU General Public License |
| 90 | +// along with quick-lint-js. If not, see <https://www.gnu.org/licenses/>. |
| 91 | +// |
| 92 | +// --- |
| 93 | +// |
| 94 | +// Portions are this file are |
| 95 | +// Copyright (c) 2014-2020, Arm Limited. |
| 96 | +// Source: |
| 97 | +// https://github.com/ARM-software/optimized-routines/blob/7a9fd1603e1179b044406fb9b6cc5770d736cde7/string/aarch64/memchr.S |
| 98 | +// |
| 99 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 100 | +// of this software and associated documentation files (the "Software"), to deal |
| 101 | +// in the Software without restriction, including without limitation the rights |
| 102 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 103 | +// copies of the Software, and to permit persons to whom the Software is |
| 104 | +// furnished to do so, subject to the following conditions: |
| 105 | +// |
| 106 | +// The above copyright notice and this permission notice shall be included in |
| 107 | +// all copies or substantial portions of the Software. |
| 108 | +// |
| 109 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 110 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 111 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 112 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 113 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 114 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 115 | +// SOFTWARE. |
0 commit comments