Skip to content

Commit dda108f

Browse files
thughesCommit Bot
authored and
Commit Bot
committed
third_party/linux: Adapt overflow.h for EC build environment
BRANCH=none BUG=b:144957935 TEST=make buildall Signed-off-by: Tom Hughes <[email protected]> Change-Id: Iea1183e8b10af651eac2fad1174128774e7fb681 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2417528 Reviewed-by: Edward Hill <[email protected]>
1 parent 53eda6a commit dda108f

File tree

3 files changed

+65
-10
lines changed

3 files changed

+65
-10
lines changed

include/compiler.h

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* Copyright 2020 The Chromium OS Authors. All rights reserved.
2+
* Use of this source code is governed by a BSD-style license that can be
3+
* found in the LICENSE file.
4+
*/
5+
6+
#ifndef __CROS_EC_COMPILER_H
7+
#define __CROS_EC_COMPILER_H
8+
9+
/*
10+
* See https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html
11+
*/
12+
#define GCC_VERSION \
13+
(__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
14+
15+
#endif /* __CROS_EC_COMPILER_H */

include/overflow.h

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* Copyright 2020 The Chromium OS Authors. All rights reserved.
2+
* Use of this source code is governed by a BSD-style license that can be
3+
* found in the LICENSE file.
4+
*/
5+
6+
#ifndef __CROS_EC_OVERFLOW_H
7+
#define __CROS_EC_OVERFLOW_H
8+
9+
#include "compiler.h"
10+
11+
/*
12+
* __builtin_add_overflow, __builtin_sub_overflow and __builtin_mul_overflow
13+
* were added in gcc 5.1: https://gcc.gnu.org/gcc-5/changes.html
14+
*/
15+
#if GCC_VERSION > 50100
16+
#define COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW 1
17+
#endif
18+
19+
/*
20+
* __has_builtin available in
21+
* clang 10 and newer: https://clang.llvm.org/docs/LanguageExtensions.html
22+
*/
23+
#ifdef __clang__
24+
#if __has_builtin(__builtin_add_overflow) && \
25+
__has_builtin(__builtin_sub_overflow) && \
26+
__has_builtin(__builtin_mul_overflow)
27+
#define COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW 1
28+
#endif
29+
#endif /* __clang__ */
30+
31+
#include "third_party/linux/overflow.h"
32+
33+
#endif /* __CROS_EC_OVERFLOW_H */

third_party/linux/overflow.h

+17-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
#ifndef __LINUX_OVERFLOW_H
33
#define __LINUX_OVERFLOW_H
44

5-
#include <linux/compiler.h>
5+
#include <stddef.h>
6+
#include <stdint.h>
67

78
/*
89
* In the fallback code below, we need to compute the minimum and
@@ -122,9 +123,9 @@
122123
* we must provide a result in *d, and in fact we must produce the
123124
* result promised by gcc's builtins, which is simply the possibly
124125
* wrapped-around value. Fortunately, we can just formally do the
125-
* operations in the widest relevant unsigned type (u64) and then
126+
* operations in the widest relevant unsigned type (uint64_t) and then
126127
* truncate the result - gcc is smart enough to generate the same code
127-
* with and without the (u64) casts.
128+
* with and without the (uint64_t) casts.
128129
*/
129130

130131
/*
@@ -138,7 +139,7 @@
138139
typeof(d) __d = (d); \
139140
(void) (&__a == &__b); \
140141
(void) (&__a == __d); \
141-
*__d = (u64)__a + (u64)__b; \
142+
*__d = (uint64_t)__a + (uint64_t)__b; \
142143
(((~(__a ^ __b)) & (*__d ^ __a)) \
143144
& type_min(typeof(__a))) != 0; \
144145
})
@@ -154,7 +155,7 @@
154155
typeof(d) __d = (d); \
155156
(void) (&__a == &__b); \
156157
(void) (&__a == __d); \
157-
*__d = (u64)__a - (u64)__b; \
158+
*__d = (uint64_t)__a - (uint64_t)__b; \
158159
((((__a ^ __b)) & (*__d ^ __a)) \
159160
& type_min(typeof(__a))) != 0; \
160161
})
@@ -183,7 +184,7 @@
183184
typeof(a) __tmin = type_min(typeof(a)); \
184185
(void) (&__a == &__b); \
185186
(void) (&__a == __d); \
186-
*__d = (u64)__a * (u64)__b; \
187+
*__d = (uint64_t)__a * (uint64_t)__b; \
187188
(__b > 0 && (__a > __tmax/__b || __a < __tmin/__b)) || \
188189
(__b < (typeof(__b))-1 && (__a > __tmin/__b || __a < __tmax/__b)) || \
189190
(__b == (typeof(__b))-1 && __a == __tmin); \
@@ -231,14 +232,19 @@
231232
typeof(a) _a = a; \
232233
typeof(s) _s = s; \
233234
typeof(d) _d = d; \
234-
u64 _a_full = _a; \
235+
uint64_t _a_full = _a; \
235236
unsigned int _to_shift = \
236237
is_non_negative(_s) && _s < 8 * sizeof(*d) ? _s : 0; \
237238
*_d = (_a_full << _to_shift); \
238239
(_to_shift != _s || is_negative(*_d) || is_negative(_a) || \
239240
(*_d >> _to_shift) != _a); \
240241
})
241242

243+
/*
244+
* Disabling for EC since we don't currently have SIZE_MAX defined in our
245+
* stdint.h.
246+
*/
247+
#if 0
242248
/**
243249
* array_size() - Calculate size of 2-dimensional array.
244250
*
@@ -250,7 +256,7 @@
250256
* Returns: number of bytes needed to represent the array or SIZE_MAX on
251257
* overflow.
252258
*/
253-
static inline __must_check size_t array_size(size_t a, size_t b)
259+
static inline /*__must_check*/ size_t array_size(size_t a, size_t b)
254260
{
255261
size_t bytes;
256262

@@ -272,7 +278,7 @@ static inline __must_check size_t array_size(size_t a, size_t b)
272278
* Returns: number of bytes needed to represent the array or SIZE_MAX on
273279
* overflow.
274280
*/
275-
static inline __must_check size_t array3_size(size_t a, size_t b, size_t c)
281+
static inline /*__must_check*/ size_t array3_size(size_t a, size_t b, size_t c)
276282
{
277283
size_t bytes;
278284

@@ -288,7 +294,7 @@ static inline __must_check size_t array3_size(size_t a, size_t b, size_t c)
288294
* Compute a*b+c, returning SIZE_MAX on overflow. Internal helper for
289295
* struct_size() below.
290296
*/
291-
static inline __must_check size_t __ab_c_size(size_t a, size_t b, size_t c)
297+
static inline /*__must_check*/ size_t __ab_c_size(size_t a, size_t b, size_t c)
292298
{
293299
size_t bytes;
294300

@@ -299,6 +305,7 @@ static inline __must_check size_t __ab_c_size(size_t a, size_t b, size_t c)
299305

300306
return bytes;
301307
}
308+
#endif
302309

303310
/**
304311
* struct_size() - Calculate size of structure with trailing array.

0 commit comments

Comments
 (0)