View | Details | Raw Unified | Return to bug 954
Collapse All | Expand All

(-)a/src/core/model/nstime.h (-7 / +457 lines)
 Lines 108-120    Link Here 
108
   */
108
   */
109
  enum Unit
109
  enum Unit
110
  {
110
  {
111
    S  = 0,   //!< second
111
    A   = 0,   //!< year
112
    MS = 1,   //!< millisecond
112
    WK  = 1,   //!< week
113
    US = 2,   //!< microsecond
113
    D   = 2,   //!< day
114
    NS = 3,   //!< nanosecond
114
    H   = 3,   //!< hour
115
    PS = 4,   //!< picosecond
115
    MIN = 4,   //!< minute
116
    FS = 5,   //!< femtosecond
116
    S   = 5,   //!< second
117
    LAST = 6
117
    MS  = 6,   //!< millisecond
118
    US  = 7,   //!< microsecond
119
    NS  = 8,   //!< nanosecond
120
    PS  = 9,   //!< picosecond
121
    FS  = 10,   //!< femtosecond
122
    LAST = 11
118
  };
123
  };
119
  inline Time &operator = (const Time &o)
124
  inline Time &operator = (const Time &o)
 Lines 204-209    Link Here 
204
   * - `ns` (nanoseconds)
209
   * - `ns` (nanoseconds)
205
   * - `ps` (picoseconds)
210
   * - `ps` (picoseconds)
206
   * - `fs` (femtoseconds)
211
   * - `fs` (femtoseconds)
212
   * - `min`  (minutes)
213
   * - `h`  (hours)
214
   * - `d`  (days)
215
   * - `wk`  (weeks)
216
   * - `a`  (years)
207
   *
217
   *
208
   * There can be no white space between the numerical portion
218
   * There can be no white space between the numerical portion
209
   * and the units.  Any otherwise malformed string causes a fatal error to
219
   * and the units.  Any otherwise malformed string causes a fatal error to
 Lines 330-335    Link Here 
330
  {
340
  {
331
    return ToInteger (Time::FS);
341
    return ToInteger (Time::FS);
332
  }
342
  }
343
344
  /**
345
   * \returns an approximation in minutes of the time stored in this
346
   *          instance.
347
   */
348
  inline double GetMinutes (void) const
349
  {
350
    return ToDouble (Time::MIN);
351
  }
352
  /**
353
   * \returns an approximation in hours of the time stored in this
354
   *          instance.
355
   */
356
  inline double GetHours (void) const
357
  {
358
    return ToDouble (Time::H);
359
  }
360
  /**
361
   * \returns an approximation in days of the time stored in this
362
   *          instance.
363
   */
364
  inline double GetDays (void) const
365
  {
366
    return ToDouble (Time::D);
367
  }
368
  /**
369
   * \returns an approximation in weeks of the time stored in this
370
   *          instance.
371
   */
372
  inline double GetWeeks (void) const
373
  {
374
    return ToDouble (Time::WK);
375
  }
376
  /**
377
   * \returns an approximation in years of the time stored in this
378
   *          instance.
379
   */
380
  inline double GetYears (void) const
381
  {
382
    return ToDouble (Time::A);
383
  }
384
333
  /**
385
  /**
334
   * \returns the raw time value, in the current units
386
   * \returns the raw time value, in the current units
335
   */
387
   */
 Lines 709-714    Link Here 
709
  return Time::FromDouble (seconds, Time::S);
761
  return Time::FromDouble (seconds, Time::S);
