src/audio/SDL_mixer_MMX_VC.c
author Ryan C. Gordon
Wed, 23 Nov 2005 07:29:56 +0000
changeset 1190 173c063d4f55
parent 1149 ef03e8789d59
child 1330 450721ad5436
permissions -rw-r--r--
OS/2 port!

This was mostly, if not entirely, written by "Doodle" and "Caetano":
doodle@scenergy.dfmk.hu
daniel@caetano.eng.br

--ryan.
slouken@746
     1
#if defined(USE_ASM_MIXER_VC)
slouken@574
     2
// MMX assembler version of SDL_MixAudio for signed little endian 16 bit samples and signed 8 bit samples
slouken@574
     3
// Copyright 2002 Stephane Marchesin (stephane.marchesin@wanadoo.fr)
slouken@574
     4
// Converted to Intel ASM notation by Cth
slouken@574
     5
// This code is licensed under the LGPL (see COPYING for details)
slouken@574
     6
// 
slouken@574
     7
// Assumes buffer size in bytes is a multiple of 16
slouken@574
     8
// Assumes SDL_MIX_MAXVOLUME = 128
slouken@574
     9
slouken@574
    10
slouken@574
    11
////////////////////////////////////////////////
slouken@574
    12
// Mixing for 16 bit signed buffers
slouken@574
    13
////////////////////////////////////////////////
slouken@574
    14
icculus@1190
    15
#ifndef __WATCOMC__
slouken@574
    16
#include <windows.h>
slouken@574
    17
#include <stdio.h>
icculus@1190
    18
#endif
slouken@574
    19
slouken@574
    20
