The impl by Deepseek of add_overflow, subtract_overflow and multiply_overflow is list below. I have not generated the code for the two int32 routines, because I'm a bit confused on its meaning. Here is the impl by Deepseek of add_overflow ---------------------------------------------------------------------------------------------------------- #include <stdio.h> #include <stdbool.h> #include <stdint.h> #include <limits.h> typedef int64_t s7_int; // Assume s7_int is int64_t // Generic overflow check for addition (for s7_int type) static bool add_overflow(s7_int A, s7_int B, s7_int *C) { *C = A + B; // Overflow condition: If A and B have the same sign, and the result has a different sign than A and B, overflow occurs return ((A ^ B) >= 0) && ((A ^ *C) < 0); } // Test cases int main() { s7_int A, B, C; bool overflow; // Test case 1: Positive overflow A = INT64_MAX; B = 1; overflow = add_overflow(A, B, &C); printf("Test 1: A = %lld, B = %lld\n", A, B); printf("Result: C = %lld, Overflow: %s\n\n", C, overflow ? "true" : "false"); // Test case 2: Negative overflow A = INT64_MIN; B = -1; overflow = add_overflow(A, B, &C); printf("Test 2: A = %lld, B = %lld\n", A, B); printf("Result: C = %lld, Overflow: %s\n\n", C, overflow ? "true" : "false"); // Test case 3: No overflow (positive numbers) A = 100; B = 200; overflow = add_overflow(A, B, &C); printf("Test 3: A = %lld, B = %lld\n", A, B); printf("Result: C = %lld, Overflow: %s\n\n", C, overflow ? "true" : "false"); // Test case 4: No overflow (different signs) A = 100; B = -50; overflow = add_overflow(A, B, &C); printf("Test 4: A = %lld, B = %lld\n", A, B); printf("Result: C = %lld, Overflow: %s\n\n", C, overflow ? "true" : "false"); // Test case 5: Edge case (zero) A = 0; B = 0; overflow = add_overflow(A, B, &C); printf("Test 5: A = %lld, B = %lld\n", A, B); printf("Result: C = %lld, Overflow: %s\n\n", C, overflow ? "true" : "false"); // Test case 6: Edge case (maximum positive number plus zero) A = INT64_MAX; B = 0; overflow = add_overflow(A, B, &C); printf("Test 6: A = %lld, B = %lld\n", A, B); printf("Result: C = %lld, Overflow: %s\n\n", C, overflow ? "true" : "false"); // Test case 7: Edge case (minimum negative number plus zero) A = INT64_MIN; B = 0; overflow = add_overflow(A, B, &C); printf("Test 7: A = %lld, B = %lld\n", A, B); printf("Result: C = %lld, Overflow: %s\n\n", C, overflow ? "true" : "false"); return 0; } Here is the impl of subtract_overflow by deepseek: ---------------------------------------------------------------------------------------------------------- #include <stdio.h> #include <stdbool.h> #include <stdint.h> #include <limits.h> typedef int64_t s7_int; // Assume s7_int is int64_t // Generic overflow check for addition (for s7_int type) static bool add_overflow(s7_int A, s7_int B, s7_int *C) { *C = A + B; // Overflow condition: If A and B have the same sign, and the result has a different sign than A and B, overflow occurs return ((A ^ B) >= 0) && ((A ^ *C) < 0); } // Test cases int main() { s7_int A, B, C; bool overflow; // Test case 1: Positive overflow A = INT64_MAX; B = 1; overflow = add_overflow(A, B, &C); printf("Test 1: A = %lld, B = %lld\n", A, B); printf("Result: C = %lld, Overflow: %s\n\n", C, overflow ? "true" : "false"); // Test case 2: Negative overflow A = INT64_MIN; B = -1; overflow = add_overflow(A, B, &C); printf("Test 2: A = %lld, B = %lld\n", A, B); printf("Result: C = %lld, Overflow: %s\n\n", C, overflow ? "true" : "false"); // Test case 3: No overflow (positive numbers) A = 100; B = 200; overflow = add_overflow(A, B, &C); printf("Test 3: A = %lld, B = %lld\n", A, B); printf("Result: C = %lld, Overflow: %s\n\n", C, overflow ? "true" : "false"); // Test case 4: No overflow (different signs) A = 100; B = -50; overflow = add_overflow(A, B, &C); printf("Test 4: A = %lld, B = %lld\n", A, B); printf("Result: C = %lld, Overflow: %s\n\n", C, overflow ? "true" : "false"); // Test case 5: Edge case (zero) A = 0; B = 0; overflow = add_overflow(A, B, &C); printf("Test 5: A = %lld, B = %lld\n", A, B); printf("Result: C = %lld, Overflow: %s\n\n", C, overflow ? "true" : "false"); // Test case 6: Edge case (maximum positive number plus zero) A = INT64_MAX; B = 0; overflow = add_overflow(A, B, &C); printf("Test 6: A = %lld, B = %lld\n", A, B); printf("Result: C = %lld, Overflow: %s\n\n", C, overflow ? "true" : "false"); // Test case 7: Edge case (minimum negative number plus zero) A = INT64_MIN; B = 0; overflow = add_overflow(A, B, &C); printf("Test 7: A = %lld, B = %lld\n", A, B); printf("Result: C = %lld, Overflow: %s\n\n", C, overflow ? "true" : "false"); return 0; } Here is the impl of multiply_overflow by deepseek: ---------------------------------------------------------------------------------------------------------- #include <stdio.h> #include <stdbool.h> #include <stdint.h> // Function to check for overflow during multiplication of two int64_t values static bool multiply_overflow(int64_t a, int64_t b, int64_t *result) { // Perform the multiplication *result = a * b; // Check for overflow if (a > 0 && b > 0) { // If both numbers are positive, the result should be positive return *result < 0; } else if (a < 0 && b < 0) { // If both numbers are negative, the result should be positive return *result < 0; } else if (a > 0 && b < 0) { // If one is positive and the other is negative, the result should be negative return *result > 0; } else if (a < 0 && b > 0) { // If one is negative and the other is positive, the result should be negative return *result > 0; } else { // If one of the numbers is zero, no overflow can occur return false; } } int main() { int64_t result; bool overflow; // Test case 1: No overflow overflow = multiply_overflow(100, 200, &result); printf("100 * 200 = %ld, Overflow: %s\n", result, overflow ? "true" : "false"); // Test case 2: Overflow (positive numbers) overflow = multiply_overflow(INT64_MAX, 2, &result); printf("INT64_MAX * 2 = %ld, Overflow: %s\n", result, overflow ? "true" : "false"); // Test case 3: Overflow (negative numbers) overflow = multiply_overflow(INT64_MIN, 2, &result); printf("INT64_MIN * 2 = %ld, Overflow: %s\n", result, overflow ? "true" : "false"); // Test case 4: No overflow (one positive, one negative) overflow = multiply_overflow(100, -200, &result); printf("100 * -200 = %ld, Overflow: %s\n", result, overflow ? "true" : "false"); // Test case 5: No overflow (one zero) overflow = multiply_overflow(0, 200, &result); printf("0 * 200 = %ld, Overflow: %s\n", result, overflow ? "true" : "false"); return 0; } ------------------------------------------------------------------ From:bil <b...@ccrma.stanford.edu> Send Time:2024 Dec. 31 (Tue.) 02:47 To:"沈达"<d...@liii.pro> Cc:cmdist<cmdist@ccrma.Stanford.EDU> Subject:Re: [CM] Failed to build S7 Scheme using MSVC when HAVE_OVERFLOW_CHECKS is enabled To implement those functions I need the equivalent of gcc's __builtin_add_overflow and friends. The MSVC versions appear to be restricted to unsigned ints, and only work on the x86 chips. I'm very leery of any code Claude or its competitors turns out. What do they suggest?
_______________________________________________ Cmdist mailing list Cmdist@ccrma.stanford.edu https://cm-mail.stanford.edu/mailman/listinfo/cmdist