I have an 32-bit reg 'count' that stores a large integer value. I intend to divide it by 14800 and store the result in a data type which is 8 bits (So values from 0-255) and is unsigned. If the result is something like 1.4, it must be rounded of to the nearest integer, in this case 1. What is the best way of doing this. - Thanks in advance.
Guest
#5397422
Dividing by a constant can often be done by multiplying with the reciprocal value. Here you need to scale it a bit before and after the multiplication. Many FPGAs have 18x18 bit multipliers with a 36bit result. To use that, all calculations should be made with max 18bits: Your 32bit integer value can only use 22 bits to fit in a byte when divided by 14800. So you can ignore the higher 10 bits, and with such a high divisor you can also ignore some lower end bits to make it fit into 18bits. With 18.18 bit fixedpoint in Verilog:
1 | |
2 | |
Guest
#5397529
Sorry I mixed up the shifts from different tries, this should be correct:
1 | |
2 | |
If you can tolerate some error and have no multipliers:
1 | |
Reply
Please log in before posting.