]> git.proxmox.com Git - mirror_ovs.git/blame - lib/sat-math.h
ovsdb-idl: Fix iteration over tracked rows with no actual data.
[mirror_ovs.git] / lib / sat-math.h
CommitLineData
064af421 1/*
74142432 2 * Copyright (c) 2008, 2012, 2019 Nicira, Inc.
064af421 3 *
a14bc59f
BP
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
064af421 7 *
a14bc59f
BP
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
064af421
BP
15 */
16
17#ifndef SAT_MATH_H
18#define SAT_MATH_H 1
19
064af421 20#include <limits.h>
8c7be52d 21#include "openvswitch/util.h"
064af421 22
74142432
BP
23#ifndef __has_builtin
24#define __has_builtin(x) 0
25#endif
26
27/* Returns x + y, clamping out-of-range results into the range of the return
28 * type. */
064af421
BP
29static inline unsigned int
30sat_add(unsigned int x, unsigned int y)
31{
32 return x + y >= x ? x + y : UINT_MAX;
33}
74142432
BP
34static inline long long int
35llsat_add__(long long int x, long long int y)
36{
37 return (x >= 0 && y >= 0 && x > LLONG_MAX - y ? LLONG_MAX
38 : x < 0 && y < 0 && x < LLONG_MIN - y ? LLONG_MIN
39 : x + y);
40}
41static inline long long int
42llsat_add(long long int x, long long int y)
43{
0175bdd4 44#if (__GNUC__ >= 5 || __has_builtin(__builtin_saddll_overflow)) && !__CHECKER__
74142432
BP
45 long long int sum;
46 return (!__builtin_saddll_overflow(x, y, &sum) ? sum
47 : x > 0 ? LLONG_MAX : LLONG_MIN);
48#else
49 return llsat_add__(x, y);
50#endif
51}
064af421 52
74142432
BP
53/* Returns x - y, clamping out-of-range results into the range of the return
54 * type. */
064af421
BP
55static inline unsigned int
56sat_sub(unsigned int x, unsigned int y)
57{
58 return x >= y ? x - y : 0;
59}
74142432
BP
60static inline long long int
61llsat_sub__(long long int x, long long int y)
62{
63 return (x >= 0 && y < 0 && x > LLONG_MAX + y ? LLONG_MAX
64 : x < 0 && y >= 0 && x < LLONG_MIN + y ? LLONG_MIN
65 : x - y);
66}
67static inline long long int
68llsat_sub(long long int x, long long int y)
69{
0175bdd4 70#if (__GNUC__ >= 5 || __has_builtin(__builtin_ssubll_overflow)) && !__CHECKER__
74142432
BP
71 long long int difference;
72 return (!__builtin_ssubll_overflow(x, y, &difference) ? difference
73 : x >= 0 ? LLONG_MAX : LLONG_MIN);
74#else
75 return llsat_sub__(x, y);
76#endif
77}
064af421 78
74142432
BP
79/* Returns x * y, clamping out-of-range results into the range of the return
80 * type. */
064af421
BP
81static inline unsigned int
82sat_mul(unsigned int x, unsigned int y)
83{
8c7be52d 84 return OVS_SAT_MUL(x, y);
064af421 85}
74142432
BP
86static inline long long int
87llsat_mul__(long long int x, long long int y)
88{
89 return ( x > 0 && y > 0 && x > LLONG_MAX / y ? LLONG_MAX
90 : x < 0 && y > 0 && x < LLONG_MIN / y ? LLONG_MIN
91 : x > 0 && y < 0 && y < LLONG_MIN / x ? LLONG_MIN
92 /* Special case because -LLONG_MIN / -1 overflows: */
93 : x == LLONG_MIN && y == -1 ? LLONG_MAX
94 : x < 0 && y < 0 && x < LLONG_MAX / y ? LLONG_MAX
95 : x * y);
96}
97static inline long long int
98llsat_mul(long long int x, long long int y)
99{
0175bdd4 100#if (__GNUC__ >= 5 || __has_builtin(__builtin_smulll_overflow)) && !__CHECKER__
74142432
BP
101 long long int product;
102 return (!__builtin_smulll_overflow(x, y, &product) ? product
103 : (x > 0) == (y > 0) ? LLONG_MAX : LLONG_MIN);
104#else
105 return llsat_mul__(x, y);
106#endif
107}
064af421
BP
108
109#endif /* sat-math.h */