Skip to content

Commit 3400303

Browse files
committed
Add BitPack instance for Index 0
1 parent d0f65c4 commit 3400303

File tree

18 files changed

+145
-110
lines changed

18 files changed

+145
-110
lines changed

.ci/stack-9.10.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@ packages:
1212
- tests
1313

1414
extra-deps:
15+
- ghc-tcplugin-api-0.18.1.0@sha256:289d8d3b6ed733a933aa518d483dd5fbbe96420e39da9a58862757a6faa476ca
16+
- ghc-typelits-extra-0.5.2@sha256:0d04483a660be3987213418316d2bd9c1a3dcb4227df0a0f80c32e98b87f76fe
17+
- ghc-typelits-knownnat-0.8.2@sha256:6e9ef33bd2cf462e7d66f5b28608c0156007947b3a7a95fd5b90bfa4a0365d1e
18+
- ghc-typelits-natnormalise-0.9.3@sha256:aa48d0755cd246643155704b0082ce8d0bf421f445bf568289d1a8c791257743
1519
- tasty-flaky-0.1.2.0@sha256:2f91ab9f55ae0c472474087f98bb54076aca18f8b058343479d24597a3aa181b,1858

.ci/stack-9.6.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ packages:
1212
- tests
1313

1414
extra-deps:
15+
- ghc-tcplugin-api-0.18.1.0@sha256:289d8d3b6ed733a933aa518d483dd5fbbe96420e39da9a58862757a6faa476ca
16+
- ghc-typelits-extra-0.5.2@sha256:0d04483a660be3987213418316d2bd9c1a3dcb4227df0a0f80c32e98b87f76fe
17+
- ghc-typelits-knownnat-0.8.2@sha256:6e9ef33bd2cf462e7d66f5b28608c0156007947b3a7a95fd5b90bfa4a0365d1e
18+
- ghc-typelits-natnormalise-0.9.3@sha256:aa48d0755cd246643155704b0082ce8d0bf421f445bf568289d1a8c791257743
1519
- hedgehog-fakedata-0.0.1.5@sha256:955223750b74b0097ce426c07409c4629d22fb6cbd3b6bd0e9aeedf991241bad,1424
1620
- tasty-1.5.3@sha256:9d56ea9dbc274fc853fc531373b2c91bfe360e21460c2c6a5838897d86e3f6d0,2923
1721
- tasty-flaky-0.1.2.0@sha256:2f91ab9f55ae0c472474087f98bb54076aca18f8b058343479d24597a3aa181b,1858

.ci/stack-9.8.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ packages:
1313

1414
extra-deps:
1515
- fakedata-1.0.5@sha256:c44c037382a296f3a92991479fdc9522c31935322a67641b8bf107872a425d21,24593
16+
- ghc-tcplugin-api-0.18.1.0@sha256:289d8d3b6ed733a933aa518d483dd5fbbe96420e39da9a58862757a6faa476ca
17+
- ghc-typelits-extra-0.5.2@sha256:0d04483a660be3987213418316d2bd9c1a3dcb4227df0a0f80c32e98b87f76fe
18+
- ghc-typelits-knownnat-0.8.2@sha256:6e9ef33bd2cf462e7d66f5b28608c0156007947b3a7a95fd5b90bfa4a0365d1e
19+
- ghc-typelits-natnormalise-0.9.3@sha256:aa48d0755cd246643155704b0082ce8d0bf421f445bf568289d1a8c791257743
1620
- hedgehog-fakedata-0.0.1.5@sha256:955223750b74b0097ce426c07409c4629d22fb6cbd3b6bd0e9aeedf991241bad,1424
1721
- string-random-0.1.4.4@sha256:5ea686e58354e89a9f236f795b9b161c8730a61b56194281639b97742d53ba48,2144
1822
- tasty-flaky-0.1.2.0@sha256:2f91ab9f55ae0c472474087f98bb54076aca18f8b058343479d24597a3aa181b,1858
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ADDED: `Index 0` instances for `BitPack`, `Bits`, `Counter`, `FiniteBits`, `MaybeNumConvert`, `NumConvert`, `Partiy`, and `SaturatingNum`.
2+
ADDED: Zero-sized block RAM implementations for `blockRam1` and `blockRamU`.

