]>
git.proxmox.com Git - libgit2.git/blob - src/integer.h
2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
7 #ifndef INCLUDE_integer_h__
8 #define INCLUDE_integer_h__
10 /** @return true if p fits into the range of a size_t */
11 GIT_INLINE(int) git__is_sizet(git_off_t p
)
14 return p
== (git_off_t
)r
;
17 /** @return true if p fits into the range of an ssize_t */
18 GIT_INLINE(int) git__is_ssizet(size_t p
)
20 ssize_t r
= (ssize_t
)p
;
21 return p
== (size_t)r
;
24 /** @return true if p fits into the range of a uint32_t */
25 GIT_INLINE(int) git__is_uint32(size_t p
)
27 uint32_t r
= (uint32_t)p
;
28 return p
== (size_t)r
;
31 /** @return true if p fits into the range of an unsigned long */
32 GIT_INLINE(int) git__is_ulong(git_off_t p
)
34 unsigned long r
= (unsigned long)p
;
35 return p
== (git_off_t
)r
;
38 /** @return true if p fits into the range of an int */
39 GIT_INLINE(int) git__is_int(long long p
)
42 return p
== (long long)r
;
46 * Sets `one + two` into `out`, unless the arithmetic would overflow.
47 * @return true if the result fits in a `uint64_t`, false on overflow.
49 GIT_INLINE(bool) git__add_uint64_overflow(uint64_t *out
, uint64_t one
, uint64_t two
)
51 if (UINT64_MAX
- one
< two
)
57 /* Use clang/gcc compiler intrinsics whenever possible */
58 #if (SIZE_MAX == UINT_MAX) && __has_builtin(__builtin_uadd_overflow)
59 # define git__add_sizet_overflow(out, one, two) \
60 __builtin_uadd_overflow(one, two, out)
61 # define git__multiply_sizet_overflow(out, one, two) \
62 __builtin_umul_overflow(one, two, out)
63 #elif (SIZE_MAX == ULONG_MAX) && __has_builtin(__builtin_uaddl_overflow)
64 # define git__add_sizet_overflow(out, one, two) \
65 __builtin_uaddl_overflow(one, two, out)
66 # define git__multiply_sizet_overflow(out, one, two) \
67 __builtin_umull_overflow(one, two, out)
71 * Sets `one + two` into `out`, unless the arithmetic would overflow.
72 * @return true if the result fits in a `size_t`, false on overflow.
74 GIT_INLINE(bool) git__add_sizet_overflow(size_t *out
, size_t one
, size_t two
)
76 if (SIZE_MAX
- one
< two
)
83 * Sets `one * two` into `out`, unless the arithmetic would overflow.
84 * @return true if the result fits in a `size_t`, false on overflow.
86 GIT_INLINE(bool) git__multiply_sizet_overflow(size_t *out
, size_t one
, size_t two
)
88 if (one
&& SIZE_MAX
/ one
< two
)
96 #endif /* INCLUDE_integer_h__ */