710
}
762
}
763
inline Time Seconds (float seconds)
764
{
765
  return Time::FromDouble (seconds, Time::S);
766
}
767
768
inline Time Seconds (uint64_t seconds)
769
{
770
  return Time::FromInteger (seconds, Time::S);
771
}
772
773
inline Time Seconds (int64_t seconds)
774
{
775
  return Time::FromInteger (seconds, Time::S);
776
}
777
778
inline Time Seconds (uint32_t seconds)
779
{
780
  return Time::FromInteger (seconds, Time::S);
781
}
782
783
inline Time Seconds (int32_t seconds)
784
{
785
  return Time::FromInteger (seconds, Time::S);
786
}
787
711
/**
788
/**
712
 * \brief create ns3::Time instances in units of milliseconds.
789
 * \brief create ns3::Time instances in units of milliseconds.
713
 *
790
 *
 Lines 720-729    Link Here 
720
 * \param ms milliseconds value
797
 * \param ms milliseconds value
721
 * \relates ns3::Time
798
 * \relates ns3::Time
722
 */
799
 */
800
inline Time MilliSeconds (double ms)
801
{
802
  return Time::FromDouble (ms, Time::MS);
803
}
804
805
inline Time MilliSeconds (float ms)
806
{
807
  return Time::FromDouble (ms, Time::MS);
808
}
809
723
inline Time MilliSeconds (uint64_t ms)
810
inline Time MilliSeconds (uint64_t ms)
724
{
811
{
725
  return Time::FromInteger (ms, Time::MS);
812
  return Time::FromInteger (ms, Time::MS);
726
}
813
}
814
815
inline Time MilliSeconds (int64_t ms)
816
{
817
  return Time::FromInteger (ms, Time::MS);
818
}
819
820
inline Time MilliSeconds (uint32_t ms)
821
{
822
  return Time::FromInteger (ms, Time::MS);
823
}
824
825
inline Time MilliSeconds (int32_t ms)
826
{
827
  return Time::FromInteger (ms, Time::MS);
828
}
829
727
/**
830
/**
728
 * \brief create ns3::Time instances in units of microseconds.
831
 * \brief create ns3::Time instances in units of microseconds.
729
 *
832
 *
 Lines 735-744    Link Here 
735
 * \param us microseconds value
838
 * \param us microseconds value
736
 * \relates ns3::Time
839
 * \relates ns3::Time
737
 */
840
 */
841
inline Time MicroSeconds (double us)
842
{
843
  return Time::FromDouble (us, Time::US);
844
}
845
846
inline Time MicroSeconds (float us)
847
{
848
  return Time::FromDouble (us, Time::US);
849
}
850
738
inline Time MicroSeconds (uint64_t us)
851
inline Time MicroSeconds (uint64_t us)
739
{
852
{
740
  return Time::FromInteger (us, Time::US);
853
  return Time::FromInteger (us, Time::US);
741
}
854
}
855
856
inline Time MicroSeconds (int64_t us)
857
{
858
  return Time::FromInteger (us, Time::US);
859
}
860
861
inline Time MicroSeconds (uint32_t us)
862
{
863
  return Time::FromInteger (us, Time::US);
864
}
865
866
inline Time MicroSeconds (int32_t us)
867
{
868
  return Time::FromInteger (us, Time::US);
869
}
870
742
/**
871
/**
743
 * \brief create ns3::Time instances in units of nanoseconds.
872
 * \brief create ns3::Time instances in units of nanoseconds.
744
 *
873
 *
 Lines 750-759    Link Here 
750
 * \param ns nanoseconds value
879
 * \param ns nanoseconds value
751
 * \relates ns3::Time
880
 * \relates ns3::Time
752
 */
881
 */
882
inline Time NanoSeconds (double ns)
883
{
884
  return Time::FromDouble (ns, Time::NS);
885
}
886
887
inline Time NanoSeconds (float ns)
888
{
889
  return Time::FromDouble (ns, Time::NS);
890
}
891
753
inline Time NanoSeconds (uint64_t ns)
892
inline Time NanoSeconds (uint64_t ns)
754
{
893
{
755
  return Time::FromInteger (ns, Time::NS);
894
  return Time::FromInteger (ns, Time::NS);
756
}
895
}
896
897
inline Time NanoSeconds (int64_t ns)
898
{
899
  return Time::FromInteger (ns, Time::NS);
900
}
901
902
inline Time NanoSeconds (uint32_t ns)
903
{
904
  return Time::FromInteger (ns, Time::NS);
905
}
906
907
inline Time NanoSeconds (int32_t ns)
908
{
909
  return Time::FromInteger (ns, Time::NS);
910
}
911
757
/**
912
/**
758
 * \brief create ns3::Time instances in units of picoseconds.
913
 * \brief create ns3::Time instances in units of picoseconds.
759
 *
914
 *
 Lines 765-774    Link Here 
765
 * \param ps picoseconds value
920
 * \param ps picoseconds value
766
 * \relates ns3::Time
921
 * \relates ns3::Time
767
 */
