/work/obj-fuzz/dist/include/mozilla/gfx/NumericTools.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #ifndef MOZILLA_GFX_NUMERICTOOLS_H_ |
8 | | #define MOZILLA_GFX_NUMERICTOOLS_H_ |
9 | | |
10 | | namespace mozilla { |
11 | | |
12 | | // XXX - Move these into mfbt/MathAlgorithms.h? |
13 | | |
14 | | // Returns the largest multiple of aMultiplied that's <= x. |
15 | | // Same as int32_t(floor(double(x) / aMultiplier)) * aMultiplier, |
16 | | // but faster. |
17 | | inline int32_t |
18 | | RoundDownToMultiple(int32_t x, int32_t aMultiplier) |
19 | 0 | { |
20 | 0 | // We don't use float division + floor because that's hard for the compiler |
21 | 0 | // to optimize. |
22 | 0 | int mod = x % aMultiplier; |
23 | 0 | if (x > 0) { |
24 | 0 | return x - mod; |
25 | 0 | } |
26 | 0 | return mod ? x - aMultiplier - mod : x; |
27 | 0 | } |
28 | | |
29 | | // Returns the smallest multiple of aMultiplied that's >= x. |
30 | | // Same as int32_t(ceil(double(x) / aMultiplier)) * aMultiplier, |
31 | | // but faster. |
32 | | inline int32_t |
33 | | RoundUpToMultiple(int32_t x, int32_t aMultiplier) |
34 | 0 | { |
35 | 0 | int mod = x % aMultiplier; |
36 | 0 | if (x > 0) { |
37 | 0 | return mod ? x + aMultiplier - mod : x; |
38 | 0 | } |
39 | 0 | return x - mod; |
40 | 0 | } |
41 | | |
42 | | } // namespace mozilla |
43 | | |
44 | | #endif /* MOZILLA_GFX_NUMERICTOOLS_H_ */ |