• Create Account

Like
15Likes
Dislike

# Practical Cross Platform SIMD Math

By AllEightUp | Published Apr 26 2013 05:11 AM in General Programming
Peer Reviewed by (Josh Vega, Vilem Otte, jbadams)

C++ C language SIMD math cross platform C++ 11

Math and math types are the glue which holds a game together: collision, physics, movement and a multitude of other game elements are basically just large math problems to be solved. A well created math library is something you begin using and tend to forget about as you go, a poor library is something you continually stumble over. There are many tutorials on basic math, library setup and architecture, there are also many tutorials about SIMD support in general. What is rarely covered is creating a library in such a manner as to allow a scalable and multi-targeted SIMD backend with no major changes for the user. In this article we will cover the multiple levels of Intel SSE and, as an option for Xcode, additionally target ARM Neon for iDevices.

Note:
Keep in mind that the simple class presented here is not the best solution to using SIMD. Compilers don't like the idea of wrapping a fundamental type in a class and often do unrequired things in such cases. The abstraction layer does not suffer this problem but you have to code in a C like manner to use it. From a practical point of view though, if you are not familiar with SIMD coding, the wrapper class provides a nice speed boost without a lot of hassles involved.

This article is built using the environment from prior articles and as such you may want to read those. It is not required reading as the work done here is focused on the Intel SSE instruction sets and some minor examples of ARM Neon for comparisons. Only minor details of the build environment will need to be understood to take advantage of the presented material. If you are interested; the prior article series can be found here: Article

Unlike prior articles, this will simply present the reader with the final library which is hosted as a live project on Google Code. ARM Neon is discussed as a possible target but at this time the library is incomplete in that area, it will get progressively better in the future so check for updates. The code is hosted at: code. The directory “MathLibrary” corresponds to this article.

## Terminology

For the rest of the article there is a bit of vocabulary in use which needs to be understood or things can become confusing. First off, what is SIMD? The terse definition is “Single Instruction Multiple Data”. Used as such, it is referring to a subclass of instructions which the target CPU supplies. This does not mean it is specifically Intel SSE, Power PC Altivec (VMX) or ARM Neon instructions, it is a general term to refer to the class of instructions on a given target CPU. This article will be focused on the Intel SSE instruction set as it provides the most complicated variation, though when referring to other instruction sets, the specific name of the target will be given as: Neon, or VMX as examples. Additionally, as SIMD refers to a set of instructions, the term will not be used when describing the process of converting normal math code to use the instructions. Instead the generic term for applying SIMD instructions is ‘vectorization’ which is appropriate as SIMD instructions are also often referred to as vector processing. Understanding the differences in terminology will be crucial to the remaining article.

## Goals

The goal of the presented math library is to show multiple targets of SIMD instructions both based on specific CPU targets and levels of support given a target CPU. For instance, there are at least eight different variations of SSE on Intel CPUs at this time. Each level of SSE becomes significantly more powerful and corrects for missing items from prior versions. For instance, SSE 1 only supports vertical concepts of manipulation. In other words an SSE 1 based library loses considerable performance during the vectorization process due to an inability to occasionally work horizontally. SSE 3 added horizontal manipulations, specifically add and subtract, because of how useful they are in certain contexts such as performing dot products. Solving how to write a library which can support SSE 1 and SSE 3 (and higher) variations in a maintainable manner is one of the goals.

A secondary goal is to perform the systematic vectorization on various types yet still allow best fit selection of the underlying implementation. Using the Steam Hardware Survey, it is possible to conclude that targeting SSE 3 is quite viable given that 99.5% of all surveyed hardware in use supports that level of SSE. This is a simple solution and very reasonable, but what happens if you target your personal machine, enable SSE 4.2 and find that it is a very large performance gain for you? You might go look at the survey again but this time you find support is around 50% and would make the game less broadly acceptable. There are several solutions to this which we wish to support. You can compile your game twice, one for SSE 3 and the other for SSE 4.2, create a little launcher app which chooses which version to load. Another variation is to compile the game itself for SSE 3 and then move all the really heavy math portions into a shared library which can be recompiled for variations of the instruction sets which you load at runtime based on the availability of instructions. While the details of each method are beyond the scope here, supporting them is intended to be covered.

The final goal is to expose the SIMD instruction sets in a manner where systematic optimization using vectorization is possible. Simply vectorizing a 3D vector or a matrix is going to give notable gains, but applying vectorization to entire algorithms can make that seem trivial in comparison. Exposing the actual SIMD instructions in a manner which allows their use without using specific knowledge of the target is going to be possible. Of course this is quite a large topic and only the generic idea of how to approach it is covered, but the work is ready for expansion and usage in such a case.

## SIMD Instruction Sets

