Align a union to 16 byte boundary in x86. - c

I got this union, which I am trying to align to 16 byte boundary, using gcc 4.8.
typedef union {
unsigned long long int ud[(1<<6)/8];
long long int d[(1<<6)/8];
unsigned int uw[(1<<6)/4];
int w[(1<<6)/4];
short uh[(1<<6)/2];
unsigned short int h[(1<<6)/2];
unsigned char ub[(1<<6)/1];
char b[(1<<6)/1];
} vector_t;
I tried
vector_t __attribute__ ((aligned(16))) t;
But it is not working. Address of variable t in stack is not aligned to 16 bytes.
I was able to align it using __declspec align(16) in VS 10. Please let me know what is the way to do this in gcc.

The keyword __attribute__ allows you to specify special attributes of struct and union types when you define such types. You need to do
typedef union __attribute__ ((aligned(16))) {
unsigned long long int ud[(1<<6)/8];
long long int d[(1<<6)/8];
unsigned int uw[(1<<6)/4];
int w[(1<<6)/4];
short uh[(1<<6)/2];
unsigned short int h[(1<<6)/2];
unsigned char ub[(1<<6)/1];
char b[(1<<6)/1];
} vector_t;

Related

How to overlay a structure / union from the C language with data in Flutter / Dart (data transfer via Bluetooth)

I am passing data byte by byte from a C application. If the receiving application is also in the C language, then the problem is solved simply - you need to declare the same structure / union and write data there. But in Dart there is no such possibility. You can, of course, allocate bytes manually by creating a helper class, but the structure can be complex and difficult to do. How to transfer a complex structure from a C application to a Dart / Flutter application via Bluetooth?
typedef struct _command_packet
{
unsigned char command;
unsigned char current_temperature;
unsigned char processor_temperature;
unsigned char heater_temperature;
unsigned char software_version;
unsigned char LEDs;
unsigned char water_level;
unsigned char Recipe_number;
unsigned char target_temperature;
unsigned char step_number;
unsigned char after_heating_temperature;
unsigned char after_heating_time;
unsigned char reserved_2;
char Coffee_Temperature_Correction;
unsigned short int Coffee_Cups_Number;
unsigned short int water_sensor_raw_data;
} __command_packet;
#define COMMAND_PACKET_LENGTH 18
typedef union _command_packet_charint
{
__command_packet command_packet;
unsigned char command_packet_char[COMMAND_PACKET_LENGTH];
} command_packet_charint;

