| 193 | } |
| 194 | |
| 195 | Halide::Expr dispatch_powc(Halide::FuncRef inp, float exp) { |
| 196 | if (almost_equal(exp, .0f)) { |
| 197 | return Halide::cast(inp.function().output_types()[0], 1); |
| 198 | } |
| 199 | auto int_neg = [exp](Halide::Expr x) { |
| 200 | if (exp < 0) { |
| 201 | return Halide::cast(x.type(), 1) / x; |
| 202 | } |
| 203 | return x; |
| 204 | }; |
| 205 | if (almost_equal(std::abs(exp), 1.f)) { |
| 206 | return int_neg(inp); |
| 207 | } |
| 208 | if (almost_equal(std::abs(exp), 2.f)) { |
| 209 | return int_neg(inp * inp); |
| 210 | } |
| 211 | if (almost_equal(std::abs(exp), 3.f)) { |
| 212 | return int_neg(inp * inp * inp); |
| 213 | } |
| 214 | if (almost_equal(std::abs(exp), 4.f)) { |
| 215 | auto x = inp * inp; |
| 216 | return int_neg(x * x); |
| 217 | } |
| 218 | |
| 219 | if (almost_equal(exp, .5f)) { |
| 220 | return Halide::sqrt(inp); |
| 221 | } |
| 222 | |
| 223 | int exp_i = std::round(exp); |
| 224 | if (almost_equal(static_cast<float>(exp_i), exp)) { |
| 225 | auto yabs = Halide::pow(Halide::abs(inp), exp); |
| 226 | if (exp_i & 1) { |
| 227 | return Halide::select(inp < 0, -yabs, yabs); |
| 228 | } else { |
| 229 | return yabs; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | return Halide::pow(inp, exp); |
| 234 | } |
| 235 | |
| 236 | } // anonymous namespace |
| 237 | |