Type-Level Operations
The previous generics section described the “type-level types”, or metatypes, that Spade supports: #int, #uint, #bool, and #str and hinted at the possibility of operating on type values by writing type-level expressions called const generics, surrounded by curly braces inside type expressions, or naked in other contexts like gen if conditions. This section describes all the operations available.
The following operators are supported:
- Arithmetic, on
#ints or#uints:+,-,*,/,%,<<,>>. - Ordering comparisons, on
#ints or#uints:<,>,<=,>=. - Logical comparisons,, on
#bools:!,&&,||,^^ - Equality comparisons, on any metatype:
==,!=.
These behave just like their value-level counterparts (they have the same precedence and perform the same operations, just on metavalues with infinite range). For example, uint<16> can be written as uint<{8 + 8}>, or uint<{4 * 4}>. Const generics also allow writing types that depend on generic parameters. These allow writing types adapted to a particular requirement, for example, ensuring that addition cannot overflow by always returning a value with one bit more than its input:
Functions together with Entities together with fn and Defines a pipeline. The number in in the parentheses is input-to-output latency of the pipeline. are the basic building blocks of Spade circuits. Unlike fn, entity can contain registers and therefore have state and unlike Defines a pipeline. The number in in the parentheses is input-to-output latency of the pipeline., they do not have a statically known latency or Defines a pipeline. The number in in the parentheses is input-to-output latency of the pipeline. structure. and Defines a pipeline. The number in in the parentheses is input-to-output latency of the pipeline. are the basic building blocks of Spade circuits. Unlike entities and pipelines, functions have no internal state, they are combinational (pure). add<#uint N>(a: Type used to represent signed integers. The generic parameter (<N>) specReturns the value of the first branch if the condition is true, otherwise the second branch.
Note that unlike software languages, Spade does not have conditional execution. You do not conditionally assign values inside if expressions, you compute a value and return it.ies the number of bits, b: Type used to represent signed integers. The generic parameter (<N>) specReturns the value of the first branch if the condition is true, otherwise the second branch.
Note that unlike software languages, Spade does not have conditional execution. You do not conditionally assign values inside if expressions, you compute a value and return it.ies the number of bits) -> Type used to represent signed integers. The generic parameter (<N>) specReturns the value of the first branch if the condition is true, otherwise the second branch.
Note that unlike software languages, Spade does not have conditional execution. You do not conditionally assign values inside if expressions, you compute a value and return it.ies the number of bits {
a + b
}
Some extra function-like operations are supported:
uint::bits_for(x)returns the minimum amount of bits needed to represent as auintvalue. This value can be calculated as . Notably, it does not return , which may be desirable when calculating address widths for a certain element count. Given a collection of elements, addresses range from to , so in that case the correct expression would beuint::bits_for(N - 1).int::bits_for(x)returns the minimum amount of bits needed to represent as anintvalue including the sign bit. The exact value returned is better understood piecewise:- When , the unsigned representation of can be augmented with a sign bit, so the number of bits required for its signed integer representation is
uint::bits_for(x) + 1or . - When , the same number of bits is required to represent than to represent its positive counterpart. Taking into account the asymmetry of two’s complement representation ( maps to , not ), the number of bits required for its signed integer representation is equivalent to
int::bits_for(-x - 1), oruint::bits_for(-x - 1) + 1or .
- When , the unsigned representation of can be augmented with a sign bit, so the number of bits required for its signed integer representation is
Using Const Generics as Values
Type-level operations can also be included in expressions directly using const type { ... }. This provides a shortcut around certain language impositions, for example allowing arbitrary-precision arithmetic and division to be performed freely and unconstrained from concerns like bit widths.
const type { 300 / 7 /* spooky! but it's not actually a divider */ }