How do I choose the correct primative?
Jake Thomas
jake at fake.com
Fri Jan 3 19:14:32 PST 2014
> We have size_t defined as uint on 32bit and ulong on 64bit.
> ptrdiff_t for int/long.
> I don't know how dmd handles it, although you do have the
> ability to align variables.
> You may want to consider gdc or ldc more than dmd as they have
> better optimization.
Sorry for the delayed response - distractions.
Thanks for the tips! I didn't know about aligning or ptrdiff_t.
I looked into and experimented with "align". At least with dmd,
it seems to only work in structs.
Looking at http://dlang.org/attribute.html, one might conclude
that this is a bug in dmd - the attributes web page doesn't say
anywhere that align should be limited to work only within
structs. No syntactical error is thrown by the compiler if align
is used outside a struct, but the symantics aren't there to back
it up.
Watch this:
[code]
import std.stdio;
int main()
{
align(8) int testInt1;
align(8) int testInt2;
align(8) int testInt3;
int* testInt1Locale = &testInt1;
int* testInt2Locale = &testInt2;
int* testInt3Locale = &testInt3;
long testLong1;
long testLong2;
long testLong3;
long* testLong1Locale = &testLong1;
long* testLong2Locale = &testLong2;
long* testLong3Locale = &testLong3;
struct AlignTest
{
align (8):
int intOne;
int intTwo;
int intThree;
}
AlignTest alignTest;
int* alignTestIntOneLocale = &alignTest.intOne;
int* alignTestIntTwoLocale = &alignTest.intTwo;
int* alignTestIntThreeLocale = &alignTest.intThree;
writeln(cast(long)testInt2Locale - cast(long)testInt1Locale);
writeln(cast(long)testInt3Locale - cast(long)testInt2Locale);
writeln(cast(long)testLong2Locale - cast(long)testLong1Locale);
writeln(cast(long)testLong3Locale - cast(long)testLong2Locale);
writeln(cast(long)alignTestIntTwoLocale -
cast(long)alignTestIntOneLocale);
writeln(cast(long)alignTestIntThreeLocale -
cast(long)alignTestIntTwoLocale);
return 0;
}
[/code]
The above code, compiled with dmd for and on 64-bit linux prints
the following:
[quote]
4 //Ints 1 & 2 are 4 bytes apart, dispite specifying them to be 8
bytes apart.
4 //Ints 2 & 3 are 4 bytes apart, dispite specifying them to be 8
bytes apart.
8 //Longs 1 & 2 are 8 bytes apart.
8 //Longs 2 & 3 are 8 bytes apart.
8 //Struct's ints 1 & 2 are 8 bytes apart - specified by align
attribute.
8 //Struct's ints 2 & 3 are 8 bytes apart - specified by align
attribute.
[/quote]
So, short-term, it seems like one would want to use my
"native/unative" technique. But long-term, hopefully not only
does this get fixed, but the default behavior for the compiler be
to pad things out to the native word size without having to
specify alignment.
Note: I tried "auto" - they come out as ints on my 64-bit
architecture, still separated by 4 bytes.
Even with size_t and ptrdiff_t, I would still want to make my own
aliases "native" and "unative" because size_t is intended for
array indexes (from what I saw when I briefly read about them)
and "native" and "unative" sound more generic and readable - to
me at least - and follows the "u" naming convention.
Stay efficeint,
Jake
More information about the Digitalmars-d-learn
mailing list