Skip to main content
edited body
Source Link
chux
  • 36.4k
  • 2
  • 43
  • 97

Why 45 in char str[ 45 ];?

bool overflow = false;
int digit = *us;
const unsigned char *start = us;
while (digit >= '0' && digit <= '9') { // or isdigit()
  // Will appending this digit overflow the result?
  if (val > INT128_MIN_DIV10 || (val >= INT128_MIN_DIV10 && digit <= -INT128_MIN_MOD10)) {
    val = val * 10 - digit;
  } else {
    overflow = true;
    val = INT128_MIN;
  }
  digit = *us++;
}

...

if (!neg) {
  if (val < -INT128_MAX) {
    overflow = true;
    val = INT128_MAX);INT128_MAX;
  }
  else {
    val = -val;
  }
}

I know, early on, it seems all that extra ornamentation makes for for code-bloat. In my experience a function that is robust in error handling is well worth the effort. Further, 128-bit values are optically long and can be far more useful in hex and/or with separators.

Why 45 in char str[ 45 ];

bool overflow = false;
int digit = *us;
const unsigned char *start = us;
while (digit >= '0' && digit <= '9') { // or isdigit()
  // Will appending this digit overflow the result?
  if (val > INT128_MIN_DIV10 || (val >= INT128_MIN_DIV10 && digit <= -INT128_MIN_MOD10)) {
    val = val * 10 - digit;
  } else {
    overflow = true;
    val = INT128_MIN;
  }
  digit = *us++;
}

...

if (!neg) {
  if (val < -INT128_MAX) {
    overflow = true;
    val = INT128_MAX);
  }
  else {
    val = -val;
  }
}

I know, early on, it seems all that extra ornamentation makes for for code-bloat. In my experience a function that is robust in error handling is well worth the effort. Further, 128-bit values are optically long and can be far more useful in hex and/or with separators.

Why 45 in char str[ 45 ];?

bool overflow = false;
int digit = *us;
const unsigned char *start = us;
while (digit >= '0' && digit <= '9') { // or isdigit()
  // Will appending this digit overflow the result?
  if (val > INT128_MIN_DIV10 || (val >= INT128_MIN_DIV10 && digit <= -INT128_MIN_MOD10)) {
    val = val * 10 - digit;
  } else {
    overflow = true;
    val = INT128_MIN;
  }
  digit = *us++;
}

...

if (!neg) {
  if (val < -INT128_MAX) {
    overflow = true;
    val = INT128_MAX;
  }
  else {
    val = -val;
  }
}

I know, early on, it seems all that extra ornamentation makes for code-bloat. In my experience a function that is robust in error handling is well worth the effort. Further, 128-bit values are optically long and can be far more useful in hex and/or with separators.

added 196 characters in body
Source Link
chux
  • 36.4k
  • 2
  • 43
  • 97

Documentation tip

Even when a function like strtol() has short comings, adding comments that say this int128_t to/from string function works:

just like strtol(), but for int128_t

conveys a lot of information to a user of your new function, making it more easy to use and reduces cognitive effort. Of course it then needs to perform jut like that strtol() too.

Good luck.

Documentation tip

Even when a function like strtol() has short comings, adding comments that say this int128_t to/from string function works:

just like strtol(), but for int128_t

conveys a lot of information to a user of your new function, making it more easy to use and reduces cognitive effort. Of course it then needs to perform jut like that strtol() too.

Good luck.

added 196 characters in body
Source Link
chux
  • 36.4k
  • 2
  • 43
  • 97

my_itoa(size_t n, char *s, int i, int base) which can serfeserve as a basis for my_int128toa(size_t n, char *s, __int128_t i, int base).


In general, int128 to/from string

Rather than re-write similar code, consider making more full-featured functions that handle other bases and perform robust error checking.

I know, early on, it seems all that extra ornamentation makes for for code-bloat. In my experience a function that is robust in error handling is well worth the effort. Further, 128-bit values are optically long and can be far more useful in hex and/or with separators.

Good engineering programming is about balance. Code what your need that functionally works for all input, include modest error checking, add a tad more functionality if simple to do so, but then pause and wait for a driving need to expanded functionality/performance.

my_itoa(size_t n, char *s, int i, int base) which can serfe as a basis for my_int128toa(size_t n, char *s, __int128_t i, int base).

my_itoa(size_t n, char *s, int i, int base) which can serve as a basis for my_int128toa(size_t n, char *s, __int128_t i, int base).


In general, int128 to/from string

Rather than re-write similar code, consider making more full-featured functions that handle other bases and perform robust error checking.

I know, early on, it seems all that extra ornamentation makes for for code-bloat. In my experience a function that is robust in error handling is well worth the effort. Further, 128-bit values are optically long and can be far more useful in hex and/or with separators.

Good engineering programming is about balance. Code what your need that functionally works for all input, include modest error checking, add a tad more functionality if simple to do so, but then pause and wait for a driving need to expanded functionality/performance.

added 196 characters in body
Source Link
chux
  • 36.4k
  • 2
  • 43
  • 97
Loading
added 184 characters in body
Source Link
chux
  • 36.4k
  • 2
  • 43
  • 97
Loading
added 478 characters in body
Source Link
chux
  • 36.4k
  • 2
  • 43
  • 97
Loading
added 478 characters in body
Source Link
chux
  • 36.4k
  • 2
  • 43
  • 97
Loading
added 15 characters in body
Source Link
chux
  • 36.4k
  • 2
  • 43
  • 97
Loading
Source Link
chux
  • 36.4k
  • 2
  • 43
  • 97
Loading