| 1 |
/* |
| 2 |
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2020 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-common-lib |
| 6 |
* Created on: 1 апр. 2020 г. |
| 7 |
* |
| 8 |
* lsp-common-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-common-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-common-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifndef LSP_PLUG_IN_COMMON_ARCH_X86_BITS_INT_LOG2_H_ |
| 23 |
#define LSP_PLUG_IN_COMMON_ARCH_X86_BITS_INT_LOG2_H_ |
| 24 |
|
| 25 |
#ifndef LSP_PLUG_IN_COMMON_BITS_IMPL |
| 26 |
#error "This file should not be included directly" |
| 27 |
#endif /* LSP_PLUG_IN_COMMON_BITS_IMPL */ |
| 28 |
|
| 29 |
namespace lsp |
| 30 |
{ |
| 31 |
inline int __lsp_forced_inline int_log2(uint8_t v) |
| 32 |
{ |
| 33 |
uint32_t res = v; |
| 34 |
|
| 35 |
ARCH_X86_ASM ( |
| 36 |
__ASM_EMIT("bsr %[res], %[res]") |
| 37 |
__ASM_EMIT("cmovz %[tmp], %[res]") |
| 38 |
: [res] "+r" (res) |
| 39 |
: [tmp] "r" (0) |
| 40 |
: "cc" |
| 41 |
); |
| 42 |
return res; |
| 43 |
} |
| 44 |
|
| 45 |
inline int __lsp_forced_inline int_log2(int8_t v) |
| 46 |
{ |
| 47 |
uint32_t res = uint8_t(v); |
| 48 |
|
| 49 |
ARCH_X86_ASM ( |
| 50 |
__ASM_EMIT("bsr %[res], %[res]") |
| 51 |
__ASM_EMIT("cmovz %[tmp], %[res]") |
| 52 |
: [res] "+r" (res) |
| 53 |
: [tmp] "r" (0) |
| 54 |
: "cc" |
| 55 |
); |
| 56 |
return res; |
| 57 |
} |
| 58 |
|
| 59 |
inline int __lsp_forced_inline int_log2(uint16_t v) |
| 60 |
{ |
| 61 |
uint32_t res = v; |
| 62 |
|
| 63 |
ARCH_X86_ASM ( |
| 64 |
__ASM_EMIT("bsr %[res], %[res]") |
| 65 |
__ASM_EMIT("cmovz %[tmp], %[res]") |
| 66 |
: [res] "+r" (res) |
| 67 |
: [tmp] "r" (0) |
| 68 |
: "cc" |
| 69 |
); |
| 70 |
return res; |
| 71 |
} |
| 72 |
|
| 73 |
inline int __lsp_forced_inline int_log2(int16_t v) |
| 74 |
{ |
| 75 |
uint32_t res = uint16_t(v); |
| 76 |
|
| 77 |
ARCH_X86_ASM ( |
| 78 |
__ASM_EMIT("bsr %[res], %[res]") |
| 79 |
__ASM_EMIT("cmovz %[tmp], %[res]") |
| 80 |
: [res] "+r" (res) |
| 81 |
: [tmp] "r" (0) |
| 82 |
: "cc" |
| 83 |
); |
| 84 |
return res; |
| 85 |
} |
| 86 |
|
| 87 |
inline int __lsp_forced_inline int_log2(uint32_t v) |
| 88 |
{ |
| 89 |
ARCH_X86_ASM ( |
| 90 |
__ASM_EMIT("bsr %[res], %[res]") |
| 91 |
__ASM_EMIT("cmovz %[tmp], %[res]") |
| 92 |
: [res] "+r" (v) |
| 93 |
: [tmp] "r" (0) |
| 94 |
: "cc" |
| 95 |
); |
| 96 |
return v; |
| 97 |
} |
| 98 |
|
| 99 |
inline int __lsp_forced_inline int_log2(int32_t v) |
| 100 |
{ |
| 101 |
ARCH_X86_ASM ( |
| 102 |
__ASM_EMIT("bsr %[res], %[res]") |
| 103 |
__ASM_EMIT("cmovz %[tmp], %[res]") |
| 104 |
: [res] "+r" (v) |
| 105 |
: [tmp] "r" (0) |
| 106 |
: "cc" |
| 107 |
); |
| 108 |
return v; |
| 109 |
} |
| 110 |
|
| 111 |
#ifdef ARCH_X86_64 |
| 112 |
inline int __lsp_forced_inline int_log2(uint64_t v) |
| 113 |
{ |
| 114 |
ARCH_X86_ASM ( |
| 115 |
__ASM_EMIT("bsr %[res], %[res]") |
| 116 |
__ASM_EMIT("cmovz %q[tmp], %[res]") |
| 117 |
: [res] "+r" (v) |
| 118 |
: [tmp] "r" (0) |
| 119 |
: "cc" |
| 120 |
); |
| 121 |
return int(v); |
| 122 |
} |
| 123 |
|
| 124 |
inline int __lsp_forced_inline int_log2(int64_t v) |
| 125 |
{ |
| 126 |
ARCH_X86_ASM ( |
| 127 |
__ASM_EMIT("bsr %[res], %[res]") |
| 128 |
__ASM_EMIT("cmovz %q[tmp], %[res]") |
| 129 |
: [res] "+r" (v) |
| 130 |
: [tmp] "r" (0) |
| 131 |
: "cc" |
| 132 |
); |
| 133 |
return int(v); |
| 134 |
} |
| 135 |
#else |
| 136 |
inline int __lsp_forced_inline int_log2(uint64_t v) |
| 137 |
{ |
| 138 |
ARCH_X86_ASM ( |
| 139 |
__ASM_EMIT("test %%edx, %%edx") |
| 140 |
__ASM_EMIT("jz 2f") |
| 141 |
__ASM_EMIT("mov %%edx, %%eax") |
| 142 |
__ASM_EMIT("xor %%edx, %%edx") |
| 143 |
__ASM_EMIT("bsr %%eax, %%eax") |
| 144 |
__ASM_EMIT("cmovz %%edx, %%eax") |
| 145 |
__ASM_EMIT("add $32, %%eax") |
| 146 |
__ASM_EMIT("jmp 4f") |
| 147 |
|
| 148 |
__ASM_EMIT("2:") |
| 149 |
__ASM_EMIT("xor %%edx, %%edx") |
| 150 |
__ASM_EMIT("bsr %%eax, %%eax") |
| 151 |
__ASM_EMIT("cmovz %%edx, %%eax") |
| 152 |
|
| 153 |
__ASM_EMIT("4:") |
| 154 |
|
| 155 |
: [v] "+A" (v) |
| 156 |
: |
| 157 |
: "cc" |
| 158 |
); |
| 159 |
return int(v); |
| 160 |
} |
| 161 |
|
| 162 |
inline int __lsp_forced_inline int_log2(int64_t v) |
| 163 |
{ |
| 164 |
ARCH_X86_ASM ( |
| 165 |
__ASM_EMIT("test %%edx, %%edx") |
| 166 |
__ASM_EMIT("jz 2f") |
| 167 |
__ASM_EMIT("mov %%edx, %%eax") |
| 168 |
__ASM_EMIT("xor %%edx, %%edx") |
| 169 |
__ASM_EMIT("bsr %%eax, %%eax") |
| 170 |
__ASM_EMIT("cmovz %%edx, %%eax") |
| 171 |
__ASM_EMIT("add $32, %%eax") |
| 172 |
__ASM_EMIT("jmp 4f") |
| 173 |
|
| 174 |
__ASM_EMIT("2:") |
| 175 |
__ASM_EMIT("xor %%edx, %%edx") |
| 176 |
__ASM_EMIT("bsr %%eax, %%eax") |
| 177 |
__ASM_EMIT("cmovz %%edx, %%eax") |
| 178 |
|
| 179 |
__ASM_EMIT("4:") |
| 180 |
|
| 181 |
: [v] "+A" (v) |
| 182 |
: |
| 183 |
: "cc" |
| 184 |
); |
| 185 |
return int(v); |
| 186 |
} |
| 187 |
#endif /* ARCH_X86_64 */ |
| 188 |
|
| 189 |
} /* namespace lsp */ |
| 190 |
|
| 191 |
#endif /* LSP_PLUG_IN_COMMON_ARCH_X86_BITS_INT_LOG2_H_ */ |