From 75ef6fcef730d3741a50ff0dddf9d4b194f48d7e Mon Sep 17 00:00:00 2001
From: Ferhat Mihoubi <ferhat.mihoubi@univ-evry.fr>
Date: Fri, 9 Apr 2010 11:34:39 +0200
Subject: [PATCH] Implement normcdf() in bytecode

---
 mex/sources/bytecode/Interpreter.cc | 22 +++++++++++++++++++++-
 preprocessor/CodeInterpreter.hh     | 24 ++++++++++++++++++++++++
 preprocessor/ExprNode.cc            |  4 ++--
 3 files changed, 47 insertions(+), 3 deletions(-)

diff --git a/mex/sources/bytecode/Interpreter.cc b/mex/sources/bytecode/Interpreter.cc
index 9b13454a4c..c979828b1e 100644
--- a/mex/sources/bytecode/Interpreter.cc
+++ b/mex/sources/bytecode/Interpreter.cc
@@ -96,7 +96,7 @@ Interpreter::compute_block_time(int Per_u_, bool evaluate, int block_num)
 {
   int var, lag = 0, op;
   ostringstream tmp_out;
-  double v1, v2;
+  double v1, v2, v3;
   bool go_on = true;
   double ll;
   while (go_on)
@@ -635,6 +635,26 @@ Interpreter::compute_block_time(int Per_u_, bool evaluate, int block_num)
               Stack.push(sqrt(v1));
 #ifdef DEBUG
               tmp_out << " |sqrt(" << v1 << ")|";
+#endif
+              break;
+            default:
+              ;
+            }
+          break;
+        case FTRINARY:
+          op = ((FTRINARY_ *) it_code->second)->get_op_type();
+          v3 = Stack.top();
+          Stack.pop();
+          v2 = Stack.top();
+          Stack.pop();
+          v1 = Stack.top();
+          Stack.pop();
+          switch (op)
+            {
+            case oNormcdf:
+              Stack.push(0.5*(1+erf((v1-v2)/v3/M_SQRT2)));
+#ifdef DEBUG
+              tmp_out << " |normcdf(" << v1 << ", " << v2 << ", " << v2  << ")|";
 #endif
               break;
             default:
diff --git a/preprocessor/CodeInterpreter.hh b/preprocessor/CodeInterpreter.hh
index 90774f6a69..cc201ee0ca 100644
--- a/preprocessor/CodeInterpreter.hh
+++ b/preprocessor/CodeInterpreter.hh
@@ -85,6 +85,7 @@ enum Tags
 
     FUNARY,       //!< A Unary operator - 14
     FBINARY,      //!< A binary operator - 15
+    FTRINARY,     //!< A trinary operator -15'
 
     FCUML,        //!< Cumulates the result - 16
 
@@ -565,6 +566,22 @@ public:
   };
 };
 
+class FTRINARY_ : public TagWithOneArgument<uint8_t>
+{
+public:
+  inline FTRINARY_() : TagWithOneArgument<uint8_t>::TagWithOneArgument(FTRINARY)
+  {
+  };
+  inline FTRINARY_(const int op_type_arg) : TagWithOneArgument<uint8_t>::TagWithOneArgument(FTRINARY, op_type_arg)
+  {
+  };
+  inline uint8_t
+  get_op_type()
+  {
+    return arg1;
+  };
+};
+
 class FOK_ : public TagWithOneArgument<int>
 {
 public:
@@ -1010,6 +1027,13 @@ public:
             tags_liste.push_back(make_pair(FBINARY, code));
             code += sizeof(FBINARY_);
             break;
+          case FTRINARY:
+# ifdef DEBUGL
+            mexPrintf("FTRINARY\n");
+# endif
+            tags_liste.push_back(make_pair(FTRINARY, code));
+            code += sizeof(FTRINARY_);
+            break;
           case FOK:
 # ifdef DEBUGL
             mexPrintf("FOK\n");
diff --git a/preprocessor/ExprNode.cc b/preprocessor/ExprNode.cc
index f8445b519e..c224cd56ac 100644
--- a/preprocessor/ExprNode.cc
+++ b/preprocessor/ExprNode.cc
@@ -2954,8 +2954,8 @@ TrinaryOpNode::compile(ostream &CompileCode, bool lhs_rhs, const temporary_terms
   arg1->compile(CompileCode, lhs_rhs, temporary_terms, map_idx, dynamic, steady_dynamic);
   arg2->compile(CompileCode, lhs_rhs, temporary_terms, map_idx, dynamic, steady_dynamic);
   arg3->compile(CompileCode, lhs_rhs, temporary_terms, map_idx, dynamic, steady_dynamic);
-  FBINARY_ fbinary(op_code);
-  fbinary.write(CompileCode);
+  FTRINARY_ ftrinary(op_code);
+  ftrinary.write(CompileCode);
 }
 
 void
-- 
GitLab