Diary Of An x264 Developer

10/25/2009 (12:06 pm)

x264′s assembly abstraction layer: now free to use

Filed under: assembly,licensing,speed,x264 ::

About two years ago we decided to merge our two trees of 32-bit and 64-bit assembly; it had become a maintainability nightmare with many functions in one that were not in the other and so forth.  Loren Merritt did this merging by adding an abstraction layer that automatically handles differences in calling convention between platforms (and was extensible to future ones as well).  Thus began the story of common/x86/x86inc.asm.

Over the years, x86inc has grown tremendously as it gained a great deal of functionality.  It now supports x86_32, x86_64, and win64 (thanks to Anton Mitrofanov), which covers the three (by far) most popular x86 C calling conventions.  It also has macros to abstract between MMX and SSE functions, along with automatic handling of register permutations and other such useful features.

All of this serves to make x264ASM, as we call it, by far the best option for writing platform-independent x86 assembly.  It keeps the full optimization capabilities and powerful preprocessor of native assembly while having the platform-independence and convenience of instrinsics and inline assembly.

We’ve received many requests by non-GPL projects (and even commercial proprietary developers) to be able to use this abstraction layer.  And now, it’s available under a permissive BSD-like license (specifically the ISC) for anyone to use, on request by a certain Adobe engineer.

Before you jump into the x264 code to see how it’s used, however, let’s go over some of the basics.  Note of course this explanation is no substitute for reading x86inc.asm itself.

The core concept of x264ASM lies in the calling convention abstraction, which works as follows:

1.  The registers are numbered r0-r6.  If you want to use more (of course) you must  special-case it since it wouldn’t be compatible with 32-bit.

2.  cglobal functionname, X,Y,Z

X: number of values that you want automatically loaded from the stack into general-purpose registers.

Y: number of general-purpose registers that you need total, including X.

Z: number of XMM registers you need.

So, for example:

cglobal foo, 3,5,8

This will put my function arguments in r0, r1, and r2.  I’ll also have r3 and r4 available, and I can use xmm0-xmm7.

3. SSE/MMX abstraction

m0-m7 are MMX if you use INIT_MMX and SSE if you use INIT_XMM.  mmsize is 8 in the first case and 16 in the second.  mova/movu/movh are movq/movq/movd in the first and movdqa/movdqu/movq in the second.  All this makes it very easy to make one function do two things just by instantiating it twice.

4.  Automatic permutation handling.

Many macros output to different registers than they input from:

%macro SBUTTERFLY 4
mova      m%4, m%2
punpckl%1 m%2, m%3
punpckh%1 m%4, m%3
SWAP %3, %4
%endmacro

To handle this, the SWAP macro swaps the contents of the m%3 and m%4 registers… without doing anything.  Specifically, it swaps all future uses of those register names.  This completely eliminates the need to mentally track which values are where as they move between a half-dozen registers.

This is nowhere near all you can do with these macros: look at the x264 source for more examples.  Write better, faster, and more readable assembly code: use x264ASM.

9 Responses to “x264′s assembly abstraction layer: now free to use”

  1. Jai Says:

    I think it would be worthwhile to mention yasm somewhere in the write-up. Or does it work with other assemblers as well?

  2. Dark Shikari Says:

    It works with nasm and yasm. We dropped nasm for x264 because of a bug with nasm (and for consistency), but the syntax is identical for the two.

  3. Steinar H. Gunderson Says:

    Isn’t calling it “by far the best option for writing platform-independent x86 assembly” overlooking the possibilities of intrinsics? Granted, if you’re pushed you can probably do register allocation better than the compiler, but if your demands are not extreme it looks like a pretty sane solution, and automatically abstracts away both calling convention register differences (naming _and_ count).

  4. Dark Shikari Says:

    @Steinar

    In theory. But in reality, the compiler is terrible with intrinsics, especially when it comes to loading data into registers. I’ve seen gcc store a value 4 times to the stack and then perform a load, just to do what would normally be:

    movd xmm0, r0m
    pshufd xmm0, xmm0, 0

    Furthermore, the issue of register allocation is larger than you think. Even if the compiler was perfect, it cannot change your code: if it turns out that your code requires more registers than the architecture has available, it would probably be worthwhile to restructure it to avoid spills. If you just blindly use intrinsics, you can’t do this. And of course, in reality, the compiler is awful at register allocation.

    If you try to ignore the lowest levels of the problem, you inherently lose the ability to optimize higher levels of your approach based on those lower levels.

    Real situations in ffmpeg have shown intrinsics-based solutions ending up significantly slower than proper asm; one recent case (an Altivec FFT) was over twice as slow in intrinsics due to GCC’s inability to optimize the inter-asmfunction calling conventions properly.

  5. Steinar H. Gunderson Says:

    @Dark Shikari: I guess it depends on what you do. For the simple things I’ve needed, it’s within the measurement error (~2%) for my part. I do agree that there’s pitfalls, though. For someone interested in squeezing out the last drop of performance, surely pure assembler is the way to go, but for the rest of us intrinsics might not be all that bad, and the maintainability for any third party is really quite a lot higher than the wall of macros x264asm is.

  6. pengvado Says:

    Dark Shikari: 1.3x, not 2x.

    Steinar: if we restricted ourselves to things we could do with intrinsics (no complex macros), then the asm would have a lower barrier to entry too. So I don’t see intrinsics as even a tradeoff wrt complexity; they’re just slower for no benefit.
    Exceptions:
    If you have lots of scalar code to mix with the simd, then intrinsics are easier than the interface between c and inline asm, and easier than asm-optimizing the scalar part.
    Or if you rely on the compiler for more than just register allocation, load/stores, and scheduling: I occasionally use intrinsics for unroll/inline/constant-propagation This made a variable-size neural net very easy; but it had 50% overhead, not 2%.

  7. Victor Says:

    “; Kludge: Something on OS X fails to align .rodata even given an align attribute,”

    This is a known bug in NASM 2.07 and it has been fixed in git.

  8. orthochronous Says:

    i’ve just found this entry whilst looking for something else. It’s a bit sad if code generation from intrinsics is so poor, since I’m interested in writing SIMD code which in broad strategy isn’t specific to x86, and with some c++ wrapping i can see a broad outline of being able to write C++ level code for algorithms which would be reasonably efficient in both x86 SSE-x and ARM NEON. However, if the compiler’s really can’t remove the “abstraction penalty” this won’t fly.

    Does anyone have experience if this is only GCC that generates poor code, or do icc/llvm/whatever also do badly?

  9. Dark Shikari Says:

    @orth

    If you’re looking to do cross-platform SIMD, you should probably look at Orc. It’s not as good as handwritten, but it’s probably better than GCC and similar since it was designed from the ground up to do this specifically.

Leave a Reply