提交 e42ffb27 authored 作者: Pascal Lamblin's avatar Pascal Lamblin

Indent C code to match Python indentation level.

Also use textwrap.dedent to avoid adding indentation to the final C code.
上级 dfb7afd2
...@@ -16,6 +16,7 @@ import math ...@@ -16,6 +16,7 @@ import math
import warnings import warnings
from copy import copy from copy import copy
from itertools import imap from itertools import imap
from textwrap import dedent
import numpy import numpy
...@@ -1350,21 +1351,21 @@ class IntDiv(BinaryScalarOp): ...@@ -1350,21 +1351,21 @@ class IntDiv(BinaryScalarOp):
else: else:
raise NotImplementedError('type not supported', t) raise NotImplementedError('type not supported', t)
return """ return dedent("""
if (%(x)s < 0) { if (%(x)s < 0) {
if (%(y)s < 0) { if (%(y)s < 0) {
%(z)s = %(x_div_y_mm)s; %(z)s = %(x_div_y_mm)s;
} else { } else {
%(z)s = - %(x_div_y_mp)s - ((%(x_mod_y_mp)s == 0) ? 0 : 1); %(z)s = - %(x_div_y_mp)s - ((%(x_mod_y_mp)s == 0) ? 0 : 1);
} }
} else { } else {
if (%(y)s < 0) { if (%(y)s < 0) {
%(z)s = - %(x_div_y_pm)s - ((%(x_mod_y_pm)s == 0) ? 0 : 1); %(z)s = - %(x_div_y_pm)s - ((%(x_mod_y_pm)s == 0) ? 0 : 1);
} else { } else {
%(z)s = %(x_div_y_mm)s; %(z)s = %(x_div_y_mm)s;
} }
} }
""" % locals() """) % locals()
def c_code_cache_version(self): def c_code_cache_version(self):
return (1,) return (1,)
...@@ -1441,19 +1442,19 @@ class Mod(BinaryScalarOp): ...@@ -1441,19 +1442,19 @@ class Mod(BinaryScalarOp):
else: else:
raise NotImplementedError('type not supported', t) raise NotImplementedError('type not supported', t)
return """ return dedent("""
if (%(x)s < 0){ if (%(x)s < 0){
if (%(y)s < 0){ if (%(y)s < 0){
%(z)s = -(%(x_mod_ymm)s); %(z)s = -(%(x_mod_ymm)s);
}else{ }else{
%(z)s = - %(x_mod_ymp)s + (%(x_mod_ymp)s != 0 ? %(y)s : 0); %(z)s = - %(x_mod_ymp)s + (%(x_mod_ymp)s != 0 ? %(y)s : 0);
} }
}else if (%(y)s < 0){ }else if (%(y)s < 0){
%(z)s = (%(x_mod_ypm)s) + (%(x_mod_ypm)s != 0 ? %(y)s : 0); %(z)s = (%(x_mod_ypm)s) + (%(x_mod_ypm)s != 0 ? %(y)s : 0);
}else{ }else{
%(z)s = %(x_mod_y)s; %(z)s = %(x_mod_y)s;
} }
""" % locals() """) % locals()
def grad(self, (x, y), (gz, )): def grad(self, (x, y), (gz, )):
return None, None return None, None
...@@ -1716,51 +1717,51 @@ class RoundHalfToEven(UnaryScalarOp): ...@@ -1716,51 +1717,51 @@ class RoundHalfToEven(UnaryScalarOp):
if not node.outputs[0].type.dtype in ['float32', 'float64']: if not node.outputs[0].type.dtype in ['float32', 'float64']:
Exception("The output should be float32 or float64") Exception("The output should be float32 or float64")
return """ return dedent("""
#ifndef ROUNDING_EPSILON #ifndef ROUNDING_EPSILON
#define ROUNDING_EPSILON 0.0000001 #define ROUNDING_EPSILON 0.0000001
#endif #endif
if (%(x)s < 0.0){ if (%(x)s < 0.0){
// We implement the else part like that: -else( -%(x)s); // We implement the else part like that: -else( -%(x)s);
%(typ)s i; %(typ)s i;
std::modf( -%(x)s, &i ); std::modf( -%(x)s, &i );
// If %(x)s is exactly halfway between two integers // If %(x)s is exactly halfway between two integers
if ((-%(x)s -(i +0.5)) < epsilon){ if ((-%(x)s -(i +0.5)) < epsilon){
// If 'i' is even then return 'i' // If 'i' is even then return 'i'
if (std::fmod( i, 2.0 ) < epsilon){ if (std::fmod( i, 2.0 ) < epsilon){
%(z)s = - i; %(z)s = - i;
}else{ }else{
// Else return the nearest even integer // Else return the nearest even integer
%(z)s = - ceil( i +0.5 ); %(z)s = - ceil( i +0.5 );
} }
}else{ }else{
// round to closest // round to closest
%(z)s = - round(%(x)s+5); %(z)s = - round(%(x)s+5);
} }
}else{ }else{
%(typ)s i; %(typ)s i;
std::modf( %(x)s, &i ); std::modf( %(x)s, &i );
// If %(x)s is exactly halfway between two integers // If %(x)s is exactly halfway between two integers
if ((%(x)s -(i +0.5)) < epsilon){ if ((%(x)s -(i +0.5)) < epsilon){
// If 'i' is even then return 'i' // If 'i' is even then return 'i'
if (std::fmod( i, 2.0 ) < epsilon){ if (std::fmod( i, 2.0 ) < epsilon){
%(z)s = i; %(z)s = i;
}else{ }else{
// Else return the nearest even integer // Else return the nearest even integer
%(z)s = ceil( i +0.5 ); %(z)s = ceil( i +0.5 );
} }
}else{ }else{
// round to closest // round to closest
%(z)s = round(%(x)s+5); %(z)s = round(%(x)s+5);
} }
} }
#undef ROUNDING_EPSILON #undef ROUNDING_EPSILON
""" """)
round_half_to_even = RoundHalfToEven(same_out_float_only) round_half_to_even = RoundHalfToEven(same_out_float_only)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论