From 1d6099c6d897bba446be5da54b72a02ce33209f6 Mon Sep 17 00:00:00 2001 From: Max Andriychuk Date: Sat, 24 Aug 2024 22:26:24 +0200 Subject: [PATCH] Improve tests --- test/Analyses/TBR.cpp | 136 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) diff --git a/test/Analyses/TBR.cpp b/test/Analyses/TBR.cpp index 87a29f971..9ac2bdf71 100644 --- a/test/Analyses/TBR.cpp +++ b/test/Analyses/TBR.cpp @@ -11,6 +11,36 @@ double f1(double x) { return t; } // == x^3 +//CHECK: void f1_grad(double x, double *_d_x) { +//CHECK-NEXT: int _d_i = 0; +//CHECK-NEXT: int i = 0; +//CHECK-NEXT: clad::tape _t1 = {}; +//CHECK-NEXT: double _d_t = 0; +//CHECK-NEXT: double t = 1; +//CHECK-NEXT: unsigned long _t0 = {{0U|0UL}}; +//CHECK-NEXT: for (i = 0; ; i++) { +//CHECK-NEXT: { +//CHECK-NEXT: if (!(i < 3)) +//CHECK-NEXT: break; +//CHECK-NEXT: } +//CHECK-NEXT: _t0++; +//CHECK-NEXT: clad::push(_t1, t); +//CHECK-NEXT: t *= x; +//CHECK-NEXT: } +//CHECK-NEXT: _d_t += 1; +//CHECK-NEXT: for (;; _t0--) { +//CHECK-NEXT: { +//CHECK-NEXT: if (!_t0) +//CHECK-NEXT: break; +//CHECK-NEXT: } +//CHECK-NEXT: t = clad::pop(_t1); +//CHECK-NEXT: double _r_d0 = _d_t; +//CHECK-NEXT: _d_t = 0; +//CHECK-NEXT: _d_t += _r_d0 * x; +//CHECK-NEXT: *_d_x += t * _r_d0; +//CHECK-NEXT: } +//CHECK-NEXT: } + double f2(double val) { double res = 0; for (int i=1; i<5; ++i) { @@ -21,6 +51,111 @@ double f2(double val) { return res; } +//CHECK: void f2_grad(double val, double *_d_val) { +//CHECK-NEXT: int _d_i = 0; +//CHECK-NEXT: int i = 0; +//CHECK-NEXT: clad::tape _cond0 = {}; +//CHECK-NEXT: clad::tape _t1 = {}; +//CHECK-NEXT: double _d_res = 0; +//CHECK-NEXT: double res = 0; +//CHECK-NEXT: unsigned long _t0 = {{0U|0UL}}; +//CHECK-NEXT: for (i = 1; ; ++i) { +//CHECK-NEXT: { +//CHECK-NEXT: if (!(i < 5)) +//CHECK-NEXT: break; +//CHECK-NEXT: } +//CHECK-NEXT: _t0++; +//CHECK-NEXT: { +//CHECK-NEXT: clad::push(_cond0, i == 3); +//CHECK-NEXT: if (clad::back(_cond0)) { +//CHECK-NEXT: clad::push(_t1, {{1U|1UL}}); +//CHECK-NEXT: continue; +//CHECK-NEXT: } +//CHECK-NEXT: } +//CHECK-NEXT: res += i * val; +//CHECK-NEXT: clad::push(_t1, {{2U|2UL}}); +//CHECK-NEXT: } +//CHECK-NEXT: _d_res += 1; +//CHECK-NEXT: for (;; _t0--) { +//CHECK-NEXT: { +//CHECK-NEXT: if (!_t0) +//CHECK-NEXT: break; +//CHECK-NEXT: } +//CHECK-NEXT: switch (clad::pop(_t1)) { +//CHECK-NEXT: case {{2U|2UL}}: +//CHECK-NEXT: ; +//CHECK-NEXT: --i; +//CHECK-NEXT: { +//CHECK-NEXT: double _r_d0 = _d_res; +//CHECK-NEXT: _d_i += _r_d0 * val; +//CHECK-NEXT: *_d_val += i * _r_d0; +//CHECK-NEXT: } +//CHECK-NEXT: { +//CHECK-NEXT: if (clad::back(_cond0)) +//CHECK-NEXT: case {{1U|1UL}}: +//CHECK-NEXT: ; +//CHECK-NEXT: clad::pop(_cond0); +//CHECK-NEXT: } +//CHECK-NEXT: } +//CHECK-NEXT: } +//CHECK-NEXT: } + +double f3 (double x){ + double i = 1; + double j = 0; + double res = 0; + res += i*x; + if(x) + res += i*x; + else if(i) + j++; + else if(j) + i++; + return res; +} + +//CHECK: void f3_grad(double x, double *_d_x) { +//CHECK-NEXT: bool _cond0; +//CHECK-NEXT: bool _cond1; +//CHECK-NEXT: bool _cond2; +//CHECK-NEXT: double _d_i = 0; +//CHECK-NEXT: double i = 1; +//CHECK-NEXT: double _d_j = 0; +//CHECK-NEXT: double j = 0; +//CHECK-NEXT: double _d_res = 0; +//CHECK-NEXT: double res = 0; +//CHECK-NEXT: res += i * x; +//CHECK-NEXT: { +//CHECK-NEXT: _cond0 = x; +//CHECK-NEXT: if (_cond0) +//CHECK-NEXT: res += i * x; +//CHECK-NEXT: else { +//CHECK-NEXT: _cond1 = i; +//CHECK-NEXT: if (_cond1) +//CHECK-NEXT: j++; +//CHECK-NEXT: else { +//CHECK-NEXT: _cond2 = j; +//CHECK-NEXT: if (_cond2) +//CHECK-NEXT: i++; +//CHECK-NEXT: } +//CHECK-NEXT: } +//CHECK-NEXT: } +//CHECK-NEXT: _d_res += 1; +//CHECK-NEXT: if (_cond0) { +//CHECK-NEXT: double _r_d1 = _d_res; +//CHECK-NEXT: _d_i += _r_d1 * x; +//CHECK-NEXT: *_d_x += i * _r_d1; +//CHECK-NEXT: } else if (!_cond1) +//CHECK-NEXT: if (_cond2) +//CHECK-NEXT: i--; +//CHECK-NEXT: { +//CHECK-NEXT: double _r_d0 = _d_res; +//CHECK-NEXT: _d_i += _r_d0 * x; +//CHECK-NEXT: *_d_x += i * _r_d0; +//CHECK-NEXT: } +//CHECK-NEXT: } + + #define TEST(F, x) { \ result[0] = 0; \ auto F##grad = clad::gradient(F);\ @@ -32,4 +167,5 @@ int main() { double result[3] = {}; TEST(f1, 3); // CHECK-EXEC: {27.00} TEST(f2, 3); // CHECK-EXEC: {9.00} + TEST(f3, 3); // CHECK-EXEC: {2.00} }