src/osd/windows/eivc.h
| r0 | r1325 | |
| 1 | //============================================================
|
| 2 | //
|
| 3 | // eivc.h
|
| 4 | //
|
| 5 | // Inline implementations for MSVC compiler.
|
| 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 | #ifndef __EIVC__
|
| 13 | #define __EIVC__
|
| 14 | |
| 15 | #ifdef PTR64
|
| 16 | #include <intrin.h>
|
| 17 | #pragma intrinsic(_BitScanReverse)
|
| 18 | #else
|
| 19 | #pragma intrinsic(_InterlockedCompareExchange)
|
| 20 | #pragma intrinsic(_InterlockedExchange)
|
| 21 | #pragma intrinsic(_InterlockedExchangeAdd)
|
| 22 | #endif
|
| 23 | |
| 24 | |
| 25 | /***************************************************************************
|
| 26 | INLINE BIT MANIPULATION FUNCTIONS
|
| 27 | ***************************************************************************/
|
| 28 | |
| 29 | /*-------------------------------------------------
|
| 30 | count_leading_zeros - return the number of
|
| 31 | leading zero bits in a 32-bit value
|
| 32 | -------------------------------------------------*/
|
| 33 | |
| 34 | #ifndef count_leading_zeros
|
| 35 | #define count_leading_zeros _count_leading_zeros
|
| 36 | INLINE UINT8 _count_leading_zeros(UINT32 value)
|
| 37 | {
|
| 38 | UINT32 index;
|
| 39 | return _BitScanReverse(&index, value) ? (index ^ 31) : 32;
|
| 40 | }
|
| 41 | #endif
|
| 42 | |
| 43 | |
| 44 | /*-------------------------------------------------
|
| 45 | count_leading_ones - return the number of
|
| 46 | leading one bits in a 32-bit value
|
| 47 | -------------------------------------------------*/
|
| 48 | |
| 49 | #ifndef count_leading_ones
|
| 50 | #define count_leading_ones _count_leading_ones
|
| 51 | INLINE UINT8 _count_leading_ones(UINT32 value)
|
| 52 | {
|
| 53 | UINT32 index;
|
| 54 | return _BitScanReverse(&index, ~value) ? (index ^ 31) : 32;
|
| 55 | }
|
| 56 | #endif
|
| 57 | |
| 58 | |
| 59 | |
| 60 | /***************************************************************************
|
| 61 | INLINE SYNCHRONIZATION FUNCTIONS
|
| 62 | ***************************************************************************/
|
| 63 | |
| 64 | /*-------------------------------------------------
|
| 65 | compare_exchange32 - compare the 'compare'
|
| 66 | value against the memory at 'ptr'; if equal,
|
| 67 | swap in the 'exchange' value. Regardless,
|
| 68 | return the previous value at 'ptr'.
|
| 69 | -------------------------------------------------*/
|
| 70 | |
| 71 | #ifndef compare_exchange32
|
| 72 | #define compare_exchange32 _compare_exchange32
|
| 73 | INLINE INT32 _compare_exchange32(INT32 volatile *ptr, INT32 compare, INT32 exchange)
|
| 74 | {
|
| 75 | return _InterlockedCompareExchange(ptr, exchange, compare);
|
| 76 | }
|
| 77 | #endif
|
| 78 | |
| 79 | |
| 80 | /*-------------------------------------------------
|
| 81 | compare_exchange64 - compare the 'compare'
|
| 82 | value against the memory at 'ptr'; if equal,
|
| 83 | swap in the 'exchange' value. Regardless,
|
| 84 | return the previous value at 'ptr'.
|
| 85 | -------------------------------------------------*/
|
| 86 | |
| 87 | #ifdef PTR64
|
| 88 | #ifndef compare_exchange64
|
| 89 | #define compare_exchange64 _compare_exchange64
|
| 90 | INLINE INT64 _compare_exchange64(INT64 volatile *ptr, INT64 compare, INT64 exchange)
|
| 91 | {
|
| 92 | return _InterlockedCompareExchange64(ptr, exchange, compare);
|
| 93 | }
|
| 94 | #endif
|
| 95 | #endif
|
| 96 | |
| 97 | |
| 98 | /*-------------------------------------------------
|
| 99 | atomic_exchange32 - atomically exchange the
|
| 100 | exchange value with the memory at 'ptr',
|
| 101 | returning the original value.
|
| 102 | -------------------------------------------------*/
|
| 103 | |
| 104 | #ifndef atomic_exchange32
|
| 105 | #define atomic_exchange32 _atomic_exchange32
|
| 106 | INLINE INT32 _atomic_exchange32(INT32 volatile *ptr, INT32 exchange)
|
| 107 | {
|
| 108 | return _InterlockedExchange(ptr, exchange);
|
| 109 | }
|
| 110 | #endif
|
| 111 | |
| 112 | |
| 113 | /*-------------------------------------------------
|
| 114 | atomic_add32 - atomically add the delta value
|
| 115 | to the memory at 'ptr', returning the final
|
| 116 | result.
|
| 117 | -------------------------------------------------*/
|
| 118 | |
| 119 | #ifndef atomic_add32
|
| 120 | #define atomic_add32 _atomic_add32
|
| 121 | INLINE INT32 _atomic_add32(INT32 volatile *ptr, INT32 delta)
|
| 122 | {
|
| 123 | return _InterlockedExchangeAdd(ptr, delta) + delta;
|
| 124 | }
|
| 125 | #endif
|
| 126 | |
| 127 | |
| 128 | /*-------------------------------------------------
|
| 129 | atomic_increment32 - atomically increment the
|
| 130 | 32-bit value in memory at 'ptr', returning the
|
| 131 | final result.
|
| 132 | -------------------------------------------------*/
|
| 133 | |
| 134 | #ifndef atomic_increment32
|
| 135 | #define atomic_increment32 _atomic_increment32
|
| 136 | INLINE INT32 _atomic_increment32(INT32 volatile *ptr)
|
| 137 | {
|
| 138 | return _InterlockedIncrement(ptr);
|
| 139 | }
|
| 140 | #endif
|
| 141 | |
| 142 | |
| 143 | /*-------------------------------------------------
|
| 144 | atomic_decrement32 - atomically decrement the
|
| 145 | 32-bit value in memory at 'ptr', returning the
|
| 146 | final result.
|
| 147 | -------------------------------------------------*/
|
| 148 | |
| 149 | #ifndef atomic_decrement32
|
| 150 | #define atomic_decrement32 _atomic_decrement32
|
| 151 | INLINE INT32 _atomic_decrement32(INT32 volatile *ptr)
|
| 152 | {
|
| 153 | return _InterlockedDecrement(ptr);
|
| 154 | }
|
| 155 | #endif
|
| 156 | |
| 157 | |
| 158 | #endif /* __EIVC__ */
|
src/osd/windows/eivcx86.h
| r0 | r1325 | |
| 1 | //============================================================
|
| 2 | //
|
| 3 | // eivcx86.h
|
| 4 | //
|
| 5 | // x86 inline implementations for MSVC compiler.
|
| 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 | #ifndef __EIVCX86__
|
| 13 | #define __EIVCX86__
|
| 14 | |
| 15 | #ifdef PTR64
|
| 16 | #include <emmintrin.h>
|
| 17 | #endif
|
| 18 | |
| 19 | |
| 20 | /***************************************************************************
|
| 21 | INLINE MATH FUNCTIONS
|
| 22 | ***************************************************************************/
|
| 23 | |
| 24 | /*-------------------------------------------------
|
| 25 | mul_32x32 - perform a signed 32 bit x 32 bit
|
| 26 | multiply and return the full 64 bit result
|
| 27 | -------------------------------------------------*/
|
| 28 | |
| 29 | #ifndef PTR64
|
| 30 | #define mul_32x32 _mul_32x32
|
| 31 | INLINE INT64 _mul_32x32(INT32 a, INT32 b)
|
| 32 | {
|
| 33 | // in theory this should work, but it is untested
|
| 34 | __asm
|
| 35 | {
|
| 36 | mov eax,a
|
| 37 | imul b
|
| 38 | // leave results in edx:eax
|
| 39 | }
|
| 40 | }
|
| 41 | #endif
|
| 42 | |
| 43 | |
| 44 | /*-------------------------------------------------
|
| 45 | mulu_32x32 - perform an unsigned 32 bit x
|
| 46 | 32 bit multiply and return the full 64 bit
|
| 47 | result
|
| 48 | -------------------------------------------------*/
|
| 49 | |
| 50 | #ifndef PTR64
|
| 51 | #define mulu_32x32 _mulu_32x32
|
| 52 | INLINE UINT64 _mulu_32x32(UINT32 a, UINT32 b)
|
| 53 | {
|
| 54 | // in theory this should work, but it is untested
|
| 55 | __asm
|
| 56 | {
|
| 57 | mov eax,a
|
| 58 | mul b
|
| 59 | // leave results in edx:eax
|
| 60 | }
|
| 61 | }
|
| 62 | #endif
|
| 63 | |
| 64 | |
| 65 | /*-------------------------------------------------
|
| 66 | mul_32x32_hi - perform a signed 32 bit x 32 bit
|
| 67 | multiply and return the upper 32 bits of the
|
| 68 | result
|
| 69 | -------------------------------------------------*/
|
| 70 | |
| 71 | #ifndef PTR64
|
| 72 | #define mul_32x32_hi _mul_32x32_hi
|
| 73 | INLINE INT32 _mul_32x32_hi(INT32 a, INT32 b)
|
| 74 | {
|
| 75 | INT32 result;
|
| 76 | |
| 77 | __asm
|
| 78 | {
|
| 79 | mov eax,a
|
| 80 | imul b
|
| 81 | mov result,edx
|
| 82 | }
|
| 83 | |
| 84 | return result;
|
| 85 | }
|
| 86 | #endif
|
| 87 | |
| 88 | |
| 89 | /*-------------------------------------------------
|
| 90 | mulu_32x32_hi - perform an unsigned 32 bit x
|
| 91 | 32 bit multiply and return the upper 32 bits
|
| 92 | of the result
|
| 93 | -------------------------------------------------*/
|
| 94 | |
| 95 | #ifndef PTR64
|
| 96 | #define mulu_32x32_hi _mulu_32x32_hi
|
| 97 | INLINE UINT32 _mulu_32x32_hi(UINT32 a, UINT32 b)
|
| 98 | {
|
| 99 | INT32 result;
|
| 100 | |
| 101 | __asm
|
| 102 | {
|
| 103 | mov eax,a
|
| 104 | mul b
|
| 105 | mov result,edx
|
| 106 | }
|
| 107 | |
| 108 | return result;
|
| 109 | }
|
| 110 | #endif
|
| 111 | |
| 112 | |
| 113 | /*-------------------------------------------------
|
| 114 | mul_32x32_shift - perform a signed 32 bit x
|
| 115 | 32 bit multiply and shift the result by the
|
| 116 | given number of bits before truncating the
|
| 117 | result to 32 bits
|
| 118 | -------------------------------------------------*/
|
| 119 | |
| 120 | #ifndef PTR64
|
| 121 | #define mul_32x32_shift _mul_32x32_shift
|
| 122 | INLINE INT32 _mul_32x32_shift(INT32 a, INT32 b, UINT8 shift)
|
| 123 | {
|
| 124 | INT32 result;
|
| 125 | |
| 126 | __asm
|
| 127 | {
|
| 128 | mov eax,a
|
| 129 | imul b
|
| 130 | mov cl,shift
|
| 131 | shrd eax,edx,cl
|
| 132 | mov result,eax
|
| 133 | }
|
| 134 | |
| 135 | return result;
|
| 136 | }
|
| 137 | #endif
|
| 138 | |
| 139 | |
| 140 | /*-------------------------------------------------
|
| 141 | mulu_32x32_shift - perform an unsigned 32 bit x
|
| 142 | 32 bit multiply and shift the result by the
|
| 143 | given number of bits before truncating the
|
| 144 | result to 32 bits
|
| 145 | -------------------------------------------------*/
|
| 146 | |
| 147 | #ifndef PTR64
|
| 148 | #define mulu_32x32_shift _mulu_32x32_shift
|
| 149 | INLINE UINT32 _mulu_32x32_shift(UINT32 a, UINT32 b, UINT8 shift)
|
| 150 | {
|
| 151 | INT32 result;
|
| 152 | |
| 153 | __asm
|
| 154 | {
|
| 155 | mov eax,a
|
| 156 | mul b
|
| 157 | mov cl,shift
|
| 158 | shrd eax,edx,cl
|
| 159 | mov result,eax
|
| 160 | }
|
| 161 | |
| 162 | return result;
|
| 163 | }
|
| 164 | #endif
|
| 165 | |
| 166 | |
| 167 | /*-------------------------------------------------
|
| 168 | div_64x32 - perform a signed 64 bit x 32 bit
|
| 169 | divide and return the 32 bit quotient
|
| 170 | -------------------------------------------------*/
|
| 171 | |
| 172 | #ifndef PTR64
|
| 173 | #define div_64x32 _div_64x32
|
| 174 | INLINE INT32 _div_64x32(INT64 a, INT32 b)
|
| 175 | {
|
| 176 | INT32 result;
|
| 177 | INT32 alow = a;
|
| 178 | INT32 ahigh = a >> 32;
|
| 179 | |
| 180 | __asm
|
| 181 | {
|
| 182 | mov eax,alow
|
| 183 | mov edx,ahigh
|
| 184 | idiv b
|
| 185 | mov result,eax
|
| 186 | }
|
| 187 | |
| 188 | return result;
|
| 189 | }
|
| 190 | #endif
|
| 191 | |
| 192 | |
| 193 | /*-------------------------------------------------
|
| 194 | divu_64x32 - perform an unsigned 64 bit x 32 bit
|
| 195 | divide and return the 32 bit quotient
|
| 196 | -------------------------------------------------*/
|
| 197 | |
| 198 | #ifndef PTR64
|
| 199 | #define divu_64x32 _divu_64x32
|
| 200 | INLINE UINT32 _divu_64x32(UINT64 a, UINT32 b)
|
| 201 | {
|
| 202 | UINT32 result;
|
| 203 | UINT32 alow = a;
|
| 204 | UINT32 ahigh = a >> 32;
|
| 205 | |
| 206 | __asm
|
| 207 | {
|
| 208 | mov eax,alow
|
| 209 | mov edx,ahigh
|
| 210 | div b
|
| 211 | mov result,eax
|
| 212 | }
|
| 213 | |
| 214 | return result;
|
| 215 | }
|
| 216 | #endif
|
| 217 | |
| 218 | |
| 219 | /*-------------------------------------------------
|
| 220 | div_64x32_rem - perform a signed 64 bit x 32
|
| 221 | bit divide and return the 32 bit quotient and
|
| 222 | 32 bit remainder
|
| 223 | -------------------------------------------------*/
|
| 224 | |
| 225 | #ifndef PTR64
|
| 226 | #define div_64x32_rem _div_64x32_rem
|
| 227 | INLINE INT32 _div_64x32_rem(INT64 a, INT32 b, INT32 *remainder)
|
| 228 | {
|
| 229 | INT32 result;
|
| 230 | INT32 alow = a;
|
| 231 | INT32 ahigh = a >> 32;
|
| 232 | INT32 rem;
|
| 233 | |
| 234 | __asm
|
| 235 | {
|
| 236 | mov eax,alow
|
| 237 | mov edx,ahigh
|
| 238 | idiv b
|
| 239 | mov result,eax
|
| 240 | mov rem,edx
|
| 241 | }
|
| 242 | |
| 243 | *remainder = rem;
|
| 244 | return result;
|
| 245 | }
|
| 246 | #endif
|
| 247 | |
| 248 | |
| 249 | /*-------------------------------------------------
|
| 250 | divu_64x32_rem - perform an unsigned 64 bit x
|
| 251 | 32 bit divide and return the 32 bit quotient
|
| 252 | and 32 bit remainder
|
| 253 | -------------------------------------------------*/
|
| 254 | |
| 255 | #ifndef PTR64
|
| 256 | #define divu_64x32_rem _divu_64x32_rem
|
| 257 | INLINE UINT32 _divu_64x32_rem(UINT64 a, UINT32 b, UINT32 *remainder)
|
| 258 | {
|
| 259 | UINT32 result;
|
| 260 | UINT32 alow = a;
|
| 261 | UINT32 ahigh = a >> 32;
|
| 262 | UINT32 rem;
|
| 263 | |
| 264 | __asm
|
| 265 | {
|
| 266 | mov eax,alow
|
| 267 | mov edx,ahigh
|
| 268 | div b
|
| 269 | mov result,eax
|
| 270 | mov rem,edx
|
| 271 | }
|
| 272 | |
| 273 | *remainder = rem;
|
| 274 | return result;
|
| 275 | }
|
| 276 | #endif
|
| 277 | |
| 278 | |
| 279 | /*-------------------------------------------------
|
| 280 | div_32x32_shift - perform a signed divide of
|
| 281 | two 32 bit values, shifting the first before
|
| 282 | division, and returning the 32 bit quotient
|
| 283 | -------------------------------------------------*/
|
| 284 | |
| 285 | #ifndef PTR64
|
| 286 | #define div_32x32_shift _div_32x32_shift
|
| 287 | INLINE INT32 _div_32x32_shift(INT32 a, INT32 b, UINT8 shift)
|
| 288 | {
|
| 289 | INT32 result;
|
| 290 | |
| 291 | __asm
|
| 292 | {
|
| 293 | mov eax,a
|
| 294 | cdq
|
| 295 | mov cl,shift
|
| 296 | shld edx,eax,cl
|
| 297 | shl eax,cl
|
| 298 | idiv b
|
| 299 | mov result,eax
|
| 300 | }
|
| 301 | |
| 302 | return result;
|
| 303 | }
|
| 304 | #endif
|
| 305 | |
| 306 | |
| 307 | /*-------------------------------------------------
|
| 308 | divu_32x32_shift - perform an unsigned divide of
|
| 309 | two 32 bit values, shifting the first before
|
| 310 | division, and returning the 32 bit quotient
|
| 311 | -------------------------------------------------*/
|
| 312 | |
| 313 | #ifndef PTR64
|
| 314 | #define divu_32x32_shift _divu_32x32_shift
|
| 315 | INLINE UINT32 _divu_32x32_shift(UINT32 a, UINT32 b, UINT8 shift)
|
| 316 | {
|
| 317 | UINT32 result;
|
| 318 | |
| 319 | __asm
|
| 320 | {
|
| 321 | mov eax,a
|
| 322 | xor edx,edx
|
| 323 | mov cl,shift
|
| 324 | shld edx,eax,cl
|
| 325 | shl eax,cl
|
| 326 | div b
|
| 327 | mov result,eax
|
| 328 | }
|
| 329 | |
| 330 | return result;
|
| 331 | }
|
| 332 | #endif
|
| 333 | |
| 334 | |
| 335 | /*-------------------------------------------------
|
| 336 | mod_64x32 - perform a signed 64 bit x 32 bit
|
| 337 | divide and return the 32 bit remainder
|
| 338 | -------------------------------------------------*/
|
| 339 | |
| 340 | #ifndef PTR64
|
| 341 | #define mod_64x32 _mod_64x32
|
| 342 | INLINE INT32 _mod_64x32(INT64 a, INT32 b)
|
| 343 | {
|
| 344 | INT32 result;
|
| 345 | INT32 alow = a;
|
| 346 | INT32 ahigh = a >> 32;
|
| 347 | |
| 348 | __asm
|
| 349 | {
|
| 350 | mov eax,alow
|
| 351 | mov edx,ahigh
|
| 352 | idiv b
|
| 353 | mov result,edx
|
| 354 | }
|
| 355 | |
| 356 | return result;
|
| 357 | }
|
| 358 | #endif
|
| 359 | |
| 360 | |
| 361 | /*-------------------------------------------------
|
| 362 | modu_64x32 - perform an unsigned 64 bit x 32 bit
|
| 363 | divide and return the 32 bit remainder
|
| 364 | -------------------------------------------------*/
|
| 365 | |
| 366 | #ifndef PTR64
|
| 367 | #define modu_64x32 _modu_64x32
|
| 368 | INLINE UINT32 _modu_64x32(UINT64 a, UINT32 b)
|
| 369 | {
|
| 370 | UINT32 result;
|
| 371 | UINT32 alow = a;
|
| 372 | UINT32 ahigh = a >> 32;
|
| 373 | |
| 374 | __asm
|
| 375 | {
|
| 376 | mov eax,alow
|
| 377 | mov edx,ahigh
|
| 378 | div b
|
| 379 | mov result,edx
|
| 380 | }
|
| 381 | |
| 382 | return result;
|
| 383 | }
|
| 384 | #endif
|
| 385 | |
| 386 | |
| 387 | /*-------------------------------------------------
|
| 388 | recip_approx - compute an approximate floating
|
| 389 | point reciprocal
|
| 390 | -------------------------------------------------*/
|
| 391 | |
| 392 | #ifdef PTR64
|
| 393 | #define recip_approx _recip_approx
|
| 394 | INLINE float _recip_approx(float z)
|
| 395 | {
|
| 396 | __m128 mz = _mm_set_ss(z);
|
| 397 | __m128 mooz = _mm_rcp_ss(mz);
|
| 398 | float ooz;
|
| 399 | _mm_store_ss(&ooz, mooz);
|
| 400 | return ooz;
|
| 401 | }
|
| 402 | #endif
|
| 403 | |
| 404 | |
| 405 | |
| 406 | /***************************************************************************
|
| 407 | INLINE BIT MANIPULATION FUNCTIONS
|
| 408 | ***************************************************************************/
|
| 409 | |
| 410 | /*-------------------------------------------------
|
| 411 | count_leading_zeros - return the number of
|
| 412 | leading zero bits in a 32-bit value
|
| 413 | -------------------------------------------------*/
|
| 414 | |
| 415 | #ifndef PTR64
|
| 416 | #define count_leading_zeros _count_leading_zeros
|
| 417 | INLINE UINT8 _count_leading_zeros(UINT32 value)
|
| 418 | {
|
| 419 | INT32 result;
|
| 420 | |
| 421 | __asm
|
| 422 | {
|
| 423 | bsr eax,value
|
| 424 | jnz skip
|
| 425 | mov eax,63
|
| 426 | skip:
|
| 427 | xor eax,31
|
| 428 | mov result,eax
|
| 429 | }
|
| 430 | |
| 431 | return result;
|
| 432 | }
|
| 433 | #endif
|
| 434 | |
| 435 | |
| 436 | /*-------------------------------------------------
|
| 437 | count_leading_ones - return the number of
|
| 438 | leading one bits in a 32-bit value
|
| 439 | -------------------------------------------------*/
|
| 440 | |
| 441 | #ifndef PTR64
|
| 442 | #define count_leading_ones _count_leading_ones
|
| 443 | INLINE UINT8 _count_leading_ones(UINT32 value)
|
| 444 | {
|
| 445 | INT32 result;
|
| 446 | |
| 447 | __asm
|
| 448 | {
|
| 449 | mov eax,value
|
| 450 | not eax
|
| 451 | bsr eax,eax
|
| 452 | jnz skip
|
| 453 | mov eax,63
|
| 454 | skip:
|
| 455 | xor eax,31
|
| 456 | mov result,eax
|
| 457 | }
|
| 458 | |
| 459 | return result;
|
| 460 | }
|
| 461 | #endif
|
| 462 | |
| 463 | #endif /* __EIVCX86__ */
|
src/osd/windows/winalloc.c
| r1324 | r1325 | |
| 30 | 30 | #define PAGE_SIZE 4096 |
| 31 | 31 | #define COOKIE_VAL 0x11335577 |
| 32 | 32 | |
| 33 | | // set this to 0 to not use guard pages |
| 34 | | #define USE_GUARD_PAGES 1 |
| 35 | | |
| 36 | 33 | // set this to 1 to align memory blocks to the start of a page; |
| 37 | 34 | // otherwise, they are aligned to the end, thus catching array |
| 38 | 35 | // overruns |
| r1324 | r1325 | |
| 188 | 185 | } |
| 189 | 186 | |
| 190 | 187 | |
| 188 | INLINE int use_malloc_tracking(void) |
| 189 | { |
| 190 | #ifdef MESS |
| 191 | extern BOOL win_is_gui_application(void); |
| 192 | return !win_is_gui_application(); |
| 193 | #elif defined(WINUI) |
| 194 | return FALSE; |
| 195 | #else |
| 196 | return TRUE; |
| 197 | #endif |
| 191 | 198 | |
| 199 | } |
| 200 | |
| 201 | |
| 202 | |
| 192 | 203 | //============================================================ |
| 193 | 204 | // IMPLEMENTATION |
| 194 | 205 | //============================================================ |
| 195 | 206 | |
| 196 | 207 | void *malloc_file_line(size_t size, const char *file, int line) |
| 197 | 208 | { |
| 198 | | UINT8 *page_base, *block_base; |
| 199 | | size_t rounded_size; |
| 200 | | memory_entry *entry; |
| 209 | UINT8 *block_base; |
| 210 | int id = current_id++; |
| 201 | 211 | |
| 202 | | if (USE_GUARD_PAGES) |
| 212 | if (use_malloc_tracking()) |
| 203 | 213 | { |
| 214 | UINT8 *page_base; |
| 215 | size_t rounded_size; |
| 216 | memory_entry *entry; |
| 217 | |
| 204 | 218 | // round the size up to a page boundary |
| 205 | 219 | rounded_size = ((size + PAGE_SIZE - 1) / PAGE_SIZE) * PAGE_SIZE; |
| 206 | 220 | |
| r1324 | r1325 | |
| 219 | 233 | block_base = page_base; |
| 220 | 234 | else |
| 221 | 235 | block_base = page_base + rounded_size - size; |
| 236 | |
| 237 | // fill in the entry |
| 238 | entry = allocate_entry(); |
| 239 | entry->size = size; |
| 240 | entry->base = block_base; |
| 241 | entry->file = file; |
| 242 | entry->line = line; |
| 243 | entry->id = id; |
| 222 | 244 | } |
| 223 | 245 | else |
| 224 | 246 | { |
| 225 | 247 | block_base = (UINT8 *)GlobalAlloc(GMEM_FIXED, size); |
| 226 | 248 | } |
| 227 | 249 | |
| 228 | | // fill in the entry |
| 229 | | entry = allocate_entry(); |
| 230 | | entry->size = size; |
| 231 | | entry->base = block_base; |
| 232 | | entry->file = file; |
| 233 | | entry->line = line; |
| 234 | | entry->id = current_id++; |
| 235 | | |
| 236 | | //if (entry->size == 72 && IsDebuggerPresent()) DebugBreak(); |
| 237 | | |
| 238 | 250 | #if LOG_CALLS |
| 239 | 251 | // logging |
| 240 | | if (entry->file) |
| 241 | | logerror("malloc #%06d size = %d (%s:%d)\n", entry->size, entry->file, entry->line); |
| 252 | if (file != NULL) |
| 253 | logerror("malloc #%06d size = %d (%s:%d)\n", id, size, file, line); |
| 242 | 254 | else |
| 243 | | logerror("malloc #%06d size = %d\n", entry->id, entry->size); |
| 244 | | #endif |
| 255 | logerror("malloc #%06d size = %d\n", id, size); |
| 256 | #endif // LOG_CALLS |
| 245 | 257 | |
| 246 | 258 | return block_base; |
| 247 | 259 | } |
| r1324 | r1325 | |
| 283 | 295 | { |
| 284 | 296 | void *newmemory = NULL; |
| 285 | 297 | |
| 286 | | // if size is non-zero, we need to reallocate memory |
| 287 | | if (size != 0) |
| 298 | if (use_malloc_tracking()) |
| 288 | 299 | { |
| 289 | | // allocate space for the new amount |
| 290 | | newmemory = malloc_file_line(size, file, line); |
| 291 | | if (newmemory == NULL) |
| 292 | | return NULL; |
| 300 | // if size is non-zero, we need to reallocate memory |
| 301 | if (size != 0) |
| 302 | { |
| 303 | // allocate space for the new amount |
| 304 | newmemory = malloc_file_line(size, file, line); |
| 305 | if (newmemory == NULL) |
| 306 | return NULL; |
| 293 | 307 | |
| 294 | | // if we have an old pointer, copy it |
| 295 | | if (memory != NULL) |
| 296 | | { |
| 297 | | memory_entry *entry = find_entry(memory); |
| 298 | | if (entry == NULL) |
| 308 | // if we have an old pointer, copy it |
| 309 | if (memory != NULL) |
| 299 | 310 | { |
| 300 | | if (winalloc_in_main_code) |
| 311 | memory_entry *entry = find_entry(memory); |
| 312 | if (entry == NULL) |
| 301 | 313 | { |
| 302 | | fprintf(stderr, "Error: realloc a non-existant block (%s:%d)\n", file, line); |
| 303 | | osd_break_into_debugger("Error: realloc a non-existant block\n"); |
| 314 | if (winalloc_in_main_code) |
| 315 | { |
| 316 | fprintf(stderr, "Error: realloc a non-existant block (%s:%d)\n", file, line); |
| 317 | osd_break_into_debugger("Error: realloc a non-existant block\n"); |
| 318 | } |
| 304 | 319 | } |
| 320 | else |
| 321 | memcpy(newmemory, memory, (size < entry->size) ? size : entry->size); |
| 305 | 322 | } |
| 306 | | else |
| 307 | | memcpy(newmemory, memory, (size < entry->size) ? size : entry->size); |
| 308 | 323 | } |
| 324 | |
| 325 | // if we have an original pointer, free it |
| 326 | if (memory != NULL) |
| 327 | free(memory); |
| 309 | 328 | } |
| 329 | else |
| 330 | { |
| 331 | newmemory = (void *) GlobalReAlloc(memory, size, GMEM_MOVEABLE); |
| 332 | } |
| 310 | 333 | |
| 311 | | // if we have an original pointer, free it |
| 312 | | if (memory != NULL) |
| 313 | | free(memory); |
| 314 | | |
| 315 | 334 | return newmemory; |
| 316 | 335 | } |
| 317 | 336 | |
| r1324 | r1325 | |
| 330 | 349 | if (memory == NULL) |
| 331 | 350 | return; |
| 332 | 351 | |
| 333 | | // error if no entry found |
| 334 | | entry = find_entry(memory); |
| 335 | | if (entry == NULL) |
| 352 | if (use_malloc_tracking()) |
| 336 | 353 | { |
| 337 | | if (winalloc_in_main_code) |
| 354 | // error if no entry found |
| 355 | entry = find_entry(memory); |
| 356 | if (entry == NULL) |
| 338 | 357 | { |
| 339 | | fprintf(stderr, "Error: free a non-existant block\n"); |
| 340 | | osd_break_into_debugger("Error: free a non-existant block"); |
| 358 | if (winalloc_in_main_code) |
| 359 | { |
| 360 | fprintf(stderr, "Error: free a non-existant block\n"); |
| 361 | osd_break_into_debugger("Error: free a non-existant block"); |
| 362 | } |
| 363 | return; |
| 341 | 364 | } |
| 342 | | return; |
| 343 | | } |
| 344 | | free_entry(entry); |
| 365 | free_entry(entry); |
| 345 | 366 | |
| 346 | | // free the memory |
| 347 | | if (USE_GUARD_PAGES) |
| 367 | // free the memory |
| 348 | 368 | VirtualFree((UINT8 *)memory - ((size_t)memory & (PAGE_SIZE-1)) - PAGE_SIZE, 0, MEM_RELEASE); |
| 369 | |
| 370 | #if LOG_CALLS |
| 371 | logerror("free #%06d size = %d\n", entry->id, entry->size); |
| 372 | #endif // LOG_CALLS |
| 373 | } |
| 349 | 374 | else |
| 375 | { |
| 350 | 376 | GlobalFree(memory); |
| 351 | | |
| 352 | | #if LOG_CALLS |
| 353 | | logerror("free #%06d size = %d\n", entry->id, entry->size); |
| 354 | | #endif |
| 377 | } |
| 355 | 378 | } |
| 356 | 379 | |
| 357 | 380 | |
| r1324 | r1325 | |
| 376 | 399 | memory_entry *entry; |
| 377 | 400 | int total = 0; |
| 378 | 401 | |
| 379 | | memory_lock_acquire(); |
| 402 | if (use_malloc_tracking()) |
| 403 | { |
| 404 | memory_lock_acquire(); |
| 380 | 405 | |
| 381 | | // check for leaked memory |
| 382 | | for (entry = alloc_list; entry; entry = entry->next) |
| 383 | | if (entry->file != NULL) |
| 384 | | { |
| 385 | | if (total == 0) |
| 386 | | fprintf(stderr, "--- memory leak warning ---\n"); |
| 387 | | total += entry->size; |
| 388 | | fprintf(stderr, "allocation #%06d, %d bytes (%s:%d)\n", entry->id, entry->size, entry->file, entry->line); |
| 389 | | } |
| 406 | // check for leaked memory |
| 407 | for (entry = alloc_list; entry; entry = entry->next) |
| 408 | if (entry->file != NULL) |
| 409 | { |
| 410 | if (total == 0) |
| 411 | fprintf(stderr, "--- memory leak warning ---\n"); |
| 412 | total += entry->size; |
| 413 | fprintf(stderr, "allocation #%06d, %d bytes (%s:%d)\n", entry->id, entry->size, entry->file, entry->line); |
| 414 | } |
| 390 | 415 | |
| 391 | | memory_lock_release(); |
| 416 | memory_lock_release(); |
| 392 | 417 | |
| 393 | | if (total > 0) |
| 394 | | fprintf(stderr, "a total of %d bytes were not free()'d\n", total); |
| 418 | if (total > 0) |
| 419 | fprintf(stderr, "a total of %d bytes were not free()'d\n", total); |
| 420 | } |
| 395 | 421 | } |
src/osd/windows/osinline.h
| r1324 | r1325 | |
| 2 | 2 | // |
| 3 | 3 | // osinline.h |
| 4 | 4 | // |
| 5 | | // x86 inline implementations for MSVC compiler. |
| 5 | // Inline implementations for non-GCC Win32 compilers |
| 6 | 6 | // |
| 7 | 7 | // Copyright (c) 1996-2007, Nicola Salmoria and the MAME Team. |
| 8 | 8 | // Visit http://mamedev.org for licensing and usage restrictions. |
| r1324 | r1325 | |
| 12 | 12 | #ifndef __OSINLINE__ |
| 13 | 13 | #define __OSINLINE__ |
| 14 | 14 | |
| 15 | | #ifdef PTR64 |
| 16 | | #include <intrin.h> |
| 17 | | #pragma intrinsic(_BitScanReverse) |
| 18 | | #else |
| 19 | | #pragma intrinsic(_InterlockedCompareExchange) |
| 20 | | #pragma intrinsic(_InterlockedExchange) |
| 21 | | #pragma intrinsic(_InterlockedExchangeAdd) |
| 15 | #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64)) |
| 16 | #include "eivcx86.h" |
| 22 | 17 | #endif |
| 23 | 18 | |
| 24 | | |
| 25 | | /*************************************************************************** |
| 26 | | INLINE MATH FUNCTIONS |
| 27 | | ***************************************************************************/ |
| 28 | | |
| 29 | | /*------------------------------------------------- |
| 30 | | mul_32x32 - perform a signed 32 bit x 32 bit |
| 31 | | multiply and return the full 64 bit result |
| 32 | | -------------------------------------------------*/ |
| 33 | | |
| 34 | | #ifndef PTR64 |
| 35 | | #define mul_32x32 _mul_32x32 |
| 36 | | INLINE INT64 _mul_32x32(INT32 a, INT32 b) |
| 37 | | { |
| 38 | | // in theory this should work, but it is untested |
| 39 | | __asm |
| 40 | | { |
| 41 | | mov eax,a |
| 42 | | imul b |
| 43 | | // leave results in edx:eax |
| 44 | | } |
| 45 | | } |
| 19 | #if defined(_MSC_VER) |
| 20 | #include "eivc.h" |
| 46 | 21 | #endif |
| 47 | 22 | |
| 23 | INT32 win_compare_exchange32(INT32 volatile *ptr, INT32 compare, INT32 exchange); |
| 24 | INT32 win_atomic_exchange32(INT32 volatile *ptr, INT32 exchange); |
| 25 | INT32 win_atomic_add32(INT32 volatile *ptr, INT32 delta); |
| 48 | 26 | |
| 49 | | /*------------------------------------------------- |
| 50 | | mulu_32x32 - perform an unsigned 32 bit x |
| 51 | | 32 bit multiply and return the full 64 bit |
| 52 | | result |
| 53 | | -------------------------------------------------*/ |
| 54 | | |
| 55 | | #ifndef PTR64 |
| 56 | | #define mulu_32x32 _mulu_32x32 |
| 57 | | INLINE UINT64 _mulu_32x32(UINT32 a, UINT32 b) |
| 58 | | { |
| 59 | | // in theory this should work, but it is untested |
| 60 | | __asm |
| 61 | | { |
| 62 | | mov eax,a |
| 63 | | mul b |
| 64 | | // leave results in edx:eax |
| 65 | | } |
| 66 | | } |
| 67 | | #endif |
| 68 | | |
| 69 | | |
| 70 | | /*------------------------------------------------- |
| 71 | | mul_32x32_hi - perform a signed 32 bit x 32 bit |
| 72 | | multiply and return the upper 32 bits of the |
| 73 | | result |
| 74 | | -------------------------------------------------*/ |
| 75 | | |
| 76 | | #ifndef PTR64 |
| 77 | | #define mul_32x32_hi _mul_32x32_hi |
| 78 | | INLINE INT32 _mul_32x32_hi(INT32 a, INT32 b) |
| 79 | | { |
| 80 | | INT32 result; |
| 81 | | |
| 82 | | __asm |
| 83 | | { |
| 84 | | mov eax,a |
| 85 | | imul b |
| 86 | | mov result,edx |
| 87 | | } |
| 88 | | |
| 89 | | return result; |
| 90 | | } |
| 91 | | #endif |
| 92 | | |
| 93 | | |
| 94 | | /*------------------------------------------------- |
| 95 | | mulu_32x32_hi - perform an unsigned 32 bit x |
| 96 | | 32 bit multiply and return the upper 32 bits |
| 97 | | of the result |
| 98 | | -------------------------------------------------*/ |
| 99 | | |
| 100 | | #ifndef PTR64 |
| 101 | | #define mulu_32x32_hi _mulu_32x32_hi |
| 102 | | INLINE UINT32 _mulu_32x32_hi(UINT32 a, UINT32 b) |
| 103 | | { |
| 104 | | INT32 result; |
| 105 | | |
| 106 | | __asm |
| 107 | | { |
| 108 | | mov eax,a |
| 109 | | mul b |
| 110 | | mov result,edx |
| 111 | | } |
| 112 | | |
| 113 | | return result; |
| 114 | | } |
| 115 | | #endif |
| 116 | | |
| 117 | | |
| 118 | | /*------------------------------------------------- |
| 119 | | mul_32x32_shift - perform a signed 32 bit x |
| 120 | | 32 bit multiply and shift the result by the |
| 121 | | given number of bits before truncating the |
| 122 | | result to 32 bits |
| 123 | | -------------------------------------------------*/ |
| 124 | | |
| 125 | | #ifndef PTR64 |
| 126 | | #define mul_32x32_shift _mul_32x32_shift |
| 127 | | INLINE INT32 _mul_32x32_shift(INT32 a, INT32 b, UINT8 shift) |
| 128 | | { |
| 129 | | INT32 result; |
| 130 | | |
| 131 | | __asm |
| 132 | | { |
| 133 | | mov eax,a |
| 134 | | imul b |
| 135 | | mov cl,shift |
| 136 | | shrd eax,edx,cl |
| 137 | | mov result,eax |
| 138 | | } |
| 139 | | |
| 140 | | return result; |
| 141 | | } |
| 142 | | #endif |
| 143 | | |
| 144 | | |
| 145 | | /*------------------------------------------------- |
| 146 | | mulu_32x32_shift - perform an unsigned 32 bit x |
| 147 | | 32 bit multiply and shift the result by the |
| 148 | | given number of bits before truncating the |
| 149 | | result to 32 bits |
| 150 | | -------------------------------------------------*/ |
| 151 | | |
| 152 | | #ifndef PTR64 |
| 153 | | #define mulu_32x32_shift _mulu_32x32_shift |
| 154 | | INLINE UINT32 _mulu_32x32_shift(UINT32 a, UINT32 b, UINT8 shift) |
| 155 | | { |
| 156 | | INT32 result; |
| 157 | | |
| 158 | | __asm |
| 159 | | { |
| 160 | | mov eax,a |
| 161 | | mul b |
| 162 | | mov cl,shift |
| 163 | | shrd eax,edx,cl |
| 164 | | mov result,eax |
| 165 | | } |
| 166 | | |
| 167 | | return result; |
| 168 | | } |
| 169 | | #endif |
| 170 | | |
| 171 | | |
| 172 | | /*------------------------------------------------- |
| 173 | | div_64x32 - perform a signed 64 bit x 32 bit |
| 174 | | divide and return the 32 bit quotient |
| 175 | | -------------------------------------------------*/ |
| 176 | | |
| 177 | | #ifndef PTR64 |
| 178 | | #define div_64x32 _div_64x32 |
| 179 | | INLINE INT32 _div_64x32(INT64 a, INT32 b) |
| 180 | | { |
| 181 | | INT32 result; |
| 182 | | INT32 alow = a; |
| 183 | | INT32 ahigh = a >> 32; |
| 184 | | |
| 185 | | __asm |
| 186 | | { |
| 187 | | mov eax,alow |
| 188 | | mov edx,ahigh |
| 189 | | idiv b |
| 190 | | mov result,eax |
| 191 | | } |
| 192 | | |
| 193 | | return result; |
| 194 | | } |
| 195 | | #endif |
| 196 | | |
| 197 | | |
| 198 | | /*------------------------------------------------- |
| 199 | | divu_64x32 - perform an unsigned 64 bit x 32 bit |
| 200 | | divide and return the 32 bit quotient |
| 201 | | -------------------------------------------------*/ |
| 202 | | |
| 203 | | #ifndef PTR64 |
| 204 | | #define divu_64x32 _divu_64x32 |
| 205 | | INLINE UINT32 _divu_64x32(UINT64 a, UINT32 b) |
| 206 | | { |
| 207 | | UINT32 result; |
| 208 | | UINT32 alow = a; |
| 209 | | UINT32 ahigh = a >> 32; |
| 210 | | |
| 211 | | __asm |
| 212 | | { |
| 213 | | mov eax,alow |
| 214 | | mov edx,ahigh |
| 215 | | div b |
| 216 | | mov result,eax |
| 217 | | } |
| 218 | | |
| 219 | | return result; |
| 220 | | } |
| 221 | | #endif |
| 222 | | |
| 223 | | |
| 224 | | /*------------------------------------------------- |
| 225 | | div_64x32_rem - perform a signed 64 bit x 32 |
| 226 | | bit divide and return the 32 bit quotient and |
| 227 | | 32 bit remainder |
| 228 | | -------------------------------------------------*/ |
| 229 | | |
| 230 | | #ifndef PTR64 |
| 231 | | #define div_64x32_rem _div_64x32_rem |
| 232 | | INLINE INT32 _div_64x32_rem(INT64 a, INT32 b, INT32 *remainder) |
| 233 | | { |
| 234 | | INT32 result; |
| 235 | | INT32 alow = a; |
| 236 | | INT32 ahigh = a >> 32; |
| 237 | | INT32 rem; |
| 238 | | |
| 239 | | __asm |
| 240 | | { |
| 241 | | mov eax,alow |
| 242 | | mov edx,ahigh |
| 243 | | idiv b |
| 244 | | mov result,eax |
| 245 | | mov rem,edx |
| 246 | | } |
| 247 | | |
| 248 | | *remainder = rem; |
| 249 | | return result; |
| 250 | | } |
| 251 | | #endif |
| 252 | | |
| 253 | | |
| 254 | | /*------------------------------------------------- |
| 255 | | divu_64x32_rem - perform an unsigned 64 bit x |
| 256 | | 32 bit divide and return the 32 bit quotient |
| 257 | | and 32 bit remainder |
| 258 | | -------------------------------------------------*/ |
| 259 | | |
| 260 | | #ifndef PTR64 |
| 261 | | #define divu_64x32_rem _divu_64x32_rem |
| 262 | | INLINE UINT32 _divu_64x32_rem(UINT64 a, UINT32 b, UINT32 *remainder) |
| 263 | | { |
| 264 | | UINT32 result; |
| 265 | | UINT32 alow = a; |
| 266 | | UINT32 ahigh = a >> 32; |
| 267 | | UINT32 rem; |
| 268 | | |
| 269 | | __asm |
| 270 | | { |
| 271 | | mov eax,alow |
| 272 | | mov edx,ahigh |
| 273 | | div b |
| 274 | | mov result,eax |
| 275 | | mov rem,edx |
| 276 | | } |
| 277 | | |
| 278 | | *remainder = rem; |
| 279 | | return result; |
| 280 | | } |
| 281 | | #endif |
| 282 | | |
| 283 | | |
| 284 | | /*------------------------------------------------- |
| 285 | | div_32x32_shift - perform a signed divide of |
| 286 | | two 32 bit values, shifting the first before |
| 287 | | division, and returning the 32 bit quotient |
| 288 | | -------------------------------------------------*/ |
| 289 | | |
| 290 | | #ifndef PTR64 |
| 291 | | #define div_32x32_shift _div_32x32_shift |
| 292 | | INLINE INT32 _div_32x32_shift(INT32 a, INT32 b, UINT8 shift) |
| 293 | | { |
| 294 | | INT32 result; |
| 295 | | |
| 296 | | __asm |
| 297 | | { |
| 298 | | mov eax,a |
| 299 | | cdq |
| 300 | | mov cl,shift |
| 301 | | shld edx,eax,cl |
| 302 | | shl eax,cl |
| 303 | | idiv b |
| 304 | | mov result,eax |
| 305 | | } |
| 306 | | |
| 307 | | return result; |
| 308 | | } |
| 309 | | #endif |
| 310 | | |
| 311 | | |
| 312 | | /*------------------------------------------------- |
| 313 | | divu_32x32_shift - perform an unsigned divide of |
| 314 | | two 32 bit values, shifting the first before |
| 315 | | division, and returning the 32 bit quotient |
| 316 | | -------------------------------------------------*/ |
| 317 | | |
| 318 | | #ifndef PTR64 |
| 319 | | #define divu_32x32_shift _divu_32x32_shift |
| 320 | | INLINE UINT32 _divu_32x32_shift(UINT32 a, UINT32 b, UINT8 shift) |
| 321 | | { |
| 322 | | UINT32 result; |
| 323 | | |
| 324 | | __asm |
| 325 | | { |
| 326 | | mov eax,a |
| 327 | | xor edx,edx |
| 328 | | mov cl,shift |
| 329 | | shld edx,eax,cl |
| 330 | | shl eax,cl |
| 331 | | div b |
| 332 | | mov result,eax |
| 333 | | } |
| 334 | | |
| 335 | | return result; |
| 336 | | } |
| 337 | | #endif |
| 338 | | |
| 339 | | |
| 340 | | /*------------------------------------------------- |
| 341 | | mod_64x32 - perform a signed 64 bit x 32 bit |
| 342 | | divide and return the 32 bit remainder |
| 343 | | -------------------------------------------------*/ |
| 344 | | |
| 345 | | #ifndef PTR64 |
| 346 | | #define mod_64x32 _mod_64x32 |
| 347 | | INLINE INT32 _mod_64x32(INT64 a, INT32 b) |
| 348 | | { |
| 349 | | INT32 result; |
| 350 | | INT32 alow = a; |
| 351 | | INT32 ahigh = a >> 32; |
| 352 | | |
| 353 | | __asm |
| 354 | | { |
| 355 | | mov eax,alow |
| 356 | | mov edx,ahigh |
| 357 | | idiv b |
| 358 | | mov result,edx |
| 359 | | } |
| 360 | | |
| 361 | | return result; |
| 362 | | } |
| 363 | | #endif |
| 364 | | |
| 365 | | |
| 366 | | /*------------------------------------------------- |
| 367 | | modu_64x32 - perform an unsigned 64 bit x 32 bit |
| 368 | | divide and return the 32 bit remainder |
| 369 | | -------------------------------------------------*/ |
| 370 | | |
| 371 | | #ifndef PTR64 |
| 372 | | #define modu_64x32 _modu_64x32 |
| 373 | | INLINE UINT32 _modu_64x32(UINT64 a, UINT32 b) |
| 374 | | { |
| 375 | | UINT32 result; |
| 376 | | UINT32 alow = a; |
| 377 | | UINT32 ahigh = a >> 32; |
| 378 | | |
| 379 | | __asm |
| 380 | | { |
| 381 | | mov eax,alow |
| 382 | | mov edx,ahigh |
| 383 | | div b |
| 384 | | mov result,edx |
| 385 | | } |
| 386 | | |
| 387 | | return result; |
| 388 | | } |
| 389 | | #endif |
| 390 | | |
| 391 | | |
| 392 | | /*------------------------------------------------- |
| 393 | | recip_approx - compute an approximate floating |
| 394 | | point reciprocal |
| 395 | | -------------------------------------------------*/ |
| 396 | | |
| 397 | 27 | #ifdef PTR64 |
| 398 | | #define recip_approx _recip_approx |
| 399 | | INLINE float _recip_approx(float z) |
| 400 | | { |
| 401 | | __m128 mz = _mm_set_ss(z); |
| 402 | | __m128 mooz = _mm_rcp_ss(mz); |
| 403 | | float ooz; |
| 404 | | _mm_store_ss(&ooz, mooz); |
| 405 | | return ooz; |
| 406 | | } |
| 28 | INT64 win_compare_exchange64(INT64 volatile *ptr, INT64 compare, INT64 exchange); |
| 407 | 29 | #endif |
| 408 | 30 | |
| 409 | 31 | |
| 32 | #ifndef compare_exchange32 |
| 33 | #define compare_exchange32 win_compare_exchange32 |
| 34 | #endif /* compare_exchange32 */ |
| 410 | 35 | |
| 411 | | /*************************************************************************** |
| 412 | | INLINE BIT MANIPULATION FUNCTIONS |
| 413 | | ***************************************************************************/ |
| 414 | | |
| 415 | | /*------------------------------------------------- |
| 416 | | count_leading_zeros - return the number of |
| 417 | | leading zero bits in a 32-bit value |
| 418 | | -------------------------------------------------*/ |
| 419 | | |
| 420 | | #define count_leading_zeros _count_leading_zeros |
| 421 | | INLINE UINT8 _count_leading_zeros(UINT32 value) |
| 422 | | { |
| 423 | 36 | #ifdef PTR64 |
| 424 | | UINT32 index; |
| 425 | | return _BitScanReverse(&index, value) ? (index ^ 31) : 32; |
| 426 | | #else |
| 427 | | INT32 result; |
| 428 | | |
| 429 | | __asm |
| 430 | | { |
| 431 | | bsr eax,value |
| 432 | | jnz skip |
| 433 | | mov eax,63 |
| 434 | | skip: |
| 435 | | xor eax,31 |
| 436 | | mov result,eax |
| 437 | | } |
| 438 | | |
| 439 | | return result; |
| 37 | #ifndef compare_exchange64 |
| 38 | #define compare_exchange64 win_compare_exchange64 |
| 39 | #endif /* compare_exchange64 */ |
| 440 | 40 | #endif |
| 441 | | } |
| 442 | 41 | |
| 42 | #ifndef atomic_exchange32 |
| 43 | #define atomic_exchange32 win_atomic_exchange32 |
| 44 | #endif /* atomic_exchange32 */ |
| 443 | 45 | |
| 444 | | /*------------------------------------------------- |
| 445 | | count_leading_ones - return the number of |
| 446 | | leading one bits in a 32-bit value |
| 447 | | -------------------------------------------------*/ |
| 448 | 46 | |
| 449 | | #define count_leading_ones _count_leading_ones |
| 450 | | INLINE UINT8 _count_leading_ones(UINT32 value) |
| 451 | | { |
| 452 | | #ifdef PTR64 |
| 453 | | UINT32 index; |
| 454 | | return _BitScanReverse(&index, ~value) ? (index ^ 31) : 32; |
| 455 | | #else |
| 456 | | INT32 result; |
| 47 | #ifndef atomic_add32 |
| 48 | #define atomic_add32 win_atomic_add32 |
| 49 | #endif /* atomic_add32 */ |
| 457 | 50 | |
| 458 | | __asm |
| 459 | | { |
| 460 | | mov eax,value |
| 461 | | not eax |
| 462 | | bsr eax,eax |
| 463 | | jnz skip |
| 464 | | mov eax,63 |
| 465 | | skip: |
| 466 | | xor eax,31 |
| 467 | | mov result,eax |
| 468 | | } |
| 469 | 51 | |
| 470 | | return result; |
| 471 | | #endif |
| 472 | | } |
| 473 | | |
| 474 | | |
| 475 | | |
| 476 | | /*************************************************************************** |
| 477 | | INLINE SYNCHRONIZATION FUNCTIONS |
| 478 | | ***************************************************************************/ |
| 479 | | |
| 480 | | /*------------------------------------------------- |
| 481 | | compare_exchange32 - compare the 'compare' |
| 482 | | value against the memory at 'ptr'; if equal, |
| 483 | | swap in the 'exchange' value. Regardless, |
| 484 | | return the previous value at 'ptr'. |
| 485 | | -------------------------------------------------*/ |
| 486 | | |
| 487 | | #define compare_exchange32 _compare_exchange32 |
| 488 | | INLINE INT32 _compare_exchange32(INT32 volatile *ptr, INT32 compare, INT32 exchange) |
| 489 | | { |
| 490 | | return _InterlockedCompareExchange(ptr, exchange, compare); |
| 491 | | } |
| 492 | | |
| 493 | | |
| 494 | | /*------------------------------------------------- |
| 495 | | compare_exchange64 - compare the 'compare' |
| 496 | | value against the memory at 'ptr'; if equal, |
| 497 | | swap in the 'exchange' value. Regardless, |
| 498 | | return the previous value at 'ptr'. |
| 499 | | -------------------------------------------------*/ |
| 500 | | |
| 501 | | #ifdef PTR64 |
| 502 | | #define compare_exchange64 _compare_exchange64 |
| 503 | | INLINE INT64 _compare_exchange64(INT64 volatile *ptr, INT64 compare, INT64 exchange) |
| 504 | | { |
| 505 | | return _InterlockedCompareExchange64(ptr, exchange, compare); |
| 506 | | } |
| 507 | | #endif |
| 508 | | |
| 509 | | |
| 510 | | /*------------------------------------------------- |
| 511 | | atomic_exchange32 - atomically exchange the |
| 512 | | exchange value with the memory at 'ptr', |
| 513 | | returning the original value. |
| 514 | | -------------------------------------------------*/ |
| 515 | | |
| 516 | | #define atomic_exchange32 _atomic_exchange32 |
| 517 | | INLINE INT32 _atomic_exchange32(INT32 volatile *ptr, INT32 exchange) |
| 518 | | { |
| 519 | | return _InterlockedExchange(ptr, exchange); |
| 520 | | } |
| 521 | | |
| 522 | | |
| 523 | | /*------------------------------------------------- |
| 524 | | atomic_add32 - atomically add the delta value |
| 525 | | to the memory at 'ptr', returning the final |
| 526 | | result. |
| 527 | | -------------------------------------------------*/ |
| 528 | | |
| 529 | | #define atomic_add32 _atomic_add32 |
| 530 | | INLINE INT32 _atomic_add32(INT32 volatile *ptr, INT32 delta) |
| 531 | | { |
| 532 | | return _InterlockedExchangeAdd(ptr, delta) + delta; |
| 533 | | } |
| 534 | | |
| 535 | | |
| 536 | | /*------------------------------------------------- |
| 537 | | atomic_increment32 - atomically increment the |
| 538 | | 32-bit value in memory at 'ptr', returning the |
| 539 | | final result. |
| 540 | | -------------------------------------------------*/ |
| 541 | | |
| 542 | | #define atomic_increment32 _atomic_increment32 |
| 543 | | INLINE INT32 _atomic_increment32(INT32 volatile *ptr) |
| 544 | | { |
| 545 | | return _InterlockedIncrement(ptr); |
| 546 | | } |
| 547 | | |
| 548 | | |
| 549 | | /*------------------------------------------------- |
| 550 | | atomic_decrement32 - atomically decrement the |
| 551 | | 32-bit value in memory at 'ptr', returning the |
| 552 | | final result. |
| 553 | | -------------------------------------------------*/ |
| 554 | | |
| 555 | | #define atomic_decrement32 _atomic_decrement32 |
| 556 | | INLINE INT32 _atomic_decrement32(INT32 volatile *ptr) |
| 557 | | { |
| 558 | | return _InterlockedDecrement(ptr); |
| 559 | | } |
| 560 | | |
| 561 | | |
| 562 | 52 | #endif /* __OSINLINE__ */ |
src/emu/attotime.c
| r1324 | r1325 | |
| 179 | 179 | |
| 180 | 180 | attotime attotime_mul(attotime _time1, UINT32 factor) |
| 181 | 181 | { |
| 182 | | INT32 attolo, attohi, reslo, reshi; |
| 183 | | INT64 temp; |
| 182 | UINT32 attolo, attohi, reslo, reshi; |
| 183 | UINT64 temp; |
| 184 | 184 | |
| 185 | 185 | /* if one of the items is attotime_never, return attotime_never */ |
| 186 | 186 | if (_time1.seconds >= ATTOTIME_MAX_SECONDS) |
| r1324 | r1325 | |
| 191 | 191 | return attotime_zero; |
| 192 | 192 | |
| 193 | 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); |
| 194 | attohi = divu_64x32_rem(_time1.attoseconds, ATTOSECONDS_PER_SECOND_SQRT, &attolo); |
| 195 | 195 | |
| 196 | 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); |
| 197 | temp = mulu_32x32(attolo, factor); |
| 198 | temp = divu_64x32_rem(temp, ATTOSECONDS_PER_SECOND_SQRT, &reslo); |
| 199 | 199 | |
| 200 | 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); |
| 201 | temp += mulu_32x32(attohi, factor); |
| 202 | temp = divu_64x32_rem(temp, ATTOSECONDS_PER_SECOND_SQRT, &reshi); |
| 203 | 203 | |
| 204 | 204 | /* scale the seconds */ |
| 205 | | temp += mul_32x32(_time1.seconds, factor); |
| 205 | temp += mulu_32x32(_time1.seconds, factor); |
| 206 | 206 | if (temp >= ATTOTIME_MAX_SECONDS) |
| 207 | 207 | return attotime_never; |
| 208 | 208 | |
| r1324 | r1325 | |
| 218 | 218 | |
| 219 | 219 | attotime attotime_div(attotime _time1, UINT32 factor) |
| 220 | 220 | { |
| 221 | | INT32 attolo, attohi, reshi, reslo, remainder; |
| 221 | UINT32 attolo, attohi, reshi, reslo, remainder; |
| 222 | 222 | attotime result; |
| 223 | | INT64 temp; |
| 223 | UINT64 temp; |
| 224 | 224 | |
| 225 | 225 | /* if one of the items is attotime_never, return attotime_never */ |
| 226 | 226 | if (_time1.seconds >= ATTOTIME_MAX_SECONDS) |
| r1324 | r1325 | |
| 231 | 231 | return _time1; |
| 232 | 232 | |
| 233 | 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); |
| 234 | attohi = divu_64x32_rem(_time1.attoseconds, ATTOSECONDS_PER_SECOND_SQRT, &attolo); |
| 235 | 235 | |
| 236 | 236 | /* divide the seconds and get the remainder */ |
| 237 | | result.seconds = div_64x32_rem(_time1.seconds, factor, &remainder); |
| 237 | result.seconds = divu_64x32_rem(_time1.seconds, factor, &remainder); |
| 238 | 238 | |
| 239 | 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); |
| 240 | temp = (INT64)attohi + mulu_32x32(remainder, ATTOSECONDS_PER_SECOND_SQRT); |
| 241 | reshi = divu_64x32_rem(temp, factor, &remainder); |
| 242 | 242 | |
| 243 | 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); |
| 244 | temp = attolo + mulu_32x32(remainder, ATTOSECONDS_PER_SECOND_SQRT); |
| 245 | reslo = divu_64x32_rem(temp, factor, &remainder); |
| 246 | 246 | |
| 247 | 247 | /* round based on the remainder */ |
| 248 | | result.attoseconds = (attoseconds_t)reslo + mul_32x32(reshi, ATTOSECONDS_PER_SECOND_SQRT); |
| 248 | result.attoseconds = (attoseconds_t)reslo + mulu_32x32(reshi, ATTOSECONDS_PER_SECOND_SQRT); |
| 249 | 249 | if (remainder >= factor / 2) |
| 250 | 250 | if (++result.attoseconds >= ATTOSECONDS_PER_SECOND) |
| 251 | 251 | { |
| r1324 | r1325 | |
| 297 | 297 | /* case 2: we want 9 or fewer digits of precision */ |
| 298 | 298 | else if (precision <= 9) |
| 299 | 299 | { |
| 300 | | INT32 upper = _time.attoseconds / ATTOSECONDS_PER_SECOND_SQRT; |
| 300 | UINT32 upper = _time.attoseconds / ATTOSECONDS_PER_SECOND_SQRT; |
| 301 | 301 | sprintf(buffer, "%d.%0*d", _time.seconds, precision, upper); |
| 302 | 302 | } |
| 303 | 303 | |
| 304 | 304 | /* case 3: more than 9 digits of precision */ |
| 305 | 305 | else |
| 306 | 306 | { |
| 307 | | INT32 lower; |
| 308 | | INT32 upper = div_64x32_rem(_time.attoseconds, ATTOSECONDS_PER_SECOND_SQRT, &lower); |
| 307 | UINT32 lower; |
| 308 | UINT32 upper = divu_64x32_rem(_time.attoseconds, ATTOSECONDS_PER_SECOND_SQRT, &lower); |
| 309 | 309 | sprintf(buffer, "%d.%09d%0*d", _time.seconds, upper, precision - 9, lower); |
| 310 | 310 | } |
| 311 | 311 | return buffer; |