| 1 |
/** |
| 2 |
* Furnace Tracker - multi-system chiptune tracker |
| 3 |
* Copyright (C) 2021-2025 tildearrow and contributors |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
*/ |
| 19 |
|
| 20 |
#if defined( _MSC_VER ) |
| 21 |
|
| 22 |
#include <intrin.h> |
| 23 |
|
| 24 |
static inline int bsr(unsigned short v) { |
| 25 |
unsigned long idx; |
| 26 |
if (_BitScanReverse(&idx,(unsigned long)v)) { |
| 27 |
return idx+1; |
| 28 |
} |
| 29 |
else { |
| 30 |
return -1; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
static inline int bsr32(unsigned int v) { |
| 35 |
unsigned long idx; |
| 36 |
if (_BitScanReverse(&idx,(unsigned long)v)) { |
| 37 |
return idx+1; |
| 38 |
} else { |
| 39 |
return -1; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
#elif defined( __GNUC__ ) |
| 44 |
|
| 45 |
static inline int bsr(unsigned short v) { |
| 46 |
if (v) { |
| 47 |
return 32-__builtin_clz(v); |
| 48 |
} else { |
| 49 |
return -1; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
static inline int bsr32(unsigned int v) { |
| 54 |
if (v) { |
| 55 |
return 32-__builtin_clz(v); |
| 56 |
} else { |
| 57 |
return -1; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
#else |
| 62 |
|
| 63 |
static inline int bsr(unsigned short v) { |
| 64 |
if (v==0) return -1; |
| 65 |
if (v&0x8000) return 16; |
| 66 |
int o=16; |
| 67 |
if (!(v&0xff00)) { |
| 68 |
o-=8; |
| 69 |
v<<=8; |
| 70 |
if (v&0x8000) return o; |
| 71 |
} |
| 72 |
if (!(v&0xf000)) { |
| 73 |
o-=4; |
| 74 |
v<<=4; |
| 75 |
if (v&0x8000) return o; |
| 76 |
} |
| 77 |
if (!(v&0xc000)) { |
| 78 |
o-=2; |
| 79 |
v<<=2; |
| 80 |
if (v&0x8000) return o; |
| 81 |
} |
| 82 |
return (v&0x8000) ? o : o-1; |
| 83 |
} |
| 84 |
|
| 85 |
static inline int bsr32(unsigned int v) { |
| 86 |
if (v==0) return -1; |
| 87 |
if (v&0x80000000) return 32; |
| 88 |
int o=32; |
| 89 |
if (!(v&0xffff0000)) { |
| 90 |
o-=16; |
| 91 |
v<<=16; |
| 92 |
if (v&0x80000000) return o; |
| 93 |
} |
| 94 |
if (!(v&0xff000000)) { |
| 95 |
o-=8; |
| 96 |
v<<=8; |
| 97 |
if (v&0x80000000) return o; |
| 98 |
} |
| 99 |
if (!(v&0xf0000000)) { |
| 100 |
o-=4; |
| 101 |
v<<=4; |
| 102 |
if (v&0x80000000) return o; |
| 103 |
} |
| 104 |
if (!(v&0xc0000000)) { |
| 105 |
o-=2; |
| 106 |
v<<=2; |
| 107 |
if (v&0x80000000) return o; |
| 108 |
} |
| 109 |
return (v&0x80000000) ? o : o-1; |
| 110 |
} |
| 111 |
|
| 112 |
#endif |
| 113 |
|