c to d: types + struct/union alignment

Michael P. baseball.mjp at gmail.com
Wed Dec 30 14:54:46 PST 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.


More information about the Digitalmars-d-learn mailing list