math_udf.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "math_ir.h"
  2. #include <yql/essentials/public/udf/udf_helpers.h>
  3. extern const char TagRoundingMode[] = "MathRoundingMode";
  4. using TTaggedRoundingMode = NYql::NUdf::TTagged<ui32, TagRoundingMode>;
  5. #define MATH_UDF_MAP(XX, XXL) \
  6. XX(Pi, double(), 0) \
  7. XX(E, double(), 0) \
  8. XX(Eps, double(), 0) \
  9. XX(RoundDownward, TTaggedRoundingMode(), 0) \
  10. XX(RoundToNearest, TTaggedRoundingMode(), 0) \
  11. XX(RoundTowardZero, TTaggedRoundingMode(), 0) \
  12. XX(RoundUpward, TTaggedRoundingMode(), 0) \
  13. XX(Abs, double(TAutoMap<double>), 0) \
  14. XX(Acos, double(TAutoMap<double>), 0) \
  15. XX(Asin, double(TAutoMap<double>), 0) \
  16. XX(Asinh, double(TAutoMap<double>), 0) \
  17. XX(Atan, double(TAutoMap<double>), 0) \
  18. XX(Cbrt, double(TAutoMap<double>), 0) \
  19. XX(Ceil, double(TAutoMap<double>), 0) \
  20. XX(Cos, double(TAutoMap<double>), 0) \
  21. XX(Cosh, double(TAutoMap<double>), 0) \
  22. XX(Erf, double(TAutoMap<double>), 0) \
  23. XX(ErfInv, double(TAutoMap<double>), 0) \
  24. XX(ErfcInv, double(TAutoMap<double>), 0) \
  25. XX(Exp, double(TAutoMap<double>), 0) \
  26. XX(Exp2, double(TAutoMap<double>), 0) \
  27. XX(Fabs, double(TAutoMap<double>), 0) \
  28. XX(Floor, double(TAutoMap<double>), 0) \
  29. XX(Lgamma, double(TAutoMap<double>), 0) \
  30. XX(Rint, double(TAutoMap<double>), 0) \
  31. XX(Sin, double(TAutoMap<double>), 0) \
  32. XX(Sinh, double(TAutoMap<double>), 0) \
  33. XX(Sqrt, double(TAutoMap<double>), 0) \
  34. XX(Tan, double(TAutoMap<double>), 0) \
  35. XX(Tanh, double(TAutoMap<double>), 0) \
  36. XX(Tgamma, double(TAutoMap<double>), 0) \
  37. XX(Trunc, double(TAutoMap<double>), 0) \
  38. XX(Log, double(TAutoMap<double>), 0) \
  39. XX(Log2, double(TAutoMap<double>), 0) \
  40. XX(Log10, double(TAutoMap<double>), 0) \
  41. XX(Atan2, double(TAutoMap<double>, TAutoMap<double>), 0) \
  42. XX(Fmod, double(TAutoMap<double>, TAutoMap<double>), 0) \
  43. XX(Hypot, double(TAutoMap<double>, TAutoMap<double>), 0) \
  44. XX(Remainder, double(TAutoMap<double>, TAutoMap<double>), 0) \
  45. XX(Pow, double(TAutoMap<double>, TAutoMap<double>), 0) \
  46. XX(Ldexp, double(TAutoMap<double>, TAutoMap<int>), 0) \
  47. XX(IsFinite, bool(TAutoMap<double>), 0) \
  48. XX(IsInf, bool(TAutoMap<double>), 0) \
  49. XX(IsNaN, bool(TAutoMap<double>), 0) \
  50. XX(Sigmoid, double(TAutoMap<double>), 0) \
  51. XX(FuzzyEquals, bool(TAutoMap<double>, TAutoMap<double>, TEpsilon), 1) \
  52. XX(Mod, TOptional<i64>(TAutoMap<i64>, i64), 0) \
  53. XX(Rem, TOptional<i64>(TAutoMap<i64>, i64), 0) \
  54. XXL(Round, double(TAutoMap<double>, TPrecision), 1)
  55. #define MATH_UDF_MAP_WITHOUT_IR(XX) \
  56. XX(NearbyInt, TOptional<i64>(TAutoMap<double>, TTaggedRoundingMode), 0)
  57. #ifdef DISABLE_IR
  58. #define MATH_STRICT_UDF(name, signature, optionalArgsCount) \
  59. SIMPLE_STRICT_UDF_WITH_OPTIONAL_ARGS(T##name, signature, optionalArgsCount) { \
  60. TUnboxedValuePod res; \
  61. name##IR(this, &res, valueBuilder, args); \
  62. return res; \
  63. }
  64. #else
  65. #define MATH_STRICT_UDF(name, signature, optionalArgsCount) \
  66. SIMPLE_STRICT_UDF_WITH_IR(T##name, signature, optionalArgsCount, "/llvm_bc/Math", #name "IR") { \
  67. TUnboxedValuePod res; \
  68. name##IR(this, &res, valueBuilder, args); \
  69. return res; \
  70. }
  71. #endif
  72. #define MATH_STRICT_UDF_WITHOUT_IR(name, signature, optionalArgsCount) \
  73. SIMPLE_STRICT_UDF_WITH_OPTIONAL_ARGS(T##name, signature, optionalArgsCount) { \
  74. TUnboxedValuePod res; \
  75. name##IR(this, &res, valueBuilder, args); \
  76. return res; \
  77. }
  78. #define REGISTER_MATH_UDF(udfName, ...) T##udfName,
  79. #define REGISTER_MATH_UDF_LAST(udfName, ...) T##udfName
  80. using namespace NKikimr;
  81. using namespace NUdf;
  82. namespace {
  83. extern const char epsilon[] = "Epsilon";
  84. using TEpsilon = TNamedArg<double, epsilon>;
  85. extern const char precision[] = "Precision";
  86. using TPrecision = TNamedArg<int, precision>;
  87. MATH_UDF_MAP(MATH_STRICT_UDF, MATH_STRICT_UDF)
  88. MATH_UDF_MAP_WITHOUT_IR(MATH_STRICT_UDF_WITHOUT_IR)
  89. SIMPLE_MODULE(TMathModule,
  90. MATH_UDF_MAP_WITHOUT_IR(REGISTER_MATH_UDF)
  91. MATH_UDF_MAP(REGISTER_MATH_UDF, REGISTER_MATH_UDF_LAST))
  92. }
  93. REGISTER_MODULES(TMathModule)