922
 */
923
inline Time PicoSeconds (double ps)
924
{
925
  return Time::FromDouble (ps, Time::PS);
926
}
927
928
inline Time PicoSeconds (float ps)
929
{
930
  return Time::FromDouble (ps, Time::PS);
931
}
932
768
inline Time PicoSeconds (uint64_t ps)
933
inline Time PicoSeconds (uint64_t ps)
769
{
934
{
770
  return Time::FromInteger (ps, Time::PS);
935
  return Time::FromInteger (ps, Time::PS);
771
}
936
}
937
938
inline Time PicoSeconds (int64_t ps)
939
{
940
  return Time::FromInteger (ps, Time::PS);
941
}
942
943
inline Time PicoSeconds (uint32_t ps)
944
{
945
  return Time::FromInteger (ps, Time::PS);
946
}
947
948
inline Time PicoSeconds (int32_t ps)
949
{
950
  return Time::FromInteger (ps, Time::PS);
951
}
952
772
/**
953
/**
773
 * \brief create ns3::Time instances in units of femtoseconds.
954
 * \brief create ns3::Time instances in units of femtoseconds.
774
 *
955
 *
 Lines 780-790    Link Here 
780
 * \param fs femtoseconds value
961
 * \param fs femtoseconds value
781
 * \relates ns3::Time
962
 * \relates ns3::Time
782
 */
963
 */
964
inline Time FemtoSeconds (double fs)
965
{
966
  return Time::FromDouble (fs, Time::FS);
967
}
968
969
inline Time FemtoSeconds (float fs)
970
{
971
  return Time::FromDouble (fs, Time::FS);
972
}
973
783
inline Time FemtoSeconds (uint64_t fs)
974
inline Time FemtoSeconds (uint64_t fs)
784
{
975
{
785
  return Time::FromInteger (fs, Time::FS);
976
  return Time::FromInteger (fs, Time::FS);
786
}
977
}
978
inline Time FemtoSeconds (int64_t fs)
979
{
980
  return Time::FromInteger (fs, Time::FS);
981
}
982
983
inline Time FemtoSeconds (uint32_t fs)
984
{
985
  return Time::FromInteger (fs, Time::FS);
986
}
987
988
inline Time FemtoSeconds (int32_t fs)
989
{
990
  return Time::FromInteger (fs, Time::FS);
991
}
992
993
/**
994
 * \brief create ns3::Time instances in units of minutes.
995
 *
996
 * For example:
997
 * \code
998
 * Time t = Minutes (2.0);
999
 * Simulator::Schedule (Minutes (5.0), ...);
1000
 * \endcode
1001
 * \param minutes mintues value
1002
 * \relates ns3::Time
1003
 */
1004
inline Time Minutes (double minutes)
1005
{
1006
  return Time::FromDouble (minutes, Time::MIN);
1007
}
1008
1009
inline Time Minutes (float minutes)
1010
{
1011
  return Time::FromDouble (minutes, Time::MIN);
1012
}
1013
1014
inline Time Minutes (uint64_t minutes)
1015
{
1016
  return Time::FromInteger (minutes, Time::MIN);
1017
}
1018
1019
inline Time Minutes (int64_t minutes)
1020
{
1021
  return Time::FromInteger (minutes, Time::MIN);
1022
}
1023
1024
inline Time Minutes (uint32_t minutes)
1025
{
1026
  return Time::FromInteger (minutes, Time::MIN);
1027
}
1028
1029
inline Time Minutes (int32_t minutes)
1030
{
1031
  return Time::FromInteger (minutes, Time::MIN);
1032
}
1033
1034
/**
1035
 * \brief create ns3::Time instances in units of hours.
1036
 *
1037
 * For example:
1038
 * \code
1039
 * Time t = Hours (2.0);
1040
 * Simulator::Schedule (Hours (5.0), ...);
1041
 * \endcode
1042
 * \param hours hours value
1043
 * \relates ns3::Time
1044
 */