void SDL_MixAudio_MMX_S16_VC(char* dst,char* src,unsigned int nSize,int volume)
slouken@574
    21
{
slouken@574
    22
	__asm
slouken@574
    23
	{
slouken@574
    24
slouken@574
    25
		push	edi
slouken@574
    26
		push	esi
slouken@574
    27
		push	ebx
slouken@574
    28
		
slouken@574
    29
		mov		edi, dst		// edi = dst
slouken@574
    30
		mov		esi, src		// esi = src
slouken@574
    31
		mov		eax, volume		// eax = volume
slouken@574
    32
		mov		ebx, nSize		// ebx = size
slouken@574
    33
		shr		ebx, 4			// process 16 bytes per iteration = 8 samples
slouken@574
    34
		jz		endS16
slouken@574
    35
		
slouken@574
    36
		pxor	mm0, mm0
slouken@574
    37
		movd	mm0, eax		//%%eax,%%mm0
slouken@574
    38
		movq	mm1, mm0		//%%mm0,%%mm1
slouken@574
    39
		psllq	mm0, 16			//$16,%%mm0
slouken@574
    40
		por		mm0, mm1		//%%mm1,%%mm0
slouken@574
    41
		psllq	mm0, 16			//$16,%%mm0
slouken@574
    42
		por		mm0, mm1		//%%mm1,%%mm0
slouken@574
    43
		psllq	mm0, 16			//$16,%%mm0
slouken@574
    44
		por		mm0, mm1		//%%mm1,%%mm0			// mm0 = vol|vol|vol|vol
slouken@574
    45
icculus@1190
    46
		#ifndef __WATCOMC__
icculus@1049
    47
		align	16
icculus@1190
    48
		#endif
slouken@574
    49
mixloopS16:
slouken@574
    50
		movq	mm1, [esi]		//(%%esi),%%mm1\n" // mm1 = a|b|c|d
slouken@574
    51
		movq	mm2, mm1		//%%mm1,%%mm2\n" // mm2 = a|b|c|d
slouken@574
    52
		movq	mm4, [esi + 8]	//8(%%esi),%%mm4\n" // mm4 = e|f|g|h
slouken@574
    53
		// pre charger le buffer dst dans mm7
slouken@574
    54
		movq	mm7, [edi]		//(%%edi),%%mm7\n" // mm7 = dst[0]"
slouken@574
    55
		// multiplier par le volume
slouken@574
    56
		pmullw	mm1, mm0		//%%mm0,%%mm1\n" // mm1 = l(a*v)|l(b*v)|l(c*v)|l(d*v)
slouken@574
    57
		pmulhw	mm2, mm0		//%%mm0,%%mm2\n" // mm2 = h(a*v)|h(b*v)|h(c*v)|h(d*v)
slouken@574
    58
		movq	mm5, mm4		//%%mm4,%%mm5\n" // mm5 = e|f|g|h
slouken@574
    59
		pmullw	mm4, mm0		//%%mm0,%%mm4\n" // mm4 = l(e*v)|l(f*v)|l(g*v)|l(h*v)
slouken@574
    60
		pmulhw	mm5, mm0		//%%mm0,%%mm5\n" // mm5 = h(e*v)|h(f*v)|h(g*v)|h(h*v)
slouken@574
    61
		movq	mm3, mm1		//%%mm1,%%mm3\n" // mm3 = l(a*v)|l(b*v)|l(c*v)|l(d*v)
slouken@574
    62
		punpckhwd	mm1, mm2	//%%mm2,%%mm1\n" // mm1 = a*v|b*v
slouken@574
    63
		movq		mm6, mm4	//%%mm4,%%mm6\n" // mm6 = l(e*v)|l(f*v)|l(g*v)|l(h*v)
slouken@574
    64
		punpcklwd	mm3, mm2	//%%mm2,%%mm3\n" // mm3 = c*v|d*v
slouken@574
    65
		punpckhwd	mm4, mm5	//%%mm5,%%mm4\n" // mm4 = e*f|f*v
slouken@574
    66
		punpcklwd	mm6, mm5	//%%mm5,%%mm6\n" // mm6 = g*v|h*v
slouken@574
    67
		// pre charger le buffer dst dans mm5
slouken@574
    68
		movq	mm5, [edi + 8]	//8(%%edi),%%mm5\n" // mm5 = dst[1]
slouken@574
    69
		// diviser par 128
slouken@574
    70
		psrad	mm1, 7			//$7,%%mm1\n" // mm1 = a*v/128|b*v/128 , 128 = SDL_MIX_MAXVOLUME
slouken@574
    71
		add		esi, 16			//$16,%%esi\n"
slouken@574
    72
		psrad	mm3, 7			//$7,%%mm3\n" // mm3 = c*v/128|d*v/128
slouken@574
    73
		psrad	mm4, 7			//$7,%%mm4\n" // mm4 = e*v/128|f*v/128
slouken@574
    74
		// mm1 = le sample avec le volume modifie
slouken@574
    75
		packssdw	mm3, mm1	//%%mm1,%%mm3\n" // mm3 = s(a*v|b*v|c*v|d*v)
slouken@574
    76
		psrad	mm6, 7			//$7,%%mm6\n" // mm6= g*v/128|h*v/128
slouken@574
    77
		paddsw	mm3, mm7		//%%mm7,%%mm3\n" // mm3 = adjust_volume(src)+dst
slouken@574
    78
		// mm4 = le sample avec le volume modifie
slouken@574
    79
		packssdw	mm6, mm4	//%%mm4,%%mm6\n" // mm6 = s(e*v|f*v|g*v|h*v)
slouken@574
    80
		movq	[edi], mm3		//%%mm3,(%%edi)\n"
slouken@574
    81
		paddsw	mm6, mm5		//%%mm5,%%mm6\n" // mm6 = adjust_volume(src)+dst
slouken@574
    82
		movq	[edi + 8], mm6	//%%mm6,8(%%edi)\n"
slouken@574
    83
		add		edi, 16			//$16,%%edi\n"
slouken@574
    84
		dec		ebx				//%%ebx\n"
slouken@574
    85
		jnz mixloopS16
slouken@574
    86
slouken@574
    87
ends16:
slouken@574
    88
		emms
slouken@574
    89
		
slouken@574
    90
		pop		ebx
slouken@574
    91
		pop		esi
slouken@574
    92
		pop		edi
slouken@574
    93
	}
slouken@574
    94
slouken@574
    95
}
slouken@574
    96
slouken@574
    97
////////////////////////////////////////////////
slouken@574
    98
// Mixing for 8 bit signed buffers
slouken@574
    99
////////////////////////////////////////////////
slouken@574
   100
slouken@574
   101
