#include <stdio.h>
#include <stdint.h>
typedef uint64_t u64;
// 修改後的除法函數,返回商和餘數
void nvme_u64_div(u64 dividend, u64 divisor, u64* quotient, u64* remainder) {
*quotient = 0;
*remainder = 0;
int i;
for (i = 63; i >= 0; i--) {
*remainder = (*remainder << 1) | ((dividend >> i) & 1);
if (*remainder >= divisor) {
*remainder -= divisor;
*quotient |= (1ULL << i);
}
}
}
// 更新後的驗證函數,返回兩個計算結果
typedef struct {
int is_valid;
u64 calc_dividend;
u64 calc_remainder;
} VerificationResult;
VerificationResult verify_result(u64 dividend, u64 divisor, u64 quotient, u64 remainder) {
VerificationResult result;
result.calc_dividend = divisor * quotient + remainder;
result.calc_remainder = dividend % divisor;
result.is_valid = (result.calc_dividend == dividend) && (remainder < divisor);
return result;
}
// 測試案例結構
typedef struct {
u64 dividend;
u64 divisor;
u64 expected_quotient;
u64 expected_remainder;
} TestCase;
// 執行測試並打印結果
void run_test_case(TestCase test) {
u64 quotient, remainder;
nvme_u64_div(test.dividend, test.divisor, "ient, &remainder);
VerificationResult ver_result = verify_result(test.dividend, test.divisor, quotient, remainder);
printf("Dividend: %llu\n", test.
dividend); printf("Divisor: %llu\n", test.
divisor); printf("Expected Quotient: %llu\n", test.
expected_quotient); printf("Actual Quotient: %llu\n", quotient
); printf("Expected Remainder: %llu\n", test.
expected_remainder); printf("Actual Remainder: %llu\n", remainder
); printf("Verification Result:\n"); printf(" Calculated Dividend: %llu\n", ver_result.
calc_dividend); printf(" Calculated Remainder: %llu\n", ver_result.
calc_remainder); printf(" Is Valid: %s\n", ver_result.
is_valid ? "Yes" : "No"); printf("Test %s\n\n", (quotient
== test.
expected_quotient && remainder
== test.
expected_remainder && ver_result.
is_valid) ? "PASSED" : "FAILED"); }
int main() {
// 測試案例數組
TestCase test_cases[] = {
// 正常案例
{1000000000000000000ULL, 3ULL, 333333333333333333ULL, 1ULL},
{9223372036854775807ULL, 2ULL, 4611686018427387903ULL, 1ULL},
{18446744073709551615ULL, 9223372036854775807ULL, 2ULL, 1ULL},
// 邊界案例
{18446744073709551615ULL, 1ULL, 18446744073709551615ULL, 0ULL},
{18446744073709551615ULL, 18446744073709551615ULL, 1ULL, 0ULL},
{0ULL, 18446744073709551615ULL, 0ULL, 0ULL},
// 特殊案例
{18446744073709551614ULL, 2ULL, 9223372036854775807ULL, 0ULL},
{18446744073709551615ULL, 2ULL, 9223372036854775807ULL, 1ULL},
{18446744073709551615ULL, 3ULL, 6148914691236517205ULL, 0ULL},
// 質數案例
{18446744073709551557ULL, 11ULL, 1676976733973595596ULL, 1ULL},
{18446744073709551557ULL, 18446744073709551557ULL, 1ULL, 0ULL},
// 2的冪次方案例
{9223372036854775808ULL, 2ULL, 4611686018427387904ULL, 0ULL},
// 大數除以3^6案例
{18446744073709551615ULL, 729ULL, 25304175686842458ULL, 603ULL},
// 其他案例
{18446744073709551615ULL, 10ULL, 1844674407370955161ULL, 5ULL},
{18446744073709551615ULL, 9223372036854775806ULL, 2ULL, 3ULL},
{12345678901234567890ULL, 98765432109876543ULL, 125ULL, 0ULL},
{18446744073709551615ULL, 18446744073709551610ULL, 1ULL, 5ULL}
};
int num_tests = sizeof(test_cases) / sizeof(test_cases[0]);
for (int i = 0; i < num_tests; i++) {
printf("Test Case %d:\n", i
+ 1); run_test_case(test_cases[i]);
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRpbnQuaD4KCnR5cGVkZWYgdWludDY0X3QgdTY0OwoKLy8g5L+u5pS55b6M55qE6Zmk5rOV5Ye95pW477yM6L+U5Zue5ZWG5ZKM6aSY5pW4CnZvaWQgbnZtZV91NjRfZGl2KHU2NCBkaXZpZGVuZCwgdTY0IGRpdmlzb3IsIHU2NCogcXVvdGllbnQsIHU2NCogcmVtYWluZGVyKSB7CiAgICAqcXVvdGllbnQgPSAwOwogICAgKnJlbWFpbmRlciA9IDA7CiAgICBpbnQgaTsKICAgIGZvciAoaSA9IDYzOyBpID49IDA7IGktLSkgewogICAgICA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