Thanks all! I decided to go with an implementation of David's algorithm below. No external calls, and completely unit testable in Visual C++.
If I get the time I will start a new thread "why I am not reporting my latest C compiler bug to IBM." Charles -----Original Message----- From: IBM Mainframe Discussion List [mailto:IBM-MAIN@LISTSERV.UA.EDU] On Behalf Of David Crayford Sent: Monday, July 22, 2013 2:57 AM To: IBM-MAIN@LISTSERV.UA.EDU Subject: Re: Looking for help with an obscure C integer problem I can see nothing obviously wrong with your code. Because the function is inlined there could be a subtle bug near where the function is called. Can you reproduce the problem with a simple test driver? FWIW, I implemented ffs64() like this. static inline int ffs64( uint64_t word ) { if ( word == 0 ) return 0; int bit = 1; if ( ( word & 0xFFFFFFFF ) == 0 ) { word >>= 32; bit += 32; } if ( ( word & 0xFFFF ) == 0 ) { word >>= 16; bit += 16; } if ( ( word & 0xFF ) == 0 ) { word >>= 8; bit += 8; } if ( ( word & 0xF ) == 0 ) { word >>= 4; bit += 4; } if ( ( word & 0x3 ) == 0 ) { word >>= 2; bit += 2; } if ( ( word & 0x1 ) == 0 ) { bit += 1; } return bit; } ---------------------------------------------------------------------- For IBM-MAIN subscribe / signoff / archive access instructions, send email to lists...@listserv.ua.edu with the message: INFO IBM-MAIN