dmlc / HalideIR

Symbolic Expression and Statement Module for new DSLs
Other
205 stars 59 forks source link

incorrect constant folding for division and mod for negative integers #53

Closed derisavi closed 5 years ago

derisavi commented 5 years ago

(Pasting the bug report from https://discuss.tvm.ai/t/integer-constant-folding-for-division-and-mod/2008) My understanding is that TVM/HalideIR uses truncated division/mod, unlike Halide that uses Euclidean division/mod. Nevertheless, TVM uses div_imp (imlementation below) to do constant folding for division. Seems like this implementation is coming from Halide source code.

template<typename T>
inline T div_imp(T a, T b) {
    Type t = type_of<T>();
    if (t.is_int()) {
        int64_t q = a / b;
        int64_t r = a - q * b;
        int64_t bs = b >> (t.bits() - 1);
        int64_t rs = r >> (t.bits() - 1);
        return (T) (q - (rs & bs) + (rs & ~bs));
    } else {
        return a / b;
    }
}