C language > Expert questions

How can I test a numeric string overflow?

<< < (4/4)

John Z:

--- Quote from: frankie on May 05, 2020, 10:43:58 PM ---For 64 bits the C short version is already optimized.

--- End quote ---
True it is good, as well as clear, also easy to change for example to int16 checking.  But it could be better 'on average' over the set of all possible int32 10 digit inputs. Knowing that only 10 digit inputs need detailed checking ( >10 is overflow, <10 is no overflow) then we also can know that 8/9ths of all 10 digit inputs don't require the full number to be created to determine if an overflow will happen.  Only the first digit needs to be checked for a possible quick return. The while loop only needs to run if the first digit is a 4, if it is 5-9 overflow occurs, if 1-3 no overflow. So adding two if's nested before the while loop will speed up the check for 8/9ths of all possible inputs and slow it slightly for 1/9. The 5-9 if first returning overflow, else 1-3 if returning no overflow, else run the While loop to check.
If there was knowledge that most cases would not overflow I'd reverse the 1-3 check and 5-9 check.

I'm probably overthinking it - but hey shelter in place so kind of ,fun ;)

PaoloC13:
/* frankie, you are a true source of hight performance code. :) My asm is rusty, but I saved your nice solution. */


--- Quote from: John Z on May 06, 2020, 02:19:06 AM ---Knowing that only 10 digit inputs need detailed checking ( >10 is overflow, <10 is no overflow)
--- End quote ---

A numeric string can be "000000000123456", it's lenght overflows but the represented value doesn't.

My first horrific and verbose solution at the top of this discussion is about 2.8 time faster than the "uint64_t solution", if a fixed length and well formatted numeric string is assured. But for generic cases I need to use my _SKIP_LEADING_ZEROES(p) macro and then I need to measure the lenght of the numeric with my NumLenA inline function. It is not possible to know the length of a string at runtime without measure it, and this count brings a huge waste of time that makes my cumbersome function no more performing (depends on the input).

John Z:
OK I get it - Thanks

Navigation

[0] Message Index

[*] Previous page

Go to full version