提交 e06004c8 authored 作者: Frederic Bastien's avatar Frederic Bastien

fix the c implementation of Mod for some type.

上级 11fdf483
...@@ -756,24 +756,32 @@ class Mod(BinaryScalarOp): ...@@ -756,24 +756,32 @@ class Mod(BinaryScalarOp):
We want the result to have the same sign as python, not the other implementaiton of mod. We want the result to have the same sign as python, not the other implementaiton of mod.
""" """
#raise NotImplementedError("Unlike Python, C's modulo returns negative modulo on negative dividend (to implement)") #raise NotImplementedError("Unlike Python, C's modulo returns negative modulo on negative dividend (to implement)")
t = node.inputs[0].type.upcast(*[ i.type for i in node.inputs[1:]])
if t in int_types:
x_mod_y = "%(x)s %% %(y)s"%locals()
elif t in float_types:
x_mod_y = "fmod(%(x)s,%(y)s)"%locals()
else:
raise NotImplementedError('type not supported', type)
return """ return """
if (%(x)s == 0 || %(y)s == 0) { if (%(x)s == 0 || %(y)s == 0) {
if (%(y)s == 0) %(z)s = %(x)s %% %(y)s; if (%(y)s == 0) %(z)s = %(x_mod_y)s;
%(z)s = 0; %(z)s = 0;
} }
//was #if @neg@, I suspect @neg@ to be platform dependant. //was #if @neg@, I suspect @neg@ to be platform dependant.
//should be true under X86, but could be false for other architecture! //should be true under X86, but could be false for other architecture!
#if 1 #if 1
else if ((%(x)s > 0) == (%(y)s > 0)) { else if ((%(x)s > 0) == (%(y)s > 0)) {
%(z)s = %(x)s %% %(y)s; %(z)s = %(x_mod_y)s;
} }
else { /* handled like Python does */ else { /* handled like Python does */
%(z)s = %(x)s %% %(y)s; %(z)s = %(x_mod_y)s;
if (%(z)s) %(z)s += %(y)s; if (%(z)s) %(z)s += %(y)s;
} }
#else #else
else else
%(z)s = %(x)s %% %(y)s; %(z)s = %(x_mod_y)s;
#endif #endif
"""%locals() """%locals()
def grad(self, (x, y), (gz, )): def grad(self, (x, y), (gz, )):
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论