I want to OR the bits of a vector together. So say I have a vector called example(23 downto 0) and I want to OR all the bits into another vector, is there any way to do this that does not involve going example(0) or example(1) or ...example(23)?
Asked
Active
Viewed 3.8k times
11
biker of the apocalypse
- 233
- 1
- 3
- 6
2 Answers
12
or_reduce is what you want, and it is available in std_logic_misc. Supported by both A and X for FPGAs.
Aaron D. Marasco
- 649
- 5
- 8
5
Verilog has a convenient "reduction operator" that does exactly what you're asking for: |example[23:0] gives the result of OR'ing all the bits of the example vector.
Unfortunately VHDL doesn't have this operator. According to the comp.lang.vhdl FAQ, though
There is no predefined VHDL operator to perform a reduction operation on all bits of vector (e.g., to "or" all bits of a vector). However, the reduction operators can be easily implemented:
[skipping an example that doesn't handle 'X' and 'Z' values]
function or_reduce( V: std_logic_vector ) return std_ulogic is variable result: std_ulogic; begin for i in V'range loop if i = V'left then result := V(i); else result := result OR V(i); end if; exit when result = '1'; end loop; return result; end or_reduce; ... b <= or_reduce( b_vec );
The Photon
- 129,671
- 3
- 164
- 309
-
-
@JohannesSchaub-litb, of course, it can be synthesized to a really big OR gate (or a tree of smaller ones). Possibly the version in the standard library (in Aaron D. Marasco's answer) will be better optimized than something generated on the fly. – The Photon Aug 03 '17 at 20:17
-
1VHDL-2008 does have unary reduction operators. The FAQ is outdated. Furthermore, the presented function is of questionable synthesizability because of the early exit which some tools may choke on and isn't necessary other than as a micro-optimization for simulation. – KevinThibedeau Jan 01 '19 at 03:08
or_result <= '0' when input=X"00000000" else '1';Change the number of zeros to match the length of the vector in question. – Dec 10 '14 at 08:18result <= '0' when (example=(example'range=>'0')) else '1';– Miguel Risco Apr 16 '17 at 05:06