OMNeT++ Simulation Library  5.6.1
simtimemath.h
1 //==========================================================================
2 // SIMTIME.H - part of
3 // OMNeT++/OMNEST
4 // Discrete System Simulation in C++
5 //
6 //==========================================================================
7 
8 /*--------------------------------------------------------------*
9  Copyright (C) 1992-2017 Andras Varga
10  Copyright (C) 2006-2017 OpenSim Ltd.
11 
12  This file is distributed WITHOUT ANY WARRANTY. See the file
13  `license' for details on this and other legal matters.
14 *--------------------------------------------------------------*/
15 
16 #ifndef __OMNETPP_SIMTIMEMATH_H
17 #define __OMNETPP_SIMTIMEMATH_H
18 
19 #include "simtime.h"
20 
21 namespace omnetpp {
22 
23 // used locally; needed because sign of a%b is implementation dependent if a<0
24 inline int64_t _i64mod(const int64_t& any_t, const int64_t& positive_u)
25 {
26  int64_t m = any_t % positive_u;
27  return m>=0 ? m : m+positive_u;
28 }
29 
33 inline const SimTime floor(const SimTime& x)
34 {
35  int64_t u = SimTime::getScale();
36  int64_t t = x.raw();
37  return SimTime().setRaw(t - _i64mod(t,u));
38 }
39 
47 inline const SimTime floor(const SimTime& x, const SimTime& unit, const SimTime& offset = SimTime())
48 {
49  int64_t off = offset.raw();
50  int64_t u = unit.raw();
51  int64_t t = x.raw() - off;
52  return SimTime().setRaw(t - _i64mod(t,u) + off);
53 }
54 
58 inline const SimTime ceil(const SimTime& x)
59 {
60  int64_t u = SimTime::getScale();
61  int64_t t = x.raw() + u-1;
62  return SimTime().setRaw(t - _i64mod(t,u));
63 }
64 
69 inline const SimTime ceil(const SimTime& x, const SimTime& unit, const SimTime& offset = SimTime())
70 {
71  int64_t off = offset.raw();
72  int64_t u = unit.raw();
73  int64_t t = x.raw() - off + u-1;
74  return SimTime().setRaw(t - _i64mod(t,u) + off);
75 }
76 
80 inline const SimTime fabs(const SimTime& x)
81 {
82  return x.raw()<0 ? -x : x;
83 }
84 
95 inline int64_t div(const SimTime& x, const SimTime& y)
96 {
97  return x.raw() / y.raw();
98 }
99 
115 inline const SimTime fmod(const SimTime& x, const SimTime& y)
116 {
117  return SimTime().setRaw(x.raw() % y.raw());
118 }
119 
129 SIM_API int64_t preciseDiv(int64_t x, const SimTime& y, int64_t& fractionNumerator, int64_t& fractionDenominator);
130 
131 } // namespace omnetpp
132 
133 #endif
134 
135 
int64_t raw() const
Definition: simtime.h:381
const SimTime & setRaw(int64_t l)
Definition: simtime.h:391
Definition: cabstracthistogram.h:21
static int64_t getScale()
Definition: simtime.h:403