From 16ed6c14484703258617f7d6540aafa26626edae Mon Sep 17 00:00:00 2001 From: jatin Date: Mon, 2 Jun 2025 11:49:57 -0700 Subject: [PATCH 1/8] Testing sinpi --- cmake/CPM.cmake | 1122 +---------------------- include/math_approx/src/trig_approx.hpp | 25 + tools/plotter/plotter.cpp | 87 +- 3 files changed, 122 insertions(+), 1112 deletions(-) diff --git a/cmake/CPM.cmake b/cmake/CPM.cmake index 464634a..a139e51 100644 --- a/cmake/CPM.cmake +++ b/cmake/CPM.cmake @@ -1,1116 +1,24 @@ -# CPM.cmake - CMake's missing package manager -# =========================================== -# See https://github.com/cpm-cmake/CPM.cmake for usage and update instructions. +# SPDX-License-Identifier: MIT # -# MIT License -# ----------- -#[[ - Copyright (c) 2019-2022 Lars Melchior and contributors +# SPDX-FileCopyrightText: Copyright (c) 2019-2023 Lars Melchior and contributors - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: +set(CPM_DOWNLOAD_VERSION 0.40.2) +set(CPM_HASH_SUM "c8cdc32c03816538ce22781ed72964dc864b2a34a310d3b7104812a5ca2d835d") - The above copyright notice and this permission notice shall be included in all - copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. -]] - -cmake_minimum_required(VERSION 3.14 FATAL_ERROR) - -# Initialize logging prefix -if(NOT CPM_INDENT) - set(CPM_INDENT - "CPM:" - CACHE INTERNAL "" - ) -endif() - -if(NOT COMMAND cpm_message) - function(cpm_message) - message(${ARGV}) - endfunction() -endif() - -set(CURRENT_CPM_VERSION 1.0.0-development-version) - -get_filename_component(CPM_CURRENT_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}" REALPATH) -if(CPM_DIRECTORY) - if(NOT CPM_DIRECTORY STREQUAL CPM_CURRENT_DIRECTORY) - if(CPM_VERSION VERSION_LESS CURRENT_CPM_VERSION) - message( - AUTHOR_WARNING - "${CPM_INDENT} \ -A dependency is using a more recent CPM version (${CURRENT_CPM_VERSION}) than the current project (${CPM_VERSION}). \ -It is recommended to upgrade CPM to the most recent version. \ -See https://github.com/cpm-cmake/CPM.cmake for more information." - ) - endif() - if(${CMAKE_VERSION} VERSION_LESS "3.17.0") - include(FetchContent) - endif() - return() - endif() - - get_property( - CPM_INITIALIZED GLOBAL "" - PROPERTY CPM_INITIALIZED - SET - ) - if(CPM_INITIALIZED) - return() - endif() -endif() - -set_property(GLOBAL PROPERTY CPM_INITIALIZED true) - -macro(cpm_set_policies) - # the policy allows us to change options without caching - cmake_policy(SET CMP0077 NEW) - set(CMAKE_POLICY_DEFAULT_CMP0077 NEW) - - # the policy allows us to change set(CACHE) without caching - if(POLICY CMP0126) - cmake_policy(SET CMP0126 NEW) - set(CMAKE_POLICY_DEFAULT_CMP0126 NEW) - endif() - - # The policy uses the download time for timestamp, instead of the timestamp in the archive. This - # allows for proper rebuilds when a projects url changes - if(POLICY CMP0135) - cmake_policy(SET CMP0135 NEW) - set(CMAKE_POLICY_DEFAULT_CMP0135 NEW) - endif() -endmacro() -cpm_set_policies() - -option(CPM_USE_LOCAL_PACKAGES "Always try to use `find_package` to get dependencies" - $ENV{CPM_USE_LOCAL_PACKAGES} - ) -option(CPM_LOCAL_PACKAGES_ONLY "Only use `find_package` to get dependencies" - $ENV{CPM_LOCAL_PACKAGES_ONLY} - ) -option(CPM_DOWNLOAD_ALL "Always download dependencies from source" $ENV{CPM_DOWNLOAD_ALL}) -option(CPM_DONT_UPDATE_MODULE_PATH "Don't update the module path to allow using find_package" - $ENV{CPM_DONT_UPDATE_MODULE_PATH} - ) -option(CPM_DONT_CREATE_PACKAGE_LOCK "Don't create a package lock file in the binary path" - $ENV{CPM_DONT_CREATE_PACKAGE_LOCK} - ) -option(CPM_INCLUDE_ALL_IN_PACKAGE_LOCK - "Add all packages added through CPM.cmake to the package lock" - $ENV{CPM_INCLUDE_ALL_IN_PACKAGE_LOCK} - ) -option(CPM_USE_NAMED_CACHE_DIRECTORIES - "Use additional directory of package name in cache on the most nested level." - $ENV{CPM_USE_NAMED_CACHE_DIRECTORIES} - ) - -set(CPM_VERSION - ${CURRENT_CPM_VERSION} - CACHE INTERNAL "" - ) -set(CPM_DIRECTORY - ${CPM_CURRENT_DIRECTORY} - CACHE INTERNAL "" - ) -set(CPM_FILE - ${CMAKE_CURRENT_LIST_FILE} - CACHE INTERNAL "" - ) -set(CPM_PACKAGES - "" - CACHE INTERNAL "" - ) -set(CPM_DRY_RUN - OFF - CACHE INTERNAL "Don't download or configure dependencies (for testing)" - ) - -if(DEFINED ENV{CPM_SOURCE_CACHE}) - set(CPM_SOURCE_CACHE_DEFAULT $ENV{CPM_SOURCE_CACHE}) +if(CPM_SOURCE_CACHE) + set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake") +elseif(DEFINED ENV{CPM_SOURCE_CACHE}) + set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake") else() - set(CPM_SOURCE_CACHE_DEFAULT OFF) -endif() - -set(CPM_SOURCE_CACHE - ${CPM_SOURCE_CACHE_DEFAULT} - CACHE PATH "Directory to download CPM dependencies" - ) - -if(NOT CPM_DONT_UPDATE_MODULE_PATH) - set(CPM_MODULE_PATH - "${CMAKE_BINARY_DIR}/CPM_modules" - CACHE INTERNAL "" - ) - # remove old modules - file(REMOVE_RECURSE ${CPM_MODULE_PATH}) - file(MAKE_DIRECTORY ${CPM_MODULE_PATH}) - # locally added CPM modules should override global packages - set(CMAKE_MODULE_PATH "${CPM_MODULE_PATH};${CMAKE_MODULE_PATH}") + set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake") endif() -if(NOT CPM_DONT_CREATE_PACKAGE_LOCK) - set(CPM_PACKAGE_LOCK_FILE - "${CMAKE_BINARY_DIR}/cpm-package-lock.cmake" - CACHE INTERNAL "" - ) - file(WRITE ${CPM_PACKAGE_LOCK_FILE} - "# CPM Package Lock\n# This file should be committed to version control\n\n" - ) -endif() - -include(FetchContent) - -# Try to infer package name from git repository uri (path or url) -function(cpm_package_name_from_git_uri URI RESULT) - if("${URI}" MATCHES "([^/:]+)/?.git/?$") - set(${RESULT} - ${CMAKE_MATCH_1} - PARENT_SCOPE - ) - else() - unset(${RESULT} PARENT_SCOPE) - endif() -endfunction() - -# Try to infer package name and version from a url -function(cpm_package_name_and_ver_from_url url outName outVer) - if(url MATCHES "[/\\?]([a-zA-Z0-9_\\.-]+)\\.(tar|tar\\.gz|tar\\.bz2|zip|ZIP)(\\?|/|$)") - # We matched an archive - set(filename "${CMAKE_MATCH_1}") - - if(filename MATCHES "([a-zA-Z0-9_\\.-]+)[_-]v?(([0-9]+\\.)*[0-9]+[a-zA-Z0-9]*)") - # We matched - (ie foo-1.2.3) - set(${outName} - "${CMAKE_MATCH_1}" - PARENT_SCOPE - ) - set(${outVer} - "${CMAKE_MATCH_2}" - PARENT_SCOPE - ) - elseif(filename MATCHES "(([0-9]+\\.)+[0-9]+[a-zA-Z0-9]*)") - # We couldn't find a name, but we found a version - # - # In many cases (which we don't handle here) the url would look something like - # `irrelevant/ACTUAL_PACKAGE_NAME/irrelevant/1.2.3.zip`. In such a case we can't possibly - # distinguish the package name from the irrelevant bits. Moreover if we try to match the - # package name from the filename, we'd get bogus at best. - unset(${outName} PARENT_SCOPE) - set(${outVer} - "${CMAKE_MATCH_1}" - PARENT_SCOPE - ) - else() - # Boldly assume that the file name is the package name. - # - # Yes, something like `irrelevant/ACTUAL_NAME/irrelevant/download.zip` will ruin our day, but - # such cases should be quite rare. No popular service does this... we think. - set(${outName} - "${filename}" - PARENT_SCOPE - ) - unset(${outVer} PARENT_SCOPE) - endif() - else() - # No ideas yet what to do with non-archives - unset(${outName} PARENT_SCOPE) - unset(${outVer} PARENT_SCOPE) - endif() -endfunction() - -function(cpm_find_package NAME VERSION) - string(REPLACE " " ";" EXTRA_ARGS "${ARGN}") - find_package(${NAME} ${VERSION} ${EXTRA_ARGS} QUIET) - if(${CPM_ARGS_NAME}_FOUND) - if(DEFINED ${CPM_ARGS_NAME}_VERSION) - set(VERSION ${${CPM_ARGS_NAME}_VERSION}) - endif() - cpm_message(STATUS "${CPM_INDENT} Using local package ${CPM_ARGS_NAME}@${VERSION}") - CPMRegisterPackage(${CPM_ARGS_NAME} "${VERSION}") - set(CPM_PACKAGE_FOUND - YES - PARENT_SCOPE - ) - else() - set(CPM_PACKAGE_FOUND - NO - PARENT_SCOPE - ) - endif() -endfunction() - -# Create a custom FindXXX.cmake module for a CPM package This prevents `find_package(NAME)` from -# finding the system library -function(cpm_create_module_file Name) - if(NOT CPM_DONT_UPDATE_MODULE_PATH) - # erase any previous modules - file(WRITE ${CPM_MODULE_PATH}/Find${Name}.cmake - "include(\"${CPM_FILE}\")\n${ARGN}\nset(${Name}_FOUND TRUE)" - ) - endif() -endfunction() - -# Find a package locally or fallback to CPMAddPackage -function(CPMFindPackage) - set(oneValueArgs NAME VERSION GIT_TAG FIND_PACKAGE_ARGUMENTS) - - cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "" ${ARGN}) - - if(NOT DEFINED CPM_ARGS_VERSION) - if(DEFINED CPM_ARGS_GIT_TAG) - cpm_get_version_from_git_tag("${CPM_ARGS_GIT_TAG}" CPM_ARGS_VERSION) - endif() - endif() - - set(downloadPackage ${CPM_DOWNLOAD_ALL}) - if(DEFINED CPM_DOWNLOAD_${CPM_ARGS_NAME}) - set(downloadPackage ${CPM_DOWNLOAD_${CPM_ARGS_NAME}}) - elseif(DEFINED ENV{CPM_DOWNLOAD_${CPM_ARGS_NAME}}) - set(downloadPackage $ENV{CPM_DOWNLOAD_${CPM_ARGS_NAME}}) - endif() - if(downloadPackage) - CPMAddPackage(${ARGN}) - cpm_export_variables(${CPM_ARGS_NAME}) - return() - endif() - - cpm_check_if_package_already_added(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}") - if(CPM_PACKAGE_ALREADY_ADDED) - cpm_export_variables(${CPM_ARGS_NAME}) - return() - endif() - - cpm_find_package(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}" ${CPM_ARGS_FIND_PACKAGE_ARGUMENTS}) - - if(NOT CPM_PACKAGE_FOUND) - CPMAddPackage(${ARGN}) - cpm_export_variables(${CPM_ARGS_NAME}) - endif() - -endfunction() - -# checks if a package has been added before -function(cpm_check_if_package_already_added CPM_ARGS_NAME CPM_ARGS_VERSION) - if("${CPM_ARGS_NAME}" IN_LIST CPM_PACKAGES) - CPMGetPackageVersion(${CPM_ARGS_NAME} CPM_PACKAGE_VERSION) - if("${CPM_PACKAGE_VERSION}" VERSION_LESS "${CPM_ARGS_VERSION}") - message( - WARNING - "${CPM_INDENT} Requires a newer version of ${CPM_ARGS_NAME} (${CPM_ARGS_VERSION}) than currently included (${CPM_PACKAGE_VERSION})." - ) - endif() - cpm_get_fetch_properties(${CPM_ARGS_NAME}) - set(${CPM_ARGS_NAME}_ADDED NO) - set(CPM_PACKAGE_ALREADY_ADDED - YES - PARENT_SCOPE - ) - cpm_export_variables(${CPM_ARGS_NAME}) - else() - set(CPM_PACKAGE_ALREADY_ADDED - NO - PARENT_SCOPE - ) - endif() -endfunction() - -# Parse the argument of CPMAddPackage in case a single one was provided and convert it to a list of -# arguments which can then be parsed idiomatically. For example gh:foo/bar@1.2.3 will be converted -# to: GITHUB_REPOSITORY;foo/bar;VERSION;1.2.3 -function(cpm_parse_add_package_single_arg arg outArgs) - # Look for a scheme - if("${arg}" MATCHES "^([a-zA-Z]+):(.+)$") - string(TOLOWER "${CMAKE_MATCH_1}" scheme) - set(uri "${CMAKE_MATCH_2}") - - # Check for CPM-specific schemes - if(scheme STREQUAL "gh") - set(out "GITHUB_REPOSITORY;${uri}") - set(packageType "git") - elseif(scheme STREQUAL "gl") - set(out "GITLAB_REPOSITORY;${uri}") - set(packageType "git") - elseif(scheme STREQUAL "bb") - set(out "BITBUCKET_REPOSITORY;${uri}") - set(packageType "git") - # A CPM-specific scheme was not found. Looks like this is a generic URL so try to determine - # type - elseif(arg MATCHES ".git/?(@|#|$)") - set(out "GIT_REPOSITORY;${arg}") - set(packageType "git") - else() - # Fall back to a URL - set(out "URL;${arg}") - set(packageType "archive") - - # We could also check for SVN since FetchContent supports it, but SVN is so rare these days. - # We just won't bother with the additional complexity it will induce in this function. SVN is - # done by multi-arg - endif() - else() - if(arg MATCHES ".git/?(@|#|$)") - set(out "GIT_REPOSITORY;${arg}") - set(packageType "git") - else() - # Give up - message(FATAL_ERROR "${CPM_INDENT} Can't determine package type of '${arg}'") - endif() - endif() - - # For all packages we interpret @... as version. Only replace the last occurrence. Thus URIs - # containing '@' can be used - string(REGEX REPLACE "@([^@]+)$" ";VERSION;\\1" out "${out}") - - # Parse the rest according to package type - if(packageType STREQUAL "git") - # For git repos we interpret #... as a tag or branch or commit hash - string(REGEX REPLACE "#([^#]+)$" ";GIT_TAG;\\1" out "${out}") - elseif(packageType STREQUAL "archive") - # For archives we interpret #... as a URL hash. - string(REGEX REPLACE "#([^#]+)$" ";URL_HASH;\\1" out "${out}") - # We don't try to parse the version if it's not provided explicitly. cpm_get_version_from_url - # should do this at a later point - else() - # We should never get here. This is an assertion and hitting it means there's a bug in the code - # above. A packageType was set, but not handled by this if-else. - message(FATAL_ERROR "${CPM_INDENT} Unsupported package type '${packageType}' of '${arg}'") - endif() - - set(${outArgs} - ${out} - PARENT_SCOPE - ) -endfunction() - -# Check that the working directory for a git repo is clean -function(cpm_check_git_working_dir_is_clean repoPath gitTag isClean) - - find_package(Git REQUIRED) - - if(NOT GIT_EXECUTABLE) - # No git executable, assume directory is clean - set(${isClean} - TRUE - PARENT_SCOPE - ) - return() - endif() - - # check for uncommitted changes - execute_process( - COMMAND ${GIT_EXECUTABLE} status --porcelain - RESULT_VARIABLE resultGitStatus - OUTPUT_VARIABLE repoStatus - OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET - WORKING_DIRECTORY ${repoPath} - ) - if(resultGitStatus) - # not supposed to happen, assume clean anyway - message(WARNING "${CPM_INDENT} Calling git status on folder ${repoPath} failed") - set(${isClean} - TRUE - PARENT_SCOPE - ) - return() - endif() - - if(NOT "${repoStatus}" STREQUAL "") - set(${isClean} - FALSE - PARENT_SCOPE - ) - return() - endif() - - # check for committed changes - execute_process( - COMMAND ${GIT_EXECUTABLE} diff -s --exit-code ${gitTag} - RESULT_VARIABLE resultGitDiff - OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_QUIET - WORKING_DIRECTORY ${repoPath} - ) - - if(${resultGitDiff} EQUAL 0) - set(${isClean} - TRUE - PARENT_SCOPE - ) - else() - set(${isClean} - FALSE - PARENT_SCOPE - ) - endif() - -endfunction() - -# method to overwrite internal FetchContent properties, to allow using CPM.cmake to overload -# FetchContent calls. As these are internal cmake properties, this method should be used carefully -# and may need modification in future CMake versions. Source: -# https://github.com/Kitware/CMake/blob/dc3d0b5a0a7d26d43d6cfeb511e224533b5d188f/Modules/FetchContent.cmake#L1152 -function(cpm_override_fetchcontent contentName) - cmake_parse_arguments(PARSE_ARGV 1 arg "" "SOURCE_DIR;BINARY_DIR" "") - if(NOT "${arg_UNPARSED_ARGUMENTS}" STREQUAL "") - message(FATAL_ERROR "${CPM_INDENT} Unsupported arguments: ${arg_UNPARSED_ARGUMENTS}") - endif() - - string(TOLOWER ${contentName} contentNameLower) - set(prefix "_FetchContent_${contentNameLower}") - - set(propertyName "${prefix}_sourceDir") - define_property( - GLOBAL - PROPERTY ${propertyName} - BRIEF_DOCS "Internal implementation detail of FetchContent_Populate()" - FULL_DOCS "Details used by FetchContent_Populate() for ${contentName}" - ) - set_property(GLOBAL PROPERTY ${propertyName} "${arg_SOURCE_DIR}") - - set(propertyName "${prefix}_binaryDir") - define_property( - GLOBAL - PROPERTY ${propertyName} - BRIEF_DOCS "Internal implementation detail of FetchContent_Populate()" - FULL_DOCS "Details used by FetchContent_Populate() for ${contentName}" - ) - set_property(GLOBAL PROPERTY ${propertyName} "${arg_BINARY_DIR}") - - set(propertyName "${prefix}_populated") - define_property( - GLOBAL - PROPERTY ${propertyName} - BRIEF_DOCS "Internal implementation detail of FetchContent_Populate()" - FULL_DOCS "Details used by FetchContent_Populate() for ${contentName}" - ) - set_property(GLOBAL PROPERTY ${propertyName} TRUE) -endfunction() - -# Download and add a package from source -function(CPMAddPackage) - cpm_set_policies() - - list(LENGTH ARGN argnLength) - if(argnLength EQUAL 1) - cpm_parse_add_package_single_arg("${ARGN}" ARGN) - - # The shorthand syntax implies EXCLUDE_FROM_ALL - set(ARGN "${ARGN};EXCLUDE_FROM_ALL;YES") - endif() - - set(oneValueArgs - NAME - FORCE - VERSION - GIT_TAG - DOWNLOAD_ONLY - GITHUB_REPOSITORY - GITLAB_REPOSITORY - BITBUCKET_REPOSITORY - GIT_REPOSITORY - SOURCE_DIR - DOWNLOAD_COMMAND - FIND_PACKAGE_ARGUMENTS - NO_CACHE - GIT_SHALLOW - EXCLUDE_FROM_ALL - SOURCE_SUBDIR - ) - - set(multiValueArgs URL OPTIONS) - - cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "${multiValueArgs}" "${ARGN}") +# Expand relative path. This is important if the provided path contains a tilde (~) +get_filename_component(CPM_DOWNLOAD_LOCATION ${CPM_DOWNLOAD_LOCATION} ABSOLUTE) - # Set default values for arguments - - if(NOT DEFINED CPM_ARGS_VERSION) - if(DEFINED CPM_ARGS_GIT_TAG) - cpm_get_version_from_git_tag("${CPM_ARGS_GIT_TAG}" CPM_ARGS_VERSION) - endif() - endif() - - if(CPM_ARGS_DOWNLOAD_ONLY) - set(DOWNLOAD_ONLY ${CPM_ARGS_DOWNLOAD_ONLY}) - else() - set(DOWNLOAD_ONLY NO) - endif() - - if(DEFINED CPM_ARGS_GITHUB_REPOSITORY) - set(CPM_ARGS_GIT_REPOSITORY "https://github.com/${CPM_ARGS_GITHUB_REPOSITORY}.git") - elseif(DEFINED CPM_ARGS_GITLAB_REPOSITORY) - set(CPM_ARGS_GIT_REPOSITORY "https://gitlab.com/${CPM_ARGS_GITLAB_REPOSITORY}.git") - elseif(DEFINED CPM_ARGS_BITBUCKET_REPOSITORY) - set(CPM_ARGS_GIT_REPOSITORY "https://bitbucket.org/${CPM_ARGS_BITBUCKET_REPOSITORY}.git") - endif() - - if(DEFINED CPM_ARGS_GIT_REPOSITORY) - list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_REPOSITORY ${CPM_ARGS_GIT_REPOSITORY}) - if(NOT DEFINED CPM_ARGS_GIT_TAG) - set(CPM_ARGS_GIT_TAG v${CPM_ARGS_VERSION}) - endif() - - # If a name wasn't provided, try to infer it from the git repo - if(NOT DEFINED CPM_ARGS_NAME) - cpm_package_name_from_git_uri(${CPM_ARGS_GIT_REPOSITORY} CPM_ARGS_NAME) - endif() - endif() - - set(CPM_SKIP_FETCH FALSE) - - if(DEFINED CPM_ARGS_GIT_TAG) - list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_TAG ${CPM_ARGS_GIT_TAG}) - # If GIT_SHALLOW is explicitly specified, honor the value. - if(DEFINED CPM_ARGS_GIT_SHALLOW) - list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_SHALLOW ${CPM_ARGS_GIT_SHALLOW}) - endif() - endif() - - if(DEFINED CPM_ARGS_URL) - # If a name or version aren't provided, try to infer them from the URL - list(GET CPM_ARGS_URL 0 firstUrl) - cpm_package_name_and_ver_from_url(${firstUrl} nameFromUrl verFromUrl) - # If we fail to obtain name and version from the first URL, we could try other URLs if any. - # However multiple URLs are expected to be quite rare, so for now we won't bother. - - # If the caller provided their own name and version, they trump the inferred ones. - if(NOT DEFINED CPM_ARGS_NAME) - set(CPM_ARGS_NAME ${nameFromUrl}) - endif() - if(NOT DEFINED CPM_ARGS_VERSION) - set(CPM_ARGS_VERSION ${verFromUrl}) - endif() - - list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS URL "${CPM_ARGS_URL}") - endif() - - # Check for required arguments - - if(NOT DEFINED CPM_ARGS_NAME) - message( - FATAL_ERROR - "${CPM_INDENT} 'NAME' was not provided and couldn't be automatically inferred for package added with arguments: '${ARGN}'" - ) - endif() - - # Check if package has been added before - cpm_check_if_package_already_added(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}") - if(CPM_PACKAGE_ALREADY_ADDED) - cpm_export_variables(${CPM_ARGS_NAME}) - return() - endif() - - # Check for manual overrides - if(NOT CPM_ARGS_FORCE AND NOT "${CPM_${CPM_ARGS_NAME}_SOURCE}" STREQUAL "") - set(PACKAGE_SOURCE ${CPM_${CPM_ARGS_NAME}_SOURCE}) - set(CPM_${CPM_ARGS_NAME}_SOURCE "") - CPMAddPackage( - NAME "${CPM_ARGS_NAME}" - SOURCE_DIR "${PACKAGE_SOURCE}" - EXCLUDE_FROM_ALL "${CPM_ARGS_EXCLUDE_FROM_ALL}" - OPTIONS "${CPM_ARGS_OPTIONS}" - SOURCE_SUBDIR "${CPM_ARGS_SOURCE_SUBDIR}" - DOWNLOAD_ONLY "${DOWNLOAD_ONLY}" - FORCE True - ) - cpm_export_variables(${CPM_ARGS_NAME}) - return() - endif() - - # Check for available declaration - if(NOT CPM_ARGS_FORCE AND NOT "${CPM_DECLARATION_${CPM_ARGS_NAME}}" STREQUAL "") - set(declaration ${CPM_DECLARATION_${CPM_ARGS_NAME}}) - set(CPM_DECLARATION_${CPM_ARGS_NAME} "") - CPMAddPackage(${declaration}) - cpm_export_variables(${CPM_ARGS_NAME}) - # checking again to ensure version and option compatibility - cpm_check_if_package_already_added(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}") - return() - endif() - - if(CPM_USE_LOCAL_PACKAGES OR CPM_LOCAL_PACKAGES_ONLY) - cpm_find_package(${CPM_ARGS_NAME} "${CPM_ARGS_VERSION}" ${CPM_ARGS_FIND_PACKAGE_ARGUMENTS}) - - if(CPM_PACKAGE_FOUND) - cpm_export_variables(${CPM_ARGS_NAME}) - return() - endif() - - if(CPM_LOCAL_PACKAGES_ONLY) - message( - SEND_ERROR - "${CPM_INDENT} ${CPM_ARGS_NAME} not found via find_package(${CPM_ARGS_NAME} ${CPM_ARGS_VERSION})" - ) - endif() - endif() - - CPMRegisterPackage("${CPM_ARGS_NAME}" "${CPM_ARGS_VERSION}") - - if(DEFINED CPM_ARGS_GIT_TAG) - set(PACKAGE_INFO "${CPM_ARGS_GIT_TAG}") - elseif(DEFINED CPM_ARGS_SOURCE_DIR) - set(PACKAGE_INFO "${CPM_ARGS_SOURCE_DIR}") - else() - set(PACKAGE_INFO "${CPM_ARGS_VERSION}") - endif() - - if(DEFINED FETCHCONTENT_BASE_DIR) - # respect user's FETCHCONTENT_BASE_DIR if set - set(CPM_FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR}) - else() - set(CPM_FETCHCONTENT_BASE_DIR ${CMAKE_BINARY_DIR}/_deps) - endif() - - if(DEFINED CPM_ARGS_DOWNLOAD_COMMAND) - list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS DOWNLOAD_COMMAND ${CPM_ARGS_DOWNLOAD_COMMAND}) - elseif(DEFINED CPM_ARGS_SOURCE_DIR) - list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS SOURCE_DIR ${CPM_ARGS_SOURCE_DIR}) - if(NOT IS_ABSOLUTE ${CPM_ARGS_SOURCE_DIR}) - # Expand `CPM_ARGS_SOURCE_DIR` relative path. This is important because EXISTS doesn't work - # for relative paths. - get_filename_component( - source_directory ${CPM_ARGS_SOURCE_DIR} REALPATH BASE_DIR ${CMAKE_CURRENT_BINARY_DIR} - ) - else() - set(source_directory ${CPM_ARGS_SOURCE_DIR}) - endif() - if(NOT EXISTS ${source_directory}) - string(TOLOWER ${CPM_ARGS_NAME} lower_case_name) - # remove timestamps so CMake will re-download the dependency - file(REMOVE_RECURSE "${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-subbuild") - endif() - elseif(CPM_SOURCE_CACHE AND NOT CPM_ARGS_NO_CACHE) - string(TOLOWER ${CPM_ARGS_NAME} lower_case_name) - set(origin_parameters ${CPM_ARGS_UNPARSED_ARGUMENTS}) - list(SORT origin_parameters) - if(CPM_USE_NAMED_CACHE_DIRECTORIES) - string(SHA1 origin_hash "${origin_parameters};NEW_CACHE_STRUCTURE_TAG") - set(download_directory ${CPM_SOURCE_CACHE}/${lower_case_name}/${origin_hash}/${CPM_ARGS_NAME}) - else() - string(SHA1 origin_hash "${origin_parameters}") - set(download_directory ${CPM_SOURCE_CACHE}/${lower_case_name}/${origin_hash}) - endif() - # Expand `download_directory` relative path. This is important because EXISTS doesn't work for - # relative paths. - get_filename_component(download_directory ${download_directory} ABSOLUTE) - list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS SOURCE_DIR ${download_directory}) - if(EXISTS ${download_directory}) - cpm_store_fetch_properties( - ${CPM_ARGS_NAME} "${download_directory}" - "${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-build" - ) - cpm_get_fetch_properties("${CPM_ARGS_NAME}") - - if(DEFINED CPM_ARGS_GIT_TAG AND NOT (PATCH_COMMAND IN_LIST CPM_ARGS_UNPARSED_ARGUMENTS)) - # warn if cache has been changed since checkout - cpm_check_git_working_dir_is_clean(${download_directory} ${CPM_ARGS_GIT_TAG} IS_CLEAN) - if(NOT ${IS_CLEAN}) - message( - WARNING "${CPM_INDENT} Cache for ${CPM_ARGS_NAME} (${download_directory}) is dirty" - ) - endif() - endif() - - cpm_add_subdirectory( - "${CPM_ARGS_NAME}" "${DOWNLOAD_ONLY}" - "${${CPM_ARGS_NAME}_SOURCE_DIR}/${CPM_ARGS_SOURCE_SUBDIR}" "${${CPM_ARGS_NAME}_BINARY_DIR}" - "${CPM_ARGS_EXCLUDE_FROM_ALL}" "${CPM_ARGS_OPTIONS}" - ) - set(PACKAGE_INFO "${PACKAGE_INFO} at ${download_directory}") - - # As the source dir is already cached/populated, we override the call to FetchContent. - set(CPM_SKIP_FETCH TRUE) - cpm_override_fetchcontent( - "${lower_case_name}" SOURCE_DIR "${${CPM_ARGS_NAME}_SOURCE_DIR}/${CPM_ARGS_SOURCE_SUBDIR}" - BINARY_DIR "${${CPM_ARGS_NAME}_BINARY_DIR}" - ) - - else() - # Enable shallow clone when GIT_TAG is not a commit hash. Our guess may not be accurate, but - # it should guarantee no commit hash get mis-detected. - if(NOT DEFINED CPM_ARGS_GIT_SHALLOW) - cpm_is_git_tag_commit_hash("${CPM_ARGS_GIT_TAG}" IS_HASH) - if(NOT ${IS_HASH}) - list(APPEND CPM_ARGS_UNPARSED_ARGUMENTS GIT_SHALLOW TRUE) - endif() - endif() - - # remove timestamps so CMake will re-download the dependency - file(REMOVE_RECURSE ${CPM_FETCHCONTENT_BASE_DIR}/${lower_case_name}-subbuild) - set(PACKAGE_INFO "${PACKAGE_INFO} to ${download_directory}") - endif() - endif() - - cpm_create_module_file(${CPM_ARGS_NAME} "CPMAddPackage(\"${ARGN}\")") - - if(CPM_PACKAGE_LOCK_ENABLED) - if((CPM_ARGS_VERSION AND NOT CPM_ARGS_SOURCE_DIR) OR CPM_INCLUDE_ALL_IN_PACKAGE_LOCK) - cpm_add_to_package_lock(${CPM_ARGS_NAME} "${ARGN}") - elseif(CPM_ARGS_SOURCE_DIR) - cpm_add_comment_to_package_lock(${CPM_ARGS_NAME} "local directory") - else() - cpm_add_comment_to_package_lock(${CPM_ARGS_NAME} "${ARGN}") - endif() - endif() - - cpm_message( - STATUS "${CPM_INDENT} Adding package ${CPM_ARGS_NAME}@${CPM_ARGS_VERSION} (${PACKAGE_INFO})" - ) - - if(NOT CPM_SKIP_FETCH) - cpm_declare_fetch( - "${CPM_ARGS_NAME}" "${CPM_ARGS_VERSION}" "${PACKAGE_INFO}" "${CPM_ARGS_UNPARSED_ARGUMENTS}" - ) - cpm_fetch_package("${CPM_ARGS_NAME}" populated) - if(${populated}) - cpm_add_subdirectory( - "${CPM_ARGS_NAME}" "${DOWNLOAD_ONLY}" - "${${CPM_ARGS_NAME}_SOURCE_DIR}/${CPM_ARGS_SOURCE_SUBDIR}" "${${CPM_ARGS_NAME}_BINARY_DIR}" - "${CPM_ARGS_EXCLUDE_FROM_ALL}" "${CPM_ARGS_OPTIONS}" - ) - endif() - cpm_get_fetch_properties("${CPM_ARGS_NAME}") - endif() - - set(${CPM_ARGS_NAME}_ADDED YES) - cpm_export_variables("${CPM_ARGS_NAME}") -endfunction() - -# Fetch a previously declared package -macro(CPMGetPackage Name) - if(DEFINED "CPM_DECLARATION_${Name}") - CPMAddPackage(NAME ${Name}) - else() - message(SEND_ERROR "${CPM_INDENT} Cannot retrieve package ${Name}: no declaration available") - endif() -endmacro() - -# export variables available to the caller to the parent scope expects ${CPM_ARGS_NAME} to be set -macro(cpm_export_variables name) - set(${name}_SOURCE_DIR - "${${name}_SOURCE_DIR}" - PARENT_SCOPE - ) - set(${name}_BINARY_DIR - "${${name}_BINARY_DIR}" - PARENT_SCOPE - ) - set(${name}_ADDED - "${${name}_ADDED}" - PARENT_SCOPE - ) - set(CPM_LAST_PACKAGE_NAME - "${name}" - PARENT_SCOPE - ) -endmacro() - -# declares a package, so that any call to CPMAddPackage for the package name will use these -# arguments instead. Previous declarations will not be overridden. -macro(CPMDeclarePackage Name) - if(NOT DEFINED "CPM_DECLARATION_${Name}") - set("CPM_DECLARATION_${Name}" "${ARGN}") - endif() -endmacro() - -function(cpm_add_to_package_lock Name) - if(NOT CPM_DONT_CREATE_PACKAGE_LOCK) - cpm_prettify_package_arguments(PRETTY_ARGN false ${ARGN}) - file(APPEND ${CPM_PACKAGE_LOCK_FILE} "# ${Name}\nCPMDeclarePackage(${Name}\n${PRETTY_ARGN})\n") - endif() -endfunction() - -function(cpm_add_comment_to_package_lock Name) - if(NOT CPM_DONT_CREATE_PACKAGE_LOCK) - cpm_prettify_package_arguments(PRETTY_ARGN true ${ARGN}) - file(APPEND ${CPM_PACKAGE_LOCK_FILE} - "# ${Name} (unversioned)\n# CPMDeclarePackage(${Name}\n${PRETTY_ARGN}#)\n" - ) - endif() -endfunction() - -# includes the package lock file if it exists and creates a target `cpm-update-package-lock` to -# update it -macro(CPMUsePackageLock file) - if(NOT CPM_DONT_CREATE_PACKAGE_LOCK) - get_filename_component(CPM_ABSOLUTE_PACKAGE_LOCK_PATH ${file} ABSOLUTE) - if(EXISTS ${CPM_ABSOLUTE_PACKAGE_LOCK_PATH}) - include(${CPM_ABSOLUTE_PACKAGE_LOCK_PATH}) - endif() - if(NOT TARGET cpm-update-package-lock) - add_custom_target( - cpm-update-package-lock COMMAND ${CMAKE_COMMAND} -E copy ${CPM_PACKAGE_LOCK_FILE} - ${CPM_ABSOLUTE_PACKAGE_LOCK_PATH} - ) - endif() - set(CPM_PACKAGE_LOCK_ENABLED true) - endif() -endmacro() - -# registers a package that has been added to CPM -function(CPMRegisterPackage PACKAGE VERSION) - list(APPEND CPM_PACKAGES ${PACKAGE}) - set(CPM_PACKAGES - ${CPM_PACKAGES} - CACHE INTERNAL "" - ) - set("CPM_PACKAGE_${PACKAGE}_VERSION" - ${VERSION} - CACHE INTERNAL "" - ) -endfunction() - -# retrieve the current version of the package to ${OUTPUT} -function(CPMGetPackageVersion PACKAGE OUTPUT) - set(${OUTPUT} - "${CPM_PACKAGE_${PACKAGE}_VERSION}" - PARENT_SCOPE - ) -endfunction() - -# declares a package in FetchContent_Declare -function(cpm_declare_fetch PACKAGE VERSION INFO) - if(${CPM_DRY_RUN}) - cpm_message(STATUS "${CPM_INDENT} Package not declared (dry run)") - return() - endif() - - FetchContent_Declare(${PACKAGE} ${ARGN}) -endfunction() - -# returns properties for a package previously defined by cpm_declare_fetch -function(cpm_get_fetch_properties PACKAGE) - if(${CPM_DRY_RUN}) - return() - endif() - - set(${PACKAGE}_SOURCE_DIR - "${CPM_PACKAGE_${PACKAGE}_SOURCE_DIR}" - PARENT_SCOPE - ) - set(${PACKAGE}_BINARY_DIR - "${CPM_PACKAGE_${PACKAGE}_BINARY_DIR}" - PARENT_SCOPE - ) -endfunction() - -function(cpm_store_fetch_properties PACKAGE source_dir binary_dir) - if(${CPM_DRY_RUN}) - return() - endif() - - set(CPM_PACKAGE_${PACKAGE}_SOURCE_DIR - "${source_dir}" - CACHE INTERNAL "" - ) - set(CPM_PACKAGE_${PACKAGE}_BINARY_DIR - "${binary_dir}" - CACHE INTERNAL "" - ) -endfunction() - -# adds a package as a subdirectory if viable, according to provided options -function( - cpm_add_subdirectory - PACKAGE - DOWNLOAD_ONLY - SOURCE_DIR - BINARY_DIR - EXCLUDE - OPTIONS +file(DOWNLOAD + https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake + ${CPM_DOWNLOAD_LOCATION} EXPECTED_HASH SHA256=${CPM_HASH_SUM} ) - if(NOT DOWNLOAD_ONLY AND EXISTS ${SOURCE_DIR}/CMakeLists.txt) - if(EXCLUDE) - set(addSubdirectoryExtraArgs EXCLUDE_FROM_ALL) - else() - set(addSubdirectoryExtraArgs "") - endif() - if(OPTIONS) - foreach(OPTION ${OPTIONS}) - cpm_parse_option("${OPTION}") - set(${OPTION_KEY} "${OPTION_VALUE}") - endforeach() - endif() - set(CPM_OLD_INDENT "${CPM_INDENT}") - set(CPM_INDENT "${CPM_INDENT} ${PACKAGE}:") - add_subdirectory(${SOURCE_DIR} ${BINARY_DIR} ${addSubdirectoryExtraArgs}) - set(CPM_INDENT "${CPM_OLD_INDENT}") - endif() -endfunction() - -# downloads a previously declared package via FetchContent and exports the variables -# `${PACKAGE}_SOURCE_DIR` and `${PACKAGE}_BINARY_DIR` to the parent scope -function(cpm_fetch_package PACKAGE populated) - set(${populated} - FALSE - PARENT_SCOPE - ) - if(${CPM_DRY_RUN}) - cpm_message(STATUS "${CPM_INDENT} Package ${PACKAGE} not fetched (dry run)") - return() - endif() - - FetchContent_GetProperties(${PACKAGE}) - - string(TOLOWER "${PACKAGE}" lower_case_name) - - if(NOT ${lower_case_name}_POPULATED) - FetchContent_Populate(${PACKAGE}) - set(${populated} - TRUE - PARENT_SCOPE - ) - endif() - - cpm_store_fetch_properties( - ${CPM_ARGS_NAME} ${${lower_case_name}_SOURCE_DIR} ${${lower_case_name}_BINARY_DIR} - ) - - set(${PACKAGE}_SOURCE_DIR - ${${lower_case_name}_SOURCE_DIR} - PARENT_SCOPE - ) - set(${PACKAGE}_BINARY_DIR - ${${lower_case_name}_BINARY_DIR} - PARENT_SCOPE - ) -endfunction() - -# splits a package option -function(cpm_parse_option OPTION) - string(REGEX MATCH "^[^ ]+" OPTION_KEY "${OPTION}") - string(LENGTH "${OPTION}" OPTION_LENGTH) - string(LENGTH "${OPTION_KEY}" OPTION_KEY_LENGTH) - if(OPTION_KEY_LENGTH STREQUAL OPTION_LENGTH) - # no value for key provided, assume user wants to set option to "ON" - set(OPTION_VALUE "ON") - else() - math(EXPR OPTION_KEY_LENGTH "${OPTION_KEY_LENGTH}+1") - string(SUBSTRING "${OPTION}" "${OPTION_KEY_LENGTH}" "-1" OPTION_VALUE) - endif() - set(OPTION_KEY - "${OPTION_KEY}" - PARENT_SCOPE - ) - set(OPTION_VALUE - "${OPTION_VALUE}" - PARENT_SCOPE - ) -endfunction() - -# guesses the package version from a git tag -function(cpm_get_version_from_git_tag GIT_TAG RESULT) - string(LENGTH ${GIT_TAG} length) - if(length EQUAL 40) - # GIT_TAG is probably a git hash - set(${RESULT} - 0 - PARENT_SCOPE - ) - else() - string(REGEX MATCH "v?([0123456789.]*).*" _ ${GIT_TAG}) - set(${RESULT} - ${CMAKE_MATCH_1} - PARENT_SCOPE - ) - endif() -endfunction() - -# guesses if the git tag is a commit hash or an actual tag or a branch name. -function(cpm_is_git_tag_commit_hash GIT_TAG RESULT) - string(LENGTH "${GIT_TAG}" length) - # full hash has 40 characters, and short hash has at least 7 characters. - if(length LESS 7 OR length GREATER 40) - set(${RESULT} - 0 - PARENT_SCOPE - ) - else() - if(${GIT_TAG} MATCHES "^[a-fA-F0-9]+$") - set(${RESULT} - 1 - PARENT_SCOPE - ) - else() - set(${RESULT} - 0 - PARENT_SCOPE - ) - endif() - endif() -endfunction() - -function(cpm_prettify_package_arguments OUT_VAR IS_IN_COMMENT) - set(oneValueArgs - NAME - FORCE - VERSION - GIT_TAG - DOWNLOAD_ONLY - GITHUB_REPOSITORY - GITLAB_REPOSITORY - GIT_REPOSITORY - SOURCE_DIR - DOWNLOAD_COMMAND - FIND_PACKAGE_ARGUMENTS - NO_CACHE - GIT_SHALLOW - ) - set(multiValueArgs OPTIONS) - cmake_parse_arguments(CPM_ARGS "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) - - foreach(oneArgName ${oneValueArgs}) - if(DEFINED CPM_ARGS_${oneArgName}) - if(${IS_IN_COMMENT}) - string(APPEND PRETTY_OUT_VAR "#") - endif() - if(${oneArgName} STREQUAL "SOURCE_DIR") - string(REPLACE ${CMAKE_SOURCE_DIR} "\${CMAKE_SOURCE_DIR}" CPM_ARGS_${oneArgName} - ${CPM_ARGS_${oneArgName}} - ) - endif() - string(APPEND PRETTY_OUT_VAR " ${oneArgName} ${CPM_ARGS_${oneArgName}}\n") - endif() - endforeach() - foreach(multiArgName ${multiValueArgs}) - if(DEFINED CPM_ARGS_${multiArgName}) - if(${IS_IN_COMMENT}) - string(APPEND PRETTY_OUT_VAR "#") - endif() - string(APPEND PRETTY_OUT_VAR " ${multiArgName}\n") - foreach(singleOption ${CPM_ARGS_${multiArgName}}) - if(${IS_IN_COMMENT}) - string(APPEND PRETTY_OUT_VAR "#") - endif() - string(APPEND PRETTY_OUT_VAR " \"${singleOption}\"\n") - endforeach() - endif() - endforeach() - - if(NOT "${CPM_ARGS_UNPARSED_ARGUMENTS}" STREQUAL "") - if(${IS_IN_COMMENT}) - string(APPEND PRETTY_OUT_VAR "#") - endif() - string(APPEND PRETTY_OUT_VAR " ") - foreach(CPM_ARGS_UNPARSED_ARGUMENT ${CPM_ARGS_UNPARSED_ARGUMENTS}) - string(APPEND PRETTY_OUT_VAR " ${CPM_ARGS_UNPARSED_ARGUMENT}") - endforeach() - string(APPEND PRETTY_OUT_VAR "\n") - endif() - - set(${OUT_VAR} - ${PRETTY_OUT_VAR} - PARENT_SCOPE - ) -endfunction() +include(${CPM_DOWNLOAD_LOCATION}) diff --git a/include/math_approx/src/trig_approx.hpp b/include/math_approx/src/trig_approx.hpp index 8dba67a..79b0897 100644 --- a/include/math_approx/src/trig_approx.hpp +++ b/include/math_approx/src/trig_approx.hpp @@ -253,4 +253,29 @@ constexpr T tan (T x) { return tan_mhalfpi_halfpi (trig_detail::fast_mod_mhalfpi_halfpi (x)); } + +/** Polynomial approximation of sin(2*pi*x) on the range [-pi/2, pi/2] */ +template +constexpr T sin_turns_mhalfpi_halfpi (T x) +{ + // static_assert (order % 2 == 1 && order >= 3 && order <= 15, "Order must be an odd number within [3, 15]"); + + // for polynomial derivation, see notebooks/tan_approx.nb + + using S = scalar_of_t; + const auto x_sq = x * x; + const auto x_q = x_sq * x_sq; + const auto x_7_9 = (S) 38.0636285939f - (S) 12.0736625515f * x_sq; + const auto x_3_5 = (S) 64.8346168010f - (S) 67.0380336036f * x_sq; + const auto x_3_5_7_9 = x_3_5 + x_7_9 * x_q; + const auto x_1_3_5_7_9 = (S) -25.1327351251f + x_3_5_7_9 * x_sq; + const auto y = x * x_1_3_5_7_9; + + + return y * (x + 0.5f) * (x - 0.5f); // (x_sq - 0.25f); + // -25.1327351251 x + 64.8346168010 x^3 - 67.0380336036 x^5 + + // 38.0636285939 x^7 - 12.0736625515 x^9 + // x + 64.8346168 x^3 - 67.0380336 x^5 + 38.0636286 x^7 - + // 12.0736626 x^9 +} } // namespace math_approx diff --git a/tools/plotter/plotter.cpp b/tools/plotter/plotter.cpp index 1021eef..6a01025 100644 --- a/tools/plotter/plotter.cpp +++ b/tools/plotter/plotter.cpp @@ -62,18 +62,95 @@ T sigmoid_ref (T x) return (T) 1 / ((T) 1 + std::exp (-x)); } +template +T std_sin_turns (T x) +{ + return std::sin ((T) 2 * (T) M_PI * x); +} + +// template +// T sin_turns (T x) +// { +// return math_approx::sin ((T) 2 * (T) M_PI * x); +// } + +/* Writes result sine result sin(πa) to the location pointed to by sp + Writes result cosine result cos(πa) to the location pointed to by cp + + In exhaustive testing, the maximum error in sine results was 0.96677 ulp, + the maximum error in cosine results was 0.96563 ulp, meaning results are + faithfully rounded. + + Copied from: https://stackoverflow.com/questions/42792939/implementation-of-sinpi-and-cospi-using-standard-c-math-library +*/ +void sincospif (float a, float *sp, float *cp) +{ + float az, t, c, r, s; + int32_t i; + + az = a * 0.0f; // must be evaluated with IEEE-754 semantics + /* for |a| > 2**24, cospi(a) = 1.0f, but cospi(Inf) = NaN */ + a = (fabsf (a) < 0x1.0p24f) ? a : az; + r = nearbyintf (a + a); // must use IEEE-754 "to nearest" rounding + i = (int32_t)r; + t = fmaf (-0.5f, r, a); + /* compute core approximations */ + s = t * t; + /* Approximate cos(pi*x) for x in [-0.25,0.25] */ + r = 0x1.d9e000p-3f; + r = fmaf (r, s, -0x1.55c400p+0f); + r = fmaf (r, s, 0x1.03c1cep+2f); + r = fmaf (r, s, -0x1.3bd3ccp+2f); + c = fmaf (r, s, 0x1.000000p+0f); + /* Approximate sin(pi*x) for x in [-0.25,0.25] */ + r = -0x1.310000p-1f; + r = fmaf (r, s, 0x1.46737ep+1f); + r = fmaf (r, s, -0x1.4abbfep+2f); + r = (t * s) * r; + s = fmaf (t, 0x1.921fb6p+1f, r); + if (i & 2) { + s = 0.0f - s; // must be evaluated with IEEE-754 semantics + c = 0.0f - c; // must be evaluated with IEEE-754 semantics + } + if (i & 1) { + t = 0.0f - s; // must be evaluated with IEEE-754 semantics + s = c; + c = t; + } + /* IEEE-754: sinPi(+n) is +0 and sinPi(-n) is -0 for positive integers n */ + if (a == floorf (a)) s = az; + *sp = s; + *cp = c; +} + +float sinpi (float x) +{ + float s, c; + sincospif (2.0f * x, &s, &c); + return s; +} + #define FLOAT_FUNC(func) [] (float x) { return func (x); } int main() { + // std::cout << math_approx::sin_turns_mhalfpi_halfpi<9> (0.0f) << '\n'; + // std::cout << math_approx::sin_turns_mhalfpi_halfpi<9> (0.5f) << '\n'; + // std::cout << math_approx::sin_turns_mhalfpi_halfpi<9> (-0.5f) << '\n'; + + const auto tt = sinpi (0.5f); + const auto dd = math_approx::sin_turns_mhalfpi_halfpi<9> (0.5f); + std::cout << tt << ' ' << dd << '\n'; + const auto err = test_helpers::compute_ulp_error ({ &tt, 1 }, { &dd, 1 }); + std::cout << err.front() << '\n'; + plt::figure(); - const auto range = std::make_pair (-10.0f, 10.0f); - static constexpr auto tol = 1.0e-2f; + const auto range = std::make_pair (-0.5f, 0.5f); + static constexpr auto tol = 4.0e-3f; const auto all_floats = test_helpers::all_32_bit_floats (range.first, range.second, tol); - const auto y_exact = test_helpers::compute_all (all_floats, FLOAT_FUNC (sigmoid_ref)); - plot_ulp_error (all_floats, y_exact, FLOAT_FUNC ((math_approx::sigmoid_exp<5, true>) ), "sigmoid_exp-5_c1"); - plot_ulp_error (all_floats, y_exact, FLOAT_FUNC ((math_approx::sigmoid_exp<6, true>) ), "sigmoid_exp-6_c1"); + const auto y_exact = test_helpers::compute_all (all_floats, FLOAT_FUNC (sinpi)); + plot_ulp_error (all_floats, y_exact, FLOAT_FUNC ((math_approx::sin_turns_mhalfpi_halfpi<9>) ), "sint-9"); plt::legend ({ { "loc", "upper right" } }); plt::xlim (range.first, range.second); From d7dc39a30489e68b80b2318971357cdb0806199b Mon Sep 17 00:00:00 2001 From: Jatin Chowdhury Date: Mon, 2 Jun 2025 13:09:47 -0700 Subject: [PATCH 2/8] More comprehensive testing --- .gitignore | 2 + include/math_approx/src/trig_approx.hpp | 29 +++- test/CMakeLists.txt | 10 +- test/src/reference/sincospi.hpp | 69 ++++++++++ test/src/trig_turns_approx_test.cpp | 174 ++++++++++++++++++++++++ tools/bench/CMakeLists.txt | 4 +- tools/bench/trig_turns_bench.cpp | 82 +++++++++++ tools/plotter/CMakeLists.txt | 1 + tools/plotter/plotter.cpp | 78 ++--------- 9 files changed, 374 insertions(+), 75 deletions(-) create mode 100644 test/src/reference/sincospi.hpp create mode 100644 test/src/trig_turns_approx_test.cpp create mode 100644 tools/bench/trig_turns_bench.cpp diff --git a/.gitignore b/.gitignore index 5463156..36f3c6c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ .idea/ .vscode/ +.focus-config +*.raddbg_project build*/ *.zip diff --git a/include/math_approx/src/trig_approx.hpp b/include/math_approx/src/trig_approx.hpp index 79b0897..60dc1b0 100644 --- a/include/math_approx/src/trig_approx.hpp +++ b/include/math_approx/src/trig_approx.hpp @@ -254,6 +254,23 @@ constexpr T tan (T x) return tan_mhalfpi_halfpi (trig_detail::fast_mod_mhalfpi_halfpi (x)); } +//=============================================================================== +namespace trig_turns_detail +{ + using namespace trig_detail; + + /** Fast method to wrap a value into the range [-pi, pi] */ + template + constexpr T fast_mod_mhalf_half (T x) + { + using std::nearbyint; +#if defined(XSIMD_HPP) + using xsimd::nearbyint; +#endif + return x - nearbyint (x); // @TODO: nearbyint is apparently very slow? + } +} + /** Polynomial approximation of sin(2*pi*x) on the range [-pi/2, pi/2] */ template constexpr T sin_turns_mhalfpi_halfpi (T x) @@ -272,10 +289,20 @@ constexpr T sin_turns_mhalfpi_halfpi (T x) const auto y = x * x_1_3_5_7_9; - return y * (x + 0.5f) * (x - 0.5f); // (x_sq - 0.25f); + // return y * (x + 0.5f) * (x - 0.5f); + return y * (x_sq - 0.25f); // -25.1327351251 x + 64.8346168010 x^3 - 67.0380336036 x^5 + // 38.0636285939 x^7 - 12.0736625515 x^9 // x + 64.8346168 x^3 - 67.0380336 x^5 + 38.0636286 x^7 - // 12.0736626 x^9 } + +/** + * Full-range approximation of sin(2*pi*x) + */ +template +constexpr T sin_turns (T x) +{ + return sin_turns_mhalfpi_halfpi (trig_turns_detail::fast_mod_mhalf_half (x)); +} } // namespace math_approx diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 5a958ce..3bc0374 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -13,10 +13,11 @@ function(setup_catch_test target) math_approx ) target_compile_features(${target} PUBLIC cxx_std_20) - target_compile_options(${target} PRIVATE - $<$:/W4 /WX> - $<$>:-Wall -Wextra -Wpedantic -Werror> - ) + target_compile_definitions(${target} PUBLIC _USE_MATH_DEFINES=1) + # target_compile_options(${target} PRIVATE + # $<$:/W4 /WX> + # $<$>:-Wall -Wextra -Wpedantic -Werror> + # ) add_custom_command(TARGET ${target} POST_BUILD @@ -30,6 +31,7 @@ function(setup_catch_test target) endfunction(setup_catch_test) setup_catch_test(trig_approx_test) +setup_catch_test(trig_turns_approx_test) setup_catch_test(inverse_trig_approx_test) setup_catch_test(pow_approx_test) setup_catch_test(log_approx_test) diff --git a/test/src/reference/sincospi.hpp b/test/src/reference/sincospi.hpp new file mode 100644 index 0000000..ac09eea --- /dev/null +++ b/test/src/reference/sincospi.hpp @@ -0,0 +1,69 @@ +#pragma once + +#include + +namespace sincospi +{ +/* Writes result sine result sin(πa) to the location pointed to by sp + Writes result cosine result cos(πa) to the location pointed to by cp + + In exhaustive testing, the maximum error in sine results was 0.96677 ulp, + the maximum error in cosine results was 0.96563 ulp, meaning results are + faithfully rounded. + + Copied from: https://stackoverflow.com/questions/42792939/implementation-of-sinpi-and-cospi-using-standard-c-math-library +*/ +void sincospif (float a, float *sp, float *cp) +{ + float az, t, c, r, s; + int32_t i; + + az = a * 0.0f; // must be evaluated with IEEE-754 semantics + /* for |a| > 2**24, cospi(a) = 1.0f, but cospi(Inf) = NaN */ + a = (fabsf (a) < 0x1.0p24f) ? a : az; + r = nearbyintf (a + a); // must use IEEE-754 "to nearest" rounding + i = (int32_t)r; + t = fmaf (-0.5f, r, a); + /* compute core approximations */ + s = t * t; + /* Approximate cos(pi*x) for x in [-0.25,0.25] */ + r = 0x1.d9e000p-3f; + r = fmaf (r, s, -0x1.55c400p+0f); + r = fmaf (r, s, 0x1.03c1cep+2f); + r = fmaf (r, s, -0x1.3bd3ccp+2f); + c = fmaf (r, s, 0x1.000000p+0f); + /* Approximate sin(pi*x) for x in [-0.25,0.25] */ + r = -0x1.310000p-1f; + r = fmaf (r, s, 0x1.46737ep+1f); + r = fmaf (r, s, -0x1.4abbfep+2f); + r = (t * s) * r; + s = fmaf (t, 0x1.921fb6p+1f, r); + if (i & 2) { + s = 0.0f - s; // must be evaluated with IEEE-754 semantics + c = 0.0f - c; // must be evaluated with IEEE-754 semantics + } + if (i & 1) { + t = 0.0f - s; // must be evaluated with IEEE-754 semantics + s = c; + c = t; + } + /* IEEE-754: sinPi(+n) is +0 and sinPi(-n) is -0 for positive integers n */ + if (a == floorf (a)) s = az; + *sp = s; + *cp = c; +} + +float sin2pi (float x) +{ + float s, c; + sincospif (2.0f * x, &s, &c); + return s; +} + +float cos2pi (float x) +{ + float s, c; + sincospif (2.0f * x, &s, &c); + return c; +} +} diff --git a/test/src/trig_turns_approx_test.cpp b/test/src/trig_turns_approx_test.cpp new file mode 100644 index 0000000..6f5abf5 --- /dev/null +++ b/test/src/trig_turns_approx_test.cpp @@ -0,0 +1,174 @@ +#include "test_helpers.hpp" +#include +#include + +#include +#include "reference/sincospi.hpp" + +TEST_CASE ("Sine Approx Test") +{ +// #if ! defined(WIN32) +// const auto all_floats = test_helpers::all_32_bit_floats (-10.0f, 10.0f, 1.0e-3f); +// #else +// const auto all_floats = test_helpers::all_32_bit_floats (-10.0f, 10.0f, 1.0e-1f); +// #endif +// const auto y_exact = test_helpers::compute_all (all_floats, [] (auto x) +// { return sincospi::sin2pi (x); }); + + // const auto test_approx = [&all_floats, &y_exact] (auto&& f_approx, float err_bound) + // { + // const auto ref = sincospi::sin2pi (-9.45f); + // const auto test = math_approx::sin_turns<9> (-9.45f); + + // // const auto y_approx = test_helpers::compute_all (all_floats, f_approx); + + // // const auto error = test_helpers::compute_error (y_exact, y_approx); + // // const auto max_error = test_helpers::abs_max (error); + + // // std::cout << max_error << std::endl; + // // REQUIRE (std::abs (max_error) < err_bound); + // }; + + SECTION ("9th-Order") + { + const auto ref = sincospi::sin2pi (-9.45f); + const auto test = math_approx::sin_turns<9> (2.0f * -9.45f); + + // test_approx ([] (auto x) + // { return math_approx::sin_turns<9> (x); }, + // 8.5e-7f); + } + // SECTION ("7th-Order") + // { + // test_approx ([] (auto x) + // { return math_approx::sin<7> (x); }, + // 1.8e-5f); + // } + // SECTION ("5th-Order") + // { + // test_approx ([] (auto x) + // { return math_approx::sin<5> (x); }, + // 7.5e-4f); + // } +} +/* +TEST_CASE ("Cosine Approx Test") +{ +#if ! defined(WIN32) + const auto all_floats = test_helpers::all_32_bit_floats (-10.0f, 10.0f, 1.0e-3f); +#else + const auto all_floats = test_helpers::all_32_bit_floats (-10.0f, 10.0f, 1.0e-1f); +#endif + const auto y_exact = test_helpers::compute_all (all_floats, [] (auto x) + { return std::cos (x); }); + + const auto test_approx = [&all_floats, &y_exact] (auto&& f_approx, float err_bound) + { + const auto y_approx = test_helpers::compute_all (all_floats, f_approx); + + const auto error = test_helpers::compute_error (y_exact, y_approx); + const auto max_error = test_helpers::abs_max (error); + + std::cout << max_error << std::endl; + REQUIRE (std::abs (max_error) < err_bound); + }; + + SECTION ("9th-Order") + { + test_approx ([] (auto x) + { return math_approx::cos<9> (x); }, + 7.5e-7f); + } + SECTION ("7th-Order") + { + test_approx ([] (auto x) + { return math_approx::cos<7> (x); }, + 1.8e-5f); + } + SECTION ("5th-Order") + { + test_approx ([] (auto x) + { return math_approx::cos<5> (x); }, + 7.5e-4f); + } +} + +TEST_CASE ("Tan Approx Test") +{ +#if ! defined(WIN32) + const auto all_floats = test_helpers::all_32_bit_floats (-1.5f, 1.5f, 1.0e-3f); +#else + const auto all_floats = test_helpers::all_32_bit_floats (-1.5f, 1.5f, 1.0e-1f); +#endif + const auto y_exact = test_helpers::compute_all (all_floats, [] (auto x) + { return std::tan (x); }); + + const auto test_approx = [&all_floats, &y_exact] (auto&& f_approx, float err_bound, float rel_err_bound, uint32_t ulp_bound) + { + const auto y_approx = test_helpers::compute_all (all_floats, f_approx); + + const auto error = test_helpers::compute_error (y_exact, y_approx); + const auto rel_error = test_helpers::compute_rel_error (y_exact, y_approx); + const auto ulp_error = test_helpers::compute_ulp_error (y_exact, y_approx); + + const auto max_error = test_helpers::abs_max (error); + const auto max_rel_error = test_helpers::abs_max (rel_error); + const auto max_ulp_error = *std::max_element (ulp_error.begin(), ulp_error.end()); + + std::cout << max_error << ", " << max_rel_error << ", " << max_ulp_error << std::endl; + REQUIRE (std::abs (max_error) < err_bound); + REQUIRE (std::abs (max_rel_error) < rel_err_bound); + if (ulp_bound > 0) + REQUIRE (max_ulp_error < ulp_bound); + }; + + SECTION ("13th-Order") + { + test_approx ([] (auto x) + { return math_approx::tan<13> (x); }, + 5.5e-5f, + 6.0e-5f, + 520); + } + SECTION ("11th-Order") + { + test_approx ([] (auto x) + { return math_approx::tan<11> (x); }, + 9.5e-5f, + 6.0e-5f, + 520); + } + SECTION ("9th-Order") + { + test_approx ([] (auto x) + { return math_approx::tan<9> (x); }, + 0.0009f, + 6.0e-5f, + 900); + } + SECTION ("7th-Order") + { + test_approx ([] (auto x) + { return math_approx::tan<7> (x); }, + 0.015f, + 0.0009f, + 0); + } + SECTION ("5th-Order") + { + test_approx ([] (auto x) + { return math_approx::tan<5> (x); }, + 0.14f, + 0.01f, + 0); + } + SECTION ("3rd-Order") + { + test_approx ([] (auto x) + { return math_approx::tan<3> (x); }, + 1.5f, + 0.09f, + 0); + } +} +*/ diff --git a/tools/bench/CMakeLists.txt b/tools/bench/CMakeLists.txt index b16236b..7cebc52 100644 --- a/tools/bench/CMakeLists.txt +++ b/tools/bench/CMakeLists.txt @@ -6,7 +6,7 @@ endif() CPMAddPackage( NAME benchmark GITHUB_REPOSITORY google/benchmark - VERSION 1.5.2 + VERSION 1.9.4 OPTIONS "BENCHMARK_ENABLE_TESTING Off" ) @@ -14,6 +14,7 @@ function(setup_bench target src) add_executable(${target} ${src}) target_link_libraries(${target} PRIVATE benchmark::benchmark math_approx) target_compile_features(${target} PUBLIC cxx_std_20) + target_compile_definitions(${target} PUBLIC _USE_MATH_DEFINES=1) endfunction(setup_bench) setup_bench(trig_approx_bench trig_bench.cpp) @@ -25,3 +26,4 @@ setup_bench(inverse_hyperbolic_trig_approx_bench inverse_hyperbolic_trig_bench.c setup_bench(sigmoid_approx_bench sigmoid_bench.cpp) setup_bench(wright_omega_approx_bench wright_omega_bench.cpp) setup_bench(polylog_approx_bench polylog_bench.cpp) +setup_bench(trig_turns_approx_bench trig_turns_bench.cpp) diff --git a/tools/bench/trig_turns_bench.cpp b/tools/bench/trig_turns_bench.cpp new file mode 100644 index 0000000..6549fd9 --- /dev/null +++ b/tools/bench/trig_turns_bench.cpp @@ -0,0 +1,82 @@ +#include +#include +#include "../test/src/reference/sincospi.hpp" + +static constexpr size_t N = 2000; +const auto data = [] +{ + std::vector x; + x.resize (N, 0.0f); + for (size_t i = 0; i < N; ++i) + x[i] = -10.0f + 20.0f * (float) i / (float) N; + return x; +}(); + +#define TRIG_BENCH(name, func) \ +void name (benchmark::State& state) \ +{ \ +for (auto _ : state) \ +{ \ +for (auto& x : data) \ +{ \ +auto y = func (x); \ +benchmark::DoNotOptimize (y); \ +} \ +} \ +} \ +BENCHMARK (name); + +// TRIG_BENCH (cos_std, std::cos) +// TRIG_BENCH (cos_ref, sincospi::) +// TRIG_BENCH (cos_approx9, math_approx::cos<9>) +// TRIG_BENCH (cos_approx7, math_approx::cos<7>) +// TRIG_BENCH (cos_approx5, math_approx::cos<5>) + +TRIG_BENCH (sin_std, std::sin) +TRIG_BENCH (sinpi_ref, sincospi::sin2pi) +TRIG_BENCH (sinpi_approx9, math_approx::sin_turns<9>) +// TRIG_BENCH (sinpi_approx7, math_approx::sin<7>) +// TRIG_BENCH (sinpi_approx5, math_approx::sin<5>) + +// TRIG_BENCH (tan_std, std::tan) +// TRIG_BENCH (tan_approx13, math_approx::tan<13>) +// TRIG_BENCH (tan_approx11, math_approx::tan<11>) +// TRIG_BENCH (tan_approx9, math_approx::tan<9>) +// TRIG_BENCH (tan_approx7, math_approx::tan<7>) +// TRIG_BENCH (tan_approx5, math_approx::tan<5>) +// TRIG_BENCH (tan_approx3, math_approx::tan<3>) + +// #define TRIG_SIMD_BENCH(name, func) \ +// void name (benchmark::State& state) \ +// { \ +// for (auto _ : state) \ +// { \ +// for (auto& x : data) \ +// { \ +// auto y = func (xsimd::broadcast (x)); \ +// static_assert (std::is_same_v, decltype(y)>); \ +// benchmark::DoNotOptimize (y); \ +// } \ +// } \ +// } \ +// BENCHMARK (name); + +// TRIG_SIMD_BENCH (sin_xsimd, xsimd::sin) +// TRIG_SIMD_BENCH (sin_simd_approx9, math_approx::sin<9>) +// TRIG_SIMD_BENCH (sin_simd_approx7, math_approx::sin<7>) +// TRIG_SIMD_BENCH (sin_simd_approx5, math_approx::sin<5>) + +// TRIG_SIMD_BENCH (cos_xsimd, xsimd::cos) +// TRIG_SIMD_BENCH (cos_simd_approx9, math_approx::cos<9>) +// TRIG_SIMD_BENCH (cos_simd_approx7, math_approx::cos<7>) +// TRIG_SIMD_BENCH (cos_simd_approx5, math_approx::cos<5>) + +// TRIG_SIMD_BENCH (tan_xsimd, xsimd::tan) +// TRIG_SIMD_BENCH (tan_simd_approx13, math_approx::tan<13>) +// TRIG_SIMD_BENCH (tan_simd_approx11, math_approx::tan<11>) +// TRIG_SIMD_BENCH (tan_simd_approx9, math_approx::tan<9>) +// TRIG_SIMD_BENCH (tan_simd_approx7, math_approx::tan<7>) +// TRIG_SIMD_BENCH (tan_simd_approx5, math_approx::tan<5>) +// TRIG_SIMD_BENCH (tan_simd_approx3, math_approx::tan<3>) + +BENCHMARK_MAIN(); diff --git a/tools/plotter/CMakeLists.txt b/tools/plotter/CMakeLists.txt index 839537a..c3570bc 100644 --- a/tools/plotter/CMakeLists.txt +++ b/tools/plotter/CMakeLists.txt @@ -12,3 +12,4 @@ CPMAddPackage( add_executable(math_approx_plotter plotter.cpp) target_link_libraries(math_approx_plotter PRIVATE matplotlib-cpp math_approx) target_compile_features(math_approx_plotter PUBLIC cxx_std_20) +target_compile_definitions(math_approx_plotter PUBLIC _USE_MATH_DEFINES=1) diff --git a/tools/plotter/plotter.cpp b/tools/plotter/plotter.cpp index 6a01025..50d57c2 100644 --- a/tools/plotter/plotter.cpp +++ b/tools/plotter/plotter.cpp @@ -8,6 +8,7 @@ namespace plt = matplotlibcpp; #include "../../test/src/reference/polylogarithm.hpp" #include "../../test/src/reference/toms917.hpp" +#include "../../test/src/reference/sincospi.hpp" #include "../../test/src/test_helpers.hpp" #include @@ -68,68 +69,6 @@ T std_sin_turns (T x) return std::sin ((T) 2 * (T) M_PI * x); } -// template -// T sin_turns (T x) -// { -// return math_approx::sin ((T) 2 * (T) M_PI * x); -// } - -/* Writes result sine result sin(πa) to the location pointed to by sp - Writes result cosine result cos(πa) to the location pointed to by cp - - In exhaustive testing, the maximum error in sine results was 0.96677 ulp, - the maximum error in cosine results was 0.96563 ulp, meaning results are - faithfully rounded. - - Copied from: https://stackoverflow.com/questions/42792939/implementation-of-sinpi-and-cospi-using-standard-c-math-library -*/ -void sincospif (float a, float *sp, float *cp) -{ - float az, t, c, r, s; - int32_t i; - - az = a * 0.0f; // must be evaluated with IEEE-754 semantics - /* for |a| > 2**24, cospi(a) = 1.0f, but cospi(Inf) = NaN */ - a = (fabsf (a) < 0x1.0p24f) ? a : az; - r = nearbyintf (a + a); // must use IEEE-754 "to nearest" rounding - i = (int32_t)r; - t = fmaf (-0.5f, r, a); - /* compute core approximations */ - s = t * t; - /* Approximate cos(pi*x) for x in [-0.25,0.25] */ - r = 0x1.d9e000p-3f; - r = fmaf (r, s, -0x1.55c400p+0f); - r = fmaf (r, s, 0x1.03c1cep+2f); - r = fmaf (r, s, -0x1.3bd3ccp+2f); - c = fmaf (r, s, 0x1.000000p+0f); - /* Approximate sin(pi*x) for x in [-0.25,0.25] */ - r = -0x1.310000p-1f; - r = fmaf (r, s, 0x1.46737ep+1f); - r = fmaf (r, s, -0x1.4abbfep+2f); - r = (t * s) * r; - s = fmaf (t, 0x1.921fb6p+1f, r); - if (i & 2) { - s = 0.0f - s; // must be evaluated with IEEE-754 semantics - c = 0.0f - c; // must be evaluated with IEEE-754 semantics - } - if (i & 1) { - t = 0.0f - s; // must be evaluated with IEEE-754 semantics - s = c; - c = t; - } - /* IEEE-754: sinPi(+n) is +0 and sinPi(-n) is -0 for positive integers n */ - if (a == floorf (a)) s = az; - *sp = s; - *cp = c; -} - -float sinpi (float x) -{ - float s, c; - sincospif (2.0f * x, &s, &c); - return s; -} - #define FLOAT_FUNC(func) [] (float x) { return func (x); } int main() @@ -138,19 +77,20 @@ int main() // std::cout << math_approx::sin_turns_mhalfpi_halfpi<9> (0.5f) << '\n'; // std::cout << math_approx::sin_turns_mhalfpi_halfpi<9> (-0.5f) << '\n'; - const auto tt = sinpi (0.5f); - const auto dd = math_approx::sin_turns_mhalfpi_halfpi<9> (0.5f); - std::cout << tt << ' ' << dd << '\n'; - const auto err = test_helpers::compute_ulp_error ({ &tt, 1 }, { &dd, 1 }); - std::cout << err.front() << '\n'; + // const auto tt = sincospi::sin2pi (0.5f); + // const auto dd = math_approx::sin_turns_mhalfpi_halfpi<9> (0.5f); + // std::cout << tt << ' ' << dd << '\n'; + // const auto err = test_helpers::compute_ulp_error ({ &tt, 1 }, { &dd, 1 }); + // std::cout << err.front() << '\n'; plt::figure(); const auto range = std::make_pair (-0.5f, 0.5f); static constexpr auto tol = 4.0e-3f; const auto all_floats = test_helpers::all_32_bit_floats (range.first, range.second, tol); - const auto y_exact = test_helpers::compute_all (all_floats, FLOAT_FUNC (sinpi)); - plot_ulp_error (all_floats, y_exact, FLOAT_FUNC ((math_approx::sin_turns_mhalfpi_halfpi<9>) ), "sint-9"); + const auto y_exact = test_helpers::compute_all (all_floats, FLOAT_FUNC (sincospi::sin2pi)); + // plot_error (all_floats, y_exact, FLOAT_FUNC ((math_approx::sin_turns<9>) ), "sint-9"); + plot_ulp_error (all_floats, y_exact, FLOAT_FUNC ((math_approx::sin_turns<9>) ), "sint-9"); plt::legend ({ { "loc", "upper right" } }); plt::xlim (range.first, range.second); From 56172ba96ee5b2118dd5a37528c4faf876cb3d4f Mon Sep 17 00:00:00 2001 From: Jatin Chowdhury Date: Mon, 2 Jun 2025 14:38:17 -0700 Subject: [PATCH 3/8] Struggling with rounding --- CMakeLists.txt | 2 ++ include/math_approx/src/trig_approx.hpp | 31 ++++++++++++++++++------- tools/plotter/plotter.cpp | 12 ++++++---- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6ff37fc..7bc59f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,11 +16,13 @@ add_library(math_approx INTERFACE) target_include_directories(math_approx INTERFACE include) if(MSVC) target_compile_definitions(math_approx INTERFACE _USE_MATH_DEFINES=1) + target_compile_options(math_approx INTERFACE /arch:SSE4.1) endif() if (TARGET xsimd) message(STATUS "math_approx -- Linking with XSIMD...") target_link_libraries(math_approx INTERFACE xsimd) target_compile_definitions(math_approx INTERFACE MATH_APPROX_XSIMD_TARGET=1) + target_compile_options(math_approx INTERFACE -msse4.1) endif() if(PROJECT_IS_TOP_LEVEL) diff --git a/include/math_approx/src/trig_approx.hpp b/include/math_approx/src/trig_approx.hpp index 60dc1b0..5bb7967 100644 --- a/include/math_approx/src/trig_approx.hpp +++ b/include/math_approx/src/trig_approx.hpp @@ -263,11 +263,26 @@ namespace trig_turns_detail template constexpr T fast_mod_mhalf_half (T x) { - using std::nearbyint; -#if defined(XSIMD_HPP) - using xsimd::nearbyint; -#endif - return x - nearbyint (x); // @TODO: nearbyint is apparently very slow? + // auto y =_mm_round_ss (_mm_load_ps1(&x), _mm_load_ps1(&x), 12); + // // auto y =_mm_round_ss (__m128{}, _mm_load_ps1(&x), 12); + // return x - reinterpret_cast (y); + + // _asm { + // roundss xmm1, xmm0, 12 + // subss xmm0, xmm1 + // ret + // } + + using S = scalar_of_t; + x += 0.5f; + const auto mod = x - truncate (x); + return select (x >= (T) 0, mod, mod + 1.0f) - 0.5f; + +// using std::nearbyint; +// #if defined(XSIMD_HPP) +// using xsimd::nearbyint; +// #endif +// return x - nearbyint (x); // @TODO: nearbyint is apparently very slow? } } @@ -289,9 +304,9 @@ constexpr T sin_turns_mhalfpi_halfpi (T x) const auto y = x * x_1_3_5_7_9; - // return y * (x + 0.5f) * (x - 0.5f); - return y * (x_sq - 0.25f); - // -25.1327351251 x + 64.8346168010 x^3 - 67.0380336036 x^5 + + return y * (x + 0.5f) * (x - 0.5f); + // return y * (x_sq - 0.25f); // this costs us a lot of precision :( + // -25.1327351251 x + 64.8346168010 x^3 - 67.0380336036 x^5 + // 38.0636285939 x^7 - 12.0736625515 x^9 // x + 64.8346168 x^3 - 67.0380336 x^5 + 38.0636286 x^7 - // 12.0736626 x^9 diff --git a/tools/plotter/plotter.cpp b/tools/plotter/plotter.cpp index 50d57c2..74ea69b 100644 --- a/tools/plotter/plotter.cpp +++ b/tools/plotter/plotter.cpp @@ -73,9 +73,12 @@ T std_sin_turns (T x) int main() { - // std::cout << math_approx::sin_turns_mhalfpi_halfpi<9> (0.0f) << '\n'; - // std::cout << math_approx::sin_turns_mhalfpi_halfpi<9> (0.5f) << '\n'; - // std::cout << math_approx::sin_turns_mhalfpi_halfpi<9> (-0.5f) << '\n'; + std::cout << sincospi::sin2pi (0.0f) << '\n'; + std::cout << sincospi::sin2pi (0.5f) << '\n'; + std::cout << sincospi::sin2pi (-0.5f) << '\n'; + std::cout << math_approx::sin_turns<9> (0.0f) << '\n'; + std::cout << math_approx::sin_turns<9> (0.5f) << '\n'; + std::cout << math_approx::sin_turns<9> (-0.5f) << '\n'; // const auto tt = sincospi::sin2pi (0.5f); // const auto dd = math_approx::sin_turns_mhalfpi_halfpi<9> (0.5f); @@ -84,9 +87,10 @@ int main() // std::cout << err.front() << '\n'; plt::figure(); - const auto range = std::make_pair (-0.5f, 0.5f); + const auto range = std::make_pair (-0.5f, 0.49f); static constexpr auto tol = 4.0e-3f; + // std::vector all_floats { -0.5f, -0.49999f, -0.49f, 0.0f, 0.49f, 0.4999999999f, 0.5f }; const auto all_floats = test_helpers::all_32_bit_floats (range.first, range.second, tol); const auto y_exact = test_helpers::compute_all (all_floats, FLOAT_FUNC (sincospi::sin2pi)); // plot_error (all_floats, y_exact, FLOAT_FUNC ((math_approx::sin_turns<9>) ), "sint-9"); From 5e18bdcbc82ade079c647aa5a1b072a869193800 Mon Sep 17 00:00:00 2001 From: jatin Date: Mon, 2 Jun 2025 18:18:37 -0700 Subject: [PATCH 4/8] Working sin approximation --- CMakeLists.txt | 2 - include/math_approx/src/trig_approx.hpp | 73 +++++++++++++++-------- test/src/test_helpers.hpp | 2 +- test/src/trig_turns_approx_test.cpp | 78 ++++++++++++++----------- tools/plotter/plotter.cpp | 9 +-- 5 files changed, 98 insertions(+), 66 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7bc59f6..6ff37fc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,13 +16,11 @@ add_library(math_approx INTERFACE) target_include_directories(math_approx INTERFACE include) if(MSVC) target_compile_definitions(math_approx INTERFACE _USE_MATH_DEFINES=1) - target_compile_options(math_approx INTERFACE /arch:SSE4.1) endif() if (TARGET xsimd) message(STATUS "math_approx -- Linking with XSIMD...") target_link_libraries(math_approx INTERFACE xsimd) target_compile_definitions(math_approx INTERFACE MATH_APPROX_XSIMD_TARGET=1) - target_compile_options(math_approx INTERFACE -msse4.1) endif() if(PROJECT_IS_TOP_LEVEL) diff --git a/include/math_approx/src/trig_approx.hpp b/include/math_approx/src/trig_approx.hpp index 5bb7967..b8bcec6 100644 --- a/include/math_approx/src/trig_approx.hpp +++ b/include/math_approx/src/trig_approx.hpp @@ -263,6 +263,7 @@ namespace trig_turns_detail template constexpr T fast_mod_mhalf_half (T x) { + // @TODO: test this on Intel... // auto y =_mm_round_ss (_mm_load_ps1(&x), _mm_load_ps1(&x), 12); // // auto y =_mm_round_ss (__m128{}, _mm_load_ps1(&x), 12); // return x - reinterpret_cast (y); @@ -273,16 +274,16 @@ namespace trig_turns_detail // ret // } - using S = scalar_of_t; - x += 0.5f; - const auto mod = x - truncate (x); - return select (x >= (T) 0, mod, mod + 1.0f) - 0.5f; - -// using std::nearbyint; -// #if defined(XSIMD_HPP) -// using xsimd::nearbyint; -// #endif -// return x - nearbyint (x); // @TODO: nearbyint is apparently very slow? + // using S = scalar_of_t; + // x += (S) 0.5; + // const auto mod = x - truncate (x); + // return select (x >= (T) 0, mod, mod + (S) 1) - (S) 0.5; + + using std::nearbyint; +#if defined(XSIMD_HPP) + using xsimd::nearbyint; +#endif + return x - nearbyint (x); } } @@ -290,26 +291,50 @@ namespace trig_turns_detail template constexpr T sin_turns_mhalfpi_halfpi (T x) { - // static_assert (order % 2 == 1 && order >= 3 && order <= 15, "Order must be an odd number within [3, 15]"); - - // for polynomial derivation, see notebooks/tan_approx.nb + static_assert (order % 2 == 1 && order <= 11 && order >= 5, "Order must be an odd number within [5, 11]"); using S = scalar_of_t; const auto x_sq = x * x; - const auto x_q = x_sq * x_sq; - const auto x_7_9 = (S) 38.0636285939f - (S) 12.0736625515f * x_sq; - const auto x_3_5 = (S) 64.8346168010f - (S) 67.0380336036f * x_sq; - const auto x_3_5_7_9 = x_3_5 + x_7_9 * x_q; - const auto x_1_3_5_7_9 = (S) -25.1327351251f + x_3_5_7_9 * x_sq; - const auto y = x * x_1_3_5_7_9; - + T y; + if constexpr (order == 11) + { + // -25.1327411554 x + 64.8358228565 x^3 - 67.0766273790 x^5 + 38.4958788775 x^7 - 14.0496638478 x^9 + 3.16160207407 + const auto x_q = x_sq * x_sq; + const auto x_9_11 = (S) -14.0496638478f + (S) 3.16160207407f * x_sq; + const auto x_5_7 = (S) -67.0766273790f + (S) 38.4958788775f * x_sq; + const auto x_1_3 = (S) -25.1327411554 + (S) 64.8358228565f * x_sq; + const auto x_5_7_9_11 = x_5_7 + x_9_11 * x_q; + const auto x_1_3_5_7_9_11 = x_1_3 + x_5_7_9_11 * x_q; + y = x * x_1_3_5_7_9_11; + } + else if constexpr (order == 9) + { + const auto x_q = x_sq * x_sq; + const auto x_7_9 = (S) 38.0636285939f - (S) 12.0736625515f * x_sq; + const auto x_3_5 = (S) 64.8346168010f - (S) 67.0380336036f * x_sq; + const auto x_3_5_7_9 = x_3_5 + x_7_9 * x_q; + const auto x_1_3_5_7_9 = (S) -25.1327351251f + x_3_5_7_9 * x_sq; + y = x * x_1_3_5_7_9; + } + else if constexpr (order == 7) + { + // -25.1323666662 x + 64.7874540567 x^3 - 66.0947787168 x^5 + 32.0267973181 x^7 + const auto x_q = x_sq * x_sq; + const auto x_5_7 = (S) -66.0947787168f + (S) 32.0267973181f * x_sq; + const auto x_1_3 = (S) -25.1323666662f + (S) 64.7874540567f * x_sq; + const auto x_1_3_5_7 = x_1_3 + x_5_7 * x_q; + y = x * x_1_3_5_7; + } + else if constexpr (order == 5) + { + // -25.1167285815 x + 63.6615119634 x^3 - 54.0847297225 x^5 + const auto x_3_5 = (S) 63.6615119634f + (S) -54.0847297225f * x_sq; + const auto x_1_3_5 = (S) -25.1167285815f + x_3_5 * x_sq; + y = x * x_1_3_5; + } return y * (x + 0.5f) * (x - 0.5f); // return y * (x_sq - 0.25f); // this costs us a lot of precision :( - // -25.1327351251 x + 64.8346168010 x^3 - 67.0380336036 x^5 + - // 38.0636285939 x^7 - 12.0736625515 x^9 - // x + 64.8346168 x^3 - 67.0380336 x^5 + 38.0636286 x^7 - - // 12.0736626 x^9 } /** diff --git a/test/src/test_helpers.hpp b/test/src/test_helpers.hpp index a2815de..07fa6c7 100644 --- a/test/src/test_helpers.hpp +++ b/test/src/test_helpers.hpp @@ -57,7 +57,7 @@ inline std::vector compute_rel_error (std::span actual, std::span err; err.resize (actual.size()); for (size_t i = 0; i < actual.size(); ++i) - err[i] = (actual[i] - approx[i]) / actual[i]; + err[i] = (actual[i] - approx[i]) / (actual[i] + std::numeric_limits::epsilon()); return err; } diff --git a/test/src/trig_turns_approx_test.cpp b/test/src/trig_turns_approx_test.cpp index 6f5abf5..3defeca 100644 --- a/test/src/trig_turns_approx_test.cpp +++ b/test/src/trig_turns_approx_test.cpp @@ -7,49 +7,57 @@ TEST_CASE ("Sine Approx Test") { -// #if ! defined(WIN32) -// const auto all_floats = test_helpers::all_32_bit_floats (-10.0f, 10.0f, 1.0e-3f); -// #else -// const auto all_floats = test_helpers::all_32_bit_floats (-10.0f, 10.0f, 1.0e-1f); -// #endif -// const auto y_exact = test_helpers::compute_all (all_floats, [] (auto x) -// { return sincospi::sin2pi (x); }); +#if ! defined(WIN32) + const auto all_floats = test_helpers::all_32_bit_floats (-10.0f, 10.0f, 1.0e-3f); +#else + const auto all_floats = test_helpers::all_32_bit_floats (-10.0f, 10.0f, 1.0e-1f); +#endif + const auto y_exact = test_helpers::compute_all (all_floats, [] (auto x) + { return sincospi::sin2pi (x); }); - // const auto test_approx = [&all_floats, &y_exact] (auto&& f_approx, float err_bound) - // { - // const auto ref = sincospi::sin2pi (-9.45f); - // const auto test = math_approx::sin_turns<9> (-9.45f); + const auto test_approx = [&all_floats, &y_exact] (auto&& f_approx, float rel_err_bound, uint32_t ulp_err_bound) + { + const auto y_approx = test_helpers::compute_all (all_floats, f_approx); - // // const auto y_approx = test_helpers::compute_all (all_floats, f_approx); + const auto rel_error = test_helpers::compute_rel_error (y_exact, y_approx); + const auto ulp_error = test_helpers::compute_ulp_error (y_exact, y_approx); - // // const auto error = test_helpers::compute_error (y_exact, y_approx); - // // const auto max_error = test_helpers::abs_max (error); + const auto max_rel_error = test_helpers::abs_max (rel_error); + const auto max_ulp_error = *std::max_element (ulp_error.begin(), ulp_error.end()); - // // std::cout << max_error << std::endl; - // // REQUIRE (std::abs (max_error) < err_bound); - // }; + std::cout << max_rel_error << ", " << max_ulp_error << std::endl; + REQUIRE (std::abs (max_rel_error) < rel_err_bound); + REQUIRE (max_ulp_error < ulp_err_bound); + }; + SECTION ("11th-Order") + { + test_approx ([] (auto x) + { return math_approx::sin_turns<11> (x); }, + 5.0e-7f, + 6); + } SECTION ("9th-Order") { - const auto ref = sincospi::sin2pi (-9.45f); - const auto test = math_approx::sin_turns<9> (2.0f * -9.45f); - - // test_approx ([] (auto x) - // { return math_approx::sin_turns<9> (x); }, - // 8.5e-7f); + test_approx ([] (auto x) + { return math_approx::sin_turns<9> (x); }, + 2.0e-6f, + 14); + } + SECTION ("9th-Order") + { + test_approx ([] (auto x) + { return math_approx::sin_turns<7> (x); }, + 9.0e-5f, + 490); + } + SECTION ("9th-Order") + { + test_approx ([] (auto x) + { return math_approx::sin_turns<5> (x); }, + 5.0e-3f, + 22'000); } - // SECTION ("7th-Order") - // { - // test_approx ([] (auto x) - // { return math_approx::sin<7> (x); }, - // 1.8e-5f); - // } - // SECTION ("5th-Order") - // { - // test_approx ([] (auto x) - // { return math_approx::sin<5> (x); }, - // 7.5e-4f); - // } } /* TEST_CASE ("Cosine Approx Test") diff --git a/tools/plotter/plotter.cpp b/tools/plotter/plotter.cpp index 74ea69b..6559ceb 100644 --- a/tools/plotter/plotter.cpp +++ b/tools/plotter/plotter.cpp @@ -87,14 +87,15 @@ int main() // std::cout << err.front() << '\n'; plt::figure(); - const auto range = std::make_pair (-0.5f, 0.49f); - static constexpr auto tol = 4.0e-3f; + const auto range = std::make_pair (-0.5f, 0.5f); + static constexpr auto tol = 1.0e-3f; - // std::vector all_floats { -0.5f, -0.49999f, -0.49f, 0.0f, 0.49f, 0.4999999999f, 0.5f }; const auto all_floats = test_helpers::all_32_bit_floats (range.first, range.second, tol); const auto y_exact = test_helpers::compute_all (all_floats, FLOAT_FUNC (sincospi::sin2pi)); - // plot_error (all_floats, y_exact, FLOAT_FUNC ((math_approx::sin_turns<9>) ), "sint-9"); + // plot_ulp_error (all_floats, y_exact, FLOAT_FUNC ((math_approx::sin_turns<5>) ), "sint-5"); + plot_ulp_error (all_floats, y_exact, FLOAT_FUNC ((math_approx::sin_turns<7>) ), "sint-7"); plot_ulp_error (all_floats, y_exact, FLOAT_FUNC ((math_approx::sin_turns<9>) ), "sint-9"); + plot_ulp_error (all_floats, y_exact, FLOAT_FUNC ((math_approx::sin_turns<11>) ), "sint-11"); plt::legend ({ { "loc", "upper right" } }); plt::xlim (range.first, range.second); From 90ed738636d15197e5c66ae47e2ca912ee99fdfb Mon Sep 17 00:00:00 2001 From: jatin Date: Mon, 2 Jun 2025 19:02:15 -0700 Subject: [PATCH 5/8] Test and benchmarks --- README.md | 1 + include/math_approx/src/trig_approx.hpp | 21 +++++ test/src/trig_turns_approx_test.cpp | 104 +++++------------------- tools/bench/trig_turns_bench.cpp | 82 ++++++++----------- tools/plotter/plotter.cpp | 21 +---- 5 files changed, 80 insertions(+), 149 deletions(-) diff --git a/README.md b/README.md index 2c4ef3d..36d79ee 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ Currently supported: - sin/cos/tan +- sin(2*pi*x)/cos(2*pi*x) - arcsin/arccos/arctan - exp/exp2/exp10/expm1 - log/log2/log10/log1p diff --git a/include/math_approx/src/trig_approx.hpp b/include/math_approx/src/trig_approx.hpp index b8bcec6..53ed3e8 100644 --- a/include/math_approx/src/trig_approx.hpp +++ b/include/math_approx/src/trig_approx.hpp @@ -345,4 +345,25 @@ constexpr T sin_turns (T x) { return sin_turns_mhalfpi_halfpi (trig_turns_detail::fast_mod_mhalf_half (x)); } + +/** Polynomial approximation of cos(2*pi*x) on the range [-pi/2, pi/2] */ +template +constexpr T cos_turns_mhalfpi_halfpi (T x) +{ + using S = scalar_of_t; + using std::abs; +#if defined(XSIMD_HPP) + using xsimd::abs; +#endif + return sin_turns_mhalfpi_halfpi ((S) 0.25 - abs (x)); +} + +/** + * Full-range approximation of cos(2*pi*x) + */ +template +constexpr T cos_turns (T x) +{ + return cos_turns_mhalfpi_halfpi (trig_turns_detail::fast_mod_mhalf_half (x)); +} } // namespace math_approx diff --git a/test/src/trig_turns_approx_test.cpp b/test/src/trig_turns_approx_test.cpp index 3defeca..64b84c7 100644 --- a/test/src/trig_turns_approx_test.cpp +++ b/test/src/trig_turns_approx_test.cpp @@ -44,14 +44,14 @@ TEST_CASE ("Sine Approx Test") 2.0e-6f, 14); } - SECTION ("9th-Order") + SECTION ("7th-Order") { test_approx ([] (auto x) { return math_approx::sin_turns<7> (x); }, 9.0e-5f, 490); } - SECTION ("9th-Order") + SECTION ("5th-Order") { test_approx ([] (auto x) { return math_approx::sin_turns<5> (x); }, @@ -59,7 +59,7 @@ TEST_CASE ("Sine Approx Test") 22'000); } } -/* + TEST_CASE ("Cosine Approx Test") { #if ! defined(WIN32) @@ -68,115 +68,49 @@ TEST_CASE ("Cosine Approx Test") const auto all_floats = test_helpers::all_32_bit_floats (-10.0f, 10.0f, 1.0e-1f); #endif const auto y_exact = test_helpers::compute_all (all_floats, [] (auto x) - { return std::cos (x); }); - - const auto test_approx = [&all_floats, &y_exact] (auto&& f_approx, float err_bound) - { - const auto y_approx = test_helpers::compute_all (all_floats, f_approx); - - const auto error = test_helpers::compute_error (y_exact, y_approx); - const auto max_error = test_helpers::abs_max (error); + { return sincospi::cos2pi (x); }); - std::cout << max_error << std::endl; - REQUIRE (std::abs (max_error) < err_bound); - }; - - SECTION ("9th-Order") - { - test_approx ([] (auto x) - { return math_approx::cos<9> (x); }, - 7.5e-7f); - } - SECTION ("7th-Order") - { - test_approx ([] (auto x) - { return math_approx::cos<7> (x); }, - 1.8e-5f); - } - SECTION ("5th-Order") - { - test_approx ([] (auto x) - { return math_approx::cos<5> (x); }, - 7.5e-4f); - } -} - -TEST_CASE ("Tan Approx Test") -{ -#if ! defined(WIN32) - const auto all_floats = test_helpers::all_32_bit_floats (-1.5f, 1.5f, 1.0e-3f); -#else - const auto all_floats = test_helpers::all_32_bit_floats (-1.5f, 1.5f, 1.0e-1f); -#endif - const auto y_exact = test_helpers::compute_all (all_floats, [] (auto x) - { return std::tan (x); }); - - const auto test_approx = [&all_floats, &y_exact] (auto&& f_approx, float err_bound, float rel_err_bound, uint32_t ulp_bound) + const auto test_approx = [&all_floats, &y_exact] (auto&& f_approx, float rel_err_bound, uint32_t ulp_err_bound) { const auto y_approx = test_helpers::compute_all (all_floats, f_approx); - const auto error = test_helpers::compute_error (y_exact, y_approx); const auto rel_error = test_helpers::compute_rel_error (y_exact, y_approx); const auto ulp_error = test_helpers::compute_ulp_error (y_exact, y_approx); - const auto max_error = test_helpers::abs_max (error); const auto max_rel_error = test_helpers::abs_max (rel_error); const auto max_ulp_error = *std::max_element (ulp_error.begin(), ulp_error.end()); - std::cout << max_error << ", " << max_rel_error << ", " << max_ulp_error << std::endl; - REQUIRE (std::abs (max_error) < err_bound); + std::cout << max_rel_error << ", " << max_ulp_error << std::endl; REQUIRE (std::abs (max_rel_error) < rel_err_bound); - if (ulp_bound > 0) - REQUIRE (max_ulp_error < ulp_bound); + REQUIRE (max_ulp_error < ulp_err_bound); }; - SECTION ("13th-Order") - { - test_approx ([] (auto x) - { return math_approx::tan<13> (x); }, - 5.5e-5f, - 6.0e-5f, - 520); - } SECTION ("11th-Order") { test_approx ([] (auto x) - { return math_approx::tan<11> (x); }, - 9.5e-5f, - 6.0e-5f, - 520); + { return math_approx::cos_turns<11> (x); }, + 5.0e-7f, + 6); } SECTION ("9th-Order") { test_approx ([] (auto x) - { return math_approx::tan<9> (x); }, - 0.0009f, - 6.0e-5f, - 900); + { return math_approx::cos_turns<9> (x); }, + 2.0e-6f, + 10); } SECTION ("7th-Order") { test_approx ([] (auto x) - { return math_approx::tan<7> (x); }, - 0.015f, - 0.0009f, - 0); + { return math_approx::cos_turns<7> (x); }, + 6.0e-5f, + 270); } SECTION ("5th-Order") { test_approx ([] (auto x) - { return math_approx::tan<5> (x); }, - 0.14f, - 0.01f, - 0); - } - SECTION ("3rd-Order") - { - test_approx ([] (auto x) - { return math_approx::tan<3> (x); }, - 1.5f, - 0.09f, - 0); + { return math_approx::cos_turns<5> (x); }, + 3.0e-3f, + 14'000); } } -*/ diff --git a/tools/bench/trig_turns_bench.cpp b/tools/bench/trig_turns_bench.cpp index 6549fd9..0077fe7 100644 --- a/tools/bench/trig_turns_bench.cpp +++ b/tools/bench/trig_turns_bench.cpp @@ -26,57 +26,45 @@ benchmark::DoNotOptimize (y); \ } \ BENCHMARK (name); -// TRIG_BENCH (cos_std, std::cos) -// TRIG_BENCH (cos_ref, sincospi::) -// TRIG_BENCH (cos_approx9, math_approx::cos<9>) -// TRIG_BENCH (cos_approx7, math_approx::cos<7>) -// TRIG_BENCH (cos_approx5, math_approx::cos<5>) +TRIG_BENCH (cos_std, std::cos) +TRIG_BENCH (cos_ref, sincospi::cos2pi) +TRIG_BENCH (cos_turns_approx11, math_approx::cos_turns<11>) +TRIG_BENCH (cos_turns_approx9, math_approx::cos_turns<9>) +TRIG_BENCH (cos_turns_approx7, math_approx::cos_turns<7>) +TRIG_BENCH (cos_turns_approx5, math_approx::cos_turns<5>) TRIG_BENCH (sin_std, std::sin) -TRIG_BENCH (sinpi_ref, sincospi::sin2pi) -TRIG_BENCH (sinpi_approx9, math_approx::sin_turns<9>) -// TRIG_BENCH (sinpi_approx7, math_approx::sin<7>) -// TRIG_BENCH (sinpi_approx5, math_approx::sin<5>) +TRIG_BENCH (sin_turns_ref, sincospi::sin2pi) +TRIG_BENCH (sin_turns_approx11, math_approx::sin_turns<11>) +TRIG_BENCH (sin_turns_approx9, math_approx::sin_turns<9>) +TRIG_BENCH (sin_turns_approx7, math_approx::sin_turns<7>) +TRIG_BENCH (sin_turns_approx5, math_approx::sin_turns<5>) -// TRIG_BENCH (tan_std, std::tan) -// TRIG_BENCH (tan_approx13, math_approx::tan<13>) -// TRIG_BENCH (tan_approx11, math_approx::tan<11>) -// TRIG_BENCH (tan_approx9, math_approx::tan<9>) -// TRIG_BENCH (tan_approx7, math_approx::tan<7>) -// TRIG_BENCH (tan_approx5, math_approx::tan<5>) -// TRIG_BENCH (tan_approx3, math_approx::tan<3>) - -// #define TRIG_SIMD_BENCH(name, func) \ -// void name (benchmark::State& state) \ -// { \ -// for (auto _ : state) \ -// { \ -// for (auto& x : data) \ -// { \ -// auto y = func (xsimd::broadcast (x)); \ -// static_assert (std::is_same_v, decltype(y)>); \ -// benchmark::DoNotOptimize (y); \ -// } \ -// } \ -// } \ -// BENCHMARK (name); - -// TRIG_SIMD_BENCH (sin_xsimd, xsimd::sin) -// TRIG_SIMD_BENCH (sin_simd_approx9, math_approx::sin<9>) -// TRIG_SIMD_BENCH (sin_simd_approx7, math_approx::sin<7>) -// TRIG_SIMD_BENCH (sin_simd_approx5, math_approx::sin<5>) +#define TRIG_SIMD_BENCH(name, func) \ +void name (benchmark::State& state) \ +{ \ +for (auto _ : state) \ +{ \ +for (auto& x : data) \ +{ \ +auto y = func (xsimd::broadcast (x)); \ +static_assert (std::is_same_v, decltype(y)>); \ +benchmark::DoNotOptimize (y); \ +} \ +} \ +} \ +BENCHMARK (name); -// TRIG_SIMD_BENCH (cos_xsimd, xsimd::cos) -// TRIG_SIMD_BENCH (cos_simd_approx9, math_approx::cos<9>) -// TRIG_SIMD_BENCH (cos_simd_approx7, math_approx::cos<7>) -// TRIG_SIMD_BENCH (cos_simd_approx5, math_approx::cos<5>) +TRIG_SIMD_BENCH (sin_xsimd, xsimd::sin) +TRIG_SIMD_BENCH (sin_turns_simd_approx11, math_approx::sin_turns<11>) +TRIG_SIMD_BENCH (sin_turns_simd_approx9, math_approx::sin_turns<9>) +TRIG_SIMD_BENCH (sin_turns_simd_approx7, math_approx::sin_turns<7>) +TRIG_SIMD_BENCH (sin_turns_simd_approx5, math_approx::sin_turns<5>) -// TRIG_SIMD_BENCH (tan_xsimd, xsimd::tan) -// TRIG_SIMD_BENCH (tan_simd_approx13, math_approx::tan<13>) -// TRIG_SIMD_BENCH (tan_simd_approx11, math_approx::tan<11>) -// TRIG_SIMD_BENCH (tan_simd_approx9, math_approx::tan<9>) -// TRIG_SIMD_BENCH (tan_simd_approx7, math_approx::tan<7>) -// TRIG_SIMD_BENCH (tan_simd_approx5, math_approx::tan<5>) -// TRIG_SIMD_BENCH (tan_simd_approx3, math_approx::tan<3>) +TRIG_SIMD_BENCH (cos_xsimd, xsimd::cos) +TRIG_SIMD_BENCH (cos_turns_simd_approx11, math_approx::cos_turns<11>) +TRIG_SIMD_BENCH (cos_turns_simd_approx9, math_approx::cos_turns<9>) +TRIG_SIMD_BENCH (cos_turns_simd_approx7, math_approx::cos_turns<7>) +TRIG_SIMD_BENCH (cos_turns_simd_approx5, math_approx::cos_turns<5>) BENCHMARK_MAIN(); diff --git a/tools/plotter/plotter.cpp b/tools/plotter/plotter.cpp index 6559ceb..32e2ce2 100644 --- a/tools/plotter/plotter.cpp +++ b/tools/plotter/plotter.cpp @@ -73,29 +73,16 @@ T std_sin_turns (T x) int main() { - std::cout << sincospi::sin2pi (0.0f) << '\n'; - std::cout << sincospi::sin2pi (0.5f) << '\n'; - std::cout << sincospi::sin2pi (-0.5f) << '\n'; - std::cout << math_approx::sin_turns<9> (0.0f) << '\n'; - std::cout << math_approx::sin_turns<9> (0.5f) << '\n'; - std::cout << math_approx::sin_turns<9> (-0.5f) << '\n'; - - // const auto tt = sincospi::sin2pi (0.5f); - // const auto dd = math_approx::sin_turns_mhalfpi_halfpi<9> (0.5f); - // std::cout << tt << ' ' << dd << '\n'; - // const auto err = test_helpers::compute_ulp_error ({ &tt, 1 }, { &dd, 1 }); - // std::cout << err.front() << '\n'; - plt::figure(); const auto range = std::make_pair (-0.5f, 0.5f); static constexpr auto tol = 1.0e-3f; const auto all_floats = test_helpers::all_32_bit_floats (range.first, range.second, tol); - const auto y_exact = test_helpers::compute_all (all_floats, FLOAT_FUNC (sincospi::sin2pi)); + const auto y_exact = test_helpers::compute_all (all_floats, FLOAT_FUNC (sincospi::cos2pi)); // plot_ulp_error (all_floats, y_exact, FLOAT_FUNC ((math_approx::sin_turns<5>) ), "sint-5"); - plot_ulp_error (all_floats, y_exact, FLOAT_FUNC ((math_approx::sin_turns<7>) ), "sint-7"); - plot_ulp_error (all_floats, y_exact, FLOAT_FUNC ((math_approx::sin_turns<9>) ), "sint-9"); - plot_ulp_error (all_floats, y_exact, FLOAT_FUNC ((math_approx::sin_turns<11>) ), "sint-11"); + // plot_ulp_error (all_floats, y_exact, FLOAT_FUNC ((math_approx::sin_turns<7>) ), "sint-7"); + // plot_ulp_error (all_floats, y_exact, FLOAT_FUNC ((math_approx::sin_turns<9>) ), "sint-9"); + plot_ulp_error (all_floats, y_exact, FLOAT_FUNC ((math_approx::cos_turns<11>) ), "cost-11"); plt::legend ({ { "loc", "upper right" } }); plt::xlim (range.first, range.second); From 911b454bb15d116e523474607ae68438f1f5be68 Mon Sep 17 00:00:00 2001 From: jatin Date: Mon, 2 Jun 2025 19:08:33 -0700 Subject: [PATCH 6/8] Fixes and update catch --- include/math_approx/src/trig_approx.hpp | 2 +- test/CMakeLists.txt | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/include/math_approx/src/trig_approx.hpp b/include/math_approx/src/trig_approx.hpp index 53ed3e8..eca8fdd 100644 --- a/include/math_approx/src/trig_approx.hpp +++ b/include/math_approx/src/trig_approx.hpp @@ -302,7 +302,7 @@ constexpr T sin_turns_mhalfpi_halfpi (T x) const auto x_q = x_sq * x_sq; const auto x_9_11 = (S) -14.0496638478f + (S) 3.16160207407f * x_sq; const auto x_5_7 = (S) -67.0766273790f + (S) 38.4958788775f * x_sq; - const auto x_1_3 = (S) -25.1327411554 + (S) 64.8358228565f * x_sq; + const auto x_1_3 = (S) -25.1327411554f + (S) 64.8358228565f * x_sq; const auto x_5_7_9_11 = x_5_7 + x_9_11 * x_q; const auto x_1_3_5_7_9_11 = x_1_3 + x_5_7_9_11 * x_q; y = x * x_1_3_5_7_9_11; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 3bc0374..f309bcf 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,6 +1,6 @@ message(STATUS "math_approx -- Configuring tests...") -CPMAddPackage("gh:catchorg/Catch2@3.2.1") +CPMAddPackage("gh:catchorg/Catch2@3.8.1") include(${Catch2_SOURCE_DIR}/extras/Catch.cmake) function(setup_catch_test target) @@ -14,10 +14,10 @@ function(setup_catch_test target) ) target_compile_features(${target} PUBLIC cxx_std_20) target_compile_definitions(${target} PUBLIC _USE_MATH_DEFINES=1) - # target_compile_options(${target} PRIVATE - # $<$:/W4 /WX> - # $<$>:-Wall -Wextra -Wpedantic -Werror> - # ) + target_compile_options(${target} PRIVATE + $<$:/W4 /WX> + $<$>:-Wall -Wextra -Wpedantic -Werror> + ) add_custom_command(TARGET ${target} POST_BUILD From 7b8d54d87cb5223c7ce157058c24870a2c2e9df2 Mon Sep 17 00:00:00 2001 From: jatin Date: Mon, 2 Jun 2025 19:20:59 -0700 Subject: [PATCH 7/8] Adjusting sinh tests --- test/src/hyperbolic_trig_approx_test.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/src/hyperbolic_trig_approx_test.cpp b/test/src/hyperbolic_trig_approx_test.cpp index 30d0a27..28435ea 100644 --- a/test/src/hyperbolic_trig_approx_test.cpp +++ b/test/src/hyperbolic_trig_approx_test.cpp @@ -38,7 +38,7 @@ TEST_CASE ("Sinh Approx Test") test_approx ([] (auto x) { return math_approx::sinh_cosh<6> (x).first; }, 6.0e-6f, - 0.00011f, + 0.00012f, 1050); } SECTION ("6th-Order") @@ -46,7 +46,7 @@ TEST_CASE ("Sinh Approx Test") test_approx ([] (auto x) { return math_approx::sinh<6> (x); }, 6.0e-6f, - 0.00011f, + 0.00012f, 1050); } SECTION ("5th-Order") @@ -62,7 +62,7 @@ TEST_CASE ("Sinh Approx Test") test_approx ([] (auto x) { return math_approx::sinh<4> (x); }, 6.0e-5f, - 0.00025f, + 0.00027f, 0); } SECTION ("3rd-Order") From e4c895cc13273658e3b564cc8f3b22d4f15dc6a5 Mon Sep 17 00:00:00 2001 From: jatin Date: Mon, 2 Jun 2025 23:35:52 -0700 Subject: [PATCH 8/8] Testing roundss on Intel --- CMakeLists.txt | 8 +++++ include/math_approx/src/trig_approx.hpp | 44 ++++++++++++------------- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6ff37fc..e4ae99f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,7 +16,15 @@ add_library(math_approx INTERFACE) target_include_directories(math_approx INTERFACE include) if(MSVC) target_compile_definitions(math_approx INTERFACE _USE_MATH_DEFINES=1) + if(CMAKE_CXX_COMPILER_ID STREQUAL Clang) + target_compile_options(math_approx INTERFACE -msse4.2) + else() + target_compile_options(math_approx INTERFACE /arch:SSE4.2) + endif() +else() + target_compile_options(math_approx INTERFACE -msse4.2 -Wno-unused-command-line-argument) endif() + if (TARGET xsimd) message(STATUS "math_approx -- Linking with XSIMD...") target_link_libraries(math_approx INTERFACE xsimd) diff --git a/include/math_approx/src/trig_approx.hpp b/include/math_approx/src/trig_approx.hpp index eca8fdd..b8fc9b7 100644 --- a/include/math_approx/src/trig_approx.hpp +++ b/include/math_approx/src/trig_approx.hpp @@ -81,7 +81,7 @@ namespace trig_detail const auto x_1_3_5 = (S) 0.101256629587 + x_3_5 * x_sq; return x * x_1_3_5; } -} // namespace sin_detail +} // namespace trig_detail /** Polynomial approximation of sin(x) on the range [-pi, pi] */ template @@ -216,7 +216,7 @@ constexpr T tan_mquarterpi_quarterpi (T x) const auto x_8 = x_q * x_q; const auto x_13_15 = (S) 0.000292958045126 + (S) 0.00427933470414 * x_sq; const auto x_9_11 = (S) 0.0213477960960 + (S) 0.0106702896251 * x_sq; - const auto x_5_7 = (S) 0.133327796402 + (S) 0.0540469276103* x_sq; + const auto x_5_7 = (S) 0.133327796402 + (S) 0.0540469276103 * x_sq; const auto x_1_3 = (S) 1 + (S) 0.333333463757 * x_sq; const auto x_9_11_13_15 = x_9_11 + x_13_15 * x_q; const auto x_1_3_5_7 = x_1_3 + x_5_7 * x_q; @@ -263,29 +263,29 @@ namespace trig_turns_detail template constexpr T fast_mod_mhalf_half (T x) { - // @TODO: test this on Intel... - // auto y =_mm_round_ss (_mm_load_ps1(&x), _mm_load_ps1(&x), 12); - // // auto y =_mm_round_ss (__m128{}, _mm_load_ps1(&x), 12); - // return x - reinterpret_cast (y); - - // _asm { - // roundss xmm1, xmm0, 12 - // subss xmm0, xmm1 - // ret - // } - - // using S = scalar_of_t; - // x += (S) 0.5; - // const auto mod = x - truncate (x); - // return select (x >= (T) 0, mod, mod + (S) 1) - (S) 0.5; - - using std::nearbyint; + if constexpr (std::is_same_v) + { +#if defined(__SSE4_1__) || defined(_MSC_VER) + auto y = _mm_round_ss (_mm_load_ps1 (&x), _mm_load_ps1 (&x), 12); + return x - reinterpret_cast (y); +#else + using std::nearbyint; #if defined(XSIMD_HPP) - using xsimd::nearbyint; + using xsimd::nearbyint; #endif - return x - nearbyint (x); + return x - nearbyint (x); +#endif + } + else + { + using std::nearbyint; +#if defined(XSIMD_HPP) + using xsimd::nearbyint; +#endif + return x - nearbyint (x); + } } -} +} // namespace trig_turns_detail /** Polynomial approximation of sin(2*pi*x) on the range [-pi/2, pi/2] */ template