Halide  12.0.1
Halide compiler and libraries
IROperator.h
Go to the documentation of this file.
1 #ifndef HALIDE_IR_OPERATOR_H
2 #define HALIDE_IR_OPERATOR_H
3 
4 /** \file
5  *
6  * Defines various operator overloads and utility functions that make
7  * it more pleasant to work with Halide expressions.
8  */
9 
10 #include <cmath>
11 
12 #include "Expr.h"
13 #include "Tuple.h"
14 
15 namespace Halide {
16 
17 namespace Internal {
18 /** Is the expression either an IntImm, a FloatImm, a StringImm, or a
19  * Cast of the same, or a Ramp or Broadcast of the same. Doesn't do
20  * any constant folding. */
21 bool is_const(const Expr &e);
22 
23 /** Is the expression an IntImm, FloatImm of a particular value, or a
24  * Cast, or Broadcast of the same. */
25 bool is_const(const Expr &e, int64_t v);
26 
27 /** If an expression is an IntImm or a Broadcast of an IntImm, return
28  * a pointer to its value. Otherwise returns nullptr. */
29 const int64_t *as_const_int(const Expr &e);
30 
31 /** If an expression is a UIntImm or a Broadcast of a UIntImm, return
32  * a pointer to its value. Otherwise returns nullptr. */
33 const uint64_t *as_const_uint(const Expr &e);
34 
35 /** If an expression is a FloatImm or a Broadcast of a FloatImm,
36  * return a pointer to its value. Otherwise returns nullptr. */
37 const double *as_const_float(const Expr &e);
38 
39 /** Is the expression a constant integer power of two. Also returns
40  * log base two of the expression if it is. Only returns true for
41  * integer types. */
42 bool is_const_power_of_two_integer(const Expr &e, int *bits);
43 
44 /** Is the expression a const (as defined by is_const), and also
45  * strictly greater than zero (in all lanes, if a vector expression) */
46 bool is_positive_const(const Expr &e);
47 
48 /** Is the expression a const (as defined by is_const), and also
49  * strictly less than zero (in all lanes, if a vector expression) */
50 bool is_negative_const(const Expr &e);
51 
52 /** Is the expression an undef */
53 bool is_undef(const Expr &e);
54 
55 /** Is the expression a const (as defined by is_const), and also equal
56  * to zero (in all lanes, if a vector expression) */
57 bool is_const_zero(const Expr &e);
58 
59 /** Is the expression a const (as defined by is_const), and also equal
60  * to one (in all lanes, if a vector expression) */
61 bool is_const_one(const Expr &e);
62 
63 /** Is the statement a no-op (which we represent as either an
64  * undefined Stmt, or as an Evaluate node of a constant) */
65 bool is_no_op(const Stmt &s);
66 
67 /** Does the expression
68  * 1) Take on the same value no matter where it appears in a Stmt, and
69  * 2) Evaluating it has no side-effects
70  */
71 bool is_pure(const Expr &e);
72 
73 /** Construct an immediate of the given type from any numeric C++ type. */
74 // @{
77 Expr make_const(Type t, double val);
78 inline Expr make_const(Type t, int32_t val) {
79  return make_const(t, (int64_t)val);
80 }
81 inline Expr make_const(Type t, uint32_t val) {
82  return make_const(t, (uint64_t)val);
83 }
84 inline Expr make_const(Type t, int16_t val) {
85  return make_const(t, (int64_t)val);
86 }
87 inline Expr make_const(Type t, uint16_t val) {
88  return make_const(t, (uint64_t)val);
89 }
90 inline Expr make_const(Type t, int8_t val) {
91  return make_const(t, (int64_t)val);
92 }
93 inline Expr make_const(Type t, uint8_t val) {
94  return make_const(t, (uint64_t)val);
95 }
96 inline Expr make_const(Type t, bool val) {
97  return make_const(t, (uint64_t)val);
98 }
99 inline Expr make_const(Type t, float val) {
100  return make_const(t, (double)val);
101 }
102 inline Expr make_const(Type t, float16_t val) {
103  return make_const(t, (double)val);
104 }
105 // @}
106 
107 /** Construct a unique signed_integer_overflow Expr */
109 
110 /** Check if a constant value can be correctly represented as the given type. */
112 
113 /** Construct a boolean constant from a C++ boolean value.
114  * May also be a vector if width is given.
115  * It is not possible to coerce a C++ boolean to Expr because
116  * if we provide such a path then char objects can ambiguously
117  * be converted to Halide Expr or to std::string. The problem
118  * is that C++ does not have a real bool type - it is in fact
119  * close enough to char that C++ does not know how to distinguish them.
120  * make_bool is the explicit coercion. */
121 Expr make_bool(bool val, int lanes = 1);
122 
123 /** Construct the representation of zero in the given type */
125 
126 /** Construct the representation of one in the given type */
128 
129 /** Construct the representation of two in the given type */
131 
132 /** Construct the constant boolean true. May also be a vector of
133  * trues, if a lanes argument is given. */
134 Expr const_true(int lanes = 1);
135 
136 /** Construct the constant boolean false. May also be a vector of
137  * falses, if a lanes argument is given. */
138 Expr const_false(int lanes = 1);
139 
140 /** Attempt to cast an expression to a smaller type while provably not
141  * losing information. If it can't be done, return an undefined
142  * Expr. */
144 
145 /** Attempt to negate x without introducing new IR and without overflow.
146  * If it can't be done, return an undefined Expr. */
148 
149 /** Coerce the two expressions to have the same type, using C-style
150  * casting rules. For the purposes of casting, a boolean type is
151  * UInt(1). We use the following procedure:
152  *
153  * If the types already match, do nothing.
154  *
155  * Then, if one type is a vector and the other is a scalar, the scalar
156  * is broadcast to match the vector width, and we continue.
157  *
158  * Then, if one type is floating-point and the other is not, the
159  * non-float is cast to the floating-point type, and we're done.
160  *
161  * Then, if both types are unsigned ints, the one with fewer bits is
162  * cast to match the one with more bits and we're done.
163  *
164  * Then, if both types are signed ints, the one with fewer bits is
165  * cast to match the one with more bits and we're done.
166  *
167  * Finally, if one type is an unsigned int and the other type is a signed
168  * int, both are cast to a signed int with the greater of the two
169  * bit-widths. For example, matching an Int(8) with a UInt(16) results
170  * in an Int(16).
171  *
172  */
173 void match_types(Expr &a, Expr &b);
174 
175 /** Asserts that both expressions are integer types and are either
176  * both signed or both unsigned. If one argument is scalar and the
177  * other a vector, the scalar is broadcasted to have the same number
178  * of lanes as the vector. If one expression is of narrower type than
179  * the other, it is widened to the bit width of the wider. */
180 void match_types_bitwise(Expr &a, Expr &b, const char *op_name);
181 
182 /** Halide's vectorizable transcendentals. */
183 // @{
184 Expr halide_log(const Expr &a);
185 Expr halide_exp(const Expr &a);
186 Expr halide_erf(const Expr &a);
187 // @}
188 
189 /** Raise an expression to an integer power by repeatedly multiplying
190  * it by itself. */
192 
193 /** Split a boolean condition into vector of ANDs. If 'cond' is undefined,
194  * return an empty vector. */
195 void split_into_ands(const Expr &cond, std::vector<Expr> &result);
196 
197 /** A builder to help create Exprs representing halide_buffer_t
198  * structs (e.g. foo.buffer) via calls to halide_buffer_init. Fill out
199  * the fields and then call build. The resulting Expr will be a call
200  * to halide_buffer_init with the struct members as arguments. If the
201  * buffer_memory field is undefined, it uses a call to alloca to make
202  * some stack memory for the buffer. If the shape_memory field is
203  * undefined, it similarly uses stack memory for the shape. If the
204  * shape_memory field is null, it uses the dim field already in the
205  * buffer. Other unitialized fields will take on a value of zero in
206  * the constructed buffer. */
211  int dimensions = 0;
212  std::vector<Expr> mins, extents, strides;
214  Expr build() const;
215 };
216 
217 /** If e is a ramp expression with stride, default 1, return the base,
218  * otherwise undefined. */
219 Expr strided_ramp_base(const Expr &e, int stride = 1);
220 
221 /** Implementations of division and mod that are specific to Halide.
222  * Use these implementations; do not use native C division or mod to
223  * simplify Halide expressions. Halide division and modulo satisify
224  * the Euclidean definition of division for integers a and b:
225  *
226  /code
227  when b != 0, (a/b)*b + a%b = a
228  0 <= a%b < |b|
229  /endcode
230  *
231  * Additionally, mod by zero returns zero, and div by zero returns
232  * zero. This makes mod and div total functions.
233  */
234 // @{
235 template<typename T>
236 inline T mod_imp(T a, T b) {
237  Type t = type_of<T>();
238  if (!t.is_float() && b == 0) {
239  return 0;
240  } else if (t.is_int()) {
241  int64_t ia = a;
242  int64_t ib = b;
243  int64_t a_neg = ia >> 63;
244  int64_t b_neg = ib >> 63;
245  int64_t b_zero = (ib == 0) ? -1 : 0;
246  ia -= a_neg;
247  int64_t r = ia % (ib | b_zero);
248  r += (a_neg & ((ib ^ b_neg) + ~b_neg));
249  r &= ~b_zero;
250  return r;
251  } else {
252  return a % b;
253  }
254 }
255 
256 template<typename T>
257 inline T div_imp(T a, T b) {
258  Type t = type_of<T>();
259  if (!t.is_float() && b == 0) {
260  return (T)0;
261  } else if (t.is_int()) {
262  // Do it as 64-bit
263  int64_t ia = a;
264  int64_t ib = b;
265  int64_t a_neg = ia >> 63;
266  int64_t b_neg = ib >> 63;
267  int64_t b_zero = (ib == 0) ? -1 : 0;
268  ib -= b_zero;
269  ia -= a_neg;
270  int64_t q = ia / ib;
271  q += a_neg & (~b_neg - b_neg);
272  q &= ~b_zero;
273  return (T)q;
274  } else {
275  return a / b;
276  }
277 }
278 // @}
279 
280 // Special cases for float, double.
281 template<>
282 inline float mod_imp<float>(float a, float b) {
283  float f = a - b * (floorf(a / b));
284  // The remainder has the same sign as b.
285  return f;
286 }
287 template<>
288 inline double mod_imp<double>(double a, double b) {
289  double f = a - b * (std::floor(a / b));
290  return f;
291 }
292 
293 template<>
294 inline float div_imp<float>(float a, float b) {
295  return a / b;
296 }
297 template<>
298 inline double div_imp<double>(double a, double b) {
299  return a / b;
300 }
301 
302 /** Return an Expr that is identical to the input Expr, but with
303  * all calls to likely() and likely_if_innermost() removed. */
305 
306 /** Return a Stmt that is identical to the input Stmt, but with
307  * all calls to likely() and likely_if_innermost() removed. */
309 
310 /** If the expression is a tag helper call, remove it and return
311  * the tagged expression. If not, returns the expression. */
313 
314 /** Expressions tagged with this intrinsic are suggestions that
315  * vectorization of loops with guard ifs should be implemented with
316  * non-faulting predicated loads and stores, instead of scalarizing
317  * an if statement. */
319 
320 // Secondary args to print can be Exprs or const char *
321 inline HALIDE_NO_USER_CODE_INLINE void collect_print_args(std::vector<Expr> &args) {
322 }
323 
324 template<typename... Args>
325 inline HALIDE_NO_USER_CODE_INLINE void collect_print_args(std::vector<Expr> &args, const char *arg, Args &&...more_args) {
326  args.emplace_back(std::string(arg));
327  collect_print_args(args, std::forward<Args>(more_args)...);
328 }
329 
330 template<typename... Args>
331 inline HALIDE_NO_USER_CODE_INLINE void collect_print_args(std::vector<Expr> &args, Expr arg, Args &&...more_args) {
332  args.push_back(std::move(arg));
333  collect_print_args(args, std::forward<Args>(more_args)...);
334 }
335 
336 Expr requirement_failed_error(Expr condition, const std::vector<Expr> &args);
337 
338 Expr memoize_tag_helper(Expr result, const std::vector<Expr> &cache_key_values);
339 
340 /** Compute widen(a) + widen(b). The result is always signed. */
342 /** Compute widen(a) * widen(b). a and b may have different signedness. */
344 /** Compute widen(a) - widen(b). The result is always signed. */
346 /** Compute widen(a) << b. */
349 /** Compute widen(a) >> b. */
352 
353 /** Compute saturating_add(a, (1 >> min(b, 0)) / 2) << b. When b is positive
354  * indicating a left shift, the rounding term is zero. */
357 /** Compute saturating_add(a, (1 << max(b, 0)) / 2) >> b. When b is negative
358  * indicating a left shift, the rounding term is zero. */
361 
362 /** Compute saturating_narrow(widen(a) + widen(b)) */
364 /** Compute saturating_narrow(widen(a) - widen(b)) */
366 
367 /** Compute narrow((widen(a) + widen(b)) / 2) */
369 /** Compute narrow((widen(a) + widen(b) + 1) / 2) */
371 /** Compute narrow((widen(a) - widen(b)) / 2) */
373 /** Compute narrow((widen(a) - widen(b) + 1) / 2) */
375 
376 /** Compute saturating_narrow(shift_right(widening_mul(a, b), q)) */
379 /** Compute saturating_narrow(rounding_shift_right(widening_mul(a, b), q)) */
382 
383 } // namespace Internal
384 
385 /** Cast an expression to the halide type corresponding to the C++ type T. */
386 template<typename T>
387 inline Expr cast(Expr a) {
388  return cast(type_of<T>(), std::move(a));
389 }
390 
391 /** Cast an expression to a new type. */
393 
394 /** Return the sum of two expressions, doing any necessary type
395  * coercion using \ref Internal::match_types */
397 
398 /** Add an expression and a constant integer. Coerces the type of the
399  * integer to match the type of the expression. Errors if the integer
400  * cannot be represented in the type of the expression. */
401 // @{
402 Expr operator+(Expr a, int b);
403 
404 /** Add a constant integer and an expression. Coerces the type of the
405  * integer to match the type of the expression. Errors if the integer
406  * cannot be represented in the type of the expression. */
407 Expr operator+(int a, Expr b);
408 
409 /** Modify the first expression to be the sum of two expressions,
410  * without changing its type. This casts the second argument to match
411  * the type of the first. */
413 
414 /** Return the difference of two expressions, doing any necessary type
415  * coercion using \ref Internal::match_types */
417 
418 /** Subtracts a constant integer from an expression. Coerces the type of the
419  * integer to match the type of the expression. Errors if the integer
420  * cannot be represented in the type of the expression. */
421 Expr operator-(Expr a, int b);
422 
423 /** Subtracts an expression from a constant integer. Coerces the type
424  * of the integer to match the type of the expression. Errors if the
425  * integer cannot be represented in the type of the expression. */
426 Expr operator-(int a, Expr b);
427 
428 /** Return the negative of the argument. Does no type casting, so more
429  * formally: return that number which when added to the original,
430  * yields zero of the same type. For unsigned integers the negative is
431  * still an unsigned integer. E.g. in UInt(8), the negative of 56 is
432  * 200, because 56 + 200 == 0 */
434 
435 /** Modify the first expression to be the difference of two expressions,
436  * without changing its type. This casts the second argument to match
437  * the type of the first. */
439 
440 /** Return the product of two expressions, doing any necessary type
441  * coercion using \ref Internal::match_types */
443 
444 /** Multiply an expression and a constant integer. Coerces the type of the
445  * integer to match the type of the expression. Errors if the integer
446  * cannot be represented in the type of the expression. */
447 Expr operator*(Expr a, int b);
448 
449 /** Multiply a constant integer and an expression. Coerces the type of
450  * the integer to match the type of the expression. Errors if the
451  * integer cannot be represented in the type of the expression. */
452 Expr operator*(int a, Expr b);
453 
454 /** Modify the first expression to be the product of two expressions,
455  * without changing its type. This casts the second argument to match
456  * the type of the first. */
458 
459 /** Return the ratio of two expressions, doing any necessary type
460  * coercion using \ref Internal::match_types. Note that integer
461  * division in Halide is not the same as integer division in C-like
462  * languages in two ways.
463  *
464  * First, signed integer division in Halide rounds according to the
465  * sign of the denominator. This means towards minus infinity for
466  * positive denominators, and towards positive infinity for negative
467  * denominators. This is unlike C, which rounds towards zero. This
468  * decision ensures that upsampling expressions like f(x/2, y/2) don't
469  * have funny discontinuities when x and y cross zero.
470  *
471  * Second, division by zero returns zero instead of faulting. For
472  * types where overflow is defined behavior, division of the largest
473  * negative signed integer by -1 returns the larged negative signed
474  * integer for the type (i.e. it wraps). This ensures that a division
475  * operation can never have a side-effect, which is helpful in Halide
476  * because scheduling directives can expand the domain of computation
477  * of a Func, potentially introducing new zero-division.
478  */
480 
481 /** Modify the first expression to be the ratio of two expressions,
482  * without changing its type. This casts the second argument to match
483  * the type of the first. Note that signed integer division in Halide
484  * rounds towards minus infinity, unlike C, which rounds towards
485  * zero. */
487 
488 /** Divides an expression by a constant integer. Coerces the type
489  * of the integer to match the type of the expression. Errors if the
490  * integer cannot be represented in the type of the expression. */
491 Expr operator/(Expr a, int b);
492 
493 /** Divides a constant integer by an expression. Coerces the type
494  * of the integer to match the type of the expression. Errors if the
495  * integer cannot be represented in the type of the expression. */
496 Expr operator/(int a, Expr b);
497 
498 /** Return the first argument reduced modulo the second, doing any
499  * necessary type coercion using \ref Internal::match_types. There are
500  * two key differences between C-like languages and Halide for the
501  * modulo operation, which complement the way division works.
502  *
503  * First, the result is never negative, so x % 2 is always zero or
504  * one, unlike in C-like languages. x % -2 is equivalent, and is also
505  * always zero or one. Second, mod by zero evaluates to zero (unlike
506  * in C, where it faults). This makes modulo, like division, a
507  * side-effect-free operation. */
509 
510 /** Mods an expression by a constant integer. Coerces the type
511  * of the integer to match the type of the expression. Errors if the
512  * integer cannot be represented in the type of the expression. */
513 Expr operator%(Expr a, int b);
514 
515 /** Mods a constant integer by an expression. Coerces the type
516  * of the integer to match the type of the expression. Errors if the
517  * integer cannot be represented in the type of the expression. */
518 Expr operator%(int a, Expr b);
519 
520 /** Return a boolean expression that tests whether the first argument
521  * is greater than the second, after doing any necessary type coercion
522  * using \ref Internal::match_types */
524 
525 /** Return a boolean expression that tests whether an expression is
526  * greater than a constant integer. Coerces the integer to the type of
527  * the expression. Errors if the integer is not representable in that
528  * type. */
529 Expr operator>(Expr a, int b);
530 
531 /** Return a boolean expression that tests whether a constant integer is
532  * greater than an expression. Coerces the integer to the type of
533  * the expression. Errors if the integer is not representable in that
534  * type. */
535 Expr operator>(int a, Expr b);
536 
537 /** Return a boolean expression that tests whether the first argument
538  * is less than the second, after doing any necessary type coercion
539  * using \ref Internal::match_types */
541 
542 /** Return a boolean expression that tests whether an expression is
543  * less than a constant integer. Coerces the integer to the type of
544  * the expression. Errors if the integer is not representable in that
545  * type. */
546 Expr operator<(Expr a, int b);
547 
548 /** Return a boolean expression that tests whether a constant integer is
549  * less than an expression. Coerces the integer to the type of
550  * the expression. Errors if the integer is not representable in that
551  * type. */
552 Expr operator<(int a, Expr b);
553 
554 /** Return a boolean expression that tests whether the first argument
555  * is less than or equal to the second, after doing any necessary type
556  * coercion using \ref Internal::match_types */
558 
559 /** Return a boolean expression that tests whether an expression is
560  * less than or equal to a constant integer. Coerces the integer to
561  * the type of the expression. Errors if the integer is not
562  * representable in that type. */
563 Expr operator<=(Expr a, int b);
564 
565 /** Return a boolean expression that tests whether a constant integer
566  * is less than or equal to an expression. Coerces the integer to the
567  * type of the expression. Errors if the integer is not representable
568  * in that type. */
569 Expr operator<=(int a, Expr b);
570 
571 /** Return a boolean expression that tests whether the first argument
572  * is greater than or equal to the second, after doing any necessary
573  * type coercion using \ref Internal::match_types */
575 
576 /** Return a boolean expression that tests whether an expression is
577  * greater than or equal to a constant integer. Coerces the integer to
578  * the type of the expression. Errors if the integer is not
579  * representable in that type. */
580 Expr operator>=(const Expr &a, int b);
581 
582 /** Return a boolean expression that tests whether a constant integer
583  * is greater than or equal to an expression. Coerces the integer to the
584  * type of the expression. Errors if the integer is not representable
585  * in that type. */
586 Expr operator>=(int a, const Expr &b);
587 
588 /** Return a boolean expression that tests whether the first argument
589  * is equal to the second, after doing any necessary type coercion
590  * using \ref Internal::match_types */
592 
593 /** Return a boolean expression that tests whether an expression is
594  * equal to a constant integer. Coerces the integer to the type of the
595  * expression. Errors if the integer is not representable in that
596  * type. */
597 Expr operator==(Expr a, int b);
598 
599 /** Return a boolean expression that tests whether a constant integer
600  * is equal to an expression. Coerces the integer to the type of the
601  * expression. Errors if the integer is not representable in that
602  * type. */
603 Expr operator==(int a, Expr b);
604 
605 /** Return a boolean expression that tests whether the first argument
606  * is not equal to the second, after doing any necessary type coercion
607  * using \ref Internal::match_types */
609 
610 /** Return a boolean expression that tests whether an expression is
611  * not equal to a constant integer. Coerces the integer to the type of
612  * the expression. Errors if the integer is not representable in that
613  * type. */
614 Expr operator!=(Expr a, int b);
615 
616 /** Return a boolean expression that tests whether a constant integer
617  * is not equal to an expression. Coerces the integer to the type of
618  * the expression. Errors if the integer is not representable in that
619  * type. */
620 Expr operator!=(int a, Expr b);
621 
622 /** Returns the logical and of the two arguments */
624 
625 /** Logical and of an Expr and a bool. Either returns the Expr or an
626  * Expr representing false, depending on the bool. */
627 // @{
628 Expr operator&&(Expr a, bool b);
629 Expr operator&&(bool a, Expr b);
630 // @}
631 
632 /** Returns the logical or of the two arguments */
634 
635 /** Logical or of an Expr and a bool. Either returns the Expr or an
636  * Expr representing true, depending on the bool. */
637 // @{
638 Expr operator||(Expr a, bool b);
639 Expr operator||(bool a, Expr b);
640 // @}
641 
642 /** Returns the logical not the argument */
644 
645 /** Returns an expression representing the greater of the two
646  * arguments, after doing any necessary type coercion using
647  * \ref Internal::match_types. Vectorizes cleanly on most platforms
648  * (with the exception of integer types on x86 without SSE4). */
650 
651 /** Returns an expression representing the greater of an expression
652  * and a constant integer. The integer is coerced to the type of the
653  * expression. Errors if the integer is not representable as that
654  * type. Vectorizes cleanly on most platforms (with the exception of
655  * integer types on x86 without SSE4). */
656 Expr max(Expr a, int b);
657 
658 /** Returns an expression representing the greater of a constant
659  * integer and an expression. The integer is coerced to the type of
660  * the expression. Errors if the integer is not representable as that
661  * type. Vectorizes cleanly on most platforms (with the exception of
662  * integer types on x86 without SSE4). */
663 Expr max(int a, Expr b);
664 
665 inline Expr max(float a, Expr b) {
666  return max(Expr(a), std::move(b));
667 }
668 inline Expr max(Expr a, float b) {
669  return max(std::move(a), Expr(b));
670 }
671 
672 /** Returns an expression representing the greater of an expressions
673  * vector, after doing any necessary type coersion using
674  * \ref Internal::match_types. Vectorizes cleanly on most platforms
675  * (with the exception of integer types on x86 without SSE4).
676  * The expressions are folded from right ie. max(.., max(.., ..)).
677  * The arguments can be any mix of types but must all be convertible to Expr. */
678 template<typename A, typename B, typename C, typename... Rest,
679  typename std::enable_if<Halide::Internal::all_are_convertible<Expr, Rest...>::value>::type * = nullptr>
680 inline Expr max(A &&a, B &&b, C &&c, Rest &&...rest) {
681  return max(std::forward<A>(a), max(std::forward<B>(b), std::forward<C>(c), std::forward<Rest>(rest)...));
682 }
683 
685 
686 /** Returns an expression representing the lesser of an expression
687  * and a constant integer. The integer is coerced to the type of the
688  * expression. Errors if the integer is not representable as that
689  * type. Vectorizes cleanly on most platforms (with the exception of
690  * integer types on x86 without SSE4). */
691 Expr min(Expr a, int b);
692 
693 /** Returns an expression representing the lesser of a constant
694  * integer and an expression. The integer is coerced to the type of
695  * the expression. Errors if the integer is not representable as that
696  * type. Vectorizes cleanly on most platforms (with the exception of
697  * integer types on x86 without SSE4). */
698 Expr min(int a, Expr b);
699 
700 inline Expr min(float a, Expr b) {
701  return min(Expr(a), std::move(b));
702 }
703 inline Expr min(Expr a, float b) {
704  return min(std::move(a), Expr(b));
705 }
706 
707 /** Returns an expression representing the lesser of an expressions
708  * vector, after doing any necessary type coersion using
709  * \ref Internal::match_types. Vectorizes cleanly on most platforms
710  * (with the exception of integer types on x86 without SSE4).
711  * The expressions are folded from right ie. min(.., min(.., ..)).
712  * The arguments can be any mix of types but must all be convertible to Expr. */
713 template<typename A, typename B, typename C, typename... Rest,
714  typename std::enable_if<Halide::Internal::all_are_convertible<Expr, Rest...>::value>::type * = nullptr>
715 inline Expr min(A &&a, B &&b, C &&c, Rest &&...rest) {
716  return min(std::forward<A>(a), min(std::forward<B>(b), std::forward<C>(c), std::forward<Rest>(rest)...));
717 }
718 
719 /** Operators on floats treats those floats as Exprs. Making these
720  * explicit prevents implicit float->int casts that might otherwise
721  * occur. */
722 // @{
723 inline Expr operator+(Expr a, float b) {
724  return std::move(a) + Expr(b);
725 }
726 inline Expr operator+(float a, Expr b) {
727  return Expr(a) + std::move(b);
728 }
729 inline Expr operator-(Expr a, float b) {
730  return std::move(a) - Expr(b);
731 }
732 inline Expr operator-(float a, Expr b) {
733  return Expr(a) - std::move(b);
734 }
735 inline Expr operator*(Expr a, float b) {
736  return std::move(a) * Expr(b);
737 }
738 inline Expr operator*(float a, Expr b) {
739  return Expr(a) * std::move(b);
740 }
741 inline Expr operator/(Expr a, float b) {
742  return std::move(a) / Expr(b);
743 }
744 inline Expr operator/(float a, Expr b) {
745  return Expr(a) / std::move(b);
746 }
747 inline Expr operator%(Expr a, float b) {
748  return std::move(a) % Expr(b);
749 }
750 inline Expr operator%(float a, Expr b) {
751  return Expr(a) % std::move(b);
752 }
753 inline Expr operator>(Expr a, float b) {
754  return std::move(a) > Expr(b);
755 }
756 inline Expr operator>(float a, Expr b) {
757  return Expr(a) > std::move(b);
758 }
759 inline Expr operator<(Expr a, float b) {
760  return std::move(a) < Expr(b);
761 }
762 inline Expr operator<(float a, Expr b) {
763  return Expr(a) < std::move(b);
764 }
765 inline Expr operator>=(Expr a, float b) {
766  return std::move(a) >= Expr(b);
767 }
768 inline Expr operator>=(float a, Expr b) {
769  return Expr(a) >= std::move(b);
770 }
771 inline Expr operator<=(Expr a, float b) {
772  return std::move(a) <= Expr(b);
773 }
774 inline Expr operator<=(float a, Expr b) {
775  return Expr(a) <= std::move(b);
776 }
777 inline Expr operator==(Expr a, float b) {
778  return std::move(a) == Expr(b);
779 }
780 inline Expr operator==(float a, Expr b) {
781  return Expr(a) == std::move(b);
782 }
783 inline Expr operator!=(Expr a, float b) {
784  return std::move(a) != Expr(b);
785 }
786 inline Expr operator!=(float a, Expr b) {
787  return Expr(a) != std::move(b);
788 }
789 // @}
790 
791 /** Clamps an expression to lie within the given bounds. The bounds
792  * are type-cast to match the expression. Vectorizes as well as min/max. */
793 Expr clamp(Expr a, const Expr &min_val, const Expr &max_val);
794 
795 /** Returns the absolute value of a signed integer or floating-point
796  * expression. Vectorizes cleanly. Unlike in C, abs of a signed
797  * integer returns an unsigned integer of the same bit width. This
798  * means that abs of the most negative integer doesn't overflow. */
800 
801 /** Return the absolute difference between two values. Vectorizes
802  * cleanly. Returns an unsigned value of the same bit width. There are
803  * various ways to write this yourself, but they contain numerous
804  * gotchas and don't always compile to good code, so use this
805  * instead. */
807 
808 /** Returns an expression similar to the ternary operator in C, except
809  * that it always evaluates all arguments. If the first argument is
810  * true, then return the second, else return the third. Typically
811  * vectorizes cleanly, but benefits from SSE41 or newer on x86. */
812 Expr select(Expr condition, Expr true_value, Expr false_value);
813 
814 /** A multi-way variant of select similar to a switch statement in C,
815  * which can accept multiple conditions and values in pairs. Evaluates
816  * to the first value for which the condition is true. Returns the
817  * final value if all conditions are false. */
818 template<typename... Args,
819  typename std::enable_if<Halide::Internal::all_are_convertible<Expr, Args...>::value>::type * = nullptr>
820 inline Expr select(Expr c0, Expr v0, Expr c1, Expr v1, Args &&...args) {
821  return select(std::move(c0), std::move(v0), select(std::move(c1), std::move(v1), std::forward<Args>(args)...));
822 }
823 
824 /** Equivalent of ternary select(), but taking/returning tuples. If the condition is
825  * a Tuple, it must match the size of the true and false Tuples. */
826 // @{
827 Tuple tuple_select(const Tuple &condition, const Tuple &true_value, const Tuple &false_value);
828 Tuple tuple_select(const Expr &condition, const Tuple &true_value, const Tuple &false_value);
829 // @}
830 
831 /** Equivalent of multiway select(), but taking/returning tuples. If the condition is
832  * a Tuple, it must match the size of the true and false Tuples. */
833 // @{
834 template<typename... Args>
835 inline Tuple tuple_select(const Tuple &c0, const Tuple &v0, const Tuple &c1, const Tuple &v1, Args &&...args) {
836  return tuple_select(c0, v0, tuple_select(c1, v1, std::forward<Args>(args)...));
837 }
838 
839 template<typename... Args>
840 inline Tuple tuple_select(const Expr &c0, const Tuple &v0, const Expr &c1, const Tuple &v1, Args &&...args) {
841  return tuple_select(c0, v0, tuple_select(c1, v1, std::forward<Args>(args)...));
842 }
843 // @}
844 
845 /** Oftentimes we want to pack a list of expressions with the same type
846  * into a channel dimension, e.g.,
847  * img(x, y, c) = select(c == 0, 100, // Red
848  * c == 1, 50, // Green
849  * 25); // Blue
850  * This is tedious when the list is long. The following function
851  * provide convinent syntax that allow one to write:
852  * img(x, y, c) = mux(c, {100, 50, 25});
853  *
854  * As with the select equivalent, if the first argument (the index) is
855  * out of range, the expression evaluates to the last value.
856  */
857 // @{
858 Expr mux(const Expr &id, const std::initializer_list<Expr> &values);
859 Expr mux(const Expr &id, const std::vector<Expr> &values);
860 Expr mux(const Expr &id, const Tuple &values);
861 // @}
862 
863 /** Return the sine of a floating-point expression. If the argument is
864  * not floating-point, it is cast to Float(32). Does not vectorize
865  * well. */
867 
868 /** Return the arcsine of a floating-point expression. If the argument
869  * is not floating-point, it is cast to Float(32). Does not vectorize
870  * well. */
872 
873 /** Return the cosine of a floating-point expression. If the argument
874  * is not floating-point, it is cast to Float(32). Does not vectorize
875  * well. */
877 
878 /** Return the arccosine of a floating-point expression. If the
879  * argument is not floating-point, it is cast to Float(32). Does not
880  * vectorize well. */
882 
883 /** Return the tangent of a floating-point expression. If the argument
884  * is not floating-point, it is cast to Float(32). Does not vectorize
885  * well. */
887 
888 /** Return the arctangent of a floating-point expression. If the
889  * argument is not floating-point, it is cast to Float(32). Does not
890  * vectorize well. */
892 
893 /** Return the angle of a floating-point gradient. If the argument is
894  * not floating-point, it is cast to Float(32). Does not vectorize
895  * well. */
897 
898 /** Return the hyperbolic sine of a floating-point expression. If the
899  * argument is not floating-point, it is cast to Float(32). Does not
900  * vectorize well. */
902 
903 /** Return the hyperbolic arcsinhe of a floating-point expression. If
904  * the argument is not floating-point, it is cast to Float(32). Does
905  * not vectorize well. */
907 
908 /** Return the hyperbolic cosine of a floating-point expression. If
909  * the argument is not floating-point, it is cast to Float(32). Does
910  * not vectorize well. */
912 
913 /** Return the hyperbolic arccosine of a floating-point expression.
914  * If the argument is not floating-point, it is cast to
915  * Float(32). Does not vectorize well. */
917 
918 /** Return the hyperbolic tangent of a floating-point expression. If
919  * the argument is not floating-point, it is cast to Float(32). Does
920  * not vectorize well. */
922 
923 /** Return the hyperbolic arctangent of a floating-point expression.
924  * If the argument is not floating-point, it is cast to
925  * Float(32). Does not vectorize well. */
927 
928 /** Return the square root of a floating-point expression. If the
929  * argument is not floating-point, it is cast to Float(32). Typically
930  * vectorizes cleanly. */
932 
933 /** Return the square root of the sum of the squares of two
934  * floating-point expressions. If the argument is not floating-point,
935  * it is cast to Float(32). Vectorizes cleanly. */
936 Expr hypot(const Expr &x, const Expr &y);
937 
938 /** Return the exponential of a floating-point expression. If the
939  * argument is not floating-point, it is cast to Float(32). For
940  * Float(64) arguments, this calls the system exp function, and does
941  * not vectorize well. For Float(32) arguments, this function is
942  * vectorizable, does the right thing for extremely small or extremely
943  * large inputs, and is accurate up to the last bit of the
944  * mantissa. Vectorizes cleanly. */
946 
947 /** Return the logarithm of a floating-point expression. If the
948  * argument is not floating-point, it is cast to Float(32). For
949  * Float(64) arguments, this calls the system log function, and does
950  * not vectorize well. For Float(32) arguments, this function is
951  * vectorizable, does the right thing for inputs <= 0 (returns -inf or
952  * nan), and is accurate up to the last bit of the
953  * mantissa. Vectorizes cleanly. */
955 
956 /** Return one floating point expression raised to the power of
957  * another. The type of the result is given by the type of the first
958  * argument. If the first argument is not a floating-point type, it is
959  * cast to Float(32). For Float(32), cleanly vectorizable, and
960  * accurate up to the last few bits of the mantissa. Gets worse when
961  * approaching overflow. Vectorizes cleanly. */
963 
964 /** Evaluate the error function erf. Only available for
965  * Float(32). Accurate up to the last three bits of the
966  * mantissa. Vectorizes cleanly. */
967 Expr erf(const Expr &x);
968 
969 /** Fast vectorizable approximation to some trigonometric functions for Float(32).
970  * Absolute approximation error is less than 1e-5. */
971 // @{
972 Expr fast_sin(const Expr &x);
973 Expr fast_cos(const Expr &x);
974 // @}
975 
976 /** Fast approximate cleanly vectorizable log for Float(32). Returns
977  * nonsense for x <= 0.0f. Accurate up to the last 5 bits of the
978  * mantissa. Vectorizes cleanly. */
979 Expr fast_log(const Expr &x);
980 
981 /** Fast approximate cleanly vectorizable exp for Float(32). Returns
982  * nonsense for inputs that would overflow or underflow. Typically
983  * accurate up to the last 5 bits of the mantissa. Gets worse when
984  * approaching overflow. Vectorizes cleanly. */
985 Expr fast_exp(const Expr &x);
986 
987 /** Fast approximate cleanly vectorizable pow for Float(32). Returns
988  * nonsense for x < 0.0f. Accurate up to the last 5 bits of the
989  * mantissa for typical exponents. Gets worse when approaching
990  * overflow. Vectorizes cleanly. */
992 
993 /** Fast approximate inverse for Float(32). Corresponds to the rcpps
994  * instruction on x86, and the vrecpe instruction on ARM. Vectorizes
995  * cleanly. Note that this can produce slightly different results
996  * across different implementations of the same architecture (e.g. AMD vs Intel),
997  * even when strict_float is enabled. */
999 
1000 /** Fast approximate inverse square root for Float(32). Corresponds to
1001  * the rsqrtps instruction on x86, and the vrsqrte instruction on
1002  * ARM. Vectorizes cleanly. Note that this can produce slightly different results
1003  * across different implementations of the same architecture (e.g. AMD vs Intel),
1004  * even when strict_float is enabled. */
1006 
1007 /** Return the greatest whole number less than or equal to a
1008  * floating-point expression. If the argument is not floating-point,
1009  * it is cast to Float(32). The return value is still in floating
1010  * point, despite being a whole number. Vectorizes cleanly. */
1012 
1013 /** Return the least whole number greater than or equal to a
1014  * floating-point expression. If the argument is not floating-point,
1015  * it is cast to Float(32). The return value is still in floating
1016  * point, despite being a whole number. Vectorizes cleanly. */
1018 
1019 /** Return the whole number closest to a floating-point expression. If the
1020  * argument is not floating-point, it is cast to Float(32). The return value
1021  * is still in floating point, despite being a whole number. On ties, we
1022  * follow IEEE754 conventions and round to the nearest even number. Vectorizes
1023  * cleanly. */
1025 
1026 /** Return the integer part of a floating-point expression. If the argument is
1027  * not floating-point, it is cast to Float(32). The return value is still in
1028  * floating point, despite being a whole number. Vectorizes cleanly. */
1030 
1031 /** Returns true if the argument is a Not a Number (NaN). Requires a
1032  * floating point argument. Vectorizes cleanly.
1033  * Note that the Expr passed in will be evaluated in strict_float mode,
1034  * regardless of whether strict_float mode is enabled in the current Target. */
1036 
1037 /** Returns true if the argument is Inf or -Inf. Requires a
1038  * floating point argument. Vectorizes cleanly.
1039  * Note that the Expr passed in will be evaluated in strict_float mode,
1040  * regardless of whether strict_float mode is enabled in the current Target. */
1042 
1043 /** Returns true if the argument is a finite value (ie, neither NaN nor Inf).
1044  * Requires a floating point argument. Vectorizes cleanly.
1045  * Note that the Expr passed in will be evaluated in strict_float mode,
1046  * regardless of whether strict_float mode is enabled in the current Target. */
1048 
1049 /** Return the fractional part of a floating-point expression. If the argument
1050  * is not floating-point, it is cast to Float(32). The return value has the
1051  * same sign as the original expression. Vectorizes cleanly. */
1052 Expr fract(const Expr &x);
1053 
1054 /** Reinterpret the bits of one value as another type. */
1056 
1057 template<typename T>
1059  return reinterpret(type_of<T>(), e);
1060 }
1061 
1062 /** Return the bitwise and of two expressions (which need not have the
1063  * same type). The result type is the wider of the two expressions.
1064  * Only integral types are allowed and both expressions must be signed
1065  * or both must be unsigned. */
1067 
1068 /** Return the bitwise and of an expression and an integer. The type
1069  * of the result is the type of the expression argument. */
1070 // @{
1071 Expr operator&(Expr x, int y);
1072 Expr operator&(int x, Expr y);
1073 // @}
1074 
1075 /** Return the bitwise or of two expressions (which need not have the
1076  * same type). The result type is the wider of the two expressions.
1077  * Only integral types are allowed and both expressions must be signed
1078  * or both must be unsigned. */
1080 
1081 /** Return the bitwise or of an expression and an integer. The type of
1082  * the result is the type of the expression argument. */
1083 // @{
1084 Expr operator|(Expr x, int y);
1085 Expr operator|(int x, Expr y);
1086 // @}
1087 
1088 /** Return the bitwise xor of two expressions (which need not have the
1089  * same type). The result type is the wider of the two expressions.
1090  * Only integral types are allowed and both expressions must be signed
1091  * or both must be unsigned. */
1093 
1094 /** Return the bitwise xor of an expression and an integer. The type
1095  * of the result is the type of the expression argument. */
1096 // @{
1097 Expr operator^(Expr x, int y);
1098 Expr operator^(int x, Expr y);
1099 // @}
1100 
1101 /** Return the bitwise not of an expression. */
1103 
1104 /** Shift the bits of an integer value left. This is actually less
1105  * efficient than multiplying by 2^n, because Halide's optimization
1106  * passes understand multiplication, and will compile it to
1107  * shifting. This operator is only for if you really really need bit
1108  * shifting (e.g. because the exponent is a run-time parameter). The
1109  * type of the result is equal to the type of the first argument. Both
1110  * arguments must have integer type. */
1111 // @{
1114 // @}
1115 
1116 /** Shift the bits of an integer value right. Does sign extension for
1117  * signed integers. This is less efficient than dividing by a power of
1118  * two. Halide's definition of division (always round to negative
1119  * infinity) means that all divisions by powers of two get compiled to
1120  * bit-shifting, and Halide's optimization routines understand
1121  * division and can work with it. The type of the result is equal to
1122  * the type of the first argument. Both arguments must have integer
1123  * type. */
1124 // @{
1127 // @}
1128 
1129 /** Linear interpolate between the two values according to a weight.
1130  * \param zero_val The result when weight is 0
1131  * \param one_val The result when weight is 1
1132  * \param weight The interpolation amount
1133  *
1134  * Both zero_val and one_val must have the same type. All types are
1135  * supported, including bool.
1136  *
1137  * The weight is treated as its own type and must be float or an
1138  * unsigned integer type. It is scaled to the bit-size of the type of
1139  * x and y if they are integer, or converted to float if they are
1140  * float. Integer weights are converted to float via division by the
1141  * full-range value of the weight's type. Floating-point weights used
1142  * to interpolate between integer values must be between 0.0f and
1143  * 1.0f, and an error may be signaled if it is not provably so. (clamp
1144  * operators can be added to provide proof. Currently an error is only
1145  * signalled for constant weights.)
1146  *
1147  * For integer linear interpolation, out of range values cannot be
1148  * represented. In particular, weights that are conceptually less than
1149  * 0 or greater than 1.0 are not representable. As such the result is
1150  * always between x and y (inclusive of course). For lerp with
1151  * floating-point values and floating-point weight, the full range of
1152  * a float is valid, however underflow and overflow can still occur.
1153  *
1154  * Ordering is not required between zero_val and one_val:
1155  * lerp(42, 69, .5f) == lerp(69, 42, .5f) == 56
1156  *
1157  * Results for integer types are for exactly rounded arithmetic. As
1158  * such, there are cases where 16-bit and float differ because 32-bit
1159  * floating-point (float) does not have enough precision to produce
1160  * the exact result. (Likely true for 32-bit integer
1161  * vs. double-precision floating-point as well.)
1162  *
1163  * At present, double precision and 64-bit integers are not supported.
1164  *
1165  * Generally, lerp will vectorize as if it were an operation on a type
1166  * twice the bit size of the inferred type for x and y.
1167  *
1168  * Some examples:
1169  * \code
1170  *
1171  * // Since Halide does not have direct type delcarations, casts
1172  * // below are used to indicate the types of the parameters.
1173  * // Such casts not required or expected in actual code where types
1174  * // are inferred.
1175  *
1176  * lerp(cast<float>(x), cast<float>(y), cast<float>(w)) ->
1177  * x * (1.0f - w) + y * w
1178  *
1179  * lerp(cast<uint8_t>(x), cast<uint8_t>(y), cast<uint8_t>(w)) ->
1180  * cast<uint8_t>(cast<uint8_t>(x) * (1.0f - cast<uint8_t>(w) / 255.0f) +
1181  * cast<uint8_t>(y) * cast<uint8_t>(w) / 255.0f + .5f)
1182  *
1183  * // Note addition in Halide promoted uint8_t + int8_t to int16_t already,
1184  * // the outer cast is added for clarity.
1185  * lerp(cast<uint8_t>(x), cast<int8_t>(y), cast<uint8_t>(w)) ->
1186  * cast<int16_t>(cast<uint8_t>(x) * (1.0f - cast<uint8_t>(w) / 255.0f) +
1187  * cast<int8_t>(y) * cast<uint8_t>(w) / 255.0f + .5f)
1188  *
1189  * lerp(cast<int8_t>(x), cast<int8_t>(y), cast<float>(w)) ->
1190  * cast<int8_t>(cast<int8_t>(x) * (1.0f - cast<float>(w)) +
1191  * cast<int8_t>(y) * cast<uint8_t>(w))
1192  *
1193  * \endcode
1194  * */
1195 Expr lerp(Expr zero_val, Expr one_val, Expr weight);
1196 
1197 /** Count the number of set bits in an expression. */
1199 
1200 /** Count the number of leading zero bits in an expression. If the expression is
1201  * zero, the result is the number of bits in the type. */
1203 
1204 /** Count the number of trailing zero bits in an expression. If the expression is
1205  * zero, the result is the number of bits in the type. */
1207 
1208 /** Divide two integers, rounding towards zero. This is the typical
1209  * behavior of most hardware architectures, which differs from
1210  * Halide's division operator, which is Euclidean (rounds towards
1211  * -infinity). Will throw a runtime error if y is zero, or if y is -1
1212  * and x is the minimum signed integer. */
1214 
1215 /** Compute the remainder of dividing two integers, when division is
1216  * rounding toward zero. This is the typical behavior of most hardware
1217  * architectures, which differs from Halide's mod operator, which is
1218  * Euclidean (produces the remainder when division rounds towards
1219  * -infinity). Will throw a runtime error if y is zero. */
1221 
1222 /** Return a random variable representing a uniformly distributed
1223  * float in the half-open interval [0.0f, 1.0f). For random numbers of
1224  * other types, use lerp with a random float as the last parameter.
1225  *
1226  * Optionally takes a seed.
1227  *
1228  * Note that:
1229  \code
1230  Expr x = random_float();
1231  Expr y = x + x;
1232  \endcode
1233  *
1234  * is very different to
1235  *
1236  \code
1237  Expr y = random_float() + random_float();
1238  \endcode
1239  *
1240  * The first doubles a random variable, and the second adds two
1241  * independent random variables.
1242  *
1243  * A given random variable takes on a unique value that depends
1244  * deterministically on the pure variables of the function they belong
1245  * to, the identity of the function itself, and which definition of
1246  * the function it is used in. They are, however, shared across tuple
1247  * elements.
1248  *
1249  * This function vectorizes cleanly.
1250  */
1252 
1253 /** Return a random variable representing a uniformly distributed
1254  * unsigned 32-bit integer. See \ref random_float. Vectorizes cleanly. */
1256 
1257 /** Return a random variable representing a uniformly distributed
1258  * 32-bit integer. See \ref random_float. Vectorizes cleanly. */
1260 
1261 /** Create an Expr that prints out its value whenever it is
1262  * evaluated. It also prints out everything else in the arguments
1263  * list, separated by spaces. This can include string literals. */
1264 //@{
1265 Expr print(const std::vector<Expr> &values);
1266 
1267 template<typename... Args>
1268 inline HALIDE_NO_USER_CODE_INLINE Expr print(Expr a, Args &&...args) {
1269  std::vector<Expr> collected_args = {std::move(a)};
1270  Internal::collect_print_args(collected_args, std::forward<Args>(args)...);
1271  return print(collected_args);
1272 }
1273 //@}
1274 
1275 /** Create an Expr that prints whenever it is evaluated, provided that
1276  * the condition is true. */
1277 // @{
1278 Expr print_when(Expr condition, const std::vector<Expr> &values);
1279 
1280 template<typename... Args>
1281 inline HALIDE_NO_USER_CODE_INLINE Expr print_when(Expr condition, Expr a, Args &&...args) {
1282  std::vector<Expr> collected_args = {std::move(a)};
1283  Internal::collect_print_args(collected_args, std::forward<Args>(args)...);
1284  return print_when(std::move(condition), collected_args);
1285 }
1286 
1287 // @}
1288 
1289 /** Create an Expr that that guarantees a precondition.
1290  * If 'condition' is true, the return value is equal to the first Expr.
1291  * If 'condition' is false, halide_error() is called, and the return value
1292  * is arbitrary. Any additional arguments after the first Expr are stringified
1293  * and passed as a user-facing message to halide_error(), similar to print().
1294  *
1295  * Note that this essentially *always* inserts a runtime check into the
1296  * generated code (except when the condition can be proven at compile time);
1297  * as such, it should be avoided inside inner loops, except for debugging
1298  * or testing purposes. Note also that it does not vectorize cleanly (vector
1299  * values will be scalarized for the check).
1300  *
1301  * However, using this to make assertions about (say) input values
1302  * can be useful, both in terms of correctness and (potentially) in terms
1303  * of code generation, e.g.
1304  \code
1305  Param<int> p;
1306  Expr y = require(p > 0, p);
1307  \endcode
1308  * will allow the optimizer to assume positive, nonzero values for y.
1309  */
1310 // @{
1311 Expr require(Expr condition, const std::vector<Expr> &values);
1312 
1313 template<typename... Args>
1314 inline HALIDE_NO_USER_CODE_INLINE Expr require(Expr condition, Expr value, Args &&...args) {
1315  std::vector<Expr> collected_args = {std::move(value)};
1316  Internal::collect_print_args(collected_args, std::forward<Args>(args)...);
1317  return require(std::move(condition), collected_args);
1318 }
1319 // @}
1320 
1321 /** Return an undef value of the given type. Halide skips stores that
1322  * depend on undef values, so you can use this to mean "do not modify
1323  * this memory location". This is an escape hatch that can be used for
1324  * several things:
1325  *
1326  * You can define a reduction with no pure step, by setting the pure
1327  * step to undef. Do this only if you're confident that the update
1328  * steps are sufficient to correctly fill in the domain.
1329  *
1330  * For a tuple-valued reduction, you can write an update step that
1331  * only updates some tuple elements.
1332  *
1333  * You can define single-stage pipeline that only has update steps,
1334  * and depends on the values already in the output buffer.
1335  *
1336  * Use this feature with great caution, as you can use it to load from
1337  * uninitialized memory.
1338  */
1340 
1341 template<typename T>
1342 inline Expr undef() {
1343  return undef(type_of<T>());
1344 }
1345 
1346 namespace Internal {
1347 
1348 /** Return an expression that should never be evaluated. Expressions
1349  * that depend on unreachabale values are also unreachable, and
1350  * statements that execute unreachable expressions are also considered
1351  * unreachable. */
1353 
1354 template<typename T>
1355 inline Expr unreachable() {
1356  return unreachable(type_of<T>());
1357 }
1358 
1359 } // namespace Internal
1360 
1361 /** Control the values used in the memoization cache key for memoize.
1362  * Normally parameters and other external dependencies are
1363  * automatically inferred and added to the cache key. The memoize_tag
1364  * operator allows computing one expression and using either the
1365  * computed value, or one or more other expressions in the cache key
1366  * instead of the parameter dependencies of the computation. The
1367  * single argument version is completely safe in that the cache key
1368  * will use the actual computed value -- it is difficult or imposible
1369  * to produce erroneous caching this way. The more-than-one argument
1370  * version allows generating cache keys that do not uniquely identify
1371  * the computation and thus can result in caching errors.
1372  *
1373  * A potential use for the single argument version is to handle a
1374  * floating-point parameter that is quantized to a small
1375  * integer. Mutliple values of the float will produce the same integer
1376  * and moving the caching to using the integer for the key is more
1377  * efficient.
1378  *
1379  * The main use for the more-than-one argument version is to provide
1380  * cache key information for Handles and ImageParams, which otherwise
1381  * are not allowed inside compute_cached operations. E.g. when passing
1382  * a group of parameters to an external array function via a Handle,
1383  * memoize_tag can be used to isolate the actual values used by that
1384  * computation. If an ImageParam is a constant image with a persistent
1385  * digest, memoize_tag can be used to key computations using that image
1386  * on the digest. */
1387 // @{
1388 template<typename... Args>
1389 inline HALIDE_NO_USER_CODE_INLINE Expr memoize_tag(Expr result, Args &&...args) {
1390  std::vector<Expr> collected_args{std::forward<Args>(args)...};
1391  return Internal::memoize_tag_helper(std::move(result), collected_args);
1392 }
1393 // @}
1394 
1395 /** Expressions tagged with this intrinsic are considered to be part
1396  * of the steady state of some loop with a nasty beginning and end
1397  * (e.g. a boundary condition). When Halide encounters likely
1398  * intrinsics, it splits the containing loop body into three, and
1399  * tries to simplify down all conditions that lead to the likely. For
1400  * example, given the expression: select(x < 1, bar, x > 10, bar,
1401  * likely(foo)), Halide will split the loop over x into portions where
1402  * x < 1, 1 <= x <= 10, and x > 10.
1403  *
1404  * You're unlikely to want to call this directly. You probably want to
1405  * use the boundary condition helpers in the BoundaryConditions
1406  * namespace instead.
1407  */
1409 
1410 /** Equivalent to likely, but only triggers a loop partitioning if
1411  * found in an innermost loop. */
1413 
1414 /** Cast an expression to the halide type corresponding to the C++
1415  * type T. As part of the cast, clamp to the minimum and maximum
1416  * values of the result type. */
1417 template<typename T>
1419  return saturating_cast(type_of<T>(), std::move(e));
1420 }
1421 
1422 /** Cast an expression to a new type, clamping to the minimum and
1423  * maximum values of the result type. */
1425 
1426 /** Makes a best effort attempt to preserve IEEE floating-point
1427  * semantics in evaluating an expression. May not be implemented for
1428  * all backends. (E.g. it is difficult to do this for C++ code
1429  * generation as it depends on the compiler flags used to compile the
1430  * generated code. */
1432 
1433 /** Create an Expr that that promises another Expr is clamped but do
1434  * not generate code to check the assertion or modify the value. No
1435  * attempt is made to prove the bound at compile time. (If it is
1436  * proved false as a result of something else, an error might be
1437  * generated, but it is also possible the compiler will crash.) The
1438  * promised bound is used in bounds inference so it will allow
1439  * satisfying bounds checks as well as possibly aiding optimization.
1440  *
1441  * unsafe_promise_clamped returns its first argument, the Expr 'value'
1442  *
1443  * This is a very easy way to make Halide generate erroneous code if
1444  * the bound promises is not kept. Use sparingly when there is no
1445  * other way to convey the information to the compiler and it is
1446  * required for a valuable optimization.
1447  *
1448  * Unsafe promises can be checked by turning on
1449  * Target::CheckUnsafePromises. This is intended for debugging only.
1450  */
1451 Expr unsafe_promise_clamped(const Expr &value, const Expr &min, const Expr &max);
1452 
1453 namespace Internal {
1454 /**
1455  * FOR INTERNAL USE ONLY.
1456  *
1457  * An entirely unchecked version of unsafe_promise_clamped, used
1458  * inside the compiler as an annotation of the known bounds of an Expr
1459  * when it has proved something is bounded and wants to record that
1460  * fact for later passes (notably bounds inference) to exploit. This
1461  * gets introduced by GuardWithIf tail strategies, because the bounds
1462  * machinery has a hard time exploiting if statement conditions.
1463  *
1464  * Unlike unsafe_promise_clamped, this expression is
1465  * context-dependent, because 'value' might be statically bounded at
1466  * some point in the IR (e.g. due to a containing if statement), but
1467  * not elsewhere.
1468  **/
1469 Expr promise_clamped(const Expr &value, const Expr &min, const Expr &max);
1470 } // namespace Internal
1471 
1472 /** Scatter and gather are used for update definition which must store
1473  * multiple values to distinct locations at the same time. The
1474  * multiple expressions on the right-hand-side are bundled together
1475  * into a "gather", which must match a "scatter" the the same number
1476  * of arguments on the left-hand-size. For example, to store the
1477  * values 1 and 2 to the locations (x, y, 3) and (x, y, 4),
1478  * respectively:
1479  *
1480 \code
1481 f(x, y, scatter(3, 4)) = gather(1, 2);
1482 \endcode
1483  *
1484  * The result of gather or scatter can be treated as an
1485  * expression. Any containing operations on it can be assumed to
1486  * distribute over the elements. If two gather expressions are
1487  * combined with an arithmetic operator (e.g. added), they combine
1488  * element-wise. The following example stores the values 2 * x, 2 * y,
1489  * and 2 * c to the locations (x + 1, y, c), (x, y + 3, c), and (x, y,
1490  * c + 2) respectively:
1491  *
1492 \code
1493 f(x + scatter(1, 0, 0), y + scatter(0, 3, 0), c + scatter(0, 0, 2)) = 2 * gather(x, y, c);
1494 \endcode
1495 *
1496 * Repeated values in the scatter cause multiple stores to the same
1497 * location. The stores happen in order from left to right, so the
1498 * rightmost value wins. The following code is equivalent to f(x) = 5
1499 *
1500 \code
1501 f(scatter(x, x)) = gather(3, 5);
1502 \endcode
1503 *
1504 * Gathers are most useful for algorithms which require in-place
1505 * swapping or permutation of multiple elements, or other kinds of
1506 * in-place mutations that require loading multiple inputs, doing some
1507 * operations to them jointly, then storing them again. The following
1508 * update definition swaps the values of f at locations 3 and 5 if an
1509 * input parameter p is true:
1510 *
1511 \code
1512 f(scatter(3, 5)) = f(select(p, gather(5, 3), gather(3, 5)));
1513 \endcode
1514 *
1515 * For more examples of the use of scatter and gather, see
1516 * test/correctness/multiple_scatter.cpp
1517 *
1518 * It is not currently possible to use scatter and gather to write an
1519 * update definition in which the *number* of values loaded or stored
1520 * varies, as the size of the scatter/gather packet must be fixed a
1521 * compile-time. A workaround is to make the unwanted extra operations
1522 * a redundant copy of the last operation, which will be
1523 * dead-code-eliminated by the compiler. For example, the following
1524 * update definition swaps the values at locations 3 and 5 when the
1525 * parameter p is true, and rotates the values at locations 1, 2, and 3
1526 * when it is false. The load from 3 and store to 5 will be redundantly
1527 * repeated:
1528 *
1529 \code
1530 f(select(p, scatter(3, 5, 5), scatter(1, 2, 3))) = f(select(p, gather(5, 3, 3), gather(2, 3, 1)));
1531 \endcode
1532 *
1533 * Note that in the p == true case, we redudantly load from 3 and write
1534 * to 5 twice.
1535 */
1536 //@{
1537 Expr scatter(const std::vector<Expr> &args);
1538 Expr gather(const std::vector<Expr> &args);
1539 
1540 template<typename... Args>
1541 Expr scatter(const Expr &e, Args &&...args) {
1542  return scatter({e, std::forward<Args>(args)...});
1543 }
1544 
1545 template<typename... Args>
1546 Expr gather(const Expr &e, Args &&...args) {
1547  return gather({e, std::forward<Args>(args)...});
1548 }
1549 // @}
1550 
1551 } // namespace Halide
1552 
1553 #endif
Base classes for Halide expressions (Halide::Expr) and statements (Halide::Internal::Stmt)
Defines Tuple - the front-end handle on small arrays of expressions.
#define HALIDE_NO_USER_CODE_INLINE
Definition: Util.h:45
Create a small array of Exprs for defining and calling functions with multiple outputs.
Definition: Tuple.h:18
Expr make_one(Type t)
Construct the representation of one in the given type.
T div_imp(T a, T b)
Definition: IROperator.h:257
bool is_const_zero(const Expr &e)
Is the expression a const (as defined by is_const), and also equal to zero (in all lanes,...
Expr memoize_tag_helper(Expr result, const std::vector< Expr > &cache_key_values)
Expr make_zero(Type t)
Construct the representation of zero in the given type.
bool is_negative_const(const Expr &e)
Is the expression a const (as defined by is_const), and also strictly less than zero (in all lanes,...
bool is_undef(const Expr &e)
Is the expression an undef.
Expr requirement_failed_error(Expr condition, const std::vector< Expr > &args)
Expr make_two(Type t)
Construct the representation of two in the given type.
Expr rounding_halving_add(Expr a, Expr b)
Compute narrow((widen(a) + widen(b) + 1) / 2)
Expr unreachable(Type t=Int(32))
Return an expression that should never be evaluated.
void check_representable(Type t, int64_t val)
Check if a constant value can be correctly represented as the given type.
Expr mul_shift_right(Expr a, Expr b, Expr q)
Compute saturating_narrow(shift_right(widening_mul(a, b), q))
Expr halide_erf(const Expr &a)
bool is_const_one(const Expr &e)
Is the expression a const (as defined by is_const), and also equal to one (in all lanes,...
void match_types(Expr &a, Expr &b)
Coerce the two expressions to have the same type, using C-style casting rules.
double div_imp< double >(double a, double b)
Definition: IROperator.h:298
Expr widening_shift_right(Expr a, Expr b)
Compute widen(a) >> b.
Expr rounding_shift_right(Expr a, Expr b)
Compute saturating_add(a, (1 << max(b, 0)) / 2) >> b.
Expr halving_sub(Expr a, Expr b)
Compute narrow((widen(a) - widen(b)) / 2)
Expr widening_shift_left(Expr a, Expr b)
Compute widen(a) << b.
Expr halide_exp(const Expr &a)
Expr saturating_sub(Expr a, Expr b)
Compute saturating_narrow(widen(a) - widen(b))
Expr make_const(Type t, int64_t val)
Construct an immediate of the given type from any numeric C++ type.
bool is_positive_const(const Expr &e)
Is the expression a const (as defined by is_const), and also strictly greater than zero (in all lanes...
Expr const_true(int lanes=1)
Construct the constant boolean true.
const double * as_const_float(const Expr &e)
If an expression is a FloatImm or a Broadcast of a FloatImm, return a pointer to its value.
T mod_imp(T a, T b)
Implementations of division and mod that are specific to Halide.
Definition: IROperator.h:236
Expr widening_sub(Expr a, Expr b)
Compute widen(a) - widen(b).
Expr widening_add(Expr a, Expr b)
Compute widen(a) + widen(b).
Expr rounding_mul_shift_right(Expr a, Expr b, Expr q)
Compute saturating_narrow(rounding_shift_right(widening_mul(a, b), q))
Expr halide_log(const Expr &a)
Halide's vectorizable transcendentals.
bool is_pure(const Expr &e)
Does the expression 1) Take on the same value no matter where it appears in a Stmt,...
void split_into_ands(const Expr &cond, std::vector< Expr > &result)
Split a boolean condition into vector of ANDs.
Expr promise_clamped(const Expr &value, const Expr &min, const Expr &max)
FOR INTERNAL USE ONLY.
bool is_no_op(const Stmt &s)
Is the statement a no-op (which we represent as either an undefined Stmt, or as an Evaluate node of a...
const int64_t * as_const_int(const Expr &e)
If an expression is an IntImm or a Broadcast of an IntImm, return a pointer to its value.
Expr rounding_halving_sub(Expr a, Expr b)
Compute narrow((widen(a) - widen(b) + 1) / 2)
Expr unwrap_tags(const Expr &e)
If the expression is a tag helper call, remove it and return the tagged expression.
float div_imp< float >(float a, float b)
Definition: IROperator.h:294
bool is_const_power_of_two_integer(const Expr &e, int *bits)
Is the expression a constant integer power of two.
Expr lossless_negate(const Expr &x)
Attempt to negate x without introducing new IR and without overflow.
Expr widening_mul(Expr a, Expr b)
Compute widen(a) * widen(b).
Expr strided_ramp_base(const Expr &e, int stride=1)
If e is a ramp expression with stride, default 1, return the base, otherwise undefined.
Expr const_false(int lanes=1)
Construct the constant boolean false.
Expr rounding_shift_left(Expr a, Expr b)
Compute saturating_add(a, (1 >> min(b, 0)) / 2) << b.
Expr predicate(Expr e)
Expressions tagged with this intrinsic are suggestions that vectorization of loops with guard ifs sho...
double mod_imp< double >(double a, double b)
Definition: IROperator.h:288
Expr make_bool(bool val, int lanes=1)
Construct a boolean constant from a C++ boolean value.
HALIDE_NO_USER_CODE_INLINE void collect_print_args(std::vector< Expr > &args)
Definition: IROperator.h:321
Expr saturating_add(Expr a, Expr b)
Compute saturating_narrow(widen(a) + widen(b))
void match_types_bitwise(Expr &a, Expr &b, const char *op_name)
Asserts that both expressions are integer types and are either both signed or both unsigned.
float mod_imp< float >(float a, float b)
Definition: IROperator.h:282
Expr lossless_cast(Type t, Expr e)
Attempt to cast an expression to a smaller type while provably not losing information.
const uint64_t * as_const_uint(const Expr &e)
If an expression is a UIntImm or a Broadcast of a UIntImm, return a pointer to its value.
Expr halving_add(Expr a, Expr b)
Compute narrow((widen(a) + widen(b)) / 2)
Expr raise_to_integer_power(Expr a, int64_t b)
Raise an expression to an integer power by repeatedly multiplying it by itself.
Expr make_signed_integer_overflow(Type type)
Construct a unique signed_integer_overflow Expr.
bool is_const(const Expr &e)
Is the expression either an IntImm, a FloatImm, a StringImm, or a Cast of the same,...
Expr remove_likelies(const Expr &e)
Return an Expr that is identical to the input Expr, but with all calls to likely() and likely_if_inne...
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
auto operator>=(const Other &a, const GeneratorParam< T > &b) -> decltype(a >=(T) b)
Greater than or equal comparison between GeneratorParam<T> and any type that supports operator>= with...
Definition: Generator.h:1112
Expr log(Expr x)
Return the logarithm of a floating-point expression.
Expr operator>>(Expr x, Expr y)
Shift the bits of an integer value right.
Expr ceil(Expr x)
Return the least whole number greater than or equal to a floating-point expression.
HALIDE_NO_USER_CODE_INLINE Expr memoize_tag(Expr result, Args &&...args)
Control the values used in the memoization cache key for memoize.
Definition: IROperator.h:1389
Expr fast_log(const Expr &x)
Fast approximate cleanly vectorizable log for Float(32).
Expr count_leading_zeros(Expr x)
Count the number of leading zero bits in an expression.
Expr reinterpret(Type t, Expr e)
Reinterpret the bits of one value as another type.
auto operator==(const Other &a, const GeneratorParam< T > &b) -> decltype(a==(T) b)
Equality comparison between GeneratorParam<T> and any type that supports operator== with T.
Definition: Generator.h:1138
Expr fast_cos(const Expr &x)
Expr random_uint(Expr seed=Expr())
Return a random variable representing a uniformly distributed unsigned 32-bit integer.
@ Internal
Not visible externally, similar to 'static' linkage in C.
Expr fract(const Expr &x)
Return the fractional part of a floating-point expression.
auto operator<(const Other &a, const GeneratorParam< T > &b) -> decltype(a<(T) b)
Less than comparison between GeneratorParam<T> and any type that supports operator< with T.
Definition: Generator.h:1099
auto operator*(const Other &a, const GeneratorParam< T > &b) -> decltype(a *(T) b)
Multiplication between GeneratorParam<T> and any type that supports operator* with T.
Definition: Generator.h:1047
Expr trunc(Expr x)
Return the integer part of a floating-point expression.
auto operator||(const Other &a, const GeneratorParam< T > &b) -> decltype(a||(T) b)
Logical or between between GeneratorParam<T> and any type that supports operator|| with T.
Definition: Generator.h:1181
Expr acosh(Expr x)
Return the hyperbolic arccosine of a floating-point expression.
Expr fast_inverse(Expr x)
Fast approximate inverse for Float(32).
Expr asin(Expr x)
Return the arcsine of a floating-point expression.
Expr & operator/=(Expr &a, Expr b)
Modify the first expression to be the ratio of two expressions, without changing its type.
auto operator-(const Other &a, const GeneratorParam< T > &b) -> decltype(a -(T) b)
Subtraction between GeneratorParam<T> and any type that supports operator- with T.
Definition: Generator.h:1034
Expr clamp(Expr a, const Expr &min_val, const Expr &max_val)
Clamps an expression to lie within the given bounds.
Expr hypot(const Expr &x, const Expr &y)
Return the square root of the sum of the squares of two floating-point expressions.
Expr popcount(Expr x)
Count the number of set bits in an expression.
Expr gather(const std::vector< Expr > &args)
Expr print_when(Expr condition, const std::vector< Expr > &values)
Create an Expr that prints whenever it is evaluated, provided that the condition is true.
Expr pow(Expr x, Expr y)
Return one floating point expression raised to the power of another.
Expr operator&(Expr x, Expr y)
Return the bitwise and of two expressions (which need not have the same type).
Expr cast(Expr a)
Cast an expression to the halide type corresponding to the C++ type T.
Definition: IROperator.h:387
auto operator!(const GeneratorParam< T > &a) -> decltype(!(T) a)
Not operator for GeneratorParam.
Definition: Generator.h:1253
Expr lerp(Expr zero_val, Expr one_val, Expr weight)
Linear interpolate between the two values according to a weight.
Expr atan2(Expr y, Expr x)
Return the angle of a floating-point gradient.
Expr saturating_cast(Expr e)
Cast an expression to the halide type corresponding to the C++ type T.
Definition: IROperator.h:1418
Expr random_float(Expr seed=Expr())
Return a random variable representing a uniformly distributed float in the half-open interval [0....
Expr sin(Expr x)
Return the sine of a floating-point expression.
Expr unsafe_promise_clamped(const Expr &value, const Expr &min, const Expr &max)
Create an Expr that that promises another Expr is clamped but do not generate code to check the asser...
Expr mux(const Expr &id, const std::initializer_list< Expr > &values)
Oftentimes we want to pack a list of expressions with the same type into a channel dimension,...
Expr cosh(Expr x)
Return the hyperbolic cosine of a floating-point expression.
std::ostream & operator<<(std::ostream &stream, const Expr &)
Emit an expression on an output stream (such as std::cout) in human-readable form.
Type Int(int bits, int lanes=1)
Constructing a signed integer type.
Definition: Type.h:498
Expr acos(Expr x)
Return the arccosine of a floating-point expression.
Expr fast_exp(const Expr &x)
Fast approximate cleanly vectorizable exp for Float(32).
Expr cos(Expr x)
Return the cosine of a floating-point expression.
auto operator+(const Other &a, const GeneratorParam< T > &b) -> decltype(a+(T) b)
Addition between GeneratorParam<T> and any type that supports operator+ with T.
Definition: Generator.h:1021
Expr min(const FuncRef &a, const FuncRef &b)
Explicit overloads of min and max for FuncRef.
Definition: Func.h:578
Expr exp(Expr x)
Return the exponential of a floating-point expression.
Expr absd(Expr a, Expr b)
Return the absolute difference between two values.
auto operator&&(const Other &a, const GeneratorParam< T > &b) -> decltype(a &&(T) b)
Logical and between between GeneratorParam<T> and any type that supports operator&& with T.
Definition: Generator.h:1164
Tuple tuple_select(const Tuple &condition, const Tuple &true_value, const Tuple &false_value)
Equivalent of ternary select(), but taking/returning tuples.
Expr fast_sin(const Expr &x)
Fast vectorizable approximation to some trigonometric functions for Float(32).
Expr undef(Type t)
Return an undef value of the given type.
Expr & operator-=(Expr &a, Expr b)
Modify the first expression to be the difference of two expressions, without changing its type.
Expr fast_pow(Expr x, Expr y)
Fast approximate cleanly vectorizable pow for Float(32).
auto operator%(const Other &a, const GeneratorParam< T > &b) -> decltype(a %(T) b)
Modulo between GeneratorParam<T> and any type that supports operator% with T.
Definition: Generator.h:1073
Expr round(Expr x)
Return the whole number closest to a floating-point expression.
Expr select(Expr condition, Expr true_value, Expr false_value)
Returns an expression similar to the ternary operator in C, except that it always evaluates all argum...
Expr count_trailing_zeros(Expr x)
Count the number of trailing zero bits in an expression.
Expr scatter(const std::vector< Expr > &args)
Scatter and gather are used for update definition which must store multiple values to distinct locati...
auto operator<=(const Other &a, const GeneratorParam< T > &b) -> decltype(a<=(T) b)
Less than or equal comparison between GeneratorParam<T> and any type that supports operator<= with T.
Definition: Generator.h:1125
Expr random_int(Expr seed=Expr())
Return a random variable representing a uniformly distributed 32-bit integer.
Expr mod_round_to_zero(Expr x, Expr y)
Compute the remainder of dividing two integers, when division is rounding toward zero.
Expr strict_float(Expr e)
Makes a best effort attempt to preserve IEEE floating-point semantics in evaluating an expression.
auto operator>(const Other &a, const GeneratorParam< T > &b) -> decltype(a >(T) b)
Greater than comparison between GeneratorParam<T> and any type that supports operator> with T.
Definition: Generator.h:1086
Expr is_nan(Expr x)
Returns true if the argument is a Not a Number (NaN).
Expr asinh(Expr x)
Return the hyperbolic arcsinhe of a floating-point expression.
Expr sqrt(Expr x)
Return the square root of a floating-point expression.
Expr sinh(Expr x)
Return the hyperbolic sine of a floating-point expression.
Expr atan(Expr x)
Return the arctangent of a floating-point expression.
Expr operator|(Expr x, Expr y)
Return the bitwise or of two expressions (which need not have the same type).
auto operator!=(const Other &a, const GeneratorParam< T > &b) -> decltype(a !=(T) b)
Inequality comparison between between GeneratorParam<T> and any type that supports operator!...
Definition: Generator.h:1151
Expr & operator*=(Expr &a, Expr b)
Modify the first expression to be the product of two expressions, without changing its type.
Expr require(Expr condition, const std::vector< Expr > &values)
Create an Expr that that guarantees a precondition.
Expr is_inf(Expr x)
Returns true if the argument is Inf or -Inf.
Expr is_finite(Expr x)
Returns true if the argument is a finite value (ie, neither NaN nor Inf).
Expr tanh(Expr x)
Return the hyperbolic tangent of a floating-point expression.
Expr likely_if_innermost(Expr e)
Equivalent to likely, but only triggers a loop partitioning if found in an innermost loop.
Expr & operator+=(Expr &a, Expr b)
Modify the first expression to be the sum of two expressions, without changing its type.
Expr atanh(Expr x)
Return the hyperbolic arctangent of a floating-point expression.
Expr tan(Expr x)
Return the tangent of a floating-point expression.
Expr fast_inverse_sqrt(Expr x)
Fast approximate inverse square root for Float(32).
Expr print(const std::vector< Expr > &values)
Create an Expr that prints out its value whenever it is evaluated.
auto operator/(const Other &a, const GeneratorParam< T > &b) -> decltype(a/(T) b)
Division between GeneratorParam<T> and any type that supports operator/ with T.
Definition: Generator.h:1060
Expr abs(Expr a)
Returns the absolute value of a signed integer or floating-point expression.
Expr max(const FuncRef &a, const FuncRef &b)
Definition: Func.h:581
Expr floor(Expr x)
Return the greatest whole number less than or equal to a floating-point expression.
Expr div_round_to_zero(Expr x, Expr y)
Divide two integers, rounding towards zero.
Expr likely(Expr e)
Expressions tagged with this intrinsic are considered to be part of the steady state of some loop wit...
Expr operator~(Expr x)
Return the bitwise not of an expression.
Expr erf(const Expr &x)
Evaluate the error function erf.
Expr operator^(Expr x, Expr y)
Return the bitwise xor of two expressions (which need not have the same type).
unsigned __INT64_TYPE__ uint64_t
signed __INT64_TYPE__ int64_t
signed __INT32_TYPE__ int32_t
unsigned __INT8_TYPE__ uint8_t
unsigned __INT16_TYPE__ uint16_t
unsigned __INT32_TYPE__ uint32_t
signed __INT16_TYPE__ int16_t
signed __INT8_TYPE__ int8_t
A fragment of Halide syntax.
Definition: Expr.h:256
A builder to help create Exprs representing halide_buffer_t structs (e.g.
Definition: IROperator.h:207
std::vector< Expr > strides
Definition: IROperator.h:212
std::vector< Expr > extents
Definition: IROperator.h:212
std::vector< Expr > mins
Definition: IROperator.h:212
A reference-counted handle to a statement node.
Definition: Expr.h:413
Types in the halide type system.
Definition: Type.h:269
HALIDE_ALWAYS_INLINE bool is_int() const
Is this type a signed integer type?
Definition: Type.h:406
HALIDE_ALWAYS_INLINE bool is_float() const
Is this type a floating point type (float or double).
Definition: Type.h:394
Class that provides a type that implements half precision floating point (IEEE754 2008 binary16) in s...
Definition: Float16.h:17