simtimemath.h Source File

API: simtimemath.h Source File
API
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
21namespace omnetpp {
22
27
28// used locally; needed because sign of a%b is implementation dependent if a<0
29inline 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
38inline 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
52inline 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
63inline 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
74inline 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
85inline const SimTime fabs(const SimTime& x)
86{
87 return x.raw()<0 ? -x : x;
88}
89
100inline int64_t div(const SimTime& x, const SimTime& y)
101{
102 return x.raw() / y.raw();
103}
104
120inline const SimTime fmod(const SimTime& x, const SimTime& y)
121{
122 return SimTime().setRaw(x.raw() % y.raw());
123}
124
134SIM_API int64_t preciseDiv(int64_t x, const SimTime& y, int64_t& fractionNumerator, int64_t& fractionDenominator);
135
137
138} // namespace omnetpp
139
140#endif
141
142
int64_t-based, base-10 fixed-point simulation time.
Definition simtime.h:67
int64_t raw() const
Definition simtime.h:426
static int64_t getScale()
Definition simtime.h:448
const SimTime & setRaw(int64_t l)
Definition simtime.h:436
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.
const SimTime floor(const SimTime &x)
simtime_t version of floor(double) from math.h.
Definition simtimemath.h:38
const SimTime fabs(const SimTime &x)
Returns the absolute value of the simulation time x.
Definition simtimemath.h:85
const SimTime fmod(const SimTime &x, const SimTime &y)
Computes the remainder of the division of two simulation times.
Definition simtimemath.h:120
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
const SimTime ceil(const SimTime &x)
simtime_t version of ceil(double) from math.h.
Definition simtimemath.h:63