xref: /aosp_15_r20/external/webrtc/net/dcsctp/common/math.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 #ifndef NET_DCSCTP_COMMON_MATH_H_
11 #define NET_DCSCTP_COMMON_MATH_H_
12 
13 namespace dcsctp {
14 
15 // Rounds up `val` to the nearest value that is divisible by four. Frequently
16 // used to e.g. pad chunks or parameters to an even 32-bit offset.
17 template <typename IntType>
RoundUpTo4(IntType val)18 IntType RoundUpTo4(IntType val) {
19   return (val + 3) & ~3;
20 }
21 
22 // Similarly, rounds down `val` to the nearest value that is divisible by four.
23 template <typename IntType>
RoundDownTo4(IntType val)24 IntType RoundDownTo4(IntType val) {
25   return val & ~3;
26 }
27 
28 // Returns true if `val` is divisible by four.
29 template <typename IntType>
IsDivisibleBy4(IntType val)30 bool IsDivisibleBy4(IntType val) {
31   return (val & 3) == 0;
32 }
33 
34 }  // namespace dcsctp
35 
36 #endif  // NET_DCSCTP_COMMON_MATH_H_
37