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