bluespec.com Forum Index bluespec.com
Bluespec Forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Best way to initialize a size variable array

 
Post new topic   Reply to topic    bluespec.com Forum Index -> Designing with BSV's Rules, Interfaces, ...
View previous topic :: View next topic  
Author Message
PhantomFav



Joined: 13 Jun 2011
Posts: 16

PostPosted: Sat Sep 21, 2013 10:52 am    Post subject: Best way to initialize a size variable array Reply with quote

Hi, i have the array offset whose size depend of intParDeep parameter.

Code:
BitF offset[intParDeep];


I have to initializes it according to a function, for example if intParDeep=3 the array must be initialized like this:
Code:
offset[1]= 265; offset[2]= 534; offset[3]= 806;


but if intParDeep=4 i have this other situation:
Code:
offset[1]= 200; offset[2]= 403; offset[3]= 608; offset[4]= 806;


now i want to static elaborate the code to fill the array automatically, i have tried this:
Code:
BitF offset[intParDeep+1] =
         (begin
            if (intParDeep==3) {265, 534, 806};
            else {200, 403, 608, 806};
         end);

but the {} operator it's elaborated like a concatenation and not like an array initialization, which is the best way to initialize an array automatically at compilation time?
Back to top
View user's profile Send private message
quark
Site Admin


Joined: 02 Nov 2007
Posts: 499

PostPosted: Sat Sep 21, 2013 12:04 pm    Post subject: Re: Best way to initialize a size variable array Reply with quote

You could try one of these two options:
Code:
if (intParDeep == 3) begin
   offset[0]= 265; offset[1]= 534; offset[2]= 806;
end
else begin
   offset[0]= 200; offset[1]= 403; offset[2]= 608; offset[3]= 806;
end
(Note that array indexing starts at 0, not 1.)

Code:
BitF offset3[3] = {265, 534, 806};
BitF offset4[4] = {200, 403, 608, 806};

BitF offset[intParDeep] = ((intParDeep == 3) ? offset3 : offset4);
which could also be written as:
Code:
BitF offset[intParDeep];
if (intParDeep == 3) begin
   BitF tmp[3] = {265, 534, 806};
   offset = tmp;
end
else begin
   BitF tmp[4] = {200, 403, 608, 806};
   offset = tmp;
end

Currently, the braces syntax for assigning to arrays is only supported when it is used to assign to an array when it is declared. This last example takes advantage of this to assign to a variable "tmp".

You may also consider, instead of an array, using a List or Vector type, which have a "cons" function for constructing a value.
Back to top
View user's profile Send private message
PhantomFav



Joined: 13 Jun 2011
Posts: 16

PostPosted: Sun Sep 22, 2013 10:36 am    Post subject: Reply with quote

thanks for you answer, I have adopted the third method. (I'm sorry for the 0th elements but during the paste&edit of the code snip I have deleted it XD)
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    bluespec.com Forum Index -> Designing with BSV's Rules, Interfaces, ... All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum
bluespec.com topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP