Skip to content

Commit

Permalink
Merge pull request #50 from czgdp1807/lc_30
Browse files Browse the repository at this point in the history
Ported ``integration_tests/arrays_elemental_15.f90`` from LFortran and improve LC to compile it
  • Loading branch information
czgdp1807 authored Jan 5, 2024
2 parents 4d42957 + 73862fc commit 125b287
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 2 deletions.
2 changes: 2 additions & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,5 @@ RUN(NAME array_17.cpp LABELS gcc llvm NOFAST
EXTRA_ARGS --extra-arg=-I${CONDA_PREFIX}/include)
RUN(NAME array_18.cpp LABELS gcc llvm NOFAST
EXTRA_ARGS --extra-arg=-I${CONDA_PREFIX}/include)
RUN(NAME array_19.cpp LABELS gcc llvm NOFAST
EXTRA_ARGS --extra-arg=-I${CONDA_PREFIX}/include)
139 changes: 139 additions & 0 deletions integration_tests/array_19.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#include <iostream>
#include <cmath>
#include <xtensor/xtensor.hpp>
#include <xtensor/xfixed.hpp>
#include "xtensor/xio.hpp"

void verify1d(const xt::xtensor<double, 1>& array,
const xt::xtensor<double, 1>& result,
const int size) {
int i;
double eps;

eps = 1e-12;

for( i = 0; i < size; i++ ) {
if (abs(sin(sin(array(i))) - result(i)) > eps) {
exit(2);
}
}
}

void verifynd(const xt::xtensor<double, 3>& array,
const xt::xtensor<double, 3>& result,
const int size1, const int size2, const int size3) {
int i, j, k;
double eps;

eps = 1e-12;

for( i = 0; i < size1; i++ ) {
for( j = 0; j < size2; j++ ) {
for( k = 0; k < size3; k++ ) {
if (abs(pow(sin(array(i, j, k)), 2.0) - result(i, j, k)) > eps) {
exit(2);
}
}
}
}
}

void verify2d(const xt::xtensor<double, 2>& array,
const xt::xtensor<double, 2>& result,
const int size1, const int size2) {
int i, j;
double eps;
eps = 1e-12;

for( i = 0; i < size1; i++ ) {
for( j = 0; j < size2; j++ ) {
if (abs(pow(cos(array(i, j)), 2.0) - result(i, j)) > eps) {
exit(2);
}
}
}
}

void elemental_sin() {
int i, j, k;
xt::xtensor<double, 1> array1d = xt::empty<double>({256});
xt::xtensor<double, 1> sin1d = xt::empty<double>({256});
xt::xtensor<double, 3> arraynd = xt::empty<double>({256, 64, 16});
xt::xtensor<double, 3> sinnd = xt::empty<double>({256, 64, 16});

for( i = 0; i < 256; i++ ) {
array1d(i) = i + 1;
}

sin1d = sin(sin(array1d));

verify1d(array1d, sin1d, 256);

for( i = 0; i < 256; i++ ) {
for( j = 0; j < 64; j++ ) {
for( k = 0; k < 16; k++ ) {
arraynd(i, j, k) = i + j + k + 3;
}
}
}

sinnd = xt::pow(sin(arraynd), 2.0);

verifynd(arraynd, sinnd, 256, 64, 16);
}

void elemental_cos() {
int i, j;
xt::xtensor<double, 2> array2d = xt::empty<double>({256, 64});
xt::xtensor<double, 2> cos2d = xt::empty<double>({256, 64});

for( i = 0; i < 256; i++ ) {
for( j = 0; j < 64; j++ ) {
array2d(i, j) = i + j;
}
}

cos2d = xt::pow(cos(array2d), 2.0);

verify2d(array2d, cos2d, 256, 64);
}

