OMNeT++ Simulation Library  6.0.3
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 
28 // used locally; needed because sign of a%b is implementation dependent if a<0
29 inline int64_t _i64mod(const int64_t& any_t, const int64_t& positive_u)
30 {
31  int64_t m = any_t % positive_u;
32  return m>=0 ? m : m+positive_u;
33 }
34 
38 inline const SimTime floor(const SimTime& x)
39 {
40  int64_t u = SimTime::getScale();
41  int64_t t = x.raw();
42  return SimTime().setRaw(t - _i64mod(t,u));
43 }
44 
52 inline const SimTime floor(const SimTime& x, const SimTime& unit, const SimTime& offset = SimTime())
53 {
54  int64_t off = offset.raw();
55  int64_t u = unit.raw();
56  int64_t t = x.raw() - off;
57  return SimTime().setRaw(t - _i64mod(t,u) + off);
58 }
59 
63 inline const SimTime ceil(const SimTime& x)
64 {
65  int64_t u = SimTime::getScale();
66  int64_t t = x.raw() + u-1;
67  return SimTime().setRaw(t - _i64mod(t,u));
68 }
69 
74 inline const SimTime ceil(const SimTime& x, const SimTime& unit, const SimTime& offset = SimTime())
75 {
76  int64_t off = offset.raw();
77  int64_t u = unit.raw();
78  int64_t t = x.raw() - off + u-1;
79  return SimTime().setRaw(t - _i64mod(t,u) + off);
80 }
81 
85 inline const SimTime fabs(const SimTime& x)
86 {
87  return x.raw()<0 ? -x : x;
88 }
89 
100 inline int64_t div(const SimTime& x, const SimTime& y)
101 {
102  return x.raw() / y.raw();
103 }
104 
120 inline const SimTime fmod(const SimTime& x, const SimTime& y)
121 {
122  return SimTime().setRaw(x.raw() % y.raw());
123 }
124 
134 SIM_API int64_t preciseDiv(int64_t x, const SimTime& y, int64_t& fractionNumerator, int64_t& fractionDenominator);
135 
138 } // namespace omnetpp
139 
140 #endif
141 
142 
omnetpp::SimTime::setRaw
const SimTime & setRaw(int64_t l)
Definition: simtime.h:397
omnetpp::SimTime
int64_t-based, base-10 fixed-point simulation time.
Definition: simtime.h:66
omnetpp::fmod
const SimTime fmod(const SimTime &x, const SimTime &y)
Computes the remainder of the division of two simulation times.
Definition: simtimemath.h:120
omnetpp::ceil
const SimTime ceil(const SimTime &x, const SimTime &unit, const SimTime &offset=SimTime())
Generalized version of ceil(), accepting a unit and an offset: ceil(x,u,off) = ceil((x-off)/u)*u + of...
Definition: simtimemath.h:74
omnetpp::SimTime::getScale
static int64_t getScale()
Definition: simtime.h:409
omnetpp::floor
const SimTime floor(const SimTime &x, const SimTime &unit, const SimTime &offset=SimTime())
Generalized version of floor(), accepting a unit and an offset: floor(x,u,off) = floor((x-off)/u)*u +...
Definition: simtimemath.h:52
omnetpp::div
int64_t div(const SimTime &x, const SimTime &y)
Computes the quotient of the simulation times x and y. The quotient is the algebraic quotient with an...
Definition: simtimemath.h:100
omnetpp::preciseDiv
SIM_API int64_t preciseDiv(int64_t x, const SimTime &y, int64_t &fractionNumerator, int64_t &fractionDenominator)
Precise division of an integer and a simulation time.
omnetpp::fabs
const SimTime fabs(const SimTime &x)
Returns the absolute value of the simulation time x.
Definition: simtimemath.h:85
omnetpp::SimTime::raw
int64_t raw() const
Definition: simtime.h:387