| 1 | /* Copyright (C) 2010-2016 The RetroArch team |
| 2 | * |
| 3 | * --------------------------------------------------------------------------------------- |
| 4 | * The following license statement only applies to this file (intrinsics.h). |
| 5 | * --------------------------------------------------------------------------------------- |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, |
| 8 | * to any person obtaining a copy of this software and associated documentation files (the "Software"), |
| 9 | * to deal in the Software without restriction, including without limitation the rights to |
| 10 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, |
| 11 | * and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 16 | * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 19 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #ifndef __LIBRETRO_SDK_COMPAT_INTRINSICS_H |
| 24 | #define __LIBRETRO_SDK_COMPAT_INTRINSICS_H |
| 25 | |
| 26 | #include <stdint.h> |
| 27 | #include <stddef.h> |
| 28 | #include <string.h> |
| 29 | |
| 30 | #include <retro_common_api.h> |
| 31 | #include <retro_inline.h> |
| 32 | |
| 33 | #ifdef _MSC_VER |
| 34 | #include <intrin.h> |
| 35 | #endif |
| 36 | |
| 37 | RETRO_BEGIN_DECLS |
| 38 | |
| 39 | /* Count Leading Zero, unsigned 16bit input value */ |
| 40 | static INLINE unsigned compat_clz_u16(uint16_t val) |
| 41 | { |
| 42 | #ifdef __GNUC__ |
| 43 | return __builtin_clz(val << 16 | 0x8000); |
| 44 | #else |
| 45 | unsigned ret = 0; |
| 46 | |
| 47 | while(!(val & 0x8000) && ret < 16) |
| 48 | { |
| 49 | val <<= 1; |
| 50 | ret++; |
| 51 | } |
| 52 | |
| 53 | return ret; |
| 54 | #endif |
| 55 | } |
| 56 | |
| 57 | /* Count Trailing Zero */ |
| 58 | #if defined(__GNUC__) |
| 59 | static INLINE int compat_ctz(unsigned x) |
| 60 | { |
| 61 | return __builtin_ctz(x); |
| 62 | } |
| 63 | #elif _MSC_VER >= 1400 |
| 64 | static INLINE int compat_ctz(unsigned x) |
| 65 | { |
| 66 | int r = 0; |
| 67 | _BitScanReverse(&r, x); |
| 68 | return r; |
| 69 | } |
| 70 | #else |
| 71 | /* Only checks at nibble granularity, |
| 72 | * because that's what we need. */ |
| 73 | static INLINE int compat_ctz(unsigned x) |
| 74 | { |
| 75 | if (x & 0x000f) |
| 76 | return 0; |
| 77 | if (x & 0x00f0) |
| 78 | return 4; |
| 79 | if (x & 0x0f00) |
| 80 | return 8; |
| 81 | if (x & 0xf000) |
| 82 | return 12; |
| 83 | return 16; |
| 84 | } |
| 85 | #endif |
| 86 | |
| 87 | RETRO_END_DECLS |
| 88 | |
| 89 | #endif |