Halide  12.0.1
Halide compiler and libraries
ConciseCasts.h
Go to the documentation of this file.
1 #ifndef HALIDE_CONCISE_CASTS_H
2 #define HALIDE_CONCISE_CASTS_H
3 
4 #include "IROperator.h"
5 
6 /** \file
7  *
8  * Defines concise cast and saturating cast operators to make it
9  * easier to read cast-heavy code. Think carefully about the
10  * readability implications before using these. They could make your
11  * code better or worse. Often it's better to add extra Funcs to your
12  * pipeline that do the upcasting and downcasting.
13  */
14 
15 namespace Halide {
16 namespace ConciseCasts {
17 
18 inline Expr f64(Expr e) {
19  Type t = Float(64, e.type().lanes());
20  return cast(t, std::move(e));
21 }
22 
23 inline Expr f32(Expr e) {
24  Type t = Float(32, e.type().lanes());
25  return cast(t, std::move(e));
26 }
27 
28 inline Expr bf16(Expr e) {
29  Type t = BFloat(16, e.type().lanes());
30  return cast(t, std::move(e));
31 }
32 
33 inline Expr i64(Expr e) {
34  Type t = Int(64, e.type().lanes());
35  return cast(t, std::move(e));
36 }
37 
38 inline Expr i32(Expr e) {
39  Type t = Int(32, e.type().lanes());
40  return cast(t, std::move(e));
41 }
42 
43 inline Expr i16(Expr e) {
44  Type t = Int(16, e.type().lanes());
45  return cast(t, std::move(e));
46 }
47 
48 inline Expr i8(Expr e) {
49  Type t = Int(8, e.type().lanes());
50  return cast(t, std::move(e));
51 }
52 
53 inline Expr u64(Expr e) {
54  Type t = UInt(64, e.type().lanes());
55  return cast(t, std::move(e));
56 }
57 
58 inline Expr u32(Expr e) {
59  Type t = UInt(32, e.type().lanes());
60  return cast(t, std::move(e));
61 }
62 
63 inline Expr u16(Expr e) {
64  Type t = UInt(16, e.type().lanes());
65  return cast(t, std::move(e));
66 }
67 
68 inline Expr u8(Expr e) {
69  Type t = UInt(8, e.type().lanes());
70  return cast(t, std::move(e));
71 }
72 
73 inline Expr i8_sat(Expr e) {
74  Type t = Int(8, e.type().lanes());
75  return saturating_cast(t, std::move(e));
76 }
77 
78 inline Expr u8_sat(Expr e) {
79  Type t = UInt(8, e.type().lanes());
80  return saturating_cast(t, std::move(e));
81 }
82 
83 inline Expr i16_sat(Expr e) {
84  Type t = Int(16, e.type().lanes());
85  return saturating_cast(t, std::move(e));
86 }
87 
88 inline Expr u16_sat(Expr e) {
89  Type t = UInt(16, e.type().lanes());
90  return saturating_cast(t, std::move(e));
91 }
92 
93 inline Expr i32_sat(Expr e) {
94  Type t = Int(32, e.type().lanes());
95  return saturating_cast(t, std::move(e));
96 }
97 
98 inline Expr u32_sat(Expr e) {
99  Type t = UInt(32, e.type().lanes());
100  return saturating_cast(t, std::move(e));
101 }
102 
103 inline Expr i64_sat(Expr e) {
104  Type t = Int(64, e.type().lanes());
105  return saturating_cast(t, std::move(e));
106 }
107 
108 inline Expr u64_sat(Expr e) {
109  Type t = UInt(64, e.type().lanes());
110  return saturating_cast(t, std::move(e));
111 }
112 
113 }; // namespace ConciseCasts
114 }; // namespace Halide
115 
116 #endif
Defines various operator overloads and utility functions that make it more pleasant to work with Hali...
Expr i64_sat(Expr e)
Definition: ConciseCasts.h:103
Expr u16_sat(Expr e)
Definition: ConciseCasts.h:88
Expr i8_sat(Expr e)
Definition: ConciseCasts.h:73
Expr u32(Expr e)
Definition: ConciseCasts.h:58
Expr u8_sat(Expr e)
Definition: ConciseCasts.h:78
Expr i32(Expr e)
Definition: ConciseCasts.h:38
Expr bf16(Expr e)
Definition: ConciseCasts.h:28
Expr u64_sat(Expr e)
Definition: ConciseCasts.h:108
Expr f64(Expr e)
Definition: ConciseCasts.h:18
Expr u16(Expr e)
Definition: ConciseCasts.h:63
Expr i16_sat(Expr e)
Definition: ConciseCasts.h:83
Expr f32(Expr e)
Definition: ConciseCasts.h:23
Expr u64(Expr e)
Definition: ConciseCasts.h:53
Expr u32_sat(Expr e)
Definition: ConciseCasts.h:98
Expr i64(Expr e)
Definition: ConciseCasts.h:33
Expr i16(Expr e)
Definition: ConciseCasts.h:43
Expr i32_sat(Expr e)
Definition: ConciseCasts.h:93
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
Type BFloat(int bits, int lanes=1)
Construct a floating-point type in the bfloat format.
Definition: Type.h:513
Type UInt(int bits, int lanes=1)
Constructing an unsigned integer type.
Definition: Type.h:503
Type Float(int bits, int lanes=1)
Construct a floating-point type.
Definition: Type.h:508
Expr cast(Expr a)
Cast an expression to the halide type corresponding to the C++ type T.
Definition: IROperator.h:387
Expr saturating_cast(Expr e)
Cast an expression to the halide type corresponding to the C++ type T.
Definition: IROperator.h:1418
Type Int(int bits, int lanes=1)
Constructing a signed integer type.
Definition: Type.h:498
A fragment of Halide syntax.
Definition: Expr.h:256
HALIDE_ALWAYS_INLINE Type type() const
Get the type of this expression node.
Definition: Expr.h:320
Types in the halide type system.
Definition: Type.h:269
HALIDE_ALWAYS_INLINE int lanes() const
Return the number of vector elements in this type.
Definition: Type.h:337