Debian Codegraph @

examples/volscale.c

raw
1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4
5 #include <stdio.h>
6 #ifndef _MSC_VER
7 #include <sys/time.h>
8 #endif
9
10 #define ORC_ENABLE_UNSTABLE_API
11
12 #include <orc/orc.h>
13 #include <orc/orcmmx.h>
14 #include <orc/orcmmxinsn.h>
15 #include <orc/orcsse.h>
16
17 static OrcProgram *p = NULL;
18
19 #if ENABLE_TARGET_MMX
20 static void
21 mmx_rule_mulhslw (OrcCompiler *p, void *user, OrcInstruction *insn)
22 {
23 int tmp1 = X86_MM4;
24 int tmp2 = X86_MM5;
25 int src1 = ORC_SRC_ARG (p, insn, 0);
26 int src2 = ORC_SRC_ARG (p, insn, 1);
27 int dest = ORC_DEST_ARG (p, insn, 0);
28
29 if (dest != src1) {
30 orc_mmx_emit_movq (p, src1, dest);
31 }
32
33 orc_mmx_emit_pxor (p, tmp1, tmp1); /* .. | 0 | 0 | */
34 orc_mmx_emit_punpcklwd (p, tmp1, src2); /* .. | 0 | p0 | */
35 orc_mmx_emit_pcmpgtw (p, dest, tmp1); /* .. | 0 | s(vl) | */
36 orc_mmx_emit_pand (p, src2, tmp1); /* .. | 0 | (p0) | (vl >> 15) & p */
37 orc_mmx_emit_movq (p, src2, tmp2);
38 orc_mmx_emit_pmulhw (p, src1, src2); /* .. | 0 | vl*p0 | */
39 orc_mmx_emit_paddw (p, tmp1, src2); /* .. | 0 | vl*p0 | + sign correct */
40 orc_mmx_emit_psrld_imm (p, 16, dest); /* .. | 0 | vh | */
41 orc_mmx_emit_pmaddwd (p, tmp2, dest); /* .. | p0 * vh | */
42 orc_mmx_emit_paddd (p, src2, dest); /* .. | p0 * v0 | */
43 }
44
45 static void
46 mmx_register_rules (void)
47 {
48 OrcRuleSet *rule_set;
49
50 rule_set = orc_rule_set_new (orc_opcode_set_get("pulse"),
51 orc_target_get_by_name ("mmx"), ORC_TARGET_MMX_MMX);
52
53 orc_rule_register (rule_set, "mulhslw", mmx_rule_mulhslw, NULL);
54 }
55 #endif
56
57 #if ENABLE_TARGET_SSE
58 static void
59 sse_rule_mulhslw (OrcCompiler *p, void *user, OrcInstruction *insn)
60 {
61 int tmp1 = X86_XMM4;
62 int tmp2 = X86_XMM5;
63 int src1 = ORC_SRC_ARG (p, insn, 0);
64 int src2 = ORC_SRC_ARG (p, insn, 1);
65 int dest = ORC_DEST_ARG (p, insn, 0);
66
67 if (dest != src1) {
68 orc_sse_emit_movdqa (p, src1, dest);
69 }
70
71 orc_sse_emit_pxor (p, tmp1, tmp1); /* .. | 0 | 0 | */
72 orc_sse_emit_punpcklwd (p, tmp1, src2); /* .. | 0 | p0 | */
73 orc_sse_emit_pcmpgtw (p, dest, tmp1); /* .. | 0 | s(vl) | */
74 orc_sse_emit_pand (p, src2, tmp1); /* .. | 0 | (p0) | (vl >> 15) & p */
75 orc_sse_emit_movdqa (p, src2, tmp2);
76 orc_sse_emit_pmulhw (p, src1, src2); /* .. | 0 | vl*p0 | */
77 orc_sse_emit_paddw (p, tmp1, src2); /* .. | 0 | vl*p0 | + sign correct */
78 orc_sse_emit_psrld_imm (p, 16, dest); /* .. | 0 | vh | */
79 orc_sse_emit_pmaddwd (p, tmp2, dest); /* .. | p0 * vh | */
80 orc_sse_emit_paddd (p, src2, dest); /* .. | p0 * v0 | */
81 }
82
83 static void
84 sse_register_rules (void)
85 {
86 OrcRuleSet *rule_set;
87
88 rule_set = orc_rule_set_new (orc_opcode_set_get("pulse"),
89 orc_target_get_by_name ("sse"), ORC_TARGET_SSE_SSE2);
90
91 orc_rule_register (rule_set, "mulhslw", sse_rule_mulhslw, NULL);
92 }
93 #endif
94
95 /* calculate the high 32 bits of a 32x16 signed multiply */
96 static void
97 emulate_mulhslw (OrcOpcodeExecutor *ex, int offset, int n)
98 {
99 int i;
100 orc_union32 * ptr0;
101 const orc_union32 * ptr4;
102 const orc_int16 * ptr5;
103 orc_union32 var32;
104 orc_int16 var33;
105 orc_union32 var34;
106
107 ptr0 = (orc_union32 *)ex->dest_ptrs[0];
108 ptr4 = (orc_union32 *)ex->src_ptrs[0];
109 ptr5 = (orc_int16 *)ex->src_ptrs[1];
110
111 for (i = 0; i < n; i++) {
112 /* 0: loadb */
113 var32 = ptr4[i];
114 /* 1: loadb */
115 var33 = ptr5[i];
116 /* 2: mulsbw */
117 var34.i = (var32.i * var33)>>16;
118 /* 3: storew */
119 ptr0[i] = var34;
120 }
121 }
122
123 static OrcStaticOpcode opcodes[] = {
124 { "mulhslw", 0, { 4 }, { 4, 2 }, emulate_mulhslw },
125
126 { "" }
127 };
128
129 static void
130 register_instr (void)
131 {
132 orc_opcode_register_static (opcodes, "pulse");
133 #if ENABLE_TARGET_MMX
134 mmx_register_rules ();
135 #endif
136
137 #if ENABLE_TARGET_SSE
138 sse_register_rules ();
139 #endif
140 }
141
142 static void
143 do_volume_c (orc_int16 *dest, const orc_int32 *vols, const orc_int16 *samp, int len)
144 {
145 int i;
146
147 for (i = 0; i < len; i++) {
148 orc_int32 t, hi, lo;
149
150 hi = vols[i] >> 16;
151 lo = vols[i] & 0xffff;
152
153 t = (orc_int32)(samp[i]);
154 t = ((t * lo) >> 16) + (t * hi);
155 dest[i] = (orc_int16) ORC_CLAMP (t, -0x8000, 0x7FFF);
156 }
157 }
158
159
160 static void
161 do_volume_backup (OrcExecutor * ORC_RESTRICT ex)
162 {
163 orc_int16 *dest;
164 orc_int32 *vols;
165 const orc_int16 *samp;
166 int len;
167
168 dest = ex->arrays[ORC_VAR_D1];
169 vols = ex->arrays[ORC_VAR_S1];
170 samp = ex->arrays[ORC_VAR_S2];
171 len = ex->n;
172
173 do_volume_c (dest, vols, samp, len);
174 }
175
176 static void
177 make_volume_orc()
178 {
179 OrcCompileResult res;
180
181 /* int16 destination samples that get scaled by int32 volumes */
182 p = orc_program_new ();
183 orc_program_set_backup_function (p, do_volume_backup);
184 orc_program_add_destination (p, 2, "d1");
185 orc_program_add_source (p, 4, "s1");
186 orc_program_add_source (p, 2, "s2");
187
188 /* a temporary for the upscaled input samples */
189 orc_program_add_temporary (p, 4, "t1");
190
191 /* multiply with the volume, keeping only the high 32bits */
192 orc_program_append (p, "mulhslw", ORC_VAR_T1, ORC_VAR_S1, ORC_VAR_S2);
193 /* pack an saturate do 16 bits again */
194 orc_program_append_ds (p, "convssslw", ORC_VAR_D1, ORC_VAR_T1);
195
196 /* Compile the program */
197 res = orc_program_compile (p);
198 fprintf (stderr, "result: %d\n", res);
199
200 if (res == ORC_COMPILE_RESULT_OK)
201 fprintf (stderr, "%s\n", orc_program_get_asm_code (p));
202 }
203
204 static void
205 do_volume_orc (orc_int16 *dest, orc_int32 *volumes, orc_int16 *samp, int length)
206 {
207 OrcExecutor _ex;
208 OrcExecutor *ex = &_ex;
209
210 /* Set the values on the executor structure */
211 orc_executor_set_program (ex, p);
212 orc_executor_set_n (ex, length);
213 orc_executor_set_array (ex, ORC_VAR_D1, dest);
214 orc_executor_set_array (ex, ORC_VAR_S1, volumes);
215 orc_executor_set_array (ex, ORC_VAR_S2, samp);
216
217 /* Run the program. This calls the code that was generated above,
218 * or, if the compilation failed, will emulate the program. */
219 orc_executor_run (ex);
220 }
221
222 static orc_uint64
223 get_timestamp ()
224 {
225 #ifndef _MSC_VER
226 struct timeval now;
227
228 gettimeofday (&now, NULL);
229
230 return now.tv_sec * 1000000LL + now.tv_usec;
231 #else
232 return 0;
233 #endif
234 }
235
236 #define TIMES 100000
237 #define N 1024
238
239 orc_int16 dest[N];
240 orc_int16 samp[N];
241 orc_int32 vols[N];
242
243 int
244 main (int argc, char *argv[])
245 {
246 int i;
247 orc_uint64 start, stop;
248
249 /* orc_init() must be called before any other Orc function */
250 orc_init ();
251
252 orc_debug_set_level (ORC_DEBUG_LOG);
253 register_instr ();
254
255 make_volume_orc();
256 orc_debug_set_level (ORC_DEBUG_NONE);
257
258 /* Create some data in the source arrays */
259 for(i=0;i<N;i++){
260 dest[i] = 0;
261 samp[i] = i + 1;
262 vols[i] = 0x10000 + i;
263 }
264
265 start = get_timestamp ();
266 for (i = 0; i < TIMES; i++)
267 do_volume_c (dest, vols, samp, N);
268 stop = get_timestamp ();
269 printf ("elapsed C: %d ms\n", (int) (stop - start));
270
271
272 start = get_timestamp ();
273 for (i = 0; i < TIMES; i++)
274 do_volume_orc (dest, vols, samp, N);
275 stop = get_timestamp ();
276 printf ("elapsed ORC: %d ms\n", (int) (stop - start));
277
278 /* Print the results */
279 for(i=0;i<20;i++){
280 printf("%d: %d -> %d\n", i, samp[i], dest[i]);
281 }
282
283 return 0;
284 }