18

I am teaching myself verilog. The book I am following stated in the introduction chapters that to perform division we use the '/' operator or '%' operator. In later chapters it's saying that division is too complex for verilog and cannot be synthesized, so to perform division it introduces a long algorithm.

So I am confused, can't verilog handle simple division? is the / operator useless?

Morgan
  • 19,934
  • 8
  • 58
  • 84
StuckInPhDNoMore
  • 2,507
  • 4
  • 41
  • 73
  • 1
    For efficient RTL if the denominator is fixed then just use 1/denominator, instead of a/3 use a*0.33. for a/2 use a>>1. The fixed point wordlength will determine the accuracy of coefficients and answers. – Morgan Jul 31 '12 at 14:49
  • 1
    Synthesis tools are pretty good these days and often the multiple by a single coefficient will be as small as a hand crafted operation. All else being equal I would choose the easier to read version, for better code quality. – Morgan Aug 06 '12 at 11:49

12 Answers12

36

It all depends what type of code you're writing.

If you're writing code that you intend to be synthesised, that you intend to go into an FPGA or ASIC, then you probably don't want to use the division or modulo operators. When you put any arithmetic operator in RTL the synthesiser instances a circuit to do the job; An adder for + & -; A multiplier for *. When you write / you're asking for a divider circuit, but a divider circuit is a very complex thing. It often takes multiple clock cycles, and may use look up tables. It's asking a lot of a synthesis tool to infer what you want when you write a / b.

(Obviously dividing by powers of 2 is simple, but normally you'd use the shift operators)

If you're writing code that you don't want to be synthesised, that is part of a test bench for example, then you can use division all you want.

So to answer your question, the / operator isn't useless, but you have be concious of where and why you're using it. The same is true of *, but to a lesser degree. Multipliers are quite expensive, but most synthesisers are able to infer them.

Paul S
  • 7,645
  • 2
  • 24
  • 36
  • I did not know there was a relation between shifting and division.. Ill look into it, thanks – StuckInPhDNoMore Jul 31 '12 at 17:08
  • 2
    @FarazKhan The relation is quite obvious, By shifting right we are dividing by 2 and by shifting right we are multiplying by 2. e.g. Think in terms of binary. Take a number say 4'd12, in binary its 4'b1100. Now if we right shift it once we get 4'b0110 which is 6 in decimal, in other words we have divided the value by 2 and it used only one clock. – Osaid Feb 25 '13 at 06:23
  • @Osaid What happens if you shift left? :P – Moberg Jul 09 '15 at 09:03
  • 1
    @Moberg It's a multiplication by a power of two. Say we have three, or 6'b000011. If we shift it left by two bits, we ge 6'b011000. This is twenty-four (that is, three multiplied by two to the third power) – nanofarad Jul 15 '15 at 11:25
  • I think Moberg was joking about Osaid only talking about shfiting right "By shifting right we are dividing, ... and by shifting right we are multiplying... " :P – Hugh Perkins Mar 11 '22 at 13:31
13

You have to think in hardware.

When you write a <= b/c you are saying to the synthesis tool "I want a divider that can provide a result every clock cycle and has no intermediate pipline registers".

If you work out the logic circuit required to create that it's very complex, especially for higher bit counts. Generally FPGAs won't have specialist hardware blocks for division so it would have to be implemented out of generic logic resources. It's likely to be both big (lots of luts) and slow (low fmax).

Some synthesisers may implement it anyway (from a quick search it seems quartus will), others won't bother because they don't think it's very useful in practice.

If you are dividing by a constant and can live with an approximate result then you can do tricks with multipliers. Take the reciprocal of what you wanted to divide by, multiply it by a power of two and round to the nearest integer.

Then in your verilog you can implement your approximate divide by multiply (which is not too expensive on modern FPGAS) followed by shift (shifting by a fixed number of bits is essentially free in hardware). Make sure you allow enough bits for the intermediate result.

If you need an exact answer or if you need to divide by something that is not a pre-defined constant you will have to decide what kind of divider you want. IF your throughput is low then you can use a state machine based approach which does one division every n clock cycles. If your throughput is high and you can afford the device area then a pipelined approach which does a division per clock cycle (but requires multiple cycles for the result to flow through) may be more appropriate.

Often tool vendors will provide pre-made blocks (altera calls them megafunctions) for this kind of stuff. The advantage of these is that the tool vendor will likely have carefully optimised them for the device. The downside is they can bring vendor lockin, if you want to move to a different device vendor you will most likely have to swap out the block and the block you swap it with may have different characteristics.

Peter green
  • 131
  • 1
  • 2
6

So im confused. cant verilog handle simple division? is the / operator useless?

The verilog synthesis spec (IEEE 1364.1) actually indicates all arithmetic operators with integer operands should be supported but nobody follows this spec. Some synthesis tools can do integer division but others will reject it(I think XST still does) because combinational division is typically very area inefficient. Multicycle implementations are the norm but these cannot be synthesized from '/'.

5

Division and modulo are never "simple". Avoid them if you can do so, e.g. through bit masks or shift operations. Especially a variable divisor is really complicated to implement in hardware.

Wormbo
  • 4,978
  • 2
  • 21
  • 41
4

"Verilog the language" handles division and modulo just fine - when you are using a computer to simulate your code you have full access to all it's abilities.

When you are synthesising your code to a particular chip, there are limitations. The limitations tend to be based on what the tool-vendor thinks is "sensible" rather than what is feasible.

In the old days, division by anything other than a power-of-two was deemed to be non-sensible for silicon as it took up a lot of space and ran very slowly. At the moment, some synthesisers with create "divide by a constant" circuits for you.

In future, I see no reason why the synthesiser shouldn't create you a divider (or make use of one that is in the DSP blocks of a potential future architecture). Whether it will or not remains to be seen, but witness the progression of multipliers (from "only powers of two" to "one input constant" to "full implementation" in just a few years)

Martin Thompson
  • 16,395
  • 1
  • 38
  • 56
  • Thanks. I did think of just using the module generated by the core generator for division, rather than going into the algorithm if it. – StuckInPhDNoMore Jul 31 '12 at 17:07
1
  1. circuits including only division by 2 : just shift the bit :)
  2. other than 2 .... see you should always think at circuit level verilog is NOT C or C++
  3. / and % is not synthesizable or if it becomes( in new versions) i believe you should keep your own division circuit this is because the ip they provide will be general ( most probably they will make for floating not fixed)
  4. i bet you had gone through morris mano computer architechure book , there in some last chapters the whole flow is given along with algo , go through it follow it and make your own
  5. see now if your works go for only logic verification and no real circuit is needed , sure go for / and % . no problem it will work for simulation