void SDL_MixAudio_MMX_S8_VC(char* dst,char* src,unsigned int nSize,int volume)
slouken@574
   102
{
slouken@574
   103
	_asm
slouken@574
   104
	{
slouken@574
   105
slouken@574
   106
		push	edi
slouken@574
   107
		push	esi
slouken@574
   108
		push	ebx
slouken@574
   109
		
slouken@574
   110
		mov		edi, dst	//movl	%0,%%edi	// edi = dst
slouken@574
   111
		mov		esi, src	//%1,%%esi	// esi = src
slouken@574
   112
		mov		eax, volume	//%3,%%eax	// eax = volume
slouken@574
   113
icculus@1149
   114
		movd	mm0, eax	//%%eax,%%mm0
slouken@574
   115
		movq	mm1, mm0	//%%mm0,%%mm1
slouken@574
   116
		psllq	mm0, 16		//$16,%%mm0
slouken@574
   117
		por		mm0, mm1	//%%mm1,%%mm0
slouken@574
   118
		psllq	mm0, 16		//$16,%%mm0
slouken@574
   119
		por		mm0, mm1	//%%mm1,%%mm0
slouken@574
   120
		psllq	mm0, 16		//$16,%%mm0
slouken@574
   121
		por		mm0, mm1	//%%mm1,%%mm0
slouken@574
   122
		
slouken@574
   123
		mov		ebx, nSize	//%2,%%ebx	// ebx = size
slouken@574
   124
		shr		ebx, 3		//$3,%%ebx	// process 8 bytes per iteration = 8 samples
slouken@574
   125
		cmp		ebx, 0		//$0,%%ebx
slouken@574
   126
		je		endS8
slouken@574
   127
icculus@1190
   128
		#ifndef __WATCOMC__
icculus@1049
   129
		align 16
icculus@1190
   130
		#endif
slouken@574
   131
mixloopS8:
slouken@574
   132
		pxor	mm2, mm2	//%%mm2,%%mm2		// mm2 = 0
slouken@574
   133
		movq	mm1, [esi]	//(%%esi),%%mm1	// mm1 = a|b|c|d|e|f|g|h
slouken@574
   134
		movq	mm3, mm1	//%%mm1,%%mm3 	// mm3 = a|b|c|d|e|f|g|h
slouken@574
   135
		// on va faire le "sign extension" en faisant un cmp avec 0 qui retourne 1 si <0, 0 si >0
slouken@574
   136
		pcmpgtb		mm2, mm1	//%%mm1,%%mm2	// mm2 = 11111111|00000000|00000000....
slouken@574
   137
		punpckhbw	mm1, mm2	//%%mm2,%%mm1	// mm1 = 0|a|0|b|0|c|0|d
slouken@574
   138
		punpcklbw	mm3, mm2	//%%mm2,%%mm3	// mm3 = 0|e|0|f|0|g|0|h
slouken@574
   139
		movq	mm2, [edi]	//(%%edi),%%mm2	// mm2 = destination
slouken@574
   140
		pmullw	mm1, mm0	//%%mm0,%%mm1	// mm1 = v*a|v*b|v*c|v*d
slouken@574
   141
		add		esi, 8		//$8,%%esi
slouken@574
   142
		pmullw	mm3, mm0	//%%mm0,%%mm3	// mm3 = v*e|v*f|v*g|v*h
slouken@574
   143
		psraw	mm1, 7		//$7,%%mm1		// mm1 = v*a/128|v*b/128|v*c/128|v*d/128 
slouken@574
   144
		psraw	mm3, 7		//$7,%%mm3		// mm3 = v*e/128|v*f/128|v*g/128|v*h/128
slouken@574
   145
		packsswb mm3, mm1	//%%mm1,%%mm3	// mm1 = v*a/128|v*b/128|v*c/128|v*d/128|v*e/128|v*f/128|v*g/128|v*h/128
slouken@574
   146
		paddsb	mm3, mm2	//%%mm2,%%mm3	// add to destination buffer
slouken@574
   147
		movq	[edi], mm3	//%%mm3,(%%edi)	// store back to ram
slouken@574
   148
		add		edi, 8		//$8,%%edi
slouken@574
   149
		dec		ebx			//%%ebx
slouken@574
   150
		jnz		mixloopS8
slouken@574
   151
		
slouken@574
   152
endS8:
slouken@574
   153
		emms
slouken@574
   154
		
slouken@574
   155
		pop		ebx
slouken@574
   156
		pop		esi
slouken@574
   157
		pop		edi
slouken@574
   158
	}
slouken@574
   159
}
slouken@574
   160
slouken@746
   161
#endif /* USE_ASM_MIXER_VC */