include/simd.h
author Radek Brich <radek.brich@devl.cz>
Mon, 19 May 2008 22:59:04 +0200
branchpyrit
changeset 98 64638385798a
parent 95 ca7d4c665531
permissions -rw-r--r--
add sections about demos to README update doxygen comments
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
94
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
     1
/**
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
     2
 * @file  simd.h
4c8abb8977dc update README
Radek Brich <radek.brich@devl.cz>
parents: 92
diff changeset
     3
 * @brief Abstraction of Intel SSE instruction set
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
 *
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
 * This file is part of Pyrit Ray Tracer.
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
 *
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
 * Copyright 2008  Radek Brich
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
 *
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
 * of this software and associated documentation files (the "Software"), to deal
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
 * in the Software without restriction, including without limitation the rights
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
 * copies of the Software, and to permit persons to whom the Software is
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
 * furnished to do so, subject to the following conditions:
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
 *
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
 * The above copyright notice and this permission notice shall be included in
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
 * all copies or substantial portions of the Software.
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
 *
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
 * THE SOFTWARE.
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
 */
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
#ifndef SIMD_H
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
#define SIMD_H
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
#include "common.h"
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
#ifndef NO_SIMD
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
#include <xmmintrin.h>
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
95
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
    37
#define mfloat4 __m128
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
95
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
    39
#define mZero  _mm_setzero_ps()
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
#define mOne   _mm_set_ps1(1.0f)
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
#define mTwo   _mm_set_ps1(2.0f)
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
#define mEps   _mm_set_ps1(Eps)
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
#define mMEps  _mm_set_ps1(-Eps)
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
#define mInf   _mm_set_ps1(Inf)
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
#define mMInf  _mm_set_ps1(-Inf)
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
#define mAllSet  _mm_cmplt_ps(mZero, mOne)
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
#define mset1 _mm_set_ps1
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
#define mset _mm_set_ps
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
#define madd _mm_add_ps
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
#define msub _mm_sub_ps
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
#define mmul _mm_mul_ps
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
#define mdiv _mm_div_ps
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
#define msqrt _mm_sqrt_ps
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    57
#define mand _mm_and_ps
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    58
#define mor  _mm_or_ps
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    59
#define mcmpgt _mm_cmpgt_ps
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    60
#define mcmplt _mm_cmplt_ps
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    61
#define mcmpge _mm_cmpge_ps
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    62
#define mcmple _mm_cmple_ps
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    63
#define mcmpeq _mm_cmpeq_ps
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    64
#define mcmpneq _mm_cmpneq_ps
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    65
#define mmin _mm_min_ps
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    66
#define mmax _mm_max_ps
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    67
#define mmovemask _mm_movemask_ps
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    68
95
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
    69
#define mShuffle0 _MM_SHUFFLE(0,0,0,0)
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
    70
#define mShuffle1 _MM_SHUFFLE(1,1,1,1)
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
    71
#define mShuffle2 _MM_SHUFFLE(2,2,2,2)
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
    72
#define mShuffle3 _MM_SHUFFLE(3,3,3,3)
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
    73
#define mshuffle _mm_shuffle_ps
ca7d4c665531 build script fixes, add ldflags build option
Radek Brich <radek.brich@devl.cz>
parents: 94
diff changeset
    74
98
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
    75
/** select values from a and b according to mask (a if mask is 1, b if mask is 0) */
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    76
inline const mfloat4 mselect(const mfloat4& mask, const mfloat4& a, const mfloat4& b)
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    77
{
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    78
	return _mm_or_ps(_mm_and_ps(mask, a), _mm_andnot_ps(mask, b));
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    79
}
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    80
98
64638385798a add sections about demos to README
Radek Brich <radek.brich@devl.cz>
parents: 95
diff changeset
    81
/** fast power function */
92
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    82
inline const mfloat4 mfastpow(const mfloat4& base, const mfloat4& exponent)
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    83
{
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    84
    __m128 denom = _mm_mul_ps(exponent, base);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    85
    denom = _mm_sub_ps(exponent, denom);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    86
    denom = _mm_add_ps(base, denom);
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    87
    return _mm_mul_ps(base, _mm_rcp_ps(denom));
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    88
}
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    89
#endif
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    90
9af5c039b678 add MSVC compiler support, make it default for Windows
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    91
#endif