direction of verilog concatenation - concatenation

I am a beginner in verilog.
Almost of all examples for concatenation are as the following.
wire [3:0] result;
reg a, b, c, d;
result = {a, b, c, d};
Is the following possible, too?
wire [3:0] result;
wire a, b, c, d;
{a, b, c, d} = result;

The LHS(left hand side) of assignments do allow concatenations.
module mod1;
wire [3:0] result;
wire a, b, c, d;
reg e,f,g,h;
{a, b, c, d} = result; //Invalid, not in procedural construct
assign {a, b, c, d} = result; //Valid
assign {a,{b,c},d} = result; //Valid
initial
{e, f, g, h} = result; //Valid
endmodule

Related

Implicit casting from float to integer in C — why not the other way around?

Based on C's implicit casting rule, data types are converted to higher type.
Nerveless when I try:
float a;
int b;
a = b = 3.4;
The output is always an integer number for both a and b.
Can I know the reason behind this? Why is it not converting int to float?
Assignment (=) has right-to-left associativity (see operator precedence) so
float a; int b; a = b = 3.4; is the same as:
float a;
int b;
b = 3.4; // b is now 3 (since it can only hold integer values)
a = b; // a is now 3.f

Error message saying undefined symbol '_swap' in module [duplicate]

Is there any built in swap function in C which works without using a third variable?
No.
C++ builtin swap function: swap(first,second);
Check this: http://www.cplusplus.com/reference/algorithm/swap/
You can use this to swap two variable value without using third variable:
a=a^b;
b=a^b;
a=b^a;
You can also check this:
https://stackoverflow.com/questions/756750/swap-the-values-of-two-variables-without-using-third-variable
How to swap without a third variable?
Why do you not want to use a third variable? It's the fastest way on the vast majority of architectures.
The XOR swap algorithm works without a third variable, but it is problematic in two ways:
The variables must be distinct i.e. swap(&a, &a) will not work.
It is slower in general.
It may sometimes be preferable to use the XOR swap if using a third variable would cause the stack to spill, but generally you aren't in such a position to make that call.
To answer your question directly, no there is no swap function in standard C, although it would be trivial to write.
Assuming you want a C solotion, not a C++ one, you could make it a macro, at least using GCC extension to have it generic enough, something like
#define SWAP(x,y) do { \
typeof(x) _x = x; \
typeof(y) _y = y; \
x = _y; \
y = _x; \
} while(0)
beware of tricks like invocations swap(t[i++],i); to avoid them, use the address operator &. And you'll better use a temporary (for integers, there is a famous and useless trick with exclusive-or).
PS: I'm using two local variables _x and _y (but I could have used one local variable only) for better readability, and perhaps also to enable more optimizations from the compiler.
There is no such function in standard C.
(In C++ you have std::swap().)
Maybe a macro from this question can be useful for you.
There is no standard function in C to swap two variables.
A macro can be written this way:
#define SWAP(T, a, b) do { T tmp = a; a = b; b = tmp; } while (0)
and the macro can be called this way:
int a = 42;
int b = 2718;
SWAP(int, a, b);
Some solutions for a writing a SWAP macro should be avoided:
#define SWAP(a, b) do { a = b + a; b = a - b; a = a - b; } while (0)
when operands are of signed types an overflow can occur and signed overflow are undefined behavior.
Also a solution trying to optimize the XOR solution like this should be avoid:
#define SWAP(a, b) (a ^= b ^= a ^=b)
a is modified twice between the previous and the next sequence point, so it violates the sequence points rules and is undefined behavior.
Since you may copy any object representation into an unsigned char array in C, the following macro allows you to swap any two objects:
#define SWAP(X,Y) \
do { \
unsigned char _buf[sizeof(*(X))]; \
memmove(_buf, (X), sizeof(_buf)); \
memmove((X), (Y), sizeof(_buf)); \
memmove((Y), _buf, sizeof(_buf)); \
} while (0)
GCC will even generate optimal code for this in some cases. You might not keep your job though...
There is is a C++ library function. It swaps the values of two integer variables. For example, swap(x, y); will swap the values of variables x and y. Similarly, swap(mat[i][j], mat[j][i]); will swap two values in matrix mat, namely the value in row i column j and the value in row j column i.
#define swap(T, x, y) \
{ \
T tmp = x; \
x = y; \
y = tmp; \
}
int main()
{
int a = 10;
int b = 20;
printf("a=%d b=%d\n", a, b);
swap(int, a, b);
printf("a=%d b=%d\n", a, b);
return 0;
}
There is no built-in swap function but you can try this
a = a ^ b;
b = a ^ b;
a = b ^ a;
I believe I've come up with a type-agnostic function for swapping any two values in standard C, though since I'm fairly new to the language I may have overlooked something. It uses the XOR swap algorithm, and I'm sure it could be optimized more, but it works as long as the two values point to the same number of bytes, specified by the 3rd argument:
void swapn(void *a, void *b, size_t n) {
if (a == b) {
return;
}
size_t i;
char *x = (char *)a,
*y = (char *)b;
for (i = 0; i < n; i++) {
*x ^= *y;
*y ^= *x;
*x ^= *y;
x++;
y++;
}
}
Example usage:
// swap two integers
int x = 5,
y = 30;
printf("%d\t%d\n", x, y);
swapn(&x, &y, sizeof(int));
printf("%d\t%d\n\n", x, y);
// swap two floats
float a = 9.23f,
b = 6.83f;
printf("%.2f\t%.2f\n", a, b);
swapn(&a, &b, sizeof(float));
printf("%.2f\t%.2f\n\n", a, b);
// swap two doubles
double p = 4.7539,
q = 0.9841;
printf("%.4f\t%.4f\n", p, q);
swapn(&p, &q, sizeof(double));
printf("%.4f\t%.4f\n\n", p, q);
// swap two chars
char m = 'M',
n = 'n';
printf("%c\t%c\n", m, n);
swapn(&m, &n, sizeof(char));
printf("%c\t%c\n\n", m, n);
// swap two strings of equivalent length
char s[] = "Hello",
t[] = "World";
printf("%s\t%s\n", s, t);
swapn(s, t, sizeof(s));
printf("%s\t%s\n\n", s, t);
The output is:
5 30
30 5
9.23 6.83
6.83 9.23
4.7539 0.9841
0.9841 4.7539
M n
n M
Hello World
World Hello

