mkql_builtins_decimal.h.txt 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #pragma once
  2. #include "mkql_builtins_impl.h" // Y_IGNORE
  3. namespace NKikimr {
  4. namespace NMiniKQL {
  5. namespace NDecimal {
  6. #ifndef MKQL_DISABLE_CODEGEN
  7. ConstantInt* GenConstant(NYql::NDecimal::TInt128 value, LLVMContext &context);
  8. template<ui8 Precision, bool IncLow = false, bool DecHigh = false>
  9. inline std::pair<ConstantInt*, ConstantInt*> GenBounds(LLVMContext &context) {
  10. const auto& bounds = NYql::NDecimal::GetBounds<Precision, IncLow, DecHigh>();
  11. return std::make_pair(GenConstant(bounds.first, context), GenConstant(bounds.second, context));
  12. }
  13. template<bool IncludeBounds = false>
  14. Value* GenInBounds(Value* val, ConstantInt* low, ConstantInt* high, BasicBlock* block);
  15. template<bool IncludeBounds = true>
  16. Value* GenOutOfBounds(Value* val, ConstantInt* low, ConstantInt* high, BasicBlock* block);
  17. Value* GenIsError(Value* val, LLVMContext &context, BasicBlock* block);
  18. Value* GenIsNormal(Value* val, LLVMContext &context, BasicBlock* block);
  19. Value* GenIsAbnormal(Value* val, LLVMContext &context, BasicBlock* block);
  20. Value* GenIsComparable(Value* val, LLVMContext &context, BasicBlock* block);
  21. Value* GenIsNonComparable(Value* val, LLVMContext &context, BasicBlock* block);
  22. template <bool And, Value* (*Checker)(Value*, LLVMContext&, BasicBlock*)>
  23. inline Value* CheckBoth(Value* left, Value* right, LLVMContext &context, BasicBlock* block) {
  24. const auto l = Checker(left, context, block);
  25. const auto r = Checker(right, context, block);
  26. return And ? BinaryOperator::CreateAnd(l, r, "and", block) : BinaryOperator::CreateOr(l, r, "or", block);
  27. }
  28. #endif
  29. template <
  30. class TFunc,
  31. template<typename, typename, typename, bool, bool> class TArgs
  32. >
  33. void RegisterBinaryFunction(IBuiltinFunctionRegistry& registry, const std::string_view& name) {
  34. RegisterFunctionImpl<TFunc, TArgs<NUdf::TDataType<NUdf::TDecimal>, NUdf::TDataType<NUdf::TDecimal>, NUdf::TDataType<NUdf::TDecimal>, false, false>, TBinaryWrap<false, false>>(registry, name);
  35. RegisterFunctionImpl<TFunc, TArgs<NUdf::TDataType<NUdf::TDecimal>, NUdf::TDataType<NUdf::TDecimal>, NUdf::TDataType<NUdf::TDecimal>, true, false>, TBinaryWrap<true, false>>(registry, name);
  36. RegisterFunctionImpl<TFunc, TArgs<NUdf::TDataType<NUdf::TDecimal>, NUdf::TDataType<NUdf::TDecimal>, NUdf::TDataType<NUdf::TDecimal>, false, true>, TBinaryWrap<false, true>>(registry, name);
  37. RegisterFunctionImpl<TFunc, TArgs<NUdf::TDataType<NUdf::TDecimal>, NUdf::TDataType<NUdf::TDecimal>, NUdf::TDataType<NUdf::TDecimal>, true, true>, TBinaryWrap<true, true>>(registry, name);
  38. }
  39. template <
  40. class TFunc,
  41. template<typename, typename, bool> class TArgs
  42. >
  43. void RegisterAggregateFunction(IBuiltinFunctionRegistry& registry, const std::string_view& name) {
  44. RegisterFunctionImpl<TFunc, TArgs<NUdf::TDataType<NUdf::TDecimal>, NUdf::TDataType<NUdf::TDecimal>, false>, TBinaryWrap<false, false>>(registry, name);
  45. RegisterFunctionImpl<TFunc, TArgs<NUdf::TDataType<NUdf::TDecimal>, NUdf::TDataType<NUdf::TDecimal>, true>, TAggregateWrap>(registry, name);
  46. }
  47. template <
  48. ui8 Precision,
  49. template<ui8> class TFunc,
  50. template<typename, typename, typename, bool, bool> class TArgs
  51. >
  52. void RegisterBinaryFunctionForPrecision(IBuiltinFunctionRegistry& registry, const std::string_view& name) {
  53. RegisterBinaryFunction<TFunc<Precision>, TArgs>(registry, TString(name) += ToString(Precision));
  54. }
  55. template <
  56. ui8 Precision,
  57. template<ui8> class TFunc,
  58. template<typename, typename, bool> class TArgs
  59. >
  60. void RegisterAggregateFunctionForPrecision(IBuiltinFunctionRegistry& registry, const std::string_view& name) {
  61. RegisterAggregateFunction<TFunc<Precision>, TArgs>(registry, TString(name) += ToString(Precision));
  62. }
  63. template <
  64. class TFunc,
  65. template<typename, typename, bool> class TArgs
  66. >
  67. void RegisterUnaryFunction(IBuiltinFunctionRegistry& registry, const std::string_view& name) {
  68. RegisterFunctionImpl<TFunc, TArgs<NUdf::TDataType<NUdf::TDecimal>, NUdf::TDataType<NUdf::TDecimal>, false>, TUnaryStub>(registry, name);
  69. RegisterFunctionImpl<TFunc, TArgs<NUdf::TDataType<NUdf::TDecimal>, NUdf::TDataType<NUdf::TDecimal>, true>, TUnaryWrap>(registry, name);
  70. }
  71. template <
  72. ui8 Precision,
  73. template<ui8> class TFunc,
  74. template<typename, typename, bool> class TArgs
  75. >
  76. void RegisterUnaryFunctionForPrecision(IBuiltinFunctionRegistry& registry, const std::string_view& name) {
  77. RegisterUnaryFunction<TFunc<Precision>, TArgs>(registry, TString(name) += ToString(Precision));
  78. }
  79. template <
  80. class TFunc,
  81. template<typename, typename, bool> class TArgs,
  82. typename TOutput
  83. >
  84. void RegisterCastFunction(IBuiltinFunctionRegistry& registry, const std::string_view& name) {
  85. RegisterFunctionImpl<TFunc, TArgs<NUdf::TDataType<NUdf::TDecimal>, TOutput, false>, TUnaryStub>(registry, name);
  86. RegisterFunctionImpl<TFunc, TArgs<NUdf::TDataType<NUdf::TDecimal>, TOutput, true>, TUnaryWrap>(registry, name);
  87. }
  88. template <
  89. ui8 Precision,
  90. template<ui8> class TFunc,
  91. template<typename, typename, bool> class TArgs,
  92. typename TOutput
  93. >
  94. void RegisterCastFunctionForPrecision(IBuiltinFunctionRegistry& registry, const std::string_view& name) {
  95. RegisterCastFunction<TFunc<Precision>, TArgs, TOutput>(registry, TString(name) += ToString(Precision));
  96. }
  97. template <
  98. template<ui8> class TFunc,
  99. template<typename, typename, bool> class TArgs
  100. >
  101. void RegisterUnaryFunctionForAllPrecisions(IBuiltinFunctionRegistry& registry, const std::string_view& name) {
  102. RegisterUnaryFunctionForPrecision<1, TFunc, TArgs>(registry, name);
  103. RegisterUnaryFunctionForPrecision<2, TFunc, TArgs>(registry, name);
  104. RegisterUnaryFunctionForPrecision<3, TFunc, TArgs>(registry, name);
  105. RegisterUnaryFunctionForPrecision<4, TFunc, TArgs>(registry, name);
  106. RegisterUnaryFunctionForPrecision<5, TFunc, TArgs>(registry, name);
  107. RegisterUnaryFunctionForPrecision<6, TFunc, TArgs>(registry, name);
  108. RegisterUnaryFunctionForPrecision<7, TFunc, TArgs>(registry, name);
  109. RegisterUnaryFunctionForPrecision<8, TFunc, TArgs>(registry, name);
  110. RegisterUnaryFunctionForPrecision<9, TFunc, TArgs>(registry, name);
  111. RegisterUnaryFunctionForPrecision<10, TFunc, TArgs>(registry, name);
  112. RegisterUnaryFunctionForPrecision<11, TFunc, TArgs>(registry, name);
  113. RegisterUnaryFunctionForPrecision<12, TFunc, TArgs>(registry, name);
  114. RegisterUnaryFunctionForPrecision<13, TFunc, TArgs>(registry, name);
  115. RegisterUnaryFunctionForPrecision<14, TFunc, TArgs>(registry, name);
  116. RegisterUnaryFunctionForPrecision<15, TFunc, TArgs>(registry, name);
  117. RegisterUnaryFunctionForPrecision<16, TFunc, TArgs>(registry, name);
  118. RegisterUnaryFunctionForPrecision<17, TFunc, TArgs>(registry, name);
  119. RegisterUnaryFunctionForPrecision<18, TFunc, TArgs>(registry, name);
  120. RegisterUnaryFunctionForPrecision<19, TFunc, TArgs>(registry, name);
  121. RegisterUnaryFunctionForPrecision<20, TFunc, TArgs>(registry, name);
  122. RegisterUnaryFunctionForPrecision<21, TFunc, TArgs>(registry, name);
  123. RegisterUnaryFunctionForPrecision<22, TFunc, TArgs>(registry, name);
  124. RegisterUnaryFunctionForPrecision<23, TFunc, TArgs>(registry, name);
  125. RegisterUnaryFunctionForPrecision<24, TFunc, TArgs>(registry, name);
  126. RegisterUnaryFunctionForPrecision<25, TFunc, TArgs>(registry, name);
  127. RegisterUnaryFunctionForPrecision<26, TFunc, TArgs>(registry, name);
  128. RegisterUnaryFunctionForPrecision<27, TFunc, TArgs>(registry, name);
  129. RegisterUnaryFunctionForPrecision<28, TFunc, TArgs>(registry, name);
  130. RegisterUnaryFunctionForPrecision<29, TFunc, TArgs>(registry, name);
  131. RegisterUnaryFunctionForPrecision<30, TFunc, TArgs>(registry, name);
  132. RegisterUnaryFunctionForPrecision<31, TFunc, TArgs>(registry, name);
  133. RegisterUnaryFunctionForPrecision<32, TFunc, TArgs>(registry, name);
  134. RegisterUnaryFunctionForPrecision<33, TFunc, TArgs>(registry, name);
  135. RegisterUnaryFunctionForPrecision<34, TFunc, TArgs>(registry, name);
  136. RegisterUnaryFunctionForPrecision<35, TFunc, TArgs>(registry, name);
  137. }
  138. template <
  139. template<ui8> class TFunc,
  140. template<typename, typename, bool> class TArgs,
  141. typename TOutput
  142. >
  143. void RegisterCastFunctionForAllPrecisions(IBuiltinFunctionRegistry& registry, const std::string_view& name) {
  144. RegisterCastFunctionForPrecision<0, TFunc, TArgs, TOutput>(registry, name);
  145. RegisterCastFunctionForPrecision<1, TFunc, TArgs, TOutput>(registry, name);
  146. RegisterCastFunctionForPrecision<2, TFunc, TArgs, TOutput>(registry, name);
  147. RegisterCastFunctionForPrecision<3, TFunc, TArgs, TOutput>(registry, name);
  148. RegisterCastFunctionForPrecision<4, TFunc, TArgs, TOutput>(registry, name);
  149. RegisterCastFunctionForPrecision<5, TFunc, TArgs, TOutput>(registry, name);
  150. RegisterCastFunctionForPrecision<6, TFunc, TArgs, TOutput>(registry, name);
  151. RegisterCastFunctionForPrecision<7, TFunc, TArgs, TOutput>(registry, name);
  152. RegisterCastFunctionForPrecision<8, TFunc, TArgs, TOutput>(registry, name);
  153. RegisterCastFunctionForPrecision<9, TFunc, TArgs, TOutput>(registry, name);
  154. RegisterCastFunctionForPrecision<10, TFunc, TArgs, TOutput>(registry, name);
  155. RegisterCastFunctionForPrecision<11, TFunc, TArgs, TOutput>(registry, name);
  156. RegisterCastFunctionForPrecision<12, TFunc, TArgs, TOutput>(registry, name);
  157. RegisterCastFunctionForPrecision<13, TFunc, TArgs, TOutput>(registry, name);
  158. RegisterCastFunctionForPrecision<14, TFunc, TArgs, TOutput>(registry, name);
  159. RegisterCastFunctionForPrecision<15, TFunc, TArgs, TOutput>(registry, name);
  160. RegisterCastFunctionForPrecision<16, TFunc, TArgs, TOutput>(registry, name);
  161. RegisterCastFunctionForPrecision<17, TFunc, TArgs, TOutput>(registry, name);
  162. RegisterCastFunctionForPrecision<18, TFunc, TArgs, TOutput>(registry, name);
  163. RegisterCastFunctionForPrecision<19, TFunc, TArgs, TOutput>(registry, name);
  164. RegisterCastFunctionForPrecision<20, TFunc, TArgs, TOutput>(registry, name);
  165. RegisterCastFunctionForPrecision<21, TFunc, TArgs, TOutput>(registry, name);
  166. RegisterCastFunctionForPrecision<22, TFunc, TArgs, TOutput>(registry, name);
  167. RegisterCastFunctionForPrecision<23, TFunc, TArgs, TOutput>(registry, name);
  168. RegisterCastFunctionForPrecision<24, TFunc, TArgs, TOutput>(registry, name);
  169. RegisterCastFunctionForPrecision<25, TFunc, TArgs, TOutput>(registry, name);
  170. RegisterCastFunctionForPrecision<26, TFunc, TArgs, TOutput>(registry, name);
  171. RegisterCastFunctionForPrecision<27, TFunc, TArgs, TOutput>(registry, name);
  172. RegisterCastFunctionForPrecision<28, TFunc, TArgs, TOutput>(registry, name);
  173. RegisterCastFunctionForPrecision<29, TFunc, TArgs, TOutput>(registry, name);
  174. RegisterCastFunctionForPrecision<30, TFunc, TArgs, TOutput>(registry, name);
  175. RegisterCastFunctionForPrecision<31, TFunc, TArgs, TOutput>(registry, name);
  176. RegisterCastFunctionForPrecision<32, TFunc, TArgs, TOutput>(registry, name);
  177. RegisterCastFunctionForPrecision<33, TFunc, TArgs, TOutput>(registry, name);
  178. RegisterCastFunctionForPrecision<34, TFunc, TArgs, TOutput>(registry, name);
  179. RegisterCastFunctionForPrecision<35, TFunc, TArgs, TOutput>(registry, name);
  180. }
  181. template <
  182. template<ui8> class TFunc,
  183. template<typename, typename, typename, bool, bool> class TArgs
  184. >
  185. void RegisterBinaryFunctionForAllPrecisions(IBuiltinFunctionRegistry& registry, const std::string_view& name) {
  186. RegisterBinaryFunctionForPrecision<1, TFunc, TArgs>(registry, name);
  187. RegisterBinaryFunctionForPrecision<2, TFunc, TArgs>(registry, name);
  188. RegisterBinaryFunctionForPrecision<3, TFunc, TArgs>(registry, name);
  189. RegisterBinaryFunctionForPrecision<4, TFunc, TArgs>(registry, name);
  190. RegisterBinaryFunctionForPrecision<5, TFunc, TArgs>(registry, name);
  191. RegisterBinaryFunctionForPrecision<6, TFunc, TArgs>(registry, name);
  192. RegisterBinaryFunctionForPrecision<7, TFunc, TArgs>(registry, name);
  193. RegisterBinaryFunctionForPrecision<8, TFunc, TArgs>(registry, name);
  194. RegisterBinaryFunctionForPrecision<9, TFunc, TArgs>(registry, name);
  195. RegisterBinaryFunctionForPrecision<10,TFunc, TArgs>(registry, name);
  196. RegisterBinaryFunctionForPrecision<11, TFunc, TArgs>(registry, name);
  197. RegisterBinaryFunctionForPrecision<12, TFunc, TArgs>(registry, name);
  198. RegisterBinaryFunctionForPrecision<13, TFunc, TArgs>(registry, name);
  199. RegisterBinaryFunctionForPrecision<14, TFunc, TArgs>(registry, name);
  200. RegisterBinaryFunctionForPrecision<15, TFunc, TArgs>(registry, name);
  201. RegisterBinaryFunctionForPrecision<16, TFunc, TArgs>(registry, name);
  202. RegisterBinaryFunctionForPrecision<17, TFunc, TArgs>(registry, name);
  203. RegisterBinaryFunctionForPrecision<18, TFunc, TArgs>(registry, name);
  204. RegisterBinaryFunctionForPrecision<19, TFunc, TArgs>(registry, name);
  205. RegisterBinaryFunctionForPrecision<20, TFunc, TArgs>(registry, name);
  206. RegisterBinaryFunctionForPrecision<21, TFunc, TArgs>(registry, name);
  207. RegisterBinaryFunctionForPrecision<22, TFunc, TArgs>(registry, name);
  208. RegisterBinaryFunctionForPrecision<23, TFunc, TArgs>(registry, name);
  209. RegisterBinaryFunctionForPrecision<24, TFunc, TArgs>(registry, name);
  210. RegisterBinaryFunctionForPrecision<25, TFunc, TArgs>(registry, name);
  211. RegisterBinaryFunctionForPrecision<26, TFunc, TArgs>(registry, name);
  212. RegisterBinaryFunctionForPrecision<27, TFunc, TArgs>(registry, name);
  213. RegisterBinaryFunctionForPrecision<28, TFunc, TArgs>(registry, name);
  214. RegisterBinaryFunctionForPrecision<29, TFunc, TArgs>(registry, name);
  215. RegisterBinaryFunctionForPrecision<30, TFunc, TArgs>(registry, name);
  216. RegisterBinaryFunctionForPrecision<31, TFunc, TArgs>(registry, name);
  217. RegisterBinaryFunctionForPrecision<32, TFunc, TArgs>(registry, name);
  218. RegisterBinaryFunctionForPrecision<33, TFunc, TArgs>(registry, name);
  219. RegisterBinaryFunctionForPrecision<34, TFunc, TArgs>(registry, name);
  220. RegisterBinaryFunctionForPrecision<35, TFunc, TArgs>(registry, name);
  221. }
  222. template <
  223. template<ui8> class TFunc,
  224. template<typename, typename, bool> class TArgs
  225. >
  226. void RegisterAggregateFunctionForAllPrecisions(IBuiltinFunctionRegistry& registry, const std::string_view& name) {
  227. RegisterAggregateFunctionForPrecision<1, TFunc, TArgs>(registry, name);
  228. RegisterAggregateFunctionForPrecision<2, TFunc, TArgs>(registry, name);
  229. RegisterAggregateFunctionForPrecision<3, TFunc, TArgs>(registry, name);
  230. RegisterAggregateFunctionForPrecision<4, TFunc, TArgs>(registry, name);
  231. RegisterAggregateFunctionForPrecision<5, TFunc, TArgs>(registry, name);
  232. RegisterAggregateFunctionForPrecision<6, TFunc, TArgs>(registry, name);
  233. RegisterAggregateFunctionForPrecision<7, TFunc, TArgs>(registry, name);
  234. RegisterAggregateFunctionForPrecision<8, TFunc, TArgs>(registry, name);
  235. RegisterAggregateFunctionForPrecision<9, TFunc, TArgs>(registry, name);
  236. RegisterAggregateFunctionForPrecision<10, TFunc, TArgs>(registry, name);
  237. RegisterAggregateFunctionForPrecision<11, TFunc, TArgs>(registry, name);
  238. RegisterAggregateFunctionForPrecision<12, TFunc, TArgs>(registry, name);
  239. RegisterAggregateFunctionForPrecision<13, TFunc, TArgs>(registry, name);
  240. RegisterAggregateFunctionForPrecision<14, TFunc, TArgs>(registry, name);
  241. RegisterAggregateFunctionForPrecision<15, TFunc, TArgs>(registry, name);
  242. RegisterAggregateFunctionForPrecision<16, TFunc, TArgs>(registry, name);
  243. RegisterAggregateFunctionForPrecision<17, TFunc, TArgs>(registry, name);
  244. RegisterAggregateFunctionForPrecision<18, TFunc, TArgs>(registry, name);
  245. RegisterAggregateFunctionForPrecision<19, TFunc, TArgs>(registry, name);
  246. RegisterAggregateFunctionForPrecision<20, TFunc, TArgs>(registry, name);
  247. RegisterAggregateFunctionForPrecision<21, TFunc, TArgs>(registry, name);
  248. RegisterAggregateFunctionForPrecision<22, TFunc, TArgs>(registry, name);
  249. RegisterAggregateFunctionForPrecision<23, TFunc, TArgs>(registry, name);
  250. RegisterAggregateFunctionForPrecision<24, TFunc, TArgs>(registry, name);
  251. RegisterAggregateFunctionForPrecision<25, TFunc, TArgs>(registry, name);
  252. RegisterAggregateFunctionForPrecision<26, TFunc, TArgs>(registry, name);
  253. RegisterAggregateFunctionForPrecision<27, TFunc, TArgs>(registry, name);
  254. RegisterAggregateFunctionForPrecision<28, TFunc, TArgs>(registry, name);
  255. RegisterAggregateFunctionForPrecision<29, TFunc, TArgs>(registry, name);
  256. RegisterAggregateFunctionForPrecision<30, TFunc, TArgs>(registry, name);
  257. RegisterAggregateFunctionForPrecision<31, TFunc, TArgs>(registry, name);
  258. RegisterAggregateFunctionForPrecision<32, TFunc, TArgs>(registry, name);
  259. RegisterAggregateFunctionForPrecision<33, TFunc, TArgs>(registry, name);
  260. RegisterAggregateFunctionForPrecision<34, TFunc, TArgs>(registry, name);
  261. RegisterAggregateFunctionForPrecision<35, TFunc, TArgs>(registry, name);
  262. }
  263. }
  264. }
  265. }