src/audio/SDL_mixer_MMX_VC.c
author Sam Lantinga
Mon, 20 Jan 2003 16:01:20 +0000
changeset 574 64fe373be3dc
child 746 72ef7ce609ef
permissions -rw-r--r--
Cth converted the MMX audio mixing routines to VC++ syntax
slouken@574
     1
// MMX assembler version of SDL_MixAudio for signed little endian 16 bit samples and signed 8 bit samples
slouken@574
     2
// Copyright 2002 Stephane Marchesin (stephane.marchesin@wanadoo.fr)
slouken@574
     3
// Converted to Intel ASM notation by Cth
slouken@574
     4
// This code is licensed under the LGPL (see COPYING for details)
slouken@574
     5
// 
slouken@574
     6
// Assumes buffer size in bytes is a multiple of 16
slouken@574
     7
// Assumes SDL_MIX_MAXVOLUME = 128
slouken@574
     8
slouken@574
     9
slouken@574
    10
////////////////////////////////////////////////
slouken@574
    11
// Mixing for 16 bit signed buffers
slouken@574
    12
////////////////////////////////////////////////
slouken@574
    13
slouken@574
    14
#if defined(USE_ASM_MIXER_VC)
slouken@574
    15
#include <windows.h>
slouken@574
    16
#include <stdio.h>
slouken@574
    17
slouken@574
    18
void SDL_MixAudio_MMX_S16_VC(char* dst,char* src,unsigned int nSize,int volume)
slouken@574
    19
{
slouken@574
    20
	__asm
slouken@574
    21
	{
slouken@574
    22
		align	16
slouken@574
    23
slouken@574
    24
		push	edi
slouken@574
    25
		push	esi
slouken@574
    26
		push	ebx
slouken@574
    27
		
slouken@574
    28
		mov		edi, dst		// edi = dst
slouken@574
    29
		mov		esi, src		// esi = src
slouken@574
    30
		mov		eax, volume		// eax = volume
slouken@574
    31
		mov		ebx, nSize		// ebx = size
slouken@574
    32
		shr		ebx, 4			// process 16 bytes per iteration = 8 samples
slouken@574
    33
		jz		endS16
slouken@574
    34
		
slouken@574
    35
		pxor	mm0, mm0
slouken@574
    36
		movd	mm0, eax		//%%eax,%%mm0
slouken@574
    37
		movq	mm1, mm0		//%%mm0,%%mm1
slouken@574
    38
		psllq	mm0, 16			//$16,%%mm0
slouken@574
    39
		por		mm0, mm1		//%%mm1,%%mm0
slouken@574
    40
		psllq	mm0, 16			//$16,%%mm0
slouken@574
    41
		por		mm0, mm1		//%%mm1,%%mm0
slouken@574
    42
		psllq	mm0, 16			//$16,%%mm0
slouken@574
    43
		por		mm0, mm1		//%%mm1,%%mm0			// mm0 = vol|vol|vol|vol
slouken@574
    44
slouken@574
    45
mixloopS16:
slouken@574
    46
		movq	mm1, [esi]		//(%%esi),%%mm1\n" // mm1 = a|b|c|d
slouken@574
    47
		movq	mm2, mm1		//%%mm1,%%mm2\n" // mm2 = a|b|c|d
slouken@574
    48
		movq	mm4, [esi + 8]	//8(%%esi),%%mm4\n" // mm4 = e|f|g|h
slouken@574
    49
		// pre charger le buffer dst dans mm7
slouken@574
    50
		movq	mm7, [edi]		//(%%edi),%%mm7\n" // mm7 = dst[0]"
slouken@574
    51
		// multiplier par le volume
slouken@574
    52
		pmullw	mm1, mm0		//%%mm0,%%mm1\n" // mm1 = l(a*v)|l(b*v)|l(c*v)|l(d*v)
slouken@574
    53
		pmulhw	mm2, mm0		//%%mm0,%%mm2\n" // mm2 = h(a*v)|h(b*v)|h(c*v)|h(d*v)
slouken@574
    54
		movq	mm5, mm4		//%%mm4,%%mm5\n" // mm5 = e|f|g|h
slouken@574
    55
		pmullw	mm4, mm0		//%%mm0,%%mm4\n" // mm4 = l(e*v)|l(f*v)|l(g*v)|l(h*v)
slouken@574
    56
		pmulhw	mm5, mm0		//%%mm0,%%mm5\n" // mm5 = h(e*v)|h(f*v)|h(g*v)|h(h*v)
slouken@574
    57
		movq	mm3, mm1		//%%mm1,%%mm3\n" // mm3 = l(a*v)|l(b*v)|l(c*v)|l(d*v)
slouken@574
    58
		punpckhwd	mm1, mm2	//%%mm2,%%mm1\n" // mm1 = a*v|b*v
slouken@574
    59
		movq		mm6, mm4	//%%mm4,%%mm6\n" // mm6 = l(e*v)|l(f*v)|l(g*v)|l(h*v)
slouken@574
    60
		punpcklwd	mm3, mm2	//%%mm2,%%mm3\n" // mm3 = c*v|d*v
slouken@574
    61
		punpckhwd	mm4, mm5	//%%mm5,%%mm4\n" // mm4 = e*f|f*v
slouken@574
    62
		punpcklwd	mm6, mm5	//%%mm5,%%mm6\n" // mm6 = g*v|h*v
slouken@574
    63
		// pre charger le buffer dst dans mm5
slouken@574
    64
		movq	mm5, [edi + 8]	//8(%%edi),%%mm5\n" // mm5 = dst[1]
slouken@574
    65
		// diviser par 128
slouken@574
    66
		psrad	mm1, 7			//$7,%%mm1\n" // mm1 = a*v/128|b*v/128 , 128 = SDL_MIX_MAXVOLUME
slouken@574
    67
		add		esi, 16			//$16,%%esi\n"
slouken@574
    68
		psrad	mm3, 7			//$7,%%mm3\n" // mm3 = c*v/128|d*v/128
slouken@574
    69
		psrad	mm4, 7			//$7,%%mm4\n" // mm4 = e*v/128|f*v/128
slouken@574
    70
		// mm1 = le sample avec le volume modifie
slouken@574
    71
		packssdw	mm3, mm1	//%%mm1,%%mm3\n" // mm3 = s(a*v|b*v|c*v|d*v)
slouken@574
    72
		psrad	mm6, 7			//$7,%%mm6\n" // mm6= g*v/128|h*v/128
slouken@574
    73
		paddsw	mm3, mm7		//%%mm7,%%mm3\n" // mm3 = adjust_volume(src)+dst
slouken@574
    74
		// mm4 = le sample avec le volume modifie
slouken@574
    75
		packssdw	mm6, mm4	//%%mm4,%%mm6\n" // mm6 = s(e*v|f*v|g*v|h*v)
slouken@574
    76
		movq	[edi], mm3		//%%mm3,(%%edi)\n"
slouken@574
    77
		paddsw	mm6, mm5		//%%mm5,%%mm6\n" // mm6 = adjust_volume(src)+dst
slouken@574
    78
		movq	[edi + 8], mm6	//%%mm6,8(%%edi)\n"
slouken@574
    79
		add		edi, 16			//$16,%%edi\n"
slouken@574
    80
		dec		ebx				//%%ebx\n"
slouken@574
    81
		jnz mixloopS16
slouken@574
    82
slouken@574
    83
ends16:
slouken@574
    84
		emms
slouken@574
    85
		
slouken@574
    86
		pop		ebx
slouken@574
    87
		pop		esi
slouken@574
    88
		pop		edi
slouken@574
    89
	}
slouken@574
    90
slouken@574
    91
}
slouken@574
    92
