Skip to content

Vivado's mod misbheaves on negative dividend #2262

Open
@martijnbastiaan

Description

@martijnbastiaan
import Clash.Prelude
import Clash.Explicit.Testbench
import Data.Int (Int8)

topEntity :: (Int8, Int8) -> Int8
topEntity = uncurry mod
{-# NOINLINE topEntity #-}

testBench :: Signal System Bool
testBench = done
  where
    testInput      = stimuliGenerator clk rst ((-1, 3) :> Nil)
    expectedOutput = outputVerifier' clk rst (2 :> Nil)
    done           = expectedOutput (topEntity <$> testInput)
    clk            = tbSystemClockGen (not <$> done)
    rst            = systemResetGen

This fails; Vivado thinks -1 % 3 = 1. The VHDL spec is unambiguous about it:

The result of the modulus operation is such that (A mod B) has the sign of B and an absolute value less than the absolute value of B; in addition, for some integer value N, this result must satisfy the relation:

A = B ∗ N + (A mod B)

This only works if N ~ -1 which in turn implies -1 % 3 = 2.

This issue has been discovered as part of #2257.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions