1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_StickyTimeDuration_h
#define mozilla_StickyTimeDuration_h
#include "mozilla/TimeStamp.h"
#include "mozilla/FloatingPoint.h"
namespace mozilla {
/**
* A ValueCalculator class that performs additional checks before performing
* arithmetic operations such that if either operand is Forever (or the
* negative equivalent) the result remains Forever (or the negative equivalent
* as appropriate).
*
* Currently this only checks if either argument to each operation is
* Forever/-Forever. However, it is possible that, for example,
* aA + aB > INT64_MAX (or < INT64_MIN).
*
* We currently don't check for that case since we don't expect that to
* happen often except under test conditions in which case the wrapping
* behavior is probably acceptable.
*/
class StickyTimeDurationValueCalculator {
public:
static int64_t Add(int64_t aA, int64_t aB) {
MOZ_ASSERT((aA != INT64_MAX || aB != INT64_MIN) &&
(aA != INT64_MIN || aB != INT64_MAX),
"'Infinity + -Infinity' and '-Infinity + Infinity'"
" are undefined");
// Forever + x = Forever
// x + Forever = Forever
if (aA == INT64_MAX || aB == INT64_MAX) {
return INT64_MAX;
}
// -Forever + x = -Forever
// x + -Forever = -Forever
if (aA == INT64_MIN || aB == INT64_MIN) {
return INT64_MIN;
}
return aA + aB;
}
// Note that we can't just define Add and have BaseTimeDuration call Add with
// negative arguments since INT64_MAX != -INT64_MIN so the saturating logic
// won't work.
static int64_t Subtract(int64_t aA, int64_t aB) {
MOZ_ASSERT((aA != INT64_MAX && aA != INT64_MIN) || aA != aB,
"'Infinity - Infinity' and '-Infinity - -Infinity'"
" are undefined");
// Forever - x = Forever
// x - -Forever = Forever
if (aA == INT64_MAX || aB == INT64_MIN) {
return INT64_MAX;
}
// -Forever - x = -Forever
// x - Forever = -Forever
if (aA == INT64_MIN || aB == INT64_MAX) {
return INT64_MIN;
}
return aA - aB;
}
template <typename T>
static int64_t Multiply(int64_t aA, T aB) {
// Specializations for double, float, and int64_t are provided following.
return Multiply(aA, static_cast<int64_t>(aB));
}
static int64_t Divide(int64_t aA, int64_t aB) {
MOZ_ASSERT(aB != 0, "Division by zero");
MOZ_ASSERT((aA != INT64_MAX && aA != INT64_MIN) ||
(aB != INT64_MAX && aB != INT64_MIN),
"Dividing +/-Infinity by +/-Infinity is undefined");
// Forever / +x = Forever
// Forever / -x = -Forever
// -Forever / +x = -Forever
// -Forever / -x = Forever
if (aA == INT64_MAX || aA == INT64_MIN) {
return (aA >= 0) ^ (aB >= 0) ? INT64_MIN : INT64_MAX;
}
// x / Forever = 0
// x / -Forever = 0
if (aB == INT64_MAX || aB == INT64_MIN) {
return 0;
}
return aA / aB;
}
static double DivideDouble(int64_t aA, int64_t aB) {
MOZ_ASSERT(aB != 0, "Division by zero");
MOZ_ASSERT((aA != INT64_MAX && aA != INT64_MIN) ||
(aB != INT64_MAX && aB != INT64_MIN),
"Dividing +/-Infinity by +/-Infinity is undefined");
// Forever / +x = Forever
// Forever / -x = -Forever
// -Forever / +x = -Forever
// -Forever / -x = Forever
if (aA == INT64_MAX || aA == INT64_MIN) {
return (aA >= 0) ^ (aB >= 0) ? NegativeInfinity<double>()
: PositiveInfinity<double>();
}
// x / Forever = 0
// x / -Forever = 0
if (aB == INT64_MAX || aB == INT64_MIN) {
return 0.0;
}
return static_cast<double>(aA) / aB;
}
static int64_t Modulo(int64_t aA, int64_t aB) {
MOZ_ASSERT(aA != INT64_MAX && aA != INT64_MIN,
"Infinity modulo x is undefined");
return aA % aB;
}
};
template <>
inline int64_t StickyTimeDurationValueCalculator::Multiply<int64_t>(
int64_t aA, int64_t aB) {
MOZ_ASSERT((aA != 0 || (aB != INT64_MIN && aB != INT64_MAX)) &&
((aA != INT64_MIN && aA != INT64_MAX) || aB != 0),
"Multiplication of infinity by zero");
// Forever * +x = Forever
// Forever * -x = -Forever
// -Forever * +x = -Forever
// -Forever * -x = Forever
//
// i.e. If one or more of the arguments is +/-Forever, then
// return -Forever if the signs differ, or +Forever otherwise.
if (aA == INT64_MAX || aA == INT64_MIN || aB == INT64_MAX ||
aB == INT64_MIN) {
return (aA >= 0) ^ (aB >= 0) ? INT64_MIN : INT64_MAX;
}
return aA * aB;
}
template <>
inline int64_t StickyTimeDurationValueCalculator::Multiply<double>(int64_t aA,
double aB) {
MOZ_ASSERT((aA != 0 || (!IsInfinite(aB))) &&
((aA != INT64_MIN && aA != INT64_MAX) || aB != 0.0),
"Multiplication of infinity by zero");
// As with Multiply<int64_t>, if one or more of the arguments is
// +/-Forever or +/-Infinity, then return -Forever if the signs differ,
// or +Forever otherwise.
if (aA == INT64_MAX || aA == INT64_MIN || IsInfinite(aB)) {
return (aA >= 0) ^ (aB >= 0.0) ? INT64_MIN : INT64_MAX;
}
return aA * aB;
}
template <>
inline int64_t StickyTimeDurationValueCalculator::Multiply<float>(int64_t aA,
float aB) {
MOZ_ASSERT(IsInfinite(aB) == IsInfinite(static_cast<double>(aB)),
"Casting to float loses infinite-ness");
return Multiply(aA, static_cast<double>(aB));
}
/**
* Specialization of BaseTimeDuration that uses
* StickyTimeDurationValueCalculator for arithmetic on the mValue member.
*
* Use this class when you need a time duration that is expected to hold values
* of Forever (or the negative equivalent) *and* when you expect that
* time duration to be used in arithmetic operations (and not just value
* comparisons).
*/
typedef BaseTimeDuration<StickyTimeDurationValueCalculator> StickyTimeDuration;
// Template specializations to allow arithmetic between StickyTimeDuration
// and TimeDuration objects by falling back to the safe behavior.
inline StickyTimeDuration operator+(const TimeDuration& aA,
const StickyTimeDuration& aB) {
return StickyTimeDuration(aA) + aB;
}
inline StickyTimeDuration operator+(const StickyTimeDuration& aA,
const TimeDuration& aB) {
return aA + StickyTimeDuration(aB);
}
inline StickyTimeDuration operator-(const TimeDuration& aA,
const StickyTimeDuration& aB) {
return StickyTimeDuration(aA) - aB;
}
inline StickyTimeDuration operator-(const StickyTimeDuration& aA,
const TimeDuration& aB) {
return aA - StickyTimeDuration(aB);
}
inline StickyTimeDuration& operator+=(StickyTimeDuration& aA,
const TimeDuration& aB) {
return aA += StickyTimeDuration(aB);
}
inline StickyTimeDuration& operator-=(StickyTimeDuration& aA,
const TimeDuration& aB) {
return aA -= StickyTimeDuration(aB);
}
inline double operator/(const TimeDuration& aA, const StickyTimeDuration& aB) {
return StickyTimeDuration(aA) / aB;
}
inline double operator/(const StickyTimeDuration& aA, const TimeDuration& aB) {
return aA / StickyTimeDuration(aB);
}
inline StickyTimeDuration operator%(const TimeDuration& aA,
const StickyTimeDuration& aB) {
return StickyTimeDuration(aA) % aB;
}
inline StickyTimeDuration operator%(const StickyTimeDuration& aA,
const TimeDuration& aB) {
return aA % StickyTimeDuration(aB);
}
} // namespace mozilla
#endif /* mozilla_StickyTimeDuration_h */
|