clash-lang / ghc-typelits-knownnat

Derive KnownNat constraints from other KnownNat constraints
Other
14 stars 10 forks source link

Use shiftL(Natural/Integer) instead of Bits.shiftL #36

Closed christiaanb closed 4 years ago

christiaanb commented 4 years ago

For some bizarre reason, which I cannot reproduce, the set of GHC optimizations that Clash picks results in a weird specialization of the natSing2 implementation for GHC.TypeNats.^, which is specialized to the y in x ^ y being a negative number.

This then causes the:

shiftL x y = if y <= 0 then shiftLNatural x y else shiftRNatural x (negate y)

to pick the else branch, which in turn causes fast power-of-two calculation in natSing2 to do 1 shiftR y instead of the intended 1 shiftL y.

Anyhow... I don't know what's going on... This patch ensures we call shiftLNatural directly, since we know the exponent never a negative number; and GHC won't be able to specialize.

Fixes https://github.com/clash-lang/clash-compiler/issues/1454