Debian Codegraph @

3rdparty/iaxclient/lib/portaudio/src/os/win/pa_x86_plain_converters.c

raw symbols SCIP
1 #include "pa_x86_plain_converters.h"
2
3 #include "pa_converters.h"
4 #include "pa_dither.h"
5
6 /*
7 plain intel assemby versions of standard pa converter functions.
8
9 the main reason these versions are faster than the equivalent C versions
10 is that float -> int casting is expensive in C on x86 because the rounding
11 mode needs to be changed for every cast. these versions only set
12 the rounding mode once outside the loop.
13
14 small additional speed gains are made by the way that clamping is
15 implemented.
16
17 TODO:
18 o- inline dither code
19 o- implement Dither only (no-clip) versions
20 o- implement int8 and uint8 versions
21 o- test thouroughly
22
23 o- the packed 24 bit functions could benefit from unrolling and avoiding
24 byte and word sized register access.
25 */
26
27 /* -------------------------------------------------------------------------- */
28
29 /*
30 #define PA_CLIP_( val, min, max )\
31 { val = ((val) < (min)) ? (min) : (((val) > (max)) ? (max) : (val)); }
32 */
33
34 /*
35 the following notes were used to determine whether a floating point
36 value should be saturated (ie >1 or <-1) by loading it into an integer
37 register. these should be rewritten so that they make sense.
38
39 an ieee floating point value
40
41 1.xxxxxxxxxxxxxxxxxxxx?
42
43
44 is less than or equal to 1 and greater than or equal to -1 either:
45
46 if the mantissa is 0 and the unbiased exponent is 0
47
48 OR
49
50 if the unbiased exponent < 0
51
52 this translates to:
53
54 if the mantissa is 0 and the biased exponent is 7F
55
56 or
57
58 if the biased exponent is less than 7F
59
60
61 therefore the value is greater than 1 or less than -1 if
62
63 the mantissa is not 0 and the biased exponent is 7F
64
65 or
66
67 if the biased exponent is greater than 7F
68
69
70 in other words, if we mask out the sign bit, the value is
71 greater than 1 or less than -1 if its integer representation is greater than:
72
73 0 01111111 0000 0000 0000 0000 0000 000
74
75 0011 1111 1000 0000 0000 0000 0000 0000 => 0x3F800000
76 */
77
78 /* -------------------------------------------------------------------------- */
79
80 static const short fpuControlWord_ = 0x033F; /*round to nearest, 64 bit precision, all exceptions masked*/
81 static const double int32Scaler_ = 0x7FFFFFFF;
82 static const double ditheredInt32Scaler_ = 0x7FFFFFFE;
83 static const double int24Scaler_ = 0x7FFFFF;
84 static const double ditheredInt24Scaler_ = 0x7FFFFE;
85 static const double int16Scaler_ = 0x7FFF;
86 static const double ditheredInt16Scaler_ = 0x7FFE;
87
88 #define PA_DITHER_BITS_ (15)
89 /* Multiply by PA_FLOAT_DITHER_SCALE_ to get a float between -2.0 and +1.99999 */
90 #define PA_FLOAT_DITHER_SCALE_ (1.0 / ((1<<PA_DITHER_BITS_)-1))
91 static const float const_float_dither_scale_ = PA_FLOAT_DITHER_SCALE_;
92 #define PA_DITHER_SHIFT_ ((32 - PA_DITHER_BITS_) + 1)
93
94 /* -------------------------------------------------------------------------- */
95
96 static void Float32_To_Int32(
97 void *destinationBuffer, signed int destinationStride,
98 void *sourceBuffer, signed int sourceStride,
99 unsigned int count, PaUtilTriangularDitherGenerator *ditherGenerator )
100 {
101 /*
102 float *src = (float*)sourceBuffer;
103 signed long *dest = (signed long*)destinationBuffer;
104 (void)ditherGenerator; // unused parameter
105
106 while( count-- )
107 {
108 // REVIEW
109 double scaled = *src * 0x7FFFFFFF;
110 *dest = (signed long) scaled;
111
112 src += sourceStride;
113 dest += destinationStride;
114 }
115 */
116
117 short savedFpuControlWord;
118
119 (void) ditherGenerator; /* unused parameter */
120
121
122 __asm{
123 // esi -> source ptr
124 // eax -> source byte stride
125 // edi -> destination ptr
126 // ebx -> destination byte stride
127 // ecx -> source end ptr
128 // edx -> temp
129
130 mov esi, sourceBuffer
131
132 mov edx, 4 // sizeof float32 and int32
133 mov eax, sourceStride
134 imul eax, edx
135
136 mov ecx, count
137 imul ecx, eax
138 add ecx, esi
139
140 mov edi, destinationBuffer
141
142 mov ebx, destinationStride
143 imul ebx, edx
144
145 fwait
146 fstcw savedFpuControlWord
147 fldcw fpuControlWord_
148
149 fld int32Scaler_ // stack: (int)0x7FFFFFFF
150
151 Float32_To_Int32_loop:
152
153 // load unscaled value into st(0)
154 fld dword ptr [esi] // stack: value, (int)0x7FFFFFFF
155 add esi, eax // increment source ptr
156 //lea esi, [esi+eax]
157 fmul st(0), st(1) // st(0) *= st(1), stack: value*0x7FFFFFFF, (int)0x7FFFFFFF
158 /*
159 note: we could store to a temporary qword here which would cause
160 wraparound distortion instead of int indefinite 0x10. that would
161 be more work, and given that not enabling clipping is only advisable
162 when you know that your signal isn't going to clip it isn't worth it.
163 */
164 fistp dword ptr [edi] // pop st(0) into dest, stack: (int)0x7FFFFFFF
165
166 add edi, ebx // increment destination ptr
167 //lea edi, [edi+ebx]
168
169 cmp esi, ecx // has src ptr reached end?
170 jne Float32_To_Int32_loop
171
172 ffree st(0)
173 fincstp
174
175 fwait
176 fnclex
177 fldcw savedFpuControlWord
178 }
179 }
180
181 /* -------------------------------------------------------------------------- */
182
183 static void Float32_To_Int32_Clip(
184 void *destinationBuffer, signed int destinationStride,
185 void *sourceBuffer, signed int sourceStride,
186 unsigned int count, PaUtilTriangularDitherGenerator *ditherGenerator )
187 {
188 /*
189 float *src = (float*)sourceBuffer;
190 signed long *dest = (signed long*)destinationBuffer;
191 (void) ditherGenerator; // unused parameter
192
193 while( count-- )
194 {
195 // REVIEW
196 double scaled = *src * 0x7FFFFFFF;
197 PA_CLIP_( scaled, -2147483648., 2147483647. );
198 *dest = (signed long) scaled;
199
200 src += sourceStride;
201 dest += destinationStride;
202 }
203 */
204
205 short savedFpuControlWord;
206
207 (void) ditherGenerator; /* unused parameter */
208
209 __asm{
210 // esi -> source ptr
211 // eax -> source byte stride
212 // edi -> destination ptr
213 // ebx -> destination byte stride
214 // ecx -> source end ptr
215 // edx -> temp
216
217 mov esi, sourceBuffer
218
219 mov edx, 4 // sizeof float32 and int32
220 mov eax, sourceStride
221 imul eax, edx
222
223 mov ecx, count
224 imul ecx, eax
225 add ecx, esi
226
227 mov edi, destinationBuffer
228
229 mov ebx, destinationStride
230 imul ebx, edx
231
232 fwait
233 fstcw savedFpuControlWord
234 fldcw fpuControlWord_
235
236 fld int32Scaler_ // stack: (int)0x7FFFFFFF
237
238 Float32_To_Int32_Clip_loop:
239
240 mov edx, dword ptr [esi] // load floating point value into integer register
241
242 and edx, 0x7FFFFFFF // mask off sign
243 cmp edx, 0x3F800000 // greater than 1.0 or less than -1.0
244
245 jg Float32_To_Int32_Clip_clamp
246
247 // load unscaled value into st(0)
248 fld dword ptr [esi] // stack: value, (int)0x7FFFFFFF
249 add esi, eax // increment source ptr
250 //lea esi, [esi+eax]
251 fmul st(0), st(1) // st(0) *= st(1), stack: value*0x7FFFFFFF, (int)0x7FFFFFFF
252 fistp dword ptr [edi] // pop st(0) into dest, stack: (int)0x7FFFFFFF
253 jmp Float32_To_Int32_Clip_stored
254
255 Float32_To_Int32_Clip_clamp:
256 mov edx, dword ptr [esi] // load floating point value into integer register
257 shr edx, 31 // move sign bit into bit 0
258 add esi, eax // increment source ptr
259 //lea esi, [esi+eax]
260 add edx, 0x7FFFFFFF // convert to maximum range integers
261 mov dword ptr [edi], edx
262
263 Float32_To_Int32_Clip_stored:
264
265 //add edi, ebx // increment destination ptr
266 lea edi, [edi+ebx]
267
268 cmp esi, ecx // has src ptr reached end?
269 jne Float32_To_Int32_Clip_loop
270
271 ffree st(0)
272 fincstp
273
274 fwait
275 fnclex
276 fldcw savedFpuControlWord
277 }
278 }
279
280 /* -------------------------------------------------------------------------- */
281
282 static void Float32_To_Int32_DitherClip(
283 void *destinationBuffer, signed int destinationStride,
284 void *sourceBuffer, signed int sourceStride,
285 unsigned int count, PaUtilTriangularDitherGenerator *ditherGenerator )
286 {
287 /*
288 float *src = (float*)sourceBuffer;
289 signed long *dest = (signed long*)destinationBuffer;
290
291 while( count-- )
292 {
293 // REVIEW
294 double dither = PaUtil_GenerateFloatTriangularDither( ditherGenerator );
295 // use smaller scaler to prevent overflow when we add the dither
296 double dithered = ((double)*src * (2147483646.0)) + dither;
297 PA_CLIP_( dithered, -2147483648., 2147483647. );
298 *dest = (signed long) dithered;
299
300
301 src += sourceStride;
302 dest += destinationStride;
303 }
304 */
305
306 short savedFpuControlWord;
307
308 // spill storage:
309 signed long sourceByteStride;
310 signed long highpassedDither;
311
312 // dither state:
313 unsigned long ditherPrevious = ditherGenerator->previous;
314 unsigned long ditherRandSeed1 = ditherGenerator->randSeed1;
315 unsigned long ditherRandSeed2 = ditherGenerator->randSeed2;
316
317 __asm{
318 // esi -> source ptr
319 // eax -> source byte stride
320 // edi -> destination ptr
321 // ebx -> destination byte stride
322 // ecx -> source end ptr
323 // edx -> temp
324
325 mov esi, sourceBuffer
326
327 mov edx, 4 // sizeof float32 and int32
328 mov eax, sourceStride
329 imul eax, edx
330
331 mov ecx, count
332 imul ecx, eax
333 add ecx, esi
334
335 mov edi, destinationBuffer
336
337 mov ebx, destinationStride
338 imul ebx, edx
339
340 fwait
341 fstcw savedFpuControlWord
342 fldcw fpuControlWord_
343
344 fld ditheredInt32Scaler_ // stack: int scaler
345
346 Float32_To_Int32_DitherClip_loop:
347
348 mov edx, dword ptr [esi] // load floating point value into integer register
349
350 and edx, 0x7FFFFFFF // mask off sign
351 cmp edx, 0x3F800000 // greater than 1.0 or less than -1.0
352
353 jg Float32_To_Int32_DitherClip_clamp
354
355 // load unscaled value into st(0)
356 fld dword ptr [esi] // stack: value, int scaler
357 add esi, eax // increment source ptr
358 //lea esi, [esi+eax]
359 fmul st(0), st(1) // st(0) *= st(1), stack: value*(int scaler), int scaler
360
361 /*
362 // call PaUtil_GenerateFloatTriangularDither with C calling convention
363 mov sourceByteStride, eax // save eax
364 mov sourceEnd, ecx // save ecx
365 push ditherGenerator // pass ditherGenerator parameter on stack
366 call PaUtil_GenerateFloatTriangularDither // stack: dither, value*(int scaler), int scaler
367 pop edx // clear parameter off stack
368 mov ecx, sourceEnd // restore ecx
369 mov eax, sourceByteStride // restore eax
370 */
371
372 // generate dither
373 mov sourceByteStride, eax // save eax
374 mov edx, 196314165
375 mov eax, ditherRandSeed1
376 mul edx // eax:edx = eax * 196314165
377 //add eax, 907633515
378 lea eax, [eax+907633515]
379 mov ditherRandSeed1, eax
380 mov edx, 196314165
381 mov eax, ditherRandSeed2
382 mul edx // eax:edx = eax * 196314165
383 //add eax, 907633515
384 lea eax, [eax+907633515]
385 mov edx, ditherRandSeed1
386 shr edx, PA_DITHER_SHIFT_
387 mov ditherRandSeed2, eax
388 shr eax, PA_DITHER_SHIFT_
389 //add eax, edx // eax -> current
390 lea eax, [eax+edx]
391 mov edx, ditherPrevious
392 neg edx
393 lea edx, [eax+edx] // highpass = current - previous
394 mov highpassedDither, edx
395 mov ditherPrevious, eax // previous = current
396 mov eax, sourceByteStride // restore eax
397 fild highpassedDither
398 fmul const_float_dither_scale_
399 // end generate dither, dither signal in st(0)
400
401 faddp st(1), st(0) // stack: dither + value*(int scaler), int scaler
402 fistp dword ptr [edi] // pop st(0) into dest, stack: int scaler
403 jmp Float32_To_Int32_DitherClip_stored
404
405 Float32_To_Int32_DitherClip_clamp:
406 mov edx, dword ptr [esi] // load floating point value into integer register
407 shr edx, 31 // move sign bit into bit 0
408 add esi, eax // increment source ptr
409 //lea esi, [esi+eax]
410 add edx, 0x7FFFFFFF // convert to maximum range integers
411 mov dword ptr [edi], edx
412
413 Float32_To_Int32_DitherClip_stored:
414
415 //add edi, ebx // increment destination ptr
416 lea edi, [edi+ebx]
417
418 cmp esi, ecx // has src ptr reached end?
419 jne Float32_To_Int32_DitherClip_loop
420
421 ffree st(0)
422 fincstp
423
424 fwait
425 fnclex
426 fldcw savedFpuControlWord
427 }
428
429 ditherGenerator->previous = ditherPrevious;
430 ditherGenerator->randSeed1 = ditherRandSeed1;
431 ditherGenerator->randSeed2 = ditherRandSeed2;
432 }
433
434 /* -------------------------------------------------------------------------- */
435
436 static void Float32_To_Int24(
437 void *destinationBuffer, signed int destinationStride,
438 void *sourceBuffer, signed int sourceStride,
439 unsigned int count, PaUtilTriangularDitherGenerator *ditherGenerator )
440 {
441 /*
442 float *src = (float*)sourceBuffer;
443 unsigned char *dest = (unsigned char*)destinationBuffer;
444 signed long temp;
445
446 (void) ditherGenerator; // unused parameter
447
448 while( count-- )
449 {
450 // convert to 32 bit and drop the low 8 bits
451 double scaled = *src * 0x7FFFFFFF;
452 temp = (signed long) scaled;
453
454 dest[0] = (unsigned char)(temp >> 8);
455 dest[1] = (unsigned char)(temp >> 16);
456 dest[2] = (unsigned char)(temp >> 24);
457
458 src += sourceStride;
459 dest += destinationStride * 3;
460 }
461 */
462
463 short savedFpuControlWord;
464
465 signed long tempInt32;
466
467 (void) ditherGenerator; /* unused parameter */
468
469 __asm{
470 // esi -> source ptr
471 // eax -> source byte stride
472 // edi -> destination ptr
473 // ebx -> destination byte stride
474 // ecx -> source end ptr
475 // edx -> temp
476
477 mov esi, sourceBuffer
478
479 mov edx, 4 // sizeof float32
480 mov eax, sourceStride
481 imul eax, edx
482
483 mov ecx, count
484 imul ecx, eax
485 add ecx, esi
486
487 mov edi, destinationBuffer
488
489 mov edx, 3 // sizeof int24
490 mov ebx, destinationStride
491 imul ebx, edx
492
493 fwait
494 fstcw savedFpuControlWord
495 fldcw fpuControlWord_
496
497 fld int24Scaler_ // stack: (int)0x7FFFFF
498
499 Float32_To_Int24_loop:
500
501 // load unscaled value into st(0)
502 fld dword ptr [esi] // stack: value, (int)0x7FFFFF
503 add esi, eax // increment source ptr
504 //lea esi, [esi+eax]
505 fmul st(0), st(1) // st(0) *= st(1), stack: value*0x7FFFFF, (int)0x7FFFFF
506 fistp tempInt32 // pop st(0) into tempInt32, stack: (int)0x7FFFFF
507 mov edx, tempInt32
508
509 mov byte ptr [edi], DL
510 shr edx, 8
511 //mov byte ptr [edi+1], DL
512 //mov byte ptr [edi+2], DH
513 mov word ptr [edi+1], DX
514
515 //add edi, ebx // increment destination ptr
516 lea edi, [edi+ebx]
517
518 cmp esi, ecx // has src ptr reached end?
519 jne Float32_To_Int24_loop
520
521 ffree st(0)
522 fincstp
523
524 fwait
525 fnclex
526 fldcw savedFpuControlWord
527 }
528 }
529
530 /* -------------------------------------------------------------------------- */
531
532 static void Float32_To_Int24_Clip(
533 void *destinationBuffer, signed int destinationStride,
534 void *sourceBuffer, signed int sourceStride,
535 unsigned int count, PaUtilTriangularDitherGenerator *ditherGenerator )
536 {
537 /*
538 float *src = (float*)sourceBuffer;
539 unsigned char *dest = (unsigned char*)destinationBuffer;
540 signed long temp;
541
542 (void) ditherGenerator; // unused parameter
543
544 while( count-- )
545 {
546 // convert to 32 bit and drop the low 8 bits
547 double scaled = *src * 0x7FFFFFFF;
548 PA_CLIP_( scaled, -2147483648., 2147483647. );
549 temp = (signed long) scaled;
550
551 dest[0] = (unsigned char)(temp >> 8);
552 dest[1] = (unsigned char)(temp >> 16);
553 dest[2] = (unsigned char)(temp >> 24);
554
555 src += sourceStride;
556 dest += destinationStride * 3;
557 }
558 */
559
560 short savedFpuControlWord;
561
562 signed long tempInt32;
563
564 (void) ditherGenerator; /* unused parameter */
565
566 __asm{
567 // esi -> source ptr
568 // eax -> source byte stride
569 // edi -> destination ptr
570 // ebx -> destination byte stride
571 // ecx -> source end ptr
572 // edx -> temp
573
574 mov esi, sourceBuffer
575
576 mov edx, 4 // sizeof float32
577 mov eax, sourceStride
578 imul eax, edx
579
580 mov ecx, count
581 imul ecx, eax
582 add ecx, esi
583
584 mov edi, destinationBuffer
585
586 mov edx, 3 // sizeof int24
587 mov ebx, destinationStride
588 imul ebx, edx
589
590 fwait
591 fstcw savedFpuControlWord
592 fldcw fpuControlWord_
593
594 fld int24Scaler_ // stack: (int)0x7FFFFF
595
596 Float32_To_Int24_Clip_loop:
597
598 mov edx, dword ptr [esi] // load floating point value into integer register
599
600 and edx, 0x7FFFFFFF // mask off sign
601 cmp edx, 0x3F800000 // greater than 1.0 or less than -1.0
602
603 jg Float32_To_Int24_Clip_clamp
604
605 // load unscaled value into st(0)
606 fld dword ptr [esi] // stack: value, (int)0x7FFFFF
607 add esi, eax // increment source ptr
608 //lea esi, [esi+eax]
609 fmul st(0), st(1) // st(0) *= st(1), stack: value*0x7FFFFF, (int)0x7FFFFF
610 fistp tempInt32 // pop st(0) into tempInt32, stack: (int)0x7FFFFF
611 mov edx, tempInt32
612 jmp Float32_To_Int24_Clip_store
613
614 Float32_To_Int24_Clip_clamp:
615 mov edx, dword ptr [esi] // load floating point value into integer register
616 shr edx, 31 // move sign bit into bit 0
617 add esi, eax // increment source ptr
618 //lea esi, [esi+eax]
619 add edx, 0x7FFFFF // convert to maximum range integers
620
621 Float32_To_Int24_Clip_store:
622
623 mov byte ptr [edi], DL
624 shr edx, 8
625 //mov byte ptr [edi+1], DL
626 //mov byte ptr [edi+2], DH
627 mov word ptr [edi+1], DX
628
629 //add edi, ebx // increment destination ptr
630 lea edi, [edi+ebx]
631
632 cmp esi, ecx // has src ptr reached end?
633 jne Float32_To_Int24_Clip_loop
634
635 ffree st(0)
636 fincstp
637
638 fwait
639 fnclex
640 fldcw savedFpuControlWord
641 }
642 }
643
644 /* -------------------------------------------------------------------------- */
645
646 static void Float32_To_Int24_DitherClip(
647 void *destinationBuffer, signed int destinationStride,
648 void *sourceBuffer, signed int sourceStride,
649 unsigned int count, PaUtilTriangularDitherGenerator *ditherGenerator )
650 {
651 /*
652 float *src = (float*)sourceBuffer;
653 unsigned char *dest = (unsigned char*)destinationBuffer;
654 signed long temp;
655
656 while( count-- )
657 {
658 // convert to 32 bit and drop the low 8 bits
659
660 // FIXME: the dither amplitude here appears to be too small by 8 bits
661 double dither = PaUtil_GenerateFloatTriangularDither( ditherGenerator );
662 // use smaller scaler to prevent overflow when we add the dither
663 double dithered = ((double)*src * (2147483646.0)) + dither;
664 PA_CLIP_( dithered, -2147483648., 2147483647. );
665
666 temp = (signed long) dithered;
667
668 dest[0] = (unsigned char)(temp >> 8);
669 dest[1] = (unsigned char)(temp >> 16);
670 dest[2] = (unsigned char)(temp >> 24);
671
672 src += sourceStride;
673 dest += destinationStride * 3;
674 }
675 */
676
677 short savedFpuControlWord;
678
679 // spill storage:
680 signed long sourceByteStride;
681 signed long highpassedDither;
682
683 // dither state:
684 unsigned long ditherPrevious = ditherGenerator->previous;
685 unsigned long ditherRandSeed1 = ditherGenerator->randSeed1;
686 unsigned long ditherRandSeed2 = ditherGenerator->randSeed2;
687
688 signed long tempInt32;
689
690 __asm{
691 // esi -> source ptr
692 // eax -> source byte stride
693 // edi -> destination ptr
694 // ebx -> destination byte stride
695 // ecx -> source end ptr
696 // edx -> temp
697
698 mov esi, sourceBuffer
699
700 mov edx, 4 // sizeof float32
701 mov eax, sourceStride
702 imul eax, edx
703
704 mov ecx, count
705 imul ecx, eax
706 add ecx, esi
707
708 mov edi, destinationBuffer
709
710 mov edx, 3 // sizeof int24
711 mov ebx, destinationStride
712 imul ebx, edx
713
714 fwait
715 fstcw savedFpuControlWord
716 fldcw fpuControlWord_
717
718 fld ditheredInt24Scaler_ // stack: int scaler
719
720 Float32_To_Int24_DitherClip_loop:
721
722 mov edx, dword ptr [esi] // load floating point value into integer register
723
724 and edx, 0x7FFFFFFF // mask off sign
725 cmp edx, 0x3F800000 // greater than 1.0 or less than -1.0
726
727 jg Float32_To_Int24_DitherClip_clamp
728
729 // load unscaled value into st(0)
730 fld dword ptr [esi] // stack: value, int scaler
731 add esi, eax // increment source ptr
732 //lea esi, [esi+eax]
733 fmul st(0), st(1) // st(0) *= st(1), stack: value*(int scaler), int scaler
734
735 /*
736 // call PaUtil_GenerateFloatTriangularDither with C calling convention
737 mov sourceByteStride, eax // save eax
738 mov sourceEnd, ecx // save ecx
739 push ditherGenerator // pass ditherGenerator parameter on stack
740 call PaUtil_GenerateFloatTriangularDither // stack: dither, value*(int scaler), int scaler
741 pop edx // clear parameter off stack
742 mov ecx, sourceEnd // restore ecx
743 mov eax, sourceByteStride // restore eax
744 */
745
746 // generate dither
747 mov sourceByteStride, eax // save eax
748 mov edx, 196314165
749 mov eax, ditherRandSeed1
750 mul edx // eax:edx = eax * 196314165
751 //add eax, 907633515
752 lea eax, [eax+907633515]
753 mov ditherRandSeed1, eax
754 mov edx, 196314165
755 mov eax, ditherRandSeed2
756 mul edx // eax:edx = eax * 196314165
757 //add eax, 907633515
758 lea eax, [eax+907633515]
759 mov edx, ditherRandSeed1
760 shr edx, PA_DITHER_SHIFT_
761 mov ditherRandSeed2, eax
762 shr eax, PA_DITHER_SHIFT_
763 //add eax, edx // eax -> current
764 lea eax, [eax+edx]
765 mov edx, ditherPrevious
766 neg edx
767 lea edx, [eax+edx] // highpass = current - previous
768 mov highpassedDither, edx
769 mov ditherPrevious, eax // previous = current
770 mov eax, sourceByteStride // restore eax
771 fild highpassedDither
772 fmul const_float_dither_scale_
773 // end generate dither, dither signal in st(0)
774
775 faddp st(1), st(0) // stack: dither * value*(int scaler), int scaler
776 fistp tempInt32 // pop st(0) into tempInt32, stack: int scaler
777 mov edx, tempInt32
778 jmp Float32_To_Int24_DitherClip_store
779
780 Float32_To_Int24_DitherClip_clamp:
781 mov edx, dword ptr [esi] // load floating point value into integer register
782 shr edx, 31 // move sign bit into bit 0
783 add esi, eax // increment source ptr
784 //lea esi, [esi+eax]
785 add edx, 0x7FFFFF // convert to maximum range integers
786
787 Float32_To_Int24_DitherClip_store:
788
789 mov byte ptr [edi], DL
790 shr edx, 8
791 //mov byte ptr [edi+1], DL
792 //mov byte ptr [edi+2], DH
793 mov word ptr [edi+1], DX
794
795 //add edi, ebx // increment destination ptr
796 lea edi, [edi+ebx]
797
798 cmp esi, ecx // has src ptr reached end?
799 jne Float32_To_Int24_DitherClip_loop
800
801 ffree st(0)
802 fincstp
803
804 fwait
805 fnclex
806 fldcw savedFpuControlWord
807 }
808
809 ditherGenerator->previous = ditherPrevious;
810 ditherGenerator->randSeed1 = ditherRandSeed1;
811 ditherGenerator->randSeed2 = ditherRandSeed2;
812 }
813
814 /* -------------------------------------------------------------------------- */
815
816 static void Float32_To_Int16(
817 void *destinationBuffer, signed int destinationStride,
818 void *sourceBuffer, signed int sourceStride,
819 unsigned int count, PaUtilTriangularDitherGenerator *ditherGenerator )
820 {
821 /*
822 float *src = (float*)sourceBuffer;
823 signed short *dest = (signed short*)destinationBuffer;
824 (void)ditherGenerator; // unused parameter
825
826 while( count-- )
827 {
828
829 short samp = (short) (*src * (32767.0f));
830 *dest = samp;
831
832 src += sourceStride;
833 dest += destinationStride;
834 }
835 */
836
837 short savedFpuControlWord;
838
839 (void) ditherGenerator; /* unused parameter */
840
841 __asm{
842 // esi -> source ptr
843 // eax -> source byte stride
844 // edi -> destination ptr
845 // ebx -> destination byte stride
846 // ecx -> source end ptr
847 // edx -> temp
848
849 mov esi, sourceBuffer
850
851 mov edx, 4 // sizeof float32
852 mov eax, sourceStride
853 imul eax, edx // source byte stride
854
855 mov ecx, count
856 imul ecx, eax
857 add ecx, esi // source end ptr = count * source byte stride + source ptr
858
859 mov edi, destinationBuffer
860
861 mov edx, 2 // sizeof int16
862 mov ebx, destinationStride
863 imul ebx, edx // destination byte stride
864
865 fwait
866 fstcw savedFpuControlWord
867 fldcw fpuControlWord_
868
869 fld int16Scaler_ // stack: (int)0x7FFF
870
871 Float32_To_Int16_loop:
872
873 // load unscaled value into st(0)
874 fld dword ptr [esi] // stack: value, (int)0x7FFF
875 add esi, eax // increment source ptr
876 //lea esi, [esi+eax]
877 fmul st(0), st(1) // st(0) *= st(1), stack: value*0x7FFF, (int)0x7FFF
878 fistp word ptr [edi] // store scaled int into dest, stack: (int)0x7FFF
879
880 add edi, ebx // increment destination ptr
881 //lea edi, [edi+ebx]
882
883 cmp esi, ecx // has src ptr reached end?
884 jne Float32_To_Int16_loop
885
886 ffree st(0)
887 fincstp
888
889 fwait
890 fnclex
891 fldcw savedFpuControlWord
892 }
893 }
894
895 /* -------------------------------------------------------------------------- */
896
897 static void Float32_To_Int16_Clip(
898 void *destinationBuffer, signed int destinationStride,
899 void *sourceBuffer, signed int sourceStride,
900 unsigned int count, PaUtilTriangularDitherGenerator *ditherGenerator )
901 {
902 /*
903 float *src = (float*)sourceBuffer;
904 signed short *dest = (signed short*)destinationBuffer;
905 (void)ditherGenerator; // unused parameter
906
907 while( count-- )
908 {
909 long samp = (signed long) (*src * (32767.0f));
910 PA_CLIP_( samp, -0x8000, 0x7FFF );
911 *dest = (signed short) samp;
912
913 src += sourceStride;
914 dest += destinationStride;
915 }
916 */
917
918 short savedFpuControlWord;
919
920 (void) ditherGenerator; /* unused parameter */
921
922 __asm{
923 // esi -> source ptr
924 // eax -> source byte stride
925 // edi -> destination ptr
926 // ebx -> destination byte stride
927 // ecx -> source end ptr
928 // edx -> temp
929
930 mov esi, sourceBuffer
931
932 mov edx, 4 // sizeof float32
933 mov eax, sourceStride
934 imul eax, edx // source byte stride
935
936 mov ecx, count
937 imul ecx, eax
938 add ecx, esi // source end ptr = count * source byte stride + source ptr
939
940 mov edi, destinationBuffer
941
942 mov edx, 2 // sizeof int16
943 mov ebx, destinationStride
944 imul ebx, edx // destination byte stride
945
946 fwait
947 fstcw savedFpuControlWord
948 fldcw fpuControlWord_
949
950 fld int16Scaler_ // stack: (int)0x7FFF
951
952 Float32_To_Int16_Clip_loop:
953
954 mov edx, dword ptr [esi] // load floating point value into integer register
955
956 and edx, 0x7FFFFFFF // mask off sign
957 cmp edx, 0x3F800000 // greater than 1.0 or less than -1.0
958
959 jg Float32_To_Int16_Clip_clamp
960
961 // load unscaled value into st(0)
962 fld dword ptr [esi] // stack: value, (int)0x7FFF
963 add esi, eax // increment source ptr
964 //lea esi, [esi+eax]
965 fmul st(0), st(1) // st(0) *= st(1), stack: value*0x7FFF, (int)0x7FFF
966 fistp word ptr [edi] // store scaled int into dest, stack: (int)0x7FFF
967 jmp Float32_To_Int16_Clip_stored
968
969 Float32_To_Int16_Clip_clamp:
970 mov edx, dword ptr [esi] // load floating point value into integer register
971 shr edx, 31 // move sign bit into bit 0
972 add esi, eax // increment source ptr
973 //lea esi, [esi+eax]
974 add dx, 0x7FFF // convert to maximum range integers
975 mov word ptr [edi], dx // store clamped into into dest
976
977 Float32_To_Int16_Clip_stored:
978
979 add edi, ebx // increment destination ptr
980 //lea edi, [edi+ebx]
981
982 cmp esi, ecx // has src ptr reached end?
983 jne Float32_To_Int16_Clip_loop
984
985 ffree st(0)
986 fincstp
987
988 fwait
989 fnclex
990 fldcw savedFpuControlWord
991 }
992 }
993
994 /* -------------------------------------------------------------------------- */
995
996 static void Float32_To_Int16_DitherClip(
997 void *destinationBuffer, signed int destinationStride,
998 void *sourceBuffer, signed int sourceStride,
999 unsigned int count, PaUtilTriangularDitherGenerator *ditherGenerator )
1000 {
1001 /*
1002 float *src = (float*)sourceBuffer;
1003 signed short *dest = (signed short*)destinationBuffer;
1004 (void)ditherGenerator; // unused parameter
1005
1006 while( count-- )
1007 {
1008
1009 float dither = PaUtil_GenerateFloatTriangularDither( ditherGenerator );
1010 // use smaller scaler to prevent overflow when we add the dither
1011 float dithered = (*src * (32766.0f)) + dither;
1012 signed long samp = (signed long) dithered;
1013 PA_CLIP_( samp, -0x8000, 0x7FFF );
1014 *dest = (signed short) samp;
1015
1016 src += sourceStride;
1017 dest += destinationStride;
1018 }
1019 */
1020
1021 short savedFpuControlWord;
1022
1023 // spill storage:
1024 signed long sourceByteStride;
1025 signed long highpassedDither;
1026
1027 // dither state:
1028 unsigned long ditherPrevious = ditherGenerator->previous;
1029 unsigned long ditherRandSeed1 = ditherGenerator->randSeed1;
1030 unsigned long ditherRandSeed2 = ditherGenerator->randSeed2;
1031
1032 __asm{
1033 // esi -> source ptr
1034 // eax -> source byte stride
1035 // edi -> destination ptr
1036 // ebx -> destination byte stride
1037 // ecx -> source end ptr
1038 // edx -> temp
1039
1040 mov esi, sourceBuffer
1041
1042 mov edx, 4 // sizeof float32
1043 mov eax, sourceStride
1044 imul eax, edx // source byte stride
1045
1046 mov ecx, count
1047 imul ecx, eax
1048 add ecx, esi // source end ptr = count * source byte stride + source ptr
1049
1050 mov edi, destinationBuffer
1051
1052 mov edx, 2 // sizeof int16
1053 mov ebx, destinationStride
1054 imul ebx, edx // destination byte stride
1055
1056 fwait
1057 fstcw savedFpuControlWord
1058 fldcw fpuControlWord_
1059
1060 fld ditheredInt16Scaler_ // stack: int scaler
1061
1062 Float32_To_Int16_DitherClip_loop:
1063
1064 mov edx, dword ptr [esi] // load floating point value into integer register
1065
1066 and edx, 0x7FFFFFFF // mask off sign
1067 cmp edx, 0x3F800000 // greater than 1.0 or less than -1.0
1068
1069 jg Float32_To_Int16_DitherClip_clamp
1070
1071 // load unscaled value into st(0)
1072 fld dword ptr [esi] // stack: value, int scaler
1073 add esi, eax // increment source ptr
1074 //lea esi, [esi+eax]
1075 fmul st(0), st(1) // st(0) *= st(1), stack: value*(int scaler), int scaler
1076
1077 /*
1078 // call PaUtil_GenerateFloatTriangularDither with C calling convention
1079 mov sourceByteStride, eax // save eax
1080 mov sourceEnd, ecx // save ecx
1081 push ditherGenerator // pass ditherGenerator parameter on stack
1082 call PaUtil_GenerateFloatTriangularDither // stack: dither, value*(int scaler), int scaler
1083 pop edx // clear parameter off stack
1084 mov ecx, sourceEnd // restore ecx
1085 mov eax, sourceByteStride // restore eax
1086 */
1087
1088 // generate dither
1089 mov sourceByteStride, eax // save eax
1090 mov edx, 196314165
1091 mov eax, ditherRandSeed1
1092 mul edx // eax:edx = eax * 196314165
1093 //add eax, 907633515
1094 lea eax, [eax+907633515]
1095 mov ditherRandSeed1, eax
1096 mov edx, 196314165
1097 mov eax, ditherRandSeed2
1098 mul edx // eax:edx = eax * 196314165
1099 //add eax, 907633515
1100 lea eax, [eax+907633515]
1101 mov edx, ditherRandSeed1
1102 shr edx, PA_DITHER_SHIFT_
1103 mov ditherRandSeed2, eax
1104 shr eax, PA_DITHER_SHIFT_
1105 //add eax, edx // eax -> current
1106 lea eax, [eax+edx] // current = randSeed1>>x + randSeed2>>x
1107 mov edx, ditherPrevious
1108 neg edx
1109 lea edx, [eax+edx] // highpass = current - previous
1110 mov highpassedDither, edx
1111 mov ditherPrevious, eax // previous = current
1112 mov eax, sourceByteStride // restore eax
1113 fild highpassedDither
1114 fmul const_float_dither_scale_
1115 // end generate dither, dither signal in st(0)
1116
1117 faddp st(1), st(0) // stack: dither * value*(int scaler), int scaler
1118 fistp word ptr [edi] // store scaled int into dest, stack: int scaler
1119 jmp Float32_To_Int16_DitherClip_stored
1120
1121 Float32_To_Int16_DitherClip_clamp:
1122 mov edx, dword ptr [esi] // load floating point value into integer register
1123 shr edx, 31 // move sign bit into bit 0
1124 add esi, eax // increment source ptr
1125 //lea esi, [esi+eax]
1126 add dx, 0x7FFF // convert to maximum range integers
1127 mov word ptr [edi], dx // store clamped into into dest
1128
1129 Float32_To_Int16_DitherClip_stored:
1130
1131 add edi, ebx // increment destination ptr
1132 //lea edi, [edi+ebx]
1133
1134 cmp esi, ecx // has src ptr reached end?
1135 jne Float32_To_Int16_DitherClip_loop
1136
1137 ffree st(0)
1138 fincstp
1139
1140 fwait
1141 fnclex
1142 fldcw savedFpuControlWord
1143 }
1144
1145 ditherGenerator->previous = ditherPrevious;
1146 ditherGenerator->randSeed1 = ditherRandSeed1;
1147 ditherGenerator->randSeed2 = ditherRandSeed2;
1148 }
1149
1150 /* -------------------------------------------------------------------------- */
1151
1152 void PaUtil_InitializeX86PlainConverters( void )
1153 {
1154 paConverters.Float32_To_Int32 = Float32_To_Int32;
1155 paConverters.Float32_To_Int32_Clip = Float32_To_Int32_Clip;
1156 paConverters.Float32_To_Int32_DitherClip = Float32_To_Int32_DitherClip;
1157
1158 paConverters.Float32_To_Int24 = Float32_To_Int24;
1159 paConverters.Float32_To_Int24_Clip = Float32_To_Int24_Clip;
1160 paConverters.Float32_To_Int24_DitherClip = Float32_To_Int24_DitherClip;
1161
1162 paConverters.Float32_To_Int16 = Float32_To_Int16;
1163 paConverters.Float32_To_Int16_Clip = Float32_To_Int16_Clip;
1164 paConverters.Float32_To_Int16_DitherClip = Float32_To_Int16_DitherClip;
1165 }
1166
1167 /* -------------------------------------------------------------------------- */