digitalmars.D.learn - c to d: types + struct/union alignment
- Michael P. <baseball.mjp gmail.com> Dec 30 2009
- BCS <none anon.com> Dec 30 2009
- Michael P. <baseball.mjp gmail.com> Dec 30 2009
- bearophile <bearophileHUGS lycos.com> Dec 30 2009
I'm converting some C headers into D, and had some questions.
1. What is the equivalent of a 'short int' in D?
e.g:
struct ScePspSRect {
short int x;
short int y;
short int w;
short int h;
}
2. If I have this union in C:
typedef union ScePspUnion128 {
/* SceULong128 qw;*/ /* Missing compiler support. */
/* SceULong128 uq;*/
/* SceLong128 q;*/
SceULong64 ul[2];
SceLong64 l[2];
unsigned int ui[4];
int i[4];
unsigned short us[8];
short int s[8];
unsigned char uc[16];
char c[16];
float f[4];
ScePspFRect fr;
ScePspIRect ir;
ScePspFVector4 fv;
ScePspIVector4 iv;
ScePspFQuaternion fq;
ScePspFColor fc;
ScePspRGBA8888 rgba8888[4];
ScePspRGBA4444 rgba4444[8];
ScePspRGBA5551 rgba5551[8];
ScePspRGB565 rgb565[8];
} ScePspUnion128 __attribute__((aligned(16)));
Does it convert to this:
union ScePspUnion128 {
/* SceULong128 qw;*/ /* Missing compiler support. */
/* SceULong128 uq;*/
/* SceLong128 q;*/
align(16)
{
SceULong64 ul[2];
SceLong64 l[2];
uint ui[4];
int i[4];
ushort us[8];
short int s[8]; //What type???
ubyte uc[16];
char c[16]; //should it be byte or char?
float f[4];
ScePspFRect fr;
ScePspIRect ir;
ScePspFVector4 fv;
ScePspIVector4 iv;
ScePspFQuaternion fq;
ScePspFColor fc;
ScePspRGBA8888 rgba8888[4];
ScePspRGBA4444 rgba4444[8];
ScePspRGBA5551 rgba5551[8];
ScePspRGB565 rgb565[8];
}
}
Are those type changes correct? Can I use the same way to align structs defined
like that?
3.
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;
Would it be okay to be make/alias all of the uintxx_t to 'uint' and all of the
intxx_t to 'int'?
P.S. If anyone is wondering, this is taken from the homebrew PSP SDK.
Thanks,
Michael P.
Dec 30 2009
Hello Michael P.,I'm converting some C headers into D, and had some questions. 1. What is the equivalent of a 'short int' in D? e.g: struct ScePspSRect { short int x; short int y; short int w; short int h; }
short3. typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; typedef uint64_t u64; typedef int8_t s8; typedef int16_t s16; typedef int32_t s32; typedef int64_t s64; Would it be okay to be make/alias all of the uintxx_t to 'uint' and all of the intxx_t to 'int'?
no, uint16_t is (I assume) 16 bits long and uint32_t is 32 bits long. if you get them mixed up (as the aliasing you proposed would do) you will get problems passing struct to non-D code.
Dec 30 2009
BCS Wrote:Hello Michael P.,I'm converting some C headers into D, and had some questions. 1. What is the equivalent of a 'short int' in D? e.g: struct ScePspSRect { short int x; short int y; short int w; short int h; }
short3. typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; typedef uint64_t u64; typedef int8_t s8; typedef int16_t s16; typedef int32_t s32; typedef int64_t s64; Would it be okay to be make/alias all of the uintxx_t to 'uint' and all of the intxx_t to 'int'?
no, uint16_t is (I assume) 16 bits long and uint32_t is 32 bits long. if you get them mixed up (as the aliasing you proposed would do) you will get problems passing struct to non-D code.
Okay. I'll look at where they came from and see what they are defined as. They come from a file called stdint.h. Also, would short unsigned int be a 'ushort' then?
Dec 30 2009
Michael P.:Also, would short unsigned int be a 'ushort' then?<
In D ushort is an unsigned integer 16 bits long. While I think in C short unsigned int is not guaranteed to be 16 bit wide. The same is true for unsigned short / uint. Bye, bearophile
Dec 30 2009








bearophile <bearophileHUGS lycos.com>