1045
inline Time Hours (double hours)
1046
{
1047
  return Time::FromDouble (hours, Time::H);
1048
}
1049
1050
inline Time Hours (float hours)
1051
{
1052
  return Time::FromDouble (hours, Time::H);
1053
}
1054
1055
inline Time Hours (uint64_t hours)
1056
{
1057
  return Time::FromInteger (hours, Time::H);
1058
}
1059
1060
inline Time Hours (int64_t hours)
1061
{
1062
  return Time::FromInteger (hours, Time::H);
1063
}
1064
1065
inline Time Hours (uint32_t hours)
1066
{
1067
  return Time::FromInteger (hours, Time::H);
1068
}
1069
1070
inline Time Hours (int32_t hours)
1071
{
1072
  return Time::FromInteger (hours, Time::H);
1073
}
1074
1075
/**
1076
 * \brief create ns3::Time instances in units of days.
1077
 *
1078
 * For example:
1079
 * \code
1080
 * Time t = Days (2.0);
1081
 * Simulator::Schedule (Days (5.0), ...);
1082
 * \endcode
1083
 * \param days days value
1084
 * \relates ns3::Time
1085
 */
1086
inline Time Days (double days)
1087
{
1088
  return Time::FromDouble (days, Time::D);
1089
}
1090
1091
inline Time Days (float days)
1092
{
1093
  return Time::FromDouble (days, Time::D);
1094
}
1095
1096
inline Time Days (uint64_t days)
1097
{
1098
  return Time::FromInteger (days, Time::D);
1099
}
1100
1101
inline Time Days (int64_t days)
1102
{
1103
  return Time::FromInteger (days, Time::D);
1104
}
1105
1106
inline Time Days (uint32_t days)
1107
{
1108
  return Time::FromInteger (days, Time::D);
1109
}
1110
1111
inline Time Days (int32_t days)
1112
{
1113
  return Time::FromInteger (days, Time::D);
1114
}
1115
1116
/**
1117
 * \brief create ns3::Time instances in units of weeks.
1118
 *
1119
 * For example:
1120
 * \code
1121
 * Time t = Weeks (2.0);
1122
 * Simulator::Schedule (Weeks (5.0), ...);
1123
 * \endcode
1124
 * \param weeks weeks value
1125
 * \relates ns3::Time
1126
 */
1127
inline Time Weeks (double weeks)
1128
{
1129
  return Time::FromDouble (weeks, Time::WK);
1130
}
1131
1132
inline Time Weeks (float weeks)
1133
{
1134
  return Time::FromDouble (weeks, Time::WK);
1135
}
1136
1137
inline Time Weeks (uint64_t weeks)
1138
{
1139
  return Time::FromInteger (weeks, Time::WK);
1140
}
1141
1142
inline Time Weeks (int64_t weeks)
1143
{
1144
  return Time::FromInteger (weeks, Time::WK);
1145
}
1146
1147
inline Time Weeks (uint32_t weeks)
1148
{
1149
  return Time::FromInteger (weeks, Time::WK);
1150
}
1151
1152
inline Time Weeks (int32_t weeks)
1153
{
1154
  return Time::FromInteger (weeks, Time::WK);
1155
}
1156
1157
/**
1158
 * \brief create ns3::Time instances in units of years.
1159
 *
1160
 * For example:
1161
 * \code
1162
 * Time t = Years (2.0);
1163
 * Simulator::Schedule (Years (5.0), ...);
1164
 * \endcode
1165
 * \param years years value
1166
 * \relates ns3::Time
1167
 */
