1/*
2 * Copyright (c) 2012, Google Inc. All rights reserved.
3 * Copyright (C) 2014 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#pragma once
33
34#include <limits>
35#include <stdint.h>
36#include <stdlib.h>
37#include <wtf/Compiler.h>
38
39inline bool signedAddOverflows(int32_t a, int32_t b, int32_t& result)
40{
41#if COMPILER_HAS_CLANG_BUILTIN(__builtin_sadd_overflow) && !(defined __clang_major__ && __clang_major__ < 7)
42 return __builtin_sadd_overflow(a, b, &result);
43#else
44 uint32_t ua = a;
45 uint32_t ub = b;
46 uint32_t uresult = ua + ub;
47 result = static_cast<int32_t>(uresult);
48
49 // Can only overflow if the signed bit of the two values match. If the signed
50 // bit of the result and one of the values differ it did overflow.
51 return !((ua ^ ub) >> 31) && (uresult ^ ua) >> 31;
52#endif
53}
54
55inline int32_t saturatedAddition(int32_t a, int32_t b)
56{
57 int32_t result;
58#if CPU(ARM_THUMB2)
59 asm("qadd %[sum], %[addend], %[augend]"
60 : [sum]"=r"(result)
61 : [augend]"r"(a), [addend]"r"(b)
62 : /* Nothing is clobbered. */
63 );
64#else
65 if (signedAddOverflows(a, b, result))
66 result = std::numeric_limits<int32_t>::max() + (static_cast<uint32_t>(a) >> 31);
67#endif
68 return result;
69}
70
71inline bool signedSubtractOverflows(int32_t a, int32_t b, int32_t& result)
72{
73#if COMPILER_HAS_CLANG_BUILTIN(__builtin_ssub_overflow) && !(defined __clang_major__ && __clang_major__ < 7)
74 return __builtin_ssub_overflow(a, b, &result);
75#else
76 uint32_t ua = a;
77 uint32_t ub = b;
78 uint32_t uresult = ua - ub;
79 result = static_cast<int32_t>(uresult);
80
81 // Can only overflow if the signed bit of the two values do not match. If the
82 // signed bit of the result and the first value differ it did overflow.
83 return (ua ^ ub) >> 31 && (uresult ^ ua) >> 31;
84#endif
85}
86
87inline int32_t saturatedSubtraction(int32_t a, int32_t b)
88{
89 int32_t result;
90#if CPU(ARM_THUMB2)
91 asm("qsub %[difference], %[minuend], %[subtrahend]"
92 : [difference]"=r"(result)
93 : [minuend]"r"(a), [subtrahend]"r"(b)
94 : /* Nothing is clobbered. */
95 );
96#else
97 if (signedSubtractOverflows(a, b, result))
98 result = std::numeric_limits<int32_t>::max() + (static_cast<uint32_t>(a) >> 31);
99#endif
100 return result;
101}
102