├── .gitattributes
├── DFTTest
├── DFTTest.vcxproj.filters
├── DFTTest.h
├── VCL2
│ ├── vectorclass.h
│ ├── instrset_detect.cpp
│ ├── LICENSE
│ ├── vectormath_common.h
│ ├── vector_convert.h
│ ├── vectormath_hyp.h
│ └── vectormath_trig.h
├── DFTTest.vcxproj
├── DFTTest_AVX2.cpp
├── DFTTest_AVX512.cpp
└── DFTTest_SSE2.cpp
├── .gitignore
├── .github
└── workflows
│ └── build.yaml
├── meson.build
├── README.md
└── LICENSE
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
--------------------------------------------------------------------------------
/DFTTest/DFTTest.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | Source Files
20 |
21 |
22 | Source Files
23 |
24 |
25 | Source Files
26 |
27 |
28 | Source Files
29 |
30 |
31 | Source Files
32 |
33 |
34 |
35 |
36 | Header Files
37 |
38 |
39 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Prerequisites
2 | *.d
3 |
4 | # Compiled Object files
5 | *.slo
6 | *.lo
7 | *.o
8 | *.obj
9 |
10 | # Precompiled Headers
11 | *.gch
12 | *.pch
13 |
14 | # Compiled Dynamic libraries
15 | *.so
16 | *.dylib
17 | *.dll
18 |
19 | # Fortran module files
20 | *.mod
21 | *.smod
22 |
23 | # Compiled Static libraries
24 | *.lai
25 | *.la
26 | *.a
27 | *.lib
28 |
29 | # Executables
30 | *.exe
31 | *.out
32 | *.app
33 |
34 | # User-specific files
35 | *.rsuser
36 | *.suo
37 | *.user
38 | *.userosscache
39 | *.sln.docstates
40 |
41 | # Visual Studio 2015/2017 cache/options directory
42 | .vs/
43 |
44 | # Files built by Visual Studio
45 | *_i.c
46 | *_p.c
47 | *_h.h
48 | *.ilk
49 | *.meta
50 | *.obj
51 | *.iobj
52 | *.pch
53 | *.pdb
54 | *.ipdb
55 | *.pgc
56 | *.pgd
57 | *.rsp
58 | *.sbr
59 | *.tlb
60 | *.tli
61 | *.tlh
62 | *.tmp
63 | *.tmp_proj
64 | *_wpftmp.csproj
65 | *.log
66 | *.vspscc
67 | *.vssscc
68 | .builds
69 | *.pidb
70 | *.svclog
71 | *.scc
72 |
73 | # Visual C++ cache files
74 | ipch/
75 | *.aps
76 | *.ncb
77 | *.opendb
78 | *.opensdf
79 | *.sdf
80 | *.cachefile
81 | *.VC.db
82 | *.VC.VC.opendb
83 |
84 | # Visual Studio profiler
85 | *.psess
86 | *.vsp
87 | *.vspx
88 | *.sap
89 |
90 | # Windows thumbnail cache files
91 | Thumbs.db
92 | Thumbs.db:encryptable
93 | ehthumbs.db
94 | ehthumbs_vista.db
95 |
96 | # Dump file
97 | *.stackdump
98 |
99 | # Folder config file
100 | [Dd]esktop.ini
101 |
102 | # Recycle Bin used on file shares
103 | $RECYCLE.BIN/
104 |
105 | # Windows Installer files
106 | *.cab
107 | *.msi
108 | *.msix
109 | *.msm
110 | *.msp
111 |
112 | # Windows shortcuts
113 | *.lnk
114 |
--------------------------------------------------------------------------------
/DFTTest/DFTTest.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 | #include
5 | #include
6 | #include
7 |
8 | #include
9 | #include
10 |
11 | #include
12 |
13 | using unique_float = std::unique_ptr;
14 | using unique_fftwf_complex = std::unique_ptr;
15 | using unique_VSFrameRef = std::unique_ptr;
16 |
17 | struct DFTTestData final {
18 | VSNodeRef * node;
19 | const VSVideoInfo * vi;
20 | int sbsize, sosize, tbsize, tosize, swin, twin;
21 | double sbeta, tbeta;
22 | float f0beta;
23 | bool zmean, process[3];
24 | float srcScale, dstScale;
25 | int barea, bvolume, ccnt, ccnt2, type, sbd1, inc, peak;
26 | bool uf0b;
27 | const VSFormat * padFormat;
28 | int padWidth[3], padHeight[3], eheight[3];
29 | unique_float hw{ nullptr, nullptr }, sigmas{ nullptr, nullptr }, sigmas2{ nullptr, nullptr }, pmins{ nullptr, nullptr }, pmaxs{ nullptr, nullptr };
30 | unique_fftwf_complex dftgc{ nullptr, nullptr };
31 | std::unique_ptr ft{ nullptr, nullptr }, fti{ nullptr, nullptr };
32 | std::unordered_map ebuff;
33 | std::unordered_map dftr;
34 | std::unordered_map dftc, dftc2;
35 | void (*copyPad)(const VSFrameRef * src, VSFrameRef * dst[3], const DFTTestData * const VS_RESTRICT d, const VSAPI * vsapi) noexcept;
36 | void (*filterCoeffs)(float * dftc, const float * sigmas, const int ccnt, const float * pmin, const float * pmax, const float * sigmas2) noexcept;
37 | void (*func_0)(VSFrameRef * src[3], VSFrameRef * dst, const DFTTestData * const VS_RESTRICT d, const VSAPI * vsapi) noexcept;
38 | void (*func_1)(VSFrameRef * src[15][3], VSFrameRef * dst, const int pos, const DFTTestData * const VS_RESTRICT d, const VSAPI * vsapi) noexcept;
39 | };
40 |
--------------------------------------------------------------------------------
/.github/workflows/build.yaml:
--------------------------------------------------------------------------------
1 | name: Build
2 | on:
3 | - push
4 | - release
5 | - pull_request
6 | - workflow_dispatch
7 |
8 | jobs:
9 | build:
10 | runs-on: windows-latest
11 | strategy:
12 | matrix:
13 | arch:
14 | - amd64
15 | steps:
16 | - uses: actions/checkout@v2
17 | - name: setup MS dev commands
18 | uses: ilammy/msvc-dev-cmd@v1
19 | with:
20 | arch: ${{ matrix.arch }}
21 | - name: Setup Python
22 | uses: actions/setup-python@v1
23 | with:
24 | python-version: '3.x'
25 | - name: install meson and ninja
26 | run: pip install meson ninja
27 |
28 | - name: Cache fftw3
29 | id: cache-fftw3
30 | uses: actions/cache@v2
31 | with:
32 | path: ${{ github.workspace }}/vcpkg/installed/
33 | key: ${{ runner.os }}-fftw3-v1
34 | - name: Run vcpkg
35 | uses: lukka/run-vcpkg@v4
36 | if: steps.cache-fftw3.outputs.cache-hit != 'true'
37 | with:
38 | vcpkgArguments: 'fftw3[avx2,threads]:x64-windows-static'
39 | vcpkgDirectory: '${{ github.workspace }}/vcpkg'
40 | vcpkgGitCommitId: 5568f110b509a9fd90711978a7cb76bae75bb092 # 2021.05.12 release
41 | - name: Install pkg-config lite
42 | run: choco install pkgconfiglite
43 |
44 | - name: download VS headers and patch header location
45 | shell: bash
46 | run: |
47 | git clone https://github.com/AmusementClub/vapoursynth-classic --depth=1 --branch R54 vapoursynth
48 | cp vapoursynth/include/*.h DFTTest/
49 | sed -i -e '/#include |""|' DFTTest/DFTTest.h
50 | - name: Meson setup
51 | run: meson setup builddir/ -Db_vscrt=mt -Dpkg_config_path=${{ github.workspace }}/vcpkg/installed/x64-windows-static/lib/pkgconfig
52 | - name: Meson compile
53 | run: meson compile -C builddir/ -v
54 | - name: Upload artifact
55 | uses: actions/upload-artifact@v2
56 | with:
57 | name: release-${{matrix.arch}}
58 | path: |
59 | builddir/dfttest.dll
60 |
--------------------------------------------------------------------------------
/meson.build:
--------------------------------------------------------------------------------
1 | project('DFTTest', 'cpp',
2 | default_options: ['buildtype=release', 'b_ndebug=if-release', 'cpp_std=c++17'],
3 | meson_version: '>=0.51.0',
4 | version: '7'
5 | )
6 |
7 | cxx = meson.get_compiler('cpp')
8 |
9 | sources = [
10 | 'DFTTest/DFTTest.cpp',
11 | 'DFTTest/DFTTest.h'
12 | ]
13 |
14 | gcc_syntax = cxx.get_argument_syntax() == 'gcc'
15 |
16 | if gcc_syntax
17 | vapoursynth_dep = dependency('vapoursynth').partial_dependency(compile_args: true, includes: true)
18 | fftw3f_dep = dependency('fftw3f')
19 | install_dir = vapoursynth_dep.get_variable(pkgconfig: 'libdir') / 'vapoursynth'
20 | else
21 | vapoursynth_dep = []
22 | fftw3f_dep = dependency('fftwf')
23 | install_dir = get_option('libdir') / 'vapoursynth'
24 | endif
25 |
26 | deps = [vapoursynth_dep, fftw3f_dep]
27 |
28 | test_fftwf_threads = '''
29 | #include
30 | int main() {
31 | fftwf_init_threads();
32 | return 0;
33 | }
34 | '''
35 | if not cxx.links(test_fftwf_threads, dependencies: fftw3f_dep)
36 | deps += cxx.find_library('fftw3f_threads')
37 | endif
38 |
39 | libs = []
40 |
41 | if host_machine.cpu_family().startswith('x86')
42 | add_project_arguments('-DDFTTEST_X86', language: 'cpp')
43 | if gcc_syntax
44 | add_project_arguments('-fno-math-errno', '-fno-trapping-math', '-mfpmath=sse', '-msse2', language: 'cpp')
45 | endif
46 |
47 | sources += [
48 | 'DFTTest/DFTTest_SSE2.cpp',
49 | 'DFTTest/VCL2/instrset.h',
50 | 'DFTTest/VCL2/instrset_detect.cpp',
51 | 'DFTTest/VCL2/vector_convert.h',
52 | 'DFTTest/VCL2/vectorclass.h',
53 | 'DFTTest/VCL2/vectorf128.h',
54 | 'DFTTest/VCL2/vectorf256.h',
55 | 'DFTTest/VCL2/vectorf256e.h',
56 | 'DFTTest/VCL2/vectorf512.h',
57 | 'DFTTest/VCL2/vectorf512e.h',
58 | 'DFTTest/VCL2/vectori128.h',
59 | 'DFTTest/VCL2/vectori256.h',
60 | 'DFTTest/VCL2/vectori256e.h',
61 | 'DFTTest/VCL2/vectori512.h',
62 | 'DFTTest/VCL2/vectori512e.h',
63 | 'DFTTest/VCL2/vectori512s.h',
64 | 'DFTTest/VCL2/vectori512se.h',
65 | 'DFTTest/VCL2/vectormath_common.h',
66 | 'DFTTest/VCL2/vectormath_exp.h',
67 | 'DFTTest/VCL2/vectormath_hyp.h',
68 | 'DFTTest/VCL2/vectormath_lib.h',
69 | 'DFTTest/VCL2/vectormath_trig.h'
70 | ]
71 |
72 | libs += static_library('avx2', 'DFTTest/DFTTest_AVX2.cpp',
73 | dependencies: deps,
74 | cpp_args: gcc_syntax ? ['-mavx2', '-mfma'] : '/arch:AVX2',
75 | gnu_symbol_visibility: 'hidden'
76 | )
77 |
78 | libs += static_library('avx512', 'DFTTest/DFTTest_AVX512.cpp',
79 | dependencies: deps,
80 | cpp_args: gcc_syntax ? ['-mavx512f', '-mavx512vl', '-mavx512bw', '-mavx512dq', '-mfma'] : '/arch:AVX512',
81 | gnu_symbol_visibility: 'hidden'
82 | )
83 | endif
84 |
85 | shared_module('dfttest', sources,
86 | dependencies: deps,
87 | link_with: libs,
88 | install: true,
89 | install_dir: install_dir,
90 | gnu_symbol_visibility: 'hidden'
91 | )
92 |
--------------------------------------------------------------------------------
/DFTTest/VCL2/vectorclass.h:
--------------------------------------------------------------------------------
1 | /**************************** vectorclass.h ********************************
2 | * Author: Agner Fog
3 | * Date created: 2012-05-30
4 | * Last modified: 2020-04-11
5 | * Version: 2.01.02
6 | * Project: vector class library
7 | * Home: https://github.com/vectorclass
8 | * Description:
9 | * Header file defining vector classes as interface to intrinsic functions
10 | * in x86 and x86-64 microprocessors with SSE2 and later instruction sets.
11 | *
12 | * Instructions:
13 | * Use Gnu, Clang, Intel or Microsoft C++ compiler. Compile for the desired
14 | * instruction set, which must be at least SSE2. Specify the supported
15 | * instruction set by a command line define, e.g. __SSE4_1__ if the
16 | * compiler does not automatically do so.
17 | * For detailed instructions, see vcl_manual.pdf
18 | *
19 | * Each vector object is represented internally in the CPU as a vector
20 | * register with 128, 256 or 512 bits.
21 | *
22 | * This header file includes the appropriate header files depending on the
23 | * selected instruction set.
24 | *
25 | * (c) Copyright 2012-2020 Agner Fog.
26 | * Apache License version 2.0 or later.
27 | ******************************************************************************/
28 | #ifndef VECTORCLASS_H
29 | #define VECTORCLASS_H 20102
30 |
31 | // Maximum vector size, bits. Allowed values are 128, 256, 512
32 | #ifndef MAX_VECTOR_SIZE
33 | #define MAX_VECTOR_SIZE 512
34 | #endif
35 |
36 | // Determine instruction set, and define platform-dependent functions
37 | #include "instrset.h" // Select supported instruction set
38 |
39 | #if INSTRSET < 2 // instruction set SSE2 is the minimum
40 | #error Please compile for the SSE2 instruction set or higher
41 | #else
42 |
43 | // Select appropriate .h files depending on instruction set
44 | #include "vectori128.h" // 128-bit integer vectors
45 | #include "vectorf128.h" // 128-bit floating point vectors
46 |
47 | #if MAX_VECTOR_SIZE >= 256
48 | #if INSTRSET >= 8
49 | #include "vectori256.h" // 256-bit integer vectors, requires AVX2 instruction set
50 | #else
51 | #include "vectori256e.h" // 256-bit integer vectors, emulated
52 | #endif // INSTRSET >= 8
53 | #if INSTRSET >= 7
54 | #include "vectorf256.h" // 256-bit floating point vectors, requires AVX instruction set
55 | #else
56 | #include "vectorf256e.h" // 256-bit floating point vectors, emulated
57 | #endif // INSTRSET >= 7
58 | #endif // MAX_VECTOR_SIZE >= 256
59 |
60 | #if MAX_VECTOR_SIZE >= 512
61 | #if INSTRSET >= 9
62 | #include "vectori512.h" // 512-bit vectors of 32 and 64 bit integers, requires AVX512F instruction set
63 | #include "vectorf512.h" // 512-bit floating point vectors, requires AVX512F instruction set
64 | #else
65 | #include "vectori512e.h" // 512-bit integer vectors, emulated
66 | #include "vectorf512e.h" // 512-bit floating point vectors, emulated
67 | #endif // INSTRSET >= 9
68 | #if INSTRSET >= 10
69 | #include "vectori512s.h" // 512-bit vectors of 8 and 16 bit integers, requires AVX512BW instruction set
70 | #else
71 | #include "vectori512se.h" // 512-bit vectors of 8 and 16 bit integers, emulated
72 | #endif
73 | #endif // MAX_VECTOR_SIZE >= 512
74 |
75 | #include "vector_convert.h" // conversion between different vector sizes
76 |
77 | #endif // INSTRSET >= 2
78 |
79 |
80 | #else // VECTORCLASS_H
81 |
82 | #if VECTORCLASS_H < 20000
83 | #error Mixed versions of vector class library
84 | #endif
85 |
86 | #endif // VECTORCLASS_H
87 |
--------------------------------------------------------------------------------
/DFTTest/DFTTest.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Release
6 | x64
7 |
8 |
9 |
10 | 16.0
11 | {23B9F54B-763D-491C-A201-657918CD1377}
12 | Win32Proj
13 | DFTTest
14 | 10.0
15 |
16 |
17 |
18 | DynamicLibrary
19 | false
20 | v142
21 | true
22 | Unicode
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 | C:\Program Files\VapourSynth\sdk\include\vapoursynth;$(IncludePath)
35 |
36 |
37 |
38 | DFTTEST_X86;_CRT_SECURE_NO_WARNINGS;NDEBUG;%(PreprocessorDefinitions)
39 | Level3
40 | true
41 | false
42 | false
43 | true
44 | stdcpp17
45 |
46 |
47 | Windows
48 | true
49 | true
50 | libfftw3f-3.lib;%(AdditionalDependencies)
51 |
52 |
53 |
54 |
55 |
56 | AdvancedVectorExtensions2
57 |
58 |
59 | AdvancedVectorExtensions512
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
--------------------------------------------------------------------------------
/DFTTest/VCL2/instrset_detect.cpp:
--------------------------------------------------------------------------------
1 | /************************** instrset_detect.cpp ****************************
2 | * Author: Agner Fog
3 | * Date created: 2012-05-30
4 | * Last modified: 2019-08-01
5 | * Version: 2.00.00
6 | * Project: vector class library
7 | * Description:
8 | * Functions for checking which instruction sets are supported.
9 | *
10 | * (c) Copyright 2012-2019 Agner Fog.
11 | * Apache License version 2.0 or later.
12 | ******************************************************************************/
13 |
14 | #include "instrset.h"
15 |
16 | #ifdef VCL_NAMESPACE
17 | namespace VCL_NAMESPACE {
18 | #endif
19 |
20 |
21 | // Define interface to xgetbv instruction
22 | static inline uint64_t xgetbv (int ctr) {
23 | #if (defined (_MSC_FULL_VER) && _MSC_FULL_VER >= 160040000) || (defined (__INTEL_COMPILER) && __INTEL_COMPILER >= 1200)
24 | // Microsoft or Intel compiler supporting _xgetbv intrinsic
25 |
26 | return uint64_t(_xgetbv(ctr)); // intrinsic function for XGETBV
27 |
28 | #elif defined(__GNUC__) || defined (__clang__) // use inline assembly, Gnu/AT&T syntax
29 |
30 | uint32_t a, d;
31 | __asm("xgetbv" : "=a"(a),"=d"(d) : "c"(ctr) : );
32 | return a | (uint64_t(d) << 32);
33 |
34 | #else // #elif defined (_WIN32) // other compiler. try inline assembly with masm/intel/MS syntax
35 | uint32_t a, d;
36 | __asm {
37 | mov ecx, ctr
38 | _emit 0x0f
39 | _emit 0x01
40 | _emit 0xd0 ; // xgetbv
41 | mov a, eax
42 | mov d, edx
43 | }
44 | return a | (uint64_t(d) << 32);
45 |
46 | #endif
47 | }
48 |
49 | /* find supported instruction set
50 | return value:
51 | 0 = 80386 instruction set
52 | 1 or above = SSE (XMM) supported by CPU (not testing for OS support)
53 | 2 or above = SSE2
54 | 3 or above = SSE3
55 | 4 or above = Supplementary SSE3 (SSSE3)
56 | 5 or above = SSE4.1
57 | 6 or above = SSE4.2
58 | 7 or above = AVX supported by CPU and operating system
59 | 8 or above = AVX2
60 | 9 or above = AVX512F
61 | 10 or above = AVX512VL, AVX512BW, AVX512DQ
62 | */
63 | int instrset_detect(void) {
64 |
65 | static int iset = -1; // remember value for next call
66 | if (iset >= 0) {
67 | return iset; // called before
68 | }
69 | iset = 0; // default value
70 | int abcd[4] = {0,0,0,0}; // cpuid results
71 | cpuid(abcd, 0); // call cpuid function 0
72 | if (abcd[0] == 0) return iset; // no further cpuid function supported
73 | cpuid(abcd, 1); // call cpuid function 1 for feature flags
74 | if ((abcd[3] & (1 << 0)) == 0) return iset; // no floating point
75 | if ((abcd[3] & (1 << 23)) == 0) return iset; // no MMX
76 | if ((abcd[3] & (1 << 15)) == 0) return iset; // no conditional move
77 | if ((abcd[3] & (1 << 24)) == 0) return iset; // no FXSAVE
78 | if ((abcd[3] & (1 << 25)) == 0) return iset; // no SSE
79 | iset = 1; // 1: SSE supported
80 | if ((abcd[3] & (1 << 26)) == 0) return iset; // no SSE2
81 | iset = 2; // 2: SSE2 supported
82 | if ((abcd[2] & (1 << 0)) == 0) return iset; // no SSE3
83 | iset = 3; // 3: SSE3 supported
84 | if ((abcd[2] & (1 << 9)) == 0) return iset; // no SSSE3
85 | iset = 4; // 4: SSSE3 supported
86 | if ((abcd[2] & (1 << 19)) == 0) return iset; // no SSE4.1
87 | iset = 5; // 5: SSE4.1 supported
88 | if ((abcd[2] & (1 << 23)) == 0) return iset; // no POPCNT
89 | if ((abcd[2] & (1 << 20)) == 0) return iset; // no SSE4.2
90 | iset = 6; // 6: SSE4.2 supported
91 | if ((abcd[2] & (1 << 27)) == 0) return iset; // no OSXSAVE
92 | if ((xgetbv(0) & 6) != 6) return iset; // AVX not enabled in O.S.
93 | if ((abcd[2] & (1 << 28)) == 0) return iset; // no AVX
94 | iset = 7; // 7: AVX supported
95 | cpuid(abcd, 7); // call cpuid leaf 7 for feature flags
96 | if ((abcd[1] & (1 << 5)) == 0) return iset; // no AVX2
97 | iset = 8;
98 | if ((abcd[1] & (1 << 16)) == 0) return iset; // no AVX512
99 | cpuid(abcd, 0xD); // call cpuid leaf 0xD for feature flags
100 | if ((abcd[0] & 0x60) != 0x60) return iset; // no AVX512
101 | iset = 9;
102 | cpuid(abcd, 7); // call cpuid leaf 7 for feature flags
103 | if ((abcd[1] & (1 << 31)) == 0) return iset; // no AVX512VL
104 | if ((abcd[1] & 0x40020000) != 0x40020000) return iset; // no AVX512BW, AVX512DQ
105 | iset = 10;
106 | return iset;
107 | }
108 |
109 | // detect if CPU supports the FMA3 instruction set
110 | bool hasFMA3(void) {
111 | if (instrset_detect() < 7) return false; // must have AVX
112 | int abcd[4]; // cpuid results
113 | cpuid(abcd, 1); // call cpuid function 1
114 | return ((abcd[2] & (1 << 12)) != 0); // ecx bit 12 indicates FMA3
115 | }
116 |
117 | // detect if CPU supports the FMA4 instruction set
118 | bool hasFMA4(void) {
119 | if (instrset_detect() < 7) return false; // must have AVX
120 | int abcd[4]; // cpuid results
121 | cpuid(abcd, 0x80000001); // call cpuid function 0x80000001
122 | return ((abcd[2] & (1 << 16)) != 0); // ecx bit 16 indicates FMA4
123 | }
124 |
125 | // detect if CPU supports the XOP instruction set
126 | bool hasXOP(void) {
127 | if (instrset_detect() < 7) return false; // must have AVX
128 | int abcd[4]; // cpuid results
129 | cpuid(abcd, 0x80000001); // call cpuid function 0x80000001
130 | return ((abcd[2] & (1 << 11)) != 0); // ecx bit 11 indicates XOP
131 | }
132 |
133 | // detect if CPU supports the F16C instruction set
134 | bool hasF16C(void) {
135 | if (instrset_detect() < 7) return false; // must have AVX
136 | int abcd[4]; // cpuid results
137 | cpuid(abcd, 1); // call cpuid function 1
138 | return ((abcd[2] & (1 << 29)) != 0); // ecx bit 29 indicates F16C
139 | }
140 |
141 | // detect if CPU supports the AVX512ER instruction set
142 | bool hasAVX512ER(void) {
143 | if (instrset_detect() < 9) return false; // must have AVX512F
144 | int abcd[4]; // cpuid results
145 | cpuid(abcd, 7); // call cpuid function 7
146 | return ((abcd[1] & (1 << 27)) != 0); // ebx bit 27 indicates AVX512ER
147 | }
148 |
149 | // detect if CPU supports the AVX512VBMI instruction set
150 | bool hasAVX512VBMI(void) {
151 | if (instrset_detect() < 10) return false; // must have AVX512BW
152 | int abcd[4]; // cpuid results
153 | cpuid(abcd, 7); // call cpuid function 7
154 | return ((abcd[2] & (1 << 1)) != 0); // ecx bit 1 indicates AVX512VBMI
155 | }
156 |
157 | // detect if CPU supports the AVX512VBMI2 instruction set
158 | bool hasAVX512VBMI2(void) {
159 | if (instrset_detect() < 10) return false; // must have AVX512BW
160 | int abcd[4]; // cpuid results
161 | cpuid(abcd, 7); // call cpuid function 7
162 | return ((abcd[2] & (1 << 6)) != 0); // ecx bit 6 indicates AVX512VBMI2
163 | }
164 |
165 | #ifdef VCL_NAMESPACE
166 | }
167 | #endif
168 |
--------------------------------------------------------------------------------
/DFTTest/VCL2/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 |
179 | Copyright 2012-2019 Agner Fog.
180 |
181 | Licensed under the Apache License, Version 2.0 (the "License");
182 | you may not use this file except in compliance with the License.
183 | You may obtain a copy of the License at
184 |
185 | http://www.apache.org/licenses/LICENSE-2.0
186 |
187 | Unless required by applicable law or agreed to in writing, software
188 | distributed under the License is distributed on an "AS IS" BASIS,
189 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
190 | See the License for the specific language governing permissions and
191 | limitations under the License.
192 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Description
2 | ===========
3 |
4 | 2D/3D frequency domain denoiser.
5 |
6 | Requires libfftw3f-3.dll to be in the search path. http://www.fftw.org/install/windows.html
7 |
8 | Ported from AviSynth plugin http://bengal.missouri.edu/~kes25c/
9 |
10 |
11 | Usage
12 | =====
13 |
14 | dfttest.DFTTest(clip clip[, int ftype=0, float sigma=8.0, float sigma2=8.0, float pmin=0.0, float pmax=500.0, int sbsize=16, int smode=1, int sosize=12, int tbsize=3, int tmode=0, int tosize=0, int swin=0, int twin=7, float sbeta=2.5, float tbeta=2.5, bint zmean=True, float f0beta=1.0, int[] nlocation=None, float alpha, float[] slocation=None, float[] ssx=None, float[] ssy=None, float[] sst=None, int ssystem=0, int[] planes=[0, 1, 2], int opt=0])
15 |
16 | ```
17 | clip -
18 |
19 | Clip to process. Any planar format with either integer sample type of 8-16 bit depth or float sample type of 32 bit depth is supported.
20 |
21 |
22 | ftype -
23 |
24 | Controls the filter type. Possible settings are:
25 |
26 | 0 - generalized wiener filter
27 |
28 | mult = max((psd - sigma) / psd, 0) ^ f0beta
29 |
30 | 1 - hard threshold
31 |
32 | mult = psd < sigma ? 0.0 : 1.0
33 |
34 | 2 - multiplier
35 |
36 | mult = sigma
37 |
38 | 3 - multiplier switched based on psd value
39 |
40 | mult = (psd >= pmin && psd <= pmax) ? sigma : sigma2
41 |
42 | 4 - multiplier modified based on psd value and range
43 |
44 | mult = sigma * sqrt((psd * pmax) / ((psd + pmin) * (psd + pmax)))
45 |
46 | The real and imaginary parts of each complex dft coefficient are multiplied
47 | by the corresponding 'mult' value.
48 |
49 | ** psd = magnitude squared = real*real + imag*imag
50 |
51 |
52 | sigma,sigma2 -
53 |
54 | Value of sigma and sigma2 (used as described in ftype parameter description).
55 | If using the slocation parameter then the sigma parameter is ignored.
56 |
57 |
58 | pmin,pmax -
59 |
60 | Used as described in the ftype parameter description.
61 |
62 |
63 | sbsize -
64 |
65 | Sets the length of the sides of the spatial window. Must be 1 or greater.
66 | Must be odd if using smode=0.
67 |
68 |
69 | smode -
70 |
71 | Sets the mode for spatial operation. There are two possible settings:
72 |
73 | 0 - process every pixel independently... center the spatial window
74 | on the current pixel, filter, move to the next pixel, repeat.
75 | Spatial overlapping 'sosize' not used.
76 |
77 | 1 - process the spatial dimension in blocks of sbsize. Spatial
78 | overlapping 'sosize' used.
79 |
80 |
81 | sosize -
82 |
83 | Sets the spatial overlap amount. Must be in the range 0 to sbsize-1 (inclusive).
84 | If sosize is greater than sbsize>>1, then sbsize%(sbsize-sosize) must equal 0.
85 | In other words, overlap greater than 50% requires that sbsize-sosize be a divisor
86 | of sbsize.
87 |
88 |
89 | tbsize -
90 |
91 | Sets the length of the temporal dimension (i.e. number of frames). Must be at
92 | least 1. Must be odd if using tmode=0.
93 |
94 |
95 | tmode -
96 |
97 | Sets the mode for temporal operation. There are two possible settings:
98 |
99 | 0 - process every frame independently... center the temporal window
100 | on the current frame, filter, move to the next frame, repeat.
101 | Temporal overlapping 'tosize' not used.
102 |
103 | 1 - process the temporal dimension in blocks of tbsize. Temporal
104 | overlapping 'tosize' used.
105 |
106 | Currently only tmode=0 is implemented.
107 |
108 |
109 | tosize -
110 |
111 | Sets the temporal overlap amount. Must be in the range 0 to tbsize-1 (inclusive).
112 | If tosize is greater than tbsize>>1, then tbsize%(tbsize-tosize) must equal 0.
113 | In other words, overlap greater than 50% requires that tbsize-tosize be a divisor
114 | of tbsize.
115 |
116 |
117 | swin,twin -
118 |
119 | Sets the type of analysis/synthesis window to be used for spatial (swin) and
120 | temporal (twin) processing. Possible settings:
121 |
122 | 0: hanning
123 | 1: hamming
124 | 2: blackman
125 | 3: 4 term blackman-harris
126 | 4: kaiser-bessel
127 | 5: 7 term blackman-harris
128 | 6: flat top
129 | 7: rectangular
130 | 8: Bartlett
131 | 9: Bartlett-Hann
132 | 10: Nuttall
133 | 11: Blackman-Nuttall
134 |
135 |
136 | sbeta,tbeta -
137 |
138 | Sets the beta value for kaiser-bessel window type. sbeta goes with swin,
139 | tbeta goes with twin. Not used unless the corresponding window value
140 | is set to 4.
141 |
142 |
143 | zmean -
144 |
145 | Controls whether the window mean is subtracted out (zero'd) prior to
146 | filtering in the frequency domain.
147 |
148 |
149 | f0beta -
150 |
151 | Power term in ftype=0. The ftype=0 formula is:
152 |
153 | max((psd-sigma)/psd,0)^f0beta
154 |
155 | For f0beta=1, this equation corresponds to the wiener filter with
156 | spectral subtraction as the estimate of the signal power. For f0beta=0.5,
157 | the equation corresponds to spectral subtraction. The 1.0 and 0.5 cases
158 | are separated from the general routine in the code to allow for fast
159 | operation. Other values will result in the general routine being used,
160 | which has to perform a pow() computation, and is therefore much slower.
161 |
162 |
163 | nlocation -
164 |
165 | When ftype<2, nlocation can be used to specify block locations in the video
166 | from which dfttest will estimate the noise power spectrum (sigma) to
167 | be used for filtering.
168 |
169 | When the noise to be removed is not white (i.e. doesn't have a flat power
170 | spectrum), specifying only a single sigma value is not adequate.
171 |
172 | The nlocation should list locations in the video that consist of noise on
173 | a flat background, separated by a space. The line syntax is:
174 |
175 | frame_number,plane,ypos,xpos e.g. 0,0,20,20
176 |
177 | plane: (0=Y,1=U,2=V)
178 | ypos/xpos: the upper left position of the block
179 | (0,0 is the upper left of the frame)
180 |
181 | dfttest positions a window (of the type defined by sbsize/tbsize/swin/twin)
182 | at the specified location, and estimates the power using fft magnitude^2.
183 | When tbsize>1, frame_number specifies the first frame of the temporal block.
184 | Make sure that the window size is large enough to capture the full noise
185 | pattern.
186 |
187 | If you list multiple blocks, then the estimates obtained at each block are
188 | averaged to form the final estimate. Having more block locations to use
189 | lowers the variance of the estimate. The more block locations you specify the
190 | closer the true noise spectrum will be estimated, resulting in better
191 | denoising. When listing multiple block locations, it is best/preferred if the
192 | locations do not overlap.
193 |
194 | An example:
195 |
196 | nlocation=[35,0,45,68, 28,0,23,87]
197 |
198 |
199 | alpha -
200 |
201 | Typically, subtracting out the noise power spectrum is not adequate becase
202 | it is only the average. In any one block the noise spectrum has the potential
203 | to exceed the average in a frequency bin. Therefore, one typically over
204 | subtracts based on some multiple of the noise spectrum (usually in the range
205 | of 3-8). The default used in dfttest is 5 if ftype=0 and 7 if ftype=1.
206 | Has no effect when nlocation is not used.
207 |
208 |
209 | slocation/ssx/ssy/sst -
210 |
211 | Used to specify functions of sigma based on frequency. If you want sigma to vary
212 | based on frequency, then use 'slocation' instead of the 'sigma' parameter. slocation
213 | allows you to enter values of sigma for different normalized [0.0,1.0] frequency
214 | locations. Values for locations between the ones you explicitly specify are computed
215 | via linear interpolation. The frequency range, which is dependent on sbsize/tbsize,
216 | is normalized to [0.0,1.0] with 0.0 being the lowest frequency and 1.0 being the
217 | highest frequency. You MUST specify sigma values for those end point locations
218 | (0.0 and 1.0)! You can specify as many other locations as you wish, and they don't
219 | have to be in any particular order. Each frequency/sigma pair is given as "f.f,s.s".
220 |
221 | For example, if you want a linear ramp of sigma from 1.0 for the lowest frequency
222 | to 10.0 for the highest frequency use:
223 |
224 | slocation = [0.0,1.0, 1.0,10.0]
225 |
226 | "0.0,1.0" => this means sigma=1.0 at frequency 0.0
227 |
228 | "1.0,10.0" => this means sigma=10.0 at frequency 1.0
229 |
230 | Sigma values for frequencies between 0.0 and 1.0 will be computed via
231 | linear interpolation.
232 |
233 | Or if you want a band-stop filter that passes low and high frequencies (filters
234 | middle frequencies) use something like:
235 |
236 | slocation = [0.0,0.0, 0.15,10.0, 0.85,10.0, 1.0,0.0]
237 |
238 |
239 | ---------------- ssx/ssy/sst explanation -------------------------------
240 |
241 | slocation breaks the 1D (sbsize=1), 2D (for tbsize=1), or 3D (for sbsize>1 and tbsize>1)
242 | frequency spectrum into chunks by normalizing each dimension to [0.0,1.0]... i.e. the
243 | frequency range [0.0,0.25] is a cube covering the first 1/4 of each dimension. This works
244 | fine if you want to treat all dimensions the same in terms of how sigma should vary.
245 | However, if you wanted to ramp sigma based only on temporal frequency or horizontal
246 | frequency, this is too limited. This is where ssx/ssy/sst come in!
247 |
248 | ssx/ssy/sst allow you to specify sigma as a function of horizontal (ssx), vertical (ssy),
249 | and temporal (sst) frequency only. The syntax is exactly the same as that of slocation. To
250 | get the final sigma value for a frequency location, the three separate values (one for
251 | each dimension) are computed and then multiplied together. As with slocation the sigma values
252 | are first raised to the 1/#_dimensions power before performing linear interpolation and
253 | multiplying. If you don't specify all three dimensions, then a flat function equal to the
254 | 'sigma' parameter is used for the missing dimensions. For dimensions of size one (the
255 | spatial dimenions if sbsize=1 or the temporal dimension for tbsize=1) the corresponding
256 | value is ignored.
257 |
258 | For example:
259 |
260 | ssx=[0.0,1.0, 1.0,10.0],ssy=[0.0,1.0, 1.0,10.0],sst=[0.0,1.0, 1.0,10.0]
261 |
262 | will give the same result as
263 |
264 | slocation=[0.0,1.0, 1.0,10.0]
265 |
266 | Or if you want to ramp sigma based on temporal frequency:
267 |
268 | sigma=10.0,sst=[0.0,1.0, 1.0,10.0]
269 |
270 | This will use 10.0 for the horizontal/vertical dimensions, and ramp
271 | sigma from 1.0 to 10.0 in the temporal dimension.
272 |
273 | If 'slocation' is specified, it takes precedence over ssx/ssy/sst.
274 |
275 |
276 | ssystem -
277 |
278 | There are two methods for computing sigma values for a given frequency bin based on
279 | slocation. ssystem=0 computes the normalized frequency location of each dimension
280 | (horizontal,vertical,temporal), interpolates sigma for each of those dimensions,
281 | and then multiples the individual sigmas to obtain the final sigma value. So that
282 | everything scales correctly, all sigma values entered in slocation are first raised to
283 | the 1/#_dimensions power before performing linear interpolation and multiplying.
284 | ssystem=1 (based on fft3dfilter's system) works by computing a single location
285 | from the seperate dimension locations (x,y,z) as:
286 |
287 | new = sqrt((x*x+y*y+z*z)/3.0)
288 |
289 | sigma is then interpolated to this location. By default the first system is used.
290 | Has no effect when slocation is not used.
291 |
292 |
293 | planes -
294 |
295 | Sets which planes will be processed. Any unprocessed planes will be simply copied.
296 |
297 |
298 | opt -
299 |
300 | Sets which cpu optimizations to use.
301 |
302 | 0 - auto detect
303 | 1 - use c
304 | 2 - use sse2
305 | 3 - use avx2
306 | 3 - use avx512
307 | ```
308 |
309 |
310 | Compilation
311 | ===========
312 |
313 | Requires `fftw3f`.
314 |
315 | ```
316 | meson build
317 | ninja -C build
318 | ninja -C build install
319 | ```
320 |
--------------------------------------------------------------------------------
/DFTTest/VCL2/vectormath_common.h:
--------------------------------------------------------------------------------
1 | /*************************** vectormath_common.h ****************************
2 | * Author: Agner Fog
3 | * Date created: 2014-04-18
4 | * Last modified: 2020-06-08
5 | * Version: 2.01.03
6 | * Project: vector classes
7 | * Description:
8 | * Header file containing common code for inline version of mathematical functions.
9 | *
10 | * For detailed instructions, see VectorClass.pdf
11 | *
12 | * (c) Copyright 2014-2020 Agner Fog.
13 | * Apache License version 2.0 or later.
14 | ******************************************************************************/
15 |
16 | #ifndef VECTORMATH_COMMON_H
17 | #define VECTORMATH_COMMON_H 2
18 |
19 | #ifdef VECTORMATH_LIB_H
20 | #error conflicting header files. More than one implementation of mathematical functions included
21 | #endif
22 |
23 | #include
24 |
25 | #ifndef VECTORCLASS_H
26 | #include "vectorclass.h"
27 | #endif
28 |
29 | #if VECTORCLASS_H < 20000
30 | #error Incompatible versions of vector class library mixed
31 | #endif
32 |
33 |
34 | /******************************************************************************
35 | Define NAN payload values
36 | ******************************************************************************/
37 | #define NAN_LOG 0x101 // logarithm for x<0
38 | #define NAN_POW 0x102 // negative number raised to non-integer power
39 | #define NAN_HYP 0x104 // acosh for x<1 and atanh for abs(x)>1
40 |
41 |
42 | /******************************************************************************
43 | Define mathematical constants
44 | ******************************************************************************/
45 | #define VM_PI 3.14159265358979323846 // pi
46 | #define VM_PI_2 1.57079632679489661923 // pi / 2
47 | #define VM_PI_4 0.785398163397448309616 // pi / 4
48 | #define VM_SQRT2 1.41421356237309504880 // sqrt(2)
49 | #define VM_LOG2E 1.44269504088896340736 // 1/log(2)
50 | #define VM_LOG10E 0.434294481903251827651 // 1/log(10)
51 | #define VM_LOG210 3.321928094887362347808 // log2(10)
52 | #define VM_LN2 0.693147180559945309417 // log(2)
53 | #define VM_LN10 2.30258509299404568402 // log(10)
54 | #define VM_SMALLEST_NORMAL 2.2250738585072014E-308 // smallest normal number, double
55 | #define VM_SMALLEST_NORMALF 1.17549435E-38f // smallest normal number, float
56 |
57 |
58 | #ifdef VCL_NAMESPACE
59 | namespace VCL_NAMESPACE {
60 | #endif
61 |
62 | /******************************************************************************
63 | templates for producing infinite and nan in desired vector type
64 | ******************************************************************************/
65 | template
66 | static inline VTYPE infinite_vec();
67 |
68 | template <>
69 | inline Vec2d infinite_vec() {
70 | return infinite2d();
71 | }
72 |
73 | template <>
74 | inline Vec4f infinite_vec() {
75 | return infinite4f();
76 | }
77 |
78 | #if MAX_VECTOR_SIZE >= 256
79 |
80 | template <>
81 | inline Vec4d infinite_vec() {
82 | return infinite4d();
83 | }
84 |
85 | template <>
86 | inline Vec8f infinite_vec() {
87 | return infinite8f();
88 | }
89 |
90 | #endif // MAX_VECTOR_SIZE >= 256
91 |
92 | #if MAX_VECTOR_SIZE >= 512
93 |
94 | template <>
95 | inline Vec8d infinite_vec() {
96 | return infinite8d();
97 | }
98 |
99 | template <>
100 | inline Vec16f infinite_vec() {
101 | return infinite16f();
102 | }
103 |
104 | #endif // MAX_VECTOR_SIZE >= 512
105 |
106 |
107 |
108 | /******************************************************************************
109 | * Detect NAN codes
110 | *
111 | * These functions return the code hidden in a NAN. The sign bit is ignored
112 | ******************************************************************************/
113 |
114 | static inline Vec4ui nan_code(Vec4f const x) {
115 | Vec4ui a = Vec4ui(reinterpret_i(x));
116 | Vec4ui const n = 0x007FFFFF;
117 | return select(Vec4ib(is_nan(x)), a & n, 0);
118 | }
119 |
120 | // This function returns the code hidden in a NAN. The sign bit is ignored
121 | static inline Vec2uq nan_code(Vec2d const x) {
122 | Vec2uq a = Vec2uq(reinterpret_i(x));
123 | return select(Vec2qb(is_nan(x)), a << 12 >> (12+29), 0);
124 | }
125 |
126 | #if MAX_VECTOR_SIZE >= 256
127 |
128 | // This function returns the code hidden in a NAN. The sign bit is ignored
129 | static inline Vec8ui nan_code(Vec8f const x) {
130 | Vec8ui a = Vec8ui(reinterpret_i(x));
131 | Vec8ui const n = 0x007FFFFF;
132 | return select(Vec8ib(is_nan(x)), a & n, 0);
133 | }
134 |
135 | // This function returns the code hidden in a NAN. The sign bit is ignored
136 | static inline Vec4uq nan_code(Vec4d const x) {
137 | Vec4uq a = Vec4uq(reinterpret_i(x));
138 | return select(Vec4qb(is_nan(x)), a << 12 >> (12+29), 0);
139 | }
140 |
141 | #endif // MAX_VECTOR_SIZE >= 256
142 | #if MAX_VECTOR_SIZE >= 512
143 |
144 | // This function returns the code hidden in a NAN. The sign bit is ignored
145 | static inline Vec16ui nan_code(Vec16f const x) {
146 | Vec16ui a = Vec16ui(reinterpret_i(x));
147 | Vec16ui const n = 0x007FFFFF;
148 | return select(Vec16ib(is_nan(x)), a & n, 0);
149 | }
150 |
151 | // This function returns the code hidden in a NAN. The sign bit is ignored
152 | static inline Vec8uq nan_code(Vec8d const x) {
153 | Vec8uq a = Vec8uq(reinterpret_i(x));
154 | return select(Vec8qb(is_nan(x)), a << 12 >> (12+29), 0);
155 | }
156 |
157 | #endif // MAX_VECTOR_SIZE >= 512
158 |
159 |
160 | /******************************************************************************
161 | templates for polynomials
162 | Using Estrin's scheme to make shorter dependency chains and use FMA, starting
163 | longest dependency chains first.
164 | ******************************************************************************/
165 |
166 | // template
167 | template
168 | static inline VTYPE polynomial_2(VTYPE const x, CTYPE c0, CTYPE c1, CTYPE c2) {
169 | // calculates polynomial c2*x^2 + c1*x + c0
170 | // VTYPE may be a vector type, CTYPE is a scalar type
171 | VTYPE x2 = x * x;
172 | //return = x2 * c2 + (x * c1 + c0);
173 | return mul_add(x2, c2, mul_add(x, c1, c0));
174 | }
175 |
176 | template
177 | static inline VTYPE polynomial_3(VTYPE const x, CTYPE c0, CTYPE c1, CTYPE c2, CTYPE c3) {
178 | // calculates polynomial c3*x^3 + c2*x^2 + c1*x + c0
179 | // VTYPE may be a vector type, CTYPE is a scalar type
180 | VTYPE x2 = x * x;
181 | //return (c2 + c3*x)*x2 + (c1*x + c0);
182 | return mul_add(mul_add(c3, x, c2), x2, mul_add(c1, x, c0));
183 | }
184 |
185 | template
186 | static inline VTYPE polynomial_4(VTYPE const x, CTYPE c0, CTYPE c1, CTYPE c2, CTYPE c3, CTYPE c4) {
187 | // calculates polynomial c4*x^4 + c3*x^3 + c2*x^2 + c1*x + c0
188 | // VTYPE may be a vector type, CTYPE is a scalar type
189 | VTYPE x2 = x * x;
190 | VTYPE x4 = x2 * x2;
191 | //return (c2+c3*x)*x2 + ((c0+c1*x) + c4*x4);
192 | return mul_add(mul_add(c3, x, c2), x2, mul_add(c1, x, c0) + c4*x4);
193 | }
194 |
195 | template
196 | static inline VTYPE polynomial_4n(VTYPE const x, CTYPE c0, CTYPE c1, CTYPE c2, CTYPE c3) {
197 | // calculates polynomial 1*x^4 + c3*x^3 + c2*x^2 + c1*x + c0
198 | // VTYPE may be a vector type, CTYPE is a scalar type
199 | VTYPE x2 = x * x;
200 | VTYPE x4 = x2 * x2;
201 | //return (c2+c3*x)*x2 + ((c0+c1*x) + x4);
202 | return mul_add(mul_add(c3, x, c2), x2, mul_add(c1, x, c0) + x4);
203 | }
204 |
205 | template
206 | static inline VTYPE polynomial_5(VTYPE const x, CTYPE c0, CTYPE c1, CTYPE c2, CTYPE c3, CTYPE c4, CTYPE c5) {
207 | // calculates polynomial c5*x^5 + c4*x^4 + c3*x^3 + c2*x^2 + c1*x + c0
208 | // VTYPE may be a vector type, CTYPE is a scalar type
209 | VTYPE x2 = x * x;
210 | VTYPE x4 = x2 * x2;
211 | //return (c2+c3*x)*x2 + ((c4+c5*x)*x4 + (c0+c1*x));
212 | return mul_add(mul_add(c3, x, c2), x2, mul_add(mul_add(c5, x, c4), x4, mul_add(c1, x, c0)));
213 | }
214 |
215 | template
216 | static inline VTYPE polynomial_5n(VTYPE const x, CTYPE c0, CTYPE c1, CTYPE c2, CTYPE c3, CTYPE c4) {
217 | // calculates polynomial 1*x^5 + c4*x^4 + c3*x^3 + c2*x^2 + c1*x + c0
218 | // VTYPE may be a vector type, CTYPE is a scalar type
219 | VTYPE x2 = x * x;
220 | VTYPE x4 = x2 * x2;
221 | //return (c2+c3*x)*x2 + ((c4+x)*x4 + (c0+c1*x));
222 | return mul_add(mul_add(c3, x, c2), x2, mul_add(c4 + x, x4, mul_add(c1, x, c0)));
223 | }
224 |
225 | template
226 | static inline VTYPE polynomial_6(VTYPE const x, CTYPE c0, CTYPE c1, CTYPE c2, CTYPE c3, CTYPE c4, CTYPE c5, CTYPE c6) {
227 | // calculates polynomial c6*x^6 + c5*x^5 + c4*x^4 + c3*x^3 + c2*x^2 + c1*x + c0
228 | // VTYPE may be a vector type, CTYPE is a scalar type
229 | VTYPE x2 = x * x;
230 | VTYPE x4 = x2 * x2;
231 | //return (c4+c5*x+c6*x2)*x4 + ((c2+c3*x)*x2 + (c0+c1*x));
232 | return mul_add(mul_add(c6, x2, mul_add(c5, x, c4)), x4, mul_add(mul_add(c3, x, c2), x2, mul_add(c1, x, c0)));
233 | }
234 |
235 | template
236 | static inline VTYPE polynomial_6n(VTYPE const x, CTYPE c0, CTYPE c1, CTYPE c2, CTYPE c3, CTYPE c4, CTYPE c5) {
237 | // calculates polynomial 1*x^6 + c5*x^5 + c4*x^4 + c3*x^3 + c2*x^2 + c1*x + c0
238 | // VTYPE may be a vector type, CTYPE is a scalar type
239 | VTYPE x2 = x * x;
240 | VTYPE x4 = x2 * x2;
241 | //return (c4+c5*x+x2)*x4 + ((c2+c3*x)*x2 + (c0+c1*x));
242 | return mul_add(mul_add(c5, x, c4 + x2), x4, mul_add(mul_add(c3, x, c2), x2, mul_add(c1, x, c0)));
243 | }
244 |
245 | template
246 | static inline VTYPE polynomial_7(VTYPE const x, CTYPE c0, CTYPE c1, CTYPE c2, CTYPE c3, CTYPE c4, CTYPE c5, CTYPE c6, CTYPE c7) {
247 | // calculates polynomial c7*x^7 + c6*x^6 + c5*x^5 + c4*x^4 + c3*x^3 + c2*x^2 + c1*x + c0
248 | // VTYPE may be a vector type, CTYPE is a scalar type
249 | VTYPE x2 = x * x;
250 | VTYPE x4 = x2 * x2;
251 | //return ((c6+c7*x)*x2 + (c4+c5*x))*x4 + ((c2+c3*x)*x2 + (c0+c1*x));
252 | return mul_add(mul_add(mul_add(c7, x, c6), x2, mul_add(c5, x, c4)), x4, mul_add(mul_add(c3, x, c2), x2, mul_add(c1, x, c0)));
253 | }
254 |
255 | template
256 | static inline VTYPE polynomial_8(VTYPE const x, CTYPE c0, CTYPE c1, CTYPE c2, CTYPE c3, CTYPE c4, CTYPE c5, CTYPE c6, CTYPE c7, CTYPE c8) {
257 | // calculates polynomial c8*x^8 + c7*x^7 + c6*x^6 + c5*x^5 + c4*x^4 + c3*x^3 + c2*x^2 + c1*x + c0
258 | // VTYPE may be a vector type, CTYPE is a scalar type
259 | VTYPE x2 = x * x;
260 | VTYPE x4 = x2 * x2;
261 | VTYPE x8 = x4 * x4;
262 | //return ((c6+c7*x)*x2 + (c4+c5*x))*x4 + (c8*x8 + (c2+c3*x)*x2 + (c0+c1*x));
263 | return mul_add(mul_add(mul_add(c7, x, c6), x2, mul_add(c5, x, c4)), x4,
264 | mul_add(mul_add(c3, x, c2), x2, mul_add(c1, x, c0) + c8*x8));
265 | }
266 |
267 | template
268 | static inline VTYPE polynomial_9(VTYPE const x, CTYPE c0, CTYPE c1, CTYPE c2, CTYPE c3, CTYPE c4, CTYPE c5, CTYPE c6, CTYPE c7, CTYPE c8, CTYPE c9) {
269 | // calculates polynomial c9*x^9 + c8*x^8 + c7*x^7 + c6*x^6 + c5*x^5 + c4*x^4 + c3*x^3 + c2*x^2 + c1*x + c0
270 | // VTYPE may be a vector type, CTYPE is a scalar type
271 | VTYPE x2 = x * x;
272 | VTYPE x4 = x2 * x2;
273 | VTYPE x8 = x4 * x4;
274 | //return (((c6+c7*x)*x2 + (c4+c5*x))*x4 + (c8+c9*x)*x8) + ((c2+c3*x)*x2 + (c0+c1*x));
275 | return mul_add(mul_add(c9, x, c8), x8, mul_add(
276 | mul_add(mul_add(c7, x, c6), x2, mul_add(c5, x, c4)), x4,
277 | mul_add(mul_add(c3, x, c2), x2, mul_add(c1, x, c0))));
278 | }
279 |
280 | template
281 | static inline VTYPE polynomial_10(VTYPE const x, CTYPE c0, CTYPE c1, CTYPE c2, CTYPE c3, CTYPE c4, CTYPE c5, CTYPE c6, CTYPE c7, CTYPE c8, CTYPE c9, CTYPE c10) {
282 | // calculates polynomial c10*x^10 + c9*x^9 + c8*x^8 + c7*x^7 + c6*x^6 + c5*x^5 + c4*x^4 + c3*x^3 + c2*x^2 + c1*x + c0
283 | // VTYPE may be a vector type, CTYPE is a scalar type
284 | VTYPE x2 = x * x;
285 | VTYPE x4 = x2 * x2;
286 | VTYPE x8 = x4 * x4;
287 | //return (((c6+c7*x)*x2 + (c4+c5*x))*x4 + (c8+c9*x+c10*x2)*x8) + ((c2+c3*x)*x2 + (c0+c1*x));
288 | return mul_add(mul_add(x2, c10, mul_add(c9, x, c8)), x8,
289 | mul_add(mul_add(mul_add(c7, x, c6), x2, mul_add(c5, x, c4)), x4,
290 | mul_add(mul_add(c3, x, c2), x2, mul_add(c1, x, c0))));
291 | }
292 |
293 | template
294 | static inline VTYPE polynomial_13(VTYPE const x, CTYPE c0, CTYPE c1, CTYPE c2, CTYPE c3, CTYPE c4, CTYPE c5, CTYPE c6, CTYPE c7, CTYPE c8, CTYPE c9, CTYPE c10, CTYPE c11, CTYPE c12, CTYPE c13) {
295 | // calculates polynomial c13*x^13 + c12*x^12 + ... + c1*x + c0
296 | // VTYPE may be a vector type, CTYPE is a scalar type
297 | VTYPE x2 = x * x;
298 | VTYPE x4 = x2 * x2;
299 | VTYPE x8 = x4 * x4;
300 | return mul_add(
301 | mul_add(
302 | mul_add(c13, x, c12), x4,
303 | mul_add(mul_add(c11, x, c10), x2, mul_add(c9, x, c8))), x8,
304 | mul_add(
305 | mul_add(mul_add(c7, x, c6), x2, mul_add(c5, x, c4)), x4,
306 | mul_add(mul_add(c3, x, c2), x2, mul_add(c1, x, c0))));
307 | }
308 |
309 |
310 | template
311 | static inline VTYPE polynomial_13m(VTYPE const x, CTYPE c2, CTYPE c3, CTYPE c4, CTYPE c5, CTYPE c6, CTYPE c7, CTYPE c8, CTYPE c9, CTYPE c10, CTYPE c11, CTYPE c12, CTYPE c13) {
312 | // calculates polynomial c13*x^13 + c12*x^12 + ... + x + 0
313 | // VTYPE may be a vector type, CTYPE is a scalar type
314 | VTYPE x2 = x * x;
315 | VTYPE x4 = x2 * x2;
316 | VTYPE x8 = x4 * x4;
317 | // return ((c8+c9*x) + (c10+c11*x)*x2 + (c12+c13*x)*x4)*x8 + (((c6+c7*x)*x2 + (c4+c5*x))*x4 + ((c2+c3*x)*x2 + x));
318 | return mul_add(
319 | mul_add(mul_add(c13, x, c12), x4, mul_add(mul_add(c11, x, c10), x2, mul_add(c9, x, c8))), x8,
320 | mul_add(mul_add(mul_add(c7, x, c6), x2, mul_add(c5, x, c4)), x4, mul_add(mul_add(c3, x, c2), x2, x)));
321 | }
322 |
323 | #ifdef VCL_NAMESPACE
324 | }
325 | #endif
326 |
327 | #endif
328 |
--------------------------------------------------------------------------------
/DFTTest/DFTTest_AVX2.cpp:
--------------------------------------------------------------------------------
1 | #ifdef DFTTEST_X86
2 | #include "DFTTest.h"
3 |
4 | #include "VCL2/vectormath_exp.h"
5 |
6 | template
7 | static inline auto proc0(const pixel_t * _s0, const float * _s1, float * d, const int p0, const int p1, const float srcScale) noexcept {
8 | for (int u = 0; u < p1; u++) {
9 | for (int v = 0; v < p1; v += Vec8f().size()) {
10 | Vec8f s0;
11 |
12 | if constexpr (std::is_same_v)
13 | s0 = to_float(Vec8i().load_8uc(_s0 + v));
14 | else if constexpr (std::is_same_v)
15 | s0 = to_float(Vec8i().load_8us(_s0 + v));
16 | else
17 | s0 = Vec8f().load(_s0 + v);
18 |
19 | const Vec8f s1 = Vec8f().load(_s1 + v);
20 |
21 | if constexpr (std::is_same_v)
22 | (s0 * s1).store(d + v);
23 | else
24 | (s0 * srcScale * s1).store(d + v);
25 | }
26 |
27 | _s0 += p0;
28 | _s1 += p1;
29 | d += p1;
30 | }
31 | }
32 |
33 | static inline auto proc1(const float * _s0, const float * _s1, float * _d, const int p0, const int p1) noexcept {
34 | for (int u = 0; u < p0; u++) {
35 | for (int v = 0; v < p0; v += Vec8f().size()) {
36 | const Vec8f s0 = Vec8f().load(_s0 + v);
37 | const Vec8f s1 = Vec8f().load(_s1 + v);
38 | const Vec8f d = Vec8f().load(_d + v);
39 | mul_add(s0, s1, d).store(_d + v);
40 | }
41 |
42 | _s0 += p0;
43 | _s1 += p0;
44 | _d += p1;
45 | }
46 | }
47 |
48 | static inline auto proc1Partial(const float * _s0, const float * _s1, float * _d, const int p0, const int p1) noexcept {
49 | const int regularPart = p0 & ~(Vec8f().size() - 1);
50 |
51 | for (int u = 0; u < p0; u++) {
52 | int v;
53 |
54 | for (v = 0; v < regularPart; v += Vec8f().size()) {
55 | const Vec8f s0 = Vec8f().load(_s0 + v);
56 | const Vec8f s1 = Vec8f().load(_s1 + v);
57 | const Vec8f d = Vec8f().load(_d + v);
58 | mul_add(s0, s1, d).store(_d + v);
59 | }
60 |
61 | const Vec8f s0 = Vec8f().load(_s0 + v);
62 | const Vec8f s1 = Vec8f().load(_s1 + v);
63 | const Vec8f d = Vec8f().load(_d + v);
64 | mul_add(s0, s1, d).store_partial(p0 - v, _d + v);
65 |
66 | _s0 += p0;
67 | _s1 += p0;
68 | _d += p1;
69 | }
70 | }
71 |
72 | static inline auto removeMean(float * _dftc, const float * _dftgc, const int ccnt, float * _dftc2) noexcept {
73 | const Vec8f gf = _dftc[0] / _dftgc[0];
74 |
75 | for (int h = 0; h < ccnt; h += Vec8f().size()) {
76 | const Vec8f dftgc = Vec8f().load_a(_dftgc + h);
77 | const Vec8f dftc = Vec8f().load_a(_dftc + h);
78 | const Vec8f dftc2 = gf * dftgc;
79 | dftc2.store_a(_dftc2 + h);
80 | (dftc - dftc2).store_a(_dftc + h);
81 | }
82 | }
83 |
84 | static inline auto addMean(float * _dftc, const int ccnt, const float * _dftc2) noexcept {
85 | for (int h = 0; h < ccnt; h += Vec8f().size()) {
86 | const Vec8f dftc = Vec8f().load_a(_dftc + h);
87 | const Vec8f dftc2 = Vec8f().load_a(_dftc2 + h);
88 | (dftc + dftc2).store_a(_dftc + h);
89 | }
90 | }
91 |
92 | template
93 | inline void filter_avx2(float * _dftc, const float * _sigmas, const int ccnt, const float * _pmin, const float * _pmax, const float * _sigmas2) noexcept {
94 | const Vec8f beta = _pmin[0];
95 |
96 | for (int h = 0; h < ccnt; h += Vec8f().size()) {
97 | Vec8f dftc, psd, sigmas, pmin, pmax, mult;
98 |
99 | dftc = Vec8f().load_a(_dftc + h);
100 | sigmas = Vec8f().load_a(_sigmas + h);
101 |
102 | if constexpr (type != 2) {
103 | const Vec8f dftcSquare = dftc * dftc;
104 | psd = dftcSquare + permute8<1, 0, 3, 2, 5, 4, 7, 6>(dftcSquare);
105 | }
106 |
107 | if constexpr (type == 3 || type == 4) {
108 | pmin = Vec8f().load_a(_pmin + h);
109 | pmax = Vec8f().load_a(_pmax + h);
110 | }
111 |
112 | if constexpr (type == 0) {
113 | mult = max((psd - sigmas) * rcp_nr(psd + 1e-15f), zero_8f());
114 | } else if constexpr (type == 1) {
115 | dftc = select(psd < sigmas, zero_8f(), dftc);
116 | } else if constexpr (type == 2) {
117 | dftc *= sigmas;
118 | } else if constexpr (type == 3) {
119 | const Vec8f sigmas2 = Vec8f().load_a(_sigmas2 + h);
120 | dftc = select(psd >= pmin && psd <= pmax, dftc * sigmas, dftc * sigmas2);
121 | } else if constexpr (type == 4) {
122 | mult = sigmas * sqrt(psd * pmax * rcp_nr(mul_add(psd + pmin, psd + pmax, 1e-15f)));
123 | } else if constexpr (type == 5) {
124 | mult = pow(max((psd - sigmas) * rcp_nr(psd + 1e-15f), zero_8f()), beta);
125 | } else {
126 | mult = sqrt(max((psd - sigmas) * rcp_nr(psd + 1e-15f), zero_8f()));
127 | }
128 |
129 | if constexpr (type == 0 || type > 3)
130 | dftc *= mult;
131 |
132 | dftc.store_a(_dftc + h);
133 | }
134 | }
135 |
136 | template
137 | static auto cast(const float * ebp, pixel_t * dstp, const int dstWidth, const int dstHeight, const int dstStride, const int ebpStride, const float dstScale, const int peak) noexcept {
138 | for (int y = 0; y < dstHeight; y++) {
139 | for (int x = 0; x < dstWidth; x += Vec8f().size()) {
140 | if constexpr (std::is_same_v) {
141 | const Vec8i srcp = truncatei(Vec8f().load(ebp + x) + 0.5f);
142 | const auto result = compress_saturated_s2u(compress_saturated(srcp, zero_si256()), zero_si256()).get_low();
143 | result.storel(dstp + x);
144 | } else if constexpr (std::is_same_v) {
145 | const Vec8i srcp = truncatei(mul_add(Vec8f().load(ebp + x), dstScale, 0.5f));
146 | const auto result = compress_saturated_s2u(srcp, zero_si256()).get_low();
147 | min(result, peak).store_nt(dstp + x);
148 | } else {
149 | const Vec8f srcp = Vec8f().load(ebp + x) * dstScale;
150 | srcp.store_nt(dstp + x);
151 | }
152 | }
153 |
154 | ebp += ebpStride;
155 | dstp += dstStride;
156 | }
157 | }
158 |
159 | template
160 | void func_0_avx2(VSFrameRef * src[3], VSFrameRef * dst, const DFTTestData * const VS_RESTRICT d, const VSAPI * vsapi) noexcept {
161 | const float * hw = d->hw.get();
162 | const float * sigmas = d->sigmas.get();
163 | const float * sigmas2 = d->sigmas2.get();
164 | const float * pmins = d->pmins.get();
165 | const float * pmaxs = d->pmaxs.get();
166 | const fftwf_complex * dftgc = d->dftgc.get();
167 | fftwf_plan ft = d->ft.get();
168 | fftwf_plan fti = d->fti.get();
169 |
170 | const auto threadId = std::this_thread::get_id();
171 | float * ebuff = reinterpret_cast(vsapi->getWritePtr(d->ebuff.at(threadId).get(), 0));
172 | float * dftr = d->dftr.at(threadId).get();
173 | fftwf_complex * dftc = d->dftc.at(threadId).get();
174 | fftwf_complex * dftc2 = d->dftc2.at(threadId).get();
175 |
176 | for (int plane = 0; plane < d->vi->format->numPlanes; plane++) {
177 | if (d->process[plane]) {
178 | const int width = d->padWidth[plane];
179 | const int height = d->padHeight[plane];
180 | const int eheight = d->eheight[plane];
181 | const int srcStride = vsapi->getStride(src[plane], 0) / sizeof(pixel_t);
182 | const int ebpStride = vsapi->getStride(d->ebuff.at(threadId).get(), 0) / sizeof(float);
183 | const pixel_t * srcp = reinterpret_cast(vsapi->getReadPtr(src[plane], 0));
184 | float * ebpSaved = ebuff;
185 |
186 | memset(ebuff, 0, ebpStride * height * sizeof(float));
187 |
188 | for (int y = 0; y < eheight; y += d->inc) {
189 | for (int x = 0; x <= width - d->sbsize; x += d->inc) {
190 | proc0(srcp + x, hw, dftr, srcStride, d->sbsize, d->srcScale);
191 |
192 | fftwf_execute_dft_r2c(ft, dftr, dftc);
193 | if (d->zmean)
194 | removeMean(reinterpret_cast(dftc), reinterpret_cast(dftgc), d->ccnt2, reinterpret_cast(dftc2));
195 |
196 | d->filterCoeffs(reinterpret_cast(dftc), sigmas, d->ccnt2, d->uf0b ? &d->f0beta : pmins, pmaxs, sigmas2);
197 |
198 | if (d->zmean)
199 | addMean(reinterpret_cast(dftc), d->ccnt2, reinterpret_cast(dftc2));
200 | fftwf_execute_dft_c2r(fti, dftc, dftr);
201 |
202 | if (d->type & 1) { // spatial overlapping
203 | if (!(d->sbsize & (Vec8f().size() - 1)))
204 | proc1(dftr, hw, ebpSaved + x, d->sbsize, ebpStride);
205 | else
206 | proc1Partial(dftr, hw, ebpSaved + x, d->sbsize, ebpStride);
207 | } else {
208 | ebpSaved[x + d->sbd1 * ebpStride + d->sbd1] = dftr[d->sbd1 * d->sbsize + d->sbd1] * hw[d->sbd1 * d->sbsize + d->sbd1];
209 | }
210 | }
211 |
212 | srcp += srcStride * d->inc;
213 | ebpSaved += ebpStride * d->inc;
214 | }
215 |
216 | const int dstWidth = vsapi->getFrameWidth(dst, plane);
217 | const int dstHeight = vsapi->getFrameHeight(dst, plane);
218 | const int dstStride = vsapi->getStride(dst, plane) / sizeof(pixel_t);
219 | pixel_t * dstp = reinterpret_cast(vsapi->getWritePtr(dst, plane));
220 | const float * ebp = ebuff + ebpStride * ((height - dstHeight) / 2) + (width - dstWidth) / 2;
221 | cast(ebp, dstp, dstWidth, dstHeight, dstStride, ebpStride, d->dstScale, d->peak);
222 | }
223 | }
224 | }
225 |
226 | template
227 | void func_1_avx2(VSFrameRef * src[15][3], VSFrameRef * dst, const int pos, const DFTTestData * const VS_RESTRICT d, const VSAPI * vsapi) noexcept {
228 | const float * hw = d->hw.get();
229 | const float * sigmas = d->sigmas.get();
230 | const float * sigmas2 = d->sigmas2.get();
231 | const float * pmins = d->pmins.get();
232 | const float * pmaxs = d->pmaxs.get();
233 | const fftwf_complex * dftgc = d->dftgc.get();
234 | fftwf_plan ft = d->ft.get();
235 | fftwf_plan fti = d->fti.get();
236 |
237 | const auto threadId = std::this_thread::get_id();
238 | float * ebuff = reinterpret_cast(vsapi->getWritePtr(d->ebuff.at(threadId).get(), 0));
239 | float * dftr = d->dftr.at(threadId).get();
240 | fftwf_complex * dftc = d->dftc.at(threadId).get();
241 | fftwf_complex * dftc2 = d->dftc2.at(threadId).get();
242 |
243 | for (int plane = 0; plane < d->vi->format->numPlanes; plane++) {
244 | if (d->process[plane]) {
245 | const int width = d->padWidth[plane];
246 | const int height = d->padHeight[plane];
247 | const int eheight = d->eheight[plane];
248 | const int srcStride = vsapi->getStride(src[0][plane], 0) / sizeof(pixel_t);
249 | const int ebpStride = vsapi->getStride(d->ebuff.at(threadId).get(), 0) / sizeof(float);
250 |
251 | const pixel_t * srcp[15] = {};
252 | for (int i = 0; i < d->tbsize; i++)
253 | srcp[i] = reinterpret_cast(vsapi->getReadPtr(src[i][plane], 0));
254 |
255 | memset(ebuff, 0, ebpStride * height * sizeof(float));
256 |
257 | for (int y = 0; y < eheight; y += d->inc) {
258 | for (int x = 0; x <= width - d->sbsize; x += d->inc) {
259 | for (int z = 0; z < d->tbsize; z++)
260 | proc0(srcp[z] + x, hw + d->barea * z, dftr + d->barea * z, srcStride, d->sbsize, d->srcScale);
261 |
262 | fftwf_execute_dft_r2c(ft, dftr, dftc);
263 | if (d->zmean)
264 | removeMean(reinterpret_cast(dftc), reinterpret_cast(dftgc), d->ccnt2, reinterpret_cast(dftc2));
265 |
266 | d->filterCoeffs(reinterpret_cast(dftc), sigmas, d->ccnt2, d->uf0b ? &d->f0beta : pmins, pmaxs, sigmas2);
267 |
268 | if (d->zmean)
269 | addMean(reinterpret_cast(dftc), d->ccnt2, reinterpret_cast(dftc2));
270 | fftwf_execute_dft_c2r(fti, dftc, dftr);
271 |
272 | if (d->type & 1) { // spatial overlapping
273 | if (!(d->sbsize & (Vec8f().size() - 1)))
274 | proc1(dftr + pos * d->barea, hw + pos * d->barea, ebuff + y * ebpStride + x, d->sbsize, ebpStride);
275 | else
276 | proc1Partial(dftr + pos * d->barea, hw + pos * d->barea, ebuff + y * ebpStride + x, d->sbsize, ebpStride);
277 | } else {
278 | ebuff[(y + d->sbd1) * ebpStride + x + d->sbd1] = dftr[pos * d->barea + d->sbd1 * d->sbsize + d->sbd1] * hw[pos * d->barea + d->sbd1 * d->sbsize + d->sbd1];
279 | }
280 | }
281 |
282 | for (int q = 0; q < d->tbsize; q++)
283 | srcp[q] += srcStride * d->inc;
284 | }
285 |
286 | const int dstWidth = vsapi->getFrameWidth(dst, plane);
287 | const int dstHeight = vsapi->getFrameHeight(dst, plane);
288 | const int dstStride = vsapi->getStride(dst, plane) / sizeof(pixel_t);
289 | pixel_t * dstp = reinterpret_cast(vsapi->getWritePtr(dst, plane));
290 | const float * ebp = ebuff + ebpStride * ((height - dstHeight) / 2) + (width - dstWidth) / 2;
291 | cast(ebp, dstp, dstWidth, dstHeight, dstStride, ebpStride, d->dstScale, d->peak);
292 | }
293 | }
294 | }
295 |
296 | template void filter_avx2<0>(float * dftc, const float * sigmas, const int ccnt, const float * pmin, const float * pmax, const float * sigmas2) noexcept;
297 | template void filter_avx2<1>(float * dftc, const float * sigmas, const int ccnt, const float * pmin, const float * pmax, const float * sigmas2) noexcept;
298 | template void filter_avx2<2>(float * dftc, const float * sigmas, const int ccnt, const float * pmin, const float * pmax, const float * sigmas2) noexcept;
299 | template void filter_avx2<3>(float * dftc, const float * sigmas, const int ccnt, const float * pmin, const float * pmax, const float * sigmas2) noexcept;
300 | template void filter_avx2<4>(float * dftc, const float * sigmas, const int ccnt, const float * pmin, const float * pmax, const float * sigmas2) noexcept;
301 | template void filter_avx2<5>(float * dftc, const float * sigmas, const int ccnt, const float * pmin, const float * pmax, const float * sigmas2) noexcept;
302 | template void filter_avx2<6>(float * dftc, const float * sigmas, const int ccnt, const float * pmin, const float * pmax, const float * sigmas2) noexcept;
303 |
304 | template void func_0_avx2(VSFrameRef * src[3], VSFrameRef * dst, const DFTTestData * const VS_RESTRICT d, const VSAPI * vsapi) noexcept;
305 | template void func_0_avx2(VSFrameRef * src[3], VSFrameRef * dst, const DFTTestData * const VS_RESTRICT d, const VSAPI * vsapi) noexcept;
306 | template void func_0_avx2(VSFrameRef * src[3], VSFrameRef * dst, const DFTTestData * const VS_RESTRICT d, const VSAPI * vsapi) noexcept;
307 |
308 | template void func_1_avx2(VSFrameRef * src[15][3], VSFrameRef * dst, const int pos, const DFTTestData * const VS_RESTRICT d, const VSAPI * vsapi) noexcept;
309 | template void func_1_avx2(VSFrameRef * src[15][3], VSFrameRef * dst, const int pos, const DFTTestData * const VS_RESTRICT d, const VSAPI * vsapi) noexcept;
310 | template void func_1_avx2(VSFrameRef * src[15][3], VSFrameRef * dst, const int pos, const DFTTestData * const VS_RESTRICT d, const VSAPI * vsapi) noexcept;
311 | #endif
312 |
--------------------------------------------------------------------------------
/DFTTest/DFTTest_AVX512.cpp:
--------------------------------------------------------------------------------
1 | #ifdef DFTTEST_X86
2 | #include "DFTTest.h"
3 |
4 | #include "VCL2/vectormath_exp.h"
5 |
6 | template
7 | static inline auto proc0(const pixel_t * _s0, const float * _s1, float * d, const int p0, const int p1, const float srcScale) noexcept {
8 | for (int u = 0; u < p1; u++) {
9 | for (int v = 0; v < p1; v += Vec16f().size()) {
10 | Vec16f s0;
11 |
12 | if constexpr (std::is_same_v)
13 | s0 = to_float(Vec16i().load_16uc(_s0 + v));
14 | else if constexpr (std::is_same_v)
15 | s0 = to_float(Vec16i().load_16us(_s0 + v));
16 | else
17 | s0 = Vec16f().load(_s0 + v);
18 |
19 | const Vec16f s1 = Vec16f().load(_s1 + v);
20 |
21 | if constexpr (std::is_same_v)
22 | (s0 * s1).store(d + v);
23 | else
24 | (s0 * srcScale * s1).store(d + v);
25 | }
26 |
27 | _s0 += p0;
28 | _s1 += p1;
29 | d += p1;
30 | }
31 | }
32 |
33 | static inline auto proc1(const float * _s0, const float * _s1, float * _d, const int p0, const int p1) noexcept {
34 | for (int u = 0; u < p0; u++) {
35 | for (int v = 0; v < p0; v += Vec16f().size()) {
36 | const Vec16f s0 = Vec16f().load(_s0 + v);
37 | const Vec16f s1 = Vec16f().load(_s1 + v);
38 | const Vec16f d = Vec16f().load(_d + v);
39 | mul_add(s0, s1, d).store(_d + v);
40 | }
41 |
42 | _s0 += p0;
43 | _s1 += p0;
44 | _d += p1;
45 | }
46 | }
47 |
48 | static inline auto proc1Partial(const float * _s0, const float * _s1, float * _d, const int p0, const int p1) noexcept {
49 | const int regularPart = p0 & ~(Vec16f().size() - 1);
50 |
51 | for (int u = 0; u < p0; u++) {
52 | int v;
53 |
54 | for (v = 0; v < regularPart; v += Vec16f().size()) {
55 | const Vec16f s0 = Vec16f().load(_s0 + v);
56 | const Vec16f s1 = Vec16f().load(_s1 + v);
57 | const Vec16f d = Vec16f().load(_d + v);
58 | mul_add(s0, s1, d).store(_d + v);
59 | }
60 |
61 | const Vec16f s0 = Vec16f().load(_s0 + v);
62 | const Vec16f s1 = Vec16f().load(_s1 + v);
63 | const Vec16f d = Vec16f().load(_d + v);
64 | mul_add(s0, s1, d).store_partial(p0 - v, _d + v);
65 |
66 | _s0 += p0;
67 | _s1 += p0;
68 | _d += p1;
69 | }
70 | }
71 |
72 | static inline auto removeMean(float * _dftc, const float * _dftgc, const int ccnt, float * _dftc2) noexcept {
73 | const Vec16f gf = _dftc[0] / _dftgc[0];
74 |
75 | for (int h = 0; h < ccnt; h += Vec16f().size()) {
76 | const Vec16f dftgc = Vec16f().load_a(_dftgc + h);
77 | const Vec16f dftc = Vec16f().load_a(_dftc + h);
78 | const Vec16f dftc2 = gf * dftgc;
79 | dftc2.store_a(_dftc2 + h);
80 | (dftc - dftc2).store_a(_dftc + h);
81 | }
82 | }
83 |
84 | static inline auto addMean(float * _dftc, const int ccnt, const float * _dftc2) noexcept {
85 | for (int h = 0; h < ccnt; h += Vec16f().size()) {
86 | const Vec16f dftc = Vec16f().load_a(_dftc + h);
87 | const Vec16f dftc2 = Vec16f().load_a(_dftc2 + h);
88 | (dftc + dftc2).store_a(_dftc + h);
89 | }
90 | }
91 |
92 | template
93 | inline void filter_avx512(float * _dftc, const float * _sigmas, const int ccnt, const float * _pmin, const float * _pmax, const float * _sigmas2) noexcept {
94 | const Vec16f beta = _pmin[0];
95 |
96 | for (int h = 0; h < ccnt; h += Vec16f().size()) {
97 | Vec16f dftc, psd, sigmas, pmin, pmax, mult;
98 |
99 | dftc = Vec16f().load_a(_dftc + h);
100 | sigmas = Vec16f().load_a(_sigmas + h);
101 |
102 | if constexpr (type != 2) {
103 | const Vec16f dftcSquare = dftc * dftc;
104 | psd = dftcSquare + permute16<1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14>(dftcSquare);
105 | }
106 |
107 | if constexpr (type == 3 || type == 4) {
108 | pmin = Vec16f().load_a(_pmin + h);
109 | pmax = Vec16f().load_a(_pmax + h);
110 | }
111 |
112 | if constexpr (type == 0) {
113 | mult = max((psd - sigmas) * rcp_nr(psd + 1e-15f), zero_16f());
114 | } else if constexpr (type == 1) {
115 | dftc = select(psd < sigmas, zero_16f(), dftc);
116 | } else if constexpr (type == 2) {
117 | dftc *= sigmas;
118 | } else if constexpr (type == 3) {
119 | const Vec16f sigmas2 = Vec16f().load_a(_sigmas2 + h);
120 | dftc = select(psd >= pmin && psd <= pmax, dftc * sigmas, dftc * sigmas2);
121 | } else if constexpr (type == 4) {
122 | mult = sigmas * sqrt(psd * pmax * rcp_nr(mul_add(psd + pmin, psd + pmax, 1e-15f)));
123 | } else if constexpr (type == 5) {
124 | mult = pow(max((psd - sigmas) * rcp_nr(psd + 1e-15f), zero_16f()), beta);
125 | } else {
126 | mult = sqrt(max((psd - sigmas) * rcp_nr(psd + 1e-15f), zero_16f()));
127 | }
128 |
129 | if constexpr (type == 0 || type > 3)
130 | dftc *= mult;
131 |
132 | dftc.store_a(_dftc + h);
133 | }
134 | }
135 |
136 | template
137 | static auto cast(const float * ebp, pixel_t * dstp, const int dstWidth, const int dstHeight, const int dstStride, const int ebpStride, const float dstScale, const int peak) noexcept {
138 | for (int y = 0; y < dstHeight; y++) {
139 | for (int x = 0; x < dstWidth; x += Vec16f().size()) {
140 | if constexpr (std::is_same_v) {
141 | const Vec16i srcp = truncatei(Vec16f().load(ebp + x) + 0.5f);
142 | const auto result = compress_saturated_s2u(compress_saturated(srcp, zero_si512()), zero_si512()).get_low().get_low();
143 | result.store_nt(dstp + x);
144 | } else if constexpr (std::is_same_v) {
145 | const Vec16i srcp = truncatei(mul_add(Vec16f().load(ebp + x), dstScale, 0.5f));
146 | const auto result = compress_saturated_s2u(srcp, zero_si512()).get_low();
147 | min(result, peak).store_nt(dstp + x);
148 | } else {
149 | const Vec16f srcp = Vec16f().load(ebp + x) * dstScale;
150 | srcp.store_nt(dstp + x);
151 | }
152 | }
153 |
154 | ebp += ebpStride;
155 | dstp += dstStride;
156 | }
157 | }
158 |
159 | template
160 | void func_0_avx512(VSFrameRef * src[3], VSFrameRef * dst, const DFTTestData * const VS_RESTRICT d, const VSAPI * vsapi) noexcept {
161 | const float * hw = d->hw.get();
162 | const float * sigmas = d->sigmas.get();
163 | const float * sigmas2 = d->sigmas2.get();
164 | const float * pmins = d->pmins.get();
165 | const float * pmaxs = d->pmaxs.get();
166 | const fftwf_complex * dftgc = d->dftgc.get();
167 | fftwf_plan ft = d->ft.get();
168 | fftwf_plan fti = d->fti.get();
169 |
170 | const auto threadId = std::this_thread::get_id();
171 | float * ebuff = reinterpret_cast(vsapi->getWritePtr(d->ebuff.at(threadId).get(), 0));
172 | float * dftr = d->dftr.at(threadId).get();
173 | fftwf_complex * dftc = d->dftc.at(threadId).get();
174 | fftwf_complex * dftc2 = d->dftc2.at(threadId).get();
175 |
176 | for (int plane = 0; plane < d->vi->format->numPlanes; plane++) {
177 | if (d->process[plane]) {
178 | const int width = d->padWidth[plane];
179 | const int height = d->padHeight[plane];
180 | const int eheight = d->eheight[plane];
181 | const int srcStride = vsapi->getStride(src[plane], 0) / sizeof(pixel_t);
182 | const int ebpStride = vsapi->getStride(d->ebuff.at(threadId).get(), 0) / sizeof(float);
183 | const pixel_t * srcp = reinterpret_cast(vsapi->getReadPtr(src[plane], 0));
184 | float * ebpSaved = ebuff;
185 |
186 | memset(ebuff, 0, ebpStride * height * sizeof(float));
187 |
188 | for (int y = 0; y < eheight; y += d->inc) {
189 | for (int x = 0; x <= width - d->sbsize; x += d->inc) {
190 | proc0(srcp + x, hw, dftr, srcStride, d->sbsize, d->srcScale);
191 |
192 | fftwf_execute_dft_r2c(ft, dftr, dftc);
193 | if (d->zmean)
194 | removeMean(reinterpret_cast(dftc), reinterpret_cast(dftgc), d->ccnt2, reinterpret_cast(dftc2));
195 |
196 | d->filterCoeffs(reinterpret_cast(dftc), sigmas, d->ccnt2, d->uf0b ? &d->f0beta : pmins, pmaxs, sigmas2);
197 |
198 | if (d->zmean)
199 | addMean(reinterpret_cast(dftc), d->ccnt2, reinterpret_cast(dftc2));
200 | fftwf_execute_dft_c2r(fti, dftc, dftr);
201 |
202 | if (d->type & 1) { // spatial overlapping
203 | if (!(d->sbsize & (Vec16f().size() - 1)))
204 | proc1(dftr, hw, ebpSaved + x, d->sbsize, ebpStride);
205 | else
206 | proc1Partial(dftr, hw, ebpSaved + x, d->sbsize, ebpStride);
207 | } else {
208 | ebpSaved[x + d->sbd1 * ebpStride + d->sbd1] = dftr[d->sbd1 * d->sbsize + d->sbd1] * hw[d->sbd1 * d->sbsize + d->sbd1];
209 | }
210 | }
211 |
212 | srcp += srcStride * d->inc;
213 | ebpSaved += ebpStride * d->inc;
214 | }
215 |
216 | const int dstWidth = vsapi->getFrameWidth(dst, plane);
217 | const int dstHeight = vsapi->getFrameHeight(dst, plane);
218 | const int dstStride = vsapi->getStride(dst, plane) / sizeof(pixel_t);
219 | pixel_t * dstp = reinterpret_cast(vsapi->getWritePtr(dst, plane));
220 | const float * ebp = ebuff + ebpStride * ((height - dstHeight) / 2) + (width - dstWidth) / 2;
221 | cast(ebp, dstp, dstWidth, dstHeight, dstStride, ebpStride, d->dstScale, d->peak);
222 | }
223 | }
224 | }
225 |
226 | template
227 | void func_1_avx512(VSFrameRef * src[15][3], VSFrameRef * dst, const int pos, const DFTTestData * const VS_RESTRICT d, const VSAPI * vsapi) noexcept {
228 | const float * hw = d->hw.get();
229 | const float * sigmas = d->sigmas.get();
230 | const float * sigmas2 = d->sigmas2.get();
231 | const float * pmins = d->pmins.get();
232 | const float * pmaxs = d->pmaxs.get();
233 | const fftwf_complex * dftgc = d->dftgc.get();
234 | fftwf_plan ft = d->ft.get();
235 | fftwf_plan fti = d->fti.get();
236 |
237 | const auto threadId = std::this_thread::get_id();
238 | float * ebuff = reinterpret_cast(vsapi->getWritePtr(d->ebuff.at(threadId).get(), 0));
239 | float * dftr = d->dftr.at(threadId).get();
240 | fftwf_complex * dftc = d->dftc.at(threadId).get();
241 | fftwf_complex * dftc2 = d->dftc2.at(threadId).get();
242 |
243 | for (int plane = 0; plane < d->vi->format->numPlanes; plane++) {
244 | if (d->process[plane]) {
245 | const int width = d->padWidth[plane];
246 | const int height = d->padHeight[plane];
247 | const int eheight = d->eheight[plane];
248 | const int srcStride = vsapi->getStride(src[0][plane], 0) / sizeof(pixel_t);
249 | const int ebpStride = vsapi->getStride(d->ebuff.at(threadId).get(), 0) / sizeof(float);
250 |
251 | const pixel_t * srcp[15] = {};
252 | for (int i = 0; i < d->tbsize; i++)
253 | srcp[i] = reinterpret_cast(vsapi->getReadPtr(src[i][plane], 0));
254 |
255 | memset(ebuff, 0, ebpStride * height * sizeof(float));
256 |
257 | for (int y = 0; y < eheight; y += d->inc) {
258 | for (int x = 0; x <= width - d->sbsize; x += d->inc) {
259 | for (int z = 0; z < d->tbsize; z++)
260 | proc0(srcp[z] + x, hw + d->barea * z, dftr + d->barea * z, srcStride, d->sbsize, d->srcScale);
261 |
262 | fftwf_execute_dft_r2c(ft, dftr, dftc);
263 | if (d->zmean)
264 | removeMean(reinterpret_cast(dftc), reinterpret_cast(dftgc), d->ccnt2, reinterpret_cast(dftc2));
265 |
266 | d->filterCoeffs(reinterpret_cast(dftc), sigmas, d->ccnt2, d->uf0b ? &d->f0beta : pmins, pmaxs, sigmas2);
267 |
268 | if (d->zmean)
269 | addMean(reinterpret_cast(dftc), d->ccnt2, reinterpret_cast(dftc2));
270 | fftwf_execute_dft_c2r(fti, dftc, dftr);
271 |
272 | if (d->type & 1) { // spatial overlapping
273 | if (!(d->sbsize & (Vec16f().size() - 1)))
274 | proc1(dftr + pos * d->barea, hw + pos * d->barea, ebuff + y * ebpStride + x, d->sbsize, ebpStride);
275 | else
276 | proc1Partial(dftr + pos * d->barea, hw + pos * d->barea, ebuff + y * ebpStride + x, d->sbsize, ebpStride);
277 | } else {
278 | ebuff[(y + d->sbd1) * ebpStride + x + d->sbd1] = dftr[pos * d->barea + d->sbd1 * d->sbsize + d->sbd1] * hw[pos * d->barea + d->sbd1 * d->sbsize + d->sbd1];
279 | }
280 | }
281 |
282 | for (int q = 0; q < d->tbsize; q++)
283 | srcp[q] += srcStride * d->inc;
284 | }
285 |
286 | const int dstWidth = vsapi->getFrameWidth(dst, plane);
287 | const int dstHeight = vsapi->getFrameHeight(dst, plane);
288 | const int dstStride = vsapi->getStride(dst, plane) / sizeof(pixel_t);
289 | pixel_t * dstp = reinterpret_cast(vsapi->getWritePtr(dst, plane));
290 | const float * ebp = ebuff + ebpStride * ((height - dstHeight) / 2) + (width - dstWidth) / 2;
291 | cast(ebp, dstp, dstWidth, dstHeight, dstStride, ebpStride, d->dstScale, d->peak);
292 | }
293 | }
294 | }
295 |
296 | template void filter_avx512<0>(float * dftc, const float * sigmas, const int ccnt, const float * pmin, const float * pmax, const float * sigmas2) noexcept;
297 | template void filter_avx512<1>(float * dftc, const float * sigmas, const int ccnt, const float * pmin, const float * pmax, const float * sigmas2) noexcept;
298 | template void filter_avx512<2>(float * dftc, const float * sigmas, const int ccnt, const float * pmin, const float * pmax, const float * sigmas2) noexcept;
299 | template void filter_avx512<3>(float * dftc, const float * sigmas, const int ccnt, const float * pmin, const float * pmax, const float * sigmas2) noexcept;
300 | template void filter_avx512<4>(float * dftc, const float * sigmas, const int ccnt, const float * pmin, const float * pmax, const float * sigmas2) noexcept;
301 | template void filter_avx512<5>(float * dftc, const float * sigmas, const int ccnt, const float * pmin, const float * pmax, const float * sigmas2) noexcept;
302 | template void filter_avx512<6>(float * dftc, const float * sigmas, const int ccnt, const float * pmin, const float * pmax, const float * sigmas2) noexcept;
303 |
304 | template void func_0_avx512(VSFrameRef * src[3], VSFrameRef * dst, const DFTTestData * const VS_RESTRICT d, const VSAPI * vsapi) noexcept;
305 | template void func_0_avx512(VSFrameRef * src[3], VSFrameRef * dst, const DFTTestData * const VS_RESTRICT d, const VSAPI * vsapi) noexcept;
306 | template void func_0_avx512(VSFrameRef * src[3], VSFrameRef * dst, const DFTTestData * const VS_RESTRICT d, const VSAPI * vsapi) noexcept;
307 |
308 | template void func_1_avx512(VSFrameRef * src[15][3], VSFrameRef * dst, const int pos, const DFTTestData * const VS_RESTRICT d, const VSAPI * vsapi) noexcept;
309 | template void func_1_avx512(VSFrameRef * src[15][3], VSFrameRef * dst, const int pos, const DFTTestData * const VS_RESTRICT d, const VSAPI * vsapi) noexcept;
310 | template void func_1_avx512(VSFrameRef * src[15][3], VSFrameRef * dst, const int pos, const DFTTestData * const VS_RESTRICT d, const VSAPI * vsapi) noexcept;
311 | #endif
312 |
--------------------------------------------------------------------------------
/DFTTest/DFTTest_SSE2.cpp:
--------------------------------------------------------------------------------
1 | #ifdef DFTTEST_X86
2 | #include "DFTTest.h"
3 |
4 | #include "VCL2/vectormath_exp.h"
5 |
6 | template
7 | static inline auto proc0(const pixel_t * _s0, const float * _s1, float * d, const int p0, const int p1, const float srcScale) noexcept {
8 | for (int u = 0; u < p1; u++) {
9 | for (int v = 0; v < p1; v += Vec4f().size()) {
10 | Vec4f s0;
11 |
12 | if constexpr (std::is_same_v)
13 | s0 = to_float(Vec4i().load_4uc(_s0 + v));
14 | else if constexpr (std::is_same_v)
15 | s0 = to_float(Vec4i().load_4us(_s0 + v));
16 | else
17 | s0 = Vec4f().load(_s0 + v);
18 |
19 | const Vec4f s1 = Vec4f().load(_s1 + v);
20 |
21 | if constexpr (std::is_same_v)
22 | (s0 * s1).store(d + v);
23 | else
24 | (s0 * srcScale * s1).store(d + v);
25 | }
26 |
27 | _s0 += p0;
28 | _s1 += p1;
29 | d += p1;
30 | }
31 | }
32 |
33 | static inline auto proc1(const float * _s0, const float * _s1, float * _d, const int p0, const int p1) noexcept {
34 | for (int u = 0; u < p0; u++) {
35 | for (int v = 0; v < p0; v += Vec4f().size()) {
36 | const Vec4f s0 = Vec4f().load(_s0 + v);
37 | const Vec4f s1 = Vec4f().load(_s1 + v);
38 | const Vec4f d = Vec4f().load(_d + v);
39 | mul_add(s0, s1, d).store(_d + v);
40 | }
41 |
42 | _s0 += p0;
43 | _s1 += p0;
44 | _d += p1;
45 | }
46 | }
47 |
48 | static inline auto proc1Partial(const float * _s0, const float * _s1, float * _d, const int p0, const int p1) noexcept {
49 | const int regularPart = p0 & ~(Vec4f().size() - 1);
50 |
51 | for (int u = 0; u < p0; u++) {
52 | int v;
53 |
54 | for (v = 0; v < regularPart; v += Vec4f().size()) {
55 | const Vec4f s0 = Vec4f().load(_s0 + v);
56 | const Vec4f s1 = Vec4f().load(_s1 + v);
57 | const Vec4f d = Vec4f().load(_d + v);
58 | mul_add(s0, s1, d).store(_d + v);
59 | }
60 |
61 | const Vec4f s0 = Vec4f().load(_s0 + v);
62 | const Vec4f s1 = Vec4f().load(_s1 + v);
63 | const Vec4f d = Vec4f().load(_d + v);
64 | mul_add(s0, s1, d).store_partial(p0 - v, _d + v);
65 |
66 | _s0 += p0;
67 | _s1 += p0;
68 | _d += p1;
69 | }
70 | }
71 |
72 | static inline auto removeMean(float * _dftc, const float * _dftgc, const int ccnt, float * _dftc2) noexcept {
73 | const Vec4f gf = _dftc[0] / _dftgc[0];
74 |
75 | for (int h = 0; h < ccnt; h += Vec4f().size()) {
76 | const Vec4f dftgc = Vec4f().load_a(_dftgc + h);
77 | const Vec4f dftc = Vec4f().load_a(_dftc + h);
78 | const Vec4f dftc2 = gf * dftgc;
79 | dftc2.store_a(_dftc2 + h);
80 | (dftc - dftc2).store_a(_dftc + h);
81 | }
82 | }
83 |
84 | static inline auto addMean(float * _dftc, const int ccnt, const float * _dftc2) noexcept {
85 | for (int h = 0; h < ccnt; h += Vec4f().size()) {
86 | const Vec4f dftc = Vec4f().load_a(_dftc + h);
87 | const Vec4f dftc2 = Vec4f().load_a(_dftc2 + h);
88 | (dftc + dftc2).store_a(_dftc + h);
89 | }
90 | }
91 |
92 | template
93 | inline void filter_sse2(float * _dftc, const float * _sigmas, const int ccnt, const float * _pmin, const float * _pmax, const float * _sigmas2) noexcept {
94 | const int step = Vec4f().size() * 2;
95 | const Vec4f beta = _pmin[0];
96 |
97 | for (int h = 0; h < ccnt; h += step) {
98 | Vec4f dftcLow, dftcHigh, real, imag, psd, sigmas, pmin, pmax, mult;
99 |
100 | if constexpr (type != 2) {
101 | dftcLow = Vec4f().load_a(_dftc + h + 0);
102 | dftcHigh = Vec4f().load_a(_dftc + h + Vec4f().size());
103 | real = blend4<0, 2, 4, 6>(dftcLow, dftcHigh);
104 | imag = blend4<1, 3, 5, 7>(dftcLow, dftcHigh);
105 | psd = mul_add(real, real, imag * imag);
106 |
107 | const Vec4f sigmasLow = Vec4f().load_a(_sigmas + h + 0);
108 | const Vec4f sigmasHigh = Vec4f().load_a(_sigmas + h + Vec4f().size());
109 | sigmas = blend4<0, 2, 4, 6>(sigmasLow, sigmasHigh);
110 | }
111 |
112 | if constexpr (type == 3 || type == 4) {
113 | const Vec4f pminLow = Vec4f().load_a(_pmin + h + 0);
114 | const Vec4f pminHigh = Vec4f().load_a(_pmin + h + Vec4f().size());
115 | pmin = blend4<0, 2, 4, 6>(pminLow, pminHigh);
116 |
117 | const Vec4f pmaxLow = Vec4f().load_a(_pmax + h + 0);
118 | const Vec4f pmaxHigh = Vec4f().load_a(_pmax + h + Vec4f().size());
119 | pmax = blend4<0, 2, 4, 6>(pmaxLow, pmaxHigh);
120 | }
121 |
122 | if constexpr (type == 0) {
123 | mult = max((psd - sigmas) * rcp_nr(psd + 1e-15f), zero_4f());
124 | } else if constexpr (type == 1) {
125 | const Vec4fb flag = (psd < sigmas);
126 | real = select(flag, zero_4f(), real);
127 | imag = select(flag, zero_4f(), imag);
128 | } else if constexpr (type == 2) {
129 | const Vec4f dftc = Vec4f().load_a(_dftc + h);
130 | sigmas = Vec4f().load_a(_sigmas + h);
131 | (dftc * sigmas).store_a(_dftc + h);
132 | } else if constexpr (type == 3) {
133 | const Vec4f sigmas2Low = Vec4f().load_a(_sigmas2 + h + 0);
134 | const Vec4f sigmas2High = Vec4f().load_a(_sigmas2 + h + Vec4f().size());
135 | const Vec4f sigmas2 = blend4<0, 2, 4, 6>(sigmas2Low, sigmas2High);
136 |
137 | const Vec4fb flag = (psd >= pmin && psd <= pmax);
138 | real = select(flag, real * sigmas, real * sigmas2);
139 | imag = select(flag, imag * sigmas, imag * sigmas2);
140 | } else if constexpr (type == 4) {
141 | mult = sigmas * sqrt(psd * pmax * rcp_nr(mul_add(psd + pmin, psd + pmax, 1e-15f)));
142 | } else if constexpr (type == 5) {
143 | mult = pow(max((psd - sigmas) * rcp_nr(psd + 1e-15f), zero_4f()), beta);
144 | } else {
145 | mult = sqrt(max((psd - sigmas) * rcp_nr(psd + 1e-15f), zero_4f()));
146 | }
147 |
148 | if constexpr (type == 0 || type > 3) {
149 | real *= mult;
150 | imag *= mult;
151 | }
152 |
153 | if constexpr (type != 2) {
154 | dftcLow = blend4<0, 4, 1, 5>(real, imag);
155 | dftcHigh = blend4<2, 6, 3, 7>(real, imag);
156 | dftcLow.store_a(_dftc + h + 0);
157 | dftcHigh.store_a(_dftc + h + Vec4f().size());
158 | }
159 | }
160 | }
161 |
162 | template
163 | static auto cast(const float * ebp, pixel_t * dstp, const int dstWidth, const int dstHeight, const int dstStride, const int ebpStride, const float dstScale, const int peak) noexcept {
164 | for (int y = 0; y < dstHeight; y++) {
165 | for (int x = 0; x < dstWidth; x += Vec4f().size()) {
166 | if constexpr (std::is_same_v) {
167 | const Vec4i srcp = truncatei(Vec4f().load(ebp + x) + 0.5f);
168 | const auto result = compress_saturated_s2u(compress_saturated(srcp, zero_si128()), zero_si128());
169 | result.store_si32(dstp + x);
170 | } else if constexpr (std::is_same_v) {
171 | const Vec4i srcp = truncatei(mul_add(Vec4f().load(ebp + x), dstScale, 0.5f));
172 | const auto result = compress_saturated_s2u(srcp, zero_si128());
173 | min(result, peak).storel(dstp + x);
174 | } else {
175 | const Vec4f srcp = Vec4f().load(ebp + x) * dstScale;
176 | srcp.store_nt(dstp + x);
177 | }
178 | }
179 |
180 | ebp += ebpStride;
181 | dstp += dstStride;
182 | }
183 | }
184 |
185 | template
186 | void func_0_sse2(VSFrameRef * src[3], VSFrameRef * dst, const DFTTestData * const VS_RESTRICT d, const VSAPI * vsapi) noexcept {
187 | const float * hw = d->hw.get();
188 | const float * sigmas = d->sigmas.get();
189 | const float * sigmas2 = d->sigmas2.get();
190 | const float * pmins = d->pmins.get();
191 | const float * pmaxs = d->pmaxs.get();
192 | const fftwf_complex * dftgc = d->dftgc.get();
193 | fftwf_plan ft = d->ft.get();
194 | fftwf_plan fti = d->fti.get();
195 |
196 | const auto threadId = std::this_thread::get_id();
197 | float * ebuff = reinterpret_cast(vsapi->getWritePtr(d->ebuff.at(threadId).get(), 0));
198 | float * dftr = d->dftr.at(threadId).get();
199 | fftwf_complex * dftc = d->dftc.at(threadId).get();
200 | fftwf_complex * dftc2 = d->dftc2.at(threadId).get();
201 |
202 | for (int plane = 0; plane < d->vi->format->numPlanes; plane++) {
203 | if (d->process[plane]) {
204 | const int width = d->padWidth[plane];
205 | const int height = d->padHeight[plane];
206 | const int eheight = d->eheight[plane];
207 | const int srcStride = vsapi->getStride(src[plane], 0) / sizeof(pixel_t);
208 | const int ebpStride = vsapi->getStride(d->ebuff.at(threadId).get(), 0) / sizeof(float);
209 | const pixel_t * srcp = reinterpret_cast(vsapi->getReadPtr(src[plane], 0));
210 | float * ebpSaved = ebuff;
211 |
212 | memset(ebuff, 0, ebpStride * height * sizeof(float));
213 |
214 | for (int y = 0; y < eheight; y += d->inc) {
215 | for (int x = 0; x <= width - d->sbsize; x += d->inc) {
216 | proc0(srcp + x, hw, dftr, srcStride, d->sbsize, d->srcScale);
217 |
218 | fftwf_execute_dft_r2c(ft, dftr, dftc);
219 | if (d->zmean)
220 | removeMean(reinterpret_cast(dftc), reinterpret_cast(dftgc), d->ccnt2, reinterpret_cast(dftc2));
221 |
222 | d->filterCoeffs(reinterpret_cast(dftc), sigmas, d->ccnt2, d->uf0b ? &d->f0beta : pmins, pmaxs, sigmas2);
223 |
224 | if (d->zmean)
225 | addMean(reinterpret_cast(dftc), d->ccnt2, reinterpret_cast(dftc2));
226 | fftwf_execute_dft_c2r(fti, dftc, dftr);
227 |
228 | if (d->type & 1) { // spatial overlapping
229 | if (!(d->sbsize & (Vec4f().size() - 1)))
230 | proc1(dftr, hw, ebpSaved + x, d->sbsize, ebpStride);
231 | else
232 | proc1Partial(dftr, hw, ebpSaved + x, d->sbsize, ebpStride);
233 | } else {
234 | ebpSaved[x + d->sbd1 * ebpStride + d->sbd1] = dftr[d->sbd1 * d->sbsize + d->sbd1] * hw[d->sbd1 * d->sbsize + d->sbd1];
235 | }
236 | }
237 |
238 | srcp += srcStride * d->inc;
239 | ebpSaved += ebpStride * d->inc;
240 | }
241 |
242 | const int dstWidth = vsapi->getFrameWidth(dst, plane);
243 | const int dstHeight = vsapi->getFrameHeight(dst, plane);
244 | const int dstStride = vsapi->getStride(dst, plane) / sizeof(pixel_t);
245 | pixel_t * dstp = reinterpret_cast(vsapi->getWritePtr(dst, plane));
246 | const float * ebp = ebuff + ebpStride * ((height - dstHeight) / 2) + (width - dstWidth) / 2;
247 | cast(ebp, dstp, dstWidth, dstHeight, dstStride, ebpStride, d->dstScale, d->peak);
248 | }
249 | }
250 | }
251 |
252 | template
253 | void func_1_sse2(VSFrameRef * src[15][3], VSFrameRef * dst, const int pos, const DFTTestData * const VS_RESTRICT d, const VSAPI * vsapi) noexcept {
254 | const float * hw = d->hw.get();
255 | const float * sigmas = d->sigmas.get();
256 | const float * sigmas2 = d->sigmas2.get();
257 | const float * pmins = d->pmins.get();
258 | const float * pmaxs = d->pmaxs.get();
259 | const fftwf_complex * dftgc = d->dftgc.get();
260 | fftwf_plan ft = d->ft.get();
261 | fftwf_plan fti = d->fti.get();
262 |
263 | const auto threadId = std::this_thread::get_id();
264 | float * ebuff = reinterpret_cast(vsapi->getWritePtr(d->ebuff.at(threadId).get(), 0));
265 | float * dftr = d->dftr.at(threadId).get();
266 | fftwf_complex * dftc = d->dftc.at(threadId).get();
267 | fftwf_complex * dftc2 = d->dftc2.at(threadId).get();
268 |
269 | for (int plane = 0; plane < d->vi->format->numPlanes; plane++) {
270 | if (d->process[plane]) {
271 | const int width = d->padWidth[plane];
272 | const int height = d->padHeight[plane];
273 | const int eheight = d->eheight[plane];
274 | const int srcStride = vsapi->getStride(src[0][plane], 0) / sizeof(pixel_t);
275 | const int ebpStride = vsapi->getStride(d->ebuff.at(threadId).get(), 0) / sizeof(float);
276 |
277 | const pixel_t * srcp[15] = {};
278 | for (int i = 0; i < d->tbsize; i++)
279 | srcp[i] = reinterpret_cast(vsapi->getReadPtr(src[i][plane], 0));
280 |
281 | memset(ebuff, 0, ebpStride * height * sizeof(float));
282 |
283 | for (int y = 0; y < eheight; y += d->inc) {
284 | for (int x = 0; x <= width - d->sbsize; x += d->inc) {
285 | for (int z = 0; z < d->tbsize; z++)
286 | proc0(srcp[z] + x, hw + d->barea * z, dftr + d->barea * z, srcStride, d->sbsize, d->srcScale);
287 |
288 | fftwf_execute_dft_r2c(ft, dftr, dftc);
289 | if (d->zmean)
290 | removeMean(reinterpret_cast(dftc), reinterpret_cast(dftgc), d->ccnt2, reinterpret_cast(dftc2));
291 |
292 | d->filterCoeffs(reinterpret_cast(dftc), sigmas, d->ccnt2, d->uf0b ? &d->f0beta : pmins, pmaxs, sigmas2);
293 |
294 | if (d->zmean)
295 | addMean(reinterpret_cast(dftc), d->ccnt2, reinterpret_cast(dftc2));
296 | fftwf_execute_dft_c2r(fti, dftc, dftr);
297 |
298 | if (d->type & 1) { // spatial overlapping
299 | if (!(d->sbsize & (Vec4f().size() - 1)))
300 | proc1(dftr + pos * d->barea, hw + pos * d->barea, ebuff + y * ebpStride + x, d->sbsize, ebpStride);
301 | else
302 | proc1Partial(dftr + pos * d->barea, hw + pos * d->barea, ebuff + y * ebpStride + x, d->sbsize, ebpStride);
303 | } else {
304 | ebuff[(y + d->sbd1) * ebpStride + x + d->sbd1] = dftr[pos * d->barea + d->sbd1 * d->sbsize + d->sbd1] * hw[pos * d->barea + d->sbd1 * d->sbsize + d->sbd1];
305 | }
306 | }
307 |
308 | for (int q = 0; q < d->tbsize; q++)
309 | srcp[q] += srcStride * d->inc;
310 | }
311 |
312 | const int dstWidth = vsapi->getFrameWidth(dst, plane);
313 | const int dstHeight = vsapi->getFrameHeight(dst, plane);
314 | const int dstStride = vsapi->getStride(dst, plane) / sizeof(pixel_t);
315 | pixel_t * dstp = reinterpret_cast(vsapi->getWritePtr(dst, plane));
316 | const float * ebp = ebuff + ebpStride * ((height - dstHeight) / 2) + (width - dstWidth) / 2;
317 | cast(ebp, dstp, dstWidth, dstHeight, dstStride, ebpStride, d->dstScale, d->peak);
318 | }
319 | }
320 | }
321 |
322 | template void filter_sse2<0>(float * dftc, const float * sigmas, const int ccnt, const float * pmin, const float * pmax, const float * sigmas2) noexcept;
323 | template void filter_sse2<1>(float * dftc, const float * sigmas, const int ccnt, const float * pmin, const float * pmax, const float * sigmas2) noexcept;
324 | template void filter_sse2<2>(float * dftc, const float * sigmas, const int ccnt, const float * pmin, const float * pmax, const float * sigmas2) noexcept;
325 | template void filter_sse2<3>(float * dftc, const float * sigmas, const int ccnt, const float * pmin, const float * pmax, const float * sigmas2) noexcept;
326 | template void filter_sse2<4>(float * dftc, const float * sigmas, const int ccnt, const float * pmin, const float * pmax, const float * sigmas2) noexcept;
327 | template void filter_sse2<5>(float * dftc, const float * sigmas, const int ccnt, const float * pmin, const float * pmax, const float * sigmas2) noexcept;
328 | template void filter_sse2<6>(float * dftc, const float * sigmas, const int ccnt, const float * pmin, const float * pmax, const float * sigmas2) noexcept;
329 |
330 | template void func_0_sse2(VSFrameRef * src[3], VSFrameRef * dst, const DFTTestData * const VS_RESTRICT d, const VSAPI * vsapi) noexcept;
331 | template void func_0_sse2(VSFrameRef * src[3], VSFrameRef * dst, const DFTTestData * const VS_RESTRICT d, const VSAPI * vsapi) noexcept;
332 | template void func_0_sse2(VSFrameRef * src[3], VSFrameRef * dst, const DFTTestData * const VS_RESTRICT d, const VSAPI * vsapi) noexcept;
333 |
334 | template void func_1_sse2(VSFrameRef * src[15][3], VSFrameRef * dst, const int pos, const DFTTestData * const VS_RESTRICT d, const VSAPI * vsapi) noexcept;
335 | template void func_1_sse2(VSFrameRef * src[15][3], VSFrameRef * dst, const int pos, const DFTTestData * const VS_RESTRICT d, const VSAPI * vsapi) noexcept;
336 | template void func_1_sse2(VSFrameRef * src[15][3], VSFrameRef * dst, const int pos, const DFTTestData * const VS_RESTRICT d, const VSAPI * vsapi) noexcept;
337 | #endif
338 |
--------------------------------------------------------------------------------
/DFTTest/VCL2/vector_convert.h:
--------------------------------------------------------------------------------
1 | /************************** vector_convert.h *******************************
2 | * Author: Agner Fog
3 | * Date created: 2014-07-23
4 | * Last modified: 2019-11-17
5 | * Version: 2.01.00
6 | * Project: vector class library
7 | * Description:
8 | * Header file for conversion between different vector classes with different
9 | * sizes. Also includes verious generic template functions.
10 | *
11 | * (c) Copyright 2012-2019 Agner Fog.
12 | * Apache License version 2.0 or later.
13 | *****************************************************************************/
14 |
15 | #ifndef VECTOR_CONVERT_H
16 | #define VECTOR_CONVERT_H
17 |
18 | #ifndef VECTORCLASS_H
19 | #include "vectorclass.h"
20 | #endif
21 |
22 | #if VECTORCLASS_H < 20100
23 | #error Incompatible versions of vector class library mixed
24 | #endif
25 |
26 | #ifdef VCL_NAMESPACE
27 | namespace VCL_NAMESPACE {
28 | #endif
29 |
30 | #if MAX_VECTOR_SIZE >= 256
31 |
32 | /*****************************************************************************
33 | *
34 | * Extend from 128 to 256 bit vectors
35 | *
36 | *****************************************************************************/
37 |
38 | #if INSTRSET >= 8 // AVX2. 256 bit integer vectors
39 |
40 | // sign extend
41 | static inline Vec16s extend (Vec16c const a) {
42 | return _mm256_cvtepi8_epi16(a);
43 | }
44 |
45 | // zero extend
46 | static inline Vec16us extend (Vec16uc const a) {
47 | return _mm256_cvtepu8_epi16(a);
48 | }
49 |
50 | // sign extend
51 | static inline Vec8i extend (Vec8s const a) {
52 | return _mm256_cvtepi16_epi32(a);
53 | }
54 |
55 | // zero extend
56 | static inline Vec8ui extend (Vec8us const a) {
57 | return _mm256_cvtepu16_epi32(a);
58 | }
59 |
60 | // sign extend
61 | static inline Vec4q extend (Vec4i const a) {
62 | return _mm256_cvtepi32_epi64(a);
63 | }
64 |
65 | // zero extend
66 | static inline Vec4uq extend (Vec4ui const a) {
67 | return _mm256_cvtepu32_epi64(a);
68 | }
69 |
70 |
71 | #else // no AVX2. 256 bit integer vectors are emulated
72 |
73 | // sign extend and zero extend functions:
74 | static inline Vec16s extend (Vec16c const a) {
75 | return Vec16s(extend_low(a), extend_high(a));
76 | }
77 |
78 | static inline Vec16us extend (Vec16uc const a) {
79 | return Vec16us(extend_low(a), extend_high(a));
80 | }
81 |
82 | static inline Vec8i extend (Vec8s const a) {
83 | return Vec8i(extend_low(a), extend_high(a));
84 | }
85 |
86 | static inline Vec8ui extend (Vec8us const a) {
87 | return Vec8ui(extend_low(a), extend_high(a));
88 | }
89 |
90 | static inline Vec4q extend (Vec4i const a) {
91 | return Vec4q(extend_low(a), extend_high(a));
92 | }
93 |
94 | static inline Vec4uq extend (Vec4ui const a) {
95 | return Vec4uq(extend_low(a), extend_high(a));
96 | }
97 |
98 | #endif // AVX2
99 |
100 | /*****************************************************************************
101 | *
102 | * Conversions between float and double
103 | *
104 | *****************************************************************************/
105 | #if INSTRSET >= 7 // AVX. 256 bit float vectors
106 |
107 | // float to double
108 | static inline Vec4d to_double (Vec4f const a) {
109 | return _mm256_cvtps_pd(a);
110 | }
111 |
112 | // double to float
113 | static inline Vec4f to_float (Vec4d const a) {
114 | return _mm256_cvtpd_ps(a);
115 | }
116 |
117 | #else // no AVX2. 256 bit float vectors are emulated
118 |
119 | // float to double
120 | static inline Vec4d to_double (Vec4f const a) {
121 | Vec2d lo = _mm_cvtps_pd(a);
122 | Vec2d hi = _mm_cvtps_pd(_mm_movehl_ps(a, a));
123 | return Vec4d(lo,hi);
124 | }
125 |
126 | // double to float
127 | static inline Vec4f to_float (Vec4d const a) {
128 | Vec4f lo = _mm_cvtpd_ps(a.get_low());
129 | Vec4f hi = _mm_cvtpd_ps(a.get_high());
130 | return _mm_movelh_ps(lo, hi);
131 | }
132 |
133 | #endif
134 |
135 | /*****************************************************************************
136 | *
137 | * Reduce from 256 to 128 bit vectors
138 | *
139 | *****************************************************************************/
140 | #if INSTRSET >= 10 // AVX512VL
141 |
142 | // compress functions. overflow wraps around
143 | static inline Vec16c compress (Vec16s const a) {
144 | return _mm256_cvtepi16_epi8(a);
145 | }
146 |
147 | static inline Vec16uc compress (Vec16us const a) {
148 | return _mm256_cvtepi16_epi8(a);
149 | }
150 |
151 | static inline Vec8s compress (Vec8i const a) {
152 | return _mm256_cvtepi32_epi16(a);
153 | }
154 |
155 | static inline Vec8us compress (Vec8ui const a) {
156 | return _mm256_cvtepi32_epi16(a);
157 | }
158 |
159 | static inline Vec4i compress (Vec4q const a) {
160 | return _mm256_cvtepi64_epi32(a);
161 | }
162 |
163 | static inline Vec4ui compress (Vec4uq const a) {
164 | return _mm256_cvtepi64_epi32(a);
165 | }
166 |
167 | #else // no AVX512
168 |
169 | // compress functions. overflow wraps around
170 | static inline Vec16c compress (Vec16s const a) {
171 | return compress(a.get_low(), a.get_high());
172 | }
173 |
174 | static inline Vec16uc compress (Vec16us const a) {
175 | return compress(a.get_low(), a.get_high());
176 | }
177 |
178 | static inline Vec8s compress (Vec8i const a) {
179 | return compress(a.get_low(), a.get_high());
180 | }
181 |
182 | static inline Vec8us compress (Vec8ui const a) {
183 | return compress(a.get_low(), a.get_high());
184 | }
185 |
186 | static inline Vec4i compress (Vec4q const a) {
187 | return compress(a.get_low(), a.get_high());
188 | }
189 |
190 | static inline Vec4ui compress (Vec4uq const a) {
191 | return compress(a.get_low(), a.get_high());
192 | }
193 |
194 | #endif // AVX512
195 |
196 | #endif // MAX_VECTOR_SIZE >= 256
197 |
198 |
199 | #if MAX_VECTOR_SIZE >= 512
200 |
201 | /*****************************************************************************
202 | *
203 | * Extend from 256 to 512 bit vectors
204 | *
205 | *****************************************************************************/
206 |
207 | #if INSTRSET >= 9 // AVX512. 512 bit integer vectors
208 |
209 | // sign extend
210 | static inline Vec32s extend (Vec32c const a) {
211 | #if INSTRSET >= 10
212 | return _mm512_cvtepi8_epi16(a);
213 | #else
214 | return Vec32s(extend_low(a), extend_high(a));
215 | #endif
216 | }
217 |
218 | // zero extend
219 | static inline Vec32us extend (Vec32uc const a) {
220 | #if INSTRSET >= 10
221 | return _mm512_cvtepu8_epi16(a);
222 | #else
223 | return Vec32us(extend_low(a), extend_high(a));
224 | #endif
225 | }
226 |
227 | // sign extend
228 | static inline Vec16i extend (Vec16s const a) {
229 | return _mm512_cvtepi16_epi32(a);
230 | }
231 |
232 | // zero extend
233 | static inline Vec16ui extend (Vec16us const a) {
234 | return _mm512_cvtepu16_epi32(a);
235 | }
236 |
237 | // sign extend
238 | static inline Vec8q extend (Vec8i const a) {
239 | return _mm512_cvtepi32_epi64(a);
240 | }
241 |
242 | // zero extend
243 | static inline Vec8uq extend (Vec8ui const a) {
244 | return _mm512_cvtepu32_epi64(a);
245 | }
246 |
247 | #else // no AVX512. 512 bit vectors are emulated
248 |
249 |
250 |
251 | // sign extend
252 | static inline Vec32s extend (Vec32c const a) {
253 | return Vec32s(extend_low(a), extend_high(a));
254 | }
255 |
256 | // zero extend
257 | static inline Vec32us extend (Vec32uc const a) {
258 | return Vec32us(extend_low(a), extend_high(a));
259 | }
260 |
261 | // sign extend
262 | static inline Vec16i extend (Vec16s const a) {
263 | return Vec16i(extend_low(a), extend_high(a));
264 | }
265 |
266 | // zero extend
267 | static inline Vec16ui extend (Vec16us const a) {
268 | return Vec16ui(extend_low(a), extend_high(a));
269 | }
270 |
271 | // sign extend
272 | static inline Vec8q extend (Vec8i const a) {
273 | return Vec8q(extend_low(a), extend_high(a));
274 | }
275 |
276 | // zero extend
277 | static inline Vec8uq extend (Vec8ui const a) {
278 | return Vec8uq(extend_low(a), extend_high(a));
279 | }
280 |
281 | #endif // AVX512
282 |
283 |
284 | /*****************************************************************************
285 | *
286 | * Reduce from 512 to 256 bit vectors
287 | *
288 | *****************************************************************************/
289 | #if INSTRSET >= 9 // AVX512F
290 |
291 | // compress functions. overflow wraps around
292 | static inline Vec32c compress (Vec32s const a) {
293 | #if INSTRSET >= 10 // AVVX512BW
294 | return _mm512_cvtepi16_epi8(a);
295 | #else
296 | return compress(a.get_low(), a.get_high());
297 | #endif
298 | }
299 |
300 | static inline Vec32uc compress (Vec32us const a) {
301 | return Vec32uc(compress(Vec32s(a)));
302 | }
303 |
304 | static inline Vec16s compress (Vec16i const a) {
305 | return _mm512_cvtepi32_epi16(a);
306 | }
307 |
308 | static inline Vec16us compress (Vec16ui const a) {
309 | return _mm512_cvtepi32_epi16(a);
310 | }
311 |
312 | static inline Vec8i compress (Vec8q const a) {
313 | return _mm512_cvtepi64_epi32(a);
314 | }
315 |
316 | static inline Vec8ui compress (Vec8uq const a) {
317 | return _mm512_cvtepi64_epi32(a);
318 | }
319 |
320 | #else // no AVX512
321 |
322 | // compress functions. overflow wraps around
323 | static inline Vec32c compress (Vec32s const a) {
324 | return compress(a.get_low(), a.get_high());
325 | }
326 |
327 | static inline Vec32uc compress (Vec32us const a) {
328 | return compress(a.get_low(), a.get_high());
329 | }
330 |
331 | static inline Vec16s compress (Vec16i const a) {
332 | return compress(a.get_low(), a.get_high());
333 | }
334 |
335 | static inline Vec16us compress (Vec16ui const a) {
336 | return compress(a.get_low(), a.get_high());
337 | }
338 |
339 | static inline Vec8i compress (Vec8q const a) {
340 | return compress(a.get_low(), a.get_high());
341 | }
342 |
343 | static inline Vec8ui compress (Vec8uq const a) {
344 | return compress(a.get_low(), a.get_high());
345 | }
346 |
347 | #endif // AVX512
348 |
349 | /*****************************************************************************
350 | *
351 | * Conversions between float and double
352 | *
353 | *****************************************************************************/
354 |
355 | #if INSTRSET >= 9 // AVX512. 512 bit float vectors
356 |
357 | // float to double
358 | static inline Vec8d to_double (Vec8f const a) {
359 | return _mm512_cvtps_pd(a);
360 | }
361 |
362 | // double to float
363 | static inline Vec8f to_float (Vec8d const a) {
364 | return _mm512_cvtpd_ps(a);
365 | }
366 |
367 | #else // no AVX512. 512 bit float vectors are emulated
368 |
369 | // float to double
370 | static inline Vec8d to_double (Vec8f const a) {
371 | Vec4d lo = to_double(a.get_low());
372 | Vec4d hi = to_double(a.get_high());
373 | return Vec8d(lo,hi);
374 | }
375 |
376 | // double to float
377 | static inline Vec8f to_float (Vec8d const a) {
378 | Vec4f lo = to_float(a.get_low());
379 | Vec4f hi = to_float(a.get_high());
380 | return Vec8f(lo, hi);
381 | }
382 |
383 | #endif
384 |
385 | #endif // MAX_VECTOR_SIZE >= 512
386 |
387 | // double to float
388 | static inline Vec4f to_float (Vec2d const a) {
389 | return _mm_cvtpd_ps(a);
390 | }
391 |
392 |
393 | /*****************************************************************************
394 | *
395 | * Generic template functions
396 | *
397 | * These templates define functions for multiple vector types in one template
398 | *
399 | *****************************************************************************/
400 |
401 | // horizontal min/max of vector elements
402 | // implemented with universal template, works for all vector types:
403 |
404 | template auto horizontal_min(T const x) {
405 | if constexpr ((T::elementtype() & 16) != 0) {
406 | // T is a float or double vector
407 | if (horizontal_or(is_nan(x))) {
408 | // check for NAN because min does not guarantee NAN propagation
409 | return x[horizontal_find_first(is_nan(x))];
410 | }
411 | }
412 | return horizontal_min1(x);
413 | }
414 |
415 | template auto horizontal_min1(T const x) {
416 | if constexpr (T::elementtype() <= 3) { // boolean vector type
417 | return horizontal_and(x);
418 | }
419 | else if constexpr (sizeof(T) >= 32) {
420 | // split recursively into smaller vectors
421 | return horizontal_min1(min(x.get_low(), x.get_high()));
422 | }
423 | else if constexpr (T::size() == 2) {
424 | T a = permute2 <1, V_DC>(x); // high half
425 | T b = min(a, x);
426 | return b[0];
427 | }
428 | else if constexpr (T::size() == 4) {
429 | T a = permute4<2, 3, V_DC, V_DC>(x); // high half
430 | T b = min(a, x);
431 | a = permute4<1, V_DC, V_DC, V_DC>(b);
432 | b = min(a, b);
433 | return b[0];
434 | }
435 | else if constexpr (T::size() == 8) {
436 | T a = permute8<4, 5, 6, 7, V_DC, V_DC, V_DC, V_DC>(x); // high half
437 | T b = min(a, x);
438 | a = permute8<2, 3, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC>(b);
439 | b = min(a, b);
440 | a = permute8<1, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC>(b);
441 | b = min(a, b);
442 | return b[0];
443 | }
444 | else {
445 | static_assert(T::size() == 16); // no other size is allowed
446 | T a = permute16<8, 9, 10, 11, 12, 13, 14, 15, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC >(x); // high half
447 | T b = min(a, x);
448 | a = permute16<4, 5, 6, 7, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC>(b);
449 | b = min(a, b);
450 | a = permute16<2, 3, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC>(b);
451 | b = min(a, b);
452 | a = permute16<1, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC>(b);
453 | b = min(a, b);
454 | return b[0];
455 | }
456 | }
457 |
458 | template auto horizontal_max(T const x) {
459 | if constexpr ((T::elementtype() & 16) != 0) {
460 | // T is a float or double vector
461 | if (horizontal_or(is_nan(x))) {
462 | // check for NAN because max does not guarantee NAN propagation
463 | return x[horizontal_find_first(is_nan(x))];
464 | }
465 | }
466 | return horizontal_max1(x);
467 | }
468 |
469 | template auto horizontal_max1(T const x) {
470 | if constexpr (T::elementtype() <= 3) { // boolean vector type
471 | return horizontal_or(x);
472 | }
473 | else if constexpr (sizeof(T) >= 32) {
474 | // split recursively into smaller vectors
475 | return horizontal_max1(max(x.get_low(), x.get_high()));
476 | }
477 | else if constexpr (T::size() == 2) {
478 | T a = permute2 <1, V_DC>(x); // high half
479 | T b = max(a, x);
480 | return b[0];
481 | }
482 | else if constexpr (T::size() == 4) {
483 | T a = permute4<2, 3, V_DC, V_DC>(x); // high half
484 | T b = max(a, x);
485 | a = permute4<1, V_DC, V_DC, V_DC>(b);
486 | b = max(a, b);
487 | return b[0];
488 | }
489 | else if constexpr (T::size() == 8) {
490 | T a = permute8<4, 5, 6, 7, V_DC, V_DC, V_DC, V_DC>(x); // high half
491 | T b = max(a, x);
492 | a = permute8<2, 3, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC>(b);
493 | b = max(a, b);
494 | a = permute8<1, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC>(b);
495 | b = max(a, b);
496 | return b[0];
497 | }
498 | else {
499 | static_assert(T::size() == 16); // no other size is allowed
500 | T a = permute16<8, 9, 10, 11, 12, 13, 14, 15, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC >(x); // high half
501 | T b = max(a, x);
502 | a = permute16<4, 5, 6, 7, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC>(b);
503 | b = max(a, b);
504 | a = permute16<2, 3, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC>(b);
505 | b = max(a, b);
506 | a = permute16<1, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC, V_DC>(b);
507 | b = max(a, b);
508 | return b[0];
509 | }
510 | }
511 |
512 | // Find first element that is true in a boolean vector
513 | template
514 | static inline int horizontal_find_first(V const x) {
515 | static_assert(V::elementtype() == 2 || V::elementtype() == 3, "Boolean vector expected");
516 | auto bits = to_bits(x); // convert to bits
517 | if (bits == 0) return -1;
518 | if constexpr (V::size() < 32) {
519 | return bit_scan_forward((uint32_t)bits);
520 | }
521 | else {
522 | return bit_scan_forward(bits);
523 | }
524 | }
525 |
526 | // Count the number of elements that are true in a boolean vector
527 | template
528 | static inline int horizontal_count(V const x) {
529 | static_assert(V::elementtype() == 2 || V::elementtype() == 3, "Boolean vector expected");
530 | auto bits = to_bits(x); // convert to bits
531 | if constexpr (V::size() < 32) {
532 | return vml_popcnt((uint32_t)bits);
533 | }
534 | else {
535 | return (int)vml_popcnt(bits);
536 | }
537 | }
538 |
539 | // maximum and minimum functions. This version is sure to propagate NANs,
540 | // conforming to the new IEEE-754 2019 standard
541 | template
542 | static inline V maximum(V const a, V const b) {
543 | if constexpr (V::elementtype() < 16) {
544 | return max(a, b); // integer type
545 | }
546 | else { // float or double vector
547 | V y = select(is_nan(a), a, max(a, b));
548 | #ifdef SIGNED_ZERO // pedantic about signed zero
549 | y = select(a == b, a & b, y); // maximum(+0, -0) = +0
550 | #endif
551 | return y;
552 | }
553 | }
554 |
555 | template
556 | static inline V minimum(V const a, V const b) {
557 | if constexpr (V::elementtype() < 16) {
558 | return min(a, b); // integer type
559 | }
560 | else { // float or double vector
561 | V y = select(is_nan(a), a, min(a, b));
562 | #ifdef SIGNED_ZERO // pedantic about signed zero
563 | y = select(a == b, a | b, y); // minimum(+0, -0) = -0
564 | #endif
565 | return y;
566 | }
567 | }
568 |
569 |
570 | #ifdef VCL_NAMESPACE
571 | }
572 | #endif
573 |
574 | #endif // VECTOR_CONVERT_H
575 |
--------------------------------------------------------------------------------
/DFTTest/VCL2/vectormath_hyp.h:
--------------------------------------------------------------------------------
1 | /**************************** vectormath_hyp.h ******************************
2 | * Author: Agner Fog
3 | * Date created: 2014-07-09
4 | * Last modified: 2019-08-01
5 | * Version: 2.00.00
6 | * Project: vector class library
7 | * Description:
8 | * Header file containing inline vector functions of hyperbolic and inverse
9 | * hyperbolic functions:
10 | * sinh hyperbolic sine
11 | * cosh hyperbolic cosine
12 | * tanh hyperbolic tangent
13 | * asinh inverse hyperbolic sine
14 | * acosh inverse hyperbolic cosine
15 | * atanh inverse hyperbolic tangent
16 | *
17 | * Theory, methods and inspiration based partially on these sources:
18 | * > Moshier, Stephen Lloyd Baluk: Methods and programs for mathematical functions.
19 | * Ellis Horwood, 1989.
20 | * > VDT library developed on CERN by Danilo Piparo, Thomas Hauth and
21 | * Vincenzo Innocente, 2012, https://svnweb.cern.ch/trac/vdt
22 | * > Cephes math library by Stephen L. Moshier 1992,
23 | * http://www.netlib.org/cephes/
24 | *
25 | * For detailed instructions, see vectormath_common.h and vcl_manual.pdf
26 | *
27 | * (c) Copyright 2014-2019 Agner Fog.
28 | * Apache License version 2.0 or later.
29 | ******************************************************************************/
30 |
31 | #ifndef VECTORMATH_HYP_H
32 | #define VECTORMATH_HYP_H 1
33 |
34 | #include "vectormath_exp.h"
35 |
36 | #ifdef VCL_NAMESPACE
37 | namespace VCL_NAMESPACE {
38 | #endif
39 |
40 | /******************************************************************************
41 | * Hyperbolic functions
42 | ******************************************************************************/
43 |
44 | // Template for sinh function, double precision
45 | // This function does not produce denormals
46 | // Template parameters:
47 | // VTYPE: double vector type
48 | template
49 | static inline VTYPE sinh_d(VTYPE const x0) {
50 | // The limit of abs(x) is 709.7, as defined by max_x in vectormath_exp.h for 0.5*exp(x).
51 |
52 | // Coefficients
53 | const double p0 = -3.51754964808151394800E5;
54 | const double p1 = -1.15614435765005216044E4;
55 | const double p2 = -1.63725857525983828727E2;
56 | const double p3 = -7.89474443963537015605E-1;
57 |
58 | const double q0 = -2.11052978884890840399E6;
59 | const double q1 = 3.61578279834431989373E4;
60 | const double q2 = -2.77711081420602794433E2;
61 | const double q3 = 1.0;
62 |
63 | // data vectors
64 | VTYPE x, x2, y1, y2;
65 |
66 | x = abs(x0);
67 | auto x_small = x <= 1.0; // use Pade approximation if abs(x) <= 1
68 |
69 | if (horizontal_or(x_small)) {
70 | // At least one element needs small method
71 | x2 = x*x;
72 | y1 = polynomial_3(x2, p0, p1, p2, p3) / polynomial_3(x2, q0, q1, q2, q3);
73 | y1 = mul_add(y1, x*x2, x); // y1 = x + x2*(x*y1);
74 | }
75 | if (!horizontal_and(x_small)) {
76 | // At least one element needs big method
77 | y2 = exp_d(x); // 0.5 * exp(x)
78 | y2 -= 0.25 / y2; // - 0.5 * exp(-x)
79 | }
80 | y1 = select(x_small, y1, y2); // choose method
81 | y1 = sign_combine(y1, x0); // get original sign
82 | // you can avoid the sign_combine by replacing x by x0 above, but at a loss of precision
83 |
84 | return y1;
85 | }
86 |
87 | // instances of sinh_d template
88 | static inline Vec2d sinh(Vec2d const x) {
89 | return sinh_d(x);
90 | }
91 |
92 | #if MAX_VECTOR_SIZE >= 256
93 | static inline Vec4d sinh(Vec4d const x) {
94 | return sinh_d(x);
95 | }
96 | #endif // MAX_VECTOR_SIZE >= 256
97 |
98 | #if MAX_VECTOR_SIZE >= 512
99 | static inline Vec8d sinh(Vec8d const x) {
100 | return sinh_d(x);
101 | }
102 | #endif // MAX_VECTOR_SIZE >= 512
103 |
104 |
105 | // Template for sinh function, single precision
106 | // This function does not produce denormals
107 | // Template parameters:
108 | // VTYPE: double vector type
109 | template
110 | static inline VTYPE sinh_f(VTYPE const x0) {
111 | // The limit of abs(x) is 89.0, as defined by max_x in vectormath_exp.h for 0.5*exp(x).
112 |
113 | // Coefficients
114 | const float r0 = 1.66667160211E-1f;
115 | const float r1 = 8.33028376239E-3f;
116 | const float r2 = 2.03721912945E-4f;
117 |
118 | // data vectors
119 | VTYPE x, x2, y1, y2;
120 |
121 | x = abs(x0);
122 | auto x_small = x <= 1.0f; // use polynomial approximation if abs(x) <= 1
123 |
124 | if (horizontal_or(x_small)) {
125 | // At least one element needs small method
126 | x2 = x*x;
127 | y1 = polynomial_2(x2, r0, r1, r2);
128 | y1 = mul_add(y1, x2*x, x); // y1 = x + x2*(x*y1);
129 | }
130 | if (!horizontal_and(x_small)) {
131 | // At least one element needs big method
132 | y2 = exp_f(x); // 0.5 * exp(x)
133 | y2 -= 0.25f / y2; // - 0.5 * exp(-x)
134 | }
135 | y1 = select(x_small, y1, y2); // choose method
136 | y1 = sign_combine(y1, x0); // get original sign
137 | // you can avoid the sign_combine by replacing x by x0 above, but at a loss of precision
138 |
139 | return y1;
140 | }
141 |
142 | // instances of sinh_f template
143 | static inline Vec4f sinh(Vec4f const x) {
144 | return sinh_f(x);
145 | }
146 |
147 | #if MAX_VECTOR_SIZE >= 256
148 | static inline Vec8f sinh(Vec8f const x) {
149 | return sinh_f(x);
150 | }
151 | #endif // MAX_VECTOR_SIZE >= 256
152 |
153 | #if MAX_VECTOR_SIZE >= 512
154 | static inline Vec16f sinh(Vec16f const x) {
155 | return sinh_f(x);
156 | }
157 | #endif // MAX_VECTOR_SIZE >= 512
158 |
159 |
160 | // Template for cosh function, double precision
161 | // This function does not produce denormals
162 | // Template parameters:
163 | // VTYPE: double vector type
164 | template
165 | static inline VTYPE cosh_d(VTYPE const x0) {
166 | // The limit of abs(x) is 709.7, as defined by max_x in vectormath_exp.h for 0.5*exp(x).
167 |
168 | // data vectors
169 | VTYPE x, y;
170 | x = abs(x0);
171 | y = exp_d(x); // 0.5 * exp(x)
172 | y += 0.25 / y; // + 0.5 * exp(-x)
173 | return y;
174 | }
175 |
176 | // instances of sinh_d template
177 | static inline Vec2d cosh(Vec2d const x) {
178 | return cosh_d(x);
179 | }
180 |
181 | #if MAX_VECTOR_SIZE >= 256
182 | static inline Vec4d cosh(Vec4d const x) {
183 | return cosh_d(x);
184 | }
185 | #endif // MAX_VECTOR_SIZE >= 256
186 |
187 | #if MAX_VECTOR_SIZE >= 512
188 | static inline Vec8d cosh(Vec8d const x) {
189 | return cosh_d(x);
190 | }
191 | #endif // MAX_VECTOR_SIZE >= 512
192 |
193 |
194 | // Template for cosh function, single precision
195 | // This function does not produce denormals
196 | // Template parameters:
197 | // VTYPE: double vector type
198 | template
199 | static inline VTYPE cosh_f(VTYPE const x0) {
200 | // The limit of abs(x) is 89.0, as defined by max_x in vectormath_exp.h for 0.5*exp(x).
201 |
202 | // data vectors
203 | VTYPE x, y;
204 | x = abs(x0);
205 | y = exp_f(x); // 0.5 * exp(x)
206 | y += 0.25f / y; // + 0.5 * exp(-x)
207 | return y;
208 | }
209 |
210 | // instances of sinh_d template
211 | static inline Vec4f cosh(Vec4f const x) {
212 | return cosh_f(x);
213 | }
214 |
215 | #if MAX_VECTOR_SIZE >= 256
216 | static inline Vec8f cosh(Vec8f const x) {
217 | return cosh_f(x);
218 | }
219 | #endif // MAX_VECTOR_SIZE >= 256
220 |
221 | #if MAX_VECTOR_SIZE >= 512
222 | static inline Vec16f cosh(Vec16f const x) {
223 | return cosh_f(x);
224 | }
225 | #endif // MAX_VECTOR_SIZE >= 512
226 |
227 |
228 | // Template for tanh function, double precision
229 | // This function does not produce denormals
230 | // Template parameters:
231 | // VTYPE: double vector type
232 | template
233 | static inline VTYPE tanh_d(VTYPE const x0) {
234 |
235 | // Coefficients
236 | const double p0 = -1.61468768441708447952E3;
237 | const double p1 = -9.92877231001918586564E1;
238 | const double p2 = -9.64399179425052238628E-1;
239 |
240 | const double q0 = 4.84406305325125486048E3;
241 | const double q1 = 2.23548839060100448583E3;
242 | const double q2 = 1.12811678491632931402E2;
243 | const double q3 = 1.0;
244 |
245 | // data vectors
246 | VTYPE x, x2, y1, y2;
247 |
248 | x = abs(x0);
249 | auto x_small = x <= 0.625; // use Pade approximation if abs(x) <= 5/8
250 |
251 | if (horizontal_or(x_small)) {
252 | // At least one element needs small method
253 | x2 = x*x;
254 | y1 = polynomial_2(x2, p0, p1, p2) / polynomial_3(x2, q0, q1, q2, q3);
255 | y1 = mul_add(y1, x2*x, x); // y1 = x + x2*(x*y1);
256 | }
257 | if (!horizontal_and(x_small)) {
258 | // At least one element needs big method
259 | y2 = exp(x+x); // exp(2*x)
260 | y2 = 1.0 - 2.0 / (y2 + 1.0); // tanh(x)
261 | }
262 | auto x_big = x > 350.;
263 | y1 = select(x_small, y1, y2); // choose method
264 | y1 = select(x_big, 1.0, y1); // avoid overflow
265 | y1 = sign_combine(y1, x0); // get original sign
266 | return y1;
267 | }
268 |
269 | // instances of tanh_d template
270 | static inline Vec2d tanh(Vec2d const x) {
271 | return tanh_d(x);
272 | }
273 |
274 | #if MAX_VECTOR_SIZE >= 256
275 | static inline Vec4d tanh(Vec4d const x) {
276 | return tanh_d(x);
277 | }
278 | #endif // MAX_VECTOR_SIZE >= 256
279 |
280 | #if MAX_VECTOR_SIZE >= 512
281 | static inline Vec8d tanh(Vec8d const x) {
282 | return tanh_d(x);
283 | }
284 | #endif // MAX_VECTOR_SIZE >= 512
285 |
286 |
287 | // Template for tanh function, single precision
288 | // This function does not produce denormals
289 | // Template parameters:
290 | // VTYPE: double vector type
291 | template
292 | static inline VTYPE tanh_f(VTYPE const x0) {
293 | // The limit of abs(x) is 89.0, as defined by max_x in vectormath_exp.h for 0.5*exp(x).
294 |
295 | // Coefficients
296 | const float r0 = -3.33332819422E-1f;
297 | const float r1 = 1.33314422036E-1f;
298 | const float r2 = -5.37397155531E-2f;
299 | const float r3 = 2.06390887954E-2f;
300 | const float r4 = -5.70498872745E-3f;
301 |
302 | // data vectors
303 | VTYPE x, x2, y1, y2;
304 |
305 | x = abs(x0);
306 | auto x_small = x <= 0.625f; // use polynomial approximation if abs(x) <= 5/8
307 |
308 | if (horizontal_or(x_small)) {
309 | // At least one element needs small method
310 | x2 = x*x;
311 | y1 = polynomial_4(x2, r0, r1, r2, r3, r4);
312 | y1 = mul_add(y1, x2*x, x); // y1 = x + (x2*x)*y1;
313 | }
314 | if (!horizontal_and(x_small)) {
315 | // At least one element needs big method
316 | y2 = exp(x+x); // exp(2*x)
317 | y2 = 1.0f - 2.0f / (y2 + 1.0f); // tanh(x)
318 | }
319 | auto x_big = x > 44.4f;
320 | y1 = select(x_small, y1, y2); // choose method
321 | y1 = select(x_big, 1.0f, y1); // avoid overflow
322 | y1 = sign_combine(y1, x0); // get original sign
323 | return y1;
324 | }
325 |
326 | // instances of tanh_f template
327 | static inline Vec4f tanh(Vec4f const x) {
328 | return tanh_f(x);
329 | }
330 |
331 | #if MAX_VECTOR_SIZE >= 256
332 | static inline Vec8f tanh(Vec8f const x) {
333 | return tanh_f(x);
334 | }
335 | #endif // MAX_VECTOR_SIZE >= 256
336 |
337 | #if MAX_VECTOR_SIZE >= 512
338 | static inline Vec16f tanh(Vec16f const x) {
339 | return tanh_f(x);
340 | }
341 | #endif // MAX_VECTOR_SIZE >= 512
342 |
343 |
344 |
345 | /******************************************************************************
346 | * Inverse hyperbolic functions
347 | ******************************************************************************/
348 |
349 | // Template for asinh function, double precision
350 | // This function does not produce denormals
351 | // Template parameters:
352 | // VTYPE: double vector type
353 | template
354 | static inline VTYPE asinh_d(VTYPE const x0) {
355 |
356 | // Coefficients
357 | const double p0 = -5.56682227230859640450E0;
358 | const double p1 = -9.09030533308377316566E0;
359 | const double p2 = -4.37390226194356683570E0;
360 | const double p3 = -5.91750212056387121207E-1;
361 | const double p4 = -4.33231683752342103572E-3;
362 |
363 | const double q0 = 3.34009336338516356383E1;
364 | const double q1 = 6.95722521337257608734E1;
365 | const double q2 = 4.86042483805291788324E1;
366 | const double q3 = 1.28757002067426453537E1;
367 | const double q4 = 1.0;
368 |
369 | // data vectors
370 | VTYPE x, x2, y1, y2;
371 |
372 | x2 = x0 * x0;
373 | x = abs(x0);
374 | auto x_small = x <= 0.533; // use Pade approximation if abs(x) <= 0.5
375 | // Both methods give the highest error close to 0.5.
376 | // This limit is adjusted for minimum error
377 | auto x_huge = x > 1.E20; // simple approximation, avoid overflow
378 |
379 | if (horizontal_or(x_small)) {
380 | // At least one element needs small method
381 | y1 = polynomial_4(x2, p0, p1, p2, p3, p4) / polynomial_4(x2, q0, q1, q2, q3, q4);
382 | y1 = mul_add(y1, x2*x, x); // y1 = x + (x2*x)*y1;
383 | }
384 | if (!horizontal_and(x_small)) {
385 | // At least one element needs big method
386 | y2 = log(x + sqrt(x2 + 1.0));
387 | if (horizontal_or(x_huge)) {
388 | // At least one element needs huge method to avoid overflow
389 | y2 = select(x_huge, log(x) + VM_LN2, y2);
390 | }
391 | }
392 | y1 = select(x_small, y1, y2); // choose method
393 | y1 = sign_combine(y1, x0); // get original sign
394 | return y1;
395 | }
396 |
397 | // instances of asinh_d template
398 | static inline Vec2d asinh(Vec2d const x) {
399 | return asinh_d(x);
400 | }
401 |
402 | #if MAX_VECTOR_SIZE >= 256
403 | static inline Vec4d asinh(Vec4d const x) {
404 | return asinh_d(x);
405 | }
406 | #endif // MAX_VECTOR_SIZE >= 256
407 |
408 | #if MAX_VECTOR_SIZE >= 512
409 | static inline Vec8d asinh(Vec8d const x) {
410 | return asinh_d(x);
411 | }
412 | #endif // MAX_VECTOR_SIZE >= 512
413 |
414 |
415 | // Template for asinh function, single precision
416 | // This function does not produce denormals
417 | // Template parameters:
418 | // VTYPE: double vector type
419 | template
420 | static inline VTYPE asinh_f(VTYPE const x0) {
421 |
422 | // Coefficients
423 | const float r0 = -1.6666288134E-1f;
424 | const float r1 = 7.4847586088E-2f;
425 | const float r2 = -4.2699340972E-2f;
426 | const float r3 = 2.0122003309E-2f;
427 |
428 | // data vectors
429 | VTYPE x, x2, y1, y2;
430 |
431 | x2 = x0 * x0;
432 | x = abs(x0);
433 | auto x_small = x <= 0.51f; // use polynomial approximation if abs(x) <= 0.5
434 | auto x_huge = x > 1.E10f; // simple approximation, avoid overflow
435 |
436 | if (horizontal_or(x_small)) {
437 | // At least one element needs small method
438 | y1 = polynomial_3(x2, r0, r1, r2, r3);
439 | y1 = mul_add(y1, x2*x, x); // y1 = x + (x2*x)*y1;
440 | }
441 | if (!horizontal_and(x_small)) {
442 | // At least one element needs big method
443 | y2 = log(x + sqrt(x2 + 1.0f));
444 | if (horizontal_or(x_huge)) {
445 | // At least one element needs huge method to avoid overflow
446 | y2 = select(x_huge, log(x) + (float)VM_LN2, y2);
447 | }
448 | }
449 | y1 = select(x_small, y1, y2); // choose method
450 | y1 = sign_combine(y1, x0); // get original sign
451 | return y1;
452 | }
453 |
454 | // instances of asinh_f template
455 | static inline Vec4f asinh(Vec4f const x) {
456 | return asinh_f(x);
457 | }
458 |
459 | #if MAX_VECTOR_SIZE >= 256
460 | static inline Vec8f asinh(Vec8f const x) {
461 | return asinh_f(x);
462 | }
463 | #endif // MAX_VECTOR_SIZE >= 256
464 |
465 | #if MAX_VECTOR_SIZE >= 512
466 | static inline Vec16f asinh(Vec16f const x) {
467 | return asinh_f(x);
468 | }
469 | #endif // MAX_VECTOR_SIZE >= 512
470 |
471 |
472 | // Template for acosh function, double precision
473 | // This function does not produce denormals
474 | // Template parameters:
475 | // VTYPE: double vector type
476 | template
477 | static inline VTYPE acosh_d(VTYPE const x0) {
478 |
479 | // Coefficients
480 | const double p0 = 1.10855947270161294369E5;
481 | const double p1 = 1.08102874834699867335E5;
482 | const double p2 = 3.43989375926195455866E4;
483 | const double p3 = 3.94726656571334401102E3;
484 | const double p4 = 1.18801130533544501356E2;
485 |
486 | const double q0 = 7.83869920495893927727E4;
487 | const double q1 = 8.29725251988426222434E4;
488 | const double q2 = 2.97683430363289370382E4;
489 | const double q3 = 4.15352677227719831579E3;
490 | const double q4 = 1.86145380837903397292E2;
491 | const double q5 = 1.0;
492 |
493 | // data vectors
494 | VTYPE x1, y1, y2;
495 |
496 | x1 = x0 - 1.0;
497 | auto undef = x0 < 1.0; // result is NAN
498 | auto x_small = x1 < 0.49; // use Pade approximation if abs(x-1) < 0.5
499 | auto x_huge = x1 > 1.E20; // simple approximation, avoid overflow
500 |
501 | if (horizontal_or(x_small)) {
502 | // At least one element needs small method
503 | y1 = sqrt(x1) * (polynomial_4(x1, p0, p1, p2, p3, p4) / polynomial_5(x1, q0, q1, q2, q3, q4, q5));
504 | // x < 1 generates NAN
505 | y1 = select(undef, nan_vec(NAN_HYP), y1);
506 | }
507 | if (!horizontal_and(x_small)) {
508 | // At least one element needs big method
509 | y2 = log(x0 + sqrt(mul_sub(x0,x0,1.0)));
510 | if (horizontal_or(x_huge)) {
511 | // At least one element needs huge method to avoid overflow
512 | y2 = select(x_huge, log(x0) + VM_LN2, y2);
513 | }
514 | }
515 | y1 = select(x_small, y1, y2); // choose method
516 | return y1;
517 | }
518 |
519 | // instances of acosh_d template
520 | static inline Vec2d acosh(Vec2d const x) {
521 | return acosh_d(x);
522 | }
523 |
524 | #if MAX_VECTOR_SIZE >= 256
525 | static inline Vec4d acosh(Vec4d const x) {
526 | return acosh_d(x);
527 | }
528 | #endif // MAX_VECTOR_SIZE >= 256
529 |
530 | #if MAX_VECTOR_SIZE >= 512
531 | static inline Vec8d acosh(Vec8d const x) {
532 | return acosh_d(x);
533 | }
534 | #endif // MAX_VECTOR_SIZE >= 512
535 |
536 |
537 | // Template for acosh function, single precision
538 | // This function does not produce denormals
539 | // Template parameters:
540 | // VTYPE: double vector type
541 | template
542 | static inline VTYPE acosh_f(VTYPE const x0) {
543 |
544 | // Coefficients
545 | const float r0 = 1.4142135263E0f;
546 | const float r1 = -1.1784741703E-1f;
547 | const float r2 = 2.6454905019E-2f;
548 | const float r3 = -7.5272886713E-3f;
549 | const float r4 = 1.7596881071E-3f;
550 |
551 | // data vectors
552 | VTYPE x1, y1, y2;
553 |
554 | x1 = x0 - 1.0f;
555 | auto undef = x0 < 1.0f; // result is NAN
556 | auto x_small = x1 < 0.49f; // use Pade approximation if abs(x-1) < 0.5
557 | auto x_huge = x1 > 1.E10f; // simple approximation, avoid overflow
558 |
559 | if (horizontal_or(x_small)) {
560 | // At least one element needs small method
561 | y1 = sqrt(x1) * polynomial_4(x1, r0, r1, r2, r3, r4);
562 | // x < 1 generates NAN
563 | y1 = select(undef, nan_vec(NAN_HYP), y1);
564 | }
565 | if (!horizontal_and(x_small)) {
566 | // At least one element needs big method
567 | y2 = log(x0 + sqrt(mul_sub(x0,x0,1.0)));
568 | if (horizontal_or(x_huge)) {
569 | // At least one element needs huge method to avoid overflow
570 | y2 = select(x_huge, log(x0) + (float)VM_LN2, y2);
571 | }
572 | }
573 | y1 = select(x_small, y1, y2); // choose method
574 | return y1;
575 | }
576 |
577 | // instances of acosh_f template
578 | static inline Vec4f acosh(Vec4f const x) {
579 | return acosh_f(x);
580 | }
581 |
582 | #if MAX_VECTOR_SIZE >= 256
583 | static inline Vec8f acosh(Vec8f const x) {
584 | return acosh_f(x);
585 | }
586 | #endif // MAX_VECTOR_SIZE >= 256
587 |
588 | #if MAX_VECTOR_SIZE >= 512
589 | static inline Vec16f acosh(Vec16f const x) {
590 | return acosh_f(x);
591 | }
592 | #endif // MAX_VECTOR_SIZE >= 512
593 |
594 |
595 | // Template for atanh function, double precision
596 | // This function does not produce denormals
597 | // Template parameters:
598 | // VTYPE: double vector type
599 | template
600 | static inline VTYPE atanh_d(VTYPE const x0) {
601 |
602 | // Coefficients
603 | const double p0 = -3.09092539379866942570E1;
604 | const double p1 = 6.54566728676544377376E1;
605 | const double p2 = -4.61252884198732692637E1;
606 | const double p3 = 1.20426861384072379242E1;
607 | const double p4 = -8.54074331929669305196E-1;
608 |
609 | const double q0 = -9.27277618139601130017E1;
610 | const double q1 = 2.52006675691344555838E2;
611 | const double q2 = -2.49839401325893582852E2;
612 | const double q3 = 1.08938092147140262656E2;
613 | const double q4 = -1.95638849376911654834E1;
614 | const double q5 = 1.0;
615 |
616 | // data vectors
617 | VTYPE x, x2, y1, y2, y3;
618 |
619 | x = abs(x0);
620 | auto x_small = x < 0.5; // use Pade approximation if abs(x) < 0.5
621 |
622 | if (horizontal_or(x_small)) {
623 | // At least one element needs small method
624 | x2 = x * x;
625 | y1 = polynomial_4(x2, p0, p1, p2, p3, p4) / polynomial_5(x2, q0, q1, q2, q3, q4, q5);
626 | y1 = mul_add(y1, x2*x, x);
627 | }
628 | if (!horizontal_and(x_small)) {
629 | // At least one element needs big method
630 | y2 = log((1.0+x)/(1.0-x)) * 0.5;
631 | // check if out of range
632 | y3 = select(x == 1.0, infinite_vec(), nan_vec(NAN_HYP));
633 | y2 = select(x >= 1.0, y3, y2);
634 | }
635 | y1 = select(x_small, y1, y2); // choose method
636 | y1 = sign_combine(y1, x0); // get original sign
637 | return y1;
638 | }
639 |
640 | // instances of atanh_d template
641 | static inline Vec2d atanh(Vec2d const x) {
642 | return atanh_d(x);
643 | }
644 |
645 | #if MAX_VECTOR_SIZE >= 256
646 | static inline Vec4d atanh(Vec4d const x) {
647 | return atanh_d(x);
648 | }
649 | #endif // MAX_VECTOR_SIZE >= 256
650 |
651 | #if MAX_VECTOR_SIZE >= 512
652 | static inline Vec8d atanh(Vec8d const x) {
653 | return atanh_d(x);
654 | }
655 | #endif // MAX_VECTOR_SIZE >= 512
656 |
657 |
658 | // Template for atanh function, single precision
659 | // This function does not produce denormals
660 | // Template parameters:
661 | // VTYPE: double vector type
662 | template
663 | static inline VTYPE atanh_f(VTYPE const x0) {
664 |
665 | // Coefficients
666 | const float r0 = 3.33337300303E-1f;
667 | const float r1 = 1.99782164500E-1f;
668 | const float r2 = 1.46691431730E-1f;
669 | const float r3 = 8.24370301058E-2f;
670 | const float r4 = 1.81740078349E-1f;
671 |
672 | // data vectors
673 | VTYPE x, x2, y1, y2, y3;
674 |
675 | x = abs(x0);
676 | auto x_small = x < 0.5f; // use polynomial approximation if abs(x) < 0.5
677 |
678 | if (horizontal_or(x_small)) {
679 | // At least one element needs small method
680 | x2 = x * x;
681 | y1 = polynomial_4(x2, r0, r1, r2, r3, r4);
682 | y1 = mul_add(y1, x2*x, x);
683 | }
684 | if (!horizontal_and(x_small)) {
685 | // At least one element needs big method
686 | y2 = log((1.0f+x)/(1.0f-x)) * 0.5f;
687 | // check if out of range
688 | y3 = select(x == 1.0f, infinite_vec(), nan_vec(NAN_HYP));
689 | y2 = select(x >= 1.0f, y3, y2);
690 | }
691 | y1 = select(x_small, y1, y2); // choose method
692 | y1 = sign_combine(y1, x0); // get original sign
693 | return y1;
694 | }
695 |
696 | // instances of atanh_f template
697 | static inline Vec4f atanh(Vec4f const x) {
698 | return atanh_f(x);
699 | }
700 |
701 | #if MAX_VECTOR_SIZE >= 256
702 | static inline Vec8f atanh(Vec8f const x) {
703 | return atanh_f(x);
704 | }
705 | #endif // MAX_VECTOR_SIZE >= 256
706 |
707 | #if MAX_VECTOR_SIZE >= 512
708 | static inline Vec16f atanh(Vec16f const x) {
709 | return atanh_f(x);
710 | }
711 | #endif // MAX_VECTOR_SIZE >= 512
712 |
713 | #ifdef VCL_NAMESPACE
714 | }
715 | #endif
716 |
717 | #endif
718 |
--------------------------------------------------------------------------------
/DFTTest/VCL2/vectormath_trig.h:
--------------------------------------------------------------------------------
1 | /**************************** vectormath_trig.h ******************************
2 | * Author: Agner Fog
3 | * Date created: 2014-04-18
4 | * Last modified: 2020-06-08
5 | * Version: 2.00.03
6 | * Project: vector class library
7 | * Description:
8 | * Header file containing inline version of trigonometric functions
9 | * and inverse trigonometric functions
10 | * sin, cos, sincos, tan
11 | * asin, acos, atan, atan2
12 | *
13 | * Theory, methods and inspiration based partially on these sources:
14 | * > Moshier, Stephen Lloyd Baluk: Methods and programs for mathematical functions.
15 | * Ellis Horwood, 1989.
16 | * > VDT library developed on CERN by Danilo Piparo, Thomas Hauth and
17 | * Vincenzo Innocente, 2012, https://svnweb.cern.ch/trac/vdt
18 | * > Cephes math library by Stephen L. Moshier 1992,
19 | * http://www.netlib.org/cephes/
20 | *
21 | * For detailed instructions, see vectormath_common.h and vcl_manual.pdf
22 | *
23 | * (c) Copyright 2014-2020 Agner Fog.
24 | * Apache License version 2.0 or later.
25 | ******************************************************************************/
26 |
27 | #ifndef VECTORMATH_TRIG_H
28 | #define VECTORMATH_TRIG_H 1
29 |
30 | #include "vectormath_common.h"
31 |
32 | #ifdef VCL_NAMESPACE
33 | namespace VCL_NAMESPACE {
34 | #endif
35 |
36 |
37 | // *************************************************************
38 | // sin/cos template, double precision
39 | // *************************************************************
40 | // Template parameters:
41 | // VTYPE: f.p. vector type
42 | // SC: 1 = sin, 2 = cos, 3 = sincos
43 | // Paramterers:
44 | // xx = input x (radians)
45 | // cosret = return pointer (only if SC = 3)
46 | template
47 | static inline VTYPE sincos_d(VTYPE * cosret, VTYPE const xx) {
48 |
49 | // define constants
50 | const double P0sin = -1.66666666666666307295E-1;
51 | const double P1sin = 8.33333333332211858878E-3;
52 | const double P2sin = -1.98412698295895385996E-4;
53 | const double P3sin = 2.75573136213857245213E-6;
54 | const double P4sin = -2.50507477628578072866E-8;
55 | const double P5sin = 1.58962301576546568060E-10;
56 |
57 | const double P0cos = 4.16666666666665929218E-2;
58 | const double P1cos = -1.38888888888730564116E-3;
59 | const double P2cos = 2.48015872888517045348E-5;
60 | const double P3cos = -2.75573141792967388112E-7;
61 | const double P4cos = 2.08757008419747316778E-9;
62 | const double P5cos = -1.13585365213876817300E-11;
63 |
64 | const double DP1 = 7.853981554508209228515625E-1 * 2.;
65 | const double DP2 = 7.94662735614792836714E-9 * 2.;
66 | const double DP3 = 3.06161699786838294307E-17 * 2.;
67 | /*
68 | const double DP1sc = 7.85398125648498535156E-1;
69 | const double DP2sc = 3.77489470793079817668E-8;
70 | const double DP3sc = 2.69515142907905952645E-15;
71 | */
72 | typedef decltype(roundi(xx)) ITYPE; // integer vector type
73 | typedef decltype(nan_code(xx)) UITYPE; // unsigned integer vector type
74 | typedef decltype(xx < xx) BVTYPE; // boolean vector type
75 |
76 | VTYPE xa, x, y, x2, s, c, sin1, cos1; // data vectors
77 | ITYPE q, qq, signsin, signcos; // integer vectors, 64 bit
78 |
79 | BVTYPE swap, overflow; // boolean vectors
80 |
81 | xa = abs(xx);
82 |
83 | // Find quadrant
84 | y = round(xa * (double)(2. / VM_PI)); // quadrant, as float
85 | q = roundi(y); // quadrant, as integer
86 | // Find quadrant
87 | // 0 - pi/4 => 0
88 | // pi/4 - 3*pi/4 => 1
89 | // 3*pi/4 - 5*pi/4 => 2
90 | // 5*pi/4 - 7*pi/4 => 3
91 | // 7*pi/4 - 8*pi/4 => 4
92 |
93 | // Reduce by extended precision modular arithmetic
94 | x = nmul_add(y, DP3, nmul_add(y, DP2, nmul_add(y, DP1, xa))); // x = ((xa - y * DP1) - y * DP2) - y * DP3;
95 |
96 | // Expansion of sin and cos, valid for -pi/4 <= x <= pi/4
97 | x2 = x * x;
98 | s = polynomial_5(x2, P0sin, P1sin, P2sin, P3sin, P4sin, P5sin);
99 | c = polynomial_5(x2, P0cos, P1cos, P2cos, P3cos, P4cos, P5cos);
100 | s = mul_add(x * x2, s, x); // s = x + (x * x2) * s;
101 | c = mul_add(x2 * x2, c, nmul_add(x2, 0.5, 1.0)); // c = 1.0 - x2 * 0.5 + (x2 * x2) * c;
102 |
103 | // swap sin and cos if odd quadrant
104 | swap = BVTYPE((q & 1) != 0);
105 |
106 | // check for overflow
107 | overflow = BVTYPE(UITYPE(q) > 0x80000000000000); // q big if overflow
108 | overflow &= is_finite(xa);
109 | s = select(overflow, 0.0, s);
110 | c = select(overflow, 1.0, c);
111 |
112 | if constexpr ((SC & 1) != 0) { // calculate sin
113 | sin1 = select(swap, c, s);
114 | signsin = ((q << 62) ^ ITYPE(reinterpret_i(xx)));
115 | sin1 = sign_combine(sin1, reinterpret_d(signsin));
116 | }
117 | if constexpr ((SC & 2) != 0) { // calculate cos
118 | cos1 = select(swap, s, c);
119 | signcos = ((q + 1) & 2) << 62;
120 | cos1 ^= reinterpret_d(signcos);
121 | }
122 | if constexpr (SC == 3) { // calculate both. cos returned through pointer
123 | *cosret = cos1;
124 | }
125 | if constexpr ((SC & 1) != 0) return sin1; else return cos1;
126 | }
127 |
128 | // instantiations of sincos_d template:
129 |
130 | static inline Vec2d sin(Vec2d const x) {
131 | return sincos_d(0, x);
132 | }
133 |
134 | static inline Vec2d cos(Vec2d const x) {
135 | return sincos_d(0, x);
136 | }
137 |
138 | static inline Vec2d sincos(Vec2d * cosret, Vec2d const x) {
139 | return sincos_d(cosret, x);
140 | }
141 |
142 | #if MAX_VECTOR_SIZE >= 256
143 | static inline Vec4d sin(Vec4d const x) {
144 | return sincos_d(0, x);
145 | }
146 |
147 | static inline Vec4d cos(Vec4d const x) {
148 | return sincos_d(0, x);
149 | }
150 |
151 | static inline Vec4d sincos(Vec4d * cosret, Vec4d const x) {
152 | return sincos_d(cosret, x);
153 | }
154 | #endif // MAX_VECTOR_SIZE >= 256
155 |
156 | #if MAX_VECTOR_SIZE >= 512
157 | static inline Vec8d sin(Vec8d const x) {
158 | return sincos_d(0, x);
159 | }
160 |
161 | static inline Vec8d cos(Vec8d const x) {
162 | return sincos_d(0, x);
163 | }
164 |
165 | static inline Vec8d sincos(Vec8d * cosret, Vec8d const x) {
166 | return sincos_d(cosret, x);
167 | }
168 | #endif // MAX_VECTOR_SIZE >= 512
169 |
170 |
171 | // *************************************************************
172 | // sincos template, single precision
173 | // *************************************************************
174 | // Template parameters:
175 | // VTYPE: f.p. vector type
176 | // SC: 1 = sin, 2 = cos, 3 = sincos, 4 = tan
177 | // Paramterers:
178 | // xx = input x (radians)
179 | // cosret = return pointer (only if SC = 3)
180 | template
181 | static inline VTYPE sincos_f(VTYPE * cosret, VTYPE const xx) {
182 |
183 | // define constants
184 | const float DP1F = 0.78515625f * 2.f;
185 | const float DP2F = 2.4187564849853515625E-4f * 2.f;
186 | const float DP3F = 3.77489497744594108E-8f * 2.f;
187 |
188 | const float P0sinf = -1.6666654611E-1f;
189 | const float P1sinf = 8.3321608736E-3f;
190 | const float P2sinf = -1.9515295891E-4f;
191 |
192 | const float P0cosf = 4.166664568298827E-2f;
193 | const float P1cosf = -1.388731625493765E-3f;
194 | const float P2cosf = 2.443315711809948E-5f;
195 |
196 | typedef decltype(roundi(xx)) ITYPE; // integer vector type
197 | typedef decltype(nan_code(xx)) UITYPE; // unsigned integer vector type
198 | typedef decltype(xx < xx) BVTYPE; // boolean vector type
199 |
200 | VTYPE xa, x, y, x2, s, c, sin1, cos1; // data vectors
201 | ITYPE q, signsin, signcos; // integer vectors
202 | BVTYPE swap, overflow; // boolean vectors
203 |
204 | xa = abs(xx);
205 |
206 | // Find quadrant
207 | y = round(xa * (float)(2. / VM_PI)); // quadrant, as float
208 | q = roundi(y); // quadrant, as integer
209 | // 0 - pi/4 => 0
210 | // pi/4 - 3*pi/4 => 1
211 | // 3*pi/4 - 5*pi/4 => 2
212 | // 5*pi/4 - 7*pi/4 => 3
213 | // 7*pi/4 - 8*pi/4 => 4
214 |
215 | // Reduce by extended precision modular arithmetic
216 | // x = ((xa - y * DP1F) - y * DP2F) - y * DP3F;
217 | x = nmul_add(y, DP3F, nmul_add(y, DP2F, nmul_add(y, DP1F, xa)));
218 |
219 | // A two-step reduction saves time at the cost of precision for very big x:
220 | //x = (xa - y * DP1F) - y * (DP2F+DP3F);
221 |
222 | // Taylor expansion of sin and cos, valid for -pi/4 <= x <= pi/4
223 | x2 = x * x;
224 | s = polynomial_2(x2, P0sinf, P1sinf, P2sinf) * (x*x2) + x;
225 | c = polynomial_2(x2, P0cosf, P1cosf, P2cosf) * (x2*x2) + nmul_add(0.5f, x2, 1.0f);
226 |
227 | // swap sin and cos if odd quadrant
228 | swap = BVTYPE((q & 1) != 0);
229 |
230 | // check for overflow
231 | overflow = BVTYPE(UITYPE(q) > 0x2000000); // q big if overflow
232 | overflow &= is_finite(xa);
233 | s = select(overflow, 0.0f, s);
234 | c = select(overflow, 1.0f, c);
235 |
236 | if constexpr ((SC & 5) != 0) { // calculate sin
237 | sin1 = select(swap, c, s);
238 | signsin = ((q << 30) ^ ITYPE(reinterpret_i(xx)));
239 | sin1 = sign_combine(sin1, reinterpret_f(signsin));
240 | }
241 | if constexpr ((SC & 6) != 0) { // calculate cos
242 | cos1 = select(swap, s, c);
243 | signcos = ((q + 1) & 2) << 30;
244 | cos1 ^= reinterpret_f(signcos);
245 | }
246 | if constexpr (SC == 1) return sin1;
247 | else if constexpr (SC == 2) return cos1;
248 | else if constexpr (SC == 3) { // calculate both. cos returned through pointer
249 | *cosret = cos1;
250 | return sin1;
251 | }
252 | else { // SC == 4. tan
253 | return sin1 / cos1;
254 | }
255 | }
256 |
257 | // instantiations of sincos_f template:
258 |
259 | static inline Vec4f sin(Vec4f const x) {
260 | return sincos_f(0, x);
261 | }
262 |
263 | static inline Vec4f cos(Vec4f const x) {
264 | return sincos_f(0, x);
265 | }
266 |
267 | static inline Vec4f sincos(Vec4f * cosret, Vec4f const x) {
268 | return sincos_f(cosret, x);
269 | }
270 |
271 | static inline Vec4f tan(Vec4f const x) {
272 | return sincos_f(0, x);
273 | }
274 |
275 | #if MAX_VECTOR_SIZE >= 256
276 | static inline Vec8f sin(Vec8f const x) {
277 | return sincos_f(0, x);
278 | }
279 |
280 | static inline Vec8f cos(Vec8f const x) {
281 | return sincos_f(0, x);
282 | }
283 |
284 | static inline Vec8f sincos(Vec8f * cosret, Vec8f const x) {
285 | return sincos_f(cosret, x);
286 | }
287 |
288 | static inline Vec8f tan(Vec8f const x) {
289 | return sincos_f(0, x);
290 | }
291 | #endif // MAX_VECTOR_SIZE >= 256
292 |
293 | #if MAX_VECTOR_SIZE >= 512
294 | static inline Vec16f sin(Vec16f const x) {
295 | return sincos_f(0, x);
296 | }
297 |
298 | static inline Vec16f cos(Vec16f const x) {
299 | return sincos_f(0, x);
300 | }
301 |
302 | static inline Vec16f sincos(Vec16f * cosret, Vec16f const x) {
303 | return sincos_f(cosret, x);
304 | }
305 |
306 | static inline Vec16f tan(Vec16f const x) {
307 | return sincos_f(0, x);
308 | }
309 | #endif // MAX_VECTOR_SIZE >= 512
310 |
311 |
312 | // *************************************************************
313 | // tan template, double precision
314 | // *************************************************************
315 | // Template parameters:
316 | // VTYPE: f.p. vector type
317 | // Paramterers:
318 | // x = input x (radians)
319 | template
320 | static inline VTYPE tan_d(VTYPE const x) {
321 |
322 | // define constants
323 | const double DP1 = 7.853981554508209228515625E-1 * 2.;;
324 | const double DP2 = 7.94662735614792836714E-9 * 2.;;
325 | const double DP3 = 3.06161699786838294307E-17 * 2.;;
326 |
327 | const double P2tan = -1.30936939181383777646E4;
328 | const double P1tan = 1.15351664838587416140E6;
329 | const double P0tan = -1.79565251976484877988E7;
330 |
331 | const double Q3tan = 1.36812963470692954678E4;
332 | const double Q2tan = -1.32089234440210967447E6;
333 | const double Q1tan = 2.50083801823357915839E7;
334 | const double Q0tan = -5.38695755929454629881E7;
335 |
336 | typedef decltype(x > x) BVTYPE; // boolean vector type
337 | VTYPE xa, y, z, zz, px, qx, tn, recip; // data vectors
338 | BVTYPE doinvert, xzero, overflow; // boolean vectors
339 | typedef decltype(nan_code(x)) UITYPE; // unsigned integer vector type
340 |
341 |
342 | xa = abs(x);
343 |
344 | // Find quadrant
345 | y = round(xa * (double)(2. / VM_PI)); // quadrant, as float
346 | auto q = roundi(y); // quadrant, as integer
347 | // Find quadrant
348 | // 0 - pi/4 => 0
349 | // pi/4 - 3*pi/4 => 1
350 | // 3*pi/4 - 5*pi/4 => 2
351 | // 5*pi/4 - 7*pi/4 => 3
352 | // 7*pi/4 - 8*pi/4 => 4
353 |
354 | // Reduce by extended precision modular arithmetic
355 | // z = ((xa - y * DP1) - y * DP2) - y * DP3;
356 | z = nmul_add(y, DP3, nmul_add(y, DP2, nmul_add(y, DP1, xa)));
357 |
358 | // Pade expansion of tan, valid for -pi/4 <= x <= pi/4
359 | zz = z * z;
360 | px = polynomial_2(zz, P0tan, P1tan, P2tan);
361 | qx = polynomial_4n(zz, Q0tan, Q1tan, Q2tan, Q3tan);
362 |
363 | // qx cannot be 0 for x <= pi/4
364 | tn = mul_add(px / qx, z * zz, z); // tn = z + z * zz * px / qx;
365 |
366 | // if (q&2) tn = -1/tn
367 | doinvert = BVTYPE((q & 1) != 0);
368 | xzero = (xa == 0.);
369 | // avoid division by 0. We will not be using recip anyway if xa == 0.
370 | // tn never becomes exactly 0 when x = pi/2 so we only have to make
371 | // a special case for x == 0.
372 | recip = (-1.) / select(xzero, VTYPE(-1.), tn);
373 | tn = select(doinvert, recip, tn);
374 | tn = sign_combine(tn, x); // get original sign
375 |
376 | overflow = BVTYPE(UITYPE(q) > 0x80000000000000) & is_finite(xa);
377 | tn = select(overflow, 0., tn);
378 |
379 | return tn;
380 | }
381 |
382 | // instantiations of tan_d template:
383 |
384 | static inline Vec2d tan(Vec2d const x) {
385 | return tan_d(x);
386 | }
387 |
388 | #if MAX_VECTOR_SIZE >= 256
389 | static inline Vec4d tan(Vec4d const x) {
390 | return tan_d(x);
391 | }
392 | #endif // MAX_VECTOR_SIZE >= 256
393 |
394 | #if MAX_VECTOR_SIZE >= 512
395 | static inline Vec8d tan(Vec8d const x) {
396 | return tan_d(x);
397 | }
398 | #endif // MAX_VECTOR_SIZE >= 512
399 |
400 |
401 | // *************************************************************
402 | // tan template, single precision
403 | // *************************************************************
404 | // This is removed for the single precision version.
405 | // It is faster to use tan(x) = sin(x)/cos(x)
406 |
407 |
408 |
409 | // *************************************************************
410 | // asin/acos template, double precision
411 | // *************************************************************
412 | // Template parameters:
413 | // VTYPE: f.p. vector type
414 | // AC: 0 = asin, 1 = acos
415 | // Paramterers:
416 | // x = input x
417 | template
418 | static inline VTYPE asin_d(VTYPE const x) {
419 |
420 | // define constants
421 | const double R4asin = 2.967721961301243206100E-3;
422 | const double R3asin = -5.634242780008963776856E-1;
423 | const double R2asin = 6.968710824104713396794E0;
424 | const double R1asin = -2.556901049652824852289E1;
425 | const double R0asin = 2.853665548261061424989E1;
426 |
427 | const double S3asin = -2.194779531642920639778E1;
428 | const double S2asin = 1.470656354026814941758E2;
429 | const double S1asin = -3.838770957603691357202E2;
430 | const double S0asin = 3.424398657913078477438E2;
431 |
432 | const double P5asin = 4.253011369004428248960E-3;
433 | const double P4asin = -6.019598008014123785661E-1;
434 | const double P3asin = 5.444622390564711410273E0;
435 | const double P2asin = -1.626247967210700244449E1;
436 | const double P1asin = 1.956261983317594739197E1;
437 | const double P0asin = -8.198089802484824371615E0;
438 |
439 | const double Q4asin = -1.474091372988853791896E1;
440 | const double Q3asin = 7.049610280856842141659E1;
441 | const double Q2asin = -1.471791292232726029859E2;
442 | const double Q1asin = 1.395105614657485689735E2;
443 | const double Q0asin = -4.918853881490881290097E1;
444 |
445 | VTYPE xa, xb, x1, x2, x3, x4, x5, px, qx, rx, sx, vx, wx, y1, yb, z, z1, z2;
446 | bool dobig, dosmall;
447 |
448 | xa = abs(x);
449 | auto big = xa >= 0.625; // boolean vector
450 |
451 | /*
452 | Small: xa < 0.625
453 | ------------------
454 | x = xa * xa;
455 | px = PX(x);
456 | qx = QX(x);
457 | y1 = x*px/qx;
458 | y1 = xa * y1 + xa;
459 |
460 | Big: xa >= 0.625
461 | ------------------
462 | x = 1.0 - xa;
463 | rx = RX(x);
464 | sx = SX(x);
465 | y1 = x * rx/sx;
466 | x3 = sqrt(x+x);
467 | y3 = x3 * y1 - MOREBITS;
468 | z = pi/2 - x3 - y3
469 | */
470 |
471 | // select a common x for all polynomials
472 | // This allows sharing of powers of x through common subexpression elimination
473 | x1 = select(big, 1.0 - xa, xa * xa);
474 |
475 | // calculate powers of x1 outside branches to make sure they are only calculated once
476 | x2 = x1 * x1;
477 | x4 = x2 * x2;
478 | x5 = x4 * x1;
479 | x3 = x2 * x1;
480 |
481 | dosmall = !horizontal_and(big); // at least one element is small
482 | dobig = horizontal_or(big); // at least one element is big
483 |
484 | // calculate polynomials (reuse powers of x)
485 | if (dosmall) {
486 | // px = polynomial_5 (x1, P0asin, P1asin, P2asin, P3asin, P4asin, P5asin);
487 | // qx = polynomial_5n(x1, Q0asin, Q1asin, Q2asin, Q3asin, Q4asin);
488 | px = mul_add(x3, P3asin, P0asin) + mul_add(x4, P4asin, x1*P1asin) + mul_add(x5, P5asin, x2*P2asin);
489 | qx = mul_add(x4, Q4asin, x5) + mul_add(x3, Q3asin, x1*Q1asin) + mul_add(x2, Q2asin, Q0asin);
490 | }
491 | if (dobig) {
492 | // rx = polynomial_4 (x1, R0asin, R1asin, R2asin, R3asin, R4asin);
493 | // sx = polynomial_4n(x1, S0asin, S1asin, S2asin, S3asin);
494 | rx = mul_add(x3, R3asin, x2*R2asin) + mul_add(x4, R4asin, mul_add(x1, R1asin, R0asin));
495 | sx = mul_add(x3, S3asin, x4) + mul_add(x2, S2asin, mul_add(x1, S1asin, S0asin));
496 | }
497 |
498 | // select and divide outside branches to avoid dividing twice
499 | vx = select(big, rx, px);
500 | wx = select(big, sx, qx);
501 | y1 = vx / wx * x1;
502 |
503 | // results for big
504 | if (dobig) { // avoid square root if all are small
505 | xb = sqrt(x1 + x1); // this produces NAN if xa > 1 so we don't need a special case for xa > 1
506 | z1 = mul_add(xb, y1, xb); // yb = xb * y1; z1 = xb + yb;
507 | }
508 |
509 | // results for small
510 | z2 = mul_add(xa, y1, xa); // z2 = xa * y1 + xa;
511 |
512 | // correct for sign
513 | if constexpr (AC == 1) { // acos
514 | z1 = select(x < 0., VM_PI - z1, z1);
515 | z2 = VM_PI_2 - sign_combine(z2, x);
516 | z = select(big, z1, z2);
517 | }
518 | else { // asin
519 | z1 = VM_PI_2 - z1;
520 | z = select(big, z1, z2);
521 | z = sign_combine(z, x);
522 | }
523 | return z;
524 | }
525 |
526 | // instantiations of asin_d template:
527 |
528 | static inline Vec2d asin(Vec2d const x) {
529 | return asin_d(x);
530 | }
531 |
532 | static inline Vec2d acos(Vec2d const x) {
533 | return asin_d(x);
534 | }
535 |
536 | #if MAX_VECTOR_SIZE >= 256
537 | static inline Vec4d asin(Vec4d const x) {
538 | return asin_d(x);
539 | }
540 |
541 | static inline Vec4d acos(Vec4d const x) {
542 | return asin_d(x);
543 | }
544 | #endif // MAX_VECTOR_SIZE >= 256
545 |
546 | #if MAX_VECTOR_SIZE >= 512
547 | static inline Vec8d asin(Vec8d const x) {
548 | return asin_d(x);
549 | }
550 |
551 | static inline Vec8d acos(Vec8d const x) {
552 | return asin_d(x);
553 | }
554 | #endif // MAX_VECTOR_SIZE >= 512
555 |
556 |
557 | // *************************************************************
558 | // asin/acos template, single precision
559 | // *************************************************************
560 | // Template parameters:
561 | // VTYPE: f.p. vector type
562 | // AC: 0 = asin, 1 = acos
563 | // Paramterers:
564 | // x = input x
565 | template
566 | static inline VTYPE asin_f(VTYPE const x) {
567 |
568 | // define constants
569 | const float P4asinf = 4.2163199048E-2f;
570 | const float P3asinf = 2.4181311049E-2f;
571 | const float P2asinf = 4.5470025998E-2f;
572 | const float P1asinf = 7.4953002686E-2f;
573 | const float P0asinf = 1.6666752422E-1f;
574 |
575 | VTYPE xa, x1, x2, x3, x4, xb, z, z1, z2;
576 |
577 | xa = abs(x);
578 | auto big = xa > 0.5f; // boolean vector
579 |
580 | x1 = 0.5f * (1.0f - xa);
581 | x2 = xa * xa;
582 | x3 = select(big, x1, x2);
583 |
584 | //if (horizontal_or(big))
585 | {
586 | xb = sqrt(x1);
587 | }
588 | x4 = select(big, xb, xa);
589 |
590 | z = polynomial_4(x3, P0asinf, P1asinf, P2asinf, P3asinf, P4asinf);
591 | z = mul_add(z, x3*x4, x4); // z = z * (x3*x4) + x4;
592 | z1 = z + z;
593 |
594 | // correct for sign
595 | if constexpr (AC == 1) { // acos
596 | z1 = select(x < 0., float(VM_PI) - z1, z1);
597 | z2 = float(VM_PI_2) - sign_combine(z, x);
598 | z = select(big, z1, z2);
599 | }
600 | else { // asin
601 | z1 = float(VM_PI_2) - z1;
602 | z = select(big, z1, z);
603 | z = sign_combine(z, x);
604 | }
605 |
606 | return z;
607 | }
608 |
609 | // instantiations of asin_f template:
610 |
611 | static inline Vec4f asin(Vec4f const x) {
612 | return asin_f(x);
613 | }
614 |
615 | static inline Vec4f acos(Vec4f const x) {
616 | return asin_f(x);
617 | }
618 |
619 | #if MAX_VECTOR_SIZE >= 256
620 | static inline Vec8f asin(Vec8f const x) {
621 | return asin_f(x);
622 | }
623 | static inline Vec8f acos(Vec8f const x) {
624 | return asin_f(x);
625 | }
626 | #endif // MAX_VECTOR_SIZE >= 256
627 |
628 | #if MAX_VECTOR_SIZE >= 512
629 | static inline Vec16f asin(Vec16f const x) {
630 | return asin_f(x);
631 | }
632 | static inline Vec16f acos(Vec16f const x) {
633 | return asin_f(x);
634 | }
635 | #endif // MAX_VECTOR_SIZE >= 512
636 |
637 |
638 | // *************************************************************
639 | // atan template, double precision
640 | // *************************************************************
641 | // Template parameters:
642 | // VTYPE: f.p. vector type
643 | // T2: 0 = atan, 1 = atan2
644 | // Paramterers:
645 | // y, x. calculate tan(y/x)
646 | // result is between -pi/2 and +pi/2 when x > 0
647 | // result is between -pi and -pi/2 or between pi/2 and pi when x < 0 for atan2
648 | template
649 | static inline VTYPE atan_d(VTYPE const y, VTYPE const x) {
650 |
651 | // define constants
652 | //const double ONEOPIO4 = 4./VM_PI;
653 | const double MOREBITS = 6.123233995736765886130E-17;
654 | const double MOREBITSO2 = MOREBITS * 0.5;
655 | const double T3PO8 = VM_SQRT2 + 1.; // 2.41421356237309504880;
656 |
657 | const double P4atan = -8.750608600031904122785E-1;
658 | const double P3atan = -1.615753718733365076637E1;
659 | const double P2atan = -7.500855792314704667340E1;
660 | const double P1atan = -1.228866684490136173410E2;
661 | const double P0atan = -6.485021904942025371773E1;
662 |
663 | const double Q4atan = 2.485846490142306297962E1;
664 | const double Q3atan = 1.650270098316988542046E2;
665 | const double Q2atan = 4.328810604912902668951E2;
666 | const double Q1atan = 4.853903996359136964868E2;
667 | const double Q0atan = 1.945506571482613964425E2;
668 |
669 | typedef decltype (x > x) BVTYPE; // boolean vector type
670 | VTYPE t, x1, x2, y1, y2, s, fac, a, b, z, zz, px, qx, re; // data vectors
671 | BVTYPE swapxy, notbig, notsmal; // boolean vectors
672 |
673 | if constexpr (T2 == 1) { // atan2(y,x)
674 | // move in first octant
675 | x1 = abs(x);
676 | y1 = abs(y);
677 | swapxy = (y1 > x1);
678 | // swap x and y if y1 > x1
679 | x2 = select(swapxy, y1, x1);
680 | y2 = select(swapxy, x1, y1);
681 |
682 | // check for special case: x and y are both +/- INF
683 | BVTYPE both_infinite = is_inf(x) & is_inf(y); // x and Y are both infinite
684 | if (horizontal_or(both_infinite)) { // at least one element has both infinite
685 | VTYPE mone = VTYPE(-1.0);
686 | x2 = select(both_infinite, x2 & mone, x2); // get 1.0 with the sign of x
687 | y2 = select(both_infinite, y2 & mone, y2); // get 1.0 with the sign of y
688 | }
689 |
690 | t = y2 / x2; // x = y = 0 gives NAN here
691 | }
692 | else { // atan(y)
693 | t = abs(y);
694 | }
695 |
696 | // small: t < 0.66
697 | // medium: 0.66 <= t <= 2.4142 (1+sqrt(2))
698 | // big: t > 2.4142
699 | notbig = t <= T3PO8; // t <= 2.4142
700 | notsmal = t >= 0.66; // t >= 0.66
701 |
702 | s = select(notbig, VTYPE(VM_PI_4), VTYPE(VM_PI_2));
703 | s = notsmal & s; // select(notsmal, s, 0.);
704 | fac = select(notbig, VTYPE(MOREBITSO2), VTYPE(MOREBITS));
705 | fac = notsmal & fac; //select(notsmal, fac, 0.);
706 |
707 | // small: z = t / 1.0;
708 | // medium: z = (t-1.0) / (t+1.0);
709 | // big: z = -1.0 / t;
710 | a = notbig & t; // select(notbig, t, 0.);
711 | a = if_add(notsmal, a, -1.);
712 | b = notbig & VTYPE(1.); // select(notbig, 1., 0.);
713 | b = if_add(notsmal, b, t);
714 | z = a / b; // division by 0 will not occur unless x and y are both 0
715 |
716 | zz = z * z;
717 |
718 | px = polynomial_4(zz, P0atan, P1atan, P2atan, P3atan, P4atan);
719 | qx = polynomial_5n(zz, Q0atan, Q1atan, Q2atan, Q3atan, Q4atan);
720 |
721 | re = mul_add(px / qx, z * zz, z); // re = (px / qx) * (z * zz) + z;
722 | re += s + fac;
723 |
724 | if constexpr (T2 == 1) { // atan2(y,x)
725 | // move back in place
726 | re = select(swapxy, VM_PI_2 - re, re);
727 | re = select((x | y) == 0., 0., re); // atan2(0,0) = 0 by convention
728 | re = select(sign_bit(x), VM_PI - re, re);// also for x = -0.
729 | }
730 | // get sign bit
731 | re = sign_combine(re, y);
732 |
733 | return re;
734 | }
735 |
736 | // instantiations of atan_d template:
737 |
738 | static inline Vec2d atan2(Vec2d const y, Vec2d const x) {
739 | return atan_d(y, x);
740 | }
741 |
742 | static inline Vec2d atan(Vec2d const y) {
743 | return atan_d(y, 0.);
744 | }
745 |
746 | #if MAX_VECTOR_SIZE >= 256
747 | static inline Vec4d atan2(Vec4d const y, Vec4d const x) {
748 | return atan_d(y, x);
749 | }
750 |
751 | static inline Vec4d atan(Vec4d const y) {
752 | return atan_d(y, 0.);
753 | }
754 | #endif // MAX_VECTOR_SIZE >= 256
755 |
756 | #if MAX_VECTOR_SIZE >= 512
757 | static inline Vec8d atan2(Vec8d const y, Vec8d const x) {
758 | return atan_d(y, x);
759 | }
760 |
761 | static inline Vec8d atan(Vec8d const y) {
762 | return atan_d(y, 0.);
763 | }
764 | #endif // MAX_VECTOR_SIZE >= 512
765 |
766 |
767 |
768 | // *************************************************************
769 | // atan template, single precision
770 | // *************************************************************
771 | // Template parameters:
772 | // VTYPE: f.p. vector type
773 | // T2: 0 = atan, 1 = atan2
774 | // Paramterers:
775 | // y, x. calculate tan(y/x)
776 | // result is between -pi/2 and +pi/2 when x > 0
777 | // result is between -pi and -pi/2 or between pi/2 and pi when x < 0 for atan2
778 | template
779 | static inline VTYPE atan_f(VTYPE const y, VTYPE const x) {
780 |
781 | // define constants
782 | const float P3atanf = 8.05374449538E-2f;
783 | const float P2atanf = -1.38776856032E-1f;
784 | const float P1atanf = 1.99777106478E-1f;
785 | const float P0atanf = -3.33329491539E-1f;
786 |
787 | typedef decltype (x > x) BVTYPE; // boolean vector type
788 | VTYPE t, x1, x2, y1, y2, s, a, b, z, zz, re;// data vectors
789 | BVTYPE swapxy, notbig, notsmal; // boolean vectors
790 |
791 | if constexpr (T2 == 1) { // atan2(y,x)
792 | // move in first octant
793 | x1 = abs(x);
794 | y1 = abs(y);
795 | swapxy = (y1 > x1);
796 | // swap x and y if y1 > x1
797 | x2 = select(swapxy, y1, x1);
798 | y2 = select(swapxy, x1, y1);
799 |
800 | // check for special case: x and y are both +/- INF
801 | BVTYPE both_infinite = is_inf(x) & is_inf(y); // x and Y are both infinite
802 | if (horizontal_or(both_infinite)) { // at least one element has both infinite
803 | VTYPE mone = VTYPE(-1.0f);
804 | x2 = select(both_infinite, x2 & mone, x2); // get 1.0 with the sign of x
805 | y2 = select(both_infinite, y2 & mone, y2); // get 1.0 with the sign of y
806 | }
807 |
808 | // x = y = 0 will produce NAN. No problem, fixed below
809 | t = y2 / x2;
810 | }
811 | else { // atan(y)
812 | t = abs(y);
813 | }
814 |
815 | // small: t < 0.4142
816 | // medium: 0.4142 <= t <= 2.4142
817 | // big: t > 2.4142 (not for atan2)
818 | if constexpr (T2 == 0) { // atan(y)
819 | notsmal = t >= float(VM_SQRT2 - 1.); // t >= tan pi/8
820 | notbig = t <= float(VM_SQRT2 + 1.); // t <= tan 3pi/8
821 |
822 | s = select(notbig, VTYPE(float(VM_PI_4)), VTYPE(float(VM_PI_2)));
823 | s = notsmal & s; // select(notsmal, s, 0.);
824 |
825 | // small: z = t / 1.0;
826 | // medium: z = (t-1.0) / (t+1.0);
827 | // big: z = -1.0 / t;
828 | a = notbig & t; // select(notbig, t, 0.);
829 | a = if_add(notsmal, a, -1.f);
830 | b = notbig & VTYPE(1.f); // select(notbig, 1., 0.);
831 | b = if_add(notsmal, b, t);
832 | z = a / b; // division by 0 will not occur unless x and y are both 0
833 | }
834 | else { // atan2(y,x)
835 | // small: z = t / 1.0;
836 | // medium: z = (t-1.0) / (t+1.0);
837 | notsmal = t >= float(VM_SQRT2 - 1.);
838 | a = if_add(notsmal, t, -1.f);
839 | b = if_add(notsmal, 1.f, t);
840 | s = notsmal & VTYPE(float(VM_PI_4));
841 | z = a / b;
842 | }
843 |
844 | zz = z * z;
845 |
846 | // Taylor expansion
847 | re = polynomial_3(zz, P0atanf, P1atanf, P2atanf, P3atanf);
848 | re = mul_add(re, zz * z, z) + s;
849 |
850 | if constexpr (T2 == 1) { // atan2(y,x)
851 | // move back in place
852 | re = select(swapxy, float(VM_PI_2) - re, re);
853 | re = select((x | y) == 0.f, 0.f, re); // atan2(0,+0) = 0 by convention
854 | re = select(sign_bit(x), float(VM_PI) - re, re); // also for x = -0.
855 | }
856 | // get sign bit
857 | re = sign_combine(re, y);
858 |
859 | return re;
860 | }
861 |
862 | // instantiations of atan_f template:
863 |
864 | static inline Vec4f atan2(Vec4f const y, Vec4f const x) {
865 | return atan_f(y, x);
866 | }
867 |
868 | static inline Vec4f atan(Vec4f const y) {
869 | return atan_f(y, 0.);
870 | }
871 |
872 | #if MAX_VECTOR_SIZE >= 256
873 | static inline Vec8f atan2(Vec8f const y, Vec8f const x) {
874 | return atan_f(y, x);
875 | }
876 |
877 | static inline Vec8f atan(Vec8f const y) {
878 | return atan_f(y, 0.);
879 | }
880 |
881 | #endif // MAX_VECTOR_SIZE >= 256
882 |
883 | #if MAX_VECTOR_SIZE >= 512
884 | static inline Vec16f atan2(Vec16f const y, Vec16f const x) {
885 | return atan_f(y, x);
886 | }
887 |
888 | static inline Vec16f atan(Vec16f const y) {
889 | return atan_f(y, 0.);
890 | }
891 |
892 | #endif // MAX_VECTOR_SIZE >= 512
893 |
894 | #ifdef VCL_NAMESPACE
895 | }
896 | #endif
897 |
898 | #endif
899 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------