1168
inline Time Years (double years)
1169
{
1170
  return Time::FromDouble (years, Time::A);
1171
}
1172
1173
inline Time Years (float years)
1174
{
1175
  return Time::FromDouble (years, Time::A);
1176
}
1177
1178
inline Time Years (uint64_t years)
1179
{
1180
  return Time::FromInteger (years, Time::A);
1181
}
1182
1183
inline Time Years (int64_t years)
1184
{
1185
  return Time::FromInteger (years, Time::A);
1186
}
1187
1188
inline Time Years (uint32_t years)
1189
{
1190
  return Time::FromInteger (years, Time::A);
1191
}
1192
1193
inline Time Years (int32_t years)
1194
{
1195
  return Time::FromInteger (years, Time::A);
1196
}
787
/**
1197
/**
788
 * \see Seconds(double)
1198
 * \see Seconds(double)
 Lines 834-839    Link Here 
834
{
1244
{
835
  return Time::From (fs, Time::FS);
1245
  return Time::From (fs, Time::FS);
836
}
1246
}
1247
/**
1248
 * \see Minutes(uint64_t)
1249
 * \relates ns3::Time
1250
 */
1251
inline Time Minutes (int64x64_t minutes)
1252
{
1253
  return Time::From (minutes, Time::MIN);
1254
}
1255
/**
1256
 * \see Minutes(uint64_t)
1257
 * \relates ns3::Time
1258
 */
1259
inline Time Hours (int64x64_t hours)
1260
{
1261
  return Time::From (hours, Time::H);
1262
}
1263
/**
1264
 * \see Minutes(uint64_t)
1265
 * \relates ns3::Time
1266
 */
1267
inline Time Days (int64x64_t days)
1268
{
1269
  return Time::From (days, Time::D);
1270
}
1271
/**
1272
 * \see Minutes(uint64_t)
1273
 * \relates ns3::Time
1274
 */
1275
inline Time Weeks (int64x64_t weeks)
1276
{
1277
  return Time::From (weeks, Time::WK);
1278
}
1279
/**
1280
 * \see Minutes(uint64_t)
1281
 * \relates ns3::Time
1282
 */
1283
inline Time Years (int64x64_t years)
1284
{
1285
  return Time::From (years, Time::A);
1286
}
837
// internal function not publicly documented
1287
// internal function not publicly documented
838
inline Time TimeStep (uint64_t ts)
1288
inline Time TimeStep (uint64_t ts)
(-)a/src/core/model/time.cc (-5 / +60 lines)
 Lines 123-128    Link Here 
123
        {
123
        {
124
          *this = Time::FromDouble (r, Time::FS);
124
          *this = Time::FromDouble (r, Time::FS);
125
        }
125
        }
126
      else if (trailer == std::string ("min"))
127
        {
128
          *this = Time::FromDouble (r, Time::MIN);
129
        }
130
      else if (trailer == std::string ("h"))
131
        {
132
          *this = Time::FromDouble (r, Time::H);
133
        }
134
      else if (trailer == std::string ("d"))
135
        {
136
          *this = Time::FromDouble (r, Time::D);
137
        }
138
      else if (trailer == std::string ("wk"))
139
        {
140
          *this = Time::FromDouble (r, Time::WK);
141
        }
142
      else if (trailer == std::string ("a"))
143
        {
144
          *this = Time::FromDouble (r, Time::A);
145
        }
126
      else
146
      else
127
        {
147
        {
128
          NS_ABORT_MSG ("Can't Parse Time " << s);
148
          NS_ABORT_MSG ("Can't Parse Time " << s);
 Lines 176-196    Link Here 
176
      ConvertTimes (unit);
196
      ConvertTimes (unit);
177
    }
197
    }
178
  int8_t power [LAST] = { 15, 12, 9, 6, 3, 0};
198
  // A, WK, D, H, MIN, S, MS, US, NS, PS, FS
