# Long integer translation

**URL:** <https://discourse.myhdl.org/t/long-integer-translation/483>\
**Category:** Uncategorized\
**Created:** [December 14, 2021, 1:35pm UTC](https://discourse.myhdl.org/t/long-integer-translation/483 "2021-12-14T13:35:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Albert](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@Albert](https://discourse.myhdl.org/u/Albert)\
**Post date:** [December 14, 2021, 1:35pm UTC](https://discourse.myhdl.org/t/long-integer-translation/483/1 "2021-12-14T13:35:34Z")

</div>

## I have a problem with a conversion of intbv to (un)signed. I use the following code:

multiply\_reg = intbv(val=0, min=-2^42, max=2^42

@always\_seq (ip\_clk.posedge, reset=ip\_rst)  
def multiply():

```
multiply_reg = 0

multiply_reg = internal_data * factor

```

* * *

Internal data is 16 bits width and factor 24 bits width

When I convert this to VHDL I get:

# 

MODEL\_MULTIPLY: process (ip\_clk, ip\_rst) is  
variable multiply\_reg: integer;

# 

Here suddenly multiply\_reg is only 32 bits, but I need 42 bits. It should not be an integer but signed width a length of 42 bits.

Did I make a mistake or is the conversion wrong?

Best Regards,  
Albert

---

<div class="post-metadata">

**Author:** ![Albert](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@Albert](https://discourse.myhdl.org/u/Albert)\
**Post date:** [December 14, 2021, 1:45pm UTC](https://discourse.myhdl.org/t/long-integer-translation/483/2 "2021-12-14T13:45:33Z")

</div>

If I use this construction in the \*.py files, conversion seems correct

multiply\_reg = intbv(internal\_data \* factor, min=-2^42, max=2^42)

VHDL looks like: variable multiply\_reg: signed(42 downto 0);

But the VHDL looks like this:

multiply\_reg := to\_signed((internal\_data \* signed(resize(factor, 25))), 43);

Which give a compile error;  
No feasible entries for subprogram “to\_signed”  
Integer \* signed =\> signed, so the “to\_signed” is not needed.

Does anyone have a solution for this?

---

<div class="post-metadata">

**Author:** ![josyb](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.myhdl.org/josyb/32/5_2.png) [@josyb](https://discourse.myhdl.org/u/josyb)\
**Post date:** [December 14, 2021, 4:01pm UTC](https://discourse.myhdl.org/t/long-integer-translation/483/3 "2021-12-14T16:01:21Z")

</div>

Hi Albert,

> Here suddenly multiply\_reg is only 32 bits,

You need to update an `intbv()` variable using the `[:]` notation to explicitly set the value held by that `intbv()`; in the other case (as you did) MyHDL considers it to be an integer.

```python
multiply_reg = intbv(val=0, min=-2 **40, max=2** 40

@always_seq (ip_clk.posedge, reset=ip_rst)
def multiply():
     multiply_reg[:] = internal_data * factor

```

IMO it is always best to make the result of any arithmetic operation fit: in your case you multiply 16 bits by 25 which only produces a 41-bit result. If you subsequently assign that result to an object with a wider bitwidth MyHDL will then correctly extend the value.  
I assume that at least one of the two operands is signed.  
You may have stumbled on one of the issues @hackfin knows all about 🙂  
Can you make a minimal example of your code? I will try it out.  
PS.: 2^42 doesn’t work; in Python we use 2\*\*42

Regards,  
Josy

---

<div class="post-metadata">

**Author:** ![hackfin](https://avatars.discourse-cdn.com/v4/letter/h/9d8465/32.png) [@hackfin](https://discourse.myhdl.org/u/hackfin)\
**Post date:** [December 14, 2021, 7:10pm UTC](https://discourse.myhdl.org/t/long-integer-translation/483/4 "2021-12-14T19:10:26Z")

</div>

I don’t think there’s an arithmetic issue in this context, although I’m puzzled about the signedness of the multiplicands, you might want to post the full example. I’d rather have assumed that `data` is unsigned and `factor` signed, so `data` would be extended instead (still with the resulting bit width: 24+17). If both are signed, you get by with 40 bits in the result.

But: Variable usage (instead of signals) comes with a few caveats, potentially leading to a number of confusing scenarios, so you might want to turn that into a Signal and use the `.next` assignment notation, although josyb’s `[:]` suggestion should synthesize as well.

To note:

- an intbv() multiplied by an intbv() is of type int, not intbv.
- First time assignment in a `@always*` generator of a variable in the Python code defines the type for the HDL (that’s why you got the integer)
- So: the variable is instanced locally per process in the result, but might have to be declared in the `@block` body for vectors/arrays, not entirely sure about that, but I remember some inconsistent behaviour.

---

<div class="post-metadata">

**Author:** ![Albert](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@Albert](https://discourse.myhdl.org/u/Albert)\
**Post date:** [December 15, 2021, 7:25am UTC](https://discourse.myhdl.org/t/long-integer-translation/483/5 "2021-12-15T07:25:20Z")

</div>

Thanks josyb ^ hackfin for the quick replay

Using multiply\_reg[:] = internal\_data \* factor solved the problem.  
Internal\_data is a signed number of 17 bits, factor is an unsigned number of 24 bits. I also trimmed the width of the multiply\_reg to 41 bits.  
In VHDL it looks like this:  
multiply\_reg := resize(internal\_data \* signed(resize(factor, 25)), 41); which is correct VHDL.

josyb, I used 2^42 because it was not correctly shown in this message box. In Python indeed I used 2**42 (must be 2 \* \* 42) (Goes wrong, maybe due to “----” or "**\*" I used on the lines before and after the Python code)

By the way how does this text formatting works in this message box?

---

<div class="post-metadata">

**Author:** ![josyb](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.myhdl.org/josyb/32/5_2.png) [@josyb](https://discourse.myhdl.org/u/josyb)\
**Post date:** [December 15, 2021, 8:00am UTC](https://discourse.myhdl.org/t/long-integer-translation/483/6 "2021-12-15T08:00:39Z")

</div>

Albert,

The [Markdown Cheat Sheet](https://www.markdownguide.org/cheat-sheet/) tells you all about formatting text, as used here in Discourse.

Regards,  
Josy
