[IPOL discuss] RGB->gray conversion / better (faster and portable?) integer formula

Nicolas Limare nicolas.limare at cmla.ens-cachan.fr
Fri Jul 29 19:54:27 CEST 2011


> > The meaning of >> and << is not related to the memory representation
> > of the numbers.
> 
> My doubt was about the use of the "&" operator, not the shifts.
> 
> I agree the shift operators << and >> are architecture-independent, but
> "&" isn't.

OK maybe, I can't make up my mind on this point. But... it doesn't
matter! My >>&<< formula was stupid and overkill, there is a much
simpler solution, without any binary AND. Instead of the x/n truncated
division, we just need to do (x+n/2)/n to achieve proper rounding!

    (x + (1<<14))>>15

and no more

    (x>>15) + ((x & (1<<14)) >> 14)

I think this formula is portable. I added it to the rgby benchmark and
its speed is better than the previous one.

-- 
Nicolas LIMARE - CMLA - ENS Cachan    http://www.cmla.ens-cachan.fr/~limare/
IPOL - image processing on line                          http://www.ipol.im/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://tools.ipol.im/mailman/archive/discuss/attachments/20110730/5c02713d/attachment.pgp>


More information about the discuss mailing list