1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "hwy/examples/skeleton.h"
16 
17 #include <assert.h>
18 #include <stdio.h>
19 
20 // First undef to prevent error when re-included.
21 #undef HWY_TARGET_INCLUDE
22 // For runtime dispatch, specify the name of the current file (unfortunately
23 // __FILE__ is not reliable) so that foreach_target.h can re-include it.
24 #define HWY_TARGET_INCLUDE "hwy/examples/skeleton.cc"
25 // Generates code for each enabled target by re-including this source file.
26 #include "hwy/foreach_target.h"
27 
28 #include "hwy/highway.h"
29 
30 // Optional, can instead add HWY_ATTR to all functions.
31 HWY_BEFORE_NAMESPACE();
32 namespace skeleton {
33 namespace HWY_NAMESPACE {
34 
35 // Highway ops reside here; ADL does not find templates nor builtins.
36 using namespace hwy::HWY_NAMESPACE;
37 
38 // Computes log2 by converting to a vector of floats. Compiled once per target.
39 template <class DF>
OneFloorLog2(const DF df,const uint8_t * HWY_RESTRICT values,uint8_t * HWY_RESTRICT log2)40 HWY_NOINLINE void OneFloorLog2(const DF df, const uint8_t* HWY_RESTRICT values,
41                                uint8_t* HWY_RESTRICT log2) {
42   // Type tags for converting to other element types (Rebind = same count).
43   const Rebind<int32_t, DF> d32;
44   const Rebind<uint8_t, DF> d8;
45 
46   const auto u8 = Load(d8, values);
47   const auto bits = BitCast(d32, ConvertTo(df, PromoteTo(d32, u8)));
48   const auto exponent = ShiftRight<23>(bits) - Set(d32, 127);
49   Store(DemoteTo(d8, exponent), d8, log2);
50 }
51 
CodepathDemo()52 HWY_NOINLINE void CodepathDemo() {
53   // Highway defaults to portability, but per-target codepaths may be selected
54   // via #if HWY_TARGET == HWY_SSE4 or by testing capability macros:
55 #if HWY_CAP_INTEGER64
56   const char* gather = "Has int64";
57 #else
58   const char* gather = "No int64";
59 #endif
60   printf("Target %s: %s\n", hwy::TargetName(HWY_TARGET), gather);
61 }
62 
FloorLog2(const uint8_t * HWY_RESTRICT values,size_t count,uint8_t * HWY_RESTRICT log2)63 HWY_NOINLINE void FloorLog2(const uint8_t* HWY_RESTRICT values, size_t count,
64                             uint8_t* HWY_RESTRICT log2) {
65   CodepathDemo();
66 
67   // Second argument is necessary on RVV until it supports fractional lengths.
68   HWY_FULL(float, 4) df;
69 
70   const size_t N = Lanes(df);
71   size_t i = 0;
72   for (; i + N <= count; i += N) {
73     OneFloorLog2(df, values + i, log2 + i);
74   }
75   // TODO(janwas): implement
76 #if HWY_TARGET != HWY_RVV
77   for (; i < count; ++i) {
78     OneFloorLog2(HWY_CAPPED(float, 1)(), values + i, log2 + i);
79   }
80 #endif
81 }
82 
83 // NOLINTNEXTLINE(google-readability-namespace-comments)
84 }  // namespace HWY_NAMESPACE
85 }  // namespace skeleton
86 HWY_AFTER_NAMESPACE();
87 
88 #if HWY_ONCE
89 
90 namespace skeleton {
91 
92 // This macro declares a static array used for dynamic dispatch; it resides in
93 // the same outer namespace that contains FloorLog2.
94 HWY_EXPORT(FloorLog2);
95 
96 // This function is optional and only needed in the case of exposing it in the
97 // header file. Otherwise using HWY_DYNAMIC_DISPATCH(FloorLog2) in this module
98 // is equivalent to inlining this function.
CallFloorLog2(const uint8_t * HWY_RESTRICT in,const size_t count,uint8_t * HWY_RESTRICT out)99 void CallFloorLog2(const uint8_t* HWY_RESTRICT in, const size_t count,
100                    uint8_t* HWY_RESTRICT out) {
101   return HWY_DYNAMIC_DISPATCH(FloorLog2)(in, count, out);
102 }
103 
104 // Optional: anything to compile only once, e.g. non-SIMD implementations of
105 // public functions provided by this module, can go inside #if HWY_ONCE.
106 
107 }  // namespace skeleton
108 #endif  // HWY_ONCE
109