math_ir.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #pragma once
  2. #include <yql/essentials/udfs/common/math/lib/round.h>
  3. #include <yql/essentials/udfs/common/math/lib/erfinv.h>
  4. #include <yql/essentials/public/udf/udf_value.h>
  5. #include <util/generic/ymath.h>
  6. #include <util/system/compiler.h>
  7. #include <math.h>
  8. namespace NYql {
  9. namespace NUdf {
  10. #define CONST_FUNCS(XX) \
  11. XX(Pi, M_PI) \
  12. XX(E, M_E) \
  13. XX(Eps, std::numeric_limits<double>::epsilon()) \
  14. XX(RoundDownward, 0) \
  15. XX(RoundToNearest, 1) \
  16. XX(RoundTowardZero, 2) \
  17. XX(RoundUpward, 3)
  18. #define SINGLE_ARG_FUNCS(XX) \
  19. XX(Abs, Abs) \
  20. XX(Acos, acos) \
  21. XX(Asin, asin) \
  22. XX(Asinh, asin) \
  23. XX(Atan, atan) \
  24. XX(Cbrt, cbrt) \
  25. XX(Ceil, ceil) \
  26. XX(Cos, cos) \
  27. XX(Cosh, cosh) \
  28. XX(Erf, Erf) \
  29. XX(Exp, exp) \
  30. XX(Exp2, Exp2) \
  31. XX(Fabs, fabs) \
  32. XX(Floor, std::floor) \
  33. XX(Lgamma, LogGamma) \
  34. XX(Rint, rint) \
  35. XX(Sin, sin) \
  36. XX(Sinh, sinh) \
  37. XX(Sqrt, sqrt) \
  38. XX(Tan, tan) \
  39. XX(Tanh, tanh) \
  40. XX(Tgamma, tgamma) \
  41. XX(Trunc, trunc) \
  42. XX(IsFinite, std::isfinite) \
  43. XX(IsInf, std::isinf) \
  44. XX(IsNaN, std::isnan)
  45. #define TWO_ARGS_FUNCS(XX) \
  46. XX(Atan2, atan2, double) \
  47. XX(Fmod, fmod, double) \
  48. XX(Hypot, hypot, double) \
  49. XX(Remainder, remainder, double) \
  50. XX(Pow, pow, double) \
  51. XX(Ldexp, ldexp, int)
  52. #define POSITIVE_SINGLE_ARG_FUNCS(XX) \
  53. XX(Log, log) \
  54. XX(Log2, Log2) \
  55. XX(Log10, log10)
  56. #define CONST_IMPL(name, cnst) \
  57. extern "C" UDF_ALWAYS_INLINE \
  58. void name##IR(const IBoxedValue* /*pThis*/, TUnboxedValuePod* result, const IValueBuilder* /*valueBuilder*/, const TUnboxedValuePod* /*args*/) {\
  59. *result = TUnboxedValuePod(cnst); \
  60. }
  61. #define SINGLE_ARG_IMPL(name, func) \
  62. extern "C" UDF_ALWAYS_INLINE \
  63. void name##IR(const IBoxedValue* /*pThis*/, TUnboxedValuePod* result, const IValueBuilder* /*valueBuilder*/, const TUnboxedValuePod* args) { \
  64. *result = TUnboxedValuePod(func(args[0].Get<double>())); \
  65. }
  66. #define TWO_ARGS_IMPL(name, func, secondType) \
  67. extern "C" UDF_ALWAYS_INLINE \
  68. void name##IR(const IBoxedValue* /*pThis*/, TUnboxedValuePod* result, const IValueBuilder* /*valueBuilder*/, const TUnboxedValuePod* args) { \
  69. *result = TUnboxedValuePod(func(args[0].Get<double>(), args[1].Get<secondType>())); \
  70. }
  71. #define POSITIVE_SINGLE_ARG_IMPL(name, func) \
  72. extern "C" UDF_ALWAYS_INLINE \
  73. void name##IR(const IBoxedValue* /*pThis*/, TUnboxedValuePod* result, const IValueBuilder* /*valueBuilder*/, const TUnboxedValuePod* args) { \
  74. double input = args[0].Get<double>(); \
  75. if (input > 0) { \
  76. *result = TUnboxedValuePod(func(input)); \
  77. } else { \
  78. *result = TUnboxedValuePod(static_cast<double>(NAN)); \
  79. } \
  80. }
  81. CONST_FUNCS(CONST_IMPL)
  82. SINGLE_ARG_FUNCS(SINGLE_ARG_IMPL)
  83. TWO_ARGS_FUNCS(TWO_ARGS_IMPL)
  84. POSITIVE_SINGLE_ARG_FUNCS(POSITIVE_SINGLE_ARG_IMPL)
  85. extern "C" UDF_ALWAYS_INLINE
  86. void SigmoidIR(const IBoxedValue* /*pThis*/, TUnboxedValuePod* result, const IValueBuilder* /*valueBuilder*/, const TUnboxedValuePod* args) {
  87. *result = TUnboxedValuePod(1. / (1. + exp(-args[0].Get<double>())));
  88. }
  89. extern "C" UDF_ALWAYS_INLINE
  90. void FuzzyEqualsIR(const IBoxedValue* /*pThis*/, TUnboxedValuePod* result, const IValueBuilder* /*valueBuilder*/, const TUnboxedValuePod* args) {
  91. if (!args[2]) {
  92. *result = TUnboxedValuePod(FuzzyEquals(args[0].Get<double>(), args[1].Get<double>()));
  93. } else {
  94. const double eps = args[2].Get<double>();
  95. *result = TUnboxedValuePod(FuzzyEquals(args[0].Get<double>(), args[1].Get<double>(), eps));
  96. }
  97. }
  98. extern "C" UDF_ALWAYS_INLINE
  99. void RoundIR(const IBoxedValue* /*pThis*/, TUnboxedValuePod* result, const IValueBuilder* /*valueBuilder*/, const TUnboxedValuePod* args) {
  100. const double val = NMathUdf::RoundToDecimal<long double>(args[0].Get<double>(), args[1].GetOrDefault<int>(0));
  101. *result = TUnboxedValuePod(val);
  102. }
  103. extern "C" UDF_ALWAYS_INLINE
  104. void ErfInvIR(const IBoxedValue* /*pThis*/, TUnboxedValuePod* result, const IValueBuilder* /*valueBuilder*/, const TUnboxedValuePod* args) {
  105. *result = TUnboxedValuePod(NMathUdf::ErfInv(args[0].Get<double>()));
  106. }
  107. extern "C" UDF_ALWAYS_INLINE
  108. void ErfcInvIR(const IBoxedValue* /*pThis*/, TUnboxedValuePod* result, const IValueBuilder* /*valueBuilder*/, const TUnboxedValuePod* args) {
  109. *result = TUnboxedValuePod(NMathUdf::ErfInv(1. - args[0].Get<double>()));
  110. }
  111. extern "C" UDF_ALWAYS_INLINE
  112. void ModIR(const IBoxedValue* /*pThis*/, TUnboxedValuePod* result, const IValueBuilder* /*valueBuilder*/, const TUnboxedValuePod* args) {
  113. const auto val = NMathUdf::Mod(args[0].Get<i64>(), args[1].Get<i64>());
  114. *result = val ? TUnboxedValuePod(*val) : TUnboxedValuePod();
  115. }
  116. extern "C" UDF_ALWAYS_INLINE
  117. void RemIR(const IBoxedValue* /*pThis*/, TUnboxedValuePod* result, const IValueBuilder* /*valueBuilder*/, const TUnboxedValuePod* args) {
  118. const auto val = NMathUdf::Rem(args[0].Get<i64>(), args[1].Get<i64>());
  119. *result = val ? TUnboxedValuePod(*val) : TUnboxedValuePod();
  120. }
  121. extern "C" UDF_ALWAYS_INLINE
  122. void NearbyIntIR(const IBoxedValue* /*pThis*/, TUnboxedValuePod* result, const IValueBuilder* /*valueBuilder*/, const TUnboxedValuePod* args) {
  123. const auto val = NMathUdf::NearbyInt(args[0].Get<double>(), args[1].Get<ui32>());
  124. *result = val ? TUnboxedValuePod(*val) : TUnboxedValuePod();
  125. }
  126. } // NUdf
  127. } // NYql