Can I assign same value to multiple variables in C?

I tried like
int a=b=3;
but the compiler says 'b' undeclared.
Please help, thanks.
You can use , to declare the variables in a single line first
int a, b, c, ... , last;
and then you can assign to them all at once
a = b = c = ... = last = 3;
note that
int a = b = 3;
which is equivalent to
int a = (b = 3);
will declare a and initialize it with the result b = 3, which in turn will assign 3 to b, which is UNDECLARED
No you can't do this because the compiler interprets that the value 3 to bshould be assigned and b is undeclared
You can do
int b;
int a=b=3;

Declaration of pointers in c

I am using gcc 4.8.1 and I am unable to understand output of following program.
#include<stdio.h>
int main()
{
char* a, b, c;
int* d, e, f;
float* g, h, i;
printf("Size of a %zu and b %zu and c %zu \n", sizeof(a), sizeof(b), sizeof(c));
printf("Size of d %zu and e %zu and f %zu and int is %zu \n", sizeof(d), sizeof(e), sizeof(f), sizeof(int*));
printf("Size of g %zu and h %zu and i %zu and float is %zu \n", sizeof(g), sizeof(h), sizeof(i), sizeof(float));
return 0;
}
Output is
Size of a 4 and b 1 and c 1
Size of d 4 and e 4 and f 4 and int is 4
Size of g 4 and h 4 and i 4 and float is 4
My question is why b and c are not char* type whereas same is possible in case of int and float. I want to know about how C grammar splits declarations.
In a declaration like
char* a, b, c;
Only the type char is used for all variables, not whether it is a pointer (* symbol). When used like that this (equivalent) syntax makes it more clear:
char *a, b, c;
To define 3 pointers:
char *a, *b, *c;
Or in cases where multiple pointers to char are often used, maybe do a typedef:
typedef char* char_buffer;
char_buffer a, b, c;
The problem is that you are using a bad style of declarations
These declarations
char* a, b, c;
int* d, e, f;
float* g, h, i;
are equivalent to
char* a;
char b, c;
int* d;
int e, f;
float* g;
float h, i;
sizeof an object of type char is equal to 1 while sizeof( char * ) in your system is equal to 4. So you get correct output
Size of a 4 and b 1 and c 1
As sizeof( int ) and sizeof( float ) in your system is equal to 4 then you get output
Size of d 4 and e 4 and f 4 and int is 4
Size of g 4 and h 4 and i 4 and float is 4
I said that you use bad style of programming because the declarations you are using like this
char* a, b, c;
do not consistent with the C grammar. The C grammar splits declarations in declaration specifiers (for the statement above it is keyword char) and declarators (in statement above they are *a, b, and c ). So you should follow the C grammar. In this case your code will be more clear.
char *a, b, c;
(Compare for example
char unsigned* c;
and
char unsigned *c;
What declaration is more clear?)
Do not forget that your code can read programmers that for example do not know C but know C#. In this case they will be simply confused. They will consider the declarations in the wrong way as you considered them in your post.
The only pointers in your program are a, d, and g.
It just happens that on your platform that int *, float *, int, and float all have the same size.
char* a, b, c;
*a is a char (when a itself is valid)
b is a char
c is a char
char* a, b, c;
here, a is of type char *, b and c are of type char. same goes for others also.
It is the case in your platform, int and float takes 4 bytes, just as a size of pointer [means sizeof(int) and sizeof(float) is same as sizeof(int *) and sizeof(float *) , respectively] whereas, char is of size 1.
So, your sizeof(a) yields 4 [a being a pointer], when sizeof(b) gives 1 [b is of type char].
Don't be under the impression that e, f are int*. They are not.
Don't be under the impression that h, i are float *. Again,
they are not.
[too long for a comment]
Just for completeness: To define three char pointers (instead of one char pointer and two chars) I'd go for:
char * a; /* This is used to do ... */
char * b; /* This is used to do ... */
char * c; /* This is used to do ... */
You need to put a separate asterisk * for every pointer you want to delare (because the language grammar says so):
// declare 3 pointers
char *a, *b, *c;
To make this clearer (to myself) I prefer to put the asterisk directly before the variable name, and not directly after the type specifier.
// I prefer this:
char *d;
// instead of this:
char* e;
This does not become apparant with int and float on your machine because these happen to have the same size as their respective pointer types (4).

