XeTLA v0.3.6
IntelĀ® Xe Templates for Linear Algebra - API Definition Document
 
Loading...
Searching...
No Matches
fastmath.hpp
Go to the documentation of this file.
1/*******************************************************************************
2* Copyright (c) 2022-2023 Intel Corporation
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
19
20#pragma once
21
23
24namespace gpu::xetla {
25
27inline int clz(int x) {
28
29 for (int i = 31; i >= 0; i--) {
30
31 if ((1 << i) & x) { return (31 - i); }
32 }
33 return 32;
34}
35
37inline int find_log2(int x) {
38
39 int a = int(31 - clz(x));
40 a = a + ((x & (x - 1)) != 0);
41 return a;
42}
43
46struct FastDivMod {
47
49 unsigned int multiplier;
50 unsigned int shift_right;
51
53 inline FastDivMod() : divisor(0), multiplier(0), shift_right(0) {}
54
55 inline FastDivMod(int divisor_) : divisor(divisor_) {
56
57 if (divisor != 1) {
58 unsigned int p = 31 + find_log2(divisor);
59 unsigned m = unsigned(
60 ((1ull << p) + unsigned(divisor) - 1) / unsigned(divisor));
61
62 multiplier = m;
63 shift_right = p - 32;
64 } else {
65
66 multiplier = 0;
67 shift_right = 0;
68 }
69 }
70
71 operator int() const { return divisor; }
72
75 int &quotient, int &remainder, int dividend) const {
76
77 int lo_part;
78 quotient = (divisor != 1)
79 ? __ESIMD_ENS::imul(lo_part, dividend, multiplier)
81 : dividend;
82
83 remainder = dividend - (quotient * divisor);
84 }
85
87 __XETLA_API KERNEL_FUNC int div(int dividend) const {
88
89 int quotient, remainder;
90 fast_divmod(quotient, remainder, dividend);
91 return quotient;
92 }
93};
94
95} // namespace gpu::xetla
#define __XETLA_API
Definition common.hpp:43
C++ API.
#define KERNEL_FUNC
KERNEL_FUNC macro.
Definition common.hpp:39
Definition arch_config.hpp:24
int find_log2(int x)
Host side utility function to compute log2 function.
Definition fastmath.hpp:37
int clz(int x)
Host side utility function to compute number of leading zeros in the binary representation.
Definition fastmath.hpp:27
Fast division + modulus operation Host code pre-computes values to avoid expensive operations in kern...
Definition fastmath.hpp:46
__XETLA_API KERNEL_FUNC void fast_divmod(int &quotient, int &remainder, int dividend) const
Kernel side function to find quotient and remainder.
Definition fastmath.hpp:74
FastDivMod(int divisor_)
Definition fastmath.hpp:55
int divisor
Definition fastmath.hpp:48
unsigned int multiplier
Definition fastmath.hpp:49
FastDivMod()
Constructor, called in Hostcode to pre-compute multiplier and shift_right;.
Definition fastmath.hpp:53
__XETLA_API KERNEL_FUNC int div(int dividend) const
kernel side utility functions for query of quotient
Definition fastmath.hpp:87
unsigned int shift_right
Definition fastmath.hpp:50