On Monday, 17 de October de 2011 16.20.41, André Pönitz wrote:
> IMNSHO anything that's called a "proper solution" has compile to 
> at most four instructions in the inner loop in code like 
> 
>    Container<int> c(100);
>    for (int i = 0; i != 100; ++i)
>       c[i] = 42;
> 
> (including the jump) at on x86 and ARM. That's what the naive way
> to write the loop produces with gcc for std::vector. There's no need
> to provide anything that's worse.

Right.

In theory, the compiler could realise that the part doing the tests is 
constant and hoist it out of the loop.

In practice, it doesn't. Which is another reason to avoid using the sign bit 
of the ints.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel Open Source Technology Center
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
struct ContainerData
{
    int ref;
    int begin;
    int size;
    int flags;

    bool isRawData() const { return flags & 1; }
};

template <typename T>
class Container
{
    struct RawData : public ContainerData {
        T *data;
    };
    struct OwnData : public ContainerData {
        T array[1];
    };
    union {
        ContainerData *d;
        OwnData *p;
        RawData *r;
    };
public:
    Container(int size);

    T *data()
    {
        if (d->isRawData()) return r->data;
        return p->array + d->begin;
    }

    T &operator[](int index) { return data()[index]; }
};

int main()
{
    Container<int> c(100);
    asm (" ## MARKER ##");
    for (int i = 0; i != 100; ++i)
        c[i] = 42;
}

Attachment: signature.asc
Description: This is a digitally signed message part.

_______________________________________________
Qt5-feedback mailing list
[email protected]
http://lists.qt.nokia.com/mailman/listinfo/qt5-feedback

Reply via email to