Bitfield structs and suggestion
bearophile
bearophileHUGS at lycos.com
Sat Nov 24 13:32:03 PST 2007
0ffh:
> OTOH, the accessor templates from the D.learn thread look very nice.
I think they aren't good enough yet for practical usage, they do too much upcasting (to 64 bit numbers, in that thread I have shown it that using 32 bit makes those bitfields faster), so they require improvement. And they need more testing.
> I think the answer may in part depend on the result of the planned
> "GCC bitfields" vs. "GDC templates" shootout as planned on D.learn
> by bearophile.
> Hi bearophile, any results yet? :)
Hello, but sadly os far I haven't succed yet to install GDC (I'll try again), despite I already have MinGW installed. So I have tried with just the MinGW, the following:
#include <stdio.h>
#define N 25
typedef union {
unsigned int i;
struct {
unsigned int i;
unsigned int sign(void) { return (i >> 0x0) & 0x1; }
void sign(unsigned int val) { i = (i & 0xfffffffffffffffeLL) | ((val & 0x1) << 0x0); }
unsigned int biasedexponent(void) { return (i >> 0x1) & 0xff; }
void biasedexponent(unsigned int val) { i = (i & 0xfffffffffffffe01LL) | ((val & 0xff) << 0x1); }
unsigned int significand(void) { return (i >> 0x9) & 0x7fffff; }
void significand(unsigned int val) { i = (i & 0x1ff) | ((val & 0x7fffff) << 0x9); }
} bits;
} INTORFLOAT;
int main(void) {
int i, j;
unsigned long long tot = 0;
INTORFLOAT f;
unsigned int v[N] = {1628676761, 1620574103, 1237153253, 1098880307, 87513741,
13181925, 14686126, 7429435, 16286706, 6474381,
4879794, 7734725, 3745958, 13353858, 4236193,
7587, 4309, 28846, 7313, 14516,
126, 143, 171, 221, 156};
for(j = 0; j < 4000000; j++)
for(i = 0; i < N; i++) {
f.i = v[i];
f.bits.biasedexponent(f.bits.biasedexponent() / 2);
tot += f.bits.biasedexponent() + f.bits.sign();
}
printf("%d\n", tot);
}
It runs even a bit faster than the normal C bitfields ;-) So they may run fast on GDC too.
Bye,
bearophile
More information about the Digitalmars-d
mailing list