Re: use of bitwise arithmetic in GEGL code

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxxxxxxxxxx
https://lists.XCF.Berkeley.EDU/mailman/listinfo/gegl-developer

[Index of Archives]     [Yosemite News]     [Yosemite Photos]     [gtk]     [GIMP Users]     [KDE]     [Gimp's Home]     [Gimp on Windows]     [Steve's Art]

  Powered by Linux