[prev in list] [next in list] [prev in thread] [next in thread] 

List:       koffice-devel
Subject:    Re: Portable Code (am I dreaming? :))
From:       Nicolas Goutte <nicog () snafu ! de>
Date:       2002-01-24 12:43:01
[Download RAW message or body]

On Thursday 24 January 2002 12:22, Clarence Dang wrote:
> On Wed, 23 Jan 2002 13:39, shaheed wrote:
> > Clarence,
> > 3. Don't worry about the direction of bit shifts for the reason Nicolas
> > gave, and don't worry about the performance of bit shifts since the
> > compiler will almost certainly put the original value in a register.
>
> It's not the performance of bit shifts that I'm worried about but it's the
> overhead of function calls. e.g. if I had:
>
> class SOME_IMPORTANT_STRUCTURE
> {
> public:
> 	short a;
> 	char b;
> 	long c;
> }
>
> Currently, I can just make one function call (fread) and read in the whole
> structure. But if I'm going to make it portable, I would have to make 3
> function calls (to read in a, then b, then c). Imagine a structure with 15
> variables :(
> n number of function calls (even inline ones) cannot beat 1 function call
> in terms of speed (n > 1). But I guess this is unavoidable anyway.

Well, you are not in a critical path of KWord. Sure, it is annoying to wait 
that the file is read, but I think that the user would prefer that it is done 
correctly.

I would not say the same if you code was called each time that you type a 
character in KWord. ;-)

>
> btw, If I write "fread (buffer, 4, 1, filepointer)", will this always read
> in a long int "correctly" or will I get endian issues? And also, what is

You will have two issues:
- big/little endian
- a long int can be 64 bits wide.

> the difference between:
>
> fread (buffer, 4, 1, filepointer);
> fread (buffer, 2, 2, filepointer);
> fread (buffer, 1, 4, filepointer);

Sorry, I have no idea. Probably, you will get the same.

>
> Are they all the same or can they give different output?
>
> > The
> > current msword code does some of this, and a typical routine looks like
> > this:
> >
> > unsigned MsWordGenerated::read(const U8 *in, __UNAL SHD *out, unsigned
> > count) {
> >     U32 shifterU32;
> >     U16 shifterU16;
> >     U8 shifterU8;
> >     U8 *ptr;
> >     unsigned bytes = 0;
> >
> >     ptr = (U8 *)out;
> >     shifterU32 = shifterU16 = shifterU8 = 0;
> >     for (unsigned i = 0; i < count; i++)
> >     {
> >         bytes += read(in + bytes, &shifterU16);
> >         out->icoFore = shifterU16;
> >         shifterU16 >>= 5;
> >         out->icoBack = shifterU16;
> >         shifterU16 >>= 5;
> >         out->ipat = shifterU16;
> >         shifterU16 >>= 6;
> >         out++;
> >     }
> >     return bytes;
> > } // SHD
> >
> > Note that the _UNAL will disappear as soon as I get round to eliminating
> > a few sizeof()s used by the current code. In the example, I have an
> > overloaded read() function for the different primitive types.
>
> Can all the packing/alignment business be avoided if I get rid of sizeof()s
> and instead have #define <structure name>_SIZEOF everywhere instead?

No, you would get exactly the same problem! It is not a problem of the size 
of the data, it is how the data is represented internally and in your file.

>
> > Finally, if you can, I heartily recommend a code generator...that way, if
> > you get the implementation wrong, you can at least change it.
>
> A code generator?  Do you mean generating data structures from a data
> format specification automatically? Or do you mean something else?  I mean
> a code generator sounds like a great way to reduce work :)

I leave this question to Shaheed.

>
> Thanks!
> Clarence

Have a nice day/evening/night!

_______________________________________________
koffice-devel mailing list
koffice-devel@mail.kde.org
http://mail.kde.org/mailman/listinfo/koffice-devel
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic