1 /*---------------------------------------------------------------------------* 2 Project: Horizon 3 File: fnd_TimeSpan.h 4 5 Copyright (C)2009-2012 Nintendo Co., Ltd. All rights reserved. 6 7 These coded instructions, statements, and computer programs contain 8 proprietary information of Nintendo of America Inc. and/or Nintendo 9 Company Ltd., and are protected by Federal copyright law. They may 10 not be disclosed to third parties or copied or duplicated in any form, 11 in whole or in part, without the prior written consent of Nintendo. 12 13 $Rev: 47798 $ 14 *---------------------------------------------------------------------------*/ 15 16 /* Please see man pages for details 17 18 19 */ 20 21 #ifndef NN_FND_TIMESPAN_H_ 22 #define NN_FND_TIMESPAN_H_ 23 24 #include <nn/types.h> 25 #include <nn/config.h> 26 #include <nn/os/os_HardwareParamsSelect.h> 27 28 #ifdef __cplusplus 29 30 namespace nn { namespace fnd { 31 32 /* Please see man pages for details 33 34 35 36 37 38 39 40 41 42 */ 43 class TimeSpan 44 { 45 private: 46 static const s64 TICKS_PER_SECOND = NN_HW_TICKS_PER_SECOND; 47 48 typedef const class ZeroOnlyTag {} * ZeroOnly; 49 50 public: 51 52 // 53 // 54 55 /* Please see man pages for details 56 57 58 59 60 61 62 63 */ 64 TimeSpan(ZeroOnly zeroOnly = 0) : m_NanoSeconds(0) { NN_UNUSED_VAR(zeroOnly); } 65 66 /* Please see man pages for details 67 68 69 70 71 72 */ FromNanoSeconds(s64 nanoSeconds)73 static TimeSpan FromNanoSeconds(s64 nanoSeconds) { TimeSpan ret; ret.m_NanoSeconds = nanoSeconds; return ret; } 74 75 /* Please see man pages for details 76 77 78 79 80 81 */ FromMicroSeconds(s64 microSeconds)82 static TimeSpan FromMicroSeconds(s64 microSeconds) { return FromNanoSeconds(microSeconds * 1000); } 83 84 /* Please see man pages for details 85 86 87 88 89 90 */ FromMilliSeconds(s64 milliSeconds)91 static TimeSpan FromMilliSeconds(s64 milliSeconds) { return FromNanoSeconds(milliSeconds * 1000 * 1000); } 92 93 /* Please see man pages for details 94 95 96 97 98 99 */ FromSeconds(s64 seconds)100 static TimeSpan FromSeconds(s64 seconds) { return FromNanoSeconds(seconds * 1000 * 1000 * 1000); } 101 102 /* Please see man pages for details 103 104 105 106 107 108 */ FromMinutes(s64 minutes)109 static TimeSpan FromMinutes(s64 minutes) { return FromNanoSeconds(minutes * 1000 * 1000 * 1000 * 60); } 110 111 /* Please see man pages for details 112 113 114 115 116 117 */ FromHours(s64 hours)118 static TimeSpan FromHours(s64 hours) { return FromNanoSeconds(hours * 1000 * 1000 * 1000 * 60 * 60); } 119 120 /* Please see man pages for details 121 122 123 124 125 126 */ FromDays(s64 days)127 static TimeSpan FromDays(s64 days) { return FromNanoSeconds(days * 1000 * 1000 * 1000 * 60 * 60 * 24); } 128 129 // 130 131 // 132 // 133 134 /* Please see man pages for details 135 136 137 138 */ GetDays()139 s64 GetDays() const { return DivideNanoSeconds(0x683fff6f48f948e3LL, 45); } 140 141 /* Please see man pages for details 142 143 144 145 */ GetHours()146 s64 GetHours() const { return DivideNanoSeconds(0x9c5fff26ed75ed55LL, 41); } 147 148 /* Please see man pages for details 149 150 151 152 */ GetMinutes()153 s64 GetMinutes() const { return DivideNanoSeconds(0x12533fe68fd3d1dLL, 28); } 154 155 /* Please see man pages for details 156 157 158 159 */ GetSeconds()160 s64 GetSeconds() const { return DivideNanoSeconds(0x112e0be826d694b3LL, 26); } 161 162 /* Please see man pages for details 163 164 165 166 */ GetMilliSeconds()167 s64 GetMilliSeconds() const { return DivideNanoSeconds(0x431bde82d7b634dbLL, 18); } 168 169 /* Please see man pages for details 170 171 172 173 */ GetMicroSeconds()174 s64 GetMicroSeconds() const { return DivideNanoSeconds(0x20c49ba5e353f7cfLL, 7); } 175 176 /* Please see man pages for details 177 178 179 180 */ GetNanoSeconds()181 s64 GetNanoSeconds() const { return m_NanoSeconds; } 182 183 // 184 185 //---------------------------------------------------------------------- 186 // 187 // 188 // 189 // 190 // 191 //---------------------------------------------------------------------- 192 friend bool operator==(const TimeSpan& lhs, const TimeSpan& rhs) { return lhs.m_NanoSeconds == rhs.m_NanoSeconds; } 193 194 //---------------------------------------------------------------------- 195 // 196 // 197 // 198 // 199 // 200 //---------------------------------------------------------------------- 201 friend bool operator!=(const TimeSpan& lhs, const TimeSpan& rhs) { return !(lhs == rhs); } 202 203 //---------------------------------------------------------------------- 204 // 205 // 206 // 207 // 208 // 209 //---------------------------------------------------------------------- 210 friend bool operator< (const TimeSpan& lhs, const TimeSpan& rhs) { return lhs.m_NanoSeconds < rhs.m_NanoSeconds; } 211 212 //---------------------------------------------------------------------- 213 // 214 // 215 // 216 // 217 // 218 //---------------------------------------------------------------------- 219 friend bool operator> (const TimeSpan& lhs, const TimeSpan& rhs) { return rhs < lhs; } 220 221 //---------------------------------------------------------------------- 222 // 223 // 224 // 225 // 226 // 227 //---------------------------------------------------------------------- 228 friend bool operator<=(const TimeSpan& lhs, const TimeSpan& rhs) { return !(lhs > rhs); } 229 230 //---------------------------------------------------------------------- 231 // 232 // 233 // 234 // 235 // 236 //---------------------------------------------------------------------- 237 friend bool operator>=(const TimeSpan& lhs, const TimeSpan& rhs) { return !(lhs < rhs); } 238 239 //---------------------------------------------------------------------- 240 // 241 // 242 // 243 // 244 //---------------------------------------------------------------------- 245 TimeSpan& operator+=(const TimeSpan& rhs) { this->m_NanoSeconds += rhs.m_NanoSeconds; return *this; } 246 247 //---------------------------------------------------------------------- 248 // 249 // 250 // 251 // 252 // 253 //---------------------------------------------------------------------- 254 friend TimeSpan operator+(const TimeSpan& lhs, const TimeSpan& rhs) { TimeSpan ret(lhs); return ret += rhs; } 255 256 //---------------------------------------------------------------------- 257 // 258 // 259 // 260 // 261 //---------------------------------------------------------------------- 262 TimeSpan& operator-=(const TimeSpan& rhs) { this->m_NanoSeconds -= rhs.m_NanoSeconds; return *this; } 263 264 //---------------------------------------------------------------------- 265 // 266 // 267 // 268 // 269 // 270 //---------------------------------------------------------------------- 271 friend TimeSpan operator-(const TimeSpan& lhs, const TimeSpan& rhs) { TimeSpan ret(lhs); return ret -= rhs; } 272 273 private: 274 s64 m_NanoSeconds; 275 276 private: DivideNanoSeconds(const s64 magic,const s32 rightShift)277 s64 DivideNanoSeconds(const s64 magic, const s32 rightShift) const 278 { 279 s64 n = MultiplyRightShift(m_NanoSeconds, magic); 280 if (magic < 0) 281 { 282 n += m_NanoSeconds; 283 } 284 n >>= rightShift; 285 return n + (static_cast<u64>(m_NanoSeconds) >> 63); 286 } 287 MultiplyRightShift(const s64 x,const s64 y)288 static s64 MultiplyRightShift(const s64 x, const s64 y) 289 { 290 const u64 x_lo = x & 0xffffffff; 291 const s64 x_hi = x >> 32; 292 293 const u64 y_lo = y & 0xffffffff; 294 const s64 y_hi = y >> 32; 295 296 const s64 z = x_hi * y_lo + ((x_lo * y_lo) >> 32); 297 const s64 z_lo = z & 0xffffffff; 298 const s64 z_hi = z >> 32; 299 300 return x_hi * y_hi + z_hi + (static_cast<s64>(x_lo * y_hi + z_lo) >> 32); 301 } 302 }; 303 304 }} // end of namespace nn 305 306 #endif // __cplusplus 307 308 #endif 309