Quote:Original post by joe_bubamara
Put your snippets into '[' code ']' and '[' /code ']' tags .... I have to add ' because of forum software, of course you use [ without '. Code tag will preserve tabs, spaces and other things. If you wish to have colorized syntax read the forum faq to see how it is used - I dislike that text area with colorized widget is so small so one sees only few lines of code at a time, so I almost never use it.
I tried to put the code into code/source formatters, but it had those issues. On second thought it also could have been the reply-preview that is buggy in this regard ...
I'll just try it again, regardless of how messed up the preview will look like
(it's not surprising that it's not very unlike the code the guy after my last post added to this thread).
inline bool StrToI32(const char * str, int32_t * number){ *number = 0; // just in case it wasn't initialized while (*str == ' ') ++str; int32_t sign = 1; char character; switch (*str) { case '-': sign = -1; // fall through case '+': ++str; // fall through default: character = *str; if ((uint32_t) (character - '0') <= 9U) // is character a number ? { int32_t lastvalue = 0, value = 0; do { value *= 10; value += character - '0'; if (lastvalue > value) { return false; // overflow } lastvalue = value; character = *(++str); } while ((uint32_t) (character - '0') <= 9U); *number = value * sign; return true; } break; } return false; // not found}
Quote:Original post by joe_bubamara
Yeah it is good question, where we should stop rolling own things instead of using libraries? :-). I believe that those conversion functions are done in optimized assembly, so that was reason why I am trying to use those rather then own thing.
cheers
It's probably optimized, but also you have to account for the function call itself, which normally will add about 60-100 cycles by itself, while a hand-rolled code could have the chance to be inlined (or forced to be) and integrate well into the code (with an additional advantage that registers can be reused and don't need to be push/popped from the stack each function call) :-)
Best to profile and see for yourself if it all makes sense in your particular piece of code.