| 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_ARM_BITS_INT_LOG2_H_ |
| 23 |
#define LSP_PLUG_IN_COMMON_ARCH_ARM_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 |
int res = v; |
| 34 |
ARCH_ARM_ASM |
| 35 |
( |
| 36 |
__ASM_EMIT("tst %[res], %[res]") |
| 37 |
__ASM_EMIT("clzne %[res], %[res]") |
| 38 |
__ASM_EMIT("rsbne %[res], %[res], $31") |
| 39 |
: [res] "+r" (res) |
| 40 |
: : "cc" |
| 41 |
); |
| 42 |
return res; |
| 43 |
} |
| 44 |
|
| 45 |
inline int __lsp_forced_inline int_log2(int8_t v) |
| 46 |
{ |
| 47 |
int res = uint8_t(v); |
| 48 |
ARCH_ARM_ASM |
| 49 |
( |
| 50 |
__ASM_EMIT("tst %[res], %[res]") |
| 51 |
__ASM_EMIT("clzne %[res], %[res]") |
| 52 |
__ASM_EMIT("rsbne %[res], %[res], $31") |
| 53 |
: [res] "+r" (res) |
| 54 |
: : "cc" |
| 55 |
); |
| 56 |
return res; |
| 57 |
} |
| 58 |
|
| 59 |
inline int __lsp_forced_inline int_log2(uint16_t v) |
| 60 |
{ |
| 61 |
int res = v; |
| 62 |
ARCH_ARM_ASM |
| 63 |
( |
| 64 |
__ASM_EMIT("tst %[res], %[res]") |
| 65 |
__ASM_EMIT("clzne %[res], %[res]") |
| 66 |
__ASM_EMIT("rsbne %[res], %[res], $31") |
| 67 |
: [res] "+r" (res) |
| 68 |
: : "cc" |
| 69 |
); |
| 70 |
return res; |
| 71 |
} |
| 72 |
|
| 73 |
inline int __lsp_forced_inline int_log2(int16_t v) |
| 74 |
{ |
| 75 |
int res = uint16_t(v); |
| 76 |
ARCH_ARM_ASM |
| 77 |
( |
| 78 |
__ASM_EMIT("tst %[res], %[res]") |
| 79 |
__ASM_EMIT("clzne %[res], %[res]") |
| 80 |
__ASM_EMIT("rsbne %[res], %[res], $31") |
| 81 |
: [res] "+r" (res) |
| 82 |
: : "cc" |
| 83 |
); |
| 84 |
return res; |
| 85 |
} |
| 86 |
|
| 87 |
inline int __lsp_forced_inline int_log2(uint32_t v) |
| 88 |
{ |
| 89 |
int res = int(v); |
| 90 |
ARCH_ARM_ASM |
| 91 |
( |
| 92 |
__ASM_EMIT("tst %[res], %[res]") |
| 93 |
__ASM_EMIT("clzne %[res], %[res]") |
| 94 |
__ASM_EMIT("rsbne %[res], %[res], $31") |
| 95 |
: [res] "+r" (res) |
| 96 |
: : "cc" |
| 97 |
); |
| 98 |
return res; |
| 99 |
} |
| 100 |
|
| 101 |
inline int __lsp_forced_inline int_log2(int32_t v) |
| 102 |
{ |
| 103 |
int res = int(v); |
| 104 |
ARCH_ARM_ASM |
| 105 |
( |
| 106 |
__ASM_EMIT("tst %[res], %[res]") |
| 107 |
__ASM_EMIT("clzne %[res], %[res]") |
| 108 |
__ASM_EMIT("rsbne %[res], %[res], $31") |
| 109 |
: [res] "+r" (res) |
| 110 |
: : "cc" |
| 111 |
); |
| 112 |
return res; |
| 113 |
} |
| 114 |
|
| 115 |
inline int __lsp_forced_inline int_log2(uint64_t v) |
| 116 |
{ |
| 117 |
int hi = int(v >> 32); |
| 118 |
int lo = int(v & 0xffffffff); |
| 119 |
ARCH_ARM_ASM |
| 120 |
( |
| 121 |
__ASM_EMIT("tst %[hi], %[hi]") |
| 122 |
__ASM_EMIT("beq 2f") |
| 123 |
__ASM_EMIT("clzne %[lo], %[hi]") |
| 124 |
__ASM_EMIT("rsbne %[lo], %[lo], $63") |
| 125 |
__ASM_EMIT("b 4f") |
| 126 |
|
| 127 |
__ASM_EMIT("2:") |
| 128 |
__ASM_EMIT("tst %[lo], %[lo]") |
| 129 |
__ASM_EMIT("clzne %[lo], %[lo]") |
| 130 |
__ASM_EMIT("rsbne %[lo], %[lo], $31") |
| 131 |
|
| 132 |
__ASM_EMIT("4:") |
| 133 |
: [lo] "+r" (lo) |
| 134 |
: [hi] "r" (hi) |
| 135 |
: "cc" |
| 136 |
); |
| 137 |
return lo; |
| 138 |
} |
| 139 |
|
| 140 |
inline int __lsp_forced_inline int_log2(int64_t v) |
| 141 |
{ |
| 142 |
int hi = int(uint64_t(v) >> 32); |
| 143 |
int lo = int(v & 0xffffffff); |
| 144 |
ARCH_ARM_ASM |
| 145 |
( |
| 146 |
__ASM_EMIT("tst %[hi], %[hi]") |
| 147 |
__ASM_EMIT("beq 2f") |
| 148 |
__ASM_EMIT("clzne %[lo], %[hi]") |
| 149 |
__ASM_EMIT("rsbne %[lo], %[lo], $63") |
| 150 |
__ASM_EMIT("b 4f") |
| 151 |
|
| 152 |
__ASM_EMIT("2:") |
| 153 |
__ASM_EMIT("tst %[lo], %[lo]") |
| 154 |
__ASM_EMIT("clzne %[lo], %[lo]") |
| 155 |
__ASM_EMIT("rsbne %[lo], %[lo], $31") |
| 156 |
|
| 157 |
__ASM_EMIT("4:") |
| 158 |
: [lo] "+r" (lo) |
| 159 |
: [hi] "r" (hi) |
| 160 |
: "cc" |
| 161 |
); |
| 162 |
return lo; |
| 163 |
} |
| 164 |
} /* namespace lsp */ |
| 165 |
|
| 166 |
#endif /* LSP_PLUG_IN_COMMON_ARCH_ARM_BITS_INT_LOG2_H_ */ |