DetectorGraph  2.0
dgalternatives.hpp
Go to the documentation of this file.
1 // Copyright 2017 Nest Labs, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef DETECTORGRAPH_INCLUDE_DETECTOR_GRAPH_DGALTERNATIVES_HPP_
16 #define DETECTORGRAPH_INCLUDE_DETECTOR_GRAPH_DGALTERNATIVES_HPP_
17 
18 
19 #if defined(BUILD_FEATURE_DETECTORGRAPH_CONFIG_NO_64BIT_REMAINDER)
20 template<typename T>
21 T mod64(T lhs, T rhs)
22 {
23  // Taken from https://stackoverflow.com/a/2566570/435007
24  T x = rhs;
25  while (x <= (lhs>>1))
26  {
27  x <<= 1;
28  }
29  while (lhs >= rhs)
30  {
31  if (lhs >= x)
32  {
33  lhs -= x;
34  }
35  x >>= 1;
36  }
37  return lhs;
38 }
39 
40 #define DG_TIMEOFFSET_REMAINDER(A,B) mod64((A),(B))
41 #endif
42 
43 #endif // DETECTORGRAPH_INCLUDE_DETECTOR_GRAPH_DGALTERNATIVES_HPP_