I try to use static cast in systemverilog code.
I have variable of type logic:
logic [127:0] data[];
and I want to obtain variable of type bit. For this, I wrote the following code:
bit [127:0] bit_data [];
bit [127:0] b_data;
typedef bit [127:0] bit_127_t;
bit_data = bit_127_t' (data);
b_data = bit_data[0];
this code didn't work.. and I don't find the error.
Thanks in advance
When you say something doesn't work, it always help to show what's not working about it. Like did you get a different result than expected, or a compiler error?
I think your typedef is incorrect. It should be
typedef bit [127:0] bit_127_t[];
It would also help to use the same typedef in the declaration of bit_data.
Related
I am converting C++ code to C code. Here is the code which needs to be converted-
struct new{
enum new_enum:uint8_t {
head = 1,
tail = 2,
data = 3,
};
};
How to convert this? Can someone explain what is uint8_t in enum? Is it assigning all variables to uint8_t?
The code you provide cannot compile in C++, since the struct name is new.
Anyway, you can try to just drop the :uint8_t. This will change the strongly typed enum to a standard C one. If you are lucky everything will work. If in any other point you have an use of head, tail or data, it won't work. You can do this:
enum new_enum {
new_enum_head = 1,
new_enum_tail = 2,
new_enum_data = 3,
};
Ugly, but likely to work. Of course the enum will have to go out of the struct it was defined in. That again can cause a lot of grief, but less likely.
I am converting C++ code to C code.
This this seems like a strange direction to take.
There is no direct conversion here. Typed enums are a c++ feature.
There is a similar question here: How to emulate strongly typed enum in C?
Thanks in advance for the help.
trying to use a fairly large C/Cpp (yes I know) library and am running into an error. Unfortunately the error that I am getting back is extremely vague but I do know what variable is causing it. All I would like to do is simply print this variable out to stdout so that I can see what is going on. However, this variable is not a standard type. It has the type
Opt_Int64 someVar = 10;
where
typedef int64_t VosT_int64;
typedef VosT_Int64 OpT_Int64;
For the life of me, I can't figure out how to convert this Opt_Int64 type into a char array so that I can print it out. I would assume that the following function would do this for me
void int64ToChar(char mesg[], Opt_Int64 num) {
for(int i = 0; i < 8; i++) mesg[i] = num >> (8-1-i)*8;
}
but it doesn't seem to work (I built a very simple hello world style program to test this). Either I simply don't understand typedef as well as I should or there must be something wrong with the function above. Isn't typedef just a way of telling the compiler that you're giving a type a different name?
int64_t is a standard type, defined in stdint.h. You can print it like this:
printf("%" PRId64 "\n", someVar);
The macro PRId64 and its kin are defined in inttypes.h.
My intention is to build such a data structure in C++:
struct callbackDataUnit {
std::string columnName;
std::string columnData;
};
std::vector<callbackDataUnit> callbackRow;
std::vector<callbackRow> callbackSet; <--- Invalid... It needs a type here
The compiler first complains about the lack os static on callbackRow. Even if I use static there, it still does not compile as the structure is naturally invalid
I would like to take this opportunity to understand a little more about C++ (I´m a beginner on that area), so here goes my questions:
a) Why do we need the static qualifier here ?
b) How can I solve this matrix of the first variables ? I could create 3 classes here (CallbackDataUnit, CallbackRow and CallbackSet) but I feel I would be missing real C++ power here. Would it make sense to make callbackRow a single element struct, so that it can be added to callbackSet ?
Thanks for helping
I think you want define new types, not variables.
To do that, you can use typedef or using.
typedef std::vector<callbackDataUnit> callbackRow;
typedef std::vector<callbackRow> callbackSet;
using callbackRow = std::vector<callbackDataUnit>;
using callbackSet = std::vector<callbackRow>;
If you want to just define variables, you can use:
std::vector<callbackDataUnit> callbackRow;
std::vector<decltype(callbackRow)> callbackSet;
(Major edit: The way I posed the original question was bit confusing. So, I am trying to improve the question)
I am trying to convert “int32_t” to “static const int32_t” type. However, I could not figure out how to use static_cast and const_cast together. Any help would be appreciated.
I want to do this so that rather than initializing my “static const int32_t IRF_MAX_ENVELOPE_ELEMENTS2” to a hardcore value, I would like to set this based on value passed to the relevant function.
Say, the value of iNoOfSamples_In is 128, I would like to set IRF_MAX_ENVELOPE_ELEMENTS2 to 128 too; but, as as a “static const int32_t” like this:
int32_t iNoOfSamples_In = 128;
static const int32_t IRF_MAX_ENVELOPE_ELEMENTS2 = iNoOfSamples_In;
However, when I go to declare an array of size IRF_MAX_ENVELOPE_ELEMENTS2
double dTime_Scale[IRF_MAX_ENVELOPE_ELEMENTS2]; // Line 80
I get the following errors (line 80 marked in code snippet):
SpecialPulses.cpp(80) : error C2057: expected constant expression
SpecialPulses.cpp(80) : error C2466: cannot allocate an array of constant size 0
SpecialPulses.cpp(80) : error C2133: 'dTime_Scale' : unknown size
So, it seems that max_envelope_elements is not constant.
Just as the error message says, you can't declare a variable as const and then change its value. However, if you are trying to call a function that takes a const int_32, that's fine - just declare the variable as int_32. In that case, the const just says that the function doesn't change the value of the parameter inside the function, but even if it did it wouldn't affect your variable anyway.
By const it means that it's constant and by definition a constant is:
a situation or state of affairs that does not change. - google"
so you cannot change the value of the const variable after you initiated it.
Please do note that const are used most of the time for code readability in replacement for magic numbers in code.
take this for example
if(a>b%2)
//do something
What the heck is 2 and what does that do?
while if you actually can use something like this.
if(a>b%SOME_CONST_VALUE)
//do something
you can actually tell by the const variable what you are actually doing rather than having the programmer/developer who will maintain your code what the heck does that if statement does.
const_cast<int32_t>(IRF_MAX_ENVELOPE_ELEMENTS2)=256;
You can change your constant values like that, I hope this will help your problem.
As a follow up to my previous question (Variable Length Array Performance Implications (C/C++)), I am having a bit of trouble maintaining const correctness using the C system call writev(). Namely, it appears as though I have run into the exact same problem as this user did in C, although I am using C++:
https://codereview.stackexchange.com/questions/9547/casting-const-pointer-to-non-const-pointer-when-using-struct-iovec
Here's a snippet of my code:
int my_awesome_transmit_function(const uint8_t* const buffer, const size_t length) {
// ... some stuff happens...
struct iovec iov[2];
iov[1].iov_base = buffer; // compiler yells about this
iov[1].iov_len = length;
// ... some more code you don't care about
writev(my_fd, iov, 2);
}
Given the solution presented from the CodeReview post, I have implemented the following change to the line that's giving me issues since I'd like to avoid the C-style cast:
iov[1].iov_base = const_cast<uint8_t*>(buffer);
Is this a valid way of using const cast? As far as I can tell, writev guarentees that the iov structure will not be modified (http://linux.die.net/man/2/writev). This blog post (http://blog.aaronballman.com/2011/06/when-should-you-use-const_cast/) leads me to believe that this is a valid usage (since I am never modifying the buffer), but I'd like to be certain since generally wherever I see const_cast or reinterpret_cast, I get a bit leery.
Thanks in advance for the help.
Yes, your use of const_cast is fine.
If one of the array elements is not going to be modified (ever), then you could declare it as a const.
const struct iovec iov_c(buffer, length); // create a constructor for your struct! always neater than
// assigning their members directly in code.
This assumes you have control over the signature of writev(..) and can pass in two iovec pointers.
If not, the usage of const_cast there looks alright.