clash-prelude-1.1.0: CAES Language for Synchronous Hardware - Prelude library
Copyright (C) 2013-2016 University of Twente BSD2 (see the file LICENSE) Christiaan Baaij Trustworthy Haskell2010 ScopedTypeVariablesBangPatternsViewPatternsDataKindsInstanceSigsStandaloneDerivingDeriveDataTypeableDeriveFunctorDeriveTraversableDeriveFoldableDeriveGenericDefaultSignaturesDeriveLiftDerivingStrategiesMagicHashKindSignaturesTupleSectionsTypeOperatorsExplicitNamespacesExplicitForAllBinaryLiteralsTypeApplications

Clash.Sized.Index

Description

Synopsis

Documentation

data Index (n :: Nat) Source #

Arbitrary-bounded unsigned integer represented by ceil(log_2(n)) bits.

Given an upper bound n, an Index n number has a range of: [0 .. n-1]

>>> maxBound :: Index 8
7
>>> minBound :: Index 8
0
>>> read (show (maxBound :: Index 8)) :: Index 8
7
>>> 1 + 2 :: Index 8
3
>>> 2 + 6 :: Index 8
*** Exception: X: Clash.Sized.Index: result 8 is out of bounds: [0..7]
...
>>> 1 - 3 :: Index 8
*** Exception: X: Clash.Sized.Index: result -2 is out of bounds: [0..7]
...
>>> 2 * 3 :: Index 8
6
>>> 2 * 4 :: Index 8
*** Exception: X: Clash.Sized.Index: result 8 is out of bounds: [0..7]
...

Instances

Instances details

bv2i :: KnownNat n => BitVector n -> Index (2 ^ n) Source #

An alternative implementation of unpack for the Index data type; for when you know the size of the BitVector and want to determine the size of the Index.

That is, the type of unpack is:

unpack :: BitVector (CLog 2 n) -> Index n

And is useful when you know the size of the Index, and want to get a value from a BitVector that is large enough (CLog 2 n) enough to hold an Index. Note that unpack can fail at run-time when the value inside the BitVector is higher than 'n-1'.

bv2i on the other hand will never fail at run-time, because the BitVector argument determines the size.

fromSNat :: (KnownNat m, n <= (m + 1)) => SNat n -> Index m Source #

Safely convert an SNat value to an Index