SIMD instructions come in many forms. If you have been using Intel CPUs for a while, you have probably watched the evolution from MMX to today's latest and greatest AVX instruction sets. Today, if you target hand held devices (iDevices specifically in this article), the ARM Neon instruction set is a relatively recent addition to the family of SIMD instruction sets. SIMD instruction sets are not new concepts, other processors have implemented such things for a while, the instructions are simply somewhat new to mainstream CPUs. In this article, we will focus on the subset of instructions intended to manipulate 4 floats at a time which is fairly common to most SIMD instruction sets.

One of the difficulties with SIMD instructions is specifically caused by Intel and the way they implemented the original SSE instructions. The intention was not to support horizontal math and they instead tried to push reorganizing data in programs to match the new instruction data layout expectations. Sometimes this was possible to do, but often it is really not a viable approach. As such, on the Intel CPUs, you often waste much of the performance gain with swizzling instructions. This changed as of SSE 3 with the addition of horizontal adds and related instructions which greatly speed the handling of functionality such as dot product and magnitude calculations. Even better, as of SSE 4.1 a single instruction dot product is available which speeds things even more in some cases.

Dealing with the multiple levels of SSE on Intel platforms is rather complicated and can lead to difficult-to-maintain code if done poorly. Targeting a completely different set of instructions for ARM Neon is even more difficult when maintaining a uniform interface. The similarities of the instruction sets help ease this difficulty but do not completely remove the problems.

# Vectorization

Starting the process of vectorization, we need to limit our scope to something fairly simple to begin with. At this point we are going to implement only a 3D vector class. There will be two implementations, not counting SIMD instruction set variations. One version is a normal C++ implementation which does not impose alignment requirements nor does it have a hidden 4th component. The other version is the vectorized variation with all the additional impositions added. Why two classes? Serialization is an area where vectorized types can get in the way. Unless you are writing a high performance binary serializer, it just doesn’t make sense to deal with the imposition of alignment and the hidden component in such a system. Awareness of the types is of course important but different systems have different needs and forcing vectorization where it does not belong is not good practice.

The names of the classes will follow a modified version of the Open GL conventions. The modification is simply a case of dropping the ‘gl’ prefix, since we use namespaces and modifying the postfix notations a little bit. So, the names will be Vector3f for a 3 component floating point standard C++ vector and Vector3fv for the vectorized version. The modified postfix naming just changes the meaning of ‘v’ from meaning a pointer to array of elements to mean vectorized. The concepts are similar and as a pointer type doesn’t make sense in the case of a primitive, the naming change does not cause conflict. This is not a particularly original naming convention but it works for the purposes of this article.

## Initial Work Environment

In prior articles I mentioned that I had a reason for duplicating the name of a library under the ‘Include’ directory but never got into the details. At this point we are about to use one of the reasons for the additional directory name. Instead of coding the Vector3f and Vector3fv classes side by side, we are going to split into two separate libraries for the time being: “Math” and “Math-SIMD”. While this is not required in general, the idea is to leverage the layout such that in a team where the primary math library could already be in use, you don’t want to introduce the vectorized types into the code base until they are ready. Of course you also want to be continually committing your work while you go, so moving the code to the separate library keeps it out of the way for others. But, you also don’t want to have different include paths which would have to be fixed up later when you merge the libraries. As such, both libraries can reference “Math” files via: “#include “Math/xxx.hpp” style inclusion but obviously only when you explicitly include the ‘Math-SIMD’ library can you access the new functionality.

While this is not required, it is a handy way to work in isolation for a while. Splitting libraries is a key feature of the build environment when discussing refactoring. When applied to large systems, you can move sections into temporary work locations, point to the original library and the new sub-library so nothing seems as if it changed. But, you can then make another library and rewrite a completely new version switching between implementations as needed. This variation of the usage is not covered here but it is a large benefit to the setup.

## Alignment Issues

One item to cover briefly is making sure any class which uses a vectorized math primitive can align itself in memory. Unfortunately the state of C++11 support is still in its infancy so it is required in this case to fall back on per-compiler style alignment specifications. Also, since this is a generic item needed through out the codebase, we will be placing it in the ‘Core’ library to be shared. If you look in ‘Libraries/Core/Include/Core/Core.hpp’ you will see the following subsection:

//////////////////////////////////////////////////////////////////////////
// Per compiler macro's.
#if defined( COMPILER_MSVC ) || defined( COMPILER_INTEL )

#       define BEGIN_ALIGNED( n, a )                            __declspec( align( a ) ) n
#       define END_ALIGNED( n, a )

#elif defined( COMPILER_GNU ) || defined( COMPILER_CLANG )

#       define BEGIN_ALIGNED( n, a )                            n
#       define END_ALIGNED( n, a )                              __attribute__( (aligned( a ) ) )

#endif


