For Loop In Verilog Does Not Converge









up vote
0
down vote

favorite












I'm attempting to using a for loop to count the repeated leading bit in a 32-bit number. For this, I am doing:



input[31:0] A;
output reg result;
Integer i;
for (i = 31; i > -1; i = i - 1) begin
if (A[i] == 0) begin
result = result + 1;
end
else if (A[i] == 1) begin
i = -1;
end
end


However, when I synthesize the program, I receive a warning saying that the program does not converge. Am I using the for loop wrong? Before this I used i >= 0 and even used a while instead but it doesn't change the outcome. I would appreciate any help. Should I set result to 0 before running the loop?










share|improve this question



















  • 1




    did you mean i = i - 1 by a chance?
    – Serge
    Nov 11 at 20:50










  • Yes, sorry. That was a copy error
    – Rafael_Jeffery
    Nov 13 at 1:04














up vote
0
down vote

favorite












I'm attempting to using a for loop to count the repeated leading bit in a 32-bit number. For this, I am doing:



input[31:0] A;
output reg result;
Integer i;
for (i = 31; i > -1; i = i - 1) begin
if (A[i] == 0) begin
result = result + 1;
end
else if (A[i] == 1) begin
i = -1;
end
end


However, when I synthesize the program, I receive a warning saying that the program does not converge. Am I using the for loop wrong? Before this I used i >= 0 and even used a while instead but it doesn't change the outcome. I would appreciate any help. Should I set result to 0 before running the loop?










share|improve this question



















  • 1




    did you mean i = i - 1 by a chance?
    – Serge
    Nov 11 at 20:50










  • Yes, sorry. That was a copy error
    – Rafael_Jeffery
    Nov 13 at 1:04












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm attempting to using a for loop to count the repeated leading bit in a 32-bit number. For this, I am doing:



input[31:0] A;
output reg result;
Integer i;
for (i = 31; i > -1; i = i - 1) begin
if (A[i] == 0) begin
result = result + 1;
end
else if (A[i] == 1) begin
i = -1;
end
end


However, when I synthesize the program, I receive a warning saying that the program does not converge. Am I using the for loop wrong? Before this I used i >= 0 and even used a while instead but it doesn't change the outcome. I would appreciate any help. Should I set result to 0 before running the loop?










share|improve this question















I'm attempting to using a for loop to count the repeated leading bit in a 32-bit number. For this, I am doing:



input[31:0] A;
output reg result;
Integer i;
for (i = 31; i > -1; i = i - 1) begin
if (A[i] == 0) begin
result = result + 1;
end
else if (A[i] == 1) begin
i = -1;
end
end


However, when I synthesize the program, I receive a warning saying that the program does not converge. Am I using the for loop wrong? Before this I used i >= 0 and even used a while instead but it doesn't change the outcome. I would appreciate any help. Should I set result to 0 before running the loop?







for-loop verilog vivado






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 at 1:04

























asked Nov 11 at 19:45









Rafael_Jeffery

3418




3418







  • 1




    did you mean i = i - 1 by a chance?
    – Serge
    Nov 11 at 20:50










  • Yes, sorry. That was a copy error
    – Rafael_Jeffery
    Nov 13 at 1:04












  • 1




    did you mean i = i - 1 by a chance?
    – Serge
    Nov 11 at 20:50










  • Yes, sorry. That was a copy error
    – Rafael_Jeffery
    Nov 13 at 1:04







1




1




did you mean i = i - 1 by a chance?
– Serge
Nov 11 at 20:50




did you mean i = i - 1 by a chance?
– Serge
Nov 11 at 20:50












Yes, sorry. That was a copy error
– Rafael_Jeffery
Nov 13 at 1:04




Yes, sorry. That was a copy error
– Rafael_Jeffery
Nov 13 at 1:04












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










A[i] == 1 makes number of iterations non-deterministic and causes synthesis to fail. The way around it is letting the loop to unroll till the end and use a conditional variable to handle your calculations. Something like the following:



input[31:0] A;
output reg result;
Integer i;
reg flag;

flag = 0;
for (i = 31; i > -1; i = i - 1) begin
if (flag == 0 && A[i] == 0) begin
result = result + 1;
end
else if (A[i] == 1) begin
flag = 1;
end
end


I assume that it was some type of a flop logic, since in any case this would produce state elements. So, you need to use correct nbas for result and flag.






share|improve this answer




















  • Thank you! It works!
    – Rafael_Jeffery
    Nov 13 at 18:32

















up vote
0
down vote













For synthesis for loops must converge during COMPILE time. Your condition of A[i]==1 can not be determined at compile time so the loops goes from 32 to 2^31-1 before it ends.



Verilog is an HDL, which in many aspects is totally different from standard computer languages.






share|improve this answer




















  • How can I implement that sort of check? Should I be using a while loop instead?
    – Rafael_Jeffery
    Nov 13 at 1:08










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252534%2ffor-loop-in-verilog-does-not-converge%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










