@@ -38,6 +38,7 @@ enum class IntrinsicScalarFunctions : int64_t {
3838 Atan2,
3939 Gamma,
4040 LogGamma,
41+ Trunc,
4142 Abs,
4243 Exp,
4344 Exp2,
@@ -96,6 +97,7 @@ inline std::string get_intrinsic_name(int x) {
9697 INTRINSIC_NAME_CASE (Atan2)
9798 INTRINSIC_NAME_CASE (Gamma)
9899 INTRINSIC_NAME_CASE (LogGamma)
100+ INTRINSIC_NAME_CASE (Trunc)
99101 INTRINSIC_NAME_CASE (Abs)
100102 INTRINSIC_NAME_CASE (Exp)
101103 INTRINSIC_NAME_CASE (Exp2)
@@ -1142,6 +1144,44 @@ static inline ASR::expr_t* instantiate_LogGamma (Allocator &al,
11421144
11431145} // namespace LogGamma
11441146
1147+ #define create_trunc_macro (X, stdeval ) \
1148+ namespace X { \
1149+ static inline ASR::expr_t *eval_##X(Allocator &al, const Location &loc, \
1150+ ASR::ttype_t *t, Vec<ASR::expr_t *>& args) { \
1151+ LCOMPILERS_ASSERT (args.size () == 1 ); \
1152+ double rv = ASR::down_cast<ASR::RealConstant_t>(args[0 ])->m_r ; \
1153+ if (ASRUtils::extract_value (args[0 ], rv)) { \
1154+ double val = std::stdeval (rv); \
1155+ return make_ConstantWithType (make_RealConstant_t, val, t, loc); \
1156+ } \
1157+ return nullptr ; \
1158+ } \
1159+ static inline ASR::asr_t * create_##X(Allocator& al, const Location& loc, \
1160+ Vec<ASR::expr_t *>& args, \
1161+ const std::function<void (const std::string &, const Location &)> err) { \
1162+ ASR::ttype_t *type = ASRUtils::expr_type (args[0 ]); \
1163+ if (args.n != 1 ) { \
1164+ err (" Intrinsic `#X` accepts exactly one argument" , loc); \
1165+ } else if (!ASRUtils::is_real (*type)) { \
1166+ err (" `x` argument of `#X` must be real" , \
1167+ args[0 ]->base .loc ); \
1168+ } \
1169+ return UnaryIntrinsicFunction::create_UnaryFunction (al, loc, args, \
1170+ eval_##X, static_cast <int64_t >(IntrinsicScalarFunctions::Trunc), \
1171+ 0 , type); \
1172+ } \
1173+ static inline ASR::expr_t * instantiate_##X (Allocator &al, \
1174+ const Location &loc, SymbolTable *scope, Vec<ASR::ttype_t *>& arg_types, \
1175+ ASR::ttype_t *return_type, Vec<ASR::call_arg_t >& new_args, \
1176+ int64_t overload_id) { \
1177+ ASR::ttype_t * arg_type = arg_types[0 ]; \
1178+ return UnaryIntrinsicFunction::instantiate_functions (al, loc, scope, \
1179+ " #X" , arg_type, return_type, new_args, overload_id); \
1180+ } \
1181+ } // namespace X
1182+
1183+ create_trunc_macro (Trunc, trunc)
1184+
11451185// `X` is the name of the function in the IntrinsicScalarFunctions enum and
11461186// we use the same name for `create_X` and other places
11471187// `stdeval` is the name of the function in the `std` namespace for compile
@@ -2879,6 +2919,8 @@ namespace IntrinsicScalarFunctionRegistry {
28792919 verify_function>>& intrinsic_function_by_id_db = {
28802920 {static_cast <int64_t >(IntrinsicScalarFunctions::LogGamma),
28812921 {&LogGamma::instantiate_LogGamma, &UnaryIntrinsicFunction::verify_args}},
2922+ {static_cast <int64_t >(IntrinsicScalarFunctions::Trunc),
2923+ {&Trunc::instantiate_Trunc, &UnaryIntrinsicFunction::verify_args}},
28822924 {static_cast <int64_t >(IntrinsicScalarFunctions::Sin),
28832925 {&Sin::instantiate_Sin, &UnaryIntrinsicFunction::verify_args}},
28842926 {static_cast <int64_t >(IntrinsicScalarFunctions::Cos),
@@ -2977,6 +3019,8 @@ namespace IntrinsicScalarFunctionRegistry {
29773019 {static_cast <int64_t >(IntrinsicScalarFunctions::LogGamma),
29783020 " log_gamma" },
29793021
3022+ {static_cast <int64_t >(IntrinsicScalarFunctions::Trunc),
3023+ " trunc" },
29803024 {static_cast <int64_t >(IntrinsicScalarFunctions::Sin),
29813025 " sin" },
29823026 {static_cast <int64_t >(IntrinsicScalarFunctions::Cos),
@@ -3074,6 +3118,7 @@ namespace IntrinsicScalarFunctionRegistry {
30743118 std::tuple<create_intrinsic_function,
30753119 eval_intrinsic_function>>& intrinsic_function_by_name_db = {
30763120 {" log_gamma" , {&LogGamma::create_LogGamma, &LogGamma::eval_log_gamma}},
3121+ {" trunc" , {&Trunc::create_Trunc, &Trunc::eval_Trunc}},
30773122 {" sin" , {&Sin::create_Sin, &Sin::eval_Sin}},
30783123 {" cos" , {&Cos::create_Cos, &Cos::eval_Cos}},
30793124 {" tan" , {&Tan::create_Tan, &Tan::eval_Tan}},
@@ -3134,6 +3179,7 @@ namespace IntrinsicScalarFunctionRegistry {
31343179 id_ == IntrinsicScalarFunctions::Cos ||
31353180 id_ == IntrinsicScalarFunctions::Gamma ||
31363181 id_ == IntrinsicScalarFunctions::LogGamma ||
3182+ id_ == IntrinsicScalarFunctions::Trunc ||
31373183 id_ == IntrinsicScalarFunctions::Sin ||
31383184 id_ == IntrinsicScalarFunctions::Exp ||
31393185 id_ == IntrinsicScalarFunctions::Exp2 ||
0 commit comments