The macro has to be split into two pieces since MSVC-style compilers and GNU-style compilers use different methods of specifying alignment. MSVC compilers use a pre-class name ‘__declspec’ directive while GNU compilers use a postfix ‘attribute’ directive. When C++11 is better implemented, it will be possible to remove this bit of macro trash, but until then, this is the practical solution.

There are several downsides to this solution. It does not cover new/delete and as such if you new up a vector it will not be guaranteed to fit the alignment requirements of the underlying vectorized type. Another problem case is putting vectors into a std::vector or other container, there is no guarantee the memory will be aligned correctly until full support of ‘alignof’ is pushed out to the compilers. Finally, and most annoying, because we are wrapping a fundamental intrinsic type, passing an item to functions by value is not supported and may also break alignment rules. We will be working around these problems in general but they are important to keep in mind.

## SIMD Intrinsic Includes

With the ARM Neon instruction set, life is fairly simple. There is only one include file required and no varying levels of support to be dealt with. But this does not mean everything is simple due to the development environment for iDevices. When targeting a real device the ARM Neon instruction set is of course supported, but when targeting the simulators they are not. Xcode supports switching targets from the device to the simulator in the IDE, as such we need to differentiate between the targets at compile time. This is fairly easy but does mean, on the simulator, we have to disable the Neon instruction set and fall back to non-SIMD code. (NOTE: You could switch to Intel SSE due to this being a simulator and not an emulator, but we’ll just turn it off for now.)

Intel SSE is a bit of a can of worms as it is broken into various levels. For ease, we don’t follow the actual levels of SSE as they progressed but instead the header file levels which Intel supplies to compilers. The levels are thus broken down into MMX, 1, 2, 3, 3.1, 4.1, 4.2 and AVX. While the library will not actually supply unique code for all of these levels, there will be a structure in place to allow future specialization. Also, we list out MMX as a possible target, though it will not be used initially. The includes in highest to lowest order, with some notes:

immintrin.h - AVX 256 bit SIMD instruction set. (Defines __m256.)
wmmintrin.h - AVX AES instructions.
nmmintrin.h - The Core 2 Duo 4.2 SIMD instruction set.
smmintrin.h - The Core 2 Duo 4.1 SIMD instruction set.
tmmintrin.h - SSSE 3 aka SSE 3.1 SIMD instruction set.
pmmintrin.h - SSE 3 SIMD instruction set.
emmintrin.h - SSE 2 SIMD instruction set.
xmmintrin.h - SSE(1) SIMD instruction set. (Defines __m128.)
mmintrin.h - MMX instruction set. (Defines __m64.)

The modifications to compiler options, the specifically exposed CMake options and detection of the various levels to select appropriate includes can be found in the math library environment. Specifically dealing with the different includes and the Xcode oddities is handled in the file “Math-SIMD/Include/Math/Simd/Simd.hpp”. It is a fairly ugly little header due to all the SSE options and the simulator difference, but it gets the job done properly.

## The Intrinsic SIMD Abstraction Layer

