Skip to content

Commit

Permalink
- Fixed code generation for integer mod() (previously used doubles, w…
Browse files Browse the repository at this point in the history
…hich don't hold large enough integers on 64-bit platforms)

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@7561 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Dec 23, 2010
1 parent 85afab4 commit 3991a5d
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 24 deletions.
1 change: 1 addition & 0 deletions Compiler/BackEnd/BackendDAECreate.mo
Expand Up @@ -853,6 +853,7 @@ algorithm
case ((DAE.T_STRING(_),_)) then BackendDAE.STRING();
case ((DAE.T_ENUMERATION(names = strLst),_)) then BackendDAE.ENUMERATION(strLst);
case ((DAE.T_COMPLEX(complexClassType = ClassInf.EXTERNAL_OBJ(path)),_)) then BackendDAE.EXT_OBJECT(path);
else equation print("lowerType failed\n"); then fail();
end matchcontinue;
end lowerType;

Expand Down
6 changes: 4 additions & 2 deletions Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -2545,7 +2545,8 @@ algorithm
*/
case ((v as BackendDAE.VAR(varName = cr,flowPrefix = flowPrefix)),(vars as BackendDAE.VARIABLES(crefIdxLstArr = hashvec,strIdxLstArr = oldhashvec,varArr = varr,bucketSize = bsize,numberOfVars = n)))
equation
failure((_,_) = getVar(cr, vars)) "adding when not existing previously" ;
failure((_,_) = getVar(cr, vars));
// print("adding when not existing previously\n");
hval = HashTable2.hashFunc(cr);
indx = intMod(hval, bsize);
newpos = vararrayLength(varr);
Expand All @@ -2564,7 +2565,8 @@ algorithm

case ((newv as BackendDAE.VAR(varName = cr,flowPrefix = flowPrefix)),(vars as BackendDAE.VARIABLES(crefIdxLstArr = hashvec,strIdxLstArr = oldhashvec,varArr = varr,bucketSize = bsize,numberOfVars = n)))
equation
(_,{indx}) = getVar(cr, vars) "adding when already present => Updating value" ;
(_,{indx}) = getVar(cr, vars);
// print("adding when already present => Updating value\n");
indx_1 = indx - 1;
varr_1 = vararraySetnth(varr, indx_1, newv);
then
Expand Down
7 changes: 3 additions & 4 deletions Compiler/FrontEnd/Ceval.mo
Expand Up @@ -4075,17 +4075,16 @@ algorithm
(cache,Values.REAL(rv1),_) = ceval(cache,env, exp1, impl, st,NONE(), msg);
(cache,Values.INTEGER(ri),_) = ceval(cache,env, exp2, impl, st,NONE(), msg);
rv2 = intReal(ri);
(cache,Values.REAL(dr),_) = cevalBuiltinDiv(cache,env,{exp1,exp2},impl,st,msg);
(cache,Values.REAL(dr),_) = cevalBuiltinDiv(cache,env,{exp1,exp2},impl,st,msg);
rvd = rv1 -. rv2 *. dr;
then
(cache,Values.REAL(rvd),st);
case (cache,env,{exp1,exp2},impl,st,msg)
equation
(cache,Values.INTEGER(ri1),_) = ceval(cache,env, exp1, impl, st,NONE(), msg);
(cache,Values.INTEGER(ri2),_) = ceval(cache,env, exp2, impl, st,NONE(), msg);
(cache,Values.INTEGER(ri),_) = ceval(cache,env, exp2, impl, st,NONE(), msg);
(cache,Values.INTEGER(di),_) = cevalBuiltinDiv(cache,env,{exp1,exp2},impl,st,msg);
ri_1 = ri1 - ri2 * di;
(cache,Values.INTEGER(di),_) = cevalBuiltinDiv(cache,env,{exp1,exp2},impl,st,msg);
ri_1 = ri1 - ri2 * di;
then
(cache,Values.INTEGER(ri_1),st);
case (cache,env,{exp1,exp2},impl,st,MSG())
Expand Down
8 changes: 7 additions & 1 deletion Compiler/susan_codegen/SimCode/SimCodeC.tpl
Expand Up @@ -4977,6 +4977,12 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/,
let var2 = daeExp(e2, context, &preExp, &varDecls)
'trunc(<%var1%>/<%var2%>)'

case CALL(tuple_=false, builtin=true,
path=IDENT(name="mod"), expLst={e1,e2}) then
let var1 = daeExp(e1, context, &preExp, &varDecls)
let var2 = daeExp(e2, context, &preExp, &varDecls)
'modelica_mod_<%expTypeShort(ty)%>(<%var1%>,<%var2%>)'

case CALL(tuple_=false, builtin=true,
path=IDENT(name="max"), expLst={array}) then
let expVar = daeExp(array, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
Expand Down Expand Up @@ -5033,7 +5039,7 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/,
let var1 = daeExp(e1, context, &preExp, &varDecls)
let var2 = daeExp(e2, context, &preExp, &varDecls)
let typeStr = expTypeFromExpShort(e1)
'mod_<%typeStr%>(<%var1%>,<%var2%>)'
'modelica_rem_<%typeStr%>(<%var1%>,<%var2%>)'

case CALL(tuple_=false, builtin=true,
path=IDENT(name="String"),
Expand Down
9 changes: 0 additions & 9 deletions c_runtime/modelica.h
Expand Up @@ -110,15 +110,6 @@ typedef modelica_real mod_rettype;
* bad typedef modelica_real rem_rettype;
*/

/* div is already defined in stdlib, so it's redefined here to modelica_div so
* that it can be implemented without changing the name. Hopefully no one uses
* the stdlib div and includes this header... */
#define div modelica_div

/* fmod in math.h does not work in the same way as mod defined by modelica, so
* we need to define our own mod. */
#define mod modelica_mod

/* Special Modelica builtin functions*/
typedef modelica_real pre_rettype;
typedef modelica_real edge_rettype;
Expand Down
15 changes: 10 additions & 5 deletions c_runtime/utility.c
Expand Up @@ -55,17 +55,22 @@ modelica_real modelica_div(modelica_real x, modelica_real y)
return (modelica_real)((modelica_integer)(x/y));
}

modelica_real modelica_mod(modelica_real x, modelica_real y)
modelica_real modelica_mod_real(modelica_real x, modelica_real y)
{
return (x - floor(x/y) * y);
}

modelica_real mod_real(modelica_real x, modelica_real y)
modelica_integer modelica_mod_integer(modelica_integer x, modelica_integer y)
{
return modelica_mod(x, y);
return x % y;
}

modelica_integer mod_integer(modelica_integer x, modelica_integer y)
modelica_real modelica_rem_real(modelica_real x, modelica_real y)
{
return x % y;
return x - y*(modelica_div(x,y));
}

modelica_integer modelica_rem_integer(modelica_integer x, modelica_integer y)
{
return x - y*((x/y));
}
11 changes: 8 additions & 3 deletions c_runtime/utility.h
Expand Up @@ -43,9 +43,14 @@ int in_range_real(modelica_real i,
modelica_real start,
modelica_real stop);

/* div is already defined in stdlib, so it's redefined here to modelica_div */
modelica_real modelica_div(modelica_real x, modelica_real y);
modelica_real modelica_mod(modelica_real x, modelica_real y);
modelica_real mod_real(modelica_real x, modelica_real y);
modelica_integer mod_integer(modelica_integer x, modelica_integer y);
/* fmod in math.h does not work in the same way as mod defined by modelica, so
* we need to define our own mod. */
modelica_real modelica_mod_real(modelica_real x, modelica_real y);
modelica_integer modelica_mod_integer(modelica_integer x, modelica_integer y);

modelica_real modelica_rem_real(modelica_real x, modelica_real y);
modelica_integer modelica_rem_integer(modelica_integer x, modelica_integer y);

#endif

0 comments on commit 3991a5d

Please sign in to comment.