1#ifndef HALIDE_IR_OPERATOR_H
2#define HALIDE_IR_OPERATOR_H
240 Type t = type_of<T>();
248 int64_t b_zero = (ib == 0) ? -1 : 0;
250 int64_t r = ia % (ib | b_zero);
251 r += (a_neg & ((ib ^ b_neg) + ~b_neg));
261 Type t = type_of<T>();
270 int64_t b_zero = (ib == 0) ? -1 : 0;
274 q += a_neg & (~b_neg - b_neg);
286 float f = a - b * (floorf(a / b));
292 double f = a - b * (std::floor(a / b));
327 static constexpr bool value = std::is_convertible<T, const char *>::value ||
328 std::is_convertible<T, Halide::Expr>::value;
331template<
typename... Args>
338template<
typename... Args>
340 args.emplace_back(std::string(arg));
344template<
typename... Args>
346 args.push_back(std::move(arg));
365 return cast(type_of<T>(), std::move(a));
643 return max(
Expr(a), std::move(b));
646 return max(std::move(a),
Expr(b));
655template<
typename A,
typename B,
typename C,
typename... Rest,
657inline Expr max(A &&a, B &&b,
C &&c, Rest &&...rest) {
658 return max(std::forward<A>(a),
max(std::forward<B>(b), std::forward<C>(c), std::forward<Rest>(rest)...));
678 return min(
Expr(a), std::move(b));
681 return min(std::move(a),
Expr(b));
690template<
typename A,
typename B,
typename C,
typename... Rest,
692inline Expr min(A &&a, B &&b,
C &&c, Rest &&...rest) {
693 return min(std::forward<A>(a),
min(std::forward<B>(b), std::forward<C>(c), std::forward<Rest>(rest)...));
701 return std::move(a) +
Expr(b);
704 return Expr(a) + std::move(b);
707 return std::move(a) -
Expr(b);
710 return Expr(a) - std::move(b);
713 return std::move(a) *
Expr(b);
716 return Expr(a) * std::move(b);
719 return std::move(a) /
Expr(b);
722 return Expr(a) / std::move(b);
725 return std::move(a) %
Expr(b);
728 return Expr(a) % std::move(b);
731 return std::move(a) >
Expr(b);
734 return Expr(a) > std::move(b);
737 return std::move(a) <
Expr(b);
740 return Expr(a) < std::move(b);
743 return std::move(a) >=
Expr(b);
746 return Expr(a) >= std::move(b);
749 return std::move(a) <=
Expr(b);
752 return Expr(a) <= std::move(b);
755 return std::move(a) ==
Expr(b);
758 return Expr(a) == std::move(b);
761 return std::move(a) !=
Expr(b);
764 return Expr(a) != std::move(b);
795template<
typename... Args,
798 return select(std::move(c0), std::move(v0),
select(std::move(c1), std::move(v1), std::forward<Args>(args)...));
815template<typename... Args>
820template<
typename... Args>
825template<
typename... Args>
827 return select(c0, v0,
select(c1, v1, std::forward<Args>(args)...));
829template<
typename... Args>
831 return select(c0, v0,
select(c1, v1, std::forward<Args>(args)...));
841template<
typename... Args>
843 return select(c0, v0,
select(c1, v1, std::forward<Args>(args)...));
860Expr mux(
const Expr &
id,
const std::initializer_list<Expr> &values);
863Expr mux(
const Expr &
id,
const std::initializer_list<FuncRef> &values);
1274template<
typename... Args>
1276 std::vector<Expr> collected_args = {std::move(a)};
1278 return print(collected_args);
1287template<
typename... Args>
1289 std::vector<Expr> collected_args = {std::move(a)};
1291 return print_when(std::move(condition), collected_args);
1320template<
typename... Args>
1322 std::vector<Expr> collected_args = {std::move(value)};
1324 return require(std::move(condition), collected_args);
1350 return undef(type_of<T>());
1395template<
typename... Args>
1397 std::vector<Expr> collected_args{std::forward<Args>(args)...};
1557template<
typename... Args>
1559 return scatter({e, std::forward<Args>(args)...});
1562template<
typename... Args>
1564 return gather({e, std::forward<Args>(args)...});
1701template<
typename T =
void>
1706template<
typename T =
void>
1711template<
typename T =
void>
1716template<
typename T =
void>
1721template<
typename T =
void>
1726template<
typename T =
void>
1731template<
typename T =
void>
1736template<
typename T =
void>
1741template<
typename T =
void>
1746template<
typename T =
void>
1751template<
typename T =
void>
1756template<
typename T =
void>
1761template<
typename T =
void>
1766template<
typename T =
void>
1771template<
typename T =
void>
1776template<
typename T =
void>
1781template<
typename T =
void>
1786template<
typename T =
void>
1791template<
typename T =
void>
1796template<
typename T =
void>
1801template<
typename T =
void>
1806template<
typename T =
void>
1811template<
typename T =
void>
Base classes for Halide expressions (Halide::Expr) and statements (Halide::Internal::Stmt)
#define HALIDE_ATTRIBUTE_DEPRECATED(x)
Defines Tuple - the front-end handle on small arrays of expressions.
#define HALIDE_NO_USER_CODE_INLINE
A fragment of front-end syntax of the form f(x, y, z), where x, y, z are Vars or Exprs.
Create a small array of Exprs for defining and calling functions with multiple outputs.
Expr make_one(Type t)
Construct the representation of one in the given type.
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 saturating_add(const Expr &a, const Expr &b, T *=nullptr)
Expr memoize_tag_helper(Expr result, const std::vector< Expr > &cache_key_values)
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.
Expr widen_right_sub(const Expr &a, const Expr &b, T *=nullptr)
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.
void check_representable(Type t, int64_t val)
Check if a constant value can be correctly represented as the given type.
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,...
Expr widening_shift_left(const Expr &a, const Expr &b, T *=nullptr)
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)
Expr saturating_sub(const Expr &a, const Expr &b, T *=nullptr)
Expr halide_exp(const Expr &a)
Expr halving_sub(const Expr &a, const Expr &b, T *=nullptr)
Expr make_const(Type t, int64_t val)
Construct an immediate of the given type from any numeric C++ type.
Expr widening_shift_right(const Expr &a, const Expr &b, T *=nullptr)
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.
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.
bool is_signed_integer_overflow(const Expr &expr)
Check if an expression is a signed_integer_overflow.
T mod_imp(T a, T b)
Implementations of division and mod that are specific to Halide.
void reset_random_counters()
Reset the counters used for random-number seeds in random_float/int/uint.
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...
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)
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.
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 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 remove_promises(const Expr &e)
Return an Expr that is identical to the input Expr, but with all calls to promise_clamped() and unsaf...
Expr widening_sub(const Expr &a, const Expr &b, T *=nullptr)
Expr rounding_mul_shift_right(const Expr &a, const Expr &b, const Expr &q, T *=nullptr)
Expr rounding_halving_add(const Expr &a, const Expr &b, T *=nullptr)
Expr widening_add(const Expr &a, const Expr &b, T *=nullptr)
Expr const_false(int lanes=1)
Construct the constant boolean false.
Expr rounding_shift_left(const Expr &a, const Expr &b, T *=nullptr)
double mod_imp< double >(double a, double b)
Expr widen_right_mul(const Expr &a, const Expr &b, T *=nullptr)
Expr rounding_shift_right(const Expr &a, const Expr &b, T *=nullptr)
Expr make_bool(bool val, int lanes=1)
Construct a boolean constant from a C++ boolean value.
Expr mul_shift_right(const Expr &a, const Expr &b, const Expr &q, T *=nullptr)
HALIDE_NO_USER_CODE_INLINE void collect_print_args(std::vector< Expr > &args)
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.
Expr halving_add(const Expr &a, const Expr &b, T *=nullptr)
float mod_imp< float >(float a, float b)
Expr lossless_cast(Type t, Expr e)
Attempt to cast an expression to a smaller type while provably not losing information.
Expr widening_mul(const Expr &a, const Expr &b, T *=nullptr)
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 widen_right_add(const Expr &a, const Expr &b, T *=nullptr)
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...
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.
Expr widen_right_add(Expr a, Expr b)
Below is a collection of intrinsics for fixed-point programming.
Expr rounding_shift_right(Expr a, Expr b)
Compute saturating_narrow(widening_add(a, (1 << max(b, 0)) / 2) >> b).
HALIDE_NO_USER_CODE_INLINE Expr memoize_tag(Expr result, Args &&...args)
Control the values used in the memoization cache key for memoize.
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.
Expr saturating_add(Expr a, Expr b)
Compute saturating_narrow(widen(a) + widen(b))
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.
Expr fast_cos(const Expr &x)
Expr & operator*=(Expr &a, Expr b)
Modify the first expression to be the product of two expressions, without changing its type.
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.
Expr halving_add(Expr a, Expr b)
Compute narrow((widen(a) + widen(b)) / 2)
Expr & operator-=(Expr &a, Expr b)
Modify the first expression to be the difference of two expressions, without changing its type.
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.
Expr widening_shift_right(Expr a, Expr b)
Compute widen(a) >> b.
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.
Expr trunc(Expr x)
Return the integer part of a floating-point expression.
Expr halving_sub(Expr a, Expr b)
Compute narrow((widen(a) - widen(b)) / 2)
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.
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 rounding_shift_left(Expr a, Expr b)
Compute saturating_narrow(widening_add(a, (1 >> min(b, 0)) / 2) << b).
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.
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 saturating_sub(Expr a, Expr b)
Compute saturating_narrow(widen(a) - widen(b))
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 widening_shift_left(Expr a, Expr b)
Compute widen(a) << b.
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.
auto operator!(const GeneratorParam< T > &a) -> decltype(!(T) a)
Not operator for GeneratorParam.
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.
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 rounding_halving_add(Expr a, Expr b)
Compute narrow((widen(a) + widen(b) + 1) / 2)
Expr extract_bits(Type t, const Expr &e, const Expr &lsb)
Extract a contiguous subsequence of the bits of 'e', starting at the bit index given by 'lsb',...
Expr concat_bits(const std::vector< Expr > &e)
Given a number of Exprs of the same type, concatenate their bits producing a single Expr of the same ...
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.
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 widening_add(Expr a, Expr b)
Compute widen(a) + widen(b).
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.
Expr min(const FuncRef &a, const FuncRef &b)
Explicit overloads of min and max for FuncRef.
Expr exp(Expr x)
Return the exponential of a floating-point expression.
Expr widen_right_mul(Expr a, Expr b)
Compute a * widen(b).
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.
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 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.
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.
Expr rounding_mul_shift_right(Expr a, Expr b, Expr q)
Compute saturating_narrow(rounding_shift_right(widening_mul(a, b), q))
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.
Expr & operator/=(Expr &a, Expr b)
Modify the first expression to be the ratio of two expressions, without changing its type.
Expr widening_mul(Expr a, Expr b)
Compute widen(a) * widen(b).
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.
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!...
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 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.
Expr mul_shift_right(Expr a, Expr b, Expr q)
Compute saturating_narrow(shift_right(widening_mul(a, b), q))
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.
Expr & operator+=(Expr &a, Expr b)
Modify the first expression to be the sum of two expressions, without changing its type.
Expr abs(Expr a)
Returns the absolute value of a signed integer or floating-point expression.
Expr widen_right_sub(Expr a, Expr b)
Compute a - widen(b).
Expr max(const FuncRef &a, const FuncRef &b)
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 widening_sub(Expr a, Expr b)
Compute widen(a) - widen(b).
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.
A builder to help create Exprs representing halide_buffer_t structs (e.g.
std::vector< Expr > strides
std::vector< Expr > extents
A reference-counted handle to a statement node.
static constexpr bool value
Types in the halide type system.
HALIDE_ALWAYS_INLINE bool is_int() const
Is this type a signed integer type?
HALIDE_ALWAYS_INLINE bool is_float() const
Is this type a floating point type (float or double).
Class that provides a type that implements half precision floating point (IEEE754 2008 binary16) in s...