In order to support the different target processors we need to begin abstraction of the data types. At the same time, we are going to be implementing a fallback case for CPUs without SIMD. In fact, implementation of the reference support is a key feature of the library. What we are going to be writing is something similar to the SSEPlus library abstraction of the Intel intrinsics. (See: http://sseplus.sourceforge.net) We will only be dealing with the 4 element floating point types, so we don’t really have that many intrinsics to abstract; basic math, load/store and some manipulation abilities are all we really need. There are exceptions we will be dealing with, but for now this is a good enough starting point.

## Doing The Real Work

For the time being, we start the Vector3fv class as an empty class. We will be refactoring this to be the vectorized version once we get some preparations out of the way. The first step is defining how we will be working, as implied previously we are using the intrinsics and not going directly to assembly language. Beyond just being easier, using the intrinsics provides nearly as much performance gain as hand coding assembly in this case. In fact, with the compilers involved, quite often using the intrinsics produces faster code since register allocations are chosen to fit into surrounding code better. Having said that, the math primitives will not be the most cutting edge and blazing fast items in the world, just a good starting point for further optimization.

Our next step is setting up the SIMD instruction set such that it is usable by the Vector3fv class (and other systems as desired) in a manner where it is an abstraction on top of the specific SIMD instructions. There are a number of items which can be used which, beyond naming, behave pretty much identically on all the different SIMD instruction sets. For instance, the ability to add, subtract, multiply and divide are pretty much common on each CPU and as such should be as light a wrapper as possible. We’ll be following the same basic style of abstraction as presented in the Intel SSE intrinsics themselves, just at an extra level of abstraction. The primary header to figure out the instruction set selection is “Include/Math/Simd/Simd.hpp”, by default it always allows use of the reference C++ implementation so even if you turn off all Simd, everything will continue to compile and function. After inclusion, a macro is defined named: “Simd_t” which is the primary abstraction into the build selected default instruction set. This is a macro since, as part of the goals, we want to be able to run multiple and simultaneous versions of the code based on different variations of the instruction sets, a typedef would not be able to be overridden by other code.

The first thing we need in order to use the SIMD instruction sets is a type. The primary 4 element floating point type is defined as “Simd_t::Vec4f_t”. The reference implementation defines this as simply a structure wrapped around a 4 element array of floats, the Intel SSE implementation defines the type as ‘__m128’ and ARM Neon defines the type as ‘float32x4_t’. The first rule is to treat these types as completely opaque black boxes, even if you know it is an ‘__m128’, don’t go using the structure underlying the type directly, always use the “Simd_t” exposed functions. Of course, a nice benefit to maintaining the reference library is that if you break the rules, either the reference version or whatever the active Simd instruction set implemented version will likely fail to compile.

In order to structure and maintain the multiple abstractions we add the following structure to the project:

Include/Math/Simd
Include/Math/Simd/Reference
Include/Math/Simd/Sse
Include/Math/Simd/Neon


Each new directory implements the specific instruction set and variations of each. So, ‘Reference’ and ‘Neon’ both contain single files which expose the SIMD abstraction and ‘Sse’ contains several different files. The content of the files end up defining structures in the namespaces as follows:

Math::Simd struct Reference;

Math::Simd struct Sse;
Math::Simd struct Sse3;
Math::Simd struct Sse4;
Math::Simd struct Avx;

Math::Simd struct Neon;


Notice that for SSE we only support 1, 3, 4 and Avx. The reason is that, for the time being, there is no use implementing MMX, SSE2 or the inbetween versions since they add nothing significant to the math instructions. SSE2 may be included eventually so as to support double types but this is a future extension. Also, if you look in the supplied code, Avx derives from Sse4 which in turn derives from Sse3 etc. We use the simple hierarchy to hide progressively lesser capable variations of the instruction set.

With all the structure out of the way, the method to use it needs to be discussed. Our first abstraction is going to be dealing with the need to initialize SIMD types. While all the current types we are targeting can be initialized with aggregate construction such as the following:

Simd_t::Vec4f_t  mine = {1.0f, 2.0f, 3.0f};


This leaves a bit to be desired and may also not work on future instruction sets if specialized types are introduced. The undesirable feature of this is that the fourth component is left uninitialized and for purposes of the abstraction, we want the hidden component always to be 0.0f. In fact, when we add debugging, asserting that the hidden component is 0.0f will be part of the checks. So, we introduce our first abstraction function, which just happens to be the standard form for all future abstraction functions:

namespace Math
{
namespace Simd
{
struct Reference
{
static Vec4f_t      Create( float x, float y, float z )
{
Vec4f_t  result  = {x, y, z, 0.0f};
return result;
}
}
}
}


The function simply creates a temporary, initializes it and returns it. Because this is such a standard form, most compilers will completely optimize the function out of release code and the cost of the abstraction remains zero. Visual Studio, Clang and GCC all deal with this form of optimization (often referred to as the Return Value Optimization) fairly well and only a few slower code generation mistakes result from this. With this function in hand, it is possible to initialize our Vector3fv.

First, there is some preparation to perform. We need to modify the Vector3fv to be an aligned class or the optimized SIMD instructions will fail:

class BEGIN_ALIGNED( Vector3fv, 16 )
{

private:
Simd_t::Vec4f_t    mData;
} END_ALIGNED( Vector3fv, 16 );


There is still a problem though. We don’t actually want to tie this class to the default data type based on build settings, we need to be able to change as desired. So, we will be renaming this class and templating it as follows with a default typedef to stand in as Vector3fv:

template< Simd = Simd_t >
class BEGIN_ALIGNED( Vector3f_Simd, 16 )
{
public:
typedef typename Simd::Vec4f_t                  ValueType_t;

private:
ValueType_t       mData;
} END_ALIGNED( Vector3f_Simd, 16 );

typedef Vector3f_Simd< Simd_t >  Vector3fv;


With the general changes to the class behind us, let’s use the SIMD abstraction and create the constructors:

template< Simd = Simd_t >
class BEGIN_ALIGNED( Vector3f_Simd, 16 )
{
public:
typedef typename Simd::Vec4f_t                  ValueType_t;

Vector3f_Simd()                                                    {}
Vector3f_Simd( const Vector3f_Simd& rhs ) : mData( rhs.mData )     {}
Vector3f_Simd( float x, float y, float z )
: mData( Simd::Create( x, y, z )                                   {}

private:
ValueType_t       mData;
} END_ALIGNED( Vector3f_Simd, 16 );

typedef Vector3f_Simd< Simd_t >  Vector3fv;


A key note here is that the default constructor does not initialize to zero. The type is intended to be used much like a fundamental float type and with the same rules. From a class purity point of view, yes this should default initialize to zero though. The general reason for this decision to act like a fundamental type is that compilers are still fairly weak at removing some unneeded temporary initializations and in the case of primitive math types it gets very expensive. The class of bugs caused by this is no different than missing float and int initializations though unfortunately the compiler will not always complain about such things. Debugging additions will be used to catch such errors later though.

Another note is that the fundamental type exposed as Vec4f_t can be copy constructed like any fundamental type. This is critical to leverage as the compilers are able to leave types in registers longer while in use if they recognize copying and optimize appropriately. Gcc seems to be the weakest of the compilers in this area but still does a fair job of not flushing registers until really needed.

## The First SIMD Instruction

At this point we will show the first SIMD instruction abstraction. It will also be the last individual instruction as the basics are the same for each. We’ll look at more complicated functionality in the next section.

We will expose an operation for “Add” in the SIMD abstraction and then implement the addition operator within the vector class. In SSE, the _mm_add_ps intrinsic (addps in assembly) is quite simple:

__m128 _mm_add_ps( __m128 lhs, __m128 rhs );


For Neon, the intrinsic is:

float32x4_t vaddq_f32( float32x4_t lhs, float32x4_t rhs );


Basically the instructions are identical except in name and type. This makes it quite simple to implement things for all targets.

So, let’s start by implementing the reference C++ version:

// in Math::Simd::Reference:
static Vec4f_t Add( const Vec4f_t& lhs, const Vec4f_t& rhs )
{
Vec4f_t  result =
{
lhs.v[ 0 ] + rhs.v[ 0 ],
lhs.v[ 1 ] + rhs.v[ 1 ],
lhs.v[ 2 ] + rhs.v[ 2 ],
lhs.v[ 3 ] + rhs.v[ 3 ]
};
return result;
}


It is worth noting that we are passing by reference in this function but in the other abstractions we will be passing by value. This is a minor optimization applied to the reference implementation which may or may not be required. But in general it has no effect on usage as C++ will figure out the proper passing style be it reference or value. Also keep in mind that while the 3rd index is hidden, we still operate on it because that is how the SIMD instructions function and we are not targeting just Vector3 but eventually 2 and 4 dimension items also.

Now the SSE implementation:

// In Math::Simd::Sse:
static Vec4f_t Add( Vec4f_t lhs, Vec4f_t rhs )
{
}


The Neon implementation:

// In Math::Simd::Neon:
static Vec4f_t Add( Vec4f_t lhs, Vec4f_t rhs )
{
}


And finally the operator:

Vector3f_Simd  operator + ( const Vector3f_Simd& rhs ) const  {return Simd::Add( mData, rhs );}


If you build and compile a test which tried to use this, you would currently get an error since you can not construct a Vector3f_Simd from the fundamental SIMD type. We will add the constructor for this, it is both required and proper. Of course it should be noted that any conversion operator or constructor should ‘usually’ be marked explicit (conversion operators are able to be marked as such in C++11) we want to encourage passing the fundamental type around as it will pass in a register and generally be faster than passing the wrapper class. So, in this rare occasion I leave the constructor and operator as non-explicit.

As you can see, this pattern is very easy to implement, once the conversions are handled. We won’t perform all the repetitions here as there is nothing notable to learn from such grunt work. Instead we move onto the more interesting bits.

## The Dot Product

The dot product of two vectors is the first composite operation to be presented. It is also a case where the different levels of Intel SSE provide additional operations which speed the computation up significantly as higher levels of SSE are used. In order to maintain the abstraction and provide the benefits of the higher level instructions, we treat the entire dot product as a single intrinsic instead of attempting to implement it using the currently exposed intrinsic instructions. Without getting into the details, we’ll just present the reference version of the operation to start with:

static float Dot3( const Vec4f_t& lhs, const Vec4f_t& rhs )
{
return ( lhs.v[ 0 ]*rhs.v[ 0 ] + lhs.v[ 1 ]*rhs.v[ 1 ] + lhs.v[ 2 ]*rhs.v[ 2 ] );
}


The operation is basically a SIMD multiplication of all elements, though ignoring the hidden in this case, and then an addition of each resulting element. From a terminology point of view, this is generally called a vertical multiply followed by a horizontal addition. Unfortunately, the original Intel SSE did not support the horizontal operation which causes a signficant loss of performance. The Intel SSE version is thus considerably more complex:

static float Dot3( const Vec4f_t& lhs, const Vec4f_t& rhs )
{
static const int swap1 = _MM_SHUFFLE( 3, 3, 3, 2 );
static const int swap2 = _MM_SHUFFLE( 3, 3, 3, 1 );
float sresult;

__m128 result = _mm_mul_ps( lhs, rhs );
__m128 part1  = _mm_shuffle_ps( result, result, swap1 );
__m128 part2  = _mm_shuffle_ps( result, result, swap2 );

result = _mm_add_ps( result, part1 );
result = _mm_add_ps( result, part2 );

_mm_store_ss( &sresult, result );
return sresult;
}


Without getting into details, after multiplying the individual components, we have to swap things around a bit in order to add them all together and then store the result. While generally faster than the reference version, it is not a major win due to the swizzle instructions. Thankfully, in steps the SSE 4 version though, at less than half the cycle count:

static float Dot3( const Vec4f_t& lhs, const Vec4f_t& rhs )
{
float sresult;
__m128 result = _mm_dp_ps( lhs, rhs, 0x77 );
_mm_store_ss( &sresult, result );
return sresult;
}


By inserting these functions into the library of support files and structures, we have access to a per target optimized dot product. By expecting only that the container classes are properly aligned, the implementation of the vector type becomes identical to the non-vectorized version when finished. The level of abstraction is a bit fine grained but we are using assembly language instructions, which are about as fine grained as possible anyway, we are actually stepping back just a bit to contain complete concepts. Further extensions such as performing transformations on entire buffers can be done as single abstractions and optimized heavilly for each target, the speed increases compared to vectorizing just the Vector3 types is fairly impressive in comparison.

## Conclusion

With the example implementation and the overview, hopefully this has given you a method of using a SIMD math library in a manner which does not intrude on everyday work. While not a particularly long or detailed article, the overview of the design and architecture of the code should provide a simple to follow pattern which extends to nearly any specialized math processing which can be used in general purpose programming. The example library will be fleshed out and expanded on for the next article in which it will be used further. At that time I will also be introducing a simple testbed for the library which will become a benchmarking item for future work.

## Note On The Results

While the next article will cover some of this, I figured I'd drop some results of the work:

Using a simple brute force ray tracer with
1024x1024 3x SuperSampling on a simple scene.

Normal : ~43500ms
Reference : ~53400ms
SSE : ~41400ms
SSE 3 : ~33800ms
SSE 4 : ~29300ms

Secondary note: the performance differences also include enabling additional compiler options which speed up the overall program. The next article will use a better controlled timing method which will reflect only the Vector3fv optimizations.

Amazing article. This is exactly the kind of thing I've been looking for recently.

Good article, although I think you got 1 thing a bit wrong - 1x supersampling - it would mean you do just 1 sample per pixel, which is obviously equal to no-supersampling at all. You probably mean 2 samples per pixel (2x supersampling) or 2x2 samples per pixel (4x supersampling).

Good starting point indeed.

Did you have a look at the soon to be proposed Boost.SIMD library. It already covers all SSE variation, AVX and Altivec and provides complete support for libc, math and TR1 functions. We also seamlessly interoperate with standard algorithm so a simple std::transform call is able to generate vectorized code.

Regards

Good article, although I think you got 1 thing a bit wrong - 1x supersampling - it would mean you do just 1 sample per pixel, which is obviously equal to no-supersampling at all. You probably mean 2 samples per pixel (2x supersampling) or 2x2 samples per pixel (4x supersampling).

Err, oops..    It's 1 additional level of sampling by the settings, or 2x = 9 per pixel..  Will correct it.  Err, hmm is that 3x, been so long....  Damn

Good starting point indeed.

Did you have a look at the soon to be proposed Boost.SIMD library. It already covers all SSE variation, AVX and Altivec and provides complete support for libc, math and TR1 functions. We also seamlessly interoperate with standard algorithm so a simple std::transform call is able to generate vectorized code.

Regards

I had not seen the Boost version as I don't tend to keep up with proposed items.  I might have to reconsider it when it gets into an official state, till then though I'm likely to avoid Boost in these articles as long as possible.  This is not anything against Boost, the C++11 standard integration pulled my favorite bits over except the file system, so there isn't a lot I care to use from Boost, except perhaps in tools.

Good starting point indeed.

Did you have a look at the soon to be proposed Boost.SIMD library. It already covers all SSE variation, AVX and Altivec and provides complete support for libc, math and TR1 functions. We also seamlessly interoperate with standard algorithm so a simple std::transform call is able to generate vectorized code.

Regards

I'm wondering, how does it fare in comparison with the C++ vector class library by Agner Fog (both w.r.t. features and performance):

http://www.agner.org/optimize/#vectorclass

// EDIT: AllEightUp -- same question :-)

Best

Good starting point indeed.

Did you have a look at the soon to be proposed Boost.SIMD library. It already covers all SSE variation, AVX and Altivec and provides complete support for libc, math and TR1 functions. We also seamlessly interoperate with standard algorithm so a simple std::transform call is able to generate vectorized code.

Regards

I'm wondering, how does it fare in comparison with the C++ vector class library by Agner Fog (both w.r.t. features and performance):

http://www.agner.org/optimize/#vectorclass

// EDIT: AllEightUp -- same question :-)

Best

Once again, I had never looked at that implementation but from a personal point of view, I don't like that version very much.  It uses SIMD conventions and as such is not a near drop in replacement for a normal C++ version.  It also doesn't seem to expose the underlying SIMD abstraction as a utility usable in other areas.  I also didn't see the ability to use different targets in the same compilation, i.e. target SSE3 for the main executable yet in the same solution/project/whatever build a SSE3-AVX supporting shared library.  I'm not proud of using macro's but in this case it makes all sorts of sense to do so.

Speed wise, any other will likely be faster right now.  I spent time on clarity and versatility and did not optimize things in any real detail.  One of the reasons for the git repository though is that I'm very open to others jumping in and helping out.

(NOTE: I just briefly looked at it, I could have missed things.  I'll look in more detail later.)

Good starting point indeed.

Did you have a look at the soon to be proposed Boost.SIMD library. It already covers all SSE variation, AVX and Altivec and provides complete support for libc, math and TR1 functions. We also seamlessly interoperate with standard algorithm so a simple std::transform call is able to generate vectorized code.

Regards

I'm wondering, how does it fare in comparison with the C++ vector class library by Agner Fog (both w.r.t. features and performance):

http://www.agner.org/optimize/#vectorclass

// EDIT: AllEightUp -- same question :-)

Best

Once again, I had never looked at that implementation but from a personal point of view, I don't like that version very much.  It uses SIMD conventions and as such is not a near drop in replacement for a normal C++ version.  It also doesn't seem to expose the underlying SIMD abstraction as a utility usable in other areas.  I also didn't see the ability to use different targets in the same compilation, i.e. target SSE3 for the main executable yet in the same solution/project/whatever build a SSE3-AVX supporting shared library.  I'm not proud of using macro's but in this case it makes all sorts of sense to do so.

Speed wise, any other will likely be faster right now.  I spent time on clarity and versatility and did not optimize things in any real detail.  One of the reasons for the git repository though is that I'm very open to others jumping in and helping out.

(NOTE: I just briefly looked at it, I could have missed things.  I'll look in more detail later.)

Good starting point indeed.

Did you have a look at the soon to be proposed Boost.SIMD library. It already covers all SSE variation, AVX and Altivec and provides complete support for libc, math and TR1 functions. We also seamlessly interoperate with standard algorithm so a simple std::transform call is able to generate vectorized code.

Regards

I'm wondering, how does it fare in comparison with the C++ vector class library by Agner Fog (both w.r.t. features and performance):

http://www.agner.org/optimize/#vectorclass

// EDIT: AllEightUp -- same question :-)

Best

Once again, I had never looked at that implementation but from a personal point of view, I don't like that version very much.  It uses SIMD conventions and as such is not a near drop in replacement for a normal C++ version.  It also doesn't seem to expose the underlying SIMD abstraction as a utility usable in other areas.  I also didn't see the ability to use different targets in the same compilation, i.e. target SSE3 for the main executable yet in the same solution/project/whatever build a SSE3-AVX supporting shared library.  I'm not proud of using macro's but in this case it makes all sorts of sense to do so.

Speed wise, any other will likely be faster right now.  I spent time on clarity and versatility and did not optimize things in any real detail.  One of the reasons for the git repository though is that I'm very open to others jumping in and helping out.

(NOTE: I just briefly looked at it, I could have missed things.  I'll look in more detail later.)

OK, I'm just going to take the parenthetical note as a partial answer for now and wait for a more detailed one later :-)

I think it's worth looking at, you may find it actually fits your needs. For instance, dispatching (multiple targets) is definitely supported, see the "Instruction sets and CPU dispatching" section:

"The vector class library requires the SSE2 instruction set as a minimum, but it makes
more efficient code when a higher instruction set is used.

[...]

The vector class library makes it possible to compile for different instruction sets
from the same source code by using preprocessing branches. Different versions
are made simply by recompiling the code with different compiler options."

• can build code for different instruction sets from the same source code
• CPU dispatching to utilize higher instruction sets when available
• uses metaprogramming (including preprocessing directives and
templates) to find the best implementation for the selected instruction set
and parameter values of a given operator or function

// BTW, I'm not sure what you mean specifically when you mention "exposing the underlying SIMD abstraction" (functions and SIMD data structures are very much available).

You might want to fix your post, it double included the thread..

OK, I'm just going to take the parenthetical note as a partial answer for now and wait for a more detailed one later :-)

I think it's worth looking at, you may find it actually fits your needs. For instance, dispatching (multiple targets) is definitely supported, see the "Instruction sets and CPU dispatching" section:

"The vector class library requires the SSE2 instruction set as a minimum, but it makes
more efficient code when a higher instruction set is used.

[...]

The vector class library makes it possible to compile for different instruction sets
from the same source code by using preprocessing branches. Different versions
are made simply by recompiling the code with different compiler options."

So, for one thing, recompiling the code is not required with the abstraction system presented here.  Well, using MSVC that is, OsX actively prevents inclusion of the intrinsics if the compiler is not set to the proper level which in turn means that you do have to fiddle build settings for the different levels.  Anyway though, if you look in the unit tests, I perform side by side testing of all variations at the same time.  What good does this do?  Well, you can set the compiler to generate SSE3 but then using the abstraction layer directly target all the way to AVX (well, when I actually add code there) for key functions.  Basically the dispatch system mentioned in the documentation you supplied but without the need of recompiling with different flags.  Of course this only works on MSVC since you have to enable different levels on gcc/clang in order to allow the intrinsics, kinda unfortunate since it is nice to be able to just put the code in the same executable instead of having to break it out into so/dll's to do the same thing.

Overall, not really the most useful thing thanks to the compiler differences, except in the case of unit testing.

• can build code for different instruction sets from the same source code
• CPU dispatching to utilize higher instruction sets when available
• uses metaprogramming (including preprocessing directives and
templates) to find the best implementation for the selected instruction set
and parameter values of a given operator or function

Yup, basically identical goals until you get to this next bit.

// BTW, I'm not sure what you mean specifically when you mention "exposing the underlying SIMD abstraction" (functions and SIMD data structures are very much available).

Any class wrapper around a fundamental type unfortunately looses a certain amount of performance compared to using the direct intrinsics.  So, using the Vector3fv from this article you loose about 5% performance compared to identical hand coded intrinsics.  I'm willing to give up a bit of performance for the ease of use and 5% is trivial compared to the 150% speed increases the next article will show in the testbed.  (And I can get the loss down to about 2% when I introduce a couple extra bits.)    Anyway, the point is you can turn around and decide you need that extra 5% back and use the provided intrinsic wrapper directly to bypass the cost of the class wrapper, this is why it is exposed separated from the class.

Additionally, because you can use the abstraction without the class you can write dedicated vectorized code using the abstraction layer, again bypassing any wrapper.  A simple example, say you need to take the average of a couple hundred floats.  i.e. "for( float it : floats ) accum+=it; accum/=floats.size();"  You have no guarantee that the floats are aligned so you could do something which takes the first couple unaligned values and adds them together, then uses the SIMD abstraction layer to add all the full aligned 4 element sections together, pull in the trailing floats, and finally add the three values together and divide for the result.  You can get a ~300% speed increase with this, it is compatible with non-simd and all functional target SIMD instructions (i.e. SSE1-AVX, Neon when it is finished, etc).  This usage is generally easier if you don't have class wrappers in the way, the intrinsics are directly usable which gives the best of both worlds.

A note about the article I have to clean up.  Turns out I was using a mix of documents for timing information and the SSE3 version of Dot3 is actually slower than the SSE1 version on the same processor.  I.e. I had older timing info for SSE1 than the information on the 'hadd' instruction I was looking at but the SSE1 instructions also got faster such that on a SSE3 capable machine the SSE1 combination of instructions is still faster.  I'll explain how I discovered this during the follow up article, it led me on a merry chase today....

Good starting point indeed.

Did you have a look at the soon to be proposed Boost.SIMD library. It already covers all SSE variation, AVX and Altivec and provides complete support for libc, math and TR1 functions. We also seamlessly interoperate with standard algorithm so a simple std::transform call is able to generate vectorized code.

Regards

I'm wondering, how does it fare in comparison with the C++ vector class library by Agner Fog (both w.r.t. features and performance):

http://www.agner.org/optimize/#vectorclass

// EDIT: AllEightUp -- same question :-)

Best

Well I'll have to do some bench but i suspect we're in the same ballpark.

Boost.SIMD main asset is the fact the code you write for both scalar and SIMD is the same as it relies on stadnard algorithm. Interface exhibiting stuff like vec<Size><Type> are a bit limited for my taste.

SESLI

Your website is very nice. I visit all the time. You been sharing useful information.
We are familiar with in your way.
How much do my very good the sulfur, the sulfur I do not know how to thank you. Thank you so much
Getting useful information for my website

Sesli Chat ne-nerede nenerede bonibon
speakychat sesliduy sesliduy sesliduy sesliduy seslichat seslichat seslichat sesli sohbet seslisohbet seslichat seslisayfam seslisayfam seslisayfam seslisayfam seslisayfam seslicevher seslisayfam seslicevher seslicevher sesli dunya seslidunya sesli dünya seslipop seslipop seslipop kral tv sesli 18 web tasarim video klip videoklip faceebook kürtce chatSchlüsselnotdienst Hamburg Schlüsseldienst Hamburg Schlüsseldienst Hamburg

Thank you for submitting this article, I learned some new things indeed!

Several questions come to my mind, though:

Note: Please offer only positive, constructive comments - we are looking to promote a positive atmosphere where collaboration is valued above all else.

PARTNERS