clash-lib/prims/commonverilog/Clash_Sized_Internal_Index.primitives.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
- BlackBox:
22
name: Clash.Sized.Internal.Index.pack#
33
kind: Expression
4-
type: 'pack# :: Index
5-
n -> BitVector (CLog 2 n)'
4+
type: |-
5+
pack# :: Index n -> BitVector (CLogWZ 2 n 0)
66
template: ~ARG[0]
77
workInfo: Never
88
- BlackBox:
99
name: Clash.Sized.Internal.Index.unpack#
1010
kind: Expression
11-
type: 'unpack# :: (KnownNat
12-
n, 1 <= n) => BitVector (CLog 2 n) -> Index n'
13-
template: ~ARG[2]
11+
type: |-
12+
unpack# :: KnownNat n => BitVector (CLogWZ 2 n 0) -> Index n
13+
template: ~ARG[1]
1414
workInfo: Never
1515
- BlackBox:
1616
name: Clash.Sized.Internal.Index.eq#

clash-lib/prims/vhdl/Clash_Sized_Internal_Index.primitives.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
- BlackBox:
22
name: Clash.Sized.Internal.Index.pack#
33
kind: Expression
4-
type: 'pack# :: Index
5-
n -> BitVector (CLog 2 n)'
4+
type: |-
5+
pack# :: Index n -> BitVector (CLogWZ 2 n 0)
66
template: std_logic_vector(~ARG[0])
77
workInfo: Never
88
- BlackBox:
99
name: Clash.Sized.Internal.Index.unpack#
1010
kind: Expression
11-
type: 'unpack# :: (KnownNat
12-
n, 1 <= n) => BitVector (CLog 2 n) -> Index n'
13-
template: unsigned(~ARG[2])
11+
type: |-
12+
unpack# :: KnownNat n => BitVector (CLogWZ 2 n 0) -> Index n
13+
template: unsigned(~ARG[1])
1414
workInfo: Never
1515
- BlackBox:
1616
name: Clash.Sized.Internal.Index.eq#

clash-prelude/clash-prelude.cabal

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Maintainer: QBayLogic B.V. <devops@qbaylogic.com>
5252
Copyright: Copyright © 2013-2016, University of Twente,
5353
2016-2017, Myrtle Software Ltd,
5454
2017-2019, QBayLogic B.V., Google Inc.,
55-
2021-2025, QBayLogic B.V.
55+
2021-2026, QBayLogic B.V.
5656
Category: Hardware
5757
Build-type: Simple
5858

@@ -331,9 +331,9 @@ Library
331331
extra >= 1.6.17 && < 1.9,
332332
ghc-bignum >= 1.0 && < 1.4,
333333
ghc-prim >= 0.5.1.0 && < 0.13,
334-
ghc-typelits-extra >= 0.4 && < 0.6,
335-
ghc-typelits-knownnat >= 0.7.2 && < 0.9,
336-
ghc-typelits-natnormalise >= 0.7.2 && < 0.10,
334+
ghc-typelits-extra >= 0.5.2 && < 0.6,
335+
ghc-typelits-knownnat >= 0.8.2 && < 0.9,
336+
ghc-typelits-natnormalise >= 0.9.3 && < 0.10,
337337
hashable >= 1.2.1.0 && < 1.6,
338338
half >= 0.2.2.3 && < 1.0,
339339
infinite-list ^>= 0.1,

