+/*
+ * Divide A by B
+ *
+ * Assumes that both A and B are positive
+ */
+void s64b_div(s32b *A1, u32b *A2, s32b B1, u32b B2)
+{
+ s32b result1 = 0;
+ u32b result2 = 0;
+ s32b A1val = (*A1);
+ u32b A2val = (*A2);
+ int bit = 0;
+
+ /* No result for B==0 */
+ if (B1 == 0 && B2 == 0) return;
+
+ /*
+ * Find the highest bit of quotient
+ */
+ while (s64b_cmp(A1val, A2val, B1, B2) == 1)
+ {
+ s64b_LSHIFT(B1, B2, 1);
+ bit++;
+ }
+
+ /* Extract bits of quotient one by one */
+ while (bit >= 0)
+ {
+ if (s64b_cmp(A1val, A2val, B1, B2) >= 0)
+ {
+ if (bit >= 32)
+ result1 |= (0x00000001L << (bit - 32));
+ else
+ result2 |= (0x00000001L << bit);
+
+ s64b_sub(&A1val, &A2val, B1, B2);
+ }
+
+ s64b_RSHIFT(B1, B2, 1);
+ bit--;
+ }
+
+ (*A1) = result1;
+ (*A2) = result2;
+}
+
+
+/* Reminder of division (A % B) */
+void s64b_mod(s32b *A1, u32b *A2, s32b B1, u32b B2)
+{
+ s32b tmp1 = (*A1);
+ u32b tmp2 = (*A2);
+
+ s64b_div(&tmp1, &tmp2, B1, B2);
+ s64b_mul(&tmp1, &tmp2, B1, 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);
+}