| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
| 3 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#ifndef mozilla_SIMD_h |
| 8 |
#define mozilla_SIMD_h |
| 9 |
|
| 10 |
#include "mozilla/Types.h" |
| 11 |
|
| 12 |
namespace mozilla { |
| 13 |
// A collection of SIMD-implemented algorithms. Some of these exist in the CRT. |
| 14 |
// However, the quality of the C runtime implementation varies wildly across |
| 15 |
// platforms, so these should at least ensure consistency. |
| 16 |
// |
| 17 |
// NOTE: these are currently only implemented with hand-written SIMD for x86 |
| 18 |
// and AMD64 platforms, and fallback to the the C runtime or naive loops on |
| 19 |
// other architectures. Please consider this before switching an already |
| 20 |
// optimized loop to these helpers. |
| 21 |
class SIMD { |
| 22 |
public: |
| 23 |
// NOTE: for memchr we have a goofy void* signature just to be an easy drop |
| 24 |
// in replacement for the CRT version. We also give memchr8 which is just a |
| 25 |
// typed version of memchr. |
| 26 |
static const void* memchr(const void* ptr, int value, size_t num) { |
| 27 |
return memchr8(reinterpret_cast<const char*>(ptr), static_cast<char>(value), |
| 28 |
num); |
| 29 |
} |
| 30 |
|
| 31 |
// Search through `ptr[0..length]` for the first occurrence of `value` and |
| 32 |
// return the pointer to it, or nullptr if it cannot be found. |
| 33 |
static MFBT_API const char* memchr8(const char* ptr, char value, |
| 34 |
size_t length); |
| 35 |
|
| 36 |
// This function just restricts our execution to the SSE2 path |
| 37 |
static MFBT_API const char* memchr8SSE2(const char* ptr, char value, |
| 38 |
size_t length); |
| 39 |
|
| 40 |
// This function just restricts our execution to the AVX2 path |
| 41 |
static MFBT_API const char* memchr8AVX2(const char* ptr, char value, |
| 42 |
size_t length); |
| 43 |
|
| 44 |
// Search through `ptr[0..length]` for the first occurrence of `value` and |
| 45 |
// return the pointer to it, or nullptr if it cannot be found. |
| 46 |
static MFBT_API const char16_t* memchr16(const char16_t* ptr, char16_t value, |
| 47 |
size_t length); |
| 48 |
|
| 49 |
// This function just restricts our execution to the SSE2 path |
| 50 |
static MFBT_API const char16_t* memchr16SSE2(const char16_t* ptr, |
| 51 |
char16_t value, size_t length); |
| 52 |
|
| 53 |
// This function just restricts our execution to the AVX2 path |
| 54 |
static MFBT_API const char16_t* memchr16AVX2(const char16_t* ptr, |
| 55 |
char16_t value, size_t length); |
| 56 |
|
| 57 |
// Search through `ptr[0..length]` for the first occurrence of `value` and |
| 58 |
// return the pointer to it, or nullptr if it cannot be found. |
| 59 |
static MFBT_API const uint32_t* memchr32(const uint32_t* ptr, uint32_t value, |
| 60 |
size_t length); |
| 61 |
|
| 62 |
// This function just restricts our execution to the AVX2 path |
| 63 |
static MFBT_API const uint32_t* memchr32AVX2(const uint32_t* ptr, |
| 64 |
uint32_t value, size_t length); |
| 65 |
|
| 66 |
// Search through `ptr[0..length]` for the first occurrence of `value` and |
| 67 |
// return the pointer to it, or nullptr if it cannot be found. |
| 68 |
static MFBT_API const uint64_t* memchr64(const uint64_t* ptr, uint64_t value, |
| 69 |
size_t length); |
| 70 |
|
| 71 |
// This function just restricts our execution to the AVX2 path |
| 72 |
static MFBT_API const uint64_t* memchr64AVX2(const uint64_t* ptr, |
| 73 |
uint64_t value, size_t length); |
| 74 |
|
| 75 |
// Search through `ptr[0..length]` for the first occurrence of `v1` which is |
| 76 |
// immediately followed by `v2` and return the pointer to the occurrence of |
| 77 |
// `v1`. |
| 78 |
static MFBT_API const char* memchr2x8(const char* ptr, char v1, char v2, |
| 79 |
size_t length); |
| 80 |
|
| 81 |
// Search through `ptr[0..length]` for the first occurrence of `v1` which is |
| 82 |
// immediately followed by `v2` and return the pointer to the occurrence of |
| 83 |
// `v1`. |
| 84 |
static MFBT_API const char16_t* memchr2x16(const char16_t* ptr, char16_t v1, |
| 85 |
char16_t v2, size_t length); |
| 86 |
}; |
| 87 |
|
| 88 |
} // namespace mozilla |
| 89 |
|
| 90 |
#endif // mozilla_SIMD_h |