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/. */
7 #ifndef MOZILLA_GFX_NUMERICTOOLS_H_
8 #define MOZILLA_GFX_NUMERICTOOLS_H_
14 // XXX - Move these into mfbt/MathAlgorithms.h?
16 // Returns the largest multiple of aMultiplied that's <= x.
17 // Same as int32_t(floor(double(x) / aMultiplier)) * aMultiplier,
19 inline int32_t RoundDownToMultiple(int32_t x
, int32_t aMultiplier
) {
20 // We don't use float division + floor because that's hard for the compiler
22 int mod
= x
% aMultiplier
;
26 return mod
? x
- aMultiplier
- mod
: x
;
29 // Returns the smallest multiple of aMultiplied that's >= x.
30 // Same as int32_t(ceil(double(x) / aMultiplier)) * aMultiplier,
32 inline int32_t RoundUpToMultiple(int32_t x
, int32_t aMultiplier
) {
33 int mod
= x
% aMultiplier
;
35 return mod
? x
+ aMultiplier
- mod
: x
;
40 inline int32_t RoundToMultiple(int32_t x
, int32_t aMultiplier
) {
41 return RoundDownToMultiple(x
+ aMultiplier
/ 2, aMultiplier
);
44 } // namespace mozilla
46 #endif /* MOZILLA_GFX_NUMERICTOOLS_H_ */