|
| 1 | +// Copyright 2026 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. |
| 15 | +// |
| 16 | +#include <stddef.h> |
| 17 | +#include <stdint.h> |
| 18 | + |
| 19 | +#include <iostream> |
| 20 | +#include <numeric> |
| 21 | +#include <vector> |
| 22 | + |
| 23 | +#undef HWY_TARGET_INCLUDE |
| 24 | +#define HWY_TARGET_INCLUDE "hwy/examples/dot_product_mixed_precision.cc" |
| 25 | +#include "hwy/foreach_target.h" // IWYU pragma: keep |
| 26 | +#include "hwy/highway.h" |
| 27 | +#include "hwy/nanobenchmark.h" |
| 28 | +#include "hwy/timer.h" |
| 29 | + |
| 30 | +/* |
| 31 | +Highway SIMD Tutorial: Mixed precision dot product |
| 32 | +
|
| 33 | +This example demonstrates how to perform a dot product where the inputs are |
| 34 | +uint8_t and are promoted uint32_t to prevent overflow. |
| 35 | +
|
| 36 | +*/ |
| 37 | + |
| 38 | +HWY_BEFORE_NAMESPACE(); |
| 39 | +namespace hwy { |
| 40 | +namespace HWY_NAMESPACE { |
| 41 | +namespace hn = hwy::HWY_NAMESPACE; |
| 42 | + |
| 43 | +uint32_t DotProductSIMD(const uint8_t* HWY_RESTRICT a, |
| 44 | + const uint8_t* HWY_RESTRICT b, size_t count) { |
| 45 | + using DU8 = hn::ScalableTag<uint8_t>; |
| 46 | + const DU8 du8; |
| 47 | + using DU32 = hn::ScalableTag<uint32_t>; |
| 48 | + const DU32 du32; |
| 49 | + using V = hn::Vec<DU32>; |
| 50 | + V sum = hn::Zero(du32); |
| 51 | + const size_t N8 = hn::Lanes(du8); |
| 52 | + size_t i = 0; |
| 53 | + if (count >= N8) { |
| 54 | + for (; i <= count - N8; i += N8) { |
| 55 | + sum = hn::SumOfMulQuadAccumulate(du32, hn::LoadU(du8, a + i), |
| 56 | + hn::LoadU(du8, b + i), sum); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // Use LoadN for remainder, sets 0 for values beyond remainder |
| 61 | + size_t remainder = count - i; |
| 62 | + HWY_DASSERT(remainder < N8); |
| 63 | + if (remainder > 0) { |
| 64 | + sum = hn::SumOfMulQuadAccumulate(du32, hn::LoadN(du8, a + i, remainder), |
| 65 | + hn::LoadN(du8, b + i, remainder), sum); |
| 66 | + } |
| 67 | + |
| 68 | + uint32_t total = hn::ReduceSum(du32, sum); |
| 69 | + |
| 70 | + return total; |
| 71 | +} |
| 72 | + |
| 73 | +} // namespace HWY_NAMESPACE |
| 74 | +} // namespace hwy |
| 75 | +HWY_AFTER_NAMESPACE(); |
| 76 | + |
| 77 | +#if HWY_ONCE |
| 78 | +namespace hwy { |
| 79 | +HWY_EXPORT(DotProductSIMD); |
| 80 | + |
| 81 | +uint32_t DotProductScalar(const uint8_t* HWY_RESTRICT a, |
| 82 | + const uint8_t* HWY_RESTRICT b, size_t count) { |
| 83 | + size_t i = 0; |
| 84 | + uint32_t total = 0; |
| 85 | + // Scalar dot product with type casting |
| 86 | + for (; i < count; ++i) { |
| 87 | + total += static_cast<uint32_t>(a[i]) * static_cast<uint32_t>(b[i]); |
| 88 | + } |
| 89 | + return total; |
| 90 | +} |
| 91 | + |
| 92 | +int Run() { |
| 93 | + const size_t count = 10000025; |
| 94 | + std::vector<uint8_t> a(count); |
| 95 | + std::vector<uint8_t> b(count); |
| 96 | + std::iota(a.begin(), a.end(), 1); |
| 97 | + std::iota(b.begin(), b.end(), 1); |
| 98 | + // Record start time |
| 99 | + const double t_scalar_0 = hwy::platform::Now(); |
| 100 | + uint32_t dpScalar = hwy::DotProductScalar(a.data(), b.data(), count); |
| 101 | + // Record end time and print execution time and dot product |
| 102 | + const double t_scalar_1 = hwy::platform::Now(); |
| 103 | + const double dt_scalar = 1000.0 * (t_scalar_1 - t_scalar_0); |
| 104 | + std::cout << "Scalar Execution time: " << dt_scalar << " ms" << std::endl; |
| 105 | + std::cout << "Scalar Dot product: " << dpScalar << std::endl; |
| 106 | + // Record start time |
| 107 | + const double t_simd_0 = hwy::platform::Now(); |
| 108 | + uint32_t dpSIMD = |
| 109 | + HWY_DYNAMIC_DISPATCH(DotProductSIMD)(a.data(), b.data(), count); |
| 110 | + // Record stop timer and print time and dot product |
| 111 | + const double t_simd_1 = hwy::platform::Now(); |
| 112 | + const double dt_simd = 1000.0 * (t_simd_1 - t_simd_0); |
| 113 | + std::cout << "SIMD Execution time: " << dt_simd << " ms" << std::endl; |
| 114 | + std::cout << "SIMD Dot product: " << dpSIMD << std::endl; |
| 115 | + return 0; |
| 116 | +} |
| 117 | +} // namespace hwy |
| 118 | + |
| 119 | +int main() { return hwy::Run(); } |
| 120 | +#endif // HWY_ONCE |
0 commit comments