Define Struct in C [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
What does ‘: number’ after a struct field mean?
What does ‘unsigned temp:3’ means
I hate to ask this type of question, but it's really bugging me, so I will ask:
What is the function of the : operator in the code below?
#include <stdio.h>
struct microFields
{
unsigned int addr:9;
unsigned int cond:2;
unsigned int wr:1;
unsigned int rd:1;
unsigned int mar:1;
unsigned int alu:3;
unsigned int b:5;
unsigned int a:5;
unsigned int c:5;
};
union micro
{
unsigned int microCode;
microFields code;
};
int main(int argc, char* argv[])
{
micro test;
return 0;
}
If anyone cares at all, I pulled this code from the link below:
http://www.cplusplus.com/forum/beginner/15843/
I would really like to know because I know I have seen this before somewhere, and I want to understand it for when I see it again.
They're bit-fields, an example being that unsigned int addr:9; creates an addr field 9 bits long.
It's commonly used to pack lots of values into an integral type. In your particular case, it defining the structure of a 32-bit microcode instruction for a (possibly) hypothetical CPU (if you add up all the bit-field lengths, they sum to 32).
The union allows you to load in a single 32-bit value and then access the individual fields with code like (minor problems fixed as well, specifically the declarations of code and test):
#include <stdio.h>
struct microFields {
unsigned int addr:9;
unsigned int cond:2;
unsigned int wr:1;
unsigned int rd:1;
unsigned int mar:1;
unsigned int alu:3;
unsigned int b:5;
unsigned int a:5;
unsigned int c:5;
};
union micro {
unsigned int microCode;
struct microFields code;
};
int main (void) {
int myAlu;
union micro test;
test.microCode = 0x0001c000;
myAlu = test.code.alu;
printf("%d\n",myAlu);
return 0;
}
This prints out 7, which is the three bits making up the alu bit-field.
It's a bit field. The number after the colon is how many bits each variable takes up.
That's a declarator that specifies the number of bits for the variable.
For more information see:
http://msdn.microsoft.com/en-us/library/yszfawxh(VS.80).aspx

Simple C data types [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
The difference of int8_t, int_least8_t and int_fast8_t?
I'm a quite confused. I think... (correct me if I'm wrong)
u_int8_t = unsigned short ?
u_int16_t = unsigned int ?
u_int32_t = unsigned long ?
u_int64_t = unsigned long long ?
int8_t = short ?
int16_t = int ?
int32_t = long ?
int64_t = long long ?
Then what does int_fast8_t mean? int_fastN_t? int_least8_t?
These are all specified in the C99 standard (section 7.18).
The [u]intN_t types are generic types (in the ISO C standard) where N represents a bit width (8, 16, etc). 8-bit is not necessarily a short or char since shorts/ints/longs/etc are defined to have a minimum range (not bitwidth) and may not be two's complement.
These newer types are two's complement regardless of the encoding of the more common types, which may be ones' complement or sign/magnitude ( see Representation of negative numbers in C? and Why is SCHAR_MIN defined as -127 in C99?).
fast and least are exactly what they sound like, fast-minimum-width types and types of at least a given width.
The standard also details which type are required and which are optional.
I writing where int is of 16-bit:
u_int8_t = unsigned char
u_int16_t = unsigned int
u_int32_t = unsigned long int
u_int64_t = unsigned long long int
int8_t = char
int16_t = int
int32_t = long int
int64_t = long long int
Q: "Then what does int_fast8_t mean? int_fastN_t? int_least8_t?"
As dan04 states in his answer here:
Suppose you have a C compiler for a 36-bit system, with char = 9
bits, short = 18 bits, int = 36 bits, and long = 72 bits. Then
int8_t does not exist, because there is no way to satisfy the constraint of having exactly 8 value bits with no padding.
int_least8_t is a typedef of char. NOT of short or int, because the standard requires the smallest type with at least 8
bits.
int_fast8_t can be anything. It's likely to be a typedef of int if the "native" size is considered to be "fast".
If you are in Linux most these are defined in /usr/include/linux/coda.h. e.g.
#ifndef __BIT_TYPES_DEFINED__
#define __BIT_TYPES_DEFINED__
typedef signed char int8_t;
typedef unsigned char u_int8_t;
typedef short int16_t;
typedef unsigned short u_int16_t;
typedef int int32_t;
typedef unsigned int u_int32_t;
#endif
And
#if defined(DJGPP) || defined(__CYGWIN32__)
#ifdef KERNEL
typedef unsigned long u_long;
typedef unsigned int u_int;
typedef unsigned short u_short;
typedef u_long ino_t;
typedef u_long dev_t;
typedef void * caddr_t;
#ifdef DOS
typedef unsigned __int64 u_quad_t;
#else
typedef unsigned long long u_quad_t;
#endif

What is the purpose of the flags struct in iOS?

struct
{
unsigned resizesCellWidths:1;
unsigned numColumns:6;
unsigned separatorStyle:3;
unsigned allowsSelection:1;
unsigned backgroundViewExtendsUp:1;
unsigned backgroundViewExtendsDown:1;
unsigned usesPagedHorizontalScrolling:1;
unsigned updating:1;
unsigned ignoreTouchSelect:1;
unsigned needsReload:1;
unsigned allCellsNeedLayout:1;
unsigned isRotating:1;
unsigned clipsContentWidthToBounds:1;
unsigned isAnimatingUpdates:1;
unsigned requiresSelection:1;
unsigned contentSizeFillsBounds:1;
unsigned delegateWillDisplayCell:1;
unsigned delegateWillSelectItem:1;
unsigned delegateWillSelectItemMultiTouch:1;
unsigned delegateWillDeselectItem:1;
unsigned delegateDidSelectItem:1;
unsigned delegateDidSelectItemMultiTouch:1;
unsigned delegateDidDeselectItem:1;
unsigned delegateGestureRecognizerActivated:1;
unsigned delegateAdjustGridCellFrame:1;
unsigned delegateDidEndUpdateAnimation:1;
unsigned dataSourceGridCellSize:1;
unsigned int isEditing:1;
unsigned __RESERVED__:1;
} _flags;
What is the purpose of this struct?
What does the :1 notation at the end of each line signify?
What is the meaning of unsigned modifier when there is no explicit type?
Thanks
Those are bitfields. Since most of these are flags, they only have 2 possible values, so there's no need to assign more than 1 bit to each field. (with a couple exceptions in that struct)
unsigned can stand alone as a type. It's just an unsigned int.

Use of the : operator in C [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
What does ‘: number’ after a struct field mean?
What does ‘unsigned temp:3’ means
I hate to ask this type of question, but it's really bugging me, so I will ask:
What is the function of the : operator in the code below?
#include <stdio.h>
struct microFields
{
unsigned int addr:9;
unsigned int cond:2;
unsigned int wr:1;
unsigned int rd:1;
unsigned int mar:1;
unsigned int alu:3;
unsigned int b:5;
unsigned int a:5;
unsigned int c:5;
};
union micro
{
unsigned int microCode;
microFields code;
};
int main(int argc, char* argv[])
{
micro test;
return 0;
}
If anyone cares at all, I pulled this code from the link below:
http://www.cplusplus.com/forum/beginner/15843/
I would really like to know because I know I have seen this before somewhere, and I want to understand it for when I see it again.
They're bit-fields, an example being that unsigned int addr:9; creates an addr field 9 bits long.
It's commonly used to pack lots of values into an integral type. In your particular case, it defining the structure of a 32-bit microcode instruction for a (possibly) hypothetical CPU (if you add up all the bit-field lengths, they sum to 32).
The union allows you to load in a single 32-bit value and then access the individual fields with code like (minor problems fixed as well, specifically the declarations of code and test):
#include <stdio.h>
struct microFields {
unsigned int addr:9;
unsigned int cond:2;
unsigned int wr:1;
unsigned int rd:1;
unsigned int mar:1;
unsigned int alu:3;
unsigned int b:5;
unsigned int a:5;
unsigned int c:5;
};
union micro {
unsigned int microCode;
struct microFields code;
};
int main (void) {
int myAlu;
union micro test;
test.microCode = 0x0001c000;
myAlu = test.code.alu;
printf("%d\n",myAlu);
return 0;
}
This prints out 7, which is the three bits making up the alu bit-field.
It's a bit field. The number after the colon is how many bits each variable takes up.
That's a declarator that specifies the number of bits for the variable.
For more information see:
http://msdn.microsoft.com/en-us/library/yszfawxh(VS.80).aspx

Resources