Skip to content

Commit 2851957

Browse files
committed
Document constant<T> and tiling
1 parent 33245d7 commit 2851957

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

docs/api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ API Reference
1111
api/fast_math.rst
1212
api/conditional.rst
1313
api/memory_read_write.rst
14+
api/utilities.rst
1415

docs/build_api.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ def build_doxygen_page(name, items):
2727
content += "-" * len(title) + "\n"
2828

2929
for symbol in symbols:
30-
content += f".. doxygen{directive}:: kernel_float::{symbol}\n\n"
30+
if directive == "define":
31+
content += f".. doxygendefine:: {symbol}\n\n"
32+
else:
33+
content += f".. doxygen{directive}:: kernel_float::{symbol}\n\n"
3134

3235
stripped_name = name.lower().replace(" ", "_").replace("/", "_")
3336
filename = f"api/{stripped_name}.rst"
@@ -218,6 +221,11 @@ def build_index_page(groups):
218221
("storen", "storen(const V&, T*, size_t)"),
219222
("storen", "storen(const V&, T*, size_t, size_t)"),
220223
("aligned_ptr", "aligned_ptr", "struct"),
224+
],
225+
"Utilities": [
226+
("constant", "constant", "struct"),
227+
("tiling", "tiling", "struct"),
228+
("KERNEL_FLOAT_TILING_FOR", "KERNEL_FLOAT_TILING_FOR", "define"),
221229
]
222230
}
223231

docs/guides.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ Guides
66
guides/introduction.rst
77
guides/promotion.rst
88
guides/prelude.rst
9+
guides/constant.rst

docs/guides/constant.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Using `kernel_float::constant`
2+
===
3+
4+
When working with mixed precision types, you will find that working with constants presents a bit a challenge.
5+
6+
For example, a simple expression such as `3.14 * x` where `x` is of type `vec<float, 2>` will NOT be performed
7+
in `float` precision as you might expect, but instead in `double` precision.
8+
This happens since the left-hand side of this expression
9+
(a constant) is a `double` and thus `kernel_float` will also cast the right-hand side to `double`.
10+
11+
To solve this problem, `kernel_float` offers a type called `constant<T>` that can be used to represents
12+
constants. Any binary operations between a value of type `U` and a `constant<T>` will result in both
13+
operands being cast to type `U` and the operation is performed in the precision of type `U`. This makes
14+
`constant<T>` useful for representing constant in your code.
15+
16+
17+
For example, consider the following code:
18+
19+
```
20+
#include "kernel_float.h"
21+
namespace kf = kernel_float;
22+
23+
int main() {
24+
using Type = float;
25+
const int N = 8;
26+
static constexpr auto PI = kf::make_constant(3.14);
27+
28+
kf::vec<int, N> i = kf::range<int, N>();
29+
kf::vec<Type, N> x = kf::cast<Type>(i) * PI;
30+
kf::vec<Type, N> y = x * kf::sin(x);
31+
Type result = kf::sum(y);
32+
printf("result=%f", double(result));
33+
34+
return EXIT_SUCCESS;
35+
}
36+
```
37+
38+
This code example uses the ``make_constant`` utility function to create `constant<T>`.

0 commit comments

Comments
 (0)