Skip to content

Commit 2a38a0c

Browse files
committed
C front-end: Handle all __builtin_*_overflow variants in the type checker
The models in the C library incurred unnecessary overhead in that they would do the arithmetic operation twice: once for the actual (possibly overflowing) result, and once for determining the overflow bit.
1 parent 0d5c737 commit 2a38a0c

File tree

2 files changed

+18
-162
lines changed

2 files changed

+18
-162
lines changed

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3170,18 +3170,36 @@ exprt c_typecheck_baset::do_special_functions(
31703170
}
31713171
else if(
31723172
identifier == "__builtin_add_overflow" ||
3173+
identifier == "__builtin_sadd_overflow" ||
3174+
identifier == "__builtin_saddl_overflow" ||
3175+
identifier == "__builtin_saddll_overflow" ||
3176+
identifier == "__builtin_uadd_overflow" ||
3177+
identifier == "__builtin_uaddl_overflow" ||
3178+
identifier == "__builtin_uaddll_overflow" ||
31733179
identifier == "__builtin_add_overflow_p")
31743180
{
31753181
return typecheck_builtin_overflow(expr, ID_plus);
31763182
}
31773183
else if(
31783184
identifier == "__builtin_sub_overflow" ||
3185+
identifier == "__builtin_ssub_overflow" ||
3186+
identifier == "__builtin_ssubl_overflow" ||
3187+
identifier == "__builtin_ssubll_overflow" ||
3188+
identifier == "__builtin_usub_overflow" ||
3189+
identifier == "__builtin_usubl_overflow" ||
3190+
identifier == "__builtin_usubll_overflow" ||
31793191
identifier == "__builtin_sub_overflow_p")
31803192
{
31813193
return typecheck_builtin_overflow(expr, ID_minus);
31823194
}
31833195
else if(
31843196
identifier == "__builtin_mul_overflow" ||
3197+
identifier == "__builtin_smul_overflow" ||
3198+
identifier == "__builtin_smull_overflow" ||
3199+
identifier == "__builtin_smulll_overflow" ||
3200+
identifier == "__builtin_umul_overflow" ||
3201+
identifier == "__builtin_umull_overflow" ||
3202+
identifier == "__builtin_umulll_overflow" ||
31853203
identifier == "__builtin_mul_overflow_p")
31863204
{
31873205
return typecheck_builtin_overflow(expr, ID_mult);

src/ansi-c/library/math.c

Lines changed: 0 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -2297,165 +2297,3 @@ long double copysignl(long double x, long double y)
22972297
long double abs = fabsl(x);
22982298
return (signbit(y)) ? -abs : abs;
22992299
}
2300-
2301-
/* FUNCTION: __builtin_sadd_overflow */
2302-
2303-
_Bool __builtin_sadd_overflow(int a, int b, int *res)
2304-
{
2305-
*res = a + b;
2306-
return __CPROVER_overflow_plus(a, b);
2307-
}
2308-
2309-
/* FUNCTION: __builtin_saddl_overflow */
2310-
2311-
_Bool __builtin_saddl_overflow(long a, long b, long *res)
2312-
{
2313-
*res = a + b;
2314-
return __CPROVER_overflow_plus(a, b);
2315-
}
2316-
2317-
/* FUNCTION: __builtin_saddll_overflow */
2318-
2319-
_Bool __builtin_saddll_overflow(long long a, long long b, long long *res)
2320-
{
2321-
*res = a + b;
2322-
return __CPROVER_overflow_plus(a, b);
2323-
}
2324-
2325-
/* FUNCTION: __builtin_uadd_overflow */
2326-
2327-
_Bool __builtin_uadd_overflow(unsigned a, unsigned b, unsigned *res)
2328-
{
2329-
*res = a + b;
2330-
return __CPROVER_overflow_plus(a, b);
2331-
}
2332-
2333-
/* FUNCTION: __builtin_uaddl_overflow */
2334-
2335-
_Bool __builtin_uaddl_overflow(
2336-
unsigned long a,
2337-
unsigned long b,
2338-
unsigned long *res)
2339-
{
2340-
*res = a + b;
2341-
return __CPROVER_overflow_plus(a, b);
2342-
}
2343-
2344-
/* FUNCTION: __builtin_uaddll_overflow */
2345-
2346-
_Bool __builtin_uaddll_overflow(
2347-
unsigned long long a,
2348-
unsigned long long b,
2349-
unsigned long long *res)
2350-
{
2351-
*res = a + b;
2352-
return __CPROVER_overflow_plus(a, b);
2353-
}
2354-
2355-
/* FUNCTION: __builtin_ssub_overflow */
2356-
2357-
_Bool __builtin_ssub_overflow(int a, int b, int *res)
2358-
{
2359-
*res = a - b;
2360-
return __CPROVER_overflow_minus(a, b);
2361-
}
2362-
2363-
/* FUNCTION: __builtin_ssubl_overflow */
2364-
2365-
_Bool __builtin_ssubl_overflow(long a, long b, long *res)
2366-
{
2367-
*res = a - b;
2368-
return __CPROVER_overflow_minus(a, b);
2369-
}
2370-
2371-
/* FUNCTION: __builtin_ssubll_overflow */
2372-
2373-
_Bool __builtin_ssubll_overflow(long long a, long long b, long long *res)
2374-
{
2375-
*res = a - b;
2376-
return __CPROVER_overflow_minus(a, b);
2377-
}
2378-
2379-
/* FUNCTION: __builtin_usub_overflow */
2380-
2381-
_Bool __builtin_usub_overflow(unsigned a, unsigned b, unsigned *res)
2382-
{
2383-
*res = a - b;
2384-
return __CPROVER_overflow_minus(a, b);
2385-
}
2386-
2387-
/* FUNCTION: __builtin_usubl_overflow */
2388-
2389-
_Bool __builtin_usubl_overflow(
2390-
unsigned long a,
2391-
unsigned long b,
2392-
unsigned long *res)
2393-
{
2394-
*res = a - b;
2395-
return __CPROVER_overflow_minus(a, b);
2396-
}
2397-
2398-
/* FUNCTION: __builtin_usubll_overflow */
2399-
2400-
_Bool __builtin_usubll_overflow(
2401-
unsigned long long a,
2402-
unsigned long long b,
2403-
unsigned long long *res)
2404-
{
2405-
*res = a - b;
2406-
return __CPROVER_overflow_minus(a, b);
2407-
}
2408-
2409-
/* FUNCTION: __builtin_smul_overflow */
2410-
2411-
_Bool __builtin_smul_overflow(int a, int b, int *res)
2412-
{
2413-
*res = a * b;
2414-
return __CPROVER_overflow_mult(a, b);
2415-
}
2416-
2417-
/* FUNCTION: __builtin_smull_overflow */
2418-
2419-
_Bool __builtin_smull_overflow(long a, long b, long *res)
2420-
{
2421-
*res = a * b;
2422-
return __CPROVER_overflow_mult(a, b);
2423-
}
2424-
2425-
/* FUNCTION: __builtin_smulll_overflow */
2426-
2427-
_Bool __builtin_smulll_overflow(long long a, long long b, long long *res)
2428-
{
2429-
*res = a * b;
2430-
return __CPROVER_overflow_mult(a, b);
2431-
}
2432-
2433-
/* FUNCTION: __builtin_umul_overflow */
2434-
2435-
_Bool __builtin_umul_overflow(unsigned a, unsigned b, unsigned *res)
2436-
{
2437-
*res = a * b;
2438-
return __CPROVER_overflow_mult(a, b);
2439-
}
2440-
2441-
/* FUNCTION: __builtin_umull_overflow */
2442-
2443-
_Bool __builtin_umull_overflow(
2444-
unsigned long a,
2445-
unsigned long b,
2446-
unsigned long *res)
2447-
{
2448-
*res = a * b;
2449-
return __CPROVER_overflow_mult(a, b);
2450-
}
2451-
2452-
/* FUNCTION: __builtin_umulll_overflow */
2453-
2454-
_Bool __builtin_umulll_overflow(
2455-
unsigned long long a,
2456-
unsigned long long b,
2457-
unsigned long long *res)
2458-
{
2459-
*res = a * b;
2460-
return __CPROVER_overflow_mult(a, b);
2461-
}

0 commit comments

Comments
 (0)