Rahul Jain
  • 99
  • 1
  • 3
0

Division using '/' is possible in verilog. But it is not a synthesizable operator. Same is the case for multiplication using '*'. There are certain algorithms to perform these operations in verliog, and they are used if the code needs to be synthesizable. ie. if you require an equivalent hardware for it.

I am not aware of any algorithms for division, but for multiplication, i have used Booth's algorithm.

badari259
  • 29
  • 4
0

if you want the synthesizable code you can use the Divison_IP or you can use the right shifting operator for some divisions like 64/8=8 same 64>>3 = 8.

0

Division isn't simple in hardware as people spent a lot of time in an efficient and fast multiplier as an example. However, you can do divid by 2 easily by right shifting one bit in hardware.

DavidYu
  • 1
  • 1
0

Actually your point is very valid and I was also confused in my initial days of learning HDLs. When you synthesise a division operator, it consumes a lot of resources on FPGA or during logic synthesis for ASIC. Try following instead.

You can also perform division(and multiplication) by shifting some vector(right = division, left = multiplication). But that will be multiplication(and divion) by 2.

Example 0100 = 4

Shift right 0010 = 2(which is 4/2) Shift left 1000 = 8(which is 4*2).

We use >> operator for shift right, and << for shift left.

But we can also produce variations out of it.

For example multiplication by 3. So if we have 0100 (4 dec) then also will be shift left and add one at each step. ((0100 << 1)+1)

Similarly division by 3

shift right and subtract one at each step. ((0100 >> 1) - 1)

These methods were made because to be honest, resources in FPGA are limited, and when it comes to ASICs, your manager tries to kill you for any additional logic. :)

John McnZ
  • 107
  • 5
-1

The division operator / is not useless in Verilog/System Verilog. It works in case of simulations as usual mathematical operator.

Some synthesis tools like Xilinx Vivado synthesize the division operator also because it is having a pre-built algorithm in it (though takes more hardware gates).

In simple words, you can do division in Verilog but have to take care of tools and simulators.

Ahsan Ali
  • 91
  • 1
  • 4
-10

Using result <= a/b and works perfectly.

Remember when using the <= operator, the answer is calculated immediately but the answer is entered inside the "result" register at next clock positive edge.

If you don't want to wait till next clock positive edge use result = a/b.

Remember, any arithmetic operation circuit needs some time to finish the operation, and during this time the circuit generates random numbers (bits).

Its like when A-10 warthog attack airplane attacks a tank it shoots lots of bullets. That's how the divider circuit acts while dividing,it spits random bits. After couple of nanoseconds it will finish dividing and return a stable good result.

This is why we wait until next clock cycle for the "result" register. We try to protect it from random garbage numbers.

Division is the most complex operation, so it will have a delay in calculation. For 16bit division the result will be calculated in approximately 6 nanoseconds.

Tim
  • 4,414
  • 4
  • 35
  • 49
ili
  • 3
  • 1
  • 5
    6 nano seconds on the module running at what clock and what type of division algorithm is it using? – Osaid Dec 20 '12 at 13:11
  • Welcome to slashdot, a little belatedly. I bet you regret answering this question :P Basically, your answer is correct for simulation, but works less well for synthesis. Do keep answering questions. It gets easier with practice :) – Hugh Perkins Feb 27 '22 at 21:50