@@ -39,6 +39,7 @@ enum class IntrinsicScalarFunctions : int64_t {
3939 Gamma,
4040 LogGamma,
4141 Trunc,
42+ Fix,
4243 Abs,
4344 Exp,
4445 Exp2,
@@ -98,6 +99,7 @@ inline std::string get_intrinsic_name(int x) {
9899 INTRINSIC_NAME_CASE (Gamma)
99100 INTRINSIC_NAME_CASE (LogGamma)
100101 INTRINSIC_NAME_CASE (Trunc)
102+ INTRINSIC_NAME_CASE (Fix)
101103 INTRINSIC_NAME_CASE (Abs)
102104 INTRINSIC_NAME_CASE (Exp)
103105 INTRINSIC_NAME_CASE (Exp2)
@@ -1182,6 +1184,46 @@ namespace X {
11821184
11831185create_trunc_macro (Trunc, trunc)
11841186
1187+ namespace Fix {
1188+ static inline ASR::expr_t *eval_Fix (Allocator &al, const Location &loc,
1189+ ASR::ttype_t *t, Vec<ASR::expr_t *>& args) {
1190+ LCOMPILERS_ASSERT (args.size () == 1 );
1191+ double rv = ASR::down_cast<ASR::RealConstant_t>(args[0 ])->m_r ;
1192+ double val;
1193+ if (rv > 0.0 ) {
1194+ val = floor (rv);
1195+ } else {
1196+ val = ceil (rv);
1197+ }
1198+ return make_ConstantWithType (make_RealConstant_t, val, t, loc);
1199+ }
1200+
1201+ static inline ASR::asr_t * create_Fix (Allocator& al, const Location& loc,
1202+ Vec<ASR::expr_t *>& args,
1203+ const std::function<void (const std::string &, const Location &)> err) {
1204+ ASR::ttype_t *type = ASRUtils::expr_type (args[0 ]);
1205+ if (args.n != 1 ) {
1206+ err (" Intrinsic `fix` accepts exactly one argument" , loc);
1207+ } else if (!ASRUtils::is_real (*type)) {
1208+ err (" `fix` argument of `fix` must be real" ,
1209+ args[0 ]->base .loc );
1210+ }
1211+ return UnaryIntrinsicFunction::create_UnaryFunction (al, loc, args,
1212+ eval_Fix, static_cast <int64_t >(IntrinsicScalarFunctions::Fix),
1213+ 0 , type);
1214+ }
1215+
1216+ static inline ASR::expr_t * instantiate_Fix (Allocator &al,
1217+ const Location &loc, SymbolTable *scope, Vec<ASR::ttype_t *>& arg_types,
1218+ ASR::ttype_t *return_type, Vec<ASR::call_arg_t >& new_args,
1219+ int64_t overload_id) {
1220+ ASR::ttype_t * arg_type = arg_types[0 ];
1221+ return UnaryIntrinsicFunction::instantiate_functions (al, loc, scope,
1222+ " fix" , arg_type, return_type, new_args, overload_id);
1223+ }
1224+
1225+ } // namespace Fix
1226+
11851227// `X` is the name of the function in the IntrinsicScalarFunctions enum and
11861228// we use the same name for `create_X` and other places
11871229// `stdeval` is the name of the function in the `std` namespace for compile
@@ -2921,6 +2963,8 @@ namespace IntrinsicScalarFunctionRegistry {
29212963 {&LogGamma::instantiate_LogGamma, &UnaryIntrinsicFunction::verify_args}},
29222964 {static_cast <int64_t >(IntrinsicScalarFunctions::Trunc),
29232965 {&Trunc::instantiate_Trunc, &UnaryIntrinsicFunction::verify_args}},
2966+ {static_cast <int64_t >(IntrinsicScalarFunctions::Fix),
2967+ {&Fix::instantiate_Fix, &UnaryIntrinsicFunction::verify_args}},
29242968 {static_cast <int64_t >(IntrinsicScalarFunctions::Sin),
29252969 {&Sin::instantiate_Sin, &UnaryIntrinsicFunction::verify_args}},
29262970 {static_cast <int64_t >(IntrinsicScalarFunctions::Cos),
@@ -3021,6 +3065,8 @@ namespace IntrinsicScalarFunctionRegistry {
30213065
30223066 {static_cast <int64_t >(IntrinsicScalarFunctions::Trunc),
30233067 " trunc" },
3068+ {static_cast <int64_t >(IntrinsicScalarFunctions::Fix),
3069+ " fix" },
30243070 {static_cast <int64_t >(IntrinsicScalarFunctions::Sin),
30253071 " sin" },
30263072 {static_cast <int64_t >(IntrinsicScalarFunctions::Cos),
@@ -3119,6 +3165,7 @@ namespace IntrinsicScalarFunctionRegistry {
31193165 eval_intrinsic_function>>& intrinsic_function_by_name_db = {
31203166 {" log_gamma" , {&LogGamma::create_LogGamma, &LogGamma::eval_log_gamma}},
31213167 {" trunc" , {&Trunc::create_Trunc, &Trunc::eval_Trunc}},
3168+ {" fix" , {&Fix::create_Fix, &Fix::eval_Fix}},
31223169 {" sin" , {&Sin::create_Sin, &Sin::eval_Sin}},
31233170 {" cos" , {&Cos::create_Cos, &Cos::eval_Cos}},
31243171 {" tan" , {&Tan::create_Tan, &Tan::eval_Tan}},
@@ -3180,6 +3227,7 @@ namespace IntrinsicScalarFunctionRegistry {
31803227 id_ == IntrinsicScalarFunctions::Gamma ||
31813228 id_ == IntrinsicScalarFunctions::LogGamma ||
31823229 id_ == IntrinsicScalarFunctions::Trunc ||
3230+ id_ == IntrinsicScalarFunctions::Fix ||
31833231 id_ == IntrinsicScalarFunctions::Sin ||
31843232 id_ == IntrinsicScalarFunctions::Exp ||
31853233 id_ == IntrinsicScalarFunctions::Exp2 ||
0 commit comments