clash-prelude/src/Clash/Class/Counter/Internal.hs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
{- |
2+
Copyright : (C) 2021-2026, QBayLogic B.V.
3+
License : BSD2 (see the file LICENSE)
4+
Maintainer : QBayLogic B.V. <devops@qbaylogic.com>
5+
-}
6+
17
{-# LANGUAGE FlexibleInstances #-}
28
{-# LANGUAGE GeneralisedNewtypeDeriving #-}
39
{-# LANGUAGE LambdaCase #-}
@@ -20,7 +26,7 @@ import Data.Bifunctor (bimap)
2026
import Data.Functor.Identity (Identity(..))
2127
import Data.Int (Int8, Int16, Int32, Int64)
2228
import Data.Word (Word8, Word16, Word32, Word64)
23-
import GHC.TypeLits (KnownNat, type (<=))
29+
import GHC.TypeLits (KnownNat)
2430

2531
-- $setup
2632
-- >>> :m -Prelude
@@ -79,7 +85,7 @@ class Counter a where
7985
| a == minBound = (True, countMax)
8086
| otherwise = (False, pred a)
8187

82-
instance (1 <= n, KnownNat n) => Counter (Index n)
88+
instance KnownNat n => Counter (Index n)
8389
instance KnownNat n => Counter (Unsigned n)
8490
instance KnownNat n => Counter (Signed n)
8591
instance KnownNat n => Counter (BitVector n)
@@ -214,7 +220,7 @@ rippleR f = mapAccumR step True
214220
-- 1 :> 0 :> Nil
215221
-- >>> iterate (SNat @5) (countSucc @T) (9 :> 8 :> Nil)
216222
-- (9 :> 8 :> Nil) :> (9 :> 9 :> Nil) :> (0 :> 0 :> Nil) :> (0 :> 1 :> Nil) :> (0 :> 2 :> Nil) :> Nil
217-
instance (Counter a, KnownNat n, 1 <= n) => Counter (Vec n a) where
223+
instance (Counter a, KnownNat n) => Counter (Vec n a) where
218224
countMin = Vec.repeat countMin
219225
countMax = Vec.repeat countMax
220226

clash-prelude/src/Clash/Class/NumConvert/Internal/MaybeNumConvert.hs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
{- |
1212
Copyright : (C) 2025 , Martijn Bastiaan
13+
2025-2026, QBayLogic B.V.
1314
License : BSD2 (see the file LICENSE)
1415
Maintainer : QBayLogic B.V. <devops@qbaylogic.com>
1516
-}
@@ -22,8 +23,8 @@ import Clash.Sized.Index
2223
import Clash.Sized.Signed
2324
import Clash.Sized.Unsigned
2425

25-
import GHC.TypeLits (KnownNat, type (+), type (<=), type (^))
26-
import GHC.TypeLits.Extra (CLog)
26+
import GHC.TypeLits (KnownNat, type (+), type (^))
27+
import GHC.TypeLits.Extra (CLogWZ)
2728

2829
import Data.Int (Int16, Int32, Int64, Int8)
2930
import Data.Word (Word16, Word32, Word64, Word8)
@@ -82,13 +83,13 @@ class MaybeNumConvert a b where
8283
instance (KnownNat n, KnownNat m) => MaybeNumConvert (Index n) (Index m) where
8384
maybeNumConvert !a = maybeResize a
8485

85-
instance (KnownNat n, KnownNat m, 1 <= n) => MaybeNumConvert (Index n) (Unsigned m) where
86-
maybeNumConvert !a = maybeResize $ bitCoerce @_ @(Unsigned (CLog 2 n)) a
86+
instance (KnownNat n, KnownNat m) => MaybeNumConvert (Index n) (Unsigned m) where
87+
maybeNumConvert !a = maybeResize $ bitCoerce @_ @(Unsigned (CLogWZ 2 n 0)) a
8788

88-
instance (KnownNat n, KnownNat m, 1 <= n) => MaybeNumConvert (Index n) (Signed m) where
89-
maybeNumConvert !a = maybeNumConvert $ bitCoerce @_ @(Unsigned (CLog 2 n)) a
89+
instance (KnownNat n, KnownNat m) => MaybeNumConvert (Index n) (Signed m) where
90+
maybeNumConvert !a = maybeNumConvert $ bitCoerce @_ @(Unsigned (CLogWZ 2 n 0)) a
9091

91-
instance (KnownNat n, KnownNat m, 1 <= n) => MaybeNumConvert (Index n) (BitVector m) where
92+
instance (KnownNat n, KnownNat m) => MaybeNumConvert (Index n) (BitVector m) where
9293
maybeNumConvert !a = maybeResize $ pack a
9394

9495
instance (KnownNat n, KnownNat m) => MaybeNumConvert (Unsigned n) (Index m) where

clash-prelude/src/Clash/Class/NumConvert/Internal/NumConvert.hs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
{- |
1313
Copyright : (C) 2025 , Martijn Bastiaan
14+
2025-2026, QBayLogic B.V.
1415
License : BSD2 (see the file LICENSE)
1516
Maintainer : QBayLogic B.V. <devops@qbaylogic.com>
1617
-}
@@ -26,7 +27,7 @@ import Clash.Sized.Signed
2627
import Clash.Sized.Unsigned
2728

2829
import GHC.TypeLits (KnownNat, type (+), type (<=), type (^))
29-
import GHC.TypeLits.Extra (CLog)
30+
import GHC.TypeLits.Extra (CLogWZ)
3031

3132
import Data.Int (Int16, Int32, Int64, Int8)
3233
import Data.Word (Word16, Word32, Word64, Word8)
@@ -83,19 +84,19 @@ class NumConvert a b where
8384
instance (KnownNat n, KnownNat m, n <= m) => NumConvert (Index n) (Index m) where
8485
numConvert = resize
8586

86-
instance (KnownNat n, KnownNat m, 1 <= n, n <= 2 ^ m) => NumConvert (Index n) (Unsigned m) where
87+
instance (KnownNat n, KnownNat m, n <= 2 ^ m) => NumConvert (Index n) (Unsigned m) where
8788
numConvert !a = resize $ bitCoerce a
8889

8990
{- | Note: Conversion from @Index 1@ to @Signed 0@ is lossless, but not within the
9091
constraints of the instance.
9192
-}
92-
instance (KnownNat n, KnownNat m, 1 <= n, CLog 2 n + 1 <= m) => NumConvert (Index n) (Signed m) where
93-
numConvert !a = numConvert $ bitCoerce @_ @(Unsigned (CLog 2 n)) a
93+
instance (KnownNat n, KnownNat m, CLogWZ 2 n 0 + 1 <= m) => NumConvert (Index n) (Signed m) where
94+
numConvert !a = numConvert $ bitCoerce @_ @(Unsigned (CLogWZ 2 n 0)) a
9495

95-
instance (KnownNat n, KnownNat m, 1 <= n, n <= 2 ^ m) => NumConvert (Index n) (BitVector m) where
96+
instance (KnownNat n, KnownNat m, n <= 2 ^ m) => NumConvert (Index n) (BitVector m) where
9697
numConvert !a = resize $ pack a
9798

98-
instance (KnownNat n, KnownNat m, 1 <= m, 2 ^ n <= m) => NumConvert (Unsigned n) (Index m) where
99+
instance (KnownNat n, KnownNat m, 2 ^ n <= m) => NumConvert (Unsigned n) (Index m) where
99100
numConvert !a = bitCoerce $ resize a
100101

101102
instance (KnownNat n, KnownNat m, n <= m) => NumConvert (Unsigned n) (Unsigned m) where
@@ -113,7 +114,7 @@ instance (KnownNat n, KnownNat m, n <= m) => NumConvert (Unsigned n) (BitVector
113114
instance (KnownNat n, KnownNat m, n <= m) => NumConvert (Signed n) (Signed m) where
114115
numConvert !a = resize a
115116

116-
instance (KnownNat n, KnownNat m, 1 <= m, 2 ^ n <= m) => NumConvert (BitVector n) (Index m) where
117+
instance (KnownNat n, KnownNat m, 2 ^ n <= m) => NumConvert (BitVector n) (Index m) where
117118
numConvert = unpack . resize
118119

119120
instance (KnownNat n, KnownNat m, n <= m) => NumConvert (BitVector n) (Unsigned m) where

0 commit comments

Comments
 (0)