Bug 1861709 replace AudioCallbackDriver::ThreadRunning() assertions that mean to...
[gecko.git] / third_party / highway / hwy / examples / skeleton.cc
blob778ba4ac0afe9f72a8d4b03bc0799af48bba9a31
1 // Copyright 2020 Google LLC
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
16 #include "hwy/examples/skeleton.h"
18 #include <stdio.h>
20 // >>>> for dynamic dispatch only, skip if you want static dispatch
22 // First undef to prevent error when re-included.
23 #undef HWY_TARGET_INCLUDE
24 // For dynamic dispatch, specify the name of the current file (unfortunately
25 // __FILE__ is not reliable) so that foreach_target.h can re-include it.
26 #define HWY_TARGET_INCLUDE "hwy/examples/skeleton.cc"
27 // Generates code for each enabled target by re-including this source file.
28 #include "hwy/foreach_target.h" // IWYU pragma: keep
30 // <<<< end of dynamic dispatch
32 // Must come after foreach_target.h to avoid redefinition errors.
33 #include "hwy/highway.h"
35 // Optional, can instead add HWY_ATTR to all functions.
36 HWY_BEFORE_NAMESPACE();
38 namespace skeleton {
39 // This namespace name is unique per target, which allows code for multiple
40 // targets to co-exist in the same translation unit. Required when using dynamic
41 // dispatch, otherwise optional.
42 namespace HWY_NAMESPACE {
44 // Highway ops reside here; ADL does not find templates nor builtins.
45 namespace hn = hwy::HWY_NAMESPACE;
47 // Computes log2 by converting to a vector of floats. Compiled once per target.
48 template <class DF>
49 HWY_ATTR_NO_MSAN void OneFloorLog2(const DF df,
50 const uint8_t* HWY_RESTRICT values,
51 uint8_t* HWY_RESTRICT log2) {
52 // Type tags for converting to other element types (Rebind = same count).
53 const hn::RebindToSigned<DF> d32;
54 const hn::Rebind<uint8_t, DF> d8;
55 using VI32 = hn::Vec<decltype(d32)>;
57 const VI32 vi32 = hn::PromoteTo(d32, hn::Load(d8, values));
58 const VI32 bits = hn::BitCast(d32, hn::ConvertTo(df, vi32));
59 const VI32 exponent = hn::Sub(hn::ShiftRight<23>(bits), hn::Set(d32, 127));
60 hn::Store(hn::DemoteTo(d8, exponent), d8, log2);
63 void CodepathDemo() {
64 // Highway defaults to portability, but per-target codepaths may be selected
65 // via #if HWY_TARGET == HWY_SSE4 or by testing capability macros:
66 #if HWY_HAVE_INTEGER64
67 const char* gather = "Has int64";
68 #else
69 const char* gather = "No int64";
70 #endif
71 printf("Target %s: %s\n", hwy::TargetName(HWY_TARGET), gather);
74 void FloorLog2(const uint8_t* HWY_RESTRICT values, size_t count,
75 uint8_t* HWY_RESTRICT log2) {
76 CodepathDemo();
78 const hn::ScalableTag<float> df;
79 const size_t N = hn::Lanes(df);
80 size_t i = 0;
81 for (; i + N <= count; i += N) {
82 OneFloorLog2(df, values + i, log2 + i);
84 for (; i < count; ++i) {
85 hn::CappedTag<float, 1> d1;
86 OneFloorLog2(d1, values + i, log2 + i);
90 // NOLINTNEXTLINE(google-readability-namespace-comments)
91 } // namespace HWY_NAMESPACE
92 } // namespace skeleton
93 HWY_AFTER_NAMESPACE();
95 // The table of pointers to the various implementations in HWY_NAMESPACE must
96 // be compiled only once (foreach_target #includes this file multiple times).
97 // HWY_ONCE is true for only one of these 'compilation passes'.
98 #if HWY_ONCE
100 namespace skeleton {
102 // This macro declares a static array used for dynamic dispatch; it resides in
103 // the same outer namespace that contains FloorLog2.
104 HWY_EXPORT(FloorLog2);
106 // This function is optional and only needed in the case of exposing it in the
107 // header file. Otherwise using HWY_DYNAMIC_DISPATCH(FloorLog2) in this module
108 // is equivalent to inlining this function.
109 HWY_DLLEXPORT void CallFloorLog2(const uint8_t* HWY_RESTRICT in,
110 const size_t count,
111 uint8_t* HWY_RESTRICT out) {
112 // This must reside outside of HWY_NAMESPACE because it references (calls the
113 // appropriate one from) the per-target implementations there.
114 // For static dispatch, use HWY_STATIC_DISPATCH.
115 return HWY_DYNAMIC_DISPATCH(FloorLog2)(in, count, out);
118 // Optional: anything to compile only once, e.g. non-SIMD implementations of
119 // public functions provided by this module, can go inside #if HWY_ONCE.
121 } // namespace skeleton
122 #endif // HWY_ONCE