OSDN Git Service

[Refactor] #37353 コメント整理。 / Refactor comments.
[hengband/hengband.git] / src / z-util.c
index d2830d0..a89c55a 100644 (file)
@@ -1,4 +1,4 @@
-/* File: z-util.c */
+/* File: z-util.c */
 
 /*
  * Copyright (c) 1997 Ben Harrison
 /*
  * Convenient storage of the program name
  */
-cptr argv0 = NULL;
+concptr argv0 = NULL;
 
 
 /*
  * Determine if string "t" is equal to string "t"
  */
-bool streq(cptr a, cptr b)
+bool streq(concptr a, concptr b)
 {
        return (!strcmp(a, b));
 }
@@ -31,7 +31,7 @@ bool streq(cptr a, cptr b)
 /*
  * Determine if string "t" is a suffix of string "s"
  */
-bool suffix(cptr s, cptr t)
+bool suffix(concptr s, concptr t)
 {
        int tlen = strlen(t);
        int slen = strlen(s);
@@ -47,7 +47,7 @@ bool suffix(cptr s, cptr t)
 /*
  * Determine if string "t" is a prefix of string "s"
  */
-bool prefix(cptr s, cptr t)
+bool prefix(concptr s, concptr t)
 {
        /* Scan "t" */
        while (*t)
@@ -65,13 +65,13 @@ bool prefix(cptr s, cptr t)
 /*
  * Redefinable "plog" action
  */
-void (*plog_aux)(cptr) = NULL;
+void (*plog_aux)(concptr) = NULL;
 
 /*
  * Print (or log) a "warning" message (ala "perror()")
  * Note the use of the (optional) "plog_aux" hook.
  */
-void plog(cptr str)
+void plog(concptr str)
 {
        /* Use the "alternative" function if possible */
        if (plog_aux) (*plog_aux)(str);
@@ -85,7 +85,7 @@ void plog(cptr str)
 /*
  * Redefinable "quit" action
  */
-void (*quit_aux)(cptr) = NULL;
+void (*quit_aux)(concptr) = NULL;
 
 /*
  * Exit (ala "exit()").  If 'str' is NULL, do "exit(0)".
@@ -93,7 +93,7 @@ void (*quit_aux)(cptr) = NULL;
  * Otherwise, plog() 'str' and exit with an error code of -1.
  * But always use 'quit_aux', if set, before anything else.
  */
-void quit(cptr str)
+void quit(concptr str)
 {
        /* Attempt to use the aux function */
        if (quit_aux) (*quit_aux)(str);
@@ -116,13 +116,13 @@ void quit(cptr str)
 /*
  * Redefinable "core" action
  */
-void (*core_aux)(cptr) = NULL;
+void (*core_aux)(concptr) = NULL;
 
 /*
  * Dump a core file, after printing a warning message
  * As with "quit()", try to use the "core_aux()" hook first.
  */
-void core(cptr str)
+void core(concptr str)
 {
        char *crash = NULL;
 
@@ -229,7 +229,7 @@ void s64b_div(s32b *A1, u32b *A2, s32b B1, u32b B2)
         */
        while (s64b_cmp(A1val, A2val, B1, B2) == 1)
        {
-               s64b_LSHIFT(B1, B2);
+               s64b_LSHIFT(B1, B2, 1);
                bit++;
        }
 
@@ -246,7 +246,7 @@ void s64b_div(s32b *A1, u32b *A2, s32b B1, u32b B2)
                        s64b_sub(&A1val, &A2val, B1, B2);
                }
        
-               s64b_RSHIFT(B1, B2);
+               s64b_RSHIFT(B1, B2, 1);
                bit--;
        }
 
@@ -266,3 +266,61 @@ void s64b_mod(s32b *A1, u32b *A2, s32b B1, u32b B2)
        s64b_sub(A1, A2, tmp1, tmp2);
 }
 
+/*!
+* @brief 符号なし32ビット整数のビット数を返す。
+* @param x ビット数を調べたい変数
+* @return ビット数
+*/
+int count_bits(BIT_FLAGS x)
+{
+       int n = 0;
+
+       if (x) do
+       {
+               n++;
+       } while (0 != (x = x&(x - 1)));
+
+       return (n);
+}
+
+/*!
+ * @brief 平方根を切り捨て整数で返す
+ * @param n 数値
+ * @return 平方根
+ */
+int mysqrt(int n)
+{
+       int tmp = n >> 1;
+       int tasu = 10;
+       int kaeriti = 1;
+
+       if (!tmp)
+       {
+               if (n) return 1;
+               else return 0;
+       }
+
+       while (tmp)
+       {
+               if ((n / tmp) < tmp)
+               {
+                       tmp >>= 1;
+               }
+               else break;
+       }
+       kaeriti = tmp;
+       while (tasu)
+       {
+               if ((n / tmp) < tmp)
+               {
+                       tasu--;
+                       tmp = kaeriti;
+               }
+               else
+               {
+                       kaeriti = tmp;
+                       tmp += tasu;
+               }
+       }
+       return kaeriti;
+}
\ No newline at end of file