slouken@574
    93
////////////////////////////////////////////////
slouken@574
    94
// Mixing for 8 bit signed buffers
slouken@574
    95
////////////////////////////////////////////////
slouken@574
    96
slouken@574
    97
void SDL_MixAudio_MMX_S8_VC(char* dst,char* src,unsigned int nSize,int volume)
slouken@574
    98
{
slouken@574
    99
	_asm
slouken@574
   100
	{
slouken@574
   101
		align 16
slouken@574
   102
slouken@574
   103
		push	edi
slouken@574
   104
		push	esi
slouken@574
   105
		push	ebx
slouken@574
   106
		
slouken@574
   107
		mov		edi, dst	//movl	%0,%%edi	// edi = dst
slouken@574
   108
		mov		esi, src	//%1,%%esi	// esi = src
slouken@574
   109
		mov		eax, volume	//%3,%%eax	// eax = volume
slouken@574
   110
slouken@574
   111
		movd	mm0, ebx	//%%ebx,%%mm0
slouken@574
   112
		movq	mm1, mm0	//%%mm0,%%mm1
slouken@574
   113
		psllq	mm0, 16		//$16,%%mm0
slouken@574
   114
		por		mm0, mm1	//%%mm1,%%mm0
slouken@574
   115
		psllq	mm0, 16		//$16,%%mm0
slouken@574
   116
		por		mm0, mm1	//%%mm1,%%mm0
slouken@574
   117
		psllq	mm0, 16		//$16,%%mm0
slouken@574
   118
		por		mm0, mm1	//%%mm1,%%mm0
slouken@574
   119
		
slouken@574
   120
		mov		ebx, nSize	//%2,%%ebx	// ebx = size
slouken@574
   121
		shr		ebx, 3		//$3,%%ebx	// process 8 bytes per iteration = 8 samples
slouken@574
   122
		cmp		ebx, 0		//$0,%%ebx
slouken@574
   123
		je		endS8
slouken@574
   124
slouken@574
   125
mixloopS8:
slouken@574
   126
		pxor	mm2, mm2	//%%mm2,%%mm2		// mm2 = 0
slouken@574
   127
		movq	mm1, [esi]	//(%%esi),%%mm1	// mm1 = a|b|c|d|e|f|g|h
slouken@574
   128
		movq	mm3, mm1	//%%mm1,%%mm3 	// mm3 = a|b|c|d|e|f|g|h
slouken@574
   129
		// on va faire le "sign extension" en faisant un cmp avec 0 qui retourne 1 si <0, 0 si >0
slouken@574
   130
		pcmpgtb		mm2, mm1	//%%mm1,%%mm2	// mm2 = 11111111|00000000|00000000....
slouken@574
   131
		punpckhbw	mm1, mm2	//%%mm2,%%mm1	// mm1 = 0|a|0|b|0|c|0|d
slouken@574
   132
		punpcklbw	mm3, mm2	//%%mm2,%%mm3	// mm3 = 0|e|0|f|0|g|0|h
slouken@574
   133
		movq	mm2, [edi]	//(%%edi),%%mm2	// mm2 = destination
slouken@574
   134
		pmullw	mm1, mm0	//%%mm0,%%mm1	// mm1 = v*a|v*b|v*c|v*d
slouken@574
   135
		add		esi, 8		//$8,%%esi
slouken@574
   136
		pmullw	mm3, mm0	//%%mm0,%%mm3	// mm3 = v*e|v*f|v*g|v*h
slouken@574
   137
		psraw	mm1, 7		//$7,%%mm1		// mm1 = v*a/128|v*b/128|v*c/128|v*d/128 
slouken@574
   138
		psraw	mm3, 7		//$7,%%mm3		// mm3 = v*e/128|v*f/128|v*g/128|v*h/128
slouken@574
   139
		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
   140
		paddsb	mm3, mm2	//%%mm2,%%mm3	// add to destination buffer
slouken@574
   141
		movq	[edi], mm3	//%%mm3,(%%edi)	// store back to ram
slouken@574
   142
		add		edi, 8		//$8,%%edi
slouken@574
   143
		dec		ebx			//%%ebx
slouken@574
   144
		jnz		mixloopS8
slouken@574
   145
		
slouken@574
   146
endS8:
slouken@574
   147
		emms
slouken@574
   148
		
slouken@574
   149
		pop		ebx
slouken@574
   150
		pop		esi
slouken@574
   151
		pop		edi
slouken@574
   152
	}
slouken@574
   153
}
slouken@574
   154