void elemental_trig_identity() {
int i, j, k, l;
double eps;
xt::xtensor<double, 4> arraynd = xt::empty<double>({64, 32, 8, 4});
xt::xtensor<double, 4> observed = xt::empty<double>({64, 32, 8, 4});
eps = 1e-12;

for( i = 0; i < 64; i++ ) {
for( j = 0; j < 32; j++ ) {
for( k = 0; k < 8; k++ ) {
for( l = 0; l < 4; l++ ) {
arraynd(i, j, k, l) = i + j + k + l;
}
}
}
}

observed = xt::pow(sin(arraynd), 2.0) + xt::pow(cos(arraynd), 2.0);

for( i = 0; i < 64; i++ ) {
for( j = 0; j < 32; j++ ) {
for( k = 0; k < 8; k++ ) {
for( l = 0; l < 4; l++ ) {
if( abs(observed(i, j, k, l) - 1.0) > eps ) {
exit(2);
}
}
}
}
}
}

int main() {

elemental_sin();
elemental_cos();
elemental_trig_identity();

}
15 changes: 14 additions & 1 deletion src/lc/clang_ast_to_asr.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ enum SpecialFunc {
Equal,
Exp,
Abs,
Sin,
Cos,
AMax,
Sum,
Range,
Expand All @@ -65,6 +67,8 @@ std::map<std::string, SpecialFunc> special_function_map = {
{"equal", SpecialFunc::Equal},
{"exp", SpecialFunc::Exp},
{"abs", SpecialFunc::Abs},
{"sin", SpecialFunc::Sin},
{"cos", SpecialFunc::Cos},
{"amax", SpecialFunc::AMax},
{"sum", SpecialFunc::Sum},
{"range", SpecialFunc::Range},
Expand Down Expand Up @@ -923,6 +927,14 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
tmp = ASRUtils::make_IntrinsicScalarFunction_t_util(al, Lloc(x),
static_cast<int64_t>(ASRUtils::IntrinsicScalarFunctions::Abs),
args.p, args.size(), 0, ASRUtils::expr_type(args.p[0]), nullptr);
} else if( sf == SpecialFunc::Sin ) {
tmp = ASRUtils::make_IntrinsicScalarFunction_t_util(al, Lloc(x),
static_cast<int64_t>(ASRUtils::IntrinsicScalarFunctions::Sin),
args.p, args.size(), 0, ASRUtils::expr_type(args.p[0]), nullptr);
} else if( sf == SpecialFunc::Cos ) {
tmp = ASRUtils::make_IntrinsicScalarFunction_t_util(al, Lloc(x),
static_cast<int64_t>(ASRUtils::IntrinsicScalarFunctions::Cos),
args.p, args.size(), 0, ASRUtils::expr_type(args.p[0]), nullptr);
} else if( sf == SpecialFunc::AMax ) {
if( args.size() > 1 && args.p[1] != nullptr ) {
throw std::runtime_error("dim argument not yet supported with " + func_name);
Expand Down Expand Up @@ -1483,7 +1495,8 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
name == "operator-" || name == "operator/" || name == "operator>" ||
name == "range" || name == "pow" || name == "equal" ||
name == "operator<" || name == "operator<=" || name == "operator>=" ||
name == "operator!=" || name == "operator\"\"i" ) {
name == "operator!=" || name == "operator\"\"i" || name == "sin" ||
name == "cos" ) {
if( sym != nullptr && ASR::is_a<ASR::Function_t>(
*ASRUtils::symbol_get_past_external(sym)) ) {
throw std::runtime_error("Special function " + name + " cannot be overshadowed yet.");
Expand Down
2 changes: 1 addition & 1 deletion src/libasr/pass/intrinsic_function_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ static inline ASR::expr_t* instantiate_functions(Allocator &al,
return b.Call(s, new_args, expr_type(f->m_return_var));
}
fill_func_arg("x", arg_type);
auto result = declare(new_name, return_type, ReturnVar);
auto result = declare(new_name, ASRUtils::extract_type(return_type), ReturnVar);

{
SymbolTable *fn_symtab_1 = al.make_new<SymbolTable>(fn_symtab);
Expand Down

0 comments on commit 125b287

Please sign in to comment.