Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- gcc test.c -o test -O2 -Wall -Werror -pipe -std=c11
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <limits.h>
- void
- swap(int32_t v1, int32_t v2)
- {
- v1 ^= v2;
- v2 ^= v1;
- v1 ^= v2;
- }
- int32_t
- satsum(int32_t v1, int32_t v2)
- {
- int32_t res;
- __builtin_sadd_overflow(v1, v2, &res);
- if(v1 >= 0 && v2 >= 0) {
- if(res < 0) {
- if(v1 < v2) {
- swap(v1, v2);
- }
- int32_t dif = v1 - v2;
- res = (res + dif) / 2 + 1;
- v1 -= res;
- return v1;
- } else {
- return res;
- }
- } else if(v1 < 0 && v2 < 0) {
- if(res >= 0) {
- if(v1 > v2) {
- swap(v1, v2);
- }
- int32_t dif = v2 - v1;
- res = (res - dif) / 2;
- v1 -= res;
- return v1;
- } else {
- return res;
- }
- } else {
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement