After declaring an arbitrary precision variable, now the question is: What are the rules of assigning 1
a value to these variables? 2
This lecture will answer this question. 3
The arbitrary-precision variable initialisation is explained in the previous lecture. 4
These variables can also be assigned together using normal or compound assignment operators in C/C++ 5
language including =, +=, -= and others. 6
This example shows the normal assignment. 7
Notice that, a variable can also be assigned to another variable upon its declaration, thanks to 8
the constructors provided by arbitrary precision class templates. 9
What would happen if we assign two variables that their bit-widths are not the same? 10
There are two general cases: wider assignment and narrower assignment. Let’s consider three arbitrary 11
variables a, b, and c with 10, 17 and 8 bit-widths. If we want to assign a to b, then we have a 12
wider assignment situation. 13
What should we do with the extra bits in the b variable? If we want to assign b to c, then we have 14
a narrower assignment situation. 15
Now, what should we do again with the extra bits in the b variable? 16
The answer depends on the variables signedness. 17
Three techniques handle these situations: Sign-extension, Zero-extension, And truncation. Through the 18
following slides, we review these techniques in Vivado-HLS. 19
When assigning the value of a narrower bit-width signed variable to a wider one, the value is sign-extended 20
to the width of the destination variable, regardless of its signedness. 21
a is defined as a 7-bit signed variable initialised with the 0x5a number, so its value 22
would be -38. b is defined as a 10-bit signed variable. 23
If we assign a to b, then the value of a is copied to b, and its sign bit fills the extra bits in b. Therefore, 24
the b holds the same number, which is -38. 25
Now consider the 10-bit unsigned c variable. 26
If we assign a to c, then a is copied to c and then as a is signed the sign-extension mechanism extends 27
the a sign bit into the extra bits in c. Notice that the sign-extension is applied even if c is unsigned. 28
Explicit casting of the source variable might be necessary to ensure the expected behaviour on assignment. 29
For example, if we assign a to c with ap-uint<10> typecast, then the output is as follows. 30
As the right-hand side of the assignment is unsigned, then the size extension won’t happen, and the 31
extra-bits are filled with zero, which is called zero-extension. 32
An unsigned source variable is zero-extended before assignment. For example, if we define variable 33
a as a 7-bit unsigned variable initialised with the 0x5a hex number, then its value is 90 34
If we assign a to a 10-bit unsigned variable, then the value of the destination variable would be 90. 35
If we assign a to a 10-bit signed variable, then the value of the destination variable would also be 90. 36
Using a typecast during this assignment might change the result. 37
Assigning a wider source variable to a narrower one leads to truncation of the assigned value. All bits beyond 38
the most significant bit (MSB) position of the destination variable are lost. 39
There is no special handling of the sign information during truncation, which may lead to unexpected 40
behavior. 41
How can we print an arbitrary precision data type on the screen? The next lecture addresses this question. 42
These are our takeaway messages. There are two different cases if we assign two variables that their 43
bit-widths are not the same: wider assignment and narrower assignment. 44
There are three techniques are introduced to handle these situations which are: Sign-extension, Zero-extension 45
and truncation. 46
Now the quiz question. What are the values of all variables in this code after execution?