slouken@574
   155
int _SDL_IsMMX_VC()
slouken@574
   156
{
slouken@574
   157
	// This	bit	flag can get set on	calling	cpuid
slouken@574
   158
	// with	register eax set to	1
slouken@574
   159
	const int _MMX_FEATURE_BIT = 0x00800000;
slouken@574
   160
	DWORD dwFeature	= 0;
slouken@574
   161
	__try {
slouken@574
   162
			_asm {
slouken@574
   163
				mov	eax,1
slouken@574
   164
				cpuid
slouken@574
   165
				mov	dwFeature,edx
slouken@574
   166
			}
slouken@574
   167
	} __except ( EXCEPTION_EXECUTE_HANDLER)	{
slouken@574
   168
			return 0;
slouken@574
   169
	}
slouken@574
   170
	if (dwFeature &	_MMX_FEATURE_BIT) {
slouken@574
   171
		__try {
slouken@574
   172
			__asm {
slouken@574
   173
				pxor mm0, mm0
slouken@574
   174
				emms
slouken@574
   175
			}
slouken@574
   176
		} __except (EXCEPTION_EXECUTE_HANDLER) {
slouken@574
   177
			return(0);
slouken@574
   178
		}
slouken@574
   179
		return 1;
slouken@574
   180
	}
slouken@574
   181
slouken@574
   182
	return 0;
slouken@574
   183
}
slouken@574
   184
slouken@574
   185
static int _notTwice = 2;
slouken@574
   186
slouken@574
   187
int SDL_IsMMX_VC()
slouken@574
   188
{
slouken@574
   189
	if (_notTwice > 1)
slouken@574
   190
	{
slouken@574
   191
		_notTwice = _SDL_IsMMX_VC();
slouken@574
   192
/*
slouken@574
   193
#ifdef _DEBUG
slouken@574
   194
		if (_notTwice)
slouken@574
   195
			MessageBox( NULL, "Using MMX!!!", "Error", MB_OK | MB_ICONINFORMATION );
slouken@574
   196
		else
slouken@574
   197
			MessageBox( NULL, "Not sing MMX!!!", "Error", MB_OK | MB_ICONINFORMATION );
slouken@574
   198
#endif
slouken@574
   199
*/
slouken@574
   200
	}
slouken@574
   201
	return _notTwice;
slouken@574
   202
}
slouken@574
   203
slouken@574
   204
#endif
slouken@574
   205