X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Flibgame%2Fmisc.c;h=3b447821aafe06315a5286fd63386ebbd8e36234;hb=cb97a0d81529dc81696e5df8e17083b30dfe633e;hp=3612ba045fcd649f9b4ec9a52dcf4b1762ad7f9d;hpb=3fd9eb037de275a9e65a712d7fbc0d95fe7b2747;p=rocksndiamonds.git diff --git a/src/libgame/misc.c b/src/libgame/misc.c index 3612ba04..3b447821 100644 --- a/src/libgame/misc.c +++ b/src/libgame/misc.c @@ -52,6 +52,7 @@ void printf_line(char *line_string, int line_length) fprintf_line(stdout, line_string, line_length); } + /* int2str() returns a number converted to a string; the used memory is static, but will be overwritten by later calls, so if you want to save the result, copy it to a private string buffer; @@ -82,6 +83,7 @@ char *int2str(int number, int size) } } + /* something similar to "int2str()" above, but allocates its own memory and has a different interface; we cannot use "itoa()", because this seems to be already defined when cross-compiling to the win32 target */ @@ -103,6 +105,23 @@ char *i_to_a(unsigned int i) } +/* calculate base-2 logarithm of argument (rounded down to integer; + this function returns the number of the highest bit set in argument) */ + +int log_2(unsigned int x) +{ + int e = 0; + + while ((1 << e) < x) + { + x -= (1 << e); /* for rounding down (rounding up: remove this line) */ + e++; + } + + return e; +} + + /* ------------------------------------------------------------------------- */ /* counter functions */ /* ------------------------------------------------------------------------- */