199
  const int8_t power [LAST] = { 17, 17, 17, 17, 16, 15, 12, 9, 6, 3, 0 };
200
  const int32_t coefficient [LAST] = { 315576, 6048, 864, 36, 6, 1, 1, 1, 1, 1, 1 };
179
  for (int i = 0; i < Time::LAST; i++)
201
  for (int i = 0; i < Time::LAST; i++)
180
    {
202
    {
181
      int shift = power[i] - power[(int)unit];
203
      int shift = power[i] - power[(int)unit];
182
      int64_t factor = (int64_t) std::pow (10, std::fabs (shift));
204
      int quotient = 1;
205
      if (coefficient[i] > coefficient[(int) unit])
206
        {
207
          quotient = coefficient[i] / coefficient[(int) unit];
208
          NS_ASSERT (quotient * coefficient[(int) unit] == coefficient[i]);
209
        }
210
      else if (coefficient[i] < coefficient[(int) unit])
211
        {
212
          quotient = coefficient[(int) unit] / coefficient[i];
213
          NS_ASSERT (quotient * coefficient[i] == coefficient[(int) unit]);
214
        }
215
      NS_LOG_DEBUG ("SetResolution for unit " << (int) unit << " loop iteration " << i
216
    		    << " has shift " << shift << " has quotient " << quotient);
217
      int64_t factor = static_cast<int64_t> (std::pow (10, std::fabs (shift)) * quotient);
218
      double realFactor = std::pow (10, shift)
219
                        * static_cast<double> (coefficient[i]) / coefficient[(int) unit];
220
      NS_LOG_DEBUG ("SetResolution factor " << factor << " real factor " << realFactor);
183
      struct Information *info = &resolution->info[i];
221
      struct Information *info = &resolution->info[i];
184
      info->factor = factor;
222
      info->factor = factor;
185
      if (shift == 0)
223
      // here we could equivalently check for realFactor == 1.0 but it's better
224
      // to avoid checking equality of doubles
225
      if (shift == 0 && quotient == 1)
186
        {
226
        {
187
          info->timeFrom = int64x64_t (1);
227
          info->timeFrom = int64x64_t (1);
188
          info->timeTo = int64x64_t (1);
228
          info->timeTo = int64x64_t (1);
189
          info->toMul = true;
229
          info->toMul = true;
190
          info->fromMul = true;
230
          info->fromMul = true;
191
        }
231
        }
192
      else if (shift > 0)
232
      else if (realFactor > 1)
193
        {
233
        {
194
          info->timeFrom = int64x64_t (factor);
234
          info->timeFrom = int64x64_t (factor);
195
          info->timeTo = int64x64_t::Invert (factor);
235
          info->timeTo = int64x64_t::Invert (factor);
 Lines 199-205    Link Here 
199
        }
239
        }
200
      else
240
      else
201
        {
241
        {
202
          NS_ASSERT (shift < 0);
242
          NS_ASSERT (realFactor < 1);
203
          info->timeFrom = int64x64_t::Invert (factor);
243
          info->timeFrom = int64x64_t::Invert (factor);
204
          info->timeTo = int64x64_t (factor);
244
          info->timeTo = int64x64_t (factor);
205
          info->toMul = true;
245
          info->toMul = true;
 Lines 368-373    Link Here 
368
    case Time::FS:
408
    case Time::FS:
369
      unit = "fs";
409
      unit = "fs";
370
      break;
410
      break;
411
    case Time::MIN:
412
      unit = "min";
413
      break;
414
    case Time::H:
415
      unit = "h";
416
      break;
417
    case Time::D:
418
      unit = "d";
419
      break;
420
    case Time::WK:
421
      unit = "wk";
422
      break;
423
    case Time::A:
424
      unit = "a";
425
      break;
371
    case Time::LAST:
426
    case Time::LAST:
372
      NS_ABORT_MSG ("can't be reached");
427
      NS_ABORT_MSG ("can't be reached");
373
      unit = "unreachable";
428
      unit = "unreachable";

Return to bug 954