NO

Author Topic: How can I test a numeric string overflow?  (Read 608 times)

Offline John Z

  • Member
  • *
  • Posts: 51
Re: How can I test a numeric string overflow?
« Reply #15 on: May 06, 2020, 02:19:06 am »
For 64 bits the C short version is already optimized.
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 ;)

Offline PaoloC13

  • Member
  • *
  • Posts: 36
Re: How can I test a numeric string overflow?
« Reply #16 on: May 06, 2020, 03:15:47 pm »
/* frankie, you are a true source of hight performance code. :) My asm is rusty, but I saved your nice solution. */

Knowing that only 10 digit inputs need detailed checking ( >10 is overflow, <10 is no overflow)

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).

Offline John Z

  • Member
  • *
  • Posts: 51
Re: How can I test a numeric string overflow?
« Reply #17 on: May 06, 2020, 06:00:33 pm »
OK I get it - Thanks