Debian Codegraph @

modules/lsp-common-lib/include/lsp-plug.in/common/arch/aarch64/bits/int_log2.h

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