1f99ccf65SEugene Zhulenev //===- PolynomialApproximation.cpp - Approximate math operations ----------===// 2f99ccf65SEugene Zhulenev // 3f99ccf65SEugene Zhulenev // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4f99ccf65SEugene Zhulenev // See https://llvm.org/LICENSE.txt for license information. 5f99ccf65SEugene Zhulenev // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6f99ccf65SEugene Zhulenev // 7f99ccf65SEugene Zhulenev //===----------------------------------------------------------------------===// 8f99ccf65SEugene Zhulenev // 9f99ccf65SEugene Zhulenev // This file implements expansion of math operations to fast approximations 10f99ccf65SEugene Zhulenev // that do not rely on any of the library functions. 11f99ccf65SEugene Zhulenev // 12f99ccf65SEugene Zhulenev //===----------------------------------------------------------------------===// 13f99ccf65SEugene Zhulenev 14*ce976d2dSEugene Zhulenev #include "mlir/Dialect/LLVMIR/LLVMDialect.h" 15*ce976d2dSEugene Zhulenev #include "mlir/Dialect/LLVMIR/LLVMTypes.h" 16f99ccf65SEugene Zhulenev #include "mlir/Dialect/Math/IR/Math.h" 17f99ccf65SEugene Zhulenev #include "mlir/Dialect/Math/Transforms/Passes.h" 18f99ccf65SEugene Zhulenev #include "mlir/Dialect/Vector/VectorOps.h" 19f99ccf65SEugene Zhulenev #include "mlir/IR/Builders.h" 20*ce976d2dSEugene Zhulenev #include "mlir/IR/ImplicitLocOpBuilder.h" 21f99ccf65SEugene Zhulenev #include "mlir/Transforms/DialectConversion.h" 22f99ccf65SEugene Zhulenev #include "mlir/Transforms/GreedyPatternRewriteDriver.h" 23f99ccf65SEugene Zhulenev 24f99ccf65SEugene Zhulenev using namespace mlir; 25f99ccf65SEugene Zhulenev using namespace mlir::vector; 26f99ccf65SEugene Zhulenev 27*ce976d2dSEugene Zhulenev using TypePredicate = llvm::function_ref<bool(Type)>; 28*ce976d2dSEugene Zhulenev 29*ce976d2dSEugene Zhulenev static bool isF32(Type type) { return type.isF32(); } 30*ce976d2dSEugene Zhulenev 31*ce976d2dSEugene Zhulenev // Returns vector width if the element type is matching the predicate (scalars 32*ce976d2dSEugene Zhulenev // that do match the predicate have width equal to `1`). 33*ce976d2dSEugene Zhulenev static Optional<int> vectorWidth(Type type, TypePredicate pred) { 34*ce976d2dSEugene Zhulenev // If the type matches the predicate then its width is `1`. 35*ce976d2dSEugene Zhulenev if (pred(type)) 36*ce976d2dSEugene Zhulenev return 1; 37*ce976d2dSEugene Zhulenev 38*ce976d2dSEugene Zhulenev // Otherwise check if the type is a vector type. 39*ce976d2dSEugene Zhulenev auto vectorType = type.dyn_cast<VectorType>(); 40*ce976d2dSEugene Zhulenev if (vectorType && pred(vectorType.getElementType())) { 41*ce976d2dSEugene Zhulenev assert(vectorType.getRank() == 1 && "only 1d vectors are supported"); 42*ce976d2dSEugene Zhulenev return vectorType.getDimSize(0); 43*ce976d2dSEugene Zhulenev } 44*ce976d2dSEugene Zhulenev 45*ce976d2dSEugene Zhulenev return llvm::None; 46*ce976d2dSEugene Zhulenev } 47*ce976d2dSEugene Zhulenev 48*ce976d2dSEugene Zhulenev // Returns vector width of the type. If the type is a scalar returns `1`. 49*ce976d2dSEugene Zhulenev static int vectorWidth(Type type) { 50*ce976d2dSEugene Zhulenev auto vectorType = type.dyn_cast<VectorType>(); 51*ce976d2dSEugene Zhulenev return vectorType ? vectorType.getDimSize(0) : 1; 52*ce976d2dSEugene Zhulenev } 53*ce976d2dSEugene Zhulenev 54*ce976d2dSEugene Zhulenev // Returns vector element type. If the type is a scalar returns the argument. 55*ce976d2dSEugene Zhulenev static Type elementType(Type type) { 56*ce976d2dSEugene Zhulenev auto vectorType = type.dyn_cast<VectorType>(); 57*ce976d2dSEugene Zhulenev return vectorType ? vectorType.getElementType() : type; 58f99ccf65SEugene Zhulenev } 59f99ccf65SEugene Zhulenev 60f99ccf65SEugene Zhulenev //----------------------------------------------------------------------------// 61*ce976d2dSEugene Zhulenev // Broadcast scalar types and values into vector types and values. 62f99ccf65SEugene Zhulenev //----------------------------------------------------------------------------// 63f99ccf65SEugene Zhulenev 64*ce976d2dSEugene Zhulenev // Broadcasts scalar type into vector type (iff width is greater then 1). 65*ce976d2dSEugene Zhulenev static Type broadcast(Type type, int width) { 66*ce976d2dSEugene Zhulenev assert(!type.isa<VectorType>() && "must be scalar type"); 67*ce976d2dSEugene Zhulenev return width > 1 ? VectorType::get({width}, type) : type; 68*ce976d2dSEugene Zhulenev } 69f99ccf65SEugene Zhulenev 70*ce976d2dSEugene Zhulenev // Broadcasts scalar value into vector (iff width is greater then 1). 71*ce976d2dSEugene Zhulenev static Value broadcast(ImplicitLocOpBuilder &builder, Value value, int width) { 72*ce976d2dSEugene Zhulenev assert(!value.getType().isa<VectorType>() && "must be scalar value"); 73*ce976d2dSEugene Zhulenev auto type = broadcast(value.getType(), width); 74*ce976d2dSEugene Zhulenev return width > 1 ? builder.create<BroadcastOp>(type, value) : value; 75*ce976d2dSEugene Zhulenev } 76f99ccf65SEugene Zhulenev 77*ce976d2dSEugene Zhulenev //----------------------------------------------------------------------------// 78*ce976d2dSEugene Zhulenev // Helper functions to create constants. 79*ce976d2dSEugene Zhulenev //----------------------------------------------------------------------------// 80f99ccf65SEugene Zhulenev 81*ce976d2dSEugene Zhulenev static Value f32Cst(ImplicitLocOpBuilder &builder, float value) { 82*ce976d2dSEugene Zhulenev return builder.create<ConstantOp>(builder.getF32Type(), 83*ce976d2dSEugene Zhulenev builder.getF32FloatAttr(value)); 84*ce976d2dSEugene Zhulenev } 85f99ccf65SEugene Zhulenev 86*ce976d2dSEugene Zhulenev static Value i32Cst(ImplicitLocOpBuilder &builder, int32_t value) { 87*ce976d2dSEugene Zhulenev return builder.create<ConstantOp>(builder.getI32Type(), 88*ce976d2dSEugene Zhulenev builder.getI32IntegerAttr(value)); 89*ce976d2dSEugene Zhulenev } 90f99ccf65SEugene Zhulenev 91*ce976d2dSEugene Zhulenev static Value f32FromBits(ImplicitLocOpBuilder &builder, uint32_t bits) { 92*ce976d2dSEugene Zhulenev Value i32Value = i32Cst(builder, static_cast<int32_t>(bits)); 93*ce976d2dSEugene Zhulenev return builder.create<LLVM::BitcastOp>(builder.getF32Type(), i32Value); 94*ce976d2dSEugene Zhulenev } 95f99ccf65SEugene Zhulenev 96*ce976d2dSEugene Zhulenev //----------------------------------------------------------------------------// 97*ce976d2dSEugene Zhulenev // Helper functions to build math functions approximations. 98*ce976d2dSEugene Zhulenev //----------------------------------------------------------------------------// 99*ce976d2dSEugene Zhulenev 100*ce976d2dSEugene Zhulenev static Value min(ImplicitLocOpBuilder &builder, Value a, Value b) { 101*ce976d2dSEugene Zhulenev return builder.create<SelectOp>( 102*ce976d2dSEugene Zhulenev builder.create<CmpFOp>(CmpFPredicate::OLT, a, b), a, b); 103*ce976d2dSEugene Zhulenev } 104*ce976d2dSEugene Zhulenev 105*ce976d2dSEugene Zhulenev static Value max(ImplicitLocOpBuilder &builder, Value a, Value b) { 106*ce976d2dSEugene Zhulenev return builder.create<SelectOp>( 107*ce976d2dSEugene Zhulenev builder.create<CmpFOp>(CmpFPredicate::OGT, a, b), a, b); 108*ce976d2dSEugene Zhulenev } 109*ce976d2dSEugene Zhulenev 110*ce976d2dSEugene Zhulenev static Value clamp(ImplicitLocOpBuilder &builder, Value value, Value lowerBound, 111*ce976d2dSEugene Zhulenev Value upperBound) { 112*ce976d2dSEugene Zhulenev return max(builder, min(builder, value, upperBound), lowerBound); 113*ce976d2dSEugene Zhulenev } 114*ce976d2dSEugene Zhulenev 115*ce976d2dSEugene Zhulenev // Decomposes given floating point value `arg` into a normalized fraction and 116*ce976d2dSEugene Zhulenev // an integral power of two (see std::frexp). Returned values have float type. 117*ce976d2dSEugene Zhulenev static std::pair<Value, Value> frexp(ImplicitLocOpBuilder &builder, Value arg, 118*ce976d2dSEugene Zhulenev bool is_positive = false) { 119*ce976d2dSEugene Zhulenev assert(isF32(elementType(arg.getType())) && "argument must be f32 type"); 120*ce976d2dSEugene Zhulenev 121*ce976d2dSEugene Zhulenev int width = vectorWidth(arg.getType()); 122*ce976d2dSEugene Zhulenev 123*ce976d2dSEugene Zhulenev auto bcast = [&](Value value) -> Value { 124*ce976d2dSEugene Zhulenev return broadcast(builder, value, width); 125f99ccf65SEugene Zhulenev }; 126f99ccf65SEugene Zhulenev 127*ce976d2dSEugene Zhulenev auto i32 = builder.getIntegerType(32); 128*ce976d2dSEugene Zhulenev auto i32Vec = broadcast(i32, width); 129*ce976d2dSEugene Zhulenev auto f32Vec = broadcast(builder.getF32Type(), width); 130f99ccf65SEugene Zhulenev 131*ce976d2dSEugene Zhulenev Value cst126f = f32Cst(builder, 126.0f); 132*ce976d2dSEugene Zhulenev Value cstHalf = f32Cst(builder, 0.5f); 133*ce976d2dSEugene Zhulenev Value cstInvMantMask = f32FromBits(builder, ~0x7f800000u); 134f99ccf65SEugene Zhulenev 135*ce976d2dSEugene Zhulenev // Bitcast to i32 for bitwise operations. 136*ce976d2dSEugene Zhulenev Value i32Half = builder.create<LLVM::BitcastOp>(i32, cstHalf); 137*ce976d2dSEugene Zhulenev Value i32InvMantMask = builder.create<LLVM::BitcastOp>(i32, cstInvMantMask); 138*ce976d2dSEugene Zhulenev Value i32Arg = builder.create<LLVM::BitcastOp>(i32Vec, arg); 139f99ccf65SEugene Zhulenev 140*ce976d2dSEugene Zhulenev // Compute normalized fraction. 141*ce976d2dSEugene Zhulenev Value tmp0 = builder.create<LLVM::AndOp>(i32Arg, bcast(i32InvMantMask)); 142*ce976d2dSEugene Zhulenev Value tmp1 = builder.create<LLVM::OrOp>(tmp0, bcast(i32Half)); 143*ce976d2dSEugene Zhulenev Value normalizedFraction = builder.create<LLVM::BitcastOp>(f32Vec, tmp1); 144f99ccf65SEugene Zhulenev 145*ce976d2dSEugene Zhulenev // Compute exponent. 146*ce976d2dSEugene Zhulenev Value arg0 = is_positive ? arg : builder.create<AbsFOp>(arg); 147*ce976d2dSEugene Zhulenev Value biasedExponentBits = builder.create<UnsignedShiftRightOp>( 148*ce976d2dSEugene Zhulenev builder.create<LLVM::BitcastOp>(i32Vec, arg0), 149*ce976d2dSEugene Zhulenev bcast(i32Cst(builder, 23))); 150*ce976d2dSEugene Zhulenev Value biasedExponent = builder.create<SIToFPOp>(f32Vec, biasedExponentBits); 151*ce976d2dSEugene Zhulenev Value exponent = builder.create<SubFOp>(biasedExponent, bcast(cst126f)); 152f99ccf65SEugene Zhulenev 153*ce976d2dSEugene Zhulenev return {normalizedFraction, exponent}; 154f99ccf65SEugene Zhulenev } 155f99ccf65SEugene Zhulenev 156f99ccf65SEugene Zhulenev //----------------------------------------------------------------------------// 157f99ccf65SEugene Zhulenev // TanhOp approximation. 158f99ccf65SEugene Zhulenev //----------------------------------------------------------------------------// 159f99ccf65SEugene Zhulenev 160f99ccf65SEugene Zhulenev namespace { 161f99ccf65SEugene Zhulenev struct TanhApproximation : public OpRewritePattern<math::TanhOp> { 162f99ccf65SEugene Zhulenev public: 163f99ccf65SEugene Zhulenev using OpRewritePattern::OpRewritePattern; 164f99ccf65SEugene Zhulenev 165f99ccf65SEugene Zhulenev LogicalResult matchAndRewrite(math::TanhOp op, 166f99ccf65SEugene Zhulenev PatternRewriter &rewriter) const final; 167f99ccf65SEugene Zhulenev }; 168f99ccf65SEugene Zhulenev } // namespace 169f99ccf65SEugene Zhulenev 170f99ccf65SEugene Zhulenev LogicalResult 171f99ccf65SEugene Zhulenev TanhApproximation::matchAndRewrite(math::TanhOp op, 172f99ccf65SEugene Zhulenev PatternRewriter &rewriter) const { 173*ce976d2dSEugene Zhulenev auto width = vectorWidth(op.operand().getType(), isF32); 174*ce976d2dSEugene Zhulenev if (!width.hasValue()) 175f99ccf65SEugene Zhulenev return rewriter.notifyMatchFailure(op, "unsupported operand type"); 176f99ccf65SEugene Zhulenev 177*ce976d2dSEugene Zhulenev ImplicitLocOpBuilder builder(op->getLoc(), rewriter); 178*ce976d2dSEugene Zhulenev auto bcast = [&](Value value) -> Value { 179*ce976d2dSEugene Zhulenev return broadcast(builder, value, *width); 180*ce976d2dSEugene Zhulenev }; 181f99ccf65SEugene Zhulenev 182f99ccf65SEugene Zhulenev // Clamp operand into [plusClamp, minusClamp] range. 183*ce976d2dSEugene Zhulenev Value minusClamp = bcast(f32Cst(builder, -7.9053111076354980f)); 184*ce976d2dSEugene Zhulenev Value plusClamp = bcast(f32Cst(builder, 7.90531110763549805f)); 185*ce976d2dSEugene Zhulenev Value x = clamp(builder, op.operand(), minusClamp, plusClamp); 186f99ccf65SEugene Zhulenev 187f99ccf65SEugene Zhulenev // Mask for tiny values that are approximated with `operand`. 188*ce976d2dSEugene Zhulenev Value tiny = bcast(f32Cst(builder, 0.0004f)); 189*ce976d2dSEugene Zhulenev Value tinyMask = builder.create<CmpFOp>( 190*ce976d2dSEugene Zhulenev CmpFPredicate::OLT, builder.create<AbsFOp>(op.operand()), tiny); 191f99ccf65SEugene Zhulenev 192f99ccf65SEugene Zhulenev // The monomial coefficients of the numerator polynomial (odd). 193*ce976d2dSEugene Zhulenev Value alpha1 = bcast(f32Cst(builder, 4.89352455891786e-03f)); 194*ce976d2dSEugene Zhulenev Value alpha3 = bcast(f32Cst(builder, 6.37261928875436e-04f)); 195*ce976d2dSEugene Zhulenev Value alpha5 = bcast(f32Cst(builder, 1.48572235717979e-05f)); 196*ce976d2dSEugene Zhulenev Value alpha7 = bcast(f32Cst(builder, 5.12229709037114e-08f)); 197*ce976d2dSEugene Zhulenev Value alpha9 = bcast(f32Cst(builder, -8.60467152213735e-11f)); 198*ce976d2dSEugene Zhulenev Value alpha11 = bcast(f32Cst(builder, 2.00018790482477e-13f)); 199*ce976d2dSEugene Zhulenev Value alpha13 = bcast(f32Cst(builder, -2.76076847742355e-16f)); 200f99ccf65SEugene Zhulenev 201f99ccf65SEugene Zhulenev // The monomial coefficients of the denominator polynomial (even). 202*ce976d2dSEugene Zhulenev Value beta0 = bcast(f32Cst(builder, 4.89352518554385e-03f)); 203*ce976d2dSEugene Zhulenev Value beta2 = bcast(f32Cst(builder, 2.26843463243900e-03f)); 204*ce976d2dSEugene Zhulenev Value beta4 = bcast(f32Cst(builder, 1.18534705686654e-04f)); 205*ce976d2dSEugene Zhulenev Value beta6 = bcast(f32Cst(builder, 1.19825839466702e-06f)); 206f99ccf65SEugene Zhulenev 207f99ccf65SEugene Zhulenev // Since the polynomials are odd/even, we need x^2. 208*ce976d2dSEugene Zhulenev Value x2 = builder.create<MulFOp>(x, x); 209f99ccf65SEugene Zhulenev 210f99ccf65SEugene Zhulenev // Evaluate the numerator polynomial p. 211*ce976d2dSEugene Zhulenev Value p = builder.create<FmaFOp>(x2, alpha13, alpha11); 212*ce976d2dSEugene Zhulenev p = builder.create<FmaFOp>(x2, p, alpha9); 213*ce976d2dSEugene Zhulenev p = builder.create<FmaFOp>(x2, p, alpha7); 214*ce976d2dSEugene Zhulenev p = builder.create<FmaFOp>(x2, p, alpha5); 215*ce976d2dSEugene Zhulenev p = builder.create<FmaFOp>(x2, p, alpha3); 216*ce976d2dSEugene Zhulenev p = builder.create<FmaFOp>(x2, p, alpha1); 217*ce976d2dSEugene Zhulenev p = builder.create<MulFOp>(x, p); 218f99ccf65SEugene Zhulenev 219f99ccf65SEugene Zhulenev // Evaluate the denominator polynomial q. 220*ce976d2dSEugene Zhulenev Value q = builder.create<FmaFOp>(x2, beta6, beta4); 221*ce976d2dSEugene Zhulenev q = builder.create<FmaFOp>(x2, q, beta2); 222*ce976d2dSEugene Zhulenev q = builder.create<FmaFOp>(x2, q, beta0); 223f99ccf65SEugene Zhulenev 224f99ccf65SEugene Zhulenev // Divide the numerator by the denominator. 225*ce976d2dSEugene Zhulenev Value res = 226*ce976d2dSEugene Zhulenev builder.create<SelectOp>(tinyMask, x, builder.create<DivFOp>(p, q)); 227f99ccf65SEugene Zhulenev 228f99ccf65SEugene Zhulenev rewriter.replaceOp(op, res); 229f99ccf65SEugene Zhulenev 230f99ccf65SEugene Zhulenev return success(); 231f99ccf65SEugene Zhulenev } 232f99ccf65SEugene Zhulenev 233f99ccf65SEugene Zhulenev //----------------------------------------------------------------------------// 234*ce976d2dSEugene Zhulenev // LogOp approximation. 235*ce976d2dSEugene Zhulenev //----------------------------------------------------------------------------// 236*ce976d2dSEugene Zhulenev 237*ce976d2dSEugene Zhulenev namespace { 238*ce976d2dSEugene Zhulenev 239*ce976d2dSEugene Zhulenev // This approximations comes from the Julien Pommier's SSE math library. 240*ce976d2dSEugene Zhulenev // Link: http://gruntthepeon.free.fr/ssemath 241*ce976d2dSEugene Zhulenev struct LogApproximation : public OpRewritePattern<math::LogOp> { 242*ce976d2dSEugene Zhulenev public: 243*ce976d2dSEugene Zhulenev using OpRewritePattern::OpRewritePattern; 244*ce976d2dSEugene Zhulenev 245*ce976d2dSEugene Zhulenev LogicalResult matchAndRewrite(math::LogOp op, 246*ce976d2dSEugene Zhulenev PatternRewriter &rewriter) const final; 247*ce976d2dSEugene Zhulenev }; 248*ce976d2dSEugene Zhulenev } // namespace 249*ce976d2dSEugene Zhulenev 250*ce976d2dSEugene Zhulenev #define LN2_VALUE \ 251*ce976d2dSEugene Zhulenev 0.693147180559945309417232121458176568075500134360255254120680009493393621L 252*ce976d2dSEugene Zhulenev 253*ce976d2dSEugene Zhulenev LogicalResult 254*ce976d2dSEugene Zhulenev LogApproximation::matchAndRewrite(math::LogOp op, 255*ce976d2dSEugene Zhulenev PatternRewriter &rewriter) const { 256*ce976d2dSEugene Zhulenev auto width = vectorWidth(op.operand().getType(), isF32); 257*ce976d2dSEugene Zhulenev if (!width.hasValue()) 258*ce976d2dSEugene Zhulenev return rewriter.notifyMatchFailure(op, "unsupported operand type"); 259*ce976d2dSEugene Zhulenev 260*ce976d2dSEugene Zhulenev ImplicitLocOpBuilder builder(op->getLoc(), rewriter); 261*ce976d2dSEugene Zhulenev auto bcast = [&](Value value) -> Value { 262*ce976d2dSEugene Zhulenev return broadcast(builder, value, *width); 263*ce976d2dSEugene Zhulenev }; 264*ce976d2dSEugene Zhulenev 265*ce976d2dSEugene Zhulenev Value cstZero = bcast(f32Cst(builder, 0.0f)); 266*ce976d2dSEugene Zhulenev Value cstOne = bcast(f32Cst(builder, 1.0f)); 267*ce976d2dSEugene Zhulenev Value cstNegHalf = bcast(f32Cst(builder, -0.5f)); 268*ce976d2dSEugene Zhulenev 269*ce976d2dSEugene Zhulenev // The smallest non denormalized float number. 270*ce976d2dSEugene Zhulenev Value cstMinNormPos = bcast(f32FromBits(builder, 0x00800000u)); 271*ce976d2dSEugene Zhulenev Value cstMinusInf = bcast(f32FromBits(builder, 0xff800000u)); 272*ce976d2dSEugene Zhulenev Value cstPosInf = bcast(f32FromBits(builder, 0x7f800000u)); 273*ce976d2dSEugene Zhulenev Value cstNan = bcast(f32FromBits(builder, 0x7fc00000)); 274*ce976d2dSEugene Zhulenev 275*ce976d2dSEugene Zhulenev // Polynomial coefficients. 276*ce976d2dSEugene Zhulenev Value cstCephesSQRTHF = bcast(f32Cst(builder, 0.707106781186547524f)); 277*ce976d2dSEugene Zhulenev Value cstCephesLogP0 = bcast(f32Cst(builder, 7.0376836292E-2f)); 278*ce976d2dSEugene Zhulenev Value cstCephesLogP1 = bcast(f32Cst(builder, -1.1514610310E-1f)); 279*ce976d2dSEugene Zhulenev Value cstCephesLogP2 = bcast(f32Cst(builder, 1.1676998740E-1f)); 280*ce976d2dSEugene Zhulenev Value cstCephesLogP3 = bcast(f32Cst(builder, -1.2420140846E-1f)); 281*ce976d2dSEugene Zhulenev Value cstCephesLogP4 = bcast(f32Cst(builder, +1.4249322787E-1f)); 282*ce976d2dSEugene Zhulenev Value cstCephesLogP5 = bcast(f32Cst(builder, -1.6668057665E-1f)); 283*ce976d2dSEugene Zhulenev Value cstCephesLogP6 = bcast(f32Cst(builder, +2.0000714765E-1f)); 284*ce976d2dSEugene Zhulenev Value cstCephesLogP7 = bcast(f32Cst(builder, -2.4999993993E-1f)); 285*ce976d2dSEugene Zhulenev Value cstCephesLogP8 = bcast(f32Cst(builder, +3.3333331174E-1f)); 286*ce976d2dSEugene Zhulenev 287*ce976d2dSEugene Zhulenev Value x = op.operand(); 288*ce976d2dSEugene Zhulenev 289*ce976d2dSEugene Zhulenev // Truncate input values to the minimum positive normal. 290*ce976d2dSEugene Zhulenev x = max(builder, x, cstMinNormPos); 291*ce976d2dSEugene Zhulenev 292*ce976d2dSEugene Zhulenev // Extract significant in the range [0.5,1) and exponent. 293*ce976d2dSEugene Zhulenev std::pair<Value, Value> pair = frexp(builder, x, /*is_positive=*/true); 294*ce976d2dSEugene Zhulenev x = pair.first; 295*ce976d2dSEugene Zhulenev Value e = pair.second; 296*ce976d2dSEugene Zhulenev 297*ce976d2dSEugene Zhulenev // Shift the inputs from the range [0.5,1) to [sqrt(1/2), sqrt(2)) and shift 298*ce976d2dSEugene Zhulenev // by -1.0. The values are then centered around 0, which improves the 299*ce976d2dSEugene Zhulenev // stability of the polynomial evaluation: 300*ce976d2dSEugene Zhulenev // 301*ce976d2dSEugene Zhulenev // if( x < SQRTHF ) { 302*ce976d2dSEugene Zhulenev // e -= 1; 303*ce976d2dSEugene Zhulenev // x = x + x - 1.0; 304*ce976d2dSEugene Zhulenev // } else { x = x - 1.0; } 305*ce976d2dSEugene Zhulenev Value mask = builder.create<CmpFOp>(CmpFPredicate::OLT, x, cstCephesSQRTHF); 306*ce976d2dSEugene Zhulenev Value tmp = builder.create<SelectOp>(mask, x, cstZero); 307*ce976d2dSEugene Zhulenev 308*ce976d2dSEugene Zhulenev x = builder.create<SubFOp>(x, cstOne); 309*ce976d2dSEugene Zhulenev e = builder.create<SubFOp>(e, 310*ce976d2dSEugene Zhulenev builder.create<SelectOp>(mask, cstOne, cstZero)); 311*ce976d2dSEugene Zhulenev x = builder.create<AddFOp>(x, tmp); 312*ce976d2dSEugene Zhulenev 313*ce976d2dSEugene Zhulenev Value x2 = builder.create<MulFOp>(x, x); 314*ce976d2dSEugene Zhulenev Value x3 = builder.create<MulFOp>(x2, x); 315*ce976d2dSEugene Zhulenev 316*ce976d2dSEugene Zhulenev // Evaluate the polynomial approximant of degree 8 in three parts. 317*ce976d2dSEugene Zhulenev Value y0, y1, y2; 318*ce976d2dSEugene Zhulenev y0 = builder.create<FmaFOp>(cstCephesLogP0, x, cstCephesLogP1); 319*ce976d2dSEugene Zhulenev y1 = builder.create<FmaFOp>(cstCephesLogP3, x, cstCephesLogP4); 320*ce976d2dSEugene Zhulenev y2 = builder.create<FmaFOp>(cstCephesLogP6, x, cstCephesLogP7); 321*ce976d2dSEugene Zhulenev y0 = builder.create<FmaFOp>(y0, x, cstCephesLogP2); 322*ce976d2dSEugene Zhulenev y1 = builder.create<FmaFOp>(y1, x, cstCephesLogP5); 323*ce976d2dSEugene Zhulenev y2 = builder.create<FmaFOp>(y2, x, cstCephesLogP8); 324*ce976d2dSEugene Zhulenev y0 = builder.create<FmaFOp>(y0, x3, y1); 325*ce976d2dSEugene Zhulenev y0 = builder.create<FmaFOp>(y0, x3, y2); 326*ce976d2dSEugene Zhulenev y0 = builder.create<MulFOp>(y0, x3); 327*ce976d2dSEugene Zhulenev 328*ce976d2dSEugene Zhulenev y0 = builder.create<FmaFOp>(cstNegHalf, x2, y0); 329*ce976d2dSEugene Zhulenev x = builder.create<AddFOp>(x, y0); 330*ce976d2dSEugene Zhulenev 331*ce976d2dSEugene Zhulenev Value cstLn2 = bcast(f32Cst(builder, static_cast<float>(LN2_VALUE))); 332*ce976d2dSEugene Zhulenev x = builder.create<FmaFOp>(e, cstLn2, x); 333*ce976d2dSEugene Zhulenev 334*ce976d2dSEugene Zhulenev Value invalidMask = 335*ce976d2dSEugene Zhulenev builder.create<CmpFOp>(CmpFPredicate::ULT, op.operand(), cstZero); 336*ce976d2dSEugene Zhulenev Value zeroMask = 337*ce976d2dSEugene Zhulenev builder.create<CmpFOp>(CmpFPredicate::OEQ, op.operand(), cstZero); 338*ce976d2dSEugene Zhulenev Value posInfMask = 339*ce976d2dSEugene Zhulenev builder.create<CmpFOp>(CmpFPredicate::OEQ, op.operand(), cstPosInf); 340*ce976d2dSEugene Zhulenev 341*ce976d2dSEugene Zhulenev // Filter out invalid values: 342*ce976d2dSEugene Zhulenev // • x == 0 -> -INF 343*ce976d2dSEugene Zhulenev // • x < 0 -> NAN 344*ce976d2dSEugene Zhulenev // • x == +INF -> +INF 345*ce976d2dSEugene Zhulenev Value aproximation = builder.create<SelectOp>( 346*ce976d2dSEugene Zhulenev zeroMask, cstMinusInf, 347*ce976d2dSEugene Zhulenev builder.create<SelectOp>( 348*ce976d2dSEugene Zhulenev invalidMask, cstNan, 349*ce976d2dSEugene Zhulenev builder.create<SelectOp>(posInfMask, cstPosInf, x))); 350*ce976d2dSEugene Zhulenev 351*ce976d2dSEugene Zhulenev rewriter.replaceOp(op, aproximation); 352*ce976d2dSEugene Zhulenev 353*ce976d2dSEugene Zhulenev return success(); 354*ce976d2dSEugene Zhulenev } 355*ce976d2dSEugene Zhulenev 356*ce976d2dSEugene Zhulenev //----------------------------------------------------------------------------// 357f99ccf65SEugene Zhulenev 358f99ccf65SEugene Zhulenev void mlir::populateMathPolynomialApproximationPatterns( 359f99ccf65SEugene Zhulenev OwningRewritePatternList &patterns, MLIRContext *ctx) { 360*ce976d2dSEugene Zhulenev patterns.insert<TanhApproximation, LogApproximation>(ctx); 361f99ccf65SEugene Zhulenev } 362