use of bitwise arithmetic in GEGL code

ForumsFor GEGL developers (read-only) ► use of bitwise arithmetic in GEGL code

Sent: 2009-08-06 21:00:27 UTC (over 2 years ago)

From: Nicolas Robidoux

use of bitwise arithmetic in GEGL code

Is the use of such tricks discouraged for GEGL (because gfloats could
be doubles at some point?)

We can take the absolute value by setting the sign bit to zero:
// Example 13.24
float f;
*(int*)&f &= 0x7FFFFFFF; // set sign bit to zero

(From http://www.agner.org/optimize/optimizing_cpp.pdf)

Nicolas Robidoux
Universite Laurentienne

_______________________________________________
Gegl-developer mailing list
Gegl-developer@lists.XCF.Berkeley.EDU
https://lists.XCF.Berkeley.EDU/mailman/listinfo/gegl-developer

Sent: 2009-08-06 21:03:19 UTC (over 2 years ago)

From: Martin Nordholts

use of bitwise arithmetic in GEGL code

On 08/06/2009 09:00 PM, Nicolas Robidoux wrote:
> Is the use of such tricks discouraged for GEGL (because gfloats could
> be doubles at some point?)
>
> We can take the absolute value by setting the sign bit to zero:
> // Example 13.24
> float f;
> *(int*)&f&= 0x7FFFFFFF; // set sign bit to zero

Why not use standard library functions?

/ Martin

--

My GIMP Blog:
http://www.chromecode.com/
_______________________________________________
Gegl-developer mailing list
Gegl-developer@lists.XCF.Berkeley.EDU
https://lists.XCF.Berkeley.EDU/mailman/listinfo/gegl-developer

Sent: 2009-08-06 21:23:50 UTC (over 2 years ago)

From: Martin Nordholts

use of bitwise arithmetic in GEGL code

On 08/06/2009 09:00 PM, Nicolas Robidoux wrote:
> Is the use of such tricks discouraged for GEGL (because gfloats could
> be doubles at some point?)
>
> We can take the absolute value by setting the sign bit to zero:
> // Example 13.24
> float f;
> *(int*)&f&= 0x7FFFFFFF; // set sign bit to zero
>
> (From http://www.agner.org/optimize/optimizing_cpp.pdf)

It was an interesting paper, but I doubt that the gained speed in
execution performance outweights the messier code. Plus, his example was
to set the sign bit, i.e. x = -abs(x). Maybe x = abs(x) is equal to the
above for optimizing compilers

/ Martin

--

My GIMP Blog:
http://www.chromecode.com/
_______________________________________________
Gegl-developer mailing list
Gegl-developer@lists.XCF.Berkeley.EDU
https://lists.XCF.Berkeley.EDU/mailman/listinfo/gegl-developer

Sent: 2009-08-06 22:02:06 UTC (over 2 years ago)

From: Nicolas Robidoux

use of bitwise arithmetic in GEGL code

Michael:

> I doubt that the gained speed in execution performance outweights
> the messier code.

The bit twiddling absolute value was just an example. It's a
branch-free float minmod we're actually after.

Do I gather that it may be OK to use such bit twiddling provided the
speed gain is sufficient to justify code opacity?

Nicolas Robidoux

PS

Really, vector operations are where speed is to be found. But
implementation is a bit more involved than the occasional bit twiddle.

_______________________________________________
Gegl-developer mailing list
Gegl-developer@lists.XCF.Berkeley.EDU
https://lists.XCF.Berkeley.EDU/mailman/listinfo/gegl-developer

Sent: 2009-08-06 22:20:02 UTC (over 2 years ago)

From: Martin Nordholts

use of bitwise arithmetic in GEGL code

On 08/06/2009 10:02 PM, Nicolas Robidoux wrote:
> Michael:
>
>> I doubt that the gained speed in execution performance outweights
>> the messier code.
>
> The bit twiddling absolute value was just an example. It's a
> branch-free float minmod we're actually after.
>
> Do I gather that it may be OK to use such bit twiddling provided the
> speed gain is sufficient to justify code opacity?

If the code is unlikely to require changes later, then I'm fine with
these kind of optimizations

/ Martin

--

My GIMP Blog:
http://www.chromecode.com/
_______________________________________________
Gegl-developer mailing list
Gegl-developer@lists.XCF.Berkeley.EDU
https://lists.XCF.Berkeley.EDU/mailman/listinfo/gegl-developer

Sent: 2009-08-06 22:34:15 UTC (over 2 years ago)

From: Nathan Summers

use of bitwise arithmetic in GEGL code

On Thu, Aug 6, 2009 at 3:00 PM, Nicolas Robidoux
wrote:
>
> Is the use of such tricks discouraged for GEGL (because gfloats could
> be doubles at some point?)
>
> We can take the absolute value by setting the sign bit to zero:
> // Example 13.24
> float f;
> *(int*)&f &= 0x7FFFFFFF; // set sign bit to zero

Doesn't this violate pointer aliasing rules?

Rockwalrus

_______________________________________________
Gegl-developer mailing list
Gegl-developer@lists.XCF.Berkeley.EDU
https://lists.XCF.Berkeley.EDU/mailman/listinfo/gegl-developer

Sent: 2009-08-06 22:39:09 UTC (over 2 years ago)

From: Nicolas Robidoux

use of bitwise arithmetic in GEGL code

Ralf Meyer (another Laurentian prof) did some quick and dirty
experimentation with preliminary versions of bit-twiddling minmods and
found that on PowerPC chips it made a big difference. Not on Intel.

Not really surprising: Avoiding branches like the plague is more
relevant to PowerPC/cell than Intel.

In GEGL (production) code? Not sure.

Nicolas Robidoux

_______________________________________________
Gegl-developer mailing list
Gegl-developer@lists.XCF.Berkeley.EDU
https://lists.XCF.Berkeley.EDU/mailman/listinfo/gegl-developer

Sent: 2009-08-08 15:31:24 UTC (over 2 years ago)

From: Nicolas Robidoux

use of bitwise arithmetic in GEGL code

Nathan Summers writes:
> On Thu, Aug 6, 2009 at 3:00 PM, Nicolas Robidoux wrote
> >
> > Is the use of such tricks discouraged for GEGL (because gfloats could
> > be doubles at some point?)
> >
> > We can take the absolute value by setting the sign bit to zero:
> > // Example 13.24
> > float f;
> > *(int*)&f &= 0x7FFFFFFF; // set sign bit to zero
>
> Doesn't this violate pointer aliasing rules?

Indeed.

Consequence of your observation: The next edition of Agner Fog's
online optimization manuals

http://www.agner.org/optimize/#manuals

will contain an updated version of the above example:

union {
float f;
int i;
} u;
u.i &= 0x7FFFFFFF; // set sign bit to zero

and the following caveat:

In general, it is faster to access a floating point variable as an
integer if it is stored in memory, but not if it is a register
variable. The union forces the variable to be stored in memory, at
least temporarily. Using the methods in the above examples will
therefore be a disadvantage if other nearby parts of the code could
benefit from using registers for the same variables. Using type
casting of pointers instead of unions in the above examples may not
work on compilers that rely on the strict aliasing rule of standard
C, specifying that pointers of different types cannot point to the
same object, except for char pointers.

Nicolas Robidoux
Laurentian University

_______________________________________________
Gegl-developer mailing list
Gegl-developer@lists.XCF.Berkeley.EDU
https://lists.XCF.Berkeley.EDU/mailman/listinfo/gegl-developer

Welcome!


Lost password?

Not a member? Sign up!

Random tutorials | Latest tutorials

  1. Hot Wallpaper with Flames Hot Wallpaper with Flames 52
  2. Creating an abstract chaos explosion Creating an abstract chaos explosion 10
  3. Create a simple Grunge-Stamp! Create a simple Grunge-Stamp! 7
  4. Ashes to ashes, dust to dust. Dissolve text into particles Ashes to ashes, dust to dust. Dissolve text into particles 3

Latest comments

2.6.12 crashes on startup in windows xp. 2.6.11 works fine. (2 days ago in Last stable 2.6 release: 2.6.12 has arrived)

Thank you! You really helped! The tutorial went great for me! Tha... (5 days ago in [AVATAR] Become a real Na'Vi using GIMP!)

maybe you still selected a channel or so? explicitly click a layer ... (5 days ago in Create cool rifts with translucent lights!)

Poll

Is GIMP an adequate application for you to create printed graphics like flyers, advertisments etc?

View results

Latest forum activities

Your Ad Here

facts & numbers

gimpusers.com RSS feed

48 identi.ca followers
749 Twitter followers

powered by bitfire it services