Is there a built in swap function in C?

Is there any built in swap function in C which works without using a third variable?
No.
C++ builtin swap function: swap(first,second);
Check this: http://www.cplusplus.com/reference/algorithm/swap/
You can use this to swap two variable value without using third variable:
a=a^b;
b=a^b;
a=b^a;
You can also check this:
https://stackoverflow.com/questions/756750/swap-the-values-of-two-variables-without-using-third-variable
How to swap without a third variable?
Why do you not want to use a third variable? It's the fastest way on the vast majority of architectures.
The XOR swap algorithm works without a third variable, but it is problematic in two ways:
The variables must be distinct i.e. swap(&a, &a) will not work.
It is slower in general.
It may sometimes be preferable to use the XOR swap if using a third variable would cause the stack to spill, but generally you aren't in such a position to make that call.
To answer your question directly, no there is no swap function in standard C, although it would be trivial to write.
Assuming you want a C solotion, not a C++ one, you could make it a macro, at least using GCC extension to have it generic enough, something like
#define SWAP(x,y) do { \
typeof(x) _x = x; \
typeof(y) _y = y; \
x = _y; \
y = _x; \
} while(0)
beware of tricks like invocations swap(t[i++],i); to avoid them, use the address operator &. And you'll better use a temporary (for integers, there is a famous and useless trick with exclusive-or).
PS: I'm using two local variables _x and _y (but I could have used one local variable only) for better readability, and perhaps also to enable more optimizations from the compiler.
There is no such function in standard C.
(In C++ you have std::swap().)
Maybe a macro from this question can be useful for you.
There is no standard function in C to swap two variables.
A macro can be written this way:
#define SWAP(T, a, b) do { T tmp = a; a = b; b = tmp; } while (0)
and the macro can be called this way:
int a = 42;
int b = 2718;
SWAP(int, a, b);
Some solutions for a writing a SWAP macro should be avoided:
#define SWAP(a, b) do { a = b + a; b = a - b; a = a - b; } while (0)
when operands are of signed types an overflow can occur and signed overflow are undefined behavior.
Also a solution trying to optimize the XOR solution like this should be avoid:
#define SWAP(a, b) (a ^= b ^= a ^=b)
a is modified twice between the previous and the next sequence point, so it violates the sequence points rules and is undefined behavior.
Since you may copy any object representation into an unsigned char array in C, the following macro allows you to swap any two objects:
#define SWAP(X,Y) \
do { \
unsigned char _buf[sizeof(*(X))]; \
memmove(_buf, (X), sizeof(_buf)); \
memmove((X), (Y), sizeof(_buf)); \
memmove((Y), _buf, sizeof(_buf)); \
} while (0)
GCC will even generate optimal code for this in some cases. You might not keep your job though...
There is is a C++ library function. It swaps the values of two integer variables. For example, swap(x, y); will swap the values of variables x and y. Similarly, swap(mat[i][j], mat[j][i]); will swap two values in matrix mat, namely the value in row i column j and the value in row j column i.
#define swap(T, x, y) \
{ \
T tmp = x; \
x = y; \
y = tmp; \
}
int main()
{
int a = 10;
int b = 20;
printf("a=%d b=%d\n", a, b);
swap(int, a, b);
printf("a=%d b=%d\n", a, b);
return 0;
}
There is no built-in swap function but you can try this
a = a ^ b;
b = a ^ b;
a = b ^ a;
I believe I've come up with a type-agnostic function for swapping any two values in standard C, though since I'm fairly new to the language I may have overlooked something. It uses the XOR swap algorithm, and I'm sure it could be optimized more, but it works as long as the two values point to the same number of bytes, specified by the 3rd argument:
void swapn(void *a, void *b, size_t n) {
if (a == b) {
return;
}
size_t i;
char *x = (char *)a,
*y = (char *)b;
for (i = 0; i < n; i++) {
*x ^= *y;
*y ^= *x;
*x ^= *y;
x++;
y++;
}
}
Example usage:
// swap two integers
int x = 5,
y = 30;
printf("%d\t%d\n", x, y);
swapn(&x, &y, sizeof(int));
printf("%d\t%d\n\n", x, y);
// swap two floats
float a = 9.23f,
b = 6.83f;
printf("%.2f\t%.2f\n", a, b);
swapn(&a, &b, sizeof(float));
printf("%.2f\t%.2f\n\n", a, b);
// swap two doubles
double p = 4.7539,
q = 0.9841;
printf("%.4f\t%.4f\n", p, q);
swapn(&p, &q, sizeof(double));
printf("%.4f\t%.4f\n\n", p, q);
// swap two chars
char m = 'M',
n = 'n';
printf("%c\t%c\n", m, n);
swapn(&m, &n, sizeof(char));
printf("%c\t%c\n\n", m, n);
// swap two strings of equivalent length
char s[] = "Hello",
t[] = "World";
printf("%s\t%s\n", s, t);
swapn(s, t, sizeof(s));
printf("%s\t%s\n\n", s, t);
The output is:
5 30
30 5
9.23 6.83
6.83 9.23
4.7539 0.9841
0.9841 4.7539
M n
n M
Hello World
World Hello

Resources