Skip to content

Instantly share code, notes, and snippets.

@jart
Created July 21, 2012 18:43
Show Gist options
  • Save jart/3156709 to your computer and use it in GitHub Desktop.
Save jart/3156709 to your computer and use it in GitHub Desktop.
Assembly Audio Mixing
/**
* Represents 20ms of 8khz signed linear 16-bit audio
*
* This is how much audio gets put in each RTP packet.
*/
typedef int16_t frame_t[160];
/**
* Fast mix two audio frames together on x86 and x86_64
*
* I take advantage of the sse2 instruction PADDSW that uses a 128-bit
* register to perform saturated addition on eight 16-bit numbers at the same
* time. I'm about two orders of a magnitude faster than c code.
*/
static void mix160(frame_t to, const frame_t from)
{
const int blocks = 160 / 8;
asm volatile (
"moar: movdqu (%[to]), %%xmm0;"
" movdqu (%[from]), %%xmm1;"
" paddsw %%xmm1, %%xmm0;"
" movdqu %%xmm0, (%[to]);"
" add $16, %[to];"
" add $16, %[from];"
" dec %[blocks];"
" cmp $0, %[blocks];"
" jg moar;"
: /* no outputs */
: [to] "r"(to),
[from] "r"(from),
[blocks] "r"(blocks)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment