www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Alignments, semantics from asserts, auto-count

reply bearophile <bearophileHUGS lycos.com> writes:
This comes partially from a discussion in the #ldc channel.
This post is for Don too.

The following program compiles and runs, the sum is done by
__arraySliceSliceAddSliceAssign_f:

float[4] a1;
void main() {
    float[4] a2;
    auto a3 = new float[4];
    a3[] = a1[] + a2[];
}

I think a2 is always aligned to 16 bytes, but are a1 and a2 too aligned to 16
bytes?

Such alignment is very important for the current CPUs (in future it will become
less important), to allow such a1[]+a2[] to be done with just 1 instruction
(addps). I'd like to see such thing implemented in the very good LDC compiler.

The following code doesn't compile, so there's no way to be sure a1 and a2 are
aligned to 16 bytes, but I may like to have a way to add such extra semantics:

align(16) float[4] a1;
void main() {
    align(16) float[4] a2;
    auto a3 = new float[4];
    a3[] = a1[] + a2[];
}

-------------------------

The following function performs a very common operation (sorry for the funny
alignment of braces, I have not found a better one yet):

float calc1(float[] v1, float[] v2)
    in {
        assert(v1.ptr % 16 == 0); // default for larghish D arrays
        assert(v2.ptr % 16 == 0); // default for larghish D arrays
        assert(v1.length == v2.length);
        assert(v1.length >= 4);
        assert(v1.length % 4 == 0);
    }            
    
    body {
        float tot = 0;
        for (uint i; i < v1.length; i++)
            tot += v1[i] * v2[i];
        return tot;
}

Such in{...} (pre contract) allows to give more semantics to the LLVM backend,
and may eventually allow to paralellize that code (with no extra operations
done on the pointer to align it and to be sure all items are multiplied), with
SSE2+, in a clean and short way. Such vectorization is able to speed up code 4+
times, when the situation is fit, for me 4X performance isn't a small thing.

What I may like is the front-end (DMD too) to be able to understand the meaning
of some of such basic asserts, so such semantics can later be offered to LLVM
(or other backends, for example GCC has an annotation that asserts a pointer is
aligned to N bytes, so it's "just" a matter of understanding
assert(v1.ptr%16==0) ).

-------------------------

This is unrelated, but I need D to become more handy:

If I don't put a number there arr becomes a dynamic array, so if I want a
static array I must count items first. Even C is more handy here. Can some
auto-guessing syntax be added, like this?

int[$] arr = [6,5,-1,-1,-3,-4,4,-3,1,3,0,-4,6,-3,3,4,-4];
int[?] arr = [6,5,-1,-1,-3,-4,4,-3,1,3,0,-4,6,-3,3,4,-4];

Thank you.

Bye,
bearophile
Jul 31 2009
parent reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
 -------------------------
 
 This is unrelated, but I need D to become more handy:
 
 If I don't put a number there arr becomes a dynamic array, so if I want a
static array I must count items first. Even C is more handy here. Can some
auto-guessing syntax be added, like this?
 
 int[$] arr = [6,5,-1,-1,-3,-4,4,-3,1,3,0,-4,6,-3,3,4,-4];
 int[?] arr = [6,5,-1,-1,-3,-4,4,-3,1,3,0,-4,6,-3,3,4,-4];
 
 Thank you.
 
 Bye,
 bearophile
What's wrong with auto arr = [6,5,-1,-1,-3,-4,4,-3,1,3,0,-4,6,-3,3,4,-4]; ?
Jul 31 2009
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
Ellery Newcomer wrote:
 -------------------------

 This is unrelated, but I need D to become more handy:

 If I don't put a number there arr becomes a dynamic array, so if I want a
static array I must count items first. Even C is more handy here. Can some
auto-guessing syntax be added, like this?

 int[$] arr = [6,5,-1,-1,-3,-4,4,-3,1,3,0,-4,6,-3,3,4,-4];
 int[?] arr = [6,5,-1,-1,-3,-4,4,-3,1,3,0,-4,6,-3,3,4,-4];

 Thank you.

 Bye,
 bearophile
What's wrong with auto arr = [6,5,-1,-1,-3,-4,4,-3,1,3,0,-4,6,-3,3,4,-4]; ?
Well we've concluded a while ago that it's vastly better for everyone if the automatic type of array literals is T[], not T[n]. So you need to declare the size, which is redundant, hence OP's request. Andrei
Jul 31 2009
parent Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
Andrei Alexandrescu wrote:
 Ellery Newcomer wrote:
 -------------------------

 This is unrelated, but I need D to become more handy:

 If I don't put a number there arr becomes a dynamic array, so if I
 want a static array I must count items first. Even C is more handy
 here. Can some auto-guessing syntax be added, like this?

 int[$] arr = [6,5,-1,-1,-3,-4,4,-3,1,3,0,-4,6,-3,3,4,-4];
 int[?] arr = [6,5,-1,-1,-3,-4,4,-3,1,3,0,-4,6,-3,3,4,-4];

 Thank you.

 Bye,
 bearophile
What's wrong with auto arr = [6,5,-1,-1,-3,-4,4,-3,1,3,0,-4,6,-3,3,4,-4]; ?
Well we've concluded a while ago that it's vastly better for everyone if the automatic type of array literals is T[], not T[n].
Ah. I didn't think it would happen. I can't wait!
Jul 31 2009