├── src ├── config.h.in ├── shared.h ├── AVX2 │ ├── AutoGain_AVX2.cpp │ └── AGM_AVX2.cpp ├── RFS.cpp ├── shared.cpp ├── AGM.cpp ├── ssimulacra.cpp ├── VisualizeDiffs.cpp ├── Butteraugli.cpp └── AutoGain.cpp ├── .clang-tidy ├── .clang-format ├── .gitignore ├── .gitmodules ├── thirdparty ├── libjxl_cache.cmake └── vectorclass │ ├── vectorclass.h │ ├── instrset_detect.cpp │ ├── LICENSE │ ├── vectormath_common.h │ ├── vector_convert.h │ ├── vectormath_hyp.h │ └── vectormath_trig.h ├── LICENSE ├── README.md ├── .github └── workflows │ └── build.yml └── CMakeLists.txt /src/config.h.in: -------------------------------------------------------------------------------- 1 | #define VERSION "@VCS_TAG@" -------------------------------------------------------------------------------- /.clang-tidy: -------------------------------------------------------------------------------- 1 | Checks: '-bugprone-unused-return-value' -------------------------------------------------------------------------------- /.clang-format: -------------------------------------------------------------------------------- 1 | BasedOnStyle: Google 2 | ColumnLimit: 0 3 | IndentWidth: 4 -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | /thirdparty/libjxl_build 3 | .vscode 4 | .cache 5 | compile_commands.json 6 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "thirdparty/libjxl"] 2 | path = thirdparty/libjxl 3 | url = https://github.com/libjxl/libjxl.git 4 | -------------------------------------------------------------------------------- /thirdparty/libjxl_cache.cmake: -------------------------------------------------------------------------------- 1 | set(CMAKE_INSTALL_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/install" CACHE PATH "Install path prefix.") 2 | set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build.") 3 | set(BUILD_SHARED_LIBS OFF CACHE BOOL "Build shared libraries.") 4 | set(BUILD_TESTING OFF CACHE BOOL "Build the testing tree.") 5 | set(JPEGXL_ENABLE_BENCHMARK OFF CACHE BOOL "Build JPEGXL benchmark tools.") 6 | set(JPEGXL_ENABLE_EXAMPLES OFF CACHE BOOL "Build JPEGXL library usage examples.") 7 | set(JPEGXL_ENABLE_FUZZERS OFF CACHE BOOL "Build JPEGXL fuzzer targets.") 8 | set(JPEGXL_ENABLE_JNI OFF CACHE BOOL "Build JPEGXL JNI Java wrapper, if Java dependencies are installed.") 9 | set(JPEGXL_ENABLE_MANPAGES OFF CACHE BOOL "Build and install man pages for the command-line tools.") 10 | set(JPEGXL_ENABLE_OPENEXR OFF CACHE BOOL "Build JPEGXL with support for OpenEXR if available.") 11 | set(JPEGXL_ENABLE_SJPEG OFF CACHE BOOL "Build JPEGXL with support for encoding with sjpeg.") 12 | set(JPEGXL_ENABLE_TOOLS OFF CACHE BOOL "Build JPEGXL user tools: cjxl and djxl.") -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Julek 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | vapoursynth-julek-plugin is a collection of some new filters and some already known ones that I am implementing here to have more performance. 2 | 3 | Please visit the [wiki](https://github.com/dnjulek/vapoursynth-julek-plugin/wiki) for more details. 4 | 5 | ### Building: 6 | 7 | ``` 8 | Requirements: 9 | - Git 10 | - C++17 compiler 11 | - CMake >= 3.23 12 | ``` 13 | ### Linux: 14 | ``` 15 | git clone --recurse-submodules https://github.com/dnjulek/vapoursynth-julek-plugin 16 | 17 | cd vapoursynth-julek-plugin/thirdparty 18 | 19 | mkdir libjxl_build 20 | cd libjxl_build 21 | 22 | cmake -C ../libjxl_cache.cmake -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_C_COMPILER=clang -G Ninja ../libjxl 23 | 24 | cmake --build . 25 | cmake --install . 26 | 27 | cd ../.. 28 | 29 | cmake -B build -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_BUILD_TYPE=Release -G Ninja 30 | 31 | cmake --build build 32 | sudo cmake --install build 33 | ``` 34 | 35 | I recommend compiling with clang or you may have problems with libjxl, if you want to try compiling with gcc you may need to add this to the second cmake command:\ 36 | ``-DCMAKE_C_FLAGS=fPIC -DCMAKE_CXX_FLAGS=-fPIC`` 37 | ### Windows: 38 | Open the ``Visual Studio 2022 Developer PowerShell`` and use cd to the folder you downloaded the repository. 39 | ``` 40 | cd vapoursynth-julek-plugin/thirdparty 41 | 42 | mkdir libjxl_build 43 | cd libjxl_build 44 | 45 | cmake -C ../libjxl_cache.cmake -G Ninja ../libjxl 46 | 47 | cmake --build . 48 | cmake --install . 49 | 50 | cd ../.. 51 | 52 | cmake -B build -DCMAKE_BUILD_TYPE=Release -DVS_INCLUDE_DIR="C:/Program Files/VapourSynth/sdk/include/vapoursynth" -G Ninja 53 | 54 | cmake --build build 55 | ``` 56 | -------------------------------------------------------------------------------- /src/shared.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "config.h" 12 | #include "lib/extras/codec.h" 13 | #include "lib/jxl/color_management.h" 14 | #include "lib/jxl/enc_butteraugli_comparator.h" 15 | #include "lib/jxl/enc_color_management.h" 16 | #include "tools/ssimulacra.h" 17 | #include "tools/ssimulacra2.h" 18 | 19 | #ifdef PLUGIN_X86 20 | #include "vectorclass.h" 21 | #include "vectormath_exp.h" 22 | #endif 23 | 24 | extern void VS_CC agmCreate(const VSMap* in, VSMap* out, void* userData, VSCore* core, const VSAPI* vsapi); 25 | extern void VS_CC autogainCreate(const VSMap* in, VSMap* out, void* userData, VSCore* core, const VSAPI* vsapi); 26 | extern void VS_CC butteraugliCreate(const VSMap* in, VSMap* out, void* userData, VSCore* core, const VSAPI* vsapi); 27 | extern void VS_CC colormapCreate(const VSMap* in, VSMap* out, void* userData, VSCore* core, const VSAPI* vsapi); 28 | extern void VS_CC rfsCreate(const VSMap* in, VSMap* out, void* userData, VSCore* core, const VSAPI* vsapi); 29 | extern void VS_CC ssimulacraCreate(const VSMap* in, VSMap* out, void* userData, VSCore* core, const VSAPI* vsapi); 30 | extern void VS_CC visualizediffsCreate(const VSMap* in, VSMap* out, void* userData, VSCore* core, const VSAPI* vsapi); 31 | 32 | #ifdef _MSC_VER 33 | #define FORCE_INLINE inline __forceinline 34 | #else 35 | #define FORCE_INLINE inline __attribute__((always_inline)) 36 | #endif 37 | 38 | struct AGMData final { 39 | VSNode* node; 40 | const VSVideoInfo* vi; 41 | float luma_scaling; 42 | float float_range[256]; 43 | int shift, peak; 44 | void (*process)(const VSFrame* src, VSFrame* dst, float& avg, const AGMData* const VS_RESTRICT d, const VSAPI* vsapi) noexcept; 45 | }; -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Build Windows 2 | 3 | on: 4 | push: 5 | workflow_dispatch: 6 | inputs: 7 | tag: 8 | description: 'which tag to upload to' 9 | default: '' 10 | 11 | jobs: 12 | build-windows: 13 | runs-on: windows-2019 14 | 15 | defaults: 16 | run: 17 | shell: cmd 18 | 19 | steps: 20 | - name: Checkout repo 21 | uses: actions/checkout@v3 22 | with: 23 | fetch-depth: 0 24 | submodules: true 25 | 26 | - name: download VS headers 27 | shell: bash 28 | run: | 29 | git clone https://github.com/AmusementClub/vapoursynth-classic --depth=1 --branch doodle2 vapoursynth 30 | cp vapoursynth/include/*.h src/ 31 | 32 | - name: Build libjxl 33 | shell: cmd 34 | run: | 35 | cd thirdparty 36 | cd libjxl 37 | git submodule update --init 38 | cd .. 39 | mkdir libjxl_build 40 | cd libjxl_build 41 | rem CMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded does not work here. 42 | cmake -C ../libjxl_cache.cmake ../libjxl -D CMAKE_CXX_FLAGS_RELEASE="/MT /O2 /Ob2 /DNDEBUG" -D CMAKE_C_FLAGS_RELEASE="/MT /O2 /Ob2 /DNDEBUG" -LA 43 | cmake --build . --config Release 44 | cmake --install . 45 | 46 | - name: Plug in vs-api3 47 | shell: bash 48 | run: | 49 | git clone https://github.com/AmusementClub/vs-api3 --depth=1 --branch master api3 50 | cp api3/api3.cc src/ 51 | sed -i -e "/AGM.cpp/s//& src\\/api3.cc/" CMakeLists.txt 52 | cat CMakeLists.txt 53 | 54 | - name: Build 55 | shell: cmd 56 | run: | 57 | mkdir build 58 | cd build 59 | cmake .. -DVS_INCLUDE_DIR=${{ github.workspace }}/src -D CMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded 60 | cmake --build . --config Release 61 | 62 | - name: Upload 63 | uses: actions/upload-artifact@v3 64 | with: 65 | name: artifact 66 | path: | 67 | build/Release/julek.dll 68 | 69 | - name: Compress artifact for release 70 | if: github.event_name == 'workflow_dispatch' && github.event.inputs.tag != '' 71 | run: | 72 | cd build\Release 73 | 7z a -t7z -mx=7 ../../julek-windows-x64.${{ github.event.inputs.tag }}.7z julek.dll 74 | 75 | - name: Release 76 | uses: softprops/action-gh-release@v1 77 | if: github.event_name == 'workflow_dispatch' && github.event.inputs.tag != '' 78 | with: 79 | tag_name: ${{ github.event.inputs.tag }} 80 | files: julek-windows-x64.${{ github.event.inputs.tag }}.7z 81 | fail_on_unmatched_files: true 82 | generate_release_notes: false 83 | prerelease: true 84 | -------------------------------------------------------------------------------- /src/AVX2/AutoGain_AVX2.cpp: -------------------------------------------------------------------------------- 1 | #ifdef PLUGIN_X86 2 | #include "../shared.h" 3 | 4 | extern void minmaxUC_c(const uint8_t* VS_RESTRICT srcp, uint8_t& dst_min, uint8_t& dst_max, ptrdiff_t stride, const int w, const int h) noexcept; 5 | extern void minmaxUS_c(const uint8_t* VS_RESTRICT srcp, uint16_t& dst_min, uint16_t& dst_max, ptrdiff_t stride, const int w, const int h) noexcept; 6 | extern void minmaxF_c(const uint8_t* VS_RESTRICT srcp, float& dst_min, float& dst_max, ptrdiff_t stride, const int w, const int h) noexcept; 7 | 8 | void autogainUC_avx2(const uint8_t* VS_RESTRICT srcp, uint8_t* VS_RESTRICT dstp, ptrdiff_t stride, const int w, const int h) noexcept { 9 | uint8_t pmin, pmax; 10 | minmaxUC_c(srcp, pmin, pmax, stride, w, h); 11 | for (int y{0}; y < h; y++) { 12 | for (int x{0}; x < w; x++) { 13 | uint8_t v = ((const uint8_t*)srcp)[x]; 14 | ((uint8_t*)dstp)[x] = (uint8_t)(((v - pmin) / (float)(pmax - pmin)) * 255 + 0.5f); 15 | } 16 | srcp += stride; 17 | dstp += stride; 18 | } 19 | } 20 | 21 | template 22 | void autogainUS_avx2(const uint8_t* VS_RESTRICT srcp, uint8_t* VS_RESTRICT dstp, ptrdiff_t stride, const int w, const int h) noexcept { 23 | uint16_t pmin, pmax; 24 | minmaxUS_c(srcp, pmin, pmax, stride, w, h); 25 | const float range = (float)(pmax - pmin); 26 | for (int y{0}; y < h; y++) { 27 | for (int x{0}; x < w; x += 16) { 28 | Vec16us v = (Vec16us().load(((const uint16_t*)srcp) + x)) - pmin; 29 | Vec8f v_l = to_float(extend_low(v)) / range * peak + 0.5f; 30 | Vec8f v_h = to_float(extend_high(v)) / range * peak + 0.5f; 31 | v = compress_saturated_s2u(truncatei(v_l), truncatei(v_h)); 32 | v.store(((uint16_t*)dstp) + x); 33 | } 34 | srcp += stride; 35 | dstp += stride; 36 | } 37 | } 38 | 39 | void autogainF_avx2(const uint8_t* VS_RESTRICT srcp, uint8_t* VS_RESTRICT dstp, ptrdiff_t stride, const int w, const int h) noexcept { 40 | float pmin, pmax; 41 | minmaxF_c(srcp, pmin, pmax, stride, w, h); 42 | const float range = (float)(pmax - pmin); 43 | for (int y{0}; y < h; y++) { 44 | for (int x{0}; x < w; x += 8) { 45 | Vec8f v = (Vec8f().load(((const float*)srcp) + x)) - pmin; 46 | (v / range).store(((float*)dstp) + x); 47 | } 48 | srcp += stride; 49 | dstp += stride; 50 | } 51 | } 52 | 53 | template void autogainUS_avx2<1023>(const uint8_t* VS_RESTRICT srcp, uint8_t* VS_RESTRICT dstp, ptrdiff_t stride, const int w, const int h) noexcept; 54 | template void autogainUS_avx2<4095>(const uint8_t* VS_RESTRICT srcp, uint8_t* VS_RESTRICT dstp, ptrdiff_t stride, const int w, const int h) noexcept; 55 | template void autogainUS_avx2<16383>(const uint8_t* VS_RESTRICT srcp, uint8_t* VS_RESTRICT dstp, ptrdiff_t stride, const int w, const int h) noexcept; 56 | template void autogainUS_avx2<65535>(const uint8_t* VS_RESTRICT srcp, uint8_t* VS_RESTRICT dstp, ptrdiff_t stride, const int w, const int h) noexcept; 57 | #endif -------------------------------------------------------------------------------- /thirdparty/vectorclass/vectorclass.h: -------------------------------------------------------------------------------- 1 | /**************************** vectorclass.h ******************************** 2 | * Author: Agner Fog 3 | * Date created: 2012-05-30 4 | * Last modified: 2021-08-18 5 | * Version: 2.01.04 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-2021 Agner Fog. 26 | * Apache License version 2.0 or later. 27 | ******************************************************************************/ 28 | #ifndef VECTORCLASS_H 29 | #define VECTORCLASS_H 20103 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 | -------------------------------------------------------------------------------- /src/RFS.cpp: -------------------------------------------------------------------------------- 1 | #include "shared.h" 2 | 3 | struct RFSData final { 4 | VSNode* node1; 5 | VSNode* node2; 6 | std::vector replace; 7 | }; 8 | 9 | struct MismatchInfo { 10 | bool match; 11 | bool differentDimensions; 12 | bool differentFormat; 13 | bool differentFrameRate; 14 | }; 15 | 16 | MismatchInfo findCommonVi(VSVideoInfo* outvi, VSNode* node2, const VSAPI* vsapi) { 17 | MismatchInfo result = {}; 18 | const VSVideoInfo* vi{vsapi->getVideoInfo(node2)}; 19 | 20 | if (outvi->width != vi->width || outvi->height != vi->height) { 21 | outvi->width = 0; 22 | outvi->height = 0; 23 | result.differentDimensions = true; 24 | } 25 | 26 | if (!vsh::isSameVideoFormat(&outvi->format, &vi->format)) { 27 | outvi->format = {}; 28 | result.differentFormat = true; 29 | } 30 | 31 | if (outvi->fpsNum != vi->fpsNum || outvi->fpsDen != vi->fpsDen) { 32 | outvi->fpsDen = 0; 33 | outvi->fpsNum = 0; 34 | result.differentFrameRate = true; 35 | } 36 | 37 | result.match = !result.differentDimensions && !result.differentFormat && !result.differentFrameRate; 38 | return result; 39 | } 40 | 41 | static const VSFrame* VS_CC rfsGetFrame(int n, int activationReason, void* instanceData, void** frameData, VSFrameContext* frameCtx, VSCore* core, const VSAPI* vsapi) { 42 | auto d{reinterpret_cast(instanceData)}; 43 | 44 | if (activationReason == arInitial) { 45 | vsapi->requestFrameFilter(n, (d->replace[n] ? d->node2 : d->node1), frameCtx); 46 | } else if (activationReason == arAllFramesReady) { 47 | return vsapi->getFrameFilter(n, (d->replace[n] ? d->node2 : d->node1), frameCtx); 48 | } 49 | return nullptr; 50 | } 51 | 52 | static void VS_CC rfsFree(void* instanceData, VSCore* core, const VSAPI* vsapi) { 53 | auto d{reinterpret_cast(instanceData)}; 54 | 55 | vsapi->freeNode(d->node1); 56 | vsapi->freeNode(d->node2); 57 | delete d; 58 | } 59 | 60 | void VS_CC rfsCreate(const VSMap* in, VSMap* out, void* userData, VSCore* core, const VSAPI* vsapi) { 61 | auto d{std::make_unique()}; 62 | int err{0}; 63 | 64 | d->node1 = vsapi->mapGetNode(in, "clip_a", 0, nullptr); 65 | d->node2 = vsapi->mapGetNode(in, "clip_b", 0, nullptr); 66 | 67 | VSVideoInfo vi = *vsapi->getVideoInfo(d->node1); 68 | MismatchInfo mminfo = findCommonVi(&vi, d->node2, vsapi); 69 | 70 | d->replace.resize(vi.numFrames); 71 | for (int i{0}; i < vsapi->mapNumElements(in, "frames"); i++) { 72 | d->replace[vsapi->mapGetInt(in, "frames", i, 0)] = true; 73 | } 74 | 75 | bool mismatch; 76 | mismatch = !!vsapi->mapGetInt(in, "mismatch", 0, &err); 77 | if (err) 78 | mismatch = false; 79 | 80 | if (!mismatch && !mminfo.match) { 81 | if (mminfo.differentDimensions) 82 | vsapi->mapSetError(out, "RFS: Clip dimensions don't match, enable mismatch if you want variable format."); 83 | else if (mminfo.differentFormat) 84 | vsapi->mapSetError(out, "RFS: Clip formats don't match, enable mismatch if you want variable format."); 85 | else if (mminfo.differentFrameRate) 86 | vsapi->mapSetError(out, "RFS: Clip frame rates don't match, enable mismatch if you want variable format."); 87 | vsapi->freeNode(d->node1); 88 | vsapi->freeNode(d->node2); 89 | return; 90 | } 91 | 92 | VSFilterDependency deps[]{{d->node1, rpGeneral}, {d->node2, rpGeneral}}; 93 | vsapi->createVideoFilter(out, "RFS", &vi, rfsGetFrame, rfsFree, fmParallel, deps, 2, d.get(), core); 94 | d.release(); 95 | } -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.23) 2 | 3 | project(libjulek LANGUAGES CXX) 4 | 5 | add_library(julek SHARED 6 | src/AGM.cpp 7 | src/AutoGain.cpp 8 | src/Butteraugli.cpp 9 | src/ColorMap.cpp 10 | src/RFS.cpp 11 | src/shared.cpp 12 | src/ssimulacra.cpp 13 | src/VisualizeDiffs.cpp 14 | thirdparty/libjxl/tools/ssimulacra.cc 15 | thirdparty/libjxl/tools/ssimulacra2.cc 16 | ) 17 | 18 | target_include_directories(julek PRIVATE 19 | thirdparty/libjxl 20 | thirdparty/libjxl_build/install/include 21 | ) 22 | 23 | set_target_properties(julek PROPERTIES 24 | CXX_EXTENSIONS OFF 25 | CXX_STANDARD 17 26 | CXX_STANDARD_REQUIRED ON 27 | ) 28 | 29 | find_library(libjxl NAMES 30 | jxl 31 | jxl-static 32 | PATHS ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/libjxl_build/install/lib 33 | NO_DEFAULT_PATH 34 | ) 35 | 36 | message(STATUS "julek: find_library returned ${libjxl}") 37 | 38 | find_library(libhwy NAMES 39 | hwy 40 | PATHS ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/libjxl_build/install/lib 41 | NO_DEFAULT_PATH 42 | ) 43 | 44 | message(STATUS "julek: find_library returned ${libhwy}") 45 | 46 | target_link_libraries(julek PRIVATE 47 | ${libjxl} 48 | ${libhwy} 49 | ) 50 | 51 | if((CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64") OR (CMAKE_SYSTEM_PROCESSOR MATCHES "AMD64")) 52 | message(STATUS "julek: ${CMAKE_SYSTEM_PROCESSOR} processor detected, using vectorclass") 53 | target_compile_definitions(julek PRIVATE PLUGIN_X86) 54 | target_include_directories(julek PRIVATE thirdparty/vectorclass) 55 | target_sources(julek PRIVATE 56 | thirdparty/vectorclass/instrset_detect.cpp 57 | src/AVX2/AGM_AVX2.cpp 58 | src/AVX2/AutoGain_AVX2.cpp 59 | ) 60 | 61 | if(MSVC) 62 | set_source_files_properties(src/AVX2/AGM_AVX2.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX2") 63 | set_source_files_properties(src/AVX2/AutoGain_AVX2.cpp PROPERTIES COMPILE_OPTIONS "/arch:AVX2") 64 | else() 65 | set_source_files_properties(src/AVX2/AGM_AVX2.cpp PROPERTIES COMPILE_OPTIONS "-mavx2;-mfma") 66 | set_source_files_properties(src/AVX2/AutoGain_AVX2.cpp PROPERTIES COMPILE_OPTIONS "-mavx2;-mfma") 67 | endif() 68 | 69 | else() 70 | message(STATUS "julek: ${CMAKE_SYSTEM_PROCESSOR} processor detected, unable to use vectorclass") 71 | endif() 72 | 73 | find_package(PkgConfig QUIET MODULE) 74 | 75 | if(PKG_CONFIG_FOUND) 76 | pkg_search_module(VS vapoursynth) 77 | 78 | if(VS_FOUND) 79 | message(STATUS "Found VapourSynth r${VS_VERSION}") 80 | 81 | cmake_path(APPEND install_dir ${VS_LIBDIR} vapoursynth) 82 | target_include_directories(julek PRIVATE ${VS_INCLUDE_DIRS}) 83 | 84 | install(TARGETS julek LIBRARY DESTINATION ${install_dir}) 85 | endif() 86 | endif() 87 | 88 | if(NOT VS_FOUND) 89 | if(EXISTS "C:/Program Files/VapourSynth/sdk/include/vapoursynth") 90 | set(VS_INCLUDE_DIR "C:/Program Files/VapourSynth/sdk/include/vapoursynth" CACHE PATH "Path to VapourSynth headers") 91 | else() 92 | set(VS_INCLUDE_DIR "" CACHE PATH "Path to VapourSynth headers") 93 | endif() 94 | 95 | if(VS_INCLUDE_DIR EQUAL "") 96 | message(WARNING "VapourSynth not found") 97 | endif() 98 | 99 | target_include_directories(julek PRIVATE ${VS_INCLUDE_DIR}) 100 | 101 | install(TARGETS julek LIBRARY RUNTIME) 102 | endif() 103 | 104 | find_package(Git QUIET) 105 | if(GIT_FOUND) 106 | execute_process( 107 | COMMAND ${GIT_EXECUTABLE} describe --tags --long --always 108 | WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" 109 | OUTPUT_VARIABLE VCS_TAG 110 | ) 111 | if(VCS_TAG) 112 | string(STRIP ${VCS_TAG} VCS_TAG) 113 | endif() 114 | endif() 115 | 116 | if(VCS_TAG) 117 | message(STATUS "vapoursynth-julek-plugin ${VCS_TAG}") 118 | else() 119 | message(WARNING "unknown plugin version") 120 | set(VCS_TAG "unknown") 121 | endif() 122 | 123 | configure_file(src/config.h.in config.h) 124 | 125 | include_directories(${CMAKE_CURRENT_BINARY_DIR}) -------------------------------------------------------------------------------- /src/AVX2/AGM_AVX2.cpp: -------------------------------------------------------------------------------- 1 | #ifdef PLUGIN_X86 2 | #include "../shared.h" 3 | 4 | FORCE_INLINE void get_mask_avx2_8(const uint8_t* VS_RESTRICT srcp, uint8_t* VS_RESTRICT dstp, const ptrdiff_t stride, const float frange[], const int width, const int height, const float scaling) { 5 | uint8_t lut[256]; 6 | 7 | for (int i{0}; i < 256; i += 8) { 8 | Vec8f srcv = Vec8f().load_a(frange + i); 9 | Vec8f result = (255.0f * pow(srcv, scaling)); 10 | compress_saturated_s2u(compress_saturated(min(max(truncatei(result + 0.5f), zero_si256()), 255), zero_si256()), zero_si256()).get_low().storel(lut + i); 11 | } 12 | 13 | for (int y{0}; y < height; y++) { 14 | for (int x{0}; x < width; x++) { 15 | dstp[x] = lut[srcp[x]]; 16 | } 17 | srcp += stride; 18 | dstp += stride; 19 | } 20 | } 21 | 22 | FORCE_INLINE void get_mask_avx2_16(const uint16_t* VS_RESTRICT srcp, uint16_t* VS_RESTRICT dstp, const ptrdiff_t stride, const float frange[], const int width, const int height, const float scaling, const int peak, const int shift) { 23 | uint16_t lut[256]; 24 | 25 | for (int i{0}; i < 256; i += 8) { 26 | Vec8f srcv = Vec8f().load_a(frange + i); 27 | Vec8f result = (static_cast(peak) * pow(srcv, scaling)); 28 | compress_saturated_s2u(min(max(truncatei(result + 0.5f), zero_si256()), peak), zero_si256()).get_low().store_a(lut + i); 29 | } 30 | 31 | for (int y{0}; y < height; y++) { 32 | for (int x{0}; x < width; x++) { 33 | dstp[x] = (lut[srcp[x] >> shift]); 34 | } 35 | srcp += stride; 36 | dstp += stride; 37 | } 38 | } 39 | 40 | FORCE_INLINE void get_mask_avx2_f(const float* VS_RESTRICT srcp, float* VS_RESTRICT dstp, const ptrdiff_t stride, const int width, const int height, const float scaling) { 41 | for (int y{0}; y < height; y++) { 42 | for (int x{0}; x < width; x += 8) { 43 | Vec8f srcv = Vec8f().load(srcp + x); 44 | min(max(pow((1.0f - (srcv * mul_add(srcv, mul_add(srcv, mul_add(srcv, mul_add(srcv, 18.188f, -45.47f), 36.624f), -9.466f), 1.124f))), scaling), zero_8f()), 1.0f).store_a(dstp + x); 45 | } 46 | srcp += stride; 47 | dstp += stride; 48 | } 49 | } 50 | 51 | template 52 | void agm_process_avx2(const VSFrame* src, VSFrame* dst, float& avg, const AGMData* const VS_RESTRICT d, const VSAPI* vsapi) noexcept { 53 | for (int plane{0}; plane < d->vi->format.numPlanes; plane++) { 54 | const auto width{vsapi->getFrameWidth(src, plane)}; 55 | const auto height{vsapi->getFrameHeight(src, plane)}; 56 | const auto stride{vsapi->getStride(src, plane) / d->vi->format.bytesPerSample}; 57 | 58 | auto srcp{reinterpret_cast(vsapi->getReadPtr(src, plane))}; 59 | auto dstp{reinterpret_cast(vsapi->getWritePtr(dst, plane))}; 60 | const float scaling{avg * avg * d->luma_scaling}; 61 | 62 | if constexpr (std::is_same_v) { 63 | get_mask_avx2_8(srcp, dstp, stride, d->float_range, width, height, scaling); 64 | } else if constexpr (std::is_same_v) { 65 | get_mask_avx2_16(srcp, dstp, stride, d->float_range, width, height, scaling, d->peak, d->shift); 66 | } else { 67 | get_mask_avx2_f(srcp, dstp, stride, width, height, scaling); 68 | } 69 | } 70 | } 71 | 72 | template void agm_process_avx2(const VSFrame* src, VSFrame* dst, float& avg, const AGMData* const VS_RESTRICT d, const VSAPI* vsapi) noexcept; 73 | template void agm_process_avx2(const VSFrame* src, VSFrame* dst, float& avg, const AGMData* const VS_RESTRICT d, const VSAPI* vsapi) noexcept; 74 | template void agm_process_avx2(const VSFrame* src, VSFrame* dst, float& avg, const AGMData* const VS_RESTRICT d, const VSAPI* vsapi) noexcept; 75 | #endif -------------------------------------------------------------------------------- /src/shared.cpp: -------------------------------------------------------------------------------- 1 | #include "shared.h" 2 | 3 | VS_EXTERNAL_API(void) 4 | VapourSynthPluginInit2(VSPlugin* plugin, const VSPLUGINAPI* vspapi) { 5 | vspapi->configPlugin("com.julek.plugin", "julek", "Julek filters", 3, VAPOURSYNTH_API_VERSION, 0, plugin); 6 | vspapi->registerFunction("AGM", "clip:vnode;luma_scaling:float:opt;", "clip:vnode;", agmCreate, nullptr, plugin); 7 | vspapi->registerFunction("AutoGain", "clip:vnode;planes:int[]:opt;", "clip:vnode;", autogainCreate, nullptr, plugin); 8 | vspapi->registerFunction("Butteraugli", "reference:vnode;distorted:vnode;distmap:int:opt;intensity_target:float:opt;linput:int:opt;", "clip:vnode;", butteraugliCreate, nullptr, plugin); 9 | vspapi->registerFunction("ColorMap", "clip:vnode;type:int:opt;", "clip:vnode;", colormapCreate, nullptr, plugin); 10 | vspapi->registerFunction("RFS", "clip_a:vnode;clip_b:vnode;frames:int[];mismatch:int:opt;", "clip:vnode;", rfsCreate, nullptr, plugin); 11 | vspapi->registerFunction("SSIMULACRA", "reference:vnode;distorted:vnode;feature:int:opt;simple:int:opt;", "clip:vnode;", ssimulacraCreate, nullptr, plugin); 12 | vspapi->registerFunction("VisualizeDiffs", "clip_a:vnode;clip_b:vnode;auto_gain:int:opt;type:int:opt;", "clip:vnode;", visualizediffsCreate, nullptr, plugin); 13 | } 14 | 15 | template 16 | void fill_image(jxl::CodecInOut& ref, jxl::CodecInOut& dist, const VSFrame* src1, const VSFrame* src2, int width, int height, const ptrdiff_t stride, const VSAPI* vsapi) noexcept { 17 | jxl_t tmp1(width, height); 18 | jxl_t tmp2(width, height); 19 | 20 | for (int i = 0; i < 3; ++i) { 21 | auto srcp1{reinterpret_cast(vsapi->getReadPtr(src1, i))}; 22 | auto srcp2{reinterpret_cast(vsapi->getReadPtr(src2, i))}; 23 | 24 | for (int y = 0; y < height; ++y) { 25 | memcpy(tmp1.PlaneRow(i, y), srcp1, width * sizeof(pixel_t)); 26 | memcpy(tmp2.PlaneRow(i, y), srcp2, width * sizeof(pixel_t)); 27 | 28 | srcp1 += stride; 29 | srcp2 += stride; 30 | } 31 | } 32 | 33 | ref.SetFromImage(std::move(jxl::ConvertToFloat(tmp1)), (linput) ? jxl::ColorEncoding::LinearSRGB(false) : jxl::ColorEncoding::SRGB(false)); 34 | dist.SetFromImage(std::move(jxl::ConvertToFloat(tmp2)), (linput) ? jxl::ColorEncoding::LinearSRGB(false) : jxl::ColorEncoding::SRGB(false)); 35 | } 36 | 37 | template 38 | void fill_imageF(jxl::CodecInOut& ref, jxl::CodecInOut& dist, const VSFrame* src1, const VSFrame* src2, int width, int height, const ptrdiff_t stride, const VSAPI* vsapi) noexcept { 39 | jxl::Image3F tmp1(width, height); 40 | jxl::Image3F tmp2(width, height); 41 | 42 | for (int i = 0; i < 3; ++i) { 43 | const float* srcp1{reinterpret_cast(vsapi->getReadPtr(src1, i))}; 44 | const float* srcp2{reinterpret_cast(vsapi->getReadPtr(src2, i))}; 45 | 46 | for (int y = 0; y < height; ++y) { 47 | memcpy(tmp1.PlaneRow(i, y), srcp1, width * sizeof(float)); 48 | memcpy(tmp2.PlaneRow(i, y), srcp2, width * sizeof(float)); 49 | 50 | srcp1 += stride; 51 | srcp2 += stride; 52 | } 53 | } 54 | 55 | ref.SetFromImage(std::move(tmp1), (linput) ? jxl::ColorEncoding::LinearSRGB(false) : jxl::ColorEncoding::SRGB(false)); 56 | dist.SetFromImage(std::move(tmp2), (linput) ? jxl::ColorEncoding::LinearSRGB(false) : jxl::ColorEncoding::SRGB(false)); 57 | } 58 | 59 | template void fill_image(jxl::CodecInOut& ref, jxl::CodecInOut& dist, const VSFrame* src1, const VSFrame* src2, int width, int height, const ptrdiff_t stride, const VSAPI* vsapi) noexcept; 60 | template void fill_image(jxl::CodecInOut& ref, jxl::CodecInOut& dist, const VSFrame* src1, const VSFrame* src2, int width, int height, const ptrdiff_t stride, const VSAPI* vsapi) noexcept; 61 | 62 | template void fill_image(jxl::CodecInOut& ref, jxl::CodecInOut& dist, const VSFrame* src1, const VSFrame* src2, int width, int height, const ptrdiff_t stride, const VSAPI* vsapi) noexcept; 63 | template void fill_image(jxl::CodecInOut& ref, jxl::CodecInOut& dist, const VSFrame* src1, const VSFrame* src2, int width, int height, const ptrdiff_t stride, const VSAPI* vsapi) noexcept; 64 | 65 | template void fill_imageF(jxl::CodecInOut& ref, jxl::CodecInOut& dist, const VSFrame* src1, const VSFrame* src2, int width, int height, const ptrdiff_t stride, const VSAPI* vsapi) noexcept; 66 | template void fill_imageF(jxl::CodecInOut& ref, jxl::CodecInOut& dist, const VSFrame* src1, const VSFrame* src2, int width, int height, const ptrdiff_t stride, const VSAPI* vsapi) noexcept; -------------------------------------------------------------------------------- /src/AGM.cpp: -------------------------------------------------------------------------------- 1 | #include "shared.h" 2 | 3 | template 4 | extern void agm_process_avx2(const VSFrame* src, VSFrame* dst, float& avg, const AGMData* const VS_RESTRICT d, const VSAPI* vsapi) noexcept; 5 | 6 | template 7 | void agm_process_c(const VSFrame* src, VSFrame* dst, float& avg, const AGMData* const VS_RESTRICT d, const VSAPI* vsapi) noexcept { 8 | for (int plane{0}; plane < d->vi->format.numPlanes; plane++) { 9 | const auto width{vsapi->getFrameWidth(src, plane)}; 10 | const auto height{vsapi->getFrameHeight(src, plane)}; 11 | const auto stride{vsapi->getStride(src, plane) / d->vi->format.bytesPerSample}; 12 | 13 | auto srcp{reinterpret_cast(vsapi->getReadPtr(src, plane))}; 14 | auto dstp{reinterpret_cast(vsapi->getWritePtr(dst, plane))}; 15 | const float scaling{avg * avg * d->luma_scaling}; 16 | const float peak{static_cast(d->peak)}; 17 | 18 | pixel_t lut[256]; 19 | 20 | for (int i{0}; i < 256; i++) { 21 | lut[i] = static_cast(std::clamp((std::pow(d->float_range[i], scaling) * peak + 0.5f), 0.0f, peak)); 22 | } 23 | 24 | for (int y{0}; y < height; y++) { 25 | for (int x{0}; x < width; x++) { 26 | if constexpr (std::is_integral_v) { 27 | dstp[x] = lut[srcp[x] >> d->shift]; 28 | } else { 29 | dstp[x] = std::clamp(std::pow(1.0f - (srcp[x] * ((srcp[x] * ((srcp[x] * ((srcp[x] * ((srcp[x] * 18.188f) - 45.47f)) + 36.624f)) - 9.466f)) + 1.124f)), scaling), 0.0f, 1.0f); 30 | } 31 | } 32 | srcp += stride; 33 | dstp += stride; 34 | } 35 | } 36 | } 37 | 38 | static const VSFrame* VS_CC agmGetFrame(int n, int activationReason, void* instanceData, void** frameData, VSFrameContext* frameCtx, VSCore* core, const VSAPI* vsapi) { 39 | auto d{static_cast(instanceData)}; 40 | 41 | if (activationReason == arInitial) { 42 | vsapi->requestFrameFilter(n, d->node, frameCtx); 43 | } else if (activationReason == arAllFramesReady) { 44 | const VSFrame* src = vsapi->getFrameFilter(n, d->node, frameCtx); 45 | 46 | const VSVideoFormat* fi = vsapi->getVideoFrameFormat(src); 47 | int srcw = vsapi->getFrameWidth(src, 0); 48 | int srch = vsapi->getFrameHeight(src, 0); 49 | 50 | VSFrame* dst = vsapi->newVideoFrame(fi, srcw, srch, src, core); 51 | 52 | float avg = vsapi->mapGetFloat(vsapi->getFramePropertiesRO(src), "PlaneStatsAverage", 0, nullptr); 53 | d->process(src, dst, avg, d, vsapi); 54 | 55 | vsapi->freeFrame(src); 56 | VSMap* dstProps = vsapi->getFramePropertiesRW(dst); 57 | vsapi->mapSetInt(dstProps, "_ColorRange", 0, maReplace); 58 | return dst; 59 | } 60 | return nullptr; 61 | } 62 | 63 | static void VS_CC agmFree(void* instanceData, VSCore* core, const VSAPI* vsapi) { 64 | auto d{static_cast(instanceData)}; 65 | vsapi->freeNode(d->node); 66 | delete d; 67 | } 68 | 69 | void VS_CC agmCreate(const VSMap* in, VSMap* out, void* userData, VSCore* core, const VSAPI* vsapi) { 70 | auto d{std::make_unique()}; 71 | int err = 0; 72 | 73 | d->node = vsapi->mapGetNode(in, "clip", 0, nullptr); 74 | d->vi = vsapi->getVideoInfo(d->node); 75 | 76 | d->luma_scaling = vsapi->mapGetFloatSaturated(in, "luma_scaling", 0, &err); 77 | if (err) 78 | d->luma_scaling = 10.0f; 79 | 80 | int bits = d->vi->format.bitsPerSample; 81 | d->shift = bits - 8; 82 | d->peak = (1 << bits) - 1; 83 | 84 | for (int i{0}; i < 256; i++) { 85 | const float x{i / 256.0f}; 86 | d->float_range[i] = (1.0f - (x * ((x * ((x * ((x * ((x * 18.188f) - 45.47f)) + 36.624f)) - 9.466f)) + 1.124f))); 87 | } 88 | 89 | VSMap* args = vsapi->createMap(); 90 | vsapi->mapConsumeNode(args, "clipa", d->node, maAppend); 91 | vsapi->mapSetData(args, "prop", "PlaneStats", -1, dtUtf8, maAppend); 92 | VSPlugin* stdplugin = vsapi->getPluginByID(VSH_STD_PLUGIN_ID, core); 93 | VSMap* ret = vsapi->invoke(stdplugin, "PlaneStats", args); 94 | d->node = vsapi->mapGetNode(ret, "clip", 0, nullptr); 95 | vsapi->freeMap(args); 96 | vsapi->freeMap(ret); 97 | 98 | if (d->vi->format.bytesPerSample == 1) { 99 | d->process = agm_process_c; 100 | #ifdef PLUGIN_X86 101 | d->process = (instrset_detect() >= 8) ? agm_process_avx2 : agm_process_c; 102 | #endif 103 | } else if (d->vi->format.bytesPerSample == 2) { 104 | d->process = agm_process_c; 105 | #ifdef PLUGIN_X86 106 | d->process = (instrset_detect() >= 8) ? agm_process_avx2 : agm_process_c; 107 | #endif 108 | } else { 109 | d->process = agm_process_c; 110 | #ifdef PLUGIN_X86 111 | d->process = (instrset_detect() >= 8) ? agm_process_avx2 : agm_process_c; 112 | #endif 113 | } 114 | 115 | VSFilterDependency deps[] = {{d->node, rpGeneral}}; 116 | vsapi->createVideoFilter(out, "AGM", d->vi, agmGetFrame, agmFree, fmParallel, deps, 1, d.get(), core); 117 | d.release(); 118 | } -------------------------------------------------------------------------------- /src/ssimulacra.cpp: -------------------------------------------------------------------------------- 1 | #include "shared.h" 2 | 3 | struct SSIMULACRAData final { 4 | VSNode* node; 5 | VSNode* node2; 6 | const VSVideoInfo* vi; 7 | bool simple; 8 | int feature; 9 | 10 | void (*fill)(jxl::CodecInOut& ref, jxl::CodecInOut& dist, const VSFrame* src1, const VSFrame* src2, int width, int height, const ptrdiff_t stride, const VSAPI* vsapi) noexcept; 11 | }; 12 | 13 | template 14 | extern void fill_image(jxl::CodecInOut& ref, jxl::CodecInOut& dist, const VSFrame* src1, const VSFrame* src2, int width, int height, const ptrdiff_t stride, const VSAPI* vsapi) noexcept; 15 | template 16 | extern void fill_imageF(jxl::CodecInOut& ref, jxl::CodecInOut& dist, const VSFrame* src1, const VSFrame* src2, int width, int height, const ptrdiff_t stride, const VSAPI* vsapi) noexcept; 17 | 18 | static const VSFrame* VS_CC ssimulacraGetFrame(int n, int activationReason, void* instanceData, void** frameData, VSFrameContext* frameCtx, VSCore* core, const VSAPI* vsapi) { 19 | auto d{static_cast(instanceData)}; 20 | 21 | if (activationReason == arInitial) { 22 | vsapi->requestFrameFilter(n, d->node, frameCtx); 23 | vsapi->requestFrameFilter(n, d->node2, frameCtx); 24 | } else if (activationReason == arAllFramesReady) { 25 | const VSFrame* src = vsapi->getFrameFilter(n, d->node, frameCtx); 26 | const VSFrame* src2 = vsapi->getFrameFilter(n, d->node2, frameCtx); 27 | 28 | int width = vsapi->getFrameWidth(src2, 0); 29 | int height = vsapi->getFrameHeight(src2, 0); 30 | const ptrdiff_t stride = vsapi->getStride(src2, 0) / d->vi->format.bytesPerSample; 31 | 32 | jxl::CodecInOut ref; 33 | jxl::CodecInOut dist; 34 | jxl::ImageF diff_map; 35 | 36 | ref.SetSize(width, height); 37 | dist.SetSize(width, height); 38 | 39 | d->fill(ref, dist, src, src2, width, height, stride, vsapi); 40 | 41 | VSFrame* dst = vsapi->copyFrame(src2, core); 42 | VSMap* dstProps = vsapi->getFramePropertiesRW(dst); 43 | 44 | if (!d->feature || d->feature == 2) { 45 | Msssim msssim{ComputeSSIMULACRA2(ref.Main(), dist.Main())}; 46 | vsapi->mapSetFloat(dstProps, "_SSIMULACRA2", msssim.Score(), maReplace); 47 | } 48 | 49 | if (d->feature) { 50 | ref.TransformTo(jxl::ColorEncoding::LinearSRGB(false), jxl::GetJxlCms()); 51 | dist.TransformTo(jxl::ColorEncoding::LinearSRGB(false), jxl::GetJxlCms()); 52 | ssimulacra::Ssimulacra ssimulacra_{ssimulacra::ComputeDiff(*ref.Main().color(), *dist.Main().color(), d->simple)}; 53 | vsapi->mapSetFloat(dstProps, "_SSIMULACRA", ssimulacra_.Score(), maReplace); 54 | } 55 | 56 | vsapi->freeFrame(src); 57 | vsapi->freeFrame(src2); 58 | return dst; 59 | } 60 | return nullptr; 61 | } 62 | 63 | static void VS_CC ssimulacraFree(void* instanceData, VSCore* core, const VSAPI* vsapi) { 64 | auto d{reinterpret_cast(instanceData)}; 65 | 66 | vsapi->freeNode(d->node); 67 | vsapi->freeNode(d->node2); 68 | delete d; 69 | } 70 | 71 | void VS_CC ssimulacraCreate(const VSMap* in, VSMap* out, void* userData, VSCore* core, const VSAPI* vsapi) { 72 | auto d{std::make_unique()}; 73 | int err{0}; 74 | 75 | d->node = vsapi->mapGetNode(in, "reference", 0, nullptr); 76 | d->node2 = vsapi->mapGetNode(in, "distorted", 0, nullptr); 77 | d->vi = vsapi->getVideoInfo(d->node); 78 | 79 | d->feature = vsapi->mapGetIntSaturated(in, "feature", 0, &err); 80 | if (err) 81 | d->feature = 0; 82 | 83 | d->simple = !!vsapi->mapGetInt(in, "simple", 0, &err); 84 | if (err) 85 | d->simple = false; 86 | 87 | if (d->vi->format.colorFamily != cfRGB) { 88 | vsapi->mapSetError(out, "SSIMULACRA: the clip must be in RGB format."); 89 | vsapi->freeNode(d->node); 90 | vsapi->freeNode(d->node2); 91 | return; 92 | } 93 | 94 | int bits = d->vi->format.bitsPerSample; 95 | if (bits != 8 && bits != 16 && bits != 32) { 96 | vsapi->mapSetError(out, "SSIMULACRA: the clip bit depth must be 8, 16, or 32."); 97 | vsapi->freeNode(d->node); 98 | vsapi->freeNode(d->node2); 99 | return; 100 | } 101 | 102 | if (!vsh::isSameVideoInfo(vsapi->getVideoInfo(d->node2), d->vi)) { 103 | vsapi->mapSetError(out, "SSIMULACRA: both clips must have the same format and dimensions."); 104 | vsapi->freeNode(d->node); 105 | vsapi->freeNode(d->node2); 106 | return; 107 | } 108 | 109 | if (d->feature < 0 || d->feature > 2) { 110 | vsapi->mapSetError(out, "SSIMULACRA: feature must be 0, 1, or 2."); 111 | vsapi->freeNode(d->node); 112 | vsapi->freeNode(d->node2); 113 | return; 114 | } 115 | 116 | if (d->vi->height < 8 || d->vi->width < 8) { 117 | vsapi->mapSetError(out, "SSIMULACRA: minimum image size is 8x8 pixels."); 118 | vsapi->freeNode(d->node); 119 | vsapi->freeNode(d->node2); 120 | return; 121 | } 122 | 123 | switch (d->vi->format.bytesPerSample) { 124 | case 1: 125 | d->fill = fill_image; 126 | break; 127 | case 2: 128 | d->fill = fill_image; 129 | break; 130 | case 4: 131 | d->fill = fill_imageF; 132 | break; 133 | } 134 | 135 | VSFilterDependency deps[]{{d->node, rpGeneral}, {d->node2, rpGeneral}}; 136 | vsapi->createVideoFilter(out, "SSIMULACRA", d->vi, ssimulacraGetFrame, ssimulacraFree, fmParallel, deps, 2, d.get(), core); 137 | d.release(); 138 | } -------------------------------------------------------------------------------- /src/VisualizeDiffs.cpp: -------------------------------------------------------------------------------- 1 | #include "shared.h" 2 | 3 | struct VISUALIZEDIFFSData final { 4 | const VSVideoInfo* vi_in; 5 | VSVideoInfo vi_out; 6 | VSNode* node1; 7 | VSNode* node2; 8 | bool auto_gain; 9 | int type; 10 | void (*autogain_process)(const uint8_t* VS_RESTRICT srcp, uint8_t* VS_RESTRICT dstp, ptrdiff_t stride, const int w, const int h) noexcept; 11 | }; 12 | 13 | extern void colormap_process(const uint8_t* srcp, VSFrame* dst, const ptrdiff_t stride, const int w, const int h, int type, const VSAPI* vsapi) noexcept; 14 | extern void autogainUC_c(const uint8_t* VS_RESTRICT srcp, uint8_t* VS_RESTRICT dstp, ptrdiff_t stride, const int w, const int h) noexcept; 15 | extern void autogainUC_avx2(const uint8_t* VS_RESTRICT srcp, uint8_t* VS_RESTRICT dstp, ptrdiff_t stride, const int w, const int h) noexcept; 16 | 17 | static const VSFrame* VS_CC visualizediffsGetFrame(int n, int activationReason, void* instanceData, void** frameData, VSFrameContext* frameCtx, VSCore* core, const VSAPI* vsapi) { 18 | auto d{static_cast(instanceData)}; 19 | 20 | if (activationReason == arInitial) { 21 | vsapi->requestFrameFilter(n, d->node1, frameCtx); 22 | vsapi->requestFrameFilter(n, d->node2, frameCtx); 23 | } else if (activationReason == arAllFramesReady) { 24 | const VSFrame* src1 = vsapi->getFrameFilter(n, d->node1, frameCtx); 25 | const VSFrame* src2 = vsapi->getFrameFilter(n, d->node2, frameCtx); 26 | const int width = vsapi->getFrameWidth(src1, 0); 27 | const int height = vsapi->getFrameHeight(src1, 0); 28 | const ptrdiff_t stride = vsapi->getStride(src1, 0); 29 | VSFrame* dst = vsapi->newVideoFrame(&d->vi_out.format, width, height, src1, core); 30 | uint8_t* tmpp = vsh::vsh_aligned_malloc(sizeof(uint8_t) * stride * height, 32); 31 | uint8_t* tmpp2 = d->auto_gain ? vsapi->getWritePtr(dst, 0) : tmpp; 32 | 33 | for (int plane{0}; plane < d->vi_in->format.numPlanes; plane++) { 34 | const ptrdiff_t stride = vsapi->getStride(src1, plane); 35 | const uint8_t* src1p = vsapi->getReadPtr(src1, plane); 36 | const uint8_t* src2p = vsapi->getReadPtr(src2, plane); 37 | uint8_t* dstp = vsapi->getWritePtr(dst, plane); 38 | 39 | if (plane == 0) { 40 | for (int y{0}; y < height; y++) { 41 | for (int x{0}; x < width; x++) { 42 | tmpp2[x] = std::abs(src1p[x] - src2p[x]); 43 | } 44 | src1p += stride; 45 | src2p += stride; 46 | tmpp2 += stride; 47 | } 48 | } else { 49 | for (int y{0}; y < height; y++) { 50 | for (int x{0}; x < width; x++) { 51 | tmpp2[x] = tmpp2[x] + std::abs(src1p[x] - src2p[x]); 52 | } 53 | src1p += stride; 54 | src2p += stride; 55 | tmpp2 += stride; 56 | } 57 | } 58 | tmpp2 -= stride * height; 59 | } 60 | 61 | if (d->auto_gain) { 62 | d->autogain_process(tmpp2, tmpp, stride, width, height); 63 | colormap_process(tmpp, dst, stride, width, height, d->type, vsapi); 64 | } else { 65 | colormap_process(tmpp2, dst, stride, width, height, d->type, vsapi); 66 | } 67 | 68 | vsapi->freeFrame(src1); 69 | vsapi->freeFrame(src2); 70 | vsh::vsh_aligned_free(tmpp); 71 | VSMap* dstProps = vsapi->getFramePropertiesRW(dst); 72 | vsapi->mapSetInt(dstProps, "_ColorRange", 0, maReplace); 73 | return dst; 74 | } 75 | return nullptr; 76 | } 77 | 78 | static void VS_CC visualizediffsFree(void* instanceData, VSCore* core, const VSAPI* vsapi) { 79 | auto d{static_cast(instanceData)}; 80 | vsapi->freeNode(d->node1); 81 | vsapi->freeNode(d->node2); 82 | delete d; 83 | } 84 | 85 | void VS_CC visualizediffsCreate(const VSMap* in, VSMap* out, void* userData, VSCore* core, const VSAPI* vsapi) { 86 | auto d{std::make_unique()}; 87 | int err{0}; 88 | 89 | d->node1 = vsapi->mapGetNode(in, "clip_a", 0, nullptr); 90 | d->node2 = vsapi->mapGetNode(in, "clip_b", 0, nullptr); 91 | 92 | d->vi_in = vsapi->getVideoInfo(d->node1); 93 | d->vi_out = *d->vi_in; 94 | vsapi->queryVideoFormat(&d->vi_out.format, cfRGB, stInteger, 8, 0, 0, core); 95 | 96 | d->auto_gain = !!vsapi->mapGetInt(in, "auto_gain", 0, &err); 97 | if (err) 98 | d->auto_gain = true; 99 | 100 | d->type = vsapi->mapGetIntSaturated(in, "type", 0, &err); 101 | if (err) 102 | d->type = 20; 103 | 104 | if (!vsh::isSameVideoInfo(vsapi->getVideoInfo(d->node2), d->vi_in)) { 105 | vsapi->mapSetError(out, "VisualizeDiffs: both clips must have the same format and dimensions."); 106 | vsapi->freeNode(d->node1); 107 | vsapi->freeNode(d->node2); 108 | return; 109 | } 110 | 111 | if (d->vi_in->format.bytesPerSample != 1) { 112 | vsapi->mapSetError(out, "VisualizeDiffs: only 8-bits clip is supported."); 113 | vsapi->freeNode(d->node1); 114 | vsapi->freeNode(d->node2); 115 | return; 116 | } 117 | 118 | if (d->vi_in->format.colorFamily == cfYUV) { 119 | int matrix = (d->vi_in->height > 650) ? 1 : 6; 120 | VSMap* args = vsapi->createMap(); 121 | vsapi->mapConsumeNode(args, "clip", d->node1, maReplace); 122 | vsapi->mapSetInt(args, "matrix_in", matrix, maReplace); 123 | vsapi->mapSetInt(args, "format", pfRGB24, maReplace); 124 | 125 | VSPlugin* vsplugin = vsapi->getPluginByID(VSH_RESIZE_PLUGIN_ID, core); 126 | VSMap* ret = vsapi->invoke(vsplugin, "Bicubic", args); 127 | d->node1 = vsapi->mapGetNode(ret, "clip", 0, nullptr); 128 | vsapi->freeMap(ret); 129 | 130 | vsapi->mapConsumeNode(args, "clip", d->node2, maReplace); 131 | VSMap* ret2 = vsapi->invoke(vsplugin, "Bicubic", args); 132 | d->node2 = vsapi->mapGetNode(ret2, "clip", 0, nullptr); 133 | vsapi->freeMap(ret2); 134 | vsapi->freeMap(args); 135 | } 136 | 137 | #ifdef PLUGIN_X86 138 | d->autogain_process = (instrset_detect() >= 8) ? autogainUC_avx2 : autogainUC_c; 139 | #else 140 | d->autogain_process = autogainUC_c; 141 | #endif 142 | 143 | VSFilterDependency deps[]{{d->node1, rpGeneral}, {d->node2, rpGeneral}}; 144 | vsapi->createVideoFilter(out, "VisualizeDiffs", &d->vi_out, visualizediffsGetFrame, visualizediffsFree, fmParallel, deps, 2, d.get(), core); 145 | d.release(); 146 | } -------------------------------------------------------------------------------- /src/Butteraugli.cpp: -------------------------------------------------------------------------------- 1 | #include "shared.h" 2 | 3 | struct BUTTERAUGLIData final { 4 | VSNode* node; 5 | VSNode* node2; 6 | const VSVideoInfo* vi; 7 | jxl::ButteraugliParams ba_params; 8 | bool distmap; 9 | bool linput; 10 | 11 | void (*hmap)(VSFrame* dst, const jxl::ImageF& distmap, int width, int height, const ptrdiff_t stride, const VSAPI* vsapi) noexcept; 12 | void (*fill)(jxl::CodecInOut& ref, jxl::CodecInOut& dist, const VSFrame* src1, const VSFrame* src2, int width, int height, const ptrdiff_t stride, const VSAPI* vsapi) noexcept; 13 | }; 14 | 15 | template 16 | extern void fill_image(jxl::CodecInOut& ref, jxl::CodecInOut& dist, const VSFrame* src1, const VSFrame* src2, int width, int height, const ptrdiff_t stride, const VSAPI* vsapi) noexcept; 17 | template 18 | extern void fill_imageF(jxl::CodecInOut& ref, jxl::CodecInOut& dist, const VSFrame* src1, const VSFrame* src2, int width, int height, const ptrdiff_t stride, const VSAPI* vsapi) noexcept; 19 | 20 | template 21 | static void heatmap(VSFrame* dst, const jxl::ImageF& distmap, int width, int height, const ptrdiff_t stride, const VSAPI* vsapi) noexcept { 22 | jxl::Image3F buff = jxl::CreateHeatMapImage(distmap, jxl::ButteraugliFuzzyInverse(1.5), jxl::ButteraugliFuzzyInverse(0.5)); 23 | jxl_t tmp(width, height); 24 | jxl::Image3Convert(buff, peak, &tmp); 25 | 26 | for (int i = 0; i < 3; i++) { 27 | auto dstp{reinterpret_cast(vsapi->getWritePtr(dst, i))}; 28 | for (int y = 0; y < height; y++) { 29 | memcpy(dstp, tmp.ConstPlaneRow(i, y), width * sizeof(pixel_t)); 30 | dstp += stride; 31 | } 32 | } 33 | } 34 | 35 | static void heatmapF(VSFrame* dst, const jxl::ImageF& distmap, int width, int height, const ptrdiff_t stride, const VSAPI* vsapi) noexcept { 36 | jxl::Image3F buff = jxl::CreateHeatMapImage(distmap, jxl::ButteraugliFuzzyInverse(1.5), jxl::ButteraugliFuzzyInverse(0.5)); 37 | 38 | for (int i = 0; i < 3; i++) { 39 | float* dstp{reinterpret_cast(vsapi->getWritePtr(dst, i))}; 40 | for (int y = 0; y < height; y++) { 41 | memcpy(dstp, buff.ConstPlaneRow(i, y), width * sizeof(float)); 42 | dstp += stride; 43 | } 44 | } 45 | } 46 | 47 | static const VSFrame* VS_CC butteraugliGetFrame(int n, int activationReason, void* instanceData, void** frameData, VSFrameContext* frameCtx, VSCore* core, const VSAPI* vsapi) { 48 | auto d{static_cast(instanceData)}; 49 | 50 | if (activationReason == arInitial) { 51 | vsapi->requestFrameFilter(n, d->node, frameCtx); 52 | vsapi->requestFrameFilter(n, d->node2, frameCtx); 53 | } else if (activationReason == arAllFramesReady) { 54 | const VSFrame* src = vsapi->getFrameFilter(n, d->node, frameCtx); 55 | const VSFrame* src2 = vsapi->getFrameFilter(n, d->node2, frameCtx); 56 | 57 | int width = vsapi->getFrameWidth(src2, 0); 58 | int height = vsapi->getFrameHeight(src2, 0); 59 | const ptrdiff_t stride = vsapi->getStride(src2, 0) / d->vi->format.bytesPerSample; 60 | 61 | jxl::CodecInOut ref; 62 | jxl::CodecInOut dist; 63 | jxl::ImageF diff_map; 64 | 65 | ref.SetSize(width, height); 66 | dist.SetSize(width, height); 67 | 68 | if (d->linput) { 69 | ref.metadata.m.color_encoding = jxl::ColorEncoding::LinearSRGB(false); 70 | dist.metadata.m.color_encoding = jxl::ColorEncoding::LinearSRGB(false); 71 | } else { 72 | ref.metadata.m.color_encoding = jxl::ColorEncoding::SRGB(false); 73 | dist.metadata.m.color_encoding = jxl::ColorEncoding::SRGB(false); 74 | } 75 | 76 | d->fill(ref, dist, src, src2, width, height, stride, vsapi); 77 | 78 | if (d->distmap) { 79 | VSFrame* dst = vsapi->newVideoFrame(vsapi->getVideoFrameFormat(src2), width, height, src2, core); 80 | double diff_value = jxl::ButteraugliDistance(ref.Main(), dist.Main(), d->ba_params, jxl::GetJxlCms(), &diff_map, nullptr); 81 | d->hmap(dst, diff_map, width, height, stride, vsapi); 82 | VSMap* dstProps = vsapi->getFramePropertiesRW(dst); 83 | vsapi->mapSetFloat(dstProps, "_FrameButteraugli", diff_value, maReplace); 84 | 85 | vsapi->freeFrame(src); 86 | vsapi->freeFrame(src2); 87 | return dst; 88 | } else { 89 | VSFrame* dst = vsapi->copyFrame(src2, core); 90 | VSMap* dstProps = vsapi->getFramePropertiesRW(dst); 91 | vsapi->mapSetFloat(dstProps, "_FrameButteraugli", jxl::ButteraugliDistance(ref.Main(), dist.Main(), d->ba_params, jxl::GetJxlCms(), &diff_map, nullptr), maReplace); 92 | 93 | vsapi->freeFrame(src); 94 | vsapi->freeFrame(src2); 95 | return dst; 96 | } 97 | } 98 | return nullptr; 99 | } 100 | 101 | static void VS_CC butteraugliFree(void* instanceData, VSCore* core, const VSAPI* vsapi) { 102 | auto d{reinterpret_cast(instanceData)}; 103 | 104 | vsapi->freeNode(d->node); 105 | vsapi->freeNode(d->node2); 106 | delete d; 107 | } 108 | 109 | void VS_CC butteraugliCreate(const VSMap* in, VSMap* out, void* userData, VSCore* core, const VSAPI* vsapi) { 110 | auto d{std::make_unique()}; 111 | int err{0}; 112 | 113 | d->node = vsapi->mapGetNode(in, "reference", 0, nullptr); 114 | d->node2 = vsapi->mapGetNode(in, "distorted", 0, nullptr); 115 | d->vi = vsapi->getVideoInfo(d->node); 116 | 117 | d->distmap = !!vsapi->mapGetInt(in, "distmap", 0, &err); 118 | if (err) 119 | d->distmap = false; 120 | 121 | d->linput = !!vsapi->mapGetInt(in, "linput", 0, &err); 122 | if (err) 123 | d->linput = false; 124 | 125 | float intensity_target; 126 | intensity_target = vsapi->mapGetFloatSaturated(in, "intensity_target", 0, &err); 127 | if (err) 128 | intensity_target = 80.0f; 129 | 130 | d->ba_params.hf_asymmetry = 0.8f; 131 | d->ba_params.xmul = 1.0f; 132 | d->ba_params.intensity_target = intensity_target; 133 | 134 | if (intensity_target <= 0.0f) { 135 | vsapi->mapSetError(out, "Butteraugli: intensity_target must be greater than 0.0."); 136 | vsapi->freeNode(d->node); 137 | vsapi->freeNode(d->node2); 138 | return; 139 | } 140 | 141 | if (d->vi->format.colorFamily != cfRGB) { 142 | vsapi->mapSetError(out, "Butteraugli: the clip must be in RGB format."); 143 | vsapi->freeNode(d->node); 144 | vsapi->freeNode(d->node2); 145 | return; 146 | } 147 | 148 | int bits = d->vi->format.bitsPerSample; 149 | if (bits != 8 && bits != 16 && bits != 32) { 150 | vsapi->mapSetError(out, "Butteraugli: the clip bit depth must be 8, 16, or 32."); 151 | vsapi->freeNode(d->node); 152 | vsapi->freeNode(d->node2); 153 | return; 154 | } 155 | 156 | if (!vsh::isSameVideoInfo(vsapi->getVideoInfo(d->node2), d->vi)) { 157 | vsapi->mapSetError(out, "Butteraugli: both clips must have the same format and dimensions."); 158 | vsapi->freeNode(d->node); 159 | vsapi->freeNode(d->node2); 160 | return; 161 | } 162 | 163 | switch (d->vi->format.bytesPerSample) { 164 | case 1: 165 | d->fill = (d->linput) ? fill_image : fill_image; 166 | d->hmap = heatmap; 167 | break; 168 | case 2: 169 | d->fill = (d->linput) ? fill_image : fill_image; 170 | d->hmap = heatmap; 171 | break; 172 | case 4: 173 | d->fill = (d->linput) ? fill_imageF : fill_imageF; 174 | d->hmap = heatmapF; 175 | break; 176 | } 177 | 178 | VSFilterDependency deps[]{{d->node, rpGeneral}, {d->node2, rpGeneral}}; 179 | vsapi->createVideoFilter(out, "Butteraugli", d->vi, butteraugliGetFrame, butteraugliFree, fmParallel, deps, 2, d.get(), core); 180 | d.release(); 181 | } -------------------------------------------------------------------------------- /thirdparty/vectorclass/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 | -------------------------------------------------------------------------------- /src/AutoGain.cpp: -------------------------------------------------------------------------------- 1 | #include "shared.h" 2 | 3 | struct AUTOGAINData final { 4 | const VSVideoInfo* vi; 5 | VSNode* node; 6 | bool process_p[3]; 7 | void (*process)(const uint8_t* VS_RESTRICT srcp, uint8_t* VS_RESTRICT dstp, ptrdiff_t stride, const int w, const int h) noexcept; 8 | }; 9 | 10 | extern void autogainUC_avx2(const uint8_t* VS_RESTRICT srcp, uint8_t* VS_RESTRICT dstp, ptrdiff_t stride, const int w, const int h) noexcept; 11 | template 12 | extern void autogainUS_avx2(const uint8_t* VS_RESTRICT srcp, uint8_t* VS_RESTRICT dstp, ptrdiff_t stride, const int w, const int h) noexcept; 13 | extern void autogainF_avx2(const uint8_t* VS_RESTRICT srcp, uint8_t* VS_RESTRICT dstp, ptrdiff_t stride, const int w, const int h) noexcept; 14 | 15 | void minmaxUC_c(const uint8_t* VS_RESTRICT srcp, uint8_t& dst_min, uint8_t& dst_max, ptrdiff_t stride, const int w, const int h) noexcept { 16 | uint8_t imin = UCHAR_MAX; 17 | uint8_t imax = 0; 18 | for (int y{0}; y < h; y++) { 19 | for (int x{0}; x < w; x++) { 20 | uint8_t v = srcp[x]; 21 | imin = VSMIN(imin, v); 22 | imax = VSMAX(imax, v); 23 | } 24 | srcp += stride; 25 | } 26 | dst_min = imin; 27 | dst_max = imax; 28 | } 29 | 30 | void minmaxUS_c(const uint8_t* VS_RESTRICT srcp, uint16_t& dst_min, uint16_t& dst_max, ptrdiff_t stride, const int w, const int h) noexcept { 31 | uint16_t imin = USHRT_MAX; 32 | uint16_t imax = 0; 33 | for (int y{0}; y < h; y++) { 34 | for (int x{0}; x < w; x++) { 35 | uint16_t v = ((const uint16_t*)srcp)[x]; 36 | imin = VSMIN(imin, v); 37 | imax = VSMAX(imax, v); 38 | } 39 | srcp += stride; 40 | } 41 | dst_min = imin; 42 | dst_max = imax; 43 | } 44 | 45 | void minmaxF_c(const uint8_t* VS_RESTRICT srcp, float& dst_min, float& dst_max, ptrdiff_t stride, const int w, const int h) noexcept { 46 | float imin = 1.0f; 47 | float imax = 0.0f; 48 | for (int y{0}; y < h; y++) { 49 | for (int x{0}; x < w; x++) { 50 | float v = ((const float*)srcp)[x]; 51 | imin = VSMIN(imin, v); 52 | imax = VSMAX(imax, v); 53 | } 54 | srcp += stride; 55 | } 56 | dst_min = imin; 57 | dst_max = imax; 58 | } 59 | 60 | void autogainUC_c(const uint8_t* VS_RESTRICT srcp, uint8_t* VS_RESTRICT dstp, ptrdiff_t stride, const int w, const int h) noexcept { 61 | uint8_t pmin, pmax; 62 | minmaxUC_c(srcp, pmin, pmax, stride, w, h); 63 | for (int y{0}; y < h; y++) { 64 | for (int x{0}; x < w; x++) { 65 | uint8_t v = srcp[x]; 66 | dstp[x] = (uint8_t)(((v - pmin) / (float)(pmax - pmin)) * 255 + 0.5f); 67 | } 68 | srcp += stride; 69 | dstp += stride; 70 | } 71 | } 72 | 73 | template 74 | void autogainUS_c(const uint8_t* VS_RESTRICT srcp, uint8_t* VS_RESTRICT dstp, ptrdiff_t stride, const int w, const int h) noexcept { 75 | uint16_t pmin, pmax; 76 | minmaxUS_c(srcp, pmin, pmax, stride, w, h); 77 | for (int y{0}; y < h; y++) { 78 | for (int x{0}; x < w; x++) { 79 | uint16_t v = ((const uint16_t*)srcp)[x]; 80 | ((uint16_t*)dstp)[x] = (uint16_t)(((v - pmin) / (float)(pmax - pmin)) * peak + 0.5f); 81 | } 82 | srcp += stride; 83 | dstp += stride; 84 | } 85 | } 86 | 87 | void autogainF_c(const uint8_t* VS_RESTRICT srcp, uint8_t* VS_RESTRICT dstp, ptrdiff_t stride, const int w, const int h) noexcept { 88 | float pmin, pmax; 89 | minmaxF_c(srcp, pmin, pmax, stride, w, h); 90 | for (int y{0}; y < h; y++) { 91 | for (int x{0}; x < w; x++) { 92 | float v = ((const float*)srcp)[x]; 93 | ((float*)dstp)[x] = (v - pmin) / (pmax - pmin); 94 | } 95 | srcp += stride; 96 | dstp += stride; 97 | } 98 | } 99 | 100 | static const VSFrame* VS_CC autogainGetFrame(int n, int activationReason, void* instanceData, void** frameData, VSFrameContext* frameCtx, VSCore* core, const VSAPI* vsapi) { 101 | auto d{static_cast(instanceData)}; 102 | 103 | if (activationReason == arInitial) { 104 | vsapi->requestFrameFilter(n, d->node, frameCtx); 105 | } else if (activationReason == arAllFramesReady) { 106 | const VSFrame* src = vsapi->getFrameFilter(n, d->node, frameCtx); 107 | 108 | const int pl[] = {0, 1, 2}; 109 | const VSFrame* fr[] = {d->process_p[0] ? nullptr : src, d->process_p[1] ? nullptr : src, d->process_p[2] ? nullptr : src}; 110 | VSFrame* dst = vsapi->newVideoFrame2(&d->vi->format, d->vi->width, d->vi->height, fr, pl, src, core); 111 | 112 | for (int plane{0}; plane < d->vi->format.numPlanes; plane++) { 113 | if (d->process_p[plane]) { 114 | const ptrdiff_t stride = vsapi->getStride(src, plane); 115 | const int width = vsapi->getFrameWidth(src, plane); 116 | const int height = vsapi->getFrameHeight(src, plane); 117 | const uint8_t* srcp = vsapi->getReadPtr(src, plane); 118 | uint8_t* dstp = vsapi->getWritePtr(dst, plane); 119 | 120 | d->process(srcp, dstp, stride, width, height); 121 | } 122 | } 123 | 124 | vsapi->freeFrame(src); 125 | VSMap* dstProps = vsapi->getFramePropertiesRW(dst); 126 | vsapi->mapSetInt(dstProps, "_ColorRange", 0, maReplace); 127 | return dst; 128 | } 129 | return nullptr; 130 | } 131 | 132 | static void VS_CC autogainFree(void* instanceData, VSCore* core, const VSAPI* vsapi) { 133 | auto d{static_cast(instanceData)}; 134 | vsapi->freeNode(d->node); 135 | delete d; 136 | } 137 | 138 | void VS_CC autogainCreate(const VSMap* in, VSMap* out, void* userData, VSCore* core, const VSAPI* vsapi) { 139 | auto d{std::make_unique()}; 140 | int err{0}; 141 | int nPlan, nElem, getP, i; 142 | 143 | d->node = vsapi->mapGetNode(in, "clip", 0, nullptr); 144 | d->vi = vsapi->getVideoInfo(d->node); 145 | 146 | nPlan = d->vi->format.numPlanes; 147 | nElem = vsapi->mapNumElements(in, "planes"); 148 | 149 | if (nElem <= 0) { 150 | if (d->vi->format.colorFamily == cfRGB) { 151 | for (i = 0; i < 3; i++) 152 | d->process_p[i] = true; 153 | } else { 154 | d->process_p[0] = true; 155 | } 156 | } else { 157 | for (i = 0; i < nElem; i++) { 158 | getP = vsapi->mapGetIntSaturated(in, "planes", i, nullptr); 159 | 160 | if (getP < 0 || getP >= nPlan) { 161 | vsapi->mapSetError(out, "AutoGain: plane index out of range"); 162 | vsapi->freeNode(d->node); 163 | return; 164 | } 165 | 166 | if (d->process_p[getP]) { 167 | vsapi->mapSetError(out, "AutoGain: plane specified twice"); 168 | vsapi->freeNode(d->node); 169 | return; 170 | } 171 | 172 | d->process_p[getP] = true; 173 | } 174 | } 175 | 176 | #ifdef PLUGIN_X86 177 | switch (d->vi->format.bitsPerSample) { 178 | case 8: 179 | d->process = (instrset_detect() >= 8) ? autogainUC_avx2 : autogainUC_c; 180 | break; 181 | case 10: 182 | d->process = (instrset_detect() >= 8) ? autogainUS_avx2<1023> : autogainUS_c<1023>; 183 | break; 184 | case 12: 185 | d->process = (instrset_detect() >= 8) ? autogainUS_avx2<4095> : autogainUS_c<4095>; 186 | break; 187 | case 14: 188 | d->process = (instrset_detect() >= 8) ? autogainUS_avx2<16383> : autogainUS_c<16383>; 189 | break; 190 | case 16: 191 | d->process = (instrset_detect() >= 8) ? autogainUS_avx2<65535> : autogainUS_c<65535>; 192 | break; 193 | case 32: 194 | d->process = (instrset_detect() >= 8) ? autogainF_avx2 : autogainF_c; 195 | break; 196 | } 197 | #else 198 | switch (d->vi->format.bitsPerSample) { 199 | case 8: 200 | d->process = autogainUC_c; 201 | break; 202 | case 10: 203 | d->process = autogainUS_c<1023>; 204 | break; 205 | case 12: 206 | d->process = autogainUS_c<4095>; 207 | break; 208 | case 14: 209 | d->process = autogainUS_c<16383>; 210 | break; 211 | case 16: 212 | d->process = autogainUS_c<65535>; 213 | break; 214 | case 32: 215 | d->process = autogainF_c; 216 | break; 217 | } 218 | #endif 219 | 220 | VSFilterDependency deps[] = {{d->node, rpGeneral}}; 221 | vsapi->createVideoFilter(out, "AutoGain", d->vi, autogainGetFrame, autogainFree, fmParallel, deps, 1, d.get(), core); 222 | d.release(); 223 | } -------------------------------------------------------------------------------- /thirdparty/vectorclass/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 | -------------------------------------------------------------------------------- /thirdparty/vectorclass/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 | -------------------------------------------------------------------------------- /thirdparty/vectorclass/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 | -------------------------------------------------------------------------------- /thirdparty/vectorclass/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 | -------------------------------------------------------------------------------- /thirdparty/vectorclass/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 | --------------------------------------------------------------------------------