A[i] == 1 makes number of iterations non-deterministic and causes synthesis to fail. The way around it is letting the loop to unroll till the end and use a conditional variable to handle your calculations. Something like the following:



input[31:0] A;
output reg result;
Integer i;
reg flag;

flag = 0;
for (i = 31; i > -1; i = i - 1) begin
if (flag == 0 && A[i] == 0) begin
result = result + 1;
end
else if (A[i] == 1) begin
flag = 1;
end
end


I assume that it was some type of a flop logic, since in any case this would produce state elements. So, you need to use correct nbas for result and flag.






share|improve this answer




















  • Thank you! It works!
    – Rafael_Jeffery
    Nov 13 at 18:32














up vote
1
down vote



accepted










A[i] == 1 makes number of iterations non-deterministic and causes synthesis to fail. The way around it is letting the loop to unroll till the end and use a conditional variable to handle your calculations. Something like the following:



input[31:0] A;
output reg result;
Integer i;
reg flag;

flag = 0;
for (i = 31; i > -1; i = i - 1) begin
if (flag == 0 && A[i] == 0) begin
result = result + 1;
end
else if (A[i] == 1) begin
flag = 1;
end
end


I assume that it was some type of a flop logic, since in any case this would produce state elements. So, you need to use correct nbas for result and flag.






share|improve this answer




















  • Thank you! It works!
    – Rafael_Jeffery
    Nov 13 at 18:32












up vote
1
down vote



accepted







up vote
1
down vote



accepted






A[i] == 1 makes number of iterations non-deterministic and causes synthesis to fail. The way around it is letting the loop to unroll till the end and use a conditional variable to handle your calculations. Something like the following:



input[31:0] A;
output reg result;
Integer i;
reg flag;

flag = 0;
for (i = 31; i > -1; i = i - 1) begin
if (flag == 0 && A[i] == 0) begin
result = result + 1;
end
else if (A[i] == 1) begin
flag = 1;
end
end


I assume that it was some type of a flop logic, since in any case this would produce state elements. So, you need to use correct nbas for result and flag.






share|improve this answer












A[i] == 1 makes number of iterations non-deterministic and causes synthesis to fail. The way around it is letting the loop to unroll till the end and use a conditional variable to handle your calculations. Something like the following:



input[31:0] A;
output reg result;
Integer i;
reg flag;

flag = 0;
for (i = 31; i > -1; i = i - 1) begin
if (flag == 0 && A[i] == 0) begin
result = result + 1;
end
else if (A[i] == 1) begin
flag = 1;
end
end


I assume that it was some type of a flop logic, since in any case this would produce state elements. So, you need to use correct nbas for result and flag.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 at 2:29









Serge

3,3732914




3,3732914











  • Thank you! It works!
    – Rafael_Jeffery
    Nov 13 at 18:32
















  • Thank you! It works!
    – Rafael_Jeffery
    Nov 13 at 18:32















Thank you! It works!
– Rafael_Jeffery
Nov 13 at 18:32




Thank you! It works!
– Rafael_Jeffery
Nov 13 at 18:32












up vote
0
down vote













For synthesis for loops must converge during COMPILE time. Your condition of A[i]==1 can not be determined at compile time so the loops goes from 32 to 2^31-1 before it ends.



Verilog is an HDL, which in many aspects is totally different from standard computer languages.






share|improve this answer




















  • How can I implement that sort of check? Should I be using a while loop instead?
    – Rafael_Jeffery
    Nov 13 at 1:08














up vote
0
down vote













For synthesis for loops must converge during COMPILE time. Your condition of A[i]==1 can not be determined at compile time so the loops goes from 32 to 2^31-1 before it ends.



Verilog is an HDL, which in many aspects is totally different from standard computer languages.






share|improve this answer




















  • How can I implement that sort of check? Should I be using a while loop instead?
    – Rafael_Jeffery
    Nov 13 at 1:08












up vote
0
down vote










up vote
0
down vote









For synthesis for loops must converge during COMPILE time. Your condition of A[i]==1 can not be determined at compile time so the loops goes from 32 to 2^31-1 before it ends.



Verilog is an HDL, which in many aspects is totally different from standard computer languages.






share|improve this answer












For synthesis for loops must converge during COMPILE time. Your condition of A[i]==1 can not be determined at compile time so the loops goes from 32 to 2^31-1 before it ends.



Verilog is an HDL, which in many aspects is totally different from standard computer languages.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 19:56









Oldfart

2,3542710




2,3542710











  • How can I implement that sort of check? Should I be using a while loop instead?
    – Rafael_Jeffery
    Nov 13 at 1:08
















  • How can I implement that sort of check? Should I be using a while loop instead?
    – Rafael_Jeffery
    Nov 13 at 1:08















How can I implement that sort of check? Should I be using a while loop instead?
– Rafael_Jeffery
Nov 13 at 1:08




How can I implement that sort of check? Should I be using a while loop instead?
– Rafael_Jeffery
Nov 13 at 1:08

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252534%2ffor-loop-in-verilog-does-not-converge%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto