Debian Codegraph @

modules/lsp-dsp-lib/include/private/dsp/arch/x86/sse/mxcsr.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-dsp-lib
6 * Created on: 31 мар. 2020 г.
7 *
8 * lsp-dsp-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-dsp-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-dsp-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #ifndef PRIVATE_DSP_ARCH_X86_SSE_MXCSR_H_
23 #define PRIVATE_DSP_ARCH_X86_SSE_MXCSR_H_
24
25 #ifndef PRIVATE_DSP_ARCH_X86_SSE_IMPL
26 #error "This header should not be included directly"
27 #endif /* PRIVATE_DSP_ARCH_X86_SSE_IMPL */
28
29 #define MXCSR_IE (1 << 0)
30 #define MXCSR_DE (1 << 1)
31 #define MXCSR_ZE (1 << 2)
32 #define MXCSR_OE (1 << 3)
33 #define MXCSR_UE (1 << 4)
34 #define MXCSR_PE (1 << 5)
35 #define MXCSR_DAZ (1 << 6) /* Denormals are zeros flag */
36 #define MXCSR_IM (1 << 7)
37 #define MXCSR_DM (1 << 8)
38 #define MXCSR_ZM (1 << 9)
39 #define MXCSR_OM (1 << 10)
40 #define MXCSR_UM (1 << 11)
41 #define MXCSR_PM (1 << 12)
42 #define MXCSR_ALL_MASK (MXCSR_IM | MXCSR_DM | MXCSR_ZM | MXCSR_OM | MXCSR_UM | MXCSR_PM)
43 #define MXCSR_RC_MASK (3 << 13)
44 #define MXCSR_RC_NEAREST (0 << 13)
45 #define MXCSR_RC_N_INF (1 << 13)
46 #define MXCSR_RC_P_INF (2 << 13)
47 #define MXCSR_RC_ZERO (3 << 13)
48 #define MXCSR_FZ (1 << 15) /* Flush to zero flag */
49
50 #define MXCSR_DEFAULT 0xffbf
51
52 namespace lsp
53 {
54 namespace sse
55 {
56 static uint32_t mxcsr_mask = MXCSR_DEFAULT;
57
58 inline void init_mxcsr_mask()
59 {
60 uint8_t fxsave[512] __lsp_aligned16;
61 uint8_t *ptr = fxsave;
62
63 ARCH_X86_ASM
64 (
65 // Clear FXSAVE structure
66 __ASM_EMIT("xor %%eax, %%eax")
67 __ASM_EMIT("mov $0x80, %%ecx")
68 __ASM_EMIT("rep stosl")
69
70 // Issue fxsave
71 __ASM_EMIT("fxsave (%[fxsave])")
72
73 // Get mask
74 __ASM_EMIT("mov 0x1c(%[fxsave]), %%eax")
75 __ASM_EMIT("test %%eax, %%eax")
76 __ASM_EMIT("jnz 1f")
77 __ASM_EMIT("mov %[mxcsr_dfl], %%eax") // Old processors issue zero MXCSR mask
78 __ASM_EMIT("1:")
79
80 // Store MXCSR mask
81 __ASM_EMIT("mov %%eax, %[mask]")
82
83 : "+D" (ptr), [fxsave] "+S" (ptr), [mask] "+m" (mxcsr_mask)
84 : [mxcsr_dfl] "i" (MXCSR_DEFAULT)
85 : "cc", "memory",
86 "%eax", "%ecx"
87 );
88 }
89
90 inline uint32_t read_mxcsr()
91 {
92 uint32_t result = 0;
93
94 ARCH_X86_ASM
95 (
96 __ASM_EMIT("stmxcsr %[result]")
97
98 : [result] "+m" (result)
99 :
100 : "memory"
101 );
102
103 return result;
104 }
105
106 inline void write_mxcsr(uint32_t value)
107 {
108 ARCH_X86_ASM
109 (
110 // Clear FXSAVE structure
111 __ASM_EMIT("and %[mask], %[value]")
112 __ASM_EMIT("ldmxcsr %[value]")
113
114 : [value] "+m" (value)
115 : [mask] "r" (mxcsr_mask)
116 : "cc", "memory"
117 );
118 }
119 }
120 }
121
122 #endif /* PRIVATE_DSP_ARCH_X86_SSE_MXCSR_H_ */