|
| 1 | +/*************************************************************************** |
| 2 | +
|
| 3 | + attotime.c |
| 4 | +
|
| 5 | + Support functions for working with attotime data. |
| 6 | +
|
| 7 | + Copyright (c) 1996-2007, Nicola Salmoria and the MAME Team. |
| 8 | + Visit http://mamedev.org for licensing and usage restrictions. |
| 9 | +
|
| 10 | +***************************************************************************/ |
| 11 | + |
| 12 | +#include "mamecore.h" |
| 13 | +#include "attotime.h" |
| 14 | +#include "eminline.h" |
| 15 | + |
| 16 | + |
| 17 | +/*************************************************************************** |
| 18 | + GLOBAL VARIABLES |
| 19 | +***************************************************************************/ |
| 20 | + |
| 21 | +const attotime attotime_zero = STATIC_ATTOTIME_IN_SEC(0); |
| 22 | +const attotime attotime_never = STATIC_ATTOTIME_IN_SEC(ATTOTIME_MAX_SECONDS); |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +/*************************************************************************** |
| 27 | + CONVERSION HELPERS |
| 28 | +***************************************************************************/ |
| 29 | + |
| 30 | +/*------------------------------------------------- |
| 31 | + attotime_to_attoseconds - convert a attotime |
| 32 | + to attoseconds, clamping to maximum positive/ |
| 33 | + negative values |
| 34 | +-------------------------------------------------*/ |
| 35 | + |
| 36 | +attoseconds_t attotime_to_attoseconds(attotime _time) |
| 37 | +{ |
| 38 | + /* positive values between 0 and 1 second */ |
| 39 | + if (_time.seconds == 0) |
| 40 | + return _time.attoseconds; |
| 41 | + |
| 42 | + /* negative values between -1 and 0 seconds */ |
| 43 | + else if (_time.seconds == -1) |
| 44 | + return _time.attoseconds - ATTOSECONDS_PER_SECOND; |
| 45 | + |
| 46 | + /* out-of-range positive values */ |
| 47 | + else if (_time.seconds > 0) |
| 48 | + return ATTOSECONDS_PER_SECOND; |
| 49 | + |
| 50 | + /* out-of-range negative values */ |
| 51 | + else |
| 52 | + return -ATTOSECONDS_PER_SECOND; |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +/*************************************************************************** |
| 58 | + CORE MATH FUNCTIONS |
| 59 | +***************************************************************************/ |
| 60 | + |
| 61 | +/*------------------------------------------------- |
| 62 | + attotime_add - add two attotimes |
| 63 | +-------------------------------------------------*/ |
| 64 | + |
| 65 | +attotime attotime_add(attotime _time1, attotime _time2) |
| 66 | +{ |
| 67 | + attotime result; |
| 68 | + |
| 69 | + /* if one of the items is attotime_never, return attotime_never */ |
| 70 | + if (_time1.seconds >= ATTOTIME_MAX_SECONDS || _time2.seconds >= ATTOTIME_MAX_SECONDS) |
| 71 | + return attotime_never; |
| 72 | + |
| 73 | + /* add the seconds and attoseconds */ |
| 74 | + result.attoseconds = _time1.attoseconds + _time2.attoseconds; |
| 75 | + result.seconds = _time1.seconds + _time2.seconds; |
| 76 | + |
| 77 | + /* normalize and return */ |
| 78 | + if (result.attoseconds >= ATTOSECONDS_PER_SECOND) |
| 79 | + { |
| 80 | + result.attoseconds -= ATTOSECONDS_PER_SECOND; |
| 81 | + result.seconds++; |
| 82 | + } |
| 83 | + |
| 84 | + /* overflow */ |
| 85 | + if (result.seconds >= ATTOTIME_MAX_SECONDS) |
| 86 | + return attotime_never; |
| 87 | + return result; |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | +/*------------------------------------------------- |
| 92 | + attotime_add_attoseconds - add attoseconds |
| 93 | + to a attotime |
| 94 | +-------------------------------------------------*/ |
| 95 | + |
| 96 | +attotime attotime_add_attoseconds(attotime _time1, attoseconds_t _attoseconds) |
| 97 | +{ |
| 98 | + attotime result; |
| 99 | + |
| 100 | + /* if one of the items is attotime_never, return attotime_never */ |
| 101 | + if (_time1.seconds >= ATTOTIME_MAX_SECONDS) |
| 102 | + return attotime_never; |
| 103 | + |
| 104 | + /* add the seconds and attoseconds */ |
| 105 | + result.attoseconds = _time1.attoseconds + _attoseconds; |
| 106 | + result.seconds = _time1.seconds; |
| 107 | + |
| 108 | + /* normalize and return */ |
| 109 | + if (result.attoseconds >= ATTOSECONDS_PER_SECOND) |
| 110 | + { |
| 111 | + result.attoseconds -= ATTOSECONDS_PER_SECOND; |
| 112 | + result.seconds++; |
| 113 | + } |
| 114 | + |
| 115 | + /* overflow */ |
| 116 | + if (result.seconds >= ATTOTIME_MAX_SECONDS) |
| 117 | + return attotime_never; |
| 118 | + return result; |
| 119 | +} |
| 120 | + |
| 121 | + |
| 122 | +/*------------------------------------------------- |
| 123 | + attotime_sub - subtract two attotimes |
| 124 | +-------------------------------------------------*/ |
| 125 | + |
| 126 | +attotime attotime_sub(attotime _time1, attotime _time2) |
| 127 | +{ |
| 128 | + attotime result; |
| 129 | + |
| 130 | + /* if time1 is attotime_never, return attotime_never */ |
| 131 | + if (_time1.seconds >= ATTOTIME_MAX_SECONDS) |
| 132 | + return attotime_never; |
| 133 | + |
| 134 | + /* add the seconds and attoseconds */ |
| 135 | + result.attoseconds = _time1.attoseconds - _time2.attoseconds; |
| 136 | + result.seconds = _time1.seconds - _time2.seconds; |
| 137 | + |
| 138 | + /* normalize and return */ |
| 139 | + if (result.attoseconds < 0) |
| 140 | + { |
| 141 | + result.attoseconds += ATTOSECONDS_PER_SECOND; |
| 142 | + result.seconds--; |
| 143 | + } |
| 144 | + return result; |
| 145 | +} |
| 146 | + |
| 147 | + |
| 148 | +/*------------------------------------------------- |
| 149 | + attotime_sub_attoseconds - subtract |
| 150 | + attoseconds from a attotime |
| 151 | +-------------------------------------------------*/ |
| 152 | + |
| 153 | +attotime attotime_sub_attoseconds(attotime _time1, attoseconds_t _attoseconds) |
| 154 | +{ |
| 155 | + attotime result; |
| 156 | + |
| 157 | + /* if time1 is attotime_never, return attotime_never */ |
| 158 | + if (_time1.seconds >= ATTOTIME_MAX_SECONDS) |
| 159 | + return attotime_never; |
| 160 | + |
| 161 | + /* add the seconds and attoseconds */ |
| 162 | + result.attoseconds = _time1.attoseconds - _attoseconds; |
| 163 | + result.seconds = _time1.seconds; |
| 164 | + |
| 165 | + /* normalize and return */ |
| 166 | + if (result.attoseconds < 0) |
| 167 | + { |
| 168 | + result.attoseconds += ATTOSECONDS_PER_SECOND; |
| 169 | + result.seconds--; |
| 170 | + } |
| 171 | + return result; |
| 172 | +} |
| 173 | + |
| 174 | + |
| 175 | +/*------------------------------------------------- |
| 176 | + attotime_mul - multiply an attotime by |
| 177 | + a constant |
| 178 | +-------------------------------------------------*/ |
| 179 | + |
| 180 | +attotime attotime_mul(attotime _time1, UINT32 factor) |
| 181 | +{ |
| 182 | + INT32 attolo, attohi, reslo, reshi; |
| 183 | + INT64 temp; |
| 184 | + |
| 185 | + /* if one of the items is attotime_never, return attotime_never */ |
| 186 | + if (_time1.seconds >= ATTOTIME_MAX_SECONDS) |
| 187 | + return attotime_never; |
| 188 | + |
| 189 | + /* 0 times anything is zero */ |
| 190 | + if (factor == 0) |
| 191 | + return attotime_zero; |
| 192 | + |
| 193 | + /* split attoseconds into upper and lower halves which fit into 32 bits */ |
| 194 | + attohi = div_64x32_rem(_time1.attoseconds, ATTOSECONDS_PER_SECOND_SQRT, &attolo); |
| 195 | + |
| 196 | + /* scale the lower half, then split into high/low parts */ |
| 197 | + temp = mul_32x32(attolo, factor); |
| 198 | + temp = div_64x32_rem(temp, ATTOSECONDS_PER_SECOND_SQRT, &reslo); |
| 199 | + |
| 200 | + /* scale the upper half, then split into high/low parts */ |
| 201 | + temp += mul_32x32(attohi, factor); |
| 202 | + temp = div_64x32_rem(temp, ATTOSECONDS_PER_SECOND_SQRT, &reshi); |
| 203 | + |
| 204 | + /* scale the seconds */ |
| 205 | + temp += mul_32x32(_time1.seconds, factor); |
| 206 | + if (temp >= ATTOTIME_MAX_SECONDS) |
| 207 | + return attotime_never; |
| 208 | + |
| 209 | + /* build the result */ |
| 210 | + return attotime_make(temp, (attoseconds_t)reslo + mul_32x32(reshi, ATTOSECONDS_PER_SECOND_SQRT)); |
| 211 | +} |
| 212 | + |
| 213 | + |
| 214 | +/*------------------------------------------------- |
| 215 | + attotime_div - divide an attotime by |
| 216 | + a constant |
| 217 | +-------------------------------------------------*/ |
| 218 | + |
| 219 | +attotime attotime_div(attotime _time1, UINT32 factor) |
| 220 | +{ |
| 221 | + INT32 attolo, attohi, reshi, reslo, remainder; |
| 222 | + attotime result; |
| 223 | + INT64 temp; |
| 224 | + |
| 225 | + /* if one of the items is attotime_never, return attotime_never */ |
| 226 | + if (_time1.seconds >= ATTOTIME_MAX_SECONDS) |
| 227 | + return attotime_never; |
| 228 | + |
| 229 | + /* ignore divide by zero */ |
| 230 | + if (factor == 0) |
| 231 | + return _time1; |
| 232 | + |
| 233 | + /* split attoseconds into upper and lower halves which fit into 32 bits */ |
| 234 | + attohi = div_64x32_rem(_time1.attoseconds, ATTOSECONDS_PER_SECOND_SQRT, &attolo); |
| 235 | + |
| 236 | + /* divide the seconds and get the remainder */ |
| 237 | + result.seconds = div_64x32_rem(_time1.seconds, factor, &remainder); |
| 238 | + |
| 239 | + /* combine the upper half of attoseconds with the remainder and divide that */ |
| 240 | + temp = (INT64)attohi + mul_32x32(remainder, ATTOSECONDS_PER_SECOND_SQRT); |
| 241 | + reshi = div_64x32_rem(temp, factor, &remainder); |
| 242 | + |
| 243 | + /* combine the lower half of attoseconds with the remainder and divide that */ |
| 244 | + temp = attolo + mul_32x32(remainder, ATTOSECONDS_PER_SECOND_SQRT); |
| 245 | + reslo = div_64x32_rem(temp, factor, &remainder); |
| 246 | + |
| 247 | + /* round based on the remainder */ |
| 248 | + result.attoseconds = (attoseconds_t)reslo + mul_32x32(reshi, ATTOSECONDS_PER_SECOND_SQRT); |
| 249 | + if (remainder >= factor / 2) |
| 250 | + if (++result.attoseconds >= ATTOSECONDS_PER_SECOND) |
| 251 | + { |
| 252 | + result.attoseconds = 0; |
| 253 | + result.seconds++; |
| 254 | + } |
| 255 | + return result; |
| 256 | +} |
| 257 | + |
| 258 | + |
| 259 | +/*------------------------------------------------- |
| 260 | + attotime_compare - compare two attotimes |
| 261 | +-------------------------------------------------*/ |
| 262 | + |
| 263 | +int attotime_compare(attotime _time1, attotime _time2) |
| 264 | +{ |
| 265 | + if (_time1.seconds > _time2.seconds) |
| 266 | + return 1; |
| 267 | + if (_time1.seconds < _time2.seconds) |
| 268 | + return -1; |
| 269 | + if (_time1.attoseconds > _time2.attoseconds) |
| 270 | + return 1; |
| 271 | + if (_time1.attoseconds < _time2.attoseconds) |
| 272 | + return -1; |
| 273 | + return 0; |
| 274 | +} |
| 275 | + |
| 276 | + |
| 277 | + |
| 278 | +/*************************************************************************** |
| 279 | + MISC UTILITIES |
| 280 | +***************************************************************************/ |
| 281 | + |
| 282 | +/*------------------------------------------------- |
| 283 | + attotime_compare - return a temporary |
| 284 | + printable string describing an attotime |
| 285 | +-------------------------------------------------*/ |
| 286 | + |
| 287 | +const char *attotime_string(attotime _time, int precision) |
| 288 | +{ |
| 289 | + static char buffers[8][30]; |
| 290 | + static int nextbuf; |
| 291 | + char *buffer = &buffers[nextbuf++ % 8][0]; |
| 292 | + |
| 293 | + /* case 1: we want no precision; seconds only */ |
| 294 | + if (precision == 0) |
| 295 | + sprintf(buffer, "%d", _time.seconds); |
| 296 | + |
| 297 | + /* case 2: we want 9 or fewer digits of precision */ |
| 298 | + else if (precision <= 9) |
| 299 | + { |
| 300 | + INT32 upper = _time.attoseconds / ATTOSECONDS_PER_SECOND_SQRT; |
| 301 | + sprintf(buffer, "%d.%0*d", _time.seconds, precision, upper); |
| 302 | + } |
| 303 | + |
| 304 | + /* case 3: more than 9 digits of precision */ |
| 305 | + else |
| 306 | + { |
| 307 | + INT32 lower; |
| 308 | + INT32 upper = div_64x32_rem(_time.attoseconds, ATTOSECONDS_PER_SECOND_SQRT, &lower); |
| 309 | + sprintf(buffer, "%d.%09d%0*d", _time.seconds, upper, precision - 9, lower); |
| 310 | + } |
| 311 | + return buffer; |
| 312 | +} |
0 commit comments