diff --git a/Kconfig b/Kconfig index a192aa93ab09..1c9849d9903e 100644 --- a/Kconfig +++ b/Kconfig @@ -1,7 +1,112 @@ # SPDX-License-Identifier: BSD-3-Clause -mainmenu "SOF $(PROJECTVERSION) Configuration" +config COMP_FFMPEG_DEC + tristate "FFmpeg (libavcodec) audio decoder" + select COMP_FFMPEG_DEC_STUB if COMP_STUBS + help + Select to include the FFmpeg audio decoder module. It wraps + libavcodec decoders behind the SOF module interface, decoding a + compressed elementary stream to PCM. The real backend cross-builds + libavcodec/libavutil/libswresample from the FFmpeg source pulled in + by west (see west.yml), enabling only the decoders selected below. + Without the source (or for CI) select COMP_FFMPEG_DEC_STUB to build + the dependency-free passthrough stub. -comment "Compiler: $(CC_VERSION_TEXT)" +config COMP_FFMPEG_DEC_STUB + bool "FFmpeg decoder stub backend" + depends on COMP_FFMPEG_DEC + help + Build the ffmpeg_dec module against a dependency-free passthrough + backend instead of libavcodec. Intended for testing and CI so the + SOF glue and LLEXT packaging can be validated without the FFmpeg + source or a cross-built archive. -source "Kconfig.sof" +if COMP_FFMPEG_DEC && !COMP_FFMPEG_DEC_STUB + +comment "FFmpeg decoders to build (footprint scales with selection)" + +config FFMPEG_DEC_FLAC + bool "FLAC decoder" + default y + help + Lossless integer decoder. Smallest footprint, no floating-point + math. Enables --enable-decoder=flac in the libavcodec build. + +config FFMPEG_DEC_AAC + bool "AAC-LC decoder" + help + AAC Low Complexity decoder. Pulls in the single-precision float + math layer (see FFMPEG_DEC_FLOAT_MATH). Enables + --enable-decoder=aac in the libavcodec build. + +config FFMPEG_DEC_OPUS + bool "Opus decoder" + help + Opus (SILK+CELT) decoder. Pulls in the single-precision float math + layer. Enables --enable-decoder=opus in the libavcodec build. + +config FFMPEG_DEC_MP3 + bool "MP3 decoder" + help + MPEG-1/2 Layer III decoder. Enables --enable-decoder=mp3 and the + mpegaudio parser in the libavcodec build. Uses the float math layer. + +config FFMPEG_ENC_MP3 + bool "MP3 encoder (libshine, fixed-point)" + help + MPEG-1 Layer III encoder via libshine, a small fixed-point encoder + (west-pinned; FFmpeg has no native MP3 encoder). Cross-builds libshine + and enables --enable-libshine --enable-encoder=libshine. Fixed-point, so + no float math dependency. The encoder is built into the archive; a module + encode path (PCM->MP3) is a separate build mode. + +comment "FFmpeg audio filters (libavfilter; need the avfilter-graph backend to run)" + +config FFMPEG_FILTER_AFFTDN + bool "FFT noise reduction (afftdn)" + help + Build FFmpeg's FFT-based denoiser (afftdn) into libavfilter. Pure DSP, + no external model file (unlike arnndn). Enabling any filter turns on + libavfilter in the cross-build. NOTE: a filter is only usable at runtime + once the module gains an avfilter-graph backend; selecting it here just + builds the filter into the archive. + +config FFMPEG_DEC_FILTER_MODE + bool "Build as a PCM filter effect instead of a decoder [EXPERIMENTAL]" + select FFMPEG_FILTER_AFFTDN + help + Build the module as a PCM source->sink effect that runs an FFmpeg + audio filter graph (default afftdn noise reduction) via the modern + .process interface, instead of the compressed-stream decoder. Turns on + libavfilter and the fast float math. Structurally complete and + load-ready; real-time latency/format tuning needs on-hardware bring-up. + +# Turns on libavfilter in the FFmpeg cross-build when any filter is selected. +config FFMPEG_BUILD_AVFILTER + bool + default y if FFMPEG_FILTER_AFFTDN + +# Selected automatically when a decoder or filter that needs IEEE float math is +# built. Gates compilation of the fast single-precision libm (fastmathf.c). +config FFMPEG_DEC_FLOAT_MATH + bool + default y if FFMPEG_DEC_AAC || FFMPEG_DEC_OPUS || FFMPEG_DEC_MP3 || FFMPEG_FILTER_AFFTDN + +config FFMPEG_DEC_COLD_SPLIT + bool "Place cold FFmpeg code (av_cold init/setup) in DRAM [EXPERIMENTAL]" + depends on COLD_STORE_EXECUTE_DRAM + default n + help + EXPERIMENTAL. Build libavcodec with -mtext-section-literals and rename + FFmpeg's cold functions (its av_cold init/table-generation code, which GCC + emits into .text.unlikely) into SOF's .cold section so the LLEXT loader + places them in DRAM, keeping the hot per-frame decode in fast SRAM. + + Measured cold code is small (~1 KB for FLAC, ~26 KB for AAC) because + FFmpeg only marks init functions av_cold. Placing that much cold code in a + -shared LLEXT currently hits Xtensa linker limits (cold->hot call8 range + needs -mlongcalls; the orphan .cold lands at a bad LMA and overlaps .hash), + so this is off by default pending proper cold-region placement in the LLEXT + memory map. See git history / TESTING notes. + +endif # COMP_FFMPEG_DEC && !COMP_FFMPEG_DEC_STUB diff --git a/app/boards/acp_6_0_adsp.conf b/app/boards/acp_6_0_adsp.conf index b527ea3753a7..f7c6dc1dd471 100644 --- a/app/boards/acp_6_0_adsp.conf +++ b/app/boards/acp_6_0_adsp.conf @@ -38,3 +38,4 @@ CONFIG_ASRC_SUPPORT_CONVERSION_48000_TO_22050=n CONFIG_ASRC_SUPPORT_CONVERSION_48000_TO_24000=n CONFIG_ASRC_SUPPORT_CONVERSION_48000_TO_32000=n CONFIG_ASRC_SUPPORT_CONVERSION_48000_TO_44100=n +CONFIG_FORMAT_CONVERT_HIFI3=n diff --git a/app/boards/intel_adsp_ace30_ptl.conf b/app/boards/intel_adsp_ace30_ptl.conf index b6ac41938398..9ee1a3e75b10 100644 --- a/app/boards/intel_adsp_ace30_ptl.conf +++ b/app/boards/intel_adsp_ace30_ptl.conf @@ -5,14 +5,14 @@ CONFIG_RIMAGE_SIGNING_SCHEMA="ptl" CONFIG_IPC_MAJOR_4=y # SOF / audio pipeline and module settings -CONFIG_COMP_TESTER=m +CONFIG_COMP_TESTER=y CONFIG_COMP_SRC_IPC4_FULL_MATRIX=y CONFIG_FORMAT_CONVERT_HIFI3=n # SOF / audio modules / mocks # This mock is part of official sof-bin releases because the CI that # tests it can't use extra CONFIGs. See #9410, #8722 and #9386 -CONFIG_COMP_GOOGLE_RTC_AUDIO_PROCESSING=m +CONFIG_COMP_GOOGLE_RTC_AUDIO_PROCESSING=y CONFIG_GOOGLE_RTC_AUDIO_PROCESSING_MOCK=y CONFIG_COMP_STFT_PROCESS=y @@ -31,7 +31,7 @@ CONFIG_LIBRARY_AUTH_SUPPORT=y CONFIG_LIBRARY_MANAGER=y CONFIG_LIBRARY_BASE_ADDRESS=0xa0688000 CONFIG_LIBRARY_BUILD_LIB=y -CONFIG_LIBRARY_DEFAULT_MODULAR=y +CONFIG_LIBRARY_DEFAULT_MODULAR=n # SOF / logging CONFIG_SOF_LOG_LEVEL_INF=y @@ -78,3 +78,19 @@ CONFIG_SOF_USERSPACE_PROXY=y CONFIG_MAX_THREAD_BYTES=3 CONFIG_MAX_DOMAIN_PARTITIONS=32 + +CONFIG_COMP_DRC=y +CONFIG_COMP_ASRC=y +CONFIG_COMP_CROSSOVER=y +CONFIG_COMP_FIR=y +CONFIG_COMP_IIR=y +CONFIG_COMP_MULTIBAND_DRC=y +CONFIG_COMP_TDFB=y +CONFIG_COMP_VOLUME=y +CONFIG_COMP_SEL=y +CONFIG_COMP_TONE=y +CONFIG_COMP_MIXIN_MIXOUT=y +CONFIG_COMP_MUX=y + +# Disable UAOL to prevent boot-time register access crash when link is unpowered +CONFIG_UAOL=n diff --git a/app/boards/native_sim.conf b/app/boards/native_sim.conf new file mode 100644 index 000000000000..6df3eb89b648 --- /dev/null +++ b/app/boards/native_sim.conf @@ -0,0 +1,4 @@ +CONFIG_ZEPHYR_POSIX=y +CONFIG_SYS_HEAP_BIG_ONLY=y +CONFIG_ZEPHYR_NATIVE_DRIVERS=y +CONFIG_ZEPHYR_LOG=y diff --git a/app/boards/qemu_xtensa_dc233c.conf b/app/boards/qemu_xtensa_dc233c.conf index effed89802af..6fd73ceee4d6 100644 --- a/app/boards/qemu_xtensa_dc233c.conf +++ b/app/boards/qemu_xtensa_dc233c.conf @@ -6,3 +6,13 @@ CONFIG_ZEPHYR_NATIVE_DRIVERS=y # Ensure the kernel can exit QEMU on shutdown/panic CONFIG_REBOOT=y + +# Disable linker relaxation because LLD's naive in-place relaxation +# of L32R + CALLX/JX into CALL/J + NOP breaks local branches targeting +# the CALLX/JX instruction (such as in z_impl_log_panic). +CONFIG_LINKER_USE_RELAX=n + +# Enable immediate logging to ensure logs are printed before semihosting exits +CONFIG_LOG_MODE_IMMEDIATE=y + + diff --git a/app/compiler_rt.conf b/app/compiler_rt.conf new file mode 100644 index 000000000000..8205aee052d4 --- /dev/null +++ b/app/compiler_rt.conf @@ -0,0 +1 @@ +CONFIG_COMPILER_RT_RTLIB=y diff --git a/app/debug_overlay.conf b/app/debug_overlay.conf index e99910ebfff1..a6f8855d5011 100644 --- a/app/debug_overlay.conf +++ b/app/debug_overlay.conf @@ -31,3 +31,8 @@ CONFIG_GDBSTUB_ENTER_IMMEDIATELY=n # Testing with runtime filtering enabled ensures the same feature set is validated. # Note: This setting has no effect if CONFIG_LOG_RUNTIME_FILTERING is disabled. CONFIG_LOG_RUNTIME_DEFAULT_LEVEL=3 + +# Record fatal exception breadcrumbs (PC/cause/vaddr) in HP-SRAM window0 so the +# crash is visible in the host dmesg "Firmware state" line when no console or +# mtrace output is available. +CONFIG_XTENSA_ADSP_FATAL_BREADCRUMB=y diff --git a/app/src/main.c b/app/src/main.c index 12cc3ffdc73a..aa9ee6960d8d 100644 --- a/app/src/main.c +++ b/app/src/main.c @@ -57,7 +57,7 @@ static int sof_app_main(void) return 0; } -#if CONFIG_SOF_BOOT_TEST && defined(QEMU_BOOT_TESTS) +#if defined(QEMU_BOOT_TESTS) /* cleanly exit qemu so CI can continue and check test results */ static inline void qemu_xtensa_exit(int status) { @@ -73,14 +73,31 @@ static inline void qemu_xtensa_exit(int status) } #endif +#ifdef CONFIG_REBOOT +void sys_arch_reboot(int type) +{ +#if defined(QEMU_BOOT_TESTS) + qemu_xtensa_exit(type); +#endif + while (1) { + k_cpu_idle(); + } +} +#endif + #if CONFIG_ZTEST void test_main(void) { sof_app_main(); -#if CONFIG_SOF_BOOT_TEST && defined(QEMU_BOOT_TESTS) +#if CONFIG_SOF_BOOT_TEST && (defined(QEMU_BOOT_TESTS) || CONFIG_SOF_BOOT_TEST_STANDALONE) sof_run_boot_tests(); +#if defined(QEMU_BOOT_TESTS) + /* qemu_xtensa_exit() only exists for QEMU targets; a standalone + * boot test (e.g. native_sim) just returns from test_main() + */ qemu_xtensa_exit(0); #endif +#endif } #else int main(void) diff --git a/ffmpeg.cmake b/ffmpeg.cmake new file mode 100644 index 000000000000..b705e02adf41 --- /dev/null +++ b/ffmpeg.cmake @@ -0,0 +1,199 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Cross-build decoder-only FFmpeg static libraries for the ffmpeg_dec module. +# +# Invokes FFmpeg's autotools build as an ExternalProject, cross-compiled with the +# Zephyr target toolchain, enabling only the decoders selected in Kconfig. The +# source comes from the west-pinned FFmpeg project (see west.yml). Produces +# libavcodec/libavutil/libswresample .a under ${FFMPEG_INSTALL_DIR} for the LLEXT +# to link, and defines the 'ffmpeg_ext' target it must depend on. + +include(ExternalProject) + +# --- 1. Locate the FFmpeg source (west module), overridable for local trees --- +if(NOT DEFINED SOF_FFMPEG_SRC_DIR) + set(SOF_FFMPEG_SRC_DIR "${sof_top_dir}/../modules/audio/ffmpeg" + CACHE PATH "FFmpeg source tree (west-pinned)") +endif() +cmake_path(NORMAL_PATH SOF_FFMPEG_SRC_DIR) +if(NOT EXISTS "${SOF_FFMPEG_SRC_DIR}/configure") + message(FATAL_ERROR + "ffmpeg_dec: FFmpeg source not found at '${SOF_FFMPEG_SRC_DIR}'.\n" + "Run 'west update' (FFmpeg is pinned in west.yml) or pass " + "-DSOF_FFMPEG_SRC_DIR=.") +endif() + +# --- 2. Kconfig -> --enable-decoder / --enable-parser lists --- +set(_ff_decoders "") +set(_ff_parsers "") +if(CONFIG_FFMPEG_DEC_FLAC) + list(APPEND _ff_decoders flac) + list(APPEND _ff_parsers flac) +endif() +if(CONFIG_FFMPEG_DEC_AAC) + list(APPEND _ff_decoders aac aac_latm) + list(APPEND _ff_parsers aac aac_latm) +endif() +if(CONFIG_FFMPEG_DEC_OPUS) + list(APPEND _ff_decoders opus) + list(APPEND _ff_parsers opus) +endif() +if(CONFIG_FFMPEG_DEC_MP3) + list(APPEND _ff_decoders mp3) + list(APPEND _ff_parsers mpegaudio) +endif() +if(NOT _ff_decoders) + message(FATAL_ERROR "ffmpeg_dec: no decoder selected (Kconfig FFMPEG_DEC_*)") +endif() +list(JOIN _ff_decoders "," _ff_dec_csv) +list(JOIN _ff_parsers "," _ff_par_csv) + +# --- 2b. Kconfig -> libavfilter audio filters (off unless a filter is chosen) --- +set(_ff_avfilter_cfg --disable-avfilter) +if(CONFIG_FFMPEG_BUILD_AVFILTER) + set(_ff_filters "") + if(CONFIG_FFMPEG_FILTER_AFFTDN) + list(APPEND _ff_filters afftdn) + endif() + list(JOIN _ff_filters "," _ff_filt_csv) + # abuffer/abuffersink feed/drain a filter graph; aformat negotiates format. + set(_ff_avfilter_cfg + --enable-avfilter + --enable-filter=abuffer,abuffersink,aformat,${_ff_filt_csv}) + message(STATUS "ffmpeg_dec: enabling avfilter, filters [${_ff_filt_csv}]") +endif() + +# --- 3. Derive the cross toolchain prefix from the Zephyr target compiler --- +# e.g. .../bin/xtensa-intel_ace30_ptl_zephyr-elf-gcc +# -> prefix .../bin/xtensa-intel_ace30_ptl_zephyr-elf- +get_filename_component(_tc_dir "${CMAKE_C_COMPILER}" DIRECTORY) +get_filename_component(_tc_name "${CMAKE_C_COMPILER}" NAME) +string(REGEX REPLACE "gcc$" "" _tc_prefix_name "${_tc_name}") +set(_ff_cross_prefix "${_tc_dir}/${_tc_prefix_name}") + +# GCC 14 (Zephyr SDK) promotes these to errors; FFmpeg 7.x trips them. +set(_ff_extra_cflags "-fPIC -Wno-error=incompatible-pointer-types -Wno-error=implicit-function-declaration") + +set(FFMPEG_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/ffmpeg-install" CACHE INTERNAL "ffmpeg_dec libs") + +# Hot/cold split: GCC emits av_cold (init/setup) functions into .text.unlikely. +# We rename that to SOF's .cold (post-install, below) so the LLEXT loader places +# it in DRAM. -mtext-section-literals interleaves each function's Xtensa L32R +# literals into its text section (as SOF does for LLEXT module code) so the +# renamed section carries its literals and L32R stays in range. We deliberately +# do NOT use -ffunction-sections: keeping one .text.unlikely per object lets a +# single objcopy --rename-section catch it, and lets .cold be placed by SOF's +# existing orphan .cold handling (a hand-written linker script that forces .cold +# ahead of .text overruns the SRAM budget and overlaps .rodata for big codecs). +if(CONFIG_FFMPEG_DEC_COLD_SPLIT) + # -mlongcalls: cold code lives in DRAM (.cold, relocated by the loader) far + # from the hot SRAM .text, so calls between them exceed call8 range; longcalls + # emit an indirect L32R+CALLX with unlimited range. -mtext-section-literals + # keeps each function's literals in its own text section so the rename carries + # them. Coarse split: rename the whole FFmpeg .text (not just av_cold) into + # .cold so essentially all of libav* executes from DRAM, freeing SRAM. + set(_ff_extra_cflags "${_ff_extra_cflags} -mtext-section-literals -mlongcalls") + set(_ff_objcopy "${_ff_cross_prefix}objcopy") + set(_ff_cold_rename + COMMAND ${_ff_objcopy} --rename-section .text=.cold + ${FFMPEG_INSTALL_DIR}/lib/libavcodec.a + COMMAND ${_ff_objcopy} --rename-section .text=.cold + ${FFMPEG_INSTALL_DIR}/lib/libavutil.a + COMMAND ${_ff_objcopy} --rename-section .text=.cold + ${FFMPEG_INSTALL_DIR}/lib/libswresample.a) +endif() + +# --- 3b. MP3 encoder via libshine (fixed-point; FFmpeg has no native one) --- +set(_ff_cfg_env "PATH=${_tc_dir}:$ENV{PATH}") +set(_ff_shine_cfg "") +set(_ff_shine_dep "") +if(CONFIG_FFMPEG_ENC_MP3) + set(SHINE_SRC "${sof_top_dir}/../modules/audio/shine") + if(NOT EXISTS "${SHINE_SRC}/src/lib/layer3.h") + message(FATAL_ERROR "ffmpeg_dec: libshine source not at '${SHINE_SRC}'; run 'west update'") + endif() + set(SHINE_INSTALL "${CMAKE_CURRENT_BINARY_DIR}/shine-install") + file(MAKE_DIRECTORY "${SHINE_INSTALL}/lib/pkgconfig" "${SHINE_INSTALL}/include/shine") + # pkg-config file for FFmpeg's require_pkg_config libshine. + file(WRITE "${SHINE_INSTALL}/lib/pkgconfig/shine.pc" +"prefix=${SHINE_INSTALL} +libdir=\${prefix}/lib +includedir=\${prefix}/include +Name: shine +Description: Shine fixed-point MP3 encoder +Version: 3.1.1 +Libs: -L\${libdir} -lshine +Cflags: -I\${includedir} +") + # FFmpeg's -lshine link *test* pulls newlib malloc -> Zephyr runtime syms + # (z_errno_wrap, ...) that only exist at module load. Provide dummies so the + # configure test links; --extra-ldflags only affects tests, not the .a build. + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/shine_cfgstub.c" +"int z_errno_wrap(void){return 0;} +void *stdout; +int open(const char *p, int f, ...){return -1;} +void *sbrk(int i){return (void *)-1;} +") + # libshine's lib needs no config.h; build the objects directly and archive + # (its autotools CLI/shared link cannot work bare-metal). + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/shine-build.sh" +"#!/bin/sh +set -e +export PATH=${_tc_dir}:\$PATH +mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/shine-obj +for f in ${SHINE_SRC}/src/lib/*.c; do + ${CMAKE_C_COMPILER} -O2 -fPIC -mtext-section-literals -DSHINE_HAVE_BSWAP_H \\ + -I${SHINE_SRC}/src/lib -c \"\$f\" \\ + -o ${CMAKE_CURRENT_BINARY_DIR}/shine-obj/\$(basename \"\$f\").o +done +${_ff_cross_prefix}ar rcs ${SHINE_INSTALL}/lib/libshine.a ${CMAKE_CURRENT_BINARY_DIR}/shine-obj/*.o +cp ${SHINE_SRC}/src/lib/layer3.h ${SHINE_INSTALL}/include/shine/layer3.h +${CMAKE_C_COMPILER} -fPIC -c ${CMAKE_CURRENT_BINARY_DIR}/shine_cfgstub.c \\ + -o ${CMAKE_CURRENT_BINARY_DIR}/shine_cfgstub.o +") + add_custom_command( + OUTPUT "${SHINE_INSTALL}/lib/libshine.a" "${CMAKE_CURRENT_BINARY_DIR}/shine_cfgstub.o" + COMMAND sh "${CMAKE_CURRENT_BINARY_DIR}/shine-build.sh" + VERBATIM) + add_custom_target(shine_ext DEPENDS "${SHINE_INSTALL}/lib/libshine.a") + + set(_ff_cfg_env "PATH=${_tc_dir}:$ENV{PATH}" "PKG_CONFIG_PATH=${SHINE_INSTALL}/lib/pkgconfig") + set(_ff_shine_cfg --enable-libshine --enable-encoder=libshine --pkg-config=pkg-config + "--extra-ldflags=${CMAKE_CURRENT_BINARY_DIR}/shine_cfgstub.o") + set(_ff_shine_dep shine_ext) + message(STATUS "ffmpeg_dec: enabling MP3 encoder via libshine (${SHINE_SRC})") +endif() + +# --- 4. Configure + make + install (out-of-tree; source must be clean) --- +ExternalProject_Add(ffmpeg_ext + DEPENDS ${_ff_shine_dep} + SOURCE_DIR "${SOF_FFMPEG_SRC_DIR}" + BUILD_IN_SOURCE 0 + CONFIGURE_COMMAND + ${CMAKE_COMMAND} -E env ${_ff_cfg_env} + /configure + --prefix=${FFMPEG_INSTALL_DIR} + --enable-cross-compile --target-os=none --arch=xtensa + --cross-prefix=${_ff_cross_prefix} --cc=${CMAKE_C_COMPILER} + --disable-asm --disable-all --disable-everything --disable-autodetect + --disable-programs --disable-doc --disable-network + --disable-avformat --disable-avdevice ${_ff_avfilter_cfg} + --disable-swscale --disable-postproc + --disable-pthreads --disable-w32threads --disable-os2threads + --disable-runtime-cpudetect --disable-debug + --enable-avcodec --enable-avutil --enable-swresample + --enable-decoder=${_ff_dec_csv} --enable-parser=${_ff_par_csv} + ${_ff_shine_cfg} + --enable-small --enable-pic + "--extra-cflags=${_ff_extra_cflags}" + BUILD_COMMAND + ${CMAKE_COMMAND} -E env "PATH=${_tc_dir}:$ENV{PATH}" make -j 8 + INSTALL_COMMAND make install + ${_ff_cold_rename} + BUILD_BYPRODUCTS + ${FFMPEG_INSTALL_DIR}/lib/libavcodec.a + ${FFMPEG_INSTALL_DIR}/lib/libavutil.a + ${FFMPEG_INSTALL_DIR}/lib/libswresample.a +) + +message(STATUS "ffmpeg_dec: cross-building FFmpeg decoders [${_ff_dec_csv}] from ${SOF_FFMPEG_SRC_DIR}") diff --git a/scripts/llext_link_helper.py b/scripts/llext_link_helper.py index f10777c9918f..1e6d8e7ff601 100755 --- a/scripts/llext_link_helper.py +++ b/scripts/llext_link_helper.py @@ -14,6 +14,7 @@ from elftools.elf.constants import SH_FLAGS import re import pathlib +import fcntl args = None def parse_args(): @@ -57,10 +58,17 @@ def main(): # It is used to automatically calculate starting address of the current # module. try: - with open(args.size_file, 'r') as f_size: - size = int(f_size.read().strip(), base = 0) + f_size_file = open(args.size_file, 'a+') + fcntl.flock(f_size_file, fcntl.LOCK_EX) + f_size_file.seek(0) + content = f_size_file.read().strip() + if content: + size = int(content, base=0) + else: + size = 0 except OSError: size = 0 + f_size_file = None text_addr = int(args.text_addr, 0) + size text_size = 0 @@ -86,13 +94,6 @@ def main(): elf = ELFFile(open(args.file, 'rb')) - # Create an object file with sections grouped by their properties, - # similar to how program segments are created: all executable sections, - # then all read-only data sections, and eventually all writable data - # sections like .data and .bss. Each group is aligned on a page boundary - # (0x1000) to make dynamic memory mapping possible. The resulting object - # file will either be a shared library or a relocatable (partially - # linked) object, depending on the build configuration. for section in elf.iter_sections(): s_flags = section.header['sh_flags'] s_type = section.header['sh_type'] @@ -106,7 +107,6 @@ def main(): if (s_flags & (SH_FLAGS.SHF_ALLOC | SH_FLAGS.SHF_EXECINSTR) == SH_FLAGS.SHF_ALLOC | SH_FLAGS.SHF_EXECINSTR and s_type == 'SHT_PROGBITS'): - # An executable section. if s_name == '.text': text_found = True text_addr = max_alignment(text_addr, 0x1000, s_alignment) @@ -114,17 +114,14 @@ def main(): command.append(f'-Wl,-Ttext=0x{text_addr:x}') else: executable.append(section) - continue if (s_flags & (SH_FLAGS.SHF_WRITE | SH_FLAGS.SHF_ALLOC) == SH_FLAGS.SHF_WRITE | SH_FLAGS.SHF_ALLOC): - # .data, .bss or other writable sections writable.append(section) continue - if s_type == 'SHT_PROGBITS' and s_flags & SH_FLAGS.SHF_ALLOC: - # .rodata or other read-only sections + else: if s_name == '.coldrodata': readonly_dram.append(section) else: @@ -133,24 +130,6 @@ def main(): if not text_found: raise RuntimeError('No .text section found in the object file') - # The original LLEXT support in SOF linked all LLEXT modules with pre- - # calculated addresses. Such modules can only be used at those exact - # addresses, so we map memory buffers for such modules to those - # addresses and copy them there. - # Now we also need to be able to re-link parts of modules at run-time to - # run at arbitrary memory locations. One of the use-cases is running - # parts of the module directly in DRAM - sacrificing performance but - # saving scarce SRAM. We achieve this by placing non-performance - # critical functions in a .cold ELF section, read-only data in a - # .coldrodata ELF section, etc. When compiling and linking such - # functions, an additional .cold.literal section is automatically - # created. Note, that for some reason the compiler also marks .cold as - # executable. - # This script links those sections at address 0. We could hard-code - # section names, but so far we choose to only link .text the "original" - # way and all other executable sections we link at 0. For data sections - # we accept only the .coldrodata name for now. - dram_addr = 0 first_dram_text = None first_dram_rodata = None @@ -158,27 +137,19 @@ def main(): for section in executable: s_alignment = section.header['sh_addralign'] s_name = section.name - if not first_dram_text: first_dram_text = s_name - dram_addr = align_up(dram_addr, s_alignment) - command.append(f'-Wl,--section-start={s_name}=0x{dram_addr:x}') - dram_addr += section.header['sh_size'] for section in readonly_dram: s_alignment = section.header['sh_addralign'] s_name = section.name - if not first_dram_rodata: first_dram_rodata = s_name - dram_addr = align_up(dram_addr, s_alignment) - command.append(f'-Wl,--section-start={s_name}=0x{dram_addr:x}') - dram_addr += section.header['sh_size'] start_addr = align_up(text_addr + text_size, 0x1000) @@ -186,11 +157,8 @@ def main(): for section in readonly: s_alignment = section.header['sh_addralign'] s_name = section.name - start_addr = align_up(start_addr, s_alignment) - command.append(f'-Wl,--section-start={s_name}=0x{start_addr:x}') - start_addr += section.header['sh_size'] start_addr = align_up(start_addr, 0x1000) @@ -198,14 +166,11 @@ def main(): for section in writable: s_alignment = section.header['sh_addralign'] s_name = section.name - start_addr = align_up(start_addr, s_alignment) - if s_name == '.data': command.append(f'-Wl,-Tdata=0x{start_addr:x}') else: command.append(f'-Wl,--section-start={s_name}=0x{start_addr:x}') - start_addr += section.header['sh_size'] command.extend(['-o', f'{args.file}.tmp']) @@ -213,6 +178,146 @@ def main(): subprocess.run(command) + # Patch the sh_addr and st_value in the output ELF file created by LLD + import struct + + def do_align_up(addr, align): + if align <= 1: + return addr + upper = addr + align - 1 + return upper - (upper % align) + + addr_map = {'.text': text_addr} + dram_addr2 = text_addr if (text_size == 0 and executable) else 0 + + tmp_sec_map = {} + try: + tmp_elf = ELFFile(open(f'{args.file}.tmp', 'rb')) + tmp_sec_map = {s.name: s for s in tmp_elf.iter_sections() if s.header['sh_flags'] & SH_FLAGS.SHF_ALLOC} + except Exception: + pass + + for section in executable: + s_obj = tmp_sec_map.get(section.name, section) + dram_addr2 = do_align_up(dram_addr2, s_obj.header['sh_addralign']) + addr_map[section.name] = dram_addr2 + dram_addr2 += s_obj.header['sh_size'] + + for section in readonly_dram: + s_obj = tmp_sec_map.get(section.name, section) + dram_addr2 = do_align_up(dram_addr2, s_obj.header['sh_addralign']) + addr_map[section.name] = dram_addr2 + dram_addr2 += s_obj.header['sh_size'] + + if text_size == 0 and executable: + ro_start = do_align_up(dram_addr2, 0x1000) + else: + ro_start = do_align_up(text_addr + text_size, 0x1000) + for section in readonly: + s_obj = tmp_sec_map.get(section.name, section) + ro_start = do_align_up(ro_start, s_obj.header['sh_addralign']) + addr_map[section.name] = ro_start + ro_start += s_obj.header['sh_size'] + + wr_start = do_align_up(ro_start, 0x1000) + for section in writable: + s_obj = tmp_sec_map.get(section.name, section) + wr_start = do_align_up(wr_start, s_obj.header['sh_addralign']) + addr_map[section.name] = wr_start + wr_start += s_obj.header['sh_size'] + + if addr_map: + out_path = f'{args.file}.tmp' + with open(out_path, 'r+b') as f: + raw = bytearray(f.read()) + e_ident = raw[:16] + little_endian = e_ident[5] == 1 + bits32 = e_ident[4] == 1 + endian = '<' if little_endian else '>' + + if bits32: + e_shoff = struct.unpack_from(f'{endian}I', raw, 0x20)[0] + e_shentsize = struct.unpack_from(f'{endian}H', raw, 0x2e)[0] + e_shnum = struct.unpack_from(f'{endian}H', raw, 0x30)[0] + e_shstrndx = struct.unpack_from(f'{endian}H', raw, 0x32)[0] + sh_addr_offset = 0x0C + sh_name_offset = 0x00 + else: + e_shoff = struct.unpack_from(f'{endian}Q', raw, 0x28)[0] + e_shentsize = struct.unpack_from(f'{endian}H', raw, 0x3a)[0] + e_shnum = struct.unpack_from(f'{endian}H', raw, 0x3c)[0] + e_shstrndx = struct.unpack_from(f'{endian}H', raw, 0x3e)[0] + sh_addr_offset = 0x10 + sh_name_offset = 0x00 + + shstr_shdr_off = e_shoff + e_shstrndx * e_shentsize + if bits32: + shstr_off = struct.unpack_from(f'{endian}I', raw, shstr_shdr_off + 0x10)[0] + else: + shstr_off = struct.unpack_from(f'{endian}Q', raw, shstr_shdr_off + 0x18)[0] + + patched = 0 + for i in range(e_shnum): + shdr_off = e_shoff + i * e_shentsize + sh_name_idx = struct.unpack_from(f'{endian}I', raw, shdr_off + sh_name_offset)[0] + name_end = raw.index(0, shstr_off + sh_name_idx) + s_name = raw[shstr_off + sh_name_idx:name_end].decode('utf-8', errors='replace') + + if s_name in addr_map: + new_addr = addr_map[s_name] + if bits32: + new_addr_u32 = new_addr & 0xFFFFFFFF + struct.pack_into(f'{endian}I', raw, shdr_off + sh_addr_offset, new_addr_u32) + else: + struct.pack_into(f'{endian}Q', raw, shdr_off + sh_addr_offset, new_addr) + patched += 1 + + sect_idx_to_addr = {} + for i2 in range(e_shnum): + shdr_off2 = e_shoff + i2 * e_shentsize + sh_name_idx2 = struct.unpack_from(f'{endian}I', raw, shdr_off2 + sh_name_offset)[0] + name_end2 = raw.index(0, shstr_off + sh_name_idx2) + s_name2 = raw[shstr_off + sh_name_idx2:name_end2].decode('utf-8', errors='replace') + if s_name2 in addr_map: + sect_idx_to_addr[i2] = addr_map[s_name2] + + sym_patched = 0 + for i in range(e_shnum): + shdr_off = e_shoff + i * e_shentsize + sh_type_val = struct.unpack_from(f'{endian}I', raw, shdr_off + 0x04)[0] + if sh_type_val != 2: + continue + sym_sh_offset = struct.unpack_from(f'{endian}I', raw, shdr_off + 0x10)[0] + sym_sh_size = struct.unpack_from(f'{endian}I', raw, shdr_off + 0x14)[0] + if bits32: + sym_entsize = struct.unpack_from(f'{endian}I', raw, shdr_off + 0x24)[0] + else: + sym_entsize = struct.unpack_from(f'{endian}Q', raw, shdr_off + 0x38)[0] + if sym_entsize == 0: + continue + for j in range(sym_sh_size // sym_entsize): + sym_off = sym_sh_offset + j * sym_entsize + if bits32: + st_info = raw[sym_off + 0x0C] + st_shndx = struct.unpack_from(f'{endian}H', raw, sym_off + 0x0E)[0] + st_value_off = sym_off + 0x04 + else: + st_info = raw[sym_off + 0x04] + st_shndx = struct.unpack_from(f'{endian}H', raw, sym_off + 0x06)[0] + st_value_off = sym_off + 0x08 + sym_type = st_info & 0xF + if sym_type == 3 and st_shndx in sect_idx_to_addr: + new_val = sect_idx_to_addr[st_shndx] & 0xFFFFFFFF + if bits32: + struct.pack_into(f'{endian}I', raw, st_value_off, new_val) + else: + struct.pack_into(f'{endian}Q', raw, st_value_off, sect_idx_to_addr[st_shndx]) + sym_patched += 1 + + f.seek(0) + f.write(raw) + print(f'llext_link_helper: patched sh_addr for {patched} sections and st_value for {sym_patched} STT_SECTION symbols') + copy_command = [args.copy] if first_dram_text: @@ -223,5 +328,35 @@ def main(): copy_command.extend([f'{args.file}.tmp', f'{args.output}']) subprocess.run(copy_command) + if f_size_file: + # Calculate new size based on the output file + start = 0xffffffff + min_start = 0x08000000 + end = 0 + with open(f'{args.output}', 'rb') as f_elf: + out_elf = ELFFile(f_elf) + for section in out_elf.iter_sections(): + s_flags = section.header['sh_flags'] + if not s_flags & SH_FLAGS.SHF_ALLOC: + continue + if section.header['sh_addr'] < min_start: + continue + if section.header['sh_addr'] < start: + start = section.header['sh_addr'] + if section.header['sh_addr'] + section.header['sh_size'] > end: + end = section.header['sh_addr'] + section.header['sh_size'] + if start < 0xffffffff: + elf_size = end - start + else: + elf_size = 0 + size += elf_size + 0xfff + size &= ~0xfff + f_size_file.seek(0) + f_size_file.truncate() + f_size_file.write(f'0x{size:x}\n') + f_size_file.flush() + fcntl.flock(f_size_file, fcntl.LOCK_UN) + f_size_file.close() + if __name__ == "__main__": main() diff --git a/scripts/llext_offset_calc.py b/scripts/llext_offset_calc.py index 0f302a8cbe12..4b5983c31166 100755 --- a/scripts/llext_offset_calc.py +++ b/scripts/llext_offset_calc.py @@ -39,7 +39,7 @@ def get_elf_size(elf_name): # Ignore detached sections, to be used in DRAM, their addresses # are below min_start - if section.header['sh_addr'] < min_start: + if section.header['sh_addr'] > 0 and section.header['sh_addr'] < min_start: continue if section.header['sh_addr'] < start: diff --git a/scripts/sof-qemu-run.py b/scripts/sof-qemu-run.py index fc985e254d51..4fc40ac439e7 100755 --- a/scripts/sof-qemu-run.py +++ b/scripts/sof-qemu-run.py @@ -16,6 +16,8 @@ import subprocess import argparse import os +import pathlib +import shutil import re @@ -76,22 +78,72 @@ def main(): parser = argparse.ArgumentParser(description="Run QEMU via west and automatically decode crashes.") parser.add_argument("--build-dir", default="build", help="Path to the build directory containing zephyr.elf, linker.cmd, etc. Defaults to 'build'.") parser.add_argument("--log-file", default="qemu-run.log", help="Path to save the QEMU output log. Defaults to 'qemu-run.log'.") + parser.add_argument("--valgrind", action="store_true", help="Run the executable under Valgrind (only valid for native_sim).") args = parser.parse_args() - # Make absolute path just in case + # Resolve to an absolute path; args.build_dir may be passed relative to + # the caller's working directory. build_dir = os.path.abspath(args.build_dir) print(f"Starting QEMU test runner. Monitoring for crashes (Build Dir: {args.build_dir})...") # We will use pexpect to spawn the west command to get PTY features - import shutil west_path = shutil.which("west") if not west_path: print("[sof-qemu-run] Error: 'west' command not found in PATH.") print("Please ensure you have sourced the Zephyr environment (e.g., source zephyr-env.sh).") sys.exit(1) - child = pexpect.spawn(west_path, ["-v", "build", "-t", "run"], encoding='utf-8') + # Detect the board configuration using zcmake (same approach as xtensa-build-zephyr.py) + is_native_sim = False + try: + # zcmake lives in the Zephyr tree, so its location is added to sys.path + # at runtime (same approach as xtensa-build-zephyr.py) and the import is + # kept local and guarded so a missing Zephyr tree degrades gracefully. + sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), + '..', '..', 'zephyr', 'scripts', 'west_commands')) + import zcmake + cmake_cache = zcmake.CMakeCache.from_build_dir(pathlib.Path(build_dir)) + board = cmake_cache.get("CACHED_BOARD") or cmake_cache.get("BOARD") or "" + if "native_sim" in board: + is_native_sim = True + except Exception: + pass + + # Determine execution command + # If the user is running the python script directly from outside the workspace, we need to provide the source directory. + # But if west finds it automatically (or we are in the build dir), providing `-s` might clear the CACHED_BOARD config. + run_cmd = [west_path, "-v", "build", "-d", build_dir] + + # Check if we are physically sitting inside the build directory + if os.path.abspath(".") != os.path.abspath(build_dir): + # We need to explicitly supply the app source to prevent west from crashing + app_source_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "app")) + run_cmd.extend(["-s", app_source_dir]) + + run_cmd.extend(["-t", "run"]) + + if args.valgrind: + if not is_native_sim: + print("[sof-qemu-run] Error: --valgrind is only supported for the native_sim board.") + sys.exit(1) + + print("[sof-qemu-run] Rebuilding before valgrind...") + subprocess.run([west_path, "build", "-d", build_dir], check=True) + + valgrind_path = shutil.which("valgrind") + if not valgrind_path: + print("[sof-qemu-run] Error: 'valgrind' command not found in PATH.") + sys.exit(1) + + exe_path = os.path.join(build_dir, "zephyr", "zephyr.exe") + if not os.path.isfile(exe_path): + print(f"[sof-qemu-run] Error: Executable not found at {exe_path}") + sys.exit(1) + + run_cmd = [valgrind_path, exe_path] + + child = pexpect.spawn(run_cmd[0], run_cmd[1:], encoding='utf-8') # We will accumulate output to check for crashes full_output = "" @@ -157,36 +209,39 @@ def main(): run_sof_crash_decode(build_dir, full_output) else: - print("\n[sof-qemu-run] No crash detected. Interacting with QEMU Monitor to grab registers...") - - # We need to send Ctrl-A c to enter the monitor - if child.isalive(): - child.send("\x01c") # Ctrl-A c - try: - # Wait for (qemu) prompt - child.expect(r"\(qemu\)", timeout=5) - # Send "info registers" - child.sendline("info registers") - # Wait for the next prompt - child.expect(r"\(qemu\)", timeout=5) - - info_regs_output = child.before - print("\n[sof-qemu-run] Successfully extracted registers from QEMU monitor.\n") - - # Quit qemu safely - child.sendline("quit") - child.expect(pexpect.EOF, timeout=2) - child.close() - - # Run the decoder on the intercepted register output - run_sof_crash_decode(build_dir, info_regs_output) - except pexpect.TIMEOUT: - print("\n[sof-qemu-run] Timed out waiting for QEMU monitor. Is it running?") - child.close(force=True) - except pexpect.EOF: - print("\n[sof-qemu-run] QEMU terminated before we could run monitor commands.") + if is_native_sim: + print("\n[sof-qemu-run] No crash detected. (Skipping QEMU monitor interaction for native_sim)") else: - print("\n[sof-qemu-run] Process is no longer alive, cannot extract registers.") + print("\n[sof-qemu-run] No crash detected. Interacting with QEMU Monitor to grab registers...") + + # We need to send Ctrl-A c to enter the monitor + if child.isalive(): + child.send("\x01c") # Ctrl-A c + try: + # Wait for (qemu) prompt + child.expect(r"\(qemu\)", timeout=5) + # Send "info registers" + child.sendline("info registers") + # Wait for the next prompt + child.expect(r"\(qemu\)", timeout=5) + + info_regs_output = child.before + print("\n[sof-qemu-run] Successfully extracted registers from QEMU monitor.\n") + + # Quit qemu safely + child.sendline("quit") + child.expect(pexpect.EOF, timeout=2) + child.close() + + # Run the decoder on the intercepted register output + run_sof_crash_decode(build_dir, info_regs_output) + except pexpect.TIMEOUT: + print("\n[sof-qemu-run] Timed out waiting for QEMU monitor. Is it running?") + child.close(force=True) + except pexpect.EOF: + print("\n[sof-qemu-run] QEMU terminated before we could run monitor commands.") + else: + print("\n[sof-qemu-run] Process is no longer alive, cannot extract registers.") if __name__ == "__main__": main() diff --git a/scripts/sof-qemu-run.sh b/scripts/sof-qemu-run.sh index e1ece1dd5125..ef915fbb1318 100755 --- a/scripts/sof-qemu-run.sh +++ b/scripts/sof-qemu-run.sh @@ -2,17 +2,54 @@ # SPDX-License-Identifier: BSD-3-Clause # Copyright(c) 2026 Intel Corporation. All rights reserved. -# Define the build directory from the first argument (or default) -BUILD_DIR="${1:-build}" +# Get the directory of this script +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +SOF_WORKSPACE="$(dirname "$(dirname "$SCRIPT_DIR")")" + +TARGET="native_sim" +VALGRIND_ARG="" + +usage() { + cat <' relative + to the workspace. (default: native_sim) +EOF + exit 0 +} + +while [[ $# -gt 0 ]]; do + case $1 in + -h|--help) + usage + ;; + --valgrind) + VALGRIND_ARG="--valgrind" + ;; + *) + # Allow users who pass the directory name directly out of habit to still work + if [[ "$1" == build-* ]]; then + TARGET="${1#build-}" + else + TARGET="$1" + fi + ;; + esac + shift +done + +BUILD_DIR="${SOF_WORKSPACE}/build-${TARGET}" # Find and source the zephyr environment script, typically via the sof-venv wrapper # or directly if running in a known zephyrproject layout. # We will use the existing helper sof-venv.sh to get the right environment. -# Get the directory of this script -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -SOF_WORKSPACE="$(dirname "$(dirname "$SCRIPT_DIR")")" - # Use the SOF workspace to locate the virtual environment VENV_DIR="$SOF_WORKSPACE/.venv" echo "Using SOF environment at $SOF_WORKSPACE" @@ -20,9 +57,5 @@ echo "Using SOF environment at $SOF_WORKSPACE" # start the virtual environment source ${VENV_DIR}/bin/activate -# Execute the QEMU runner from within the correct build directory -cd "${BUILD_DIR}" || exit 1 - # Finally run the python script which will now correctly inherit 'west' from the sourced environment. -python3 "${SCRIPT_DIR}/sof-qemu-run.py" --build-dir "${BUILD_DIR}" - +python3 "${SCRIPT_DIR}/sof-qemu-run.py" --build-dir "${BUILD_DIR}" $VALGRIND_ARG diff --git a/scripts/xtensa-build-zephyr.py b/scripts/xtensa-build-zephyr.py index a3f6581999b9..8db3d80c95ed 100755 --- a/scripts/xtensa-build-zephyr.py +++ b/scripts/xtensa-build-zephyr.py @@ -121,8 +121,7 @@ class PlatformConfig: "amd", "acp_7_x_adsp/acp_7_x", f"RI-2022.9{xtensa_tools_version_postfix}", "ACP73x_HiFi5_NNE_PROD", - RIMAGE_KEY = "key param ignored by acp_7_x", - aliases = ["acp7x"] + RIMAGE_KEY = "key param ignored by acp_7_x" ), # MediaTek platforms # (move to platform_configs_all on next Zephyr SDK release after 0.17.0) @@ -229,10 +228,6 @@ class PlatformConfig: "hifi4_mscale_v2_0_2_prod", RIMAGE_KEY = "key param ignored by imx8m" ), - "imx8m_cm7" : PlatformConfig( - "imx", "imx8mp_evk/mimx8ml8/m7/ddr", - "", "", "", "" - ), "imx8ulp" : PlatformConfig( "imx", "imx8ulp_evk/mimx8ud7/adsp", f"RI-2023.11{xtensa_tools_version_postfix}", @@ -251,6 +246,10 @@ class PlatformConfig: "zephyr", "qemu_xtensa/dc233c/mmu", "", "", "zephyr" ), + "native_sim" : PlatformConfig( + "zephyr", "native_sim", + "", "", "zephyr" + ), } platform_configs = platform_configs_all.copy() @@ -429,6 +428,19 @@ def parse_args(): help="Build menuconfig for target") parser.add_argument("-z", "--zephyrsdk", required=False, action="store_true", help="Force Build using Zephyr SDK for target") + parser.add_argument("--llvm-clang", required=False, type=pathlib.Path, default=None, + metavar="LLVM_BUILD_DIR", + help="""Use a custom LLVM/Clang build for C/ASM compilation instead of +xt-clang or GCC. Specify the LLVM build directory containing bin/clang. +A wrapper script is auto-generated to handle GCC flag translation, +external assembler usage, and Xtensa target configuration. +Example: --llvm-clang /home/user/llvm-project/build""") + parser.add_argument("--llvm-no-sdk", required=False, action="store_true", default=False, + help="""Use LLVM/Clang with no Zephyr SDK support. +This enables the integrated assembler and pure LLD linking, with zero dependency on the Zephyr SDK.""") + parser.add_argument("--build-dir-suffix", default="", + help="Suffix to append to the build directory name (e.g. '-llvm' or '-gcc').\n" + "This is useful for keeping side-by-side build environments.") args = parser.parse_args() @@ -822,6 +834,168 @@ def rimage_options(platform_dict): return opts +def generate_clang_wrapper(llvm_build_dir, platform, plat_config, top_dir): + """Generate a clang wrapper script that translates GCC-only flags and + configures the external assembler for Xtensa targets. + + The Zephyr build system passes several GCC-specific flags that clang + does not support. This wrapper filters those out, ensures the external + GCC assembler is used (clang's integrated asm doesn't support the full + Xtensa ISA), and adds the necessary -B/-I paths. + + Returns the path to the generated wrapper script. + """ + llvm_build_dir = pathlib.Path(llvm_build_dir).resolve() + clang_bin = llvm_build_dir / "bin" / "clang" + if not clang_bin.exists(): + raise FileNotFoundError(f"Clang not found at {clang_bin}") + + # Derive the Zephyr SDK cross-compiler name from the board config + # e.g. "intel_adsp/ace30/ptl" -> "xtensa-intel_ace30_ptl_zephyr-elf" + # The Zephyr SDK naming uses the SoC name with underscores + board_parts = plat_config.replace("/", "_") + # Try to find the actual SDK cross-compiler directory + sdk_dir = os.environ.get("ZEPHYR_SDK_INSTALL_DIR", "") + cross_prefix = f"xtensa-{platform_configs[platform].XTENSA_CORE.split('_')[0]}" + + # Find the GNU toolchain directory in the Zephyr SDK + gnu_dir = None + if sdk_dir: + sdk_path = pathlib.Path(sdk_dir) + # Look for the xtensa-*_zephyr-elf directory + for d in sdk_path.glob("gnu/xtensa-*_zephyr-elf"): + # Match based on platform name in the directory + if platform in str(d) or board_parts.split("_")[2] in str(d): + gnu_dir = d + break + # Fallback: try exact board-based name + if gnu_dir is None: + # Convert board config to SDK name + # intel_adsp/ace30/ptl -> intel_ace30_ptl + parts = plat_config.split("/") + if len(parts) >= 3: + sdk_name = f"{parts[1]}_{parts[2]}" + else: + sdk_name = parts[-1] + for d in sdk_path.glob(f"gnu/xtensa-*{sdk_name}*"): + gnu_dir = d + break + + # Find the HAL SoC directory for core-isa.h + hal_soc_dir = None + modules_hal = top_dir / "modules" / "hal" / "xtensa" + if modules_hal.exists(): + parts = plat_config.split("/") + if len(parts) >= 3: + soc_name = f"{parts[0].replace('intel_adsp', 'intel')}_{parts[1]}_{parts[2]}" + else: + soc_name = parts[-1] + for d in modules_hal.glob(f"zephyr/soc/*{parts[1]}*{parts[-1]}*"): + hal_soc_dir = d + break + # Fallback: try matching just the last part + if hal_soc_dir is None: + for d in modules_hal.glob(f"zephyr/soc/*{parts[-1]}*"): + hal_soc_dir = d + break + + # Build the wrapper script + wrapper_dir = llvm_build_dir / "bin" + wrapper_path = wrapper_dir / f"clang-sof-wrapper-{platform}.sh" + + # Determine target triple from GNU dir name + target_triple = "" + if gnu_dir: + # e.g. "xtensa-intel_ace30_ptl_zephyr-elf" from the dir name + target_triple = gnu_dir.name + + # GCC compatibility queries must come before arg processing + gcc_compat = "" + if gnu_dir: + gcc_bin = gnu_dir / "bin" / f"{target_triple}-gcc" + gcc_compat = f""" +# GCC compatibility: forward GCC-specific queries to the real GCC. +# Zephyr's cmake toolchain detection calls -dumpfullversion, +# --print-file-name, --print-multi-directory etc. which clang doesn't support. +case "$1" in + -dumpversion|-dumpfullversion|-dumpmachine|\ + --print-file-name=*|--print-multi-directory|--print-multi-lib|\ + --print-libgcc-file-name|--print-search-dirs|-print-search-dirs|\ + -print-multi-directory) + exec {gcc_bin} "$@" + ;; +esac +""" + + wrapper_content = f"""#!/bin/bash +# Auto-generated clang wrapper for SOF/{platform} builds +# Generated by xtensa-build-zephyr.py --llvm-clang +# Clang: {clang_bin} +# Target: {target_triple} +{gcc_compat} +ARGS=() +HAS_NO_INTEGRATED_AS=0 +HAS_TARGET=0 +IS_ASM=0 +for arg in "$@"; do + case "$arg" in + -fno-reorder-functions) ;; # GCC only + -fno-defer-pop) ;; # GCC only + --param=*) ;; # GCC only + -fstrict-overflow) ;; # deprecated GCC flag + -mlongcalls) ARGS+=("-mlong-calls") ;; # translate + -fno-integrated-as) HAS_NO_INTEGRATED_AS=1; ARGS+=("$arg") ;; + --target=*) HAS_TARGET=1; ARGS+=("$arg") ;; + *.s|*.S) IS_ASM=1; ARGS+=("$arg") ;; + *) ARGS+=("$arg") ;; + esac +done + +# Use external assembler only for hand-written assembly files +if [ "$HAS_NO_INTEGRATED_AS" -eq 0 ] && [ "$IS_ASM" -eq 1 ]; then + ARGS+=("-fno-integrated-as") +fi +""" + + if target_triple: + wrapper_content += f""" +# Set Xtensa target if not already specified +if [ "$HAS_TARGET" -eq 0 ]; then + ARGS+=("--target={target_triple}") +fi +""" + + if hal_soc_dir: + core_isa_dir = hal_soc_dir / "xtensa" / "config" + if core_isa_dir.exists(): + wrapper_content += f""" +# core-isa.h lives in the SoC-specific HAL overlay, not alongside core.h +ARGS+=("-I{core_isa_dir}") +""" + + if gnu_dir: + sysroot_bin = gnu_dir / target_triple / "bin" + gnu_bin = gnu_dir / "bin" + wrapper_content += f""" +# Paths so clang can find the GCC external assembler and linker +ARGS+=("-B{sysroot_bin}") +ARGS+=("-B{gnu_bin}") +""" + + wrapper_content += f""" +# Enable FLIX VLIW instruction bundling +ARGS+=("-Xclang" "-target-feature" "-Xclang" "+flix") + +exec {clang_bin} "${{ARGS[@]}}" +""" + + with open(wrapper_path, "w") as f: + f.write(wrapper_content) + os.chmod(wrapper_path, 0o755) + print(f"Generated clang wrapper: {wrapper_path}") + return str(wrapper_path) + + STAGING_DIR = None def build_platforms(): global west_top, SOF_TOP @@ -871,7 +1045,16 @@ def build_platforms(): _dict = dataclasses.asdict(platform_configs[platform]) platform_dict = { k:v for (k,v) in _dict.items() if _dict[k] is not None } - if args.zephyrsdk: + if args.llvm_clang: + print(f"Using LLVM/Clang from {args.llvm_clang}") + xtensa_tools_root_dir = None + # Use llvm toolchain variant for native Clang support + platf_build_environ["ZEPHYR_TOOLCHAIN_VARIANT"] = "llvm" + platf_build_environ["LLVM_TOOLCHAIN_PATH"] = str(pathlib.Path(args.llvm_clang).resolve()) + print("LLVM: Zephyr SDK support is disabled (native Clang only)") + if "ZEPHYR_SDK_INSTALL_DIR" in platf_build_environ: + del platf_build_environ["ZEPHYR_SDK_INSTALL_DIR"] + elif args.zephyrsdk: print("Using Zephyr SDK for building") xtensa_tools_root_dir = None else: @@ -896,6 +1079,7 @@ def build_platforms(): # following variables. platf_build_environ["ZEPHYR_TOOLCHAIN_VARIANT"] = platf_build_environ.get("ZEPHYR_TOOLCHAIN_VARIANT", platform_dict["DEFAULT_TOOLCHAIN_VARIANT"]) + platf_build_environ["ZEPHYR_BASE"] = str(pathlib.Path(west_top, "zephyr")) platf_build_environ["XTENSA_TOOLCHAIN_PATH"] = str( xtensa_tools_root_dir / "install" / "tools" ) @@ -915,7 +1099,7 @@ def build_platforms(): XTENSA_SYSTEM = str(builds_toolchainver / platform_dict["XTENSA_CORE"] / "config") platf_build_environ["XTENSA_SYSTEM"] = XTENSA_SYSTEM - platform_build_dir_name = f"build-{platform}" + platform_build_dir_name = f"build-{platform}{args.build_dir_suffix}" PLAT_CONFIG = platform_dict["PLAT_CONFIG"] @@ -937,22 +1121,58 @@ def build_platforms(): if args.cmake_args: build_cmd += args.cmake_args + # When using LLVM/Clang, the llvm toolchain variant handles: + # - Compiler detection via LLVM_TOOLCHAIN_PATH + # - GCC assembler/linker integration via CROSS_COMPILE + # - FLIX VLIW bundling via TOOLCHAIN_C_FLAGS + # - LLEXT shared library link wrapper + # No wrapper script or CMAKE_C_COMPILER override needed. + if args.llvm_clang: + # Set XTENSA_CORE_ID so cmake/toolchain/llvm/target.cmake + # selects the correct -mcpu for the LLVM backend. + # Platforms that share an SDK toolchain (e.g. NVL uses PTL's + # assembler) need an explicit CPU override for HiFi5 support. + platform_vendor = platform_dict["vendor"] + if platform_vendor == "intel": + parts = PLAT_CONFIG.split("/") + if len(parts) >= 3: + core_id = f"{parts[1]}_{parts[2]}" # e.g. "ace40_nvl" -> "intel_ace40_adsp" + # Map board-level names to LLVM processor names + core_id_map = { + "ace30_ptl": "intel_ace30_adsp", + "ace40_nvl": "intel_ace40_adsp", + "ace40_nvls": "intel_ace40_adsp", + } + platf_build_environ["XTENSA_CORE_ID"] = core_id_map.get(core_id, "intel_ace30_adsp") + + # Map board-level names to Zephyr SDK toolchain names + sdk_target_map = { + "ace30_ptl": "intel_ace30_ptl", + "ace40_nvl": "intel_ace40", + "ace40_nvls": "intel_ace40", + } + platf_build_environ["XTENSA_TOOLCHAIN_TARGET"] = sdk_target_map.get(core_id, f"intel_{core_id}") + extra_conf_files = [str(item.resolve(True)) for item in args.overlay] # The '-d' option is a shortcut for '-o path_to_debug_overlay', we are good # if both are provided, because it's no harm to merge the same overlay twice. if args.debug: extra_conf_files.append(str(pathlib.Path(SOF_TOP, "app", "debug_overlay.conf"))) - # The xt-cland Cadence toolchain currently cannot link shared + # The xt-clang Cadence toolchain currently cannot link shared # libraries for Xtensa. Therefore when it's used we switch to - # building relocatable ELF objects. - if platf_build_environ.get("ZEPHYR_TOOLCHAIN_VARIANT") == 'xt-clang': + # building relocatable ELF objects. We do the same for LLVM/Clang + # to match the xt-clang LLEXT format and avoid GOT/PLT overhead. + if platf_build_environ.get("ZEPHYR_TOOLCHAIN_VARIANT") == 'xt-clang' or args.llvm_clang: extra_conf_files.append(str(pathlib.Path(SOF_TOP, "app", "llext_relocatable.conf"))) if extra_conf_files: extra_conf_files = ";".join(extra_conf_files) build_cmd.append(f"-DEXTRA_CONF_FILE={extra_conf_files}") + if args.llvm_no_sdk: + build_cmd.append("-DLLVM_NO_ZEPHYR_SDK=ON") + abs_build_dir = pathlib.Path(west_top, platform_build_dir_name) # Longer story in https://github.com/zephyrproject-rtos/zephyr/pull/56671 @@ -970,15 +1190,6 @@ def build_platforms(): platform_dict, STAGING_DIR / "sof-info" / platform ) - - # SOF marks ELF sections in the range [SOF_MODULE_DRAM_LINK_START, SOF_MODULE_DRAM_LINK_END] - # as "detached". On imx8m_cm7 that address range overlaps with the M7 ITCM, so rimage would - # incorrectly classify ITCM sections as detached. Pass -d to ignore detached classification - if platform == "imx8m_cm7": - existing = platform_wcfg.get("rimage.extra-args") or "" - platform_wcfg.set("rimage.extra-args", - shlex.join(shlex.split(existing) + ["-d"])) - platf_build_environ['WEST_CONFIG_LOCAL'] = str(wcfg_path) # Make sure the build logs don't leave anything hidden @@ -986,22 +1197,6 @@ def build_platforms(): env=platf_build_environ, sof_log_env=True) print() - # Delete the LLEXT VMA accumulator file before every build. - # llext_offset_calc.py uses module_size as a persistent counter - # across cmake custom-command invocations. If ninja is invoked - # more than once without a pristine rebuild this counter keeps - # accumulating, pushing pre-linked VMAs beyond the valid LLEXT - # virtual region and causing sys_mm_drv_map_region() to fail - # silently at runtime (IPC4_MOD_NOT_INITIALIZED error 104). - # llext_offset_calc.py already handles a missing file as size=0, - # so deletion is the simplest and most reliable reset. - zephyr_build_dir = pathlib.Path(abs_build_dir) / "zephyr" - if zephyr_build_dir.is_dir(): - acc_path = zephyr_build_dir / "module_size" - if acc_path.is_file(): - acc_path.unlink() - print(f"Deleted LLEXT VMA accumulator: {acc_path}") - # Build try: execute_command(build_cmd, cwd=west_top, env=platf_build_environ, sof_log_env=True) @@ -1173,7 +1368,7 @@ def install_lib(platform, sof_output_dir, abs_build_dir, platform_wconfig): def install_platform(platform, sof_output_dir, platf_build_environ, platform_wconfig): # Keep in sync with caller - platform_build_dir_name = f"build-{platform}" + platform_build_dir_name = f"build-{platform}{args.build_dir_suffix}" # Install to STAGING_DIR abs_build_dir = pathlib.Path(west_top) / platform_build_dir_name / "zephyr" @@ -1194,8 +1389,8 @@ def install_platform(platform, sof_output_dir, platf_build_environ, platform_wco install_key_dir = install_key_dir / args.key_type_subdir os.makedirs(install_key_dir, exist_ok=True) - # looses file owner and group - file is commonly accessible, dont install qemu. - if platform not in ("qemu_xtensa", "qemu_xtensa_mmu"): + # loses file owner and group - file is commonly accessible, don't install for qemu. + if platform not in ("qemu_xtensa", "qemu_xtensa_mmu", "native_sim"): shutil.copy2(abs_build_dir / "zephyr.ri", install_key_dir / output_fwname) if args.deployable_build and platform_configs[platform].ipc4: @@ -1346,10 +1541,10 @@ def gzip_compress(fname, gzdst=None): # Don't run sof_ri_info and ignore silently .ri files that don't have one. RI_INFO_UNSUPPORTED = [] -RI_INFO_UNSUPPORTED += ['imx8', 'imx8x', 'imx8m', 'imx8m_cm7', 'imx8ulp', 'imx95'] +RI_INFO_UNSUPPORTED += ['imx8', 'imx8x', 'imx8m', 'imx8ulp', 'imx95'] RI_INFO_UNSUPPORTED += ['rn', 'acp_6_0', 'acp_7_0', 'acp_7_x'] RI_INFO_UNSUPPORTED += ['mt8186', 'mt8188', 'mt8195', 'mt8196', 'mt8365'] -RI_INFO_UNSUPPORTED += ['qemu_xtensa', 'qemu_xtensa_mmu'] +RI_INFO_UNSUPPORTED += ['qemu_xtensa', 'qemu_xtensa_mmu', 'native_sim'] # For temporary workarounds. Unlike _UNSUPPORTED above, the platforms below will print a warning. diff --git a/src/audio/CMakeLists.txt b/src/audio/CMakeLists.txt index 92002c8b7c1c..fdfd460ca6aa 100644 --- a/src/audio/CMakeLists.txt +++ b/src/audio/CMakeLists.txt @@ -41,6 +41,9 @@ if(NOT CONFIG_COMP_MODULE_SHARED_LIBRARY_BUILD) if(CONFIG_COMP_DRC) add_subdirectory(drc) endif() + if(CONFIG_COMP_FFMPEG_DEC) + add_subdirectory(ffmpeg_dec) + endif() if(CONFIG_COMP_FIR) add_subdirectory(eq_fir) endif() @@ -107,6 +110,18 @@ if(NOT CONFIG_COMP_MODULE_SHARED_LIBRARY_BUILD) if(CONFIG_COMP_VOLUME) add_subdirectory(volume) endif() + if(CONFIG_COMP_WEBRTC_VAD) + add_subdirectory(webrtc_vad) + endif() + if(CONFIG_COMP_WEBRTC_NS) + add_subdirectory(webrtc_ns) + endif() + if(CONFIG_COMP_WEBRTC_AEC) + add_subdirectory(webrtc_aec) + endif() + if(CONFIG_COMP_WEBRTC_NS2) + add_subdirectory(webrtc_ns2) + endif() if(CONFIG_DTS_CODEC) add_subdirectory(codec) endif() diff --git a/src/audio/Kconfig b/src/audio/Kconfig index 8accb25738a2..511568858384 100644 --- a/src/audio/Kconfig +++ b/src/audio/Kconfig @@ -139,6 +139,7 @@ rsource "dcblock/Kconfig" rsource "drc/Kconfig" rsource "eq_fir/Kconfig" rsource "eq_iir/Kconfig" +rsource "ffmpeg_dec/Kconfig" rsource "google/Kconfig" rsource "igo_nr/Kconfig" rsource "mfcc/Kconfig" @@ -161,6 +162,10 @@ rsource "tensorflow/Kconfig" rsource "tone/Kconfig" rsource "up_down_mixer/Kconfig" rsource "volume/Kconfig" +rsource "webrtc_vad/Kconfig" +rsource "webrtc_ns/Kconfig" +rsource "webrtc_aec/Kconfig" +rsource "webrtc_ns2/Kconfig" # --- End Kconfig Sources (alphabetical order) --- rsource "level_multiplier/Kconfig" diff --git a/src/audio/aria/aria_hifi3.c b/src/audio/aria/aria_hifi3.c index 21fe7e82fffe..5948ee996cc5 100644 --- a/src/audio/aria/aria_hifi3.c +++ b/src/audio/aria/aria_hifi3.c @@ -5,7 +5,13 @@ #include "aria.h" #if SOF_USE_HIFI(3, ARIA) || SOF_USE_HIFI(4, ARIA) -#include +#if defined(__has_include) +# if __has_include() +# include +# endif +#else +# include +#endif #include extern const int32_t sof_aria_index_tab[]; diff --git a/src/audio/aria/aria_hifi5.c b/src/audio/aria/aria_hifi5.c index ebe044ab8a48..e15d1e46336a 100644 --- a/src/audio/aria/aria_hifi5.c +++ b/src/audio/aria/aria_hifi5.c @@ -5,7 +5,13 @@ #include "aria.h" #if SOF_USE_HIFI(5, ARIA) -#include +#if defined(__has_include) +# if __has_include() +# include +# endif +#else +# include +#endif #include extern const int32_t sof_aria_index_tab[]; diff --git a/src/audio/base_fw.c b/src/audio/base_fw.c index b03b97ad998e..723795557838 100644 --- a/src/audio/base_fw.c +++ b/src/audio/base_fw.c @@ -104,6 +104,18 @@ static void get_codec_info(struct sof_tlv **tuple) codec_info.items[codec_info.count++] = SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_BESPOKE, SOF_IPC_STREAM_CAPTURE); #endif +#ifdef CONFIG_FFMPEG_DEC_FLAC + /* FLAC decode is provided by the ffmpeg_dec (libavcodec) module. */ + codec_info.items[codec_info.count++] = + SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_FLAC, SOF_IPC_STREAM_PLAYBACK); +#endif +#if defined(CONFIG_FFMPEG_DEC_MP3) && !defined(CONFIG_CADENCE_CODEC_MP3_DEC) + /* MP3 decode is provided by the ffmpeg_dec (libavcodec) module (guarded so + * it is not advertised twice when the Cadence MP3 decoder is also present). + */ + codec_info.items[codec_info.count++] = + SET_CODEC_INFO_ITEM(SND_AUDIOCODEC_MP3, SOF_IPC_STREAM_PLAYBACK); +#endif if (!codec_info.count) return; diff --git a/src/audio/copier/copier_host.c b/src/audio/copier/copier_host.c index 3e704b2b1163..56128d64c599 100644 --- a/src/audio/copier/copier_host.c +++ b/src/audio/copier/copier_host.c @@ -194,10 +194,12 @@ __cold int copier_host_create(struct processing_module *mod, } #if CONFIG_HOST_DMA_STREAM_SYNCHRONIZATION /* Size of a configuration without optional parameters. */ + const uint32_t extra_cfg_len = copier_cfg->gtw_cfg.config_length; const uint32_t basic_size = sizeof(*copier_cfg) + - (copier_cfg->gtw_cfg.config_length - 1) * sizeof(uint32_t); + (extra_cfg_len > 0 ? (extra_cfg_len - 1) * sizeof(uint32_t) : 0); /* Additional data size */ - const uint32_t tlv_buff_size = config->ipc_config_size - basic_size; + const uint32_t tlv_buff_size = config->ipc_config_size > basic_size ? + config->ipc_config_size - basic_size : 0; const uint32_t min_tlv_size = sizeof(struct ipc4_copier_sync_group) + 2 * sizeof(uint32_t); if (tlv_buff_size >= min_tlv_size) { diff --git a/src/audio/drc/drc.c b/src/audio/drc/drc.c index 7fe86ce01c56..a727b68d55fd 100644 --- a/src/audio/drc/drc.c +++ b/src/audio/drc/drc.c @@ -139,6 +139,20 @@ static int drc_setup(struct processing_module *mod, uint16_t channels, uint32_t return drc_set_pre_delay_time(&cd->state, cd->config->params.pre_delay_time, rate); } +static int drc_validator(struct comp_dev *dev, void *new_data, uint32_t new_data_size) +{ + struct sof_drc_config *config = new_data; + + if (new_data_size != sizeof(struct sof_drc_config) || + new_data_size != config->size) { + comp_err(dev, "invalid configuration blob, size %u, expected %zu", + new_data_size, sizeof(struct sof_drc_config)); + return -EINVAL; + } + + return 0; +} + /* * End of DRC setup code. Next the standard component methods. */ @@ -168,6 +182,11 @@ __cold static int drc_init(struct processing_module *mod) goto cd_fail; } + /* Reject malformed blobs at IPC time so a bad run-time update cannot + * replace the working configuration. + */ + comp_data_blob_set_validator(cd->model_handler, drc_validator); + drc_reset_state(mod, &cd->state); /* Initialize DRC to enabled. If defined by topology, a control may set @@ -326,7 +345,6 @@ static int drc_prepare(struct processing_module *mod, struct drc_comp_data *cd = module_get_private_data(mod); struct comp_buffer *sourceb, *sinkb; struct comp_dev *dev = mod->dev; - size_t data_size; int channels; int rate; int ret; @@ -352,11 +370,9 @@ static int drc_prepare(struct processing_module *mod, /* Initialize DRC */ comp_info(dev, "source_format=%d", cd->source_format); - cd->config = comp_get_data_blob(cd->model_handler, &data_size, NULL); - /* the blob is dereferenced as a struct sof_drc_config below and in - * drc_setup(), so require it to be at least that large - */ - if (cd->config && data_size >= sizeof(struct sof_drc_config)) { + cd->config = comp_get_data_blob(cd->model_handler, NULL, NULL); + + if (cd->config) { ret = drc_setup(mod, channels, rate); if (ret < 0) { comp_err(dev, "error: drc_setup failed."); diff --git a/src/audio/eq_fir/eq_fir_hifi2ep.c b/src/audio/eq_fir/eq_fir_hifi2ep.c index 71b40edcee32..042be00e6090 100644 --- a/src/audio/eq_fir/eq_fir_hifi2ep.c +++ b/src/audio/eq_fir/eq_fir_hifi2ep.c @@ -13,7 +13,13 @@ #include #include #include -#include +#if defined(__has_include) +# if __has_include() +# include +# endif +#else +# include +#endif #include #include #include diff --git a/src/audio/eq_fir/eq_fir_hifi3.c b/src/audio/eq_fir/eq_fir_hifi3.c index 0d25851ea447..d95fb0534cd4 100644 --- a/src/audio/eq_fir/eq_fir_hifi3.c +++ b/src/audio/eq_fir/eq_fir_hifi3.c @@ -12,7 +12,13 @@ #include #include #include -#include +#if defined(__has_include) +# if __has_include() +# include +# endif +#else +# include +#endif #include #include #include diff --git a/src/audio/ffmpeg_dec/CMakeLists.txt b/src/audio/ffmpeg_dec/CMakeLists.txt new file mode 100644 index 000000000000..c7c00fe7dbf3 --- /dev/null +++ b/src/audio/ffmpeg_dec/CMakeLists.txt @@ -0,0 +1,54 @@ +# SPDX-License-Identifier: BSD-3-Clause + +if(CONFIG_COMP_FFMPEG_DEC STREQUAL "m" AND DEFINED CONFIG_LLEXT) + add_subdirectory(llext ${PROJECT_BINARY_DIR}/ffmpeg_dec_llext) + add_dependencies(app ffmpeg_dec) +else() + add_local_sources(sof ffmpeg_dec.c) + + if(CONFIG_COMP_FFMPEG_DEC_STUB) + add_local_sources(sof ffmpeg_dec-stub.c) + else() + add_local_sources(sof ffmpeg_dec-ffmpeg.c) + # NOTE: ffmpeg_dec-shims.c / ffmpeg_dec-alloc.c are LLEXT-only (see + # llext/CMakeLists.txt). They provide a full private libc (malloc, str*, + # snprintf, ...) the isolated LLEXT cannot import; in a built-in (=y) image + # Zephyr's common libc already provides those (whole-archived), so including + # them here would multiply-define malloc/str*/snprintf. Instead + # ffmpeg_dec-builtin-libc.c supplies ONLY the handful of libc symbols common + # libc does NOT export that libav* still references (stderr/fread/getenv/ + # sscanf/..., z_errno_wrap, and the __errno bridge the newlib math objects + # linked below use on error paths) plus a no-op ffmpeg_dec_libc_bind (=y + # allocates from common-libc malloc, no module-heap binding needed). Double + # libm itself is real newlib math, not a stub -- see FFMPEG_TARGET_LIBM below. + add_local_sources(sof ffmpeg_dec-builtin-libc.c) + + # Built-in (=y) real backend: cross-build the FFmpeg archive via ffmpeg.cmake + # and statically link it into the base firmware. Mirrors the google_rtc =y + # pattern (src/audio/google/CMakeLists.txt): headers via + # zephyr_include_directories, archive via the SOF interface library which + # propagates to zephyr.elf. LLD resolves all relocations at link time, so + # this avoids the LLEXT runtime-relocation path entirely. + is_zephyr(zephyr) + if(zephyr) + include(${CMAKE_CURRENT_LIST_DIR}/ffmpeg.cmake) + zephyr_include_directories(${FFMPEG_INSTALL_DIR}/include) + target_link_directories(SOF INTERFACE ${FFMPEG_INSTALL_DIR}/lib) + # avcodec must precede the libs it depends on (swresample/avutil), and the + # newlib math archive (FFMPEG_TARGET_LIBM) must come LAST so the linker + # resolves libav*'s double libm references (sin/cos/exp/log/pow/...) against + # it -- as an archive it only pulls the math objects actually referenced. + # Do NOT add -lc: the base + # image links bare-metal (-nostdlib) and the remaining libc symbols libav* + # needs resolve from Zephyr's minimal libc already in the link (plus the + # stubs in ffmpeg_dec-builtin-libc.c). FLAC is integer-only so it never + # calls libm at runtime, but the references still resolve to real kernels + # now, and lossy decoders (AAC/Opus/Vorbis) need them to decode correctly. + target_link_libraries(SOF INTERFACE avcodec swresample avutil ${FFMPEG_TARGET_LIBM}) + # Both archives must be built before modules_sof links against them: + # ffmpeg_ext (libav*) and newlib_m (libnewlib_m.a, the double math compiled + # from vendored newlib source with -mlongcalls -- see ffmpeg.cmake 3c). + add_dependencies(modules_sof ffmpeg_ext newlib_m) + endif() + endif() +endif() diff --git a/src/audio/ffmpeg_dec/HIFI.md b/src/audio/ffmpeg_dec/HIFI.md new file mode 100644 index 000000000000..8e2a19becf53 --- /dev/null +++ b/src/audio/ffmpeg_dec/HIFI.md @@ -0,0 +1,225 @@ +# HiFi intrinsics opportunities in ffmpeg_dec + +Analysis of the hot audio-processing paths in the FFmpeg SOF module (decode / +filter / encode) and where Xtensa **HiFi** SIMD intrinsics would give the biggest +wins. It covers both the code this module owns and the FFmpeg code it links. + +## Background: why this matters on Xtensa + +- The FFmpeg archive is cross-built with **`--disable-asm`** (see `ffmpeg.cmake`), + because FFmpeg has no Xtensa assembly. Every DSP kernel therefore runs as + **scalar C** on the DSP — no SIMD, no fused multiply-add vectorisation. +- FFmpeg dispatches its DSP through function-pointer contexts with per-architecture + init hooks (`ff_*_init_aarch64/arm/x86/riscv`) — but **no `_xtensa`** variant. The + generic `_c` kernels are what execute on our target. +- SOF already ships HiFi-optimised DSP and the toolchain for it. Use it as the + template: + - ISA select + intrinsics header (`src/math/exp_fcn_hifi.c`): + ```c + #if XCHAL_HAVE_HIFI5 + #include + #elif XCHAL_HAVE_HIFI4 + #include + #else + #include + #endif + ``` + - Existing HiFi kernels to mirror: `src/math/fir_hifi{2ep,3,5}.c`, + `src/math/iir_df1_hifi{3,4,5}.c`, `src/math/fft/fft_{16,32}_hifi3.c`, + `src/math/exp_fcn_hifi.c`. `ace30` (ptl) is HiFi4-class. + +There are two distinct bodies of code: + +- **(A) Module glue we own** — the PCM format-conversion loops in + `ffmpeg_dec-*.c`. Small, per-sample, easy to hand-vectorise with HiFi, and we + can change them freely. +- **(B) FFmpeg DSP** — the actual codec compute (MDCT/FFT, LPC, windowing). Much + higher cycle cost, but upstream; adding HiFi means either FFmpeg fork patches + (an `_xtensa` DSP init, mirroring the other arch dirs) or routing to SOF's own + HiFi kernels. + +## Hot-path summary + +| # | Path | File | Kind | Now | HiFi win | Effort | +|---|------|------|------|-----|----------|--------| +| 1 | MDCT/FFT (AAC/MP3/Opus/afftdn) | FFmpeg `libavutil/tx*` | float/fixed transform | scalar C | **very high** | high | +| 2 | float vector kernels (fmul/window/overlap-add) | FFmpeg `libavutil/float_dsp` | float SIMD | scalar C | **high** | low–med | +| 3 | FLAC LPC / residual | FFmpeg `libavcodec/flacdsp` | int32/64 MAC | scalar C | high | med | +| 4 | MP3 synthesis window / imdct36 | FFmpeg `libavcodec/mpegaudiodsp` | float/fixed | scalar C | high | med | +| 5 | AAC PS / SBR DSP | FFmpeg `aacpsdsp`/`sbrdsp` | float SIMD | scalar C | med (HE-AAC only) | med | +| 6 | S32↔float PCM convert (filter) | `ffmpeg_dec-filter.c:230,255` | int↔float + scale | scalar C | med | **low** | +| 7 | planar→interleaved PCM (decode) | `ffmpeg_dec-ffmpeg.c:231` | copy/interleave | scalar C | low–med | **low** | +| 8 | S32→S16 pack (encode) | `ffmpeg_dec-encode.c:147` | narrow + interleave | scalar C | med | **low** | +| 9 | fast float libm (powf/exp2f/...) | `fastmathf.c` | scalar poly/Newton | scalar C | med (per-band) | med | + +Priority order (cost/benefit): **2 → 6/7/8 → 3 → 1 → 4 → 9 → 5**. Start with the +easy, high-use float vector kernels (2) and the conversion loops we own (6–8); +tackle the transform (1) last because it is the most work despite the highest +raw cost. + +## (A) Module conversion loops — we own these, do them first + +These run **per sample over a whole frame** (typically 1024–4608 samples × +channels per call), so they are genuinely hot and are trivial, self-contained HiFi +wins. Add HiFi versions guarded by `#if XCHAL_HAVE_HIFI*` with the current scalar +loop as the `#else` fallback. + +1. **Filter S32→float deinterleave + normalise** — `ffmpeg_dec-filter.c:230` + ```c + ((float *)in->data[c])[i] = (float)src[i * ch + c] / FFMPEG_AF_S32_SCALE; + ``` + HiFi: load 32-bit lanes, convert int32→float (`AE_FLOAT32`-family), multiply by + the reciprocal scale, store to the per-channel plane. Deinterleave with + strided/`AE_SEL` moves. + +2. **Filter float→S32 interleave + denormalise + clip** — `ffmpeg_dec-filter.c:255` + The scalar path multiplies, branches to clamp, then casts. HiFi does the + float→int32 convert **with saturation** in one op (no per-sample branch), which + is both faster and removes the mispredicted clamp branch. + +3. **Encode S32→S16 narrow + deinterleave** — `ffmpeg_dec-encode.c:147` + ```c + int16_t s = (int16_t)(in[i * ch + c] >> 16); + ``` + HiFi: pack 32→16 with rounding (`AE_ROUND16X4F32`-style) and store; SIMD handles + 4–8 samples per iteration. + +4. **Decode planar→interleaved** — `ffmpeg_dec-ffmpeg.c:231` + Per-element `memcpy`-equivalent; a HiFi interleave (vector load per plane, + `AE_SEL` interleave, vector store) removes the per-sample loop overhead. Lower + priority as many decoders already output the sink format. + +A single small `ffmpeg_dec-convert.c` with HiFi + scalar variants (like SOF's +`*_generic.c` / `*_hifi3.c` split) would hold all four and be reusable across the +three modes. + +## (B) FFmpeg DSP — the real compute + +FFmpeg's DSP contexts are function-pointer tables initialised per arch. The +Xtensa-idiomatic fix is a **fork patch** adding `ff__init_xtensa()` with HiFi +intrinsics, mirroring `libavcodec/aarch64/`, `x86/` etc. (our FFmpeg is already a +SOF fork — see `west.yml` — so such patches have a home). + +- **`AVFloatDSPContext`** (`libavutil/float_dsp.h`) — `vector_fmul`, + `vector_fmul_window`, `vector_fmul_add`, `vector_fmul_reverse`, + `scalarproduct_float`. Used for windowing / overlap-add by **AAC, MP3 and Opus**. + These are plain multiply/MAC over contiguous float arrays — the easiest and + highest-leverage HiFi target (one small `float_dsp_init_xtensa` benefits three + codecs). **Do this first on the FFmpeg side.** +- **`FLACDSPContext`** (`libavcodec/flacdsp.h`) — `lpc16`/`lpc32`/`lpc33` and + decorrelate. FLAC decode is dominated by the LPC prediction MAC loop over the + residual: a natural fit for HiFi multiply-accumulate. `ff_flacdsp_init_xtensa`. +- **`MPADSPContext`** (`libavcodec/mpegaudiodsp.h`) — `apply_window_{float,fixed}`, + `imdct36_blocks_*`, `synth_filter`. The MP3 subband synthesis window is the MP3 + decode hot loop. +- **`libavutil/tx`** (MDCT/FFT) — the single largest cost for AAC/MP3/Opus decode + and for the `afftdn` filter. It is a "codelet" system rather than one function + pointer, so it is the most work. Two routes: + 1. Add HiFi FFT/MDCT codelets to the fork (largest effort, cleanest for FFmpeg). + 2. Bridge to **SOF's existing HiFi3 FFT** (`src/math/fft/fft_*_hifi3.c`) by + replacing FFmpeg's transform behind `av_tx_init` for the sizes SOF supports. + Less code, but a semantic bridge (twiddle/scaling/layout must match) and only + covers power-of-two sizes. +- **`PSDSPContext` / SBR DSP** — only relevant if HE-AAC (SBR/PS) is enabled; skip + unless needed. + +## (C) fastmathf — the module's float libm + +`fastmathf.c` (`powf`/`exp2f`/`log2f`/`sinf`/`cosf`/`sqrtf`/`cbrtf`) is scalar +polynomial/Newton code. These are called per-band / per-frame (not the innermost +per-sample loop), so they are a secondary target. HiFi wins come from: +- `sqrtf`/`cbrtf`: HiFi `RSQRT`/reciprocal seed instructions instead of the + bit-trick + Newton. +- `exp2f`/`log2f`/`sinf`/`cosf`: process 2/4 lanes at once when a decoder needs a + vector of them (e.g. AAC scalefactor gains), and use FMA for the polynomials. + Mirror `src/math/exp_fcn_hifi.c`. + +## Appendix: AAC-LC decode hot spots (per-frame detail) + +Where the cycles actually go decoding one AAC-LC frame (1024 samples long, or +8×128 short) per channel, mapped to the code in the tree. Everything below runs as +**scalar C** on Xtensa today (`--disable-asm`, no `_xtensa` DSP init). + +### Toolchain requirement (applies to all FFmpeg-side HiFi work) + +The Cadence HiFi intrinsic headers (``) and a +`core-isa.h` with `XCHAL_HAVE_HIFI4 == 1` are **only** available through the +**LLVM/xt-clang Xtensa** toolchain (on this build host under +`llvm-project/.../clang/lib/Headers/xtensa/tie/` plus the ace30 core config at +`modules/hal/xtensa/.../soc/intel_ace30_ptl/xtensa/config/core-isa.h`). The +Zephyr-SDK GCC that `ffmpeg.cmake` currently drives does **not** ship these headers. + +Consequence: the `ff_*_init_xtensa` kernels must be written with `#if +XCHAL_HAVE_HIFI*` intrinsics **and** a scalar `#else` fallback. Under the current +GCC cross-build the guard is 0, so the fallback compiles (correct, but no speedup). +Actual HiFi codegen requires cross-building FFmpeg with the LLVM Xtensa clang and +adding the ace30 core-config include dir to the FFmpeg `CFLAGS` — the same +toolchain SOF's own `src/math/*_hifi*.c` already rely on. + +### Tier 1 — dominant, best return + +1. **IMDCT** — `mdct1024_fn` / `mdct128_fn` (short blocks) via `libavutil/tx`, + driven from `imdct_and_windowing()` (`libavcodec/aac/aacdec_dsp_template.c:341-343`). + One 1024-pt (or 8×128) inverse MDCT per channel per frame — the single largest + compute. Dispatch: `AVTXContext`. HiFi = complex-FMA butterflies, via HiFi `tx` + codelets or a bridge to SOF's HiFi3 FFT (`src/math/fft/`). Highest cost, most work. + +2. **`AVFloatDSPContext` vector kernels** — pervasive for windowing / overlap-add / + stereo, and the easiest high-leverage target: + - `vector_fmul_window` — windowed overlap-add, ~10 call sites (`:354-419`); *the* + per-sample AAC output kernel. + - `vector_fmul` / `vector_fmul_reverse` — window application (`:235-308`). + - `vector_fmul_scalar` — intensity-stereo gain (`:146`). + - `butterflies_float` — M/S stereo (`:102`). + One `ff_float_dsp_init_xtensa` (in `libavutil/`) covers all of them and also + accelerates MP3 and Opus. **Start here** (lowest effort, cross-codec). + +### Tier 2 — AAC-specific loops (in `aacdec_dsp_template.c`, HiFi'd inline in the fork) + +3. **Inverse quantization `x^(4/3)`** — `decode_spectrum_and_dequant()` + (`libavcodec/aac/aacdec.c:1768`) + `dequant_scalefactors()` (`:41`): up to 1024 + coeffs, each `ff_cbrt_tab` lookup for `|x|^(4/3)` × scalefactor gain + `2^(0.25·sf)`. HiFi = vectorised table gather + FMA (per-SFB gain is a + `vector_fmul_scalar`). +4. **TNS** — `apply_tns()` (`:164`): LPC all-pole filter (order ≤ ~20) over spectral + coeffs per window. HiFi = MAC filter (mirror `src/math/iir_df1_hifi*`). + +### Tier 3 — HE-AAC only (skip unless SBR/PS enabled) + +5. **SBR QMF** — `libavcodec/sbrdsp.c`: `sbr_qmf_pre/post_shuffle`, `sbr_hf_gen`, + `sbr_hf_g_filt`, `sbr_sum64x5` + a 64-pt QMF transform. Dispatch: + `AACSBRDSPContext` (`sbrdsp.h`, `ff_sbrdsp_init`). Nearly as heavy as the base IMDCT. +6. **PS** — `libavcodec/aacps.c` `hybrid_analysis` / `stereo_interpolate` via + `PSDSPContext` (`aacpsdsp.h`, `ff_psdsp_init`). + +### Where the HiFi hooks land + priority + +| Priority | Target | Dispatch / location | Effort | +|---|--------|---------------------|--------| +| 1 | `vector_fmul_window` + friends | `AVFloatDSPContext` → `ff_float_dsp_init_xtensa` (libavutil) | low; AAC+MP3+Opus | +| 2 | IMDCT (mdct128/1024) | `libavutil/tx` — HiFi codelets or SOF-FFT bridge | high | +| 3 | dequant `x^(4/3)` | `aac/aacdec.c` + dsp_template (cbrt gather+FMA) | med | +| 4 | TNS | `apply_tns` (LPC MAC) | med | +| 5 | SBR / PS | `sbrdsp`/`aacpsdsp` `_xtensa` inits | med, HE-AAC only | + +AAC-LC order: **float_dsp (2) → IMDCT (1) → dequant (3) → TNS (4)**; SBR/PS only if +HE-AAC. `float_dsp`/`tx`/`sbrdsp`/`psdsp` are `libavutil`/`libavcodec` and take +`_xtensa` inits mirroring the existing `aarch64`/`x86` dirs; dequant/TNS are AAC +decoder code patched in place. All on the `lgirdwood/FFmpeg` `sof-hifi` branch. + +## Recommendations + +1. **Quick wins we own** — hand-vectorise the four conversion loops (§A) with + `#if XCHAL_HAVE_HIFI*` + scalar fallback. Small, self-contained, no fork needed. +2. **Biggest FFmpeg leverage-per-effort** — add `ff_float_dsp_init_xtensa` (HiFi + `vector_fmul*`/`scalarproduct`) to the FFmpeg fork; benefits AAC + MP3 + Opus. +3. **Per-codec kernels** — `flacdsp` (FLAC), `mpegaudiodsp` (MP3) HiFi inits as + fork patches, gated on the enabled decoders. +4. **Transform** — evaluate SOF-HiFi-FFT bridge vs. HiFi tx codelets for + `libavutil/tx`; highest payoff, most work, do once the cheaper items land. +5. Always keep the scalar C path as the fallback (`#else`) and behind the same ISA + guards SOF uses, so non-HiFi / host/testbench builds still work. + +Measurement: profile on hardware per codec before and after; the ranking above is +by expected cost, but the actual hot spot depends on the codec and content (FLAC +is LPC-bound, AAC/MP3/Opus are transform+window-bound). diff --git a/src/audio/ffmpeg_dec/Kconfig b/src/audio/ffmpeg_dec/Kconfig new file mode 100644 index 000000000000..944bdc882470 --- /dev/null +++ b/src/audio/ffmpeg_dec/Kconfig @@ -0,0 +1,122 @@ +# SPDX-License-Identifier: BSD-3-Clause + +config COMP_FFMPEG_DEC + tristate "FFmpeg (libavcodec) audio decoder" + select COMP_FFMPEG_DEC_STUB if COMP_STUBS + help + Select to include the FFmpeg audio decoder module. It wraps + libavcodec decoders behind the SOF module interface, decoding a + compressed elementary stream to PCM. The real backend cross-builds + libavcodec/libavutil/libswresample from the FFmpeg source pulled in + by west (see west.yml), enabling only the decoders selected below. + Without the source (or for CI) select COMP_FFMPEG_DEC_STUB to build + the dependency-free passthrough stub. + +config COMP_FFMPEG_DEC_STUB + bool "FFmpeg decoder stub backend" + depends on COMP_FFMPEG_DEC + help + Build the ffmpeg_dec module against a dependency-free passthrough + backend instead of libavcodec. Intended for testing and CI so the + SOF glue and LLEXT packaging can be validated without the FFmpeg + source or a cross-built archive. + +if COMP_FFMPEG_DEC && !COMP_FFMPEG_DEC_STUB + +comment "FFmpeg decoders to build (footprint scales with selection)" + +config FFMPEG_DEC_FLAC + bool "FLAC decoder" + default y + help + Lossless integer decoder. Smallest footprint, no floating-point + math. Enables --enable-decoder=flac in the libavcodec build. + +config FFMPEG_DEC_AAC + bool "AAC-LC decoder" + help + AAC Low Complexity decoder. Pulls in the single-precision float + math layer (see FFMPEG_DEC_FLOAT_MATH). Enables + --enable-decoder=aac in the libavcodec build. + +config FFMPEG_DEC_OPUS + bool "Opus decoder" + help + Opus (SILK+CELT) decoder. Pulls in the single-precision float math + layer. Enables --enable-decoder=opus in the libavcodec build. + +config FFMPEG_DEC_MP3 + bool "MP3 decoder" + help + MPEG-1/2 Layer III decoder. Enables --enable-decoder=mp3 and the + mpegaudio parser in the libavcodec build. Uses the float math layer. + +config FFMPEG_ENC_MP3 + bool "MP3 encoder (libshine, fixed-point)" + help + MPEG-1 Layer III encoder via libshine, a small fixed-point encoder + (west-pinned; FFmpeg has no native MP3 encoder). Cross-builds libshine + and enables --enable-libshine --enable-encoder=libshine. Fixed-point, so + no float math dependency. The encoder is built into the archive; a module + encode path (PCM->MP3) is a separate build mode. + +comment "FFmpeg audio filters (libavfilter; need the avfilter-graph backend to run)" + +config FFMPEG_FILTER_AFFTDN + bool "FFT noise reduction (afftdn)" + help + Build FFmpeg's FFT-based denoiser (afftdn) into libavfilter. Pure DSP, + no external model file (unlike arnndn). Enabling any filter turns on + libavfilter in the cross-build. NOTE: a filter is only usable at runtime + once the module gains an avfilter-graph backend; selecting it here just + builds the filter into the archive. + +config FFMPEG_DEC_ENCODE_MODE + bool "Build as an audio encoder (PCM -> compressed) instead of a decoder [EXPERIMENTAL]" + select FFMPEG_ENC_MP3 + help + Build the module as an encoder: PCM in, compressed elementary stream out + (MP3 via libshine), driven with avcodec_send_frame/avcodec_receive_packet + (the reverse of the decoder). Selects the libshine MP3 encoder. Do not set + together with FFMPEG_DEC_FILTER_MODE. Structurally complete and load-ready; + real-time framing (MP3 fixed 1152-sample frames) needs on-hardware tuning. + +config FFMPEG_DEC_FILTER_MODE + bool "Build as a PCM filter effect instead of a decoder [EXPERIMENTAL]" + select FFMPEG_FILTER_AFFTDN + help + Build the module as a PCM source->sink effect that runs an FFmpeg + audio filter graph (default afftdn noise reduction) via the modern + .process interface, instead of the compressed-stream decoder. Turns on + libavfilter and the fast float math. Structurally complete and + load-ready; real-time latency/format tuning needs on-hardware bring-up. + +# Turns on libavfilter in the FFmpeg cross-build when any filter is selected. +config FFMPEG_BUILD_AVFILTER + bool + default y if FFMPEG_FILTER_AFFTDN + +# Selected automatically when a decoder or filter that needs IEEE float math is +# built. Gates compilation of the fast single-precision libm (fastmathf.c). +config FFMPEG_DEC_FLOAT_MATH + bool + default y if FFMPEG_DEC_AAC || FFMPEG_DEC_OPUS || FFMPEG_DEC_MP3 || FFMPEG_FILTER_AFFTDN + +config FFMPEG_DEC_COLD_SPLIT + bool "Place cold FFmpeg code (av_cold init/setup) in DRAM [EXPERIMENTAL]" + depends on COLD_STORE_EXECUTE_DRAM + default n + help + EXPERIMENTAL. Build libavcodec with -mtext-section-literals and rename + FFmpeg's cold functions (its av_cold init/table-generation code, which GCC + emits into .text.unlikely) into SOF's .cold section so the LLEXT loader + places them in DRAM, keeping the hot per-frame decode in fast SRAM. + + Measured cold code is small (~1 KB for FLAC, ~26 KB for AAC) because + FFmpeg only marks init functions av_cold. Placing that much cold code in a + -shared LLEXT currently hits Xtensa linker limits (cold->hot call8 range + needs -mlongcalls; the orphan .cold lands at a bad LMA and overlaps .hash), + so this is off by default pending proper cold-region placement in the LLEXT + memory map. See git history / TESTING notes. + +endif # COMP_FFMPEG_DEC && !COMP_FFMPEG_DEC_STUB diff --git a/src/audio/ffmpeg_dec/README.md b/src/audio/ffmpeg_dec/README.md new file mode 100644 index 000000000000..326e878b2829 --- /dev/null +++ b/src/audio/ffmpeg_dec/README.md @@ -0,0 +1,100 @@ +# FFmpeg Audio Processing Module (`ffmpeg_dec`) + +Wraps FFmpeg's `libavcodec` and `libavfilter` libraries behind the SOF `module_interface`. This module enables running on-DSP audio decoders, encoders, and real-time audio filter graphs. + +--- + +## Features + +- **Decoder Mode**: Decodes compressed audio streams (e.g., FLAC, MP3) to raw PCM. +- **Encoder Mode**: Encodes raw PCM streams to compressed formats (e.g., MP3 using `libshine`). +- **Filter Mode**: Runs FFmpeg's `libavfilter` graphs directly on raw PCM data (e.g., `afftdn` for noise reduction). +- **Embedded-Optimized**: Supports single-threaded execution, memory shims for dynamic allocation, and LLEXT packaging. +- **CI-Friendly**: Includes a pass-through stub backend to allow pipeline, IPC, and topology verification without external dependencies. + +--- + +## Architecture & Data Flow + +The following Mermaid diagram illustrates the module's internal architecture and the interaction between the SOF pipeline, the `ffmpeg_dec` core, and its backend translation units: + +```mermaid +graph TD + %% Audio Data Flow + InBuf[Input Audio Buffer] -->|Raw Bytes or PCM| Core[ffmpeg_dec.c Core] + Core -->|Pass-through| BackendStub[ffmpeg_dec-stub.c Stub] + Core -->|Process| BackendFFmpeg[ffmpeg_dec-ffmpeg.c FFmpeg Backend] + + BackendFFmpeg -->|Parse/Decode/Filter| Libavcodec[libavcodec / libavfilter] + Libavcodec -->|Output PCM Frame| Core + BackendStub -->|Pass-through S16/S32| Core + Core -->|Interleaved PCM or Encoded Bytes| OutBuf[Output Audio Buffer] + + %% Control Flow + IPC[SOF IPC set_configuration] -.->|Metadata/Extradata| Core + Core -.->|avcodec_open2 / init| Libavcodec +``` + +### Modular Design +- **`ffmpeg_dec.c` (Core Glue)**: Implements the standard `module_interface` functions (`init`, `prepare`, `process`, `reset`, `free`). +- **`ffmpeg_dec-stub.c`**: Pass-through stub backend that bypasses FFmpeg libraries. +- **`ffmpeg_dec-ffmpeg.c` / `ffmpeg_dec-filter.c` / `ffmpeg_dec-encode.c`**: Real backend implementations interfacing with `libavcodec` and `libavfilter`. +- **`ffmpeg_dec-shims.c`**: Overrides dynamic memory allocations (`malloc`, `realloc`, `free`, `calloc`) inside FFmpeg to use SOF's `rballoc` pools. + +--- + +## Build Instructions + +### 1. Stub Mode (Default / CI / Staging) +No external libraries are required. The module acts as a simple pass-through. +```ini +CONFIG_COMP_FFMPEG_DEC=y # or =m for LLEXT +CONFIG_COMP_FFMPEG_DEC_STUB=y +``` + +### 2. Real FFmpeg Backend +Requires pre-compiled static libraries and headers placed under the `third_party/` directory of the workspace. +```ini +CONFIG_COMP_FFMPEG_DEC=m +CONFIG_COMP_FFMPEG_DEC_STUB=n +``` + +#### Configuring the FFmpeg Build +When cross-compiling FFmpeg for Xtensa DSP targets, disable unnecessary components to minimize code size: +```bash +./configure \ + --target-os=none \ + --arch=xtensa \ + --enable-cross-compile \ + --disable-everything \ + --disable-avformat \ + --disable-pthreads \ + --enable-decoder=flac,mp3 \ + --enable-encoder=mp3 \ + --enable-parser=flac,mpegaudio \ + --enable-filter=afftdn \ + --enable-static \ + --disable-shared +``` + +#### Useful Kconfig Options +- `CONFIG_FFMPEG_DEC_FILTER_MODE`: Configures the module to run as a PCM filter graph instead of a decoder. +- `CONFIG_FFMPEG_DEC_FLOAT_MATH`: Automatically selected to pull in optimized floating-point shims (`fastmathf.c`). +- `CONFIG_FFMPEG_DEC_COLD_SPLIT`: Relocates initialization tables and code to DRAM to conserve fast SRAM. + +--- + +## Usage & Topology + +### 1. Decoder Mode +The topology should feed compressed bytes (e.g., from a host gateway) into the decoder input pin. The decoder outputs decoded PCM. +- **Initialization**: Provide codec-specific setup data (e.g., FLAC `STREAMINFO` or MP3 headers) via IPC bytes control `set_configuration`. +- **Buffer Period**: Keep period size large enough (typically 10ms or 20ms) to reduce parsing overhead. + +### 2. Filter Mode +The module is placed in the capture or playback pipeline as a normal 1-in / 1-out effect. +- **Routing Example**: +``` +DAI Copier (Mic Capture) ---> [ ffmpeg_dec (Filter Mode) ] ---> Host Copier (PCM) +``` +- **Control**: Filter coefficients and parameters can be set dynamically via standard TLV bytes controls. diff --git a/src/audio/ffmpeg_dec/TESTING.md b/src/audio/ffmpeg_dec/TESTING.md new file mode 100644 index 000000000000..3d50ed8d84f6 --- /dev/null +++ b/src/audio/ffmpeg_dec/TESTING.md @@ -0,0 +1,66 @@ +# Testing the ffmpeg_dec module + +This describes how to verify the FFmpeg FLAC decoder module end to end. FLAC is +lossless, so a correct decode is **bit-exact** against a reference decode. + +## Test vectors + +Generate vectors with the helper (needs `ffmpeg` on the host): + +``` +tools/test/audio/ffmpeg_dec_prepare.sh -i song.wav -o vectors/ +``` + +Produces in `vectors/`: +- `streaminfo.bin` — 34-byte FLAC STREAMINFO = the codec **extradata** blob to + load into the module's bytes control (`set_configuration`). +- `stream.raw` — the raw FLAC elementary stream fed to the decoder input. +- `ref_s32le.raw` — reference interleaved S32_LE PCM; the decoder output must + match this byte-for-byte. + +## Topology + +The component widget is `tools/topology/topology2/include/components/ffmpeg_dec.conf` +(models the DTS codec: `type "effect"`, one input/one output pin, a `bytes` +control for STREAMINFO). Its `uuid` must match the firmware's registry entry for +`ffmpeg_dec` (see `uuid-registry.txt`). + +A runnable pipeline must deliver **compressed bytes** to the module and take PCM +out. Two paths: + +1. **ALSA compress-offload** (the production path for a decoder): the host writes + the FLAC elementary stream to a compress DMA gateway; the module decodes; PCM + goes to a DAI or host capture. This is the correct model (a decoder is not a + fixed-rate PCM effect) and is the remaining integration to wire on real ptl + hardware. +2. **Direct raw-data injection** (bring-up shortcut): a minimal pipeline where a + host copier delivers `stream.raw` as an opaque byte buffer into the module's + `process_raw_data`, capturing PCM on the other side. + +## Verification paths + +### A. On target (ptl hardware) — full verification +1. Build + flash firmware with the real backend: + `CONFIG_COMP_FFMPEG_DEC=m`, `CONFIG_COMP_FFMPEG_DEC_STUB=n` (requires the + FFmpeg static libs in `third_party/`; see the module README). +2. Load the topology; set the STREAMINFO bytes control: + `amixer -c0 cset name='FFMPGDEC...' -- $(od -An -tx1 vectors/streaminfo.bin)` + (or via `tinymix`). +3. Play `stream.raw` through the pipeline, capture the PCM output. +4. `cmp captured.raw vectors/ref_s32le.raw` — must be identical. + +### B. Native testbench (no hardware) — decode-only check +The module builds native (built-in, real libc — the LLEXT-only shims/alloc are +excluded automatically, see `CMakeLists.txt`). Linking the **host** libavcodec +and driving `process_raw_data` over `stream.raw` with `ref_s32le.raw` as the +oracle gives a bit-exact check without a DSP. This harness is not yet wired (the +testbench file component is PCM-oriented; feeding compressed bytes needs a small +adapter) and is the recommended next step for CI-friendly verification. + +## Status + +- Component widget conf: **done**. +- Host vector/reference tooling: **done** (`ffmpeg_dec_prepare.sh`). +- Runnable pipeline (compress-offload) + on-target bit-exact run: **pending + hardware**. +- Native testbench harness: **pending** (small compressed-input adapter). diff --git a/src/audio/ffmpeg_dec/fastmathf.c b/src/audio/ffmpeg_dec/fastmathf.c new file mode 100644 index 000000000000..02153fd10dc1 --- /dev/null +++ b/src/audio/ffmpeg_dec/fastmathf.c @@ -0,0 +1,257 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Fast single-precision float math for FFmpeg lossy decoders (AAC-LC / Opus). +// +// Empirically these decoders need only 7 single-precision functions on their +// decode paths: powf, sqrtf, cbrtf, exp2f, log2f, sinf, cosf. SOF's own math is +// fixed-point Q-format (range-limited) and cannot serve them, so this provides +// IEEE single-precision approximations. 24-bit mantissa precision is ample for +// 32-bit audio samples destined for 24/16-bit rendering, and these are per-frame +// / per-band calls (not the innermost per-sample loop, which is MDCT + table +// lookups), so scalar polynomial/Newton approximations are fast enough. +// +// On Xtensa HiFi, sqrtf/rsqrt and FMA map to hardware intrinsics; the scalar C +// here is the portable reference. Marked spots are where HiFi intrinsics would +// slot in. +// +// Build standalone accuracy/perf test: +// gcc -O2 -DSOFM_FASTMATH_TEST fastmathf.c -lm -o t && ./t + +#include + +/* --- helpers --- */ + +static inline float sofm_flint(int n) +{ + return (float)n; +} + +/* Build 2^n as a float via the exponent field (n in [-126,127]). */ +static inline float sofm_ldexp2(int n) +{ + union { float f; uint32_t i; } u; + + u.i = (uint32_t)(n + 127) << 23; + return u.f; +} + +/* --- sqrtf: bit-trick seed + 2 Newton steps --- */ +float sofm_sqrtf(float x) +{ + union { float f; uint32_t i; } u = { .f = x }; + float y; + + if (x <= 0.0f) + return 0.0f; + + /* HiFi: use FSQRT/RSQRT intrinsic instead of this seed. */ + u.i = 0x1fbd1df5 + (u.i >> 1); + y = u.f; + y = 0.5f * (y + x / y); + y = 0.5f * (y + x / y); + return y; +} + +/* --- cbrtf: bit-trick seed + 2 Newton steps, sign-symmetric --- */ +float sofm_cbrtf(float x) +{ + union { float f; uint32_t i; } u; + int neg = x < 0.0f; + float a = neg ? -x : x; + float y; + + if (x == 0.0f) + return 0.0f; + + u.f = a; + u.i = u.i / 3 + 0x2a514067; + y = u.f; + y = (2.0f * y + a / (y * y)) * (1.0f / 3.0f); + y = (2.0f * y + a / (y * y)) * (1.0f / 3.0f); + return neg ? -y : y; +} + +/* --- exp2f: split x = n + f, 2^x = 2^n * 2^f, poly for 2^f on [0,1) --- */ +float sofm_exp2f(float x) +{ + int n; + float f, p; + + if (x > 127.0f) + return sofm_ldexp2(127) * 2.0f; /* +inf-ish */ + if (x < -126.0f) + return 0.0f; + + n = (int)x; + if (x < 0.0f && (float)n != x) + n--; /* floor */ + f = x - (float)n; + + /* Taylor of 2^f, coeffs (ln2)^k/k!, degree 5 -> ~1e-6 rel error. */ + p = 1.0f + f * (0.6931472f + f * (0.2402265f + f * (0.0555041f + + f * (0.0096181f + f * 0.0013334f)))); + return p * sofm_ldexp2(n); +} + +/* --- log2f: x = 2^e * m, reduce m to [sqrt(2)/2, sqrt(2)), atanh series --- */ +float sofm_log2f(float x) +{ + union { float f; uint32_t i; } u = { .f = x }; + int e; + float m, a, a2, ln_m; + + if (x <= 0.0f) + return -127.0f; + + e = (int)((u.i >> 23) & 0xff) - 127; + u.i = (u.i & 0x007fffff) | 0x3f800000; /* m in [1,2) */ + m = u.f; + if (m > 1.41421356f) { /* center around 1: m in [~.707,~1.414) */ + m *= 0.5f; + e += 1; + } + + /* ln(m) = 2*atanh(a), a = (m-1)/(m+1); exact 0 at m==1. */ + a = (m - 1.0f) / (m + 1.0f); + a2 = a * a; + ln_m = 2.0f * a * (1.0f + a2 * (0.3333333f + a2 * (0.2f + a2 * 0.14285714f))); + return (float)e + ln_m * 1.44269504f; /* / ln(2) */ +} + +/* --- powf: x^y = 2^(y*log2(x)) for x > 0 (AAC uses non-negative magnitudes) --- */ +float sofm_powf(float x, float y) +{ + if (x <= 0.0f) + return 0.0f; + return sofm_exp2f(y * sofm_log2f(x)); +} + +/* --- sinf/cosf: Cody-Waite reduction by pi/2 + quadrant, polys on [-pi/4,pi/4] --- */ +static float sofm_poly_sin(float r) +{ + float r2 = r * r; + + return r * (1.0f + r2 * (-0.16666667f + r2 * (0.00833216f + + r2 * -0.00019515f))); +} + +static float sofm_poly_cos(float r) +{ + float r2 = r * r; + + return 1.0f + r2 * (-0.5f + r2 * (0.04166664f + + r2 * (-0.00138873f + r2 * 2.4433e-5f))); +} + +static float sofm_sincos_core(float x, int cos_phase) +{ + /* pi/2 split so k*PIO2_hi is exact for moderate k (Cody-Waite). */ + const float two_over_pi = 0.636619772f; + const float PIO2_hi = 1.5707855225f; + const float PIO2_lo = 1.08043006e-5f; + int k = (int)(x * two_over_pi + (x < 0.0f ? -0.5f : 0.5f)); + float r = (x - (float)k * PIO2_hi) - (float)k * PIO2_lo; + int q = (k + cos_phase) & 3; + + switch (q) { + case 0: return sofm_poly_sin(r); + case 1: return sofm_poly_cos(r); + case 2: return -sofm_poly_sin(r); + default: return -sofm_poly_cos(r); + } +} + +float sofm_sinf(float x) +{ + return sofm_sincos_core(x, 0); +} + +float sofm_cosf(float x) +{ + return sofm_sincos_core(x, 1); +} + +/* + * libc-name entry points used by libavcodec. Defined here so the LLEXT resolves + * them internally. (In a native/testbench build the real libm provides these; + * this file, like the other shims, is LLEXT-only.) + */ +#ifndef SOFM_FASTMATH_TEST +float sqrtf(float x) { return sofm_sqrtf(x); } +float cbrtf(float x) { return sofm_cbrtf(x); } +float exp2f(float x) { return sofm_exp2f(x); } +float log2f(float x) { return sofm_log2f(x); } +float powf(float x, float y) { return sofm_powf(x, y); } +float sinf(float x) { return sofm_sinf(x); } +float cosf(float x) { return sofm_cosf(x); } +#endif + +#ifdef SOFM_FASTMATH_TEST +#include +#include +#include +#include + +/* metric: 0 = relative error, 1 = absolute error */ +static double maxerr(const char *name, float (*approx)(float), + double (*ref)(double), float lo, float hi, int log_sweep, + int absolute) +{ + double worst = 0.0, at = 0.0; + int i, N = 200000; + + for (i = 0; i < N; i++) { + double t = (double)i / (N - 1); + float x = log_sweep ? (float)(lo * pow(hi / lo, t)) + : (float)(lo + (hi - lo) * t); + double r = ref((double)x), a = approx(x); + double e = absolute ? fabs(a - r) + : (fabs(r) > 1e-9 ? fabs((a - r) / r) : fabs(a - r)); + + if (e > worst) { worst = e; at = x; } + } + printf(" %-8s max_%s_err=%.3e at x=%.6g\n", name, + absolute ? "abs" : "rel", worst, at); + return worst; +} + +static double refpow_x(double x) { return pow(x, 1.3333333333); } + +int main(void) +{ + volatile float acc = 0; + int i; + clock_t t0; + + printf("accuracy (approx vs libm double reference):\n"); + maxerr("sqrtf", sofm_sqrtf, sqrt, 1e-6f, 1e6f, 1, 0); + maxerr("cbrtf", sofm_cbrtf, cbrt, 1e-6f, 1e6f, 1, 0); + maxerr("exp2f", sofm_exp2f, exp2, -30.f, 30.f, 0, 0); + maxerr("log2f", sofm_log2f, log2, 1e-6f, 1e6f, 1, 1); /* abs: log2 crosses 0 */ + maxerr("sinf", sofm_sinf, sin, -25.f, 25.f, 0, 1); /* abs: sin/cos in [-1,1] */ + maxerr("cosf", sofm_cosf, cos, -25.f, 25.f, 0, 1); + /* powf specifically over the AAC dequant shape x^(4/3), x in [0,8192] */ + { + double worst = 0, at = 0; + for (i = 1; i < 200000; i++) { + float x = (float)(8192.0 * i / 200000.0); + double r = refpow_x(x), a = sofm_powf(x, 1.3333333f); + double e = fabs(r) > 1e-9 ? fabs((a - r) / r) : fabs(a - r); + if (e > worst) { worst = e; at = x; } + } + printf(" %-8s max_rel_err=%.3e at x=%.6g (x^4/3)\n", "powf", worst, at); + } + + /* rough timing: fast vs libm for exp2f/log2f/powf */ + printf("timing (200M calls):\n"); + t0 = clock(); + for (i = 0; i < 200000000; i++) acc += sofm_exp2f((float)(i & 63) - 32.f); + printf(" sofm_exp2f: %.2fs\n", (double)(clock() - t0) / CLOCKS_PER_SEC); + t0 = clock(); + for (i = 0; i < 200000000; i++) acc += exp2f((float)(i & 63) - 32.f); + printf(" libm exp2f: %.2fs\n", (double)(clock() - t0) / CLOCKS_PER_SEC); + return (int)acc & 0; +} +#endif diff --git a/src/audio/ffmpeg_dec/ffmpeg.cmake b/src/audio/ffmpeg_dec/ffmpeg.cmake new file mode 100644 index 000000000000..8e5da7ef7b60 --- /dev/null +++ b/src/audio/ffmpeg_dec/ffmpeg.cmake @@ -0,0 +1,402 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Cross-build decoder-only FFmpeg static libraries for the ffmpeg_dec module. +# +# Invokes FFmpeg's autotools build as an ExternalProject, cross-compiled with the +# Zephyr target toolchain, enabling only the decoders selected in Kconfig. The +# source comes from the west-pinned FFmpeg project (see west.yml). Produces +# libavcodec/libavutil/libswresample .a under ${FFMPEG_INSTALL_DIR} for the LLEXT +# to link, and defines the 'ffmpeg_ext' target it must depend on. + +include(ExternalProject) + +# --- 1. Locate the FFmpeg source (west module), overridable for local trees --- +if(NOT DEFINED SOF_FFMPEG_SRC_DIR) + set(SOF_FFMPEG_SRC_DIR "${sof_top_dir}/../modules/audio/ffmpeg" + CACHE PATH "FFmpeg source tree (west-pinned)") +endif() +cmake_path(NORMAL_PATH SOF_FFMPEG_SRC_DIR) +if(NOT EXISTS "${SOF_FFMPEG_SRC_DIR}/configure") + message(FATAL_ERROR + "ffmpeg_dec: FFmpeg source not found at '${SOF_FFMPEG_SRC_DIR}'.\n" + "Run 'west update' (FFmpeg is pinned in west.yml) or pass " + "-DSOF_FFMPEG_SRC_DIR=.") +endif() + +# --- 2. Kconfig -> --enable-decoder / --enable-parser lists --- +set(_ff_decoders "") +set(_ff_parsers "") +if(CONFIG_FFMPEG_DEC_FLAC) + list(APPEND _ff_decoders flac) + list(APPEND _ff_parsers flac) +endif() +if(CONFIG_FFMPEG_DEC_AAC) + list(APPEND _ff_decoders aac aac_latm) + list(APPEND _ff_parsers aac aac_latm) +endif() +if(CONFIG_FFMPEG_DEC_OPUS) + list(APPEND _ff_decoders opus) + list(APPEND _ff_parsers opus) +endif() +if(CONFIG_FFMPEG_DEC_MP3) + list(APPEND _ff_decoders mp3) + list(APPEND _ff_parsers mpegaudio) +endif() +# A decoder is required unless this is an encoder-only build. +if(NOT _ff_decoders AND NOT CONFIG_FFMPEG_ENC_MP3) + message(FATAL_ERROR "ffmpeg_dec: no decoder or encoder selected (Kconfig FFMPEG_DEC_*/FFMPEG_ENC_*)") +endif() +set(_ff_dec_cfg "") +if(_ff_decoders) + list(JOIN _ff_decoders "," _ff_dec_csv) + list(JOIN _ff_parsers "," _ff_par_csv) + set(_ff_dec_cfg --enable-decoder=${_ff_dec_csv} --enable-parser=${_ff_par_csv}) +endif() + +# --- 2b. Kconfig -> libavfilter audio filters (off unless a filter is chosen) --- +set(_ff_avfilter_cfg --disable-avfilter) +if(CONFIG_FFMPEG_BUILD_AVFILTER) + set(_ff_filters "") + if(CONFIG_FFMPEG_FILTER_AFFTDN) + list(APPEND _ff_filters afftdn) + endif() + list(JOIN _ff_filters "," _ff_filt_csv) + # abuffer/abuffersink feed/drain a filter graph; aformat negotiates format. + set(_ff_avfilter_cfg + --enable-avfilter + --enable-filter=abuffer,abuffersink,aformat,${_ff_filt_csv}) + message(STATUS "ffmpeg_dec: enabling avfilter, filters [${_ff_filt_csv}]") +endif() + +# --- 3. Derive the cross toolchain + per-compiler cross flags --- +# GCC 14 (Zephyr SDK) promotes these to errors; FFmpeg 7.x trips them. +set(_ff_extra_cflags "-fPIC -Wno-error=incompatible-pointer-types -Wno-error=implicit-function-declaration") + +if(CMAKE_C_COMPILER_ID STREQUAL "Clang") + # LLVM/xt-clang build. clang is a generic driver, so unlike the target- + # specific GCC it needs the target/core/sysroot spelled out, it uses the + # Zephyr-SDK GNU binutils for ar/as/nm/objcopy, and it cannot link the bare- + # metal configure *test* executables itself (no default crt/linker script) -- + # so those are linked with the GNU gcc via --ld. We only ever build .a + # archives (ar), so the linker choice affects configure tests only. + # The GNU binutils cross-prefix normally comes from CMAKE_AR, but the Zephyr + # LLVM toolchain (cmake/toolchain/llvm/target.cmake, BINTOOLS=llvm) sets + # CMAKE_AR=llvm-ar and only names the SDK GNU tools through the ld it puts in + # CMAKE_EXE_LINKER_FLAGS (--ld-path=.../xtensa-_zephyr-elf-ld). Prefer + # CMAKE_AR when it is already the SDK's xtensa ar (xt-clang), else recover the + # SDK cross-prefix from that --ld-path. e.g. .../bin/xtensa-intel_ace30_ptl_zephyr-elf-ar: + get_filename_component(_ff_ar_name "${CMAKE_AR}" NAME) + if(_ff_ar_name MATCHES "^xtensa-.+_zephyr-elf-ar$") + set(_ff_ar "${CMAKE_AR}") + elseif(CMAKE_EXE_LINKER_FLAGS MATCHES "--ld-path=([^ ]+xtensa-[^ /]+_zephyr-elf)-ld") + set(_ff_ar "${CMAKE_MATCH_1}-ar") + else() + message(FATAL_ERROR + "ffmpeg_dec: cannot locate the Zephyr SDK xtensa binutils for the " + "FFmpeg cross-build (CMAKE_AR='${CMAKE_AR}' is not an SDK ar and no " + "--ld-path in CMAKE_EXE_LINKER_FLAGS). FFmpeg's configure needs the " + "SDK sysroot/linker; clang alone has no bare-metal libc/crt.") + endif() + string(REGEX REPLACE "ar$" "" _ff_cross_prefix "${_ff_ar}") # ...-elf- + get_filename_component(_tc_dir "${_ff_ar}" DIRECTORY) # .../bin + get_filename_component(_ff_gnuroot "${_tc_dir}" DIRECTORY) # gnu root + get_filename_component(_ff_triple "${_ff_cross_prefix}" NAME) # ...-elf- + string(REGEX REPLACE "-$" "" _ff_triple "${_ff_triple}") # triple + set(_ff_sysroot "${_ff_gnuroot}/${_ff_triple}") + # SDK toolchain core: triple minus the xtensa- prefix and _zephyr-elf suffix + # (xtensa-intel_ace30_ptl_zephyr-elf -> intel_ace30_ptl). + string(REGEX REPLACE "^xtensa-" "" _ff_core "${_ff_triple}") + string(REGEX REPLACE "_zephyr-elf$" "" _ff_core "${_ff_core}") + # clang's -mcpu is the upstream LLVM Xtensa core id, which for some Intel ADSP + # cores differs from the SDK toolchain target (intel_ace30_ptl -> intel_ace30_adsp). + # Mirror the mapping in cmake/toolchain/llvm/target.cmake (env override first, + # then the few names that differ, else identity) so the FFmpeg objects target + # the exact same core as the rest of the image. + set(_ff_mcpu "$ENV{XTENSA_CORE_ID}") + if(NOT _ff_mcpu) + if(_ff_core STREQUAL "intel_ace30_ptl") + set(_ff_mcpu "intel_ace30_adsp") + elseif(_ff_core STREQUAL "intel_ace40") + set(_ff_mcpu "intel_ace40_adsp") + elseif(_ff_core STREQUAL "intel_ace15_mtpm") + set(_ff_mcpu "intel_ace15_adsp") + else() + set(_ff_mcpu "${_ff_core}") + endif() + endif() + set(_ff_cc "${CMAKE_C_COMPILER} --target=xtensa -mcpu=${_ff_mcpu} --sysroot=${_ff_sysroot}") + # NOTE: FFmpeg is built soft-float for the scalar FPU -- see the _ff_cc_ff + # definition at the end of this branch. (It is scoped to FFmpeg's own --cc and + # must NOT reach the newlib libm build, so it is applied there, not here where + # _ff_cc is shared with libm.) + set(_ff_ld "${_ff_cross_prefix}gcc") + # clang --sysroot does NOT add GCC's internal fixed-include directory, and + # that is where lives (the GNU driver adds it + # implicitly). Without it __has_include() fails, + # XCHAL_HAVE_HIFI*_VFPU stay undefined, and libavutil/xtensa/float_dsp_init.c + # silently falls back to scalar C -- i.e. the HiFi5 .sx2x2 float SIMD is + # dropped from the ace40 build. The header lives under xtensa/ in GCC's fixed- + # include dir, but that dir ALSO holds GCC's own -- and putting + # the whole dir on the include path (any of -I/-idirafter/-isystem) makes + # clang's hosted #include_next into GCC's, whose atomic_* macros + # expand to the GNU __atomic_* builtins on _Atomic(int)* pointers that clang + # rejects ("address argument to atomic operation must be a pointer to a + # trivially-copyable type" -- hit by libavcodec/threadprogress.h). So expose + # ONLY the xtensa/ subtree via a private dir (symlink) and -I that: clang finds + # for the VFPU detection but keeps using its own + # freestanding (__c11_atomic_*). (core-isa.h includes nothing + # outside xtensa/, so the single symlink is sufficient.) + execute_process( + COMMAND ${_ff_cross_prefix}gcc -print-file-name=include + OUTPUT_VARIABLE _ff_gcc_inc OUTPUT_STRIP_TRAILING_WHITESPACE) + if(_ff_gcc_inc AND IS_DIRECTORY "${_ff_gcc_inc}/xtensa/config") + set(_ff_xinc "${CMAKE_CURRENT_BINARY_DIR}/ffmpeg-xtensa-inc") + file(MAKE_DIRECTORY "${_ff_xinc}") + file(REMOVE "${_ff_xinc}/xtensa") + file(CREATE_LINK "${_ff_gcc_inc}/xtensa" "${_ff_xinc}/xtensa" SYMBOLIC) + set(_ff_cc "${_ff_cc} -I${_ff_xinc}") + endif() + # The LLVM Xtensa backend cannot lower the SLP-vectorised v2i32 bswap that + # FFmpeg byteswap code produces ("Cannot select: v2i32 = bswap"); disable + # vectorisation to avoid it (and it buys nothing -- no packed float SIMD). + set(_ff_extra_cflags "${_ff_extra_cflags} -fno-vectorize -fno-slp-vectorize") + # A switch() compiles to a jump table whose base address is materialised with + # an L32R against a literal pool. This Xtensa target has L32R-range/relocation + # issues (see the zephyr auto-litpool revert), and a mis-relocated table base + # sends the switch dispatch into garbage -- observed as a silent spin (no + # exception breadcrumb) inside av_opt_set_defaults2's switch on option type. + # Force branch-chain lowering (no jump-table base literal) instead. + set(_ff_extra_cflags "${_ff_extra_cflags} -fno-jump-tables") + # Scalar soft-float for FFmpeg. The ace30/40 core-isa reports XCHAL_HAVE_FP=1 + # (a single-precision scalar FP0 unit), so LLVM lowers plain-C float math to + # hardware FP0 ops (mul.s, mov.s, ...). But the firmware is built CONFIG_FPU=n: + # the scalar FP coprocessor is never enabled for any thread (SOF's own float + # code uses the HiFi VECTOR VFPU, a separate always-available feature). An FP0 + # instruction therefore faults at runtime as EXCCAUSE 0 (illegal instruction) + # the first time a libav* codec runs scalar float math (e.g. ff_mpadsp_init's + # MP3 synthesis-window table setup). Integer codecs (FLAC) never emit one, which + # is why they worked. Disabling the 'fp' target feature makes LLVM emit + # soft-float libcalls (__mulsf3, __truncdfsf2, ...) -- correct on a CONFIG_FPU=n + # image, and free for MP3 whose per-frame decode is fixed-point (float appears + # only in one-time table init). The HiFi VFPU vector-float intrinsics use a + # separate feature and are unaffected. + # + # This is scoped to FFmpeg's own --cc, NOT the shared _ff_cc: the newlib libm + # build must keep 'fp' enabled, because selects machine/fenv-fp.h via + # `#if XCHAL_HAVE_FP` (a core-isa.h macro, still 1 here) and its rur.fcr/rur.fsr + # inline asm needs the 'fp' feature to name the FPU control regs -- disabling it + # makes those uncompilable (s_fma.c). libm's double math is soft-float + # regardless of the feature, and its single-precision (sinf/...) FP0 variants are + # unreferenced by the fixed-point codecs and get GC'd, so libm is safe on _ff_cc. + set(_ff_cc_ff "${_ff_cc} -Xclang -target-feature -Xclang -fp") +else() + # GCC (Zephyr SDK): target-specific driver, brings its own sysroot + crt. + # e.g. .../bin/xtensa-intel_ace30_ptl_zephyr-elf-gcc -> prefix ...-elf- + get_filename_component(_tc_dir "${CMAKE_C_COMPILER}" DIRECTORY) + get_filename_component(_tc_name "${CMAKE_C_COMPILER}" NAME) + string(REGEX REPLACE "gcc$" "" _tc_prefix_name "${_tc_name}") + set(_ff_cross_prefix "${_tc_dir}/${_tc_prefix_name}") + set(_ff_cc "${CMAKE_C_COMPILER}") + set(_ff_ld "${CMAKE_C_COMPILER}") +endif() +# FFmpeg's compile uses _ff_cc_ff (the scalar soft-float variant on LLVM); libm and +# everything else use plain _ff_cc. On the GCC branch they are identical. +if(NOT DEFINED _ff_cc_ff) + set(_ff_cc_ff "${_ff_cc}") +endif() + +# --- 3c. Build a private newlib math archive (vendored source, -mlongcalls) --- +# libavcodec/libavutil reference double libm (sin/cos/exp/log/pow/scalbn/...); the +# lossy decoders (AAC/Opus/Vorbis) actually call it. The base image uses +# CONFIG_MINIMAL_LIBC, which ships NO double libm, so the math must come from +# newlib. +# +# The SDK's PRE-BUILT newlib math cannot be linked as-is: the image is compiled +# -mlongcalls and linked --no-relax, but the SDK objects use direct call8 (there is +# no -mlongcalls multilib), so an out-of-range inter-object call (e.g. -> memset) +# is a link-time "dangerous relocation: call8: call target out of range". So we +# compile newlib's math FROM SOURCE with -mlongcalls -mtext-section-literals -- the +# compiler then emits callx8 (L32R+CALLX, unlimited range), which links cleanly +# under --no-relax. The sources are vendored under libm/ (newlib libm math/ + +# common/, the exact member set the SDK's libc.a selects -- see libm/README). +# +# Linked as an archive (FFMPEG_TARGET_LIBM, last on the link line) it pulls only +# the math objects a decoder actually calls; the whole set's external tail is +# compiler soft-float builtins (__adddf3/__muldf3/comparisons/conversions) + memset +# (already in the image) + __errno on the error paths (bridged to Zephyr's +# per-thread errno in ffmpeg_dec-builtin-libc.c). +set(_ff_libm_src "${CMAKE_CURRENT_LIST_DIR}/libm") +set(_ff_libm_dir "${CMAKE_CURRENT_BINARY_DIR}/newlib-libm") +set(FFMPEG_TARGET_LIBM "${_ff_libm_dir}/libnewlib_m.a" + CACHE INTERNAL "newlib math compiled from source (-mlongcalls) for libav* double math") +# newlib_math_sources.cmake defines NEWLIB_MATH_SOURCES: the exact files to compile +# (relative to libm/). The 13 common/*.c it omits are only textually #included by +# their s_*.c wrappers -- compiling them standalone would multiply-define exp/log/ +# pow/... (see libm/README). +include("${_ff_libm_src}/newlib_math_sources.cmake") +if(NOT NEWLIB_MATH_SOURCES) + message(FATAL_ERROR + "ffmpeg_dec: vendored newlib math manifest empty/missing " + "('${_ff_libm_src}/newlib_math_sources.cmake').") +endif() +set(_ff_libm_abs "") +foreach(_s IN LISTS NEWLIB_MATH_SOURCES) + list(APPEND _ff_libm_abs "${_ff_libm_src}/${_s}") +endforeach() +# Compile each source with the same core/sysroot as the rest of the image (_ff_cc) +# plus the same codegen guards (_ff_extra_cflags: -fno-vectorize/-fno-jump-tables/ +# ...) and -mlongcalls -mtext-section-literals for --no-relax linkability, then +# archive. -ffreestanding: this is bare-metal math, no hosted libc assumptions. +string(REPLACE ";" " " _ff_libm_srclist "${_ff_libm_abs}") +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/newlib-m-build.sh" +"#!/bin/sh +set -e +CC=\"${_ff_cc}\" +OBJDIR=${_ff_libm_dir}/obj +mkdir -p \"\$OBJDIR\" +: > ${_ff_libm_dir}/objs.txt +for f in ${_ff_libm_srclist}; do + o=\"\$OBJDIR/\$(basename \"\$f\").o\" + \$CC ${_ff_extra_cflags} -mlongcalls -mtext-section-literals -O2 -ffreestanding \\ + -I${_ff_libm_src}/common -I${_ff_libm_src}/math -c \"\$f\" -o \"\$o\" + echo \"\$o\" >> ${_ff_libm_dir}/objs.txt +done +rm -f ${FFMPEG_TARGET_LIBM} +${_ff_cross_prefix}ar rcs ${FFMPEG_TARGET_LIBM} \$(cat ${_ff_libm_dir}/objs.txt) +") +add_custom_command( + OUTPUT "${FFMPEG_TARGET_LIBM}" + COMMAND sh "${CMAKE_CURRENT_BINARY_DIR}/newlib-m-build.sh" + DEPENDS ${_ff_libm_abs} "${_ff_libm_src}/newlib_math_sources.cmake" + COMMENT "ffmpeg_dec: building newlib math archive (libnewlib_m.a, -mlongcalls)" + VERBATIM) +add_custom_target(newlib_m DEPENDS "${FFMPEG_TARGET_LIBM}") + +set(FFMPEG_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/ffmpeg-install" CACHE INTERNAL "ffmpeg_dec libs") + +# Hot/cold split: GCC emits av_cold (init/setup) functions into .text.unlikely. +# We rename that to SOF's .cold (post-install, below) so the LLEXT loader places +# it in DRAM. -mtext-section-literals interleaves each function's Xtensa L32R +# literals into its text section (as SOF does for LLEXT module code) so the +# renamed section carries its literals and L32R stays in range. We deliberately +# do NOT use -ffunction-sections: keeping one .text.unlikely per object lets a +# single objcopy --rename-section catch it, and lets .cold be placed by SOF's +# existing orphan .cold handling (a hand-written linker script that forces .cold +# ahead of .text overruns the SRAM budget and overlaps .rodata for big codecs). +if(CONFIG_FFMPEG_DEC_COLD_SPLIT) + # -mlongcalls: cold code lives in DRAM (.cold, relocated by the loader) far + # from the hot SRAM .text, so calls between them exceed call8 range; longcalls + # emit an indirect L32R+CALLX with unlimited range. -mtext-section-literals + # keeps each function's literals in its own text section so the rename carries + # them. Coarse split: rename the whole FFmpeg .text (not just av_cold) into + # .cold so essentially all of libav* executes from DRAM, freeing SRAM. + set(_ff_extra_cflags "${_ff_extra_cflags} -mtext-section-literals -mlongcalls") + set(_ff_objcopy "${_ff_cross_prefix}objcopy") + set(_ff_cold_rename + COMMAND ${_ff_objcopy} --rename-section .text=.cold + ${FFMPEG_INSTALL_DIR}/lib/libavcodec.a + COMMAND ${_ff_objcopy} --rename-section .text=.cold + ${FFMPEG_INSTALL_DIR}/lib/libavutil.a + COMMAND ${_ff_objcopy} --rename-section .text=.cold + ${FFMPEG_INSTALL_DIR}/lib/libswresample.a) +endif() + +# --- 3b. MP3 encoder via libshine (fixed-point; FFmpeg has no native one) --- +set(_ff_cfg_env "PATH=${_tc_dir}:$ENV{PATH}") +set(_ff_shine_cfg "") +set(_ff_shine_dep "") +if(CONFIG_FFMPEG_ENC_MP3) + set(SHINE_SRC "${sof_top_dir}/../modules/audio/shine") + if(NOT EXISTS "${SHINE_SRC}/src/lib/layer3.h") + message(FATAL_ERROR "ffmpeg_dec: libshine source not at '${SHINE_SRC}'; run 'west update'") + endif() + set(SHINE_INSTALL "${CMAKE_CURRENT_BINARY_DIR}/shine-install") + file(MAKE_DIRECTORY "${SHINE_INSTALL}/lib/pkgconfig" "${SHINE_INSTALL}/include/shine") + # pkg-config file for FFmpeg's require_pkg_config libshine. + file(WRITE "${SHINE_INSTALL}/lib/pkgconfig/shine.pc" +"prefix=${SHINE_INSTALL} +libdir=\${prefix}/lib +includedir=\${prefix}/include +Name: shine +Description: Shine fixed-point MP3 encoder +Version: 3.1.1 +Libs: -L\${libdir} -lshine +Cflags: -I\${includedir} +") + # FFmpeg's -lshine link *test* pulls newlib malloc -> Zephyr runtime syms + # (z_errno_wrap, ...) that only exist at module load. Provide dummies so the + # configure test links; --extra-ldflags only affects tests, not the .a build. + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/shine_cfgstub.c" +"int z_errno_wrap(void){return 0;} +void *stdout; +int open(const char *p, int f, ...){return -1;} +void *sbrk(int i){return (void *)-1;} +") + # libshine's lib needs no config.h; build the objects directly and archive + # (its autotools CLI/shared link cannot work bare-metal). + file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/shine-build.sh" +"#!/bin/sh +set -e +export PATH=${_tc_dir}:\$PATH +mkdir -p ${CMAKE_CURRENT_BINARY_DIR}/shine-obj +for f in ${SHINE_SRC}/src/lib/*.c; do + ${CMAKE_C_COMPILER} -O2 -fPIC -mtext-section-literals -DSHINE_HAVE_BSWAP_H \\ + -I${SHINE_SRC}/src/lib -c \"\$f\" \\ + -o ${CMAKE_CURRENT_BINARY_DIR}/shine-obj/\$(basename \"\$f\").o +done +${_ff_cross_prefix}ar rcs ${SHINE_INSTALL}/lib/libshine.a ${CMAKE_CURRENT_BINARY_DIR}/shine-obj/*.o +cp ${SHINE_SRC}/src/lib/layer3.h ${SHINE_INSTALL}/include/shine/layer3.h +${CMAKE_C_COMPILER} -fPIC -c ${CMAKE_CURRENT_BINARY_DIR}/shine_cfgstub.c \\ + -o ${CMAKE_CURRENT_BINARY_DIR}/shine_cfgstub.o +") + add_custom_command( + OUTPUT "${SHINE_INSTALL}/lib/libshine.a" "${CMAKE_CURRENT_BINARY_DIR}/shine_cfgstub.o" + COMMAND sh "${CMAKE_CURRENT_BINARY_DIR}/shine-build.sh" + VERBATIM) + add_custom_target(shine_ext DEPENDS "${SHINE_INSTALL}/lib/libshine.a") + + set(_ff_cfg_env "PATH=${_tc_dir}:$ENV{PATH}" "PKG_CONFIG_PATH=${SHINE_INSTALL}/lib/pkgconfig") + set(_ff_shine_cfg --enable-libshine --enable-encoder=libshine --pkg-config=pkg-config + "--extra-ldflags=${CMAKE_CURRENT_BINARY_DIR}/shine_cfgstub.o") + set(_ff_shine_dep shine_ext) + message(STATUS "ffmpeg_dec: enabling MP3 encoder via libshine (${SHINE_SRC})") +endif() + +# --- 4. Configure + make + install (out-of-tree; source must be clean) --- +ExternalProject_Add(ffmpeg_ext + DEPENDS ${_ff_shine_dep} + SOURCE_DIR "${SOF_FFMPEG_SRC_DIR}" + BUILD_IN_SOURCE 0 + CONFIGURE_COMMAND + ${CMAKE_COMMAND} -E env ${_ff_cfg_env} + /configure + --prefix=${FFMPEG_INSTALL_DIR} + --enable-cross-compile --target-os=none --arch=xtensa + --cross-prefix=${_ff_cross_prefix} "--cc=${_ff_cc_ff}" "--ld=${_ff_ld}" + # NOTE: do NOT pass --disable-asm. FFmpeg treats every per-arch + # optimisation dir (incl. our C-intrinsic libavutil/xtensa/) as + # "asm"; --disable-asm forces arch=c and drops them. There is no + # Xtensa assembly, so leaving asm enabled is safe (HiFi kernels are + # C intrinsics) and is required to build ff_float_dsp_init_xtensa. + --disable-all --disable-everything --disable-autodetect + --disable-programs --disable-doc --disable-network + --disable-avformat --disable-avdevice ${_ff_avfilter_cfg} + --disable-swscale --disable-postproc + --disable-pthreads --disable-w32threads --disable-os2threads + --disable-runtime-cpudetect --disable-debug + --enable-avcodec --enable-avutil --enable-swresample + ${_ff_dec_cfg} + ${_ff_shine_cfg} + --enable-small --enable-pic + "--extra-cflags=${_ff_extra_cflags}" + BUILD_COMMAND + ${CMAKE_COMMAND} -E env "PATH=${_tc_dir}:$ENV{PATH}" make -j 8 + INSTALL_COMMAND make install + ${_ff_cold_rename} + BUILD_BYPRODUCTS + ${FFMPEG_INSTALL_DIR}/lib/libavcodec.a + ${FFMPEG_INSTALL_DIR}/lib/libavutil.a + ${FFMPEG_INSTALL_DIR}/lib/libswresample.a +) + +message(STATUS "ffmpeg_dec: cross-building FFmpeg decoders [${_ff_dec_csv}] from ${SOF_FFMPEG_SRC_DIR}") diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec-alloc.c b/src/audio/ffmpeg_dec/ffmpeg_dec-alloc.c new file mode 100644 index 000000000000..817ee06c480a --- /dev/null +++ b/src/audio/ffmpeg_dec/ffmpeg_dec-alloc.c @@ -0,0 +1,103 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// malloc/free/realloc for the ffmpeg_dec libavcodec backend, backed by the SOF +// module heap. libavcodec's av_malloc() bottoms out in libc malloc; the SOF core +// does not export malloc to LLEXT modules, so we provide it here and route it to +// mod_alloc_align()/mod_free() — no large static reservation, memory comes from +// the DSP heap like any other SOF module. +// +// Each block is prefixed with a small header storing its payload size so realloc +// can copy the old contents (SOF's allocator does not expose a realloc). The +// header is 16 bytes to preserve 16-byte alignment of the returned pointer. +// +// The active processing_module is bound at the entry of every backend op (init/ +// prepare/process/reset/free) via ffmpeg_dec_libc_bind(); decode is serialized +// per module on a pipeline core, so a single global binding is safe. + +#include +#include +#include +#include +#include "ffmpeg_dec.h" + +#define FFMPEG_DEC_ALLOC_HDR 16 + +static struct processing_module *ffmpeg_dec_alloc_mod; + +void ffmpeg_dec_libc_bind(struct processing_module *mod) +{ + ffmpeg_dec_alloc_mod = mod; +} + +void *malloc(size_t size) +{ + uint8_t *base; + + if (!ffmpeg_dec_alloc_mod || !size) + return NULL; + + base = mod_alloc_align(ffmpeg_dec_alloc_mod, size + FFMPEG_DEC_ALLOC_HDR, + FFMPEG_DEC_ALLOC_HDR); + if (!base) + return NULL; + + *(size_t *)base = size; + return base + FFMPEG_DEC_ALLOC_HDR; +} + +void free(void *ptr) +{ + if (!ptr || !ffmpeg_dec_alloc_mod) + return; + + mod_free(ffmpeg_dec_alloc_mod, (uint8_t *)ptr - FFMPEG_DEC_ALLOC_HDR); +} + +void *realloc(void *ptr, size_t size) +{ + size_t old_size; + void *new_ptr; + + if (!ptr) + return malloc(size); + + if (!size) { + free(ptr); + return NULL; + } + + old_size = *(size_t *)((uint8_t *)ptr - FFMPEG_DEC_ALLOC_HDR); + new_ptr = malloc(size); + if (new_ptr) { + memcpy(new_ptr, ptr, old_size < size ? old_size : size); + free(ptr); + } + + return new_ptr; +} + +void *calloc(size_t nmemb, size_t size) +{ + size_t total = nmemb * size; + void *ptr = malloc(total); + + if (ptr) + memset(ptr, 0, total); + return ptr; +} + +/* av_malloc may use posix_memalign. Our allocator returns 16-byte-aligned blocks, + * which is sufficient for the --disable-asm (no-SIMD) build; larger alignment + * requests are satisfied at 16 bytes. */ +int posix_memalign(void **memptr, size_t alignment, size_t size) +{ + void *ptr = malloc(size); + + (void)alignment; + if (!ptr) + return 12; /* ENOMEM */ + *memptr = ptr; + return 0; +} diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec-builtin-libc.c b/src/audio/ffmpeg_dec/ffmpeg_dec-builtin-libc.c new file mode 100644 index 000000000000..1b4a74894f63 --- /dev/null +++ b/src/audio/ffmpeg_dec/ffmpeg_dec-builtin-libc.c @@ -0,0 +1,95 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Built-in (=y) libc surface for the ffmpeg_dec libavcodec backend. +// +// The LLEXT (=m) build uses ffmpeg_dec-shims.c, which defines a full private +// libc (string/stdio/stdlib/snprintf/...) because an isolated module cannot +// import the core's libc. A built-in (=y) image links against Zephyr's common +// libc, which already provides malloc/free/calloc/realloc (a real sys_heap over +// all remaining SRAM on Intel ADSP), snprintf, memcpy, str*, strto*, bsearch, +// etc. -- all whole-archived. Re-defining those here would multiply-define. +// +// So this file provides ONLY the symbols libavcodec/libavutil reference that +// Zephyr's common/minimal libc does NOT export, and nothing else. These are the +// exact unresolved references from the base-image link: +// +// * stdio / time / env (stderr, fread, fclose, fdopen, open, setvbuf, sscanf, +// getenv, clock, gmtime, mktime, strftime): NEVER used on a SOF DSP nor on +// the FLAC decode path -> no-op stubs. av_log()'s stderr path is bypassed by +// the Zephyr LOG wrapper installed in ffmpeg_dec-ffmpeg.c. +// * strtod: not on the FLAC decode path -> return-0 stub. +// * __errno: the double libm libav* references (acos/sin/exp/log/pow/scalbn/ +// __isinfd/...) is NOT stubbed here -- it is the real newlib math for this +// core, compiled from vendored newlib source with -mlongcalls into +// FFMPEG_TARGET_LIBM and linked in CMakeLists.txt (see ffmpeg.cmake 3c + +// libm/README). Those kernels report domain/range +// errors through *__errno(); it is the only libc-side symbol they need that +// the image doesn't already provide (the soft-float builtins __adddf3/ +// __muldf3/... and memset are already linked), so bridge it to Zephyr's real +// per-thread errno below. This replaces the former return-0 libm stubs, so +// lossy codecs (AAC/Opus/Vorbis) now decode with correct math, not garbage. +// * z_errno_wrap: a Zephyr libc runtime errno wrapper referenced by +// libavutil/avsscanf; harmless stub (errno unused on the decode path). +// * ffmpeg_dec_libc_bind: in the =y build av_malloc() bottoms out in the +// common-libc malloc backed by the DSP SRAM heap, so no per-module heap +// binding is needed -> no-op (the =m build routes it to the module heap in +// ffmpeg_dec-alloc.c). +// +// As in ffmpeg_dec-shims.c, this file intentionally includes NO libc headers so +// these definitions cannot clash with newlib prototypes; only the symbol names +// matter to the linker. + +#include + +#ifdef __clang__ +#pragma clang diagnostic ignored "-Wincompatible-library-redeclaration" +#pragma clang diagnostic ignored "-Wbuiltin-requires-header" +#endif + +struct processing_module; + +/* ============================ stdio / time / env ========================== */ + +/* stderr is referenced as a data object by FFmpeg's default log path. */ +void *stderr; + +unsigned long fread(void *p, unsigned long sz, unsigned long n, void *stream) +{ (void)p; (void)sz; (void)n; (void)stream; return 0; } +int fclose(void *stream) { (void)stream; return 0; } +void *fdopen(int fd, const char *mode) { (void)fd; (void)mode; return NULL; } +int open(const char *path, int flags, ...) { (void)path; (void)flags; return -1; } +int setvbuf(void *s, char *buf, int mode, size_t size) +{ (void)s; (void)buf; (void)mode; (void)size; return 0; } +int sscanf(const char *str, const char *fmt, ...) { (void)str; (void)fmt; return 0; } +char *getenv(const char *name) { (void)name; return NULL; } +unsigned long clock(void) { return 0; } +void *gmtime(const void *timep) { (void)timep; return NULL; } +long mktime(void *tm) { (void)tm; return -1; } +size_t strftime(char *s, size_t max, const char *fmt, const void *tm) +{ (void)fmt; (void)tm; if (max) s[0] = '\0'; return 0; } +double strtod(const char *nptr, char **endptr) +{ if (endptr) *endptr = (char *)nptr; return 0; } + +/* Zephyr libc runtime errno wrapper referenced by libavutil/avsscanf. */ +int z_errno_wrap(void) { return 0; } + +/* ================================== libm =================================== */ +/* + * The double libm itself is the real newlib libm.a for this core (linked in + * CMakeLists.txt) -- no math stubs live here. newlib's kernels record domain/ + * range errors through *__errno(); it is the only libm-side symbol the base image + * lacks. Bridge it to Zephyr's genuine per-thread errno (z_impl_z_errno, the + * implementation behind the z_errno syscall) so those writes land in the calling + * thread's errno rather than a shared dummy. The decode path never reads errno, + * but the reference must resolve for libm.a to link. + */ +extern int *z_impl_z_errno(void); +int *__errno(void) { return z_impl_z_errno(); } + +/* ============================ module heap binding ========================== */ +/* No-op: the =y build allocates from common-libc malloc (DSP SRAM heap), so no + * per-module heap binding is needed (cf. ffmpeg_dec-alloc.c in the =m build). + */ +void ffmpeg_dec_libc_bind(struct processing_module *mod) { (void)mod; } diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec-encode.c b/src/audio/ffmpeg_dec/ffmpeg_dec-encode.c new file mode 100644 index 000000000000..69d2d14807b1 --- /dev/null +++ b/src/audio/ffmpeg_dec/ffmpeg_dec-encode.c @@ -0,0 +1,207 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// libavcodec encode backend for the ffmpeg_dec module (encode mode): PCM in, +// compressed elementary stream out (MP3 via libshine). It is the mirror of the +// decoder path - avcodec_send_frame / avcodec_receive_packet instead of +// send_packet / receive_frame. +// +// SOF PCM is interleaved S32; the encoder is opened in whatever sample format it +// supports (libshine: S16), so S32 is converted to the encoder's format per +// frame. MP3 uses fixed 1152-sample frames; this feeds one encoder frame worth +// of input per process call when enough is available. + +#include +#include +#include +#include +#include +#include "ffmpeg_dec.h" + +#include +#include +#include + +LOG_MODULE_DECLARE(ffmpeg_dec, CONFIG_SOF_LOG_LEVEL); + +#define FFMPEG_ENC_DEFAULT_BITRATE 128000 + +struct ffmpeg_enc_data { + const AVCodec *codec; + AVCodecContext *avctx; + AVPacket *pkt; + AVFrame *frame; + int frame_bytes; /* SOF S32 input frame size (all channels) */ +}; + +int ffmpeg_enc_mod_init(struct processing_module *mod) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + struct comp_dev *dev = mod->dev; + struct ffmpeg_enc_data *e; + + ffmpeg_dec_libc_bind(mod); + + e = mod_zalloc(mod, sizeof(*e)); + if (!e) + return -ENOMEM; + + /* libshine is the fixed-point MP3 encoder built into the archive. */ + e->codec = avcodec_find_encoder_by_name("libshine"); + if (!e->codec) { + comp_err(dev, "libshine MP3 encoder not found"); + mod_free(mod, e); + return -ENODEV; + } + + e->avctx = avcodec_alloc_context3(e->codec); + e->pkt = av_packet_alloc(); + e->frame = av_frame_alloc(); + if (!e->avctx || !e->pkt || !e->frame) { + if (e->frame) + av_frame_free(&e->frame); + if (e->pkt) + av_packet_free(&e->pkt); + if (e->avctx) + avcodec_free_context(&e->avctx); + mod_free(mod, e); + return -ENOMEM; + } + + cd->backend_data = e; + return 0; +} + +int ffmpeg_enc_mod_prepare(struct processing_module *mod, + struct sof_source **sources, int num_of_sources, + struct sof_sink **sinks, int num_of_sinks) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + struct ffmpeg_enc_data *e = cd->backend_data; + struct comp_dev *dev = mod->dev; + int ret; + + if (num_of_sources != 1) + return -EINVAL; + + ffmpeg_dec_libc_bind(mod); + + cd->out_channels = source_get_channels(sources[0]); + cd->out_rate = source_get_rate(sources[0]); + e->frame_bytes = source_get_frame_bytes(sources[0]); + + e->avctx->sample_rate = cd->out_rate; + av_channel_layout_default(&e->avctx->ch_layout, cd->out_channels); + /* Use the encoder's native sample format (libshine: S16). */ + e->avctx->sample_fmt = e->codec->sample_fmts ? + e->codec->sample_fmts[0] : AV_SAMPLE_FMT_S16; + e->avctx->bit_rate = FFMPEG_ENC_DEFAULT_BITRATE; + e->avctx->thread_count = 1; + + ret = avcodec_open2(e->avctx, e->codec, NULL); + if (ret < 0) { + comp_err(dev, "avcodec_open2 (encoder) failed %d", ret); + return -EIO; + } + + comp_info(dev, "ffmpeg_enc: libshine MP3, rate %u ch %u frame_size %d", + cd->out_rate, cd->out_channels, e->avctx->frame_size); + return 0; +} + +/* Convert one interleaved S32 frame block to the encoder AVFrame, then encode. */ +int ffmpeg_enc_mod_process(struct processing_module *mod, + struct input_stream_buffer *input_buffers, int num_input_buffers, + struct output_stream_buffer *output_buffers, int num_output_buffers) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + struct ffmpeg_enc_data *e = cd->backend_data; + struct comp_dev *dev = mod->dev; + int ch = cd->out_channels; + int nb = e->avctx->frame_size ? e->avctx->frame_size : 1152; + const int32_t *in; + uint8_t *out = output_buffers[0].data; + size_t out_off = 0; + int planar, i, c, ret; + + if (num_input_buffers != 1 || num_output_buffers != 1) + return -EINVAL; + + ffmpeg_dec_libc_bind(mod); + + /* Need a full encoder frame of input. */ + if (input_buffers[0].size < (uint32_t)(nb * e->frame_bytes)) + return -ENODATA; + + e->frame->nb_samples = nb; + e->frame->format = e->avctx->sample_fmt; + e->frame->sample_rate = cd->out_rate; + av_channel_layout_copy(&e->frame->ch_layout, &e->avctx->ch_layout); + if (av_frame_get_buffer(e->frame, 0) < 0) + return -ENOMEM; + + /* S32 interleaved -> S16 (planar or packed per the encoder format). */ + in = input_buffers[0].data; + planar = av_sample_fmt_is_planar(e->frame->format); + for (i = 0; i < nb; i++) + for (c = 0; c < ch; c++) { + int16_t s = (int16_t)(in[i * ch + c] >> 16); + + if (planar) + ((int16_t *)e->frame->data[c])[i] = s; + else + ((int16_t *)e->frame->data[0])[i * ch + c] = s; + } + input_buffers[0].consumed = nb * e->frame_bytes; + + ret = avcodec_send_frame(e->avctx, e->frame); + av_frame_unref(e->frame); + if (ret < 0) { + comp_err(dev, "avcodec_send_frame failed %d", ret); + return -EIO; + } + + /* Drain compressed packets into the output buffer. */ + while (ret >= 0) { + ret = avcodec_receive_packet(e->avctx, e->pkt); + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) + break; + if (ret < 0) { + comp_err(dev, "avcodec_receive_packet failed %d", ret); + return -EIO; + } + if (out_off + e->pkt->size <= output_buffers[0].size) { + memcpy_s(out + out_off, output_buffers[0].size - out_off, + e->pkt->data, e->pkt->size); + out_off += e->pkt->size; + } else { + comp_warn(dev, "output full, MP3 packet dropped"); + } + av_packet_unref(e->pkt); + ret = 0; + } + + output_buffers[0].size = out_off; + return 0; +} + +int ffmpeg_enc_mod_free(struct processing_module *mod) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + struct ffmpeg_enc_data *e = cd->backend_data; + + ffmpeg_dec_libc_bind(mod); + if (!e) + return 0; + + if (e->frame) + av_frame_free(&e->frame); + if (e->pkt) + av_packet_free(&e->pkt); + if (e->avctx) + avcodec_free_context(&e->avctx); + mod_free(mod, e); + cd->backend_data = NULL; + return 0; +} diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec-ffmpeg.c b/src/audio/ffmpeg_dec/ffmpeg_dec-ffmpeg.c new file mode 100644 index 000000000000..ced3cb8023f0 --- /dev/null +++ b/src/audio/ffmpeg_dec/ffmpeg_dec-ffmpeg.c @@ -0,0 +1,461 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// libavcodec decode backend for the ffmpeg_dec module. +// +// Drives the modern send-packet / receive-frame libavcodec API standalone (no +// libavformat, no file I/O). A raw compressed elementary stream is parsed on +// the DSP with an AVCodecParser (av_parser_parse2), each recovered frame is fed +// to avcodec_send_packet, and decoded PCM is drained with avcodec_receive_frame +// and interleaved into the SOF output buffer. +// +// Requires pre-compiled libavcodec / libavutil / libswresample static libraries +// and headers for the target under third_party/ (see the Phase 0 build). It is +// only compiled when CONFIG_COMP_FFMPEG_DEC_STUB is not selected. + +#include +#include +#include +#include +#include +#include +#include "ffmpeg_dec.h" + +#include +#include +#include +#include +#include + +LOG_MODULE_DECLARE(ffmpeg_dec, CONFIG_SOF_LOG_LEVEL); + +/* Padding libavcodec's bitstream readers over-read past the end of a packet. */ +#ifndef AV_INPUT_BUFFER_PADDING_SIZE +#define AV_INPUT_BUFFER_PADDING_SIZE 64 +#endif + +/* + * Route FFmpeg's av_log() into the SOF/Zephyr logging subsystem instead of its + * default callback (which writes to stderr via fprintf). The line buffer is + * static: decode is single-threaded, and a static buffer avoids handing Zephyr's + * deferred logging a pointer into a stack frame that has since been unwound. + */ +static char ffmpeg_dec_log_line[160]; + +static void ffmpeg_dec_av_log(void *avcl, int level, const char *fmt, va_list vl) +{ + int n; + + if (level > av_log_get_level()) + return; + + n = vsnprintf(ffmpeg_dec_log_line, sizeof(ffmpeg_dec_log_line), fmt, vl); + if (n <= 0) + return; + + /* Trim the trailing newline FFmpeg conventionally appends. */ + if (n < (int)sizeof(ffmpeg_dec_log_line) && ffmpeg_dec_log_line[n - 1] == '\n') + ffmpeg_dec_log_line[n - 1] = '\0'; + + if (level <= AV_LOG_ERROR) { + /* A stalled/hostile stream (e.g. the silence padding a compress + * host emits after the file ends) can make libavcodec log the + * same error every DSP cycle. Collapse identical repeats so the + * trace ring keeps room for other messages. + */ + static char last[64]; + static int reps; + + if (!strncmp(last, ffmpeg_dec_log_line, sizeof(last) - 1)) { + if (++reps > 3) + return; + } else { + reps = 0; + strncpy(last, ffmpeg_dec_log_line, sizeof(last) - 1); + } + LOG_ERR("ffmpeg: %s", ffmpeg_dec_log_line); + } else if (level <= AV_LOG_WARNING) + LOG_WRN("ffmpeg: %s", ffmpeg_dec_log_line); + else if (level <= AV_LOG_INFO) + LOG_INF("ffmpeg: %s", ffmpeg_dec_log_line); + else + LOG_DBG("ffmpeg: %s", ffmpeg_dec_log_line); +} + +/** + * struct ffmpeg_dec_ffmpeg_data - libavcodec backend private state. + * @avctx: Decoder context. + * @parser: Elementary-stream frame parser. + * @pkt: Reusable packet handed to the decoder. + * @frame: Reusable decoded PCM frame. + * @pktbuf: Padded scratch buffer for parsed packet payloads. + * @codec: Resolved AVCodec for the selected codec id. + */ +struct ffmpeg_dec_ffmpeg_data { + AVCodecContext *avctx; + AVCodecParserContext *parser; + AVPacket *pkt; + AVFrame *frame; + uint8_t *pktbuf; + const AVCodec *codec; +}; + +/* Map the SOF codec enum to a libavcodec decoder id. */ +static enum AVCodecID ffmpeg_dec_av_codec_id(enum ffmpeg_dec_codec codec) +{ + switch (codec) { +#if defined(CONFIG_FFMPEG_DEC_FLAC) + case FFMPEG_DEC_CODEC_FLAC: + return AV_CODEC_ID_FLAC; +#endif +#if defined(CONFIG_FFMPEG_DEC_AAC) + case FFMPEG_DEC_CODEC_AAC: + return AV_CODEC_ID_AAC; +#endif +#if defined(CONFIG_FFMPEG_DEC_OPUS) + case FFMPEG_DEC_CODEC_OPUS: + return AV_CODEC_ID_OPUS; +#endif +#if defined(CONFIG_FFMPEG_DEC_MP3) + case FFMPEG_DEC_CODEC_MP3: + return AV_CODEC_ID_MP3; +#endif + default: + return AV_CODEC_ID_NONE; + } +} + +static int ffmpeg_dec_ff_init(struct processing_module *mod) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + struct comp_dev *dev = mod->dev; + struct ffmpeg_dec_ffmpeg_data *ff; + enum AVCodecID id; + + /* Bind the heap that backs FFmpeg's malloc before any av_malloc runs. */ + ffmpeg_dec_libc_bind(mod); + + /* Redirect libavcodec logging into SOF/Zephyr before anything can log. */ + av_log_set_level(AV_LOG_ERROR); + av_log_set_callback(ffmpeg_dec_av_log); + + ff = mod_zalloc(mod, sizeof(*ff)); + if (!ff) + return -ENOMEM; + + id = ffmpeg_dec_av_codec_id(cd->codec); + ff->codec = avcodec_find_decoder(id); + if (!ff->codec) { + comp_err(dev, "no libavcodec decoder for codec %d", cd->codec); + mod_free(mod, ff); + return -ENODEV; + } + + ff->parser = av_parser_init(ff->codec->id); + ff->avctx = avcodec_alloc_context3(ff->codec); + ff->pkt = av_packet_alloc(); + ff->frame = av_frame_alloc(); + if (!ff->avctx || !ff->pkt || !ff->frame) { + comp_err(dev, "libavcodec object allocation failed"); + goto err; + } + comp_info(dev, "ff_init: libavcodec ready (%s)", ff->codec->name); + + cd->backend_data = ff; + return 0; + +err: + if (ff->frame) + av_frame_free(&ff->frame); + if (ff->pkt) + av_packet_free(&ff->pkt); + if (ff->avctx) + avcodec_free_context(&ff->avctx); + if (ff->parser) + av_parser_close(ff->parser); + mod_free(mod, ff); + return -ENOMEM; +} + +static int ffmpeg_dec_ff_configure(struct processing_module *mod) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + struct ffmpeg_dec_ffmpeg_data *ff = cd->backend_data; + struct comp_dev *dev = mod->dev; + int ret; + + ffmpeg_dec_libc_bind(mod); + + /* Some codecs (FLAC, Opus, Vorbis, AAC-in-MP4) need their setup header + * in avctx->extradata before avcodec_open2(). + */ + if (cd->extradata && cd->extradata_size) { + av_freep(&ff->avctx->extradata); + ff->avctx->extradata = + av_mallocz(cd->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE); + if (!ff->avctx->extradata) + return -ENOMEM; + memcpy_s(ff->avctx->extradata, cd->extradata_size, + cd->extradata, cd->extradata_size); + ff->avctx->extradata_size = cd->extradata_size; + } + + /* Force single-threaded decode: no pthreads, no frame-threading latency, + * and get_buffer2 need not be thread-safe. + */ + ff->avctx->thread_count = 1; + + /* avcodec_alloc_context3() does not apply the AVOption defaults on this + * cut-down static build, so max_samples is left zero-initialised. That + * makes ff_get_buffer() reject every audio frame ("samples per frame N + * exceeds max_samples 0", -EINVAL). Restore the upstream default. + */ + if (ff->avctx->max_samples <= 0) + ff->avctx->max_samples = INT_MAX; + + ret = avcodec_open2(ff->avctx, ff->codec, NULL); + if (ret < 0) { + comp_err(dev, "avcodec_open2 failed %d", ret); + return -EIO; + } + + /* Padded scratch for a parsed packet payload, sized to OBS as a bound. */ + ff->pktbuf = mod_alloc(mod, cd->out_frame_bytes ? + cd->out_frame_bytes * 4096 : 65536); + if (!ff->pktbuf) + return -ENOMEM; + + return 0; +} + +/* + * Convert one decoded source sample to a signed Q1.31 value (full-scale in a + * 32-bit word), so it can be re-quantised to any SOF sink format below. The + * integer paths are what the fixed-point decoders built today exercise (mp3 -> + * S16, 16-bit FLAC -> S16, 24-bit FLAC -> S32); the float paths cover the + * planar-float decoders (AAC, Opus) added later. + */ +static inline int32_t ffmpeg_dec_src_to_q31(const uint8_t *p, int fmt) +{ + switch (fmt) { + case AV_SAMPLE_FMT_U8: + case AV_SAMPLE_FMT_U8P: + return ((int32_t)*p - 0x80) << 24; + case AV_SAMPLE_FMT_S16: + case AV_SAMPLE_FMT_S16P: + return (int32_t)*(const int16_t *)p << 16; + case AV_SAMPLE_FMT_S32: + case AV_SAMPLE_FMT_S32P: + return *(const int32_t *)p; + case AV_SAMPLE_FMT_FLT: + case AV_SAMPLE_FMT_FLTP: { + float f = *(const float *)p; + + if (f >= 1.0f) + return INT32_MAX; + if (f <= -1.0f) + return INT32_MIN; + return (int32_t)(f * 2147483648.0f); + } + default: + return 0; + } +} + +/* Store a Q1.31 sample into @p in the SOF sink frame format @fmt. */ +static inline void ffmpeg_dec_q31_to_sink(uint8_t *p, int32_t v, int fmt) +{ + switch (fmt) { + case SOF_IPC_FRAME_S16_LE: + *(int16_t *)p = (int16_t)(v >> 16); + break; + case SOF_IPC_FRAME_S24_4LE: + *(int32_t *)p = v >> 8; /* 24-bit, sign-extended in 32 */ + break; + case SOF_IPC_FRAME_FLOAT: + *(float *)p = (float)v / 2147483648.0f; + break; + case SOF_IPC_FRAME_S32_LE: + default: + *(int32_t *)p = v; + break; + } +} + +/* + * Convert and interleave one decoded AVFrame into @out. Returns bytes written, + * or a negative errno if @out cannot hold the frame. + * + * The decoder's native sample format need not match the negotiated sink: the + * SOF IPC4 pipeline propagates the PCM (sink) bit depth back onto this widget's + * output pin, so e.g. the fixed-point mp3 decoder (S16) must be re-quantised to + * an S32 sink. Writing the raw decoder samples straight into an S32 sink packs + * two S16 samples into each S32 slot -> constant harmonic distortion. Convert + * every sample through a Q1.31 intermediate instead of assuming source == sink. + */ +static int ffmpeg_dec_emit_frame(struct processing_module *mod, AVFrame *frame, + uint8_t *out, size_t out_size) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + int channels = cd->out_channels; + int src_bps = av_get_bytes_per_sample(frame->format); + int planar = av_sample_fmt_is_planar(frame->format); + int sink_bps = channels ? (int)(cd->out_frame_bytes / channels) : 0; + size_t need = (size_t)frame->nb_samples * channels * sink_bps; + int i, ch; + + if (src_bps <= 0 || sink_bps <= 0 || channels <= 0) + return -EINVAL; + if (need > out_size) + return -ENOSPC; + + for (i = 0; i < frame->nb_samples; i++) { + for (ch = 0; ch < channels; ch++) { + const uint8_t *src = planar ? + frame->data[ch] + (size_t)i * src_bps : + frame->data[0] + ((size_t)i * channels + ch) * src_bps; + int32_t q = ffmpeg_dec_src_to_q31(src, frame->format); + + ffmpeg_dec_q31_to_sink(out + ((size_t)i * channels + ch) * sink_bps, + q, cd->out_frame_fmt); + } + } + + return need; +} + +static int ffmpeg_dec_ff_decode(struct processing_module *mod, + const uint8_t *in, size_t in_size, size_t *consumed, + uint8_t *out, size_t out_size, size_t *produced) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + struct ffmpeg_dec_ffmpeg_data *ff = cd->backend_data; + struct comp_dev *dev = mod->dev; + size_t out_off = 0; + int used; + int ret; + + ffmpeg_dec_libc_bind(mod); + + *consumed = 0; + *produced = 0; + + /* Parse one frame out of the raw stream. */ + used = av_parser_parse2(ff->parser, ff->avctx, + &ff->pkt->data, &ff->pkt->size, + in, in_size, + AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0); + if (used < 0) { + comp_err(dev, "av_parser_parse2 failed %d", used); + return -EIO; + } + *consumed = used; + + /* Parser still buffering: no complete frame yet. */ + if (ff->pkt->size == 0) + return 0; + + /* + * FFmpeg decoders read AV_INPUT_BUFFER_PADDING_SIZE bytes past the + * packet payload (the bitstream reader over-reads its word cache). The + * parser can hand back a packet pointing into the unpadded host input, + * so copy the payload into the padded scratch and zero the tail before + * decoding. + */ + { + size_t pktbuf_sz = cd->out_frame_bytes ? + (size_t)cd->out_frame_bytes * 4096 : 65536; + + if ((size_t)ff->pkt->size + AV_INPUT_BUFFER_PADDING_SIZE <= pktbuf_sz) { + memcpy_s(ff->pktbuf, pktbuf_sz, ff->pkt->data, ff->pkt->size); + memset(ff->pktbuf + ff->pkt->size, 0, + AV_INPUT_BUFFER_PADDING_SIZE); + ff->pkt->data = ff->pktbuf; + } + } + + ret = avcodec_send_packet(ff->avctx, ff->pkt); + if (ret < 0) { + comp_err(dev, "avcodec_send_packet failed %d", ret); + return -EIO; + } + + /* Drain all frames this packet produced. */ + while (ret >= 0) { + ret = avcodec_receive_frame(ff->avctx, ff->frame); + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) + break; + if (ret < 0) { + comp_err(dev, "avcodec_receive_frame failed %d", ret); + return -EIO; + } + + ret = ffmpeg_dec_emit_frame(mod, ff->frame, out + out_off, + out_size - out_off); + if (ret < 0) { + comp_warn(dev, "output buffer full, PCM dropped"); + break; + } + out_off += ret; + ret = 0; + } + + *produced = out_off; + return 0; +} + +static int ffmpeg_dec_ff_reset(struct processing_module *mod) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + struct ffmpeg_dec_ffmpeg_data *ff = cd->backend_data; + + ffmpeg_dec_libc_bind(mod); + + /* + * The pipeline may issue COMP_TRIGGER_RESET before prepare() has run + * ffmpeg_dec_ff_configure()/avcodec_open2(), so avctx->internal is still + * NULL. avcodec_flush_buffers() -> ff_decode_flush_buffers() dereferences + * that internal state unconditionally and faults (EXCCAUSE 13) on a + * not-yet-opened context. Only flush once the decoder is actually open. + */ + if (ff && ff->avctx && avcodec_is_open(ff->avctx)) + avcodec_flush_buffers(ff->avctx); + + return 0; +} + +static int ffmpeg_dec_ff_free(struct processing_module *mod) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + struct ffmpeg_dec_ffmpeg_data *ff = cd->backend_data; + + ffmpeg_dec_libc_bind(mod); + + if (!ff) + return 0; + + if (ff->frame) + av_frame_free(&ff->frame); + if (ff->pkt) + av_packet_free(&ff->pkt); + if (ff->avctx) + avcodec_free_context(&ff->avctx); + if (ff->parser) + av_parser_close(ff->parser); + if (ff->pktbuf) + mod_free(mod, ff->pktbuf); + + mod_free(mod, ff); + cd->backend_data = NULL; + return 0; +} + +const struct ffmpeg_dec_backend ffmpeg_dec_backend = { + .name = "libavcodec", + .init = ffmpeg_dec_ff_init, + .configure = ffmpeg_dec_ff_configure, + .decode = ffmpeg_dec_ff_decode, + .reset = ffmpeg_dec_ff_reset, + .free = ffmpeg_dec_ff_free, +}; diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec-filter.c b/src/audio/ffmpeg_dec/ffmpeg_dec-filter.c new file mode 100644 index 000000000000..33d91afce516 --- /dev/null +++ b/src/audio/ffmpeg_dec/ffmpeg_dec-filter.c @@ -0,0 +1,367 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// libavfilter graph backend for the ffmpeg_dec module: runs an FFmpeg audio +// filter (e.g. afftdn FFT noise reduction) over PCM. Unlike the decoder path +// (send_packet/receive_frame), filters use the avfilter graph API: +// +// abuffer(src) -> -> abuffersink(sink) +// +// PCM AVFrames are pushed into the source, pulled filtered from the sink. This +// is a PCM->PCM effect path, distinct from the compressed->PCM decode path; it +// is only built when a filter is selected (CONFIG_FFMPEG_FILTER_*), which pulls +// libavfilter into the module. + +#include +#include +#include +#include +#include "ffmpeg_dec.h" + +#include +#include +#include +#include +#include +#include + +LOG_MODULE_DECLARE(ffmpeg_dec, CONFIG_SOF_LOG_LEVEL); + +/** + * struct ffmpeg_af_graph - one audio filter graph instance. + * @graph: The avfilter graph. + * @src: abuffer source context (frames pushed here). + * @sink: abuffersink context (filtered frames pulled here). + */ +struct ffmpeg_af_graph { + AVFilterGraph *graph; + AVFilterContext *src; + AVFilterContext *sink; +}; + +/** + * ffmpeg_af_open() - Build a src -> -> sink graph for the given PCM + * format. + * @g: Graph to initialise. + * @filter: Filter name, e.g. "afftdn". + * @rate: Sample rate (Hz). + * @channels: Channel count. + * @fmt: Interleaved AV sample format (e.g. AV_SAMPLE_FMT_S32). + * + * Return: Zero on success, negative errno otherwise. + */ +int ffmpeg_af_open(struct ffmpeg_af_graph *g, const char *filter, + int rate, int channels, enum AVSampleFormat fmt) +{ + const AVFilter *abuffer = avfilter_get_by_name("abuffer"); + const AVFilter *abuffersink = avfilter_get_by_name("abuffersink"); + const AVFilter *filt = avfilter_get_by_name(filter); + AVFilterContext *filt_ctx = NULL; + AVChannelLayout layout; + char chbuf[64]; + char args[256]; + int ret; + + memset(g, 0, sizeof(*g)); + if (!abuffer || !abuffersink || !filt) + return -ENODEV; + + g->graph = avfilter_graph_alloc(); + if (!g->graph) + return -ENOMEM; + + av_channel_layout_default(&layout, channels); + av_channel_layout_describe(&layout, chbuf, sizeof(chbuf)); + + snprintf(args, sizeof(args), + "time_base=1/%d:sample_rate=%d:sample_fmt=%s:channel_layout=%s", + rate, rate, av_get_sample_fmt_name(fmt), chbuf); + + ret = avfilter_graph_create_filter(&g->src, abuffer, "in", args, NULL, g->graph); + if (ret < 0) + goto err; + ret = avfilter_graph_create_filter(&filt_ctx, filt, "filter", NULL, NULL, g->graph); + if (ret < 0) + goto err; + ret = avfilter_graph_create_filter(&g->sink, abuffersink, "out", NULL, NULL, g->graph); + if (ret < 0) + goto err; + + ret = avfilter_link(g->src, 0, filt_ctx, 0); + if (!ret) + ret = avfilter_link(filt_ctx, 0, g->sink, 0); + if (ret < 0) + goto err; + + ret = avfilter_graph_config(g->graph, NULL); + if (ret < 0) + goto err; + + return 0; + +err: + avfilter_graph_free(&g->graph); + return -EIO; +} + +/** + * ffmpeg_af_filter() - Push one PCM frame through the graph, pull the result. + * @g: Graph. + * @in: Input PCM frame. + * @out: Output frame to receive the filtered PCM. + * + * Return: 0 on a produced frame, -EAGAIN if the filter needs more input, + * negative errno on error. + */ +int ffmpeg_af_filter(struct ffmpeg_af_graph *g, AVFrame *in, AVFrame *out) +{ + int ret = av_buffersrc_add_frame(g->src, in); + + if (ret < 0) + return -EIO; + + ret = av_buffersink_get_frame(g->sink, out); + if (ret == AVERROR(EAGAIN)) + return -EAGAIN; + if (ret < 0) + return -EIO; + + return 0; +} + +void ffmpeg_af_close(struct ffmpeg_af_graph *g) +{ + if (g->graph) + avfilter_graph_free(&g->graph); +} + +#if CONFIG_FFMPEG_DEC_FILTER_MODE +/* + * Filter-mode SOF module: a PCM source->sink effect that runs the graph + * (default afftdn). SOF PCM is interleaved S32; afftdn works on float planar + * (FLTP), so we deinterleave+normalize S32 -> float on the way in and the + * reverse on the way out. Bounded chunk per cycle. + * + * NOTE: afftdn has internal latency/framing, so produced samples per cycle may + * differ from consumed; the structure below drives the graph correctly but + * real-time latency/underrun tuning is left for on-hardware bring-up. + */ + +#include +#include +#include + +/* + * On HiFi4/HiFi5 VFPU cores our LLVM Xtensa clang provides packed int<->float + * conversion intrinsics (float.sx2 / trunc.sx2), each converting two lanes per + * op. float.sx2(x, 31) yields (float)x * 2^-31 (fusing the /2^31 normalize) and + * trunc.sx2(f, 31) yields (int32_t)(f * 2^31) with int32 saturation (replacing + * the manual clamp). Fall back to scalar C on GCC and on non-VFPU cores. + */ +#if defined(__XTENSA__) && defined(__has_include) +#if __has_include() +#include +#endif +#if defined(__has_builtin) && __has_builtin(__builtin_xtensa_float_sx2) && \ + defined(XCHAL_HAVE_HIFI4_VFPU) && XCHAL_HAVE_HIFI4_VFPU && \ + __has_include() +#include +#define FFMPEG_AF_VFPU_CONV 1 +#endif +#endif + +#define FFMPEG_AF_MAX_CHUNK 4096 +#define FFMPEG_AF_S32_SCALE 2147483648.0f /* 2^31 */ +#ifndef CONFIG_FFMPEG_AF_FILTER_NAME +#define CONFIG_FFMPEG_AF_FILTER_NAME "afftdn" +#endif + +int ffmpeg_af_mod_init(struct processing_module *mod) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + + ffmpeg_dec_libc_bind(mod); + cd->af_graph = mod_zalloc(mod, sizeof(struct ffmpeg_af_graph)); + return cd->af_graph ? 0 : -ENOMEM; +} + +int ffmpeg_af_mod_prepare(struct processing_module *mod, + struct sof_source **sources, int num_of_sources, + struct sof_sink **sinks, int num_of_sinks) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + struct comp_dev *dev = mod->dev; + + if (num_of_sources != 1 || num_of_sinks != 1) + return -EINVAL; + + ffmpeg_dec_libc_bind(mod); + cd->out_rate = source_get_rate(sources[0]); + cd->out_channels = source_get_channels(sources[0]); + cd->out_frame_bytes = source_get_frame_bytes(sources[0]); + + comp_info(dev, "ffmpeg_af: %s, rate %u ch %u", + CONFIG_FFMPEG_AF_FILTER_NAME, cd->out_rate, cd->out_channels); + + /* Graph runs in float planar; the module converts to/from S32. */ + return ffmpeg_af_open(cd->af_graph, CONFIG_FFMPEG_AF_FILTER_NAME, + cd->out_rate, cd->out_channels, AV_SAMPLE_FMT_FLTP); +} + +int ffmpeg_af_mod_process(struct processing_module *mod, + struct sof_source **sources, int num_of_sources, + struct sof_sink **sinks, int num_of_sinks) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + struct sof_source *source = sources[0]; + struct sof_sink *sink = sinks[0]; + int ch = cd->out_channels; + int in_frames = source_get_data_frames_available(source); + int n = MIN(in_frames, FFMPEG_AF_MAX_CHUNK); + AVFrame *in, *out; + const int32_t *src; + int32_t *dst; + int i, c, m, ret; + + ffmpeg_dec_libc_bind(mod); + if (n <= 0) + return 0; + + /* Build the FLTP input frame (deinterleave + normalize S32 -> float). */ + in = av_frame_alloc(); + if (!in) + return -ENOMEM; + in->format = AV_SAMPLE_FMT_FLTP; + in->nb_samples = n; + in->sample_rate = cd->out_rate; + av_channel_layout_default(&in->ch_layout, ch); + if (av_frame_get_buffer(in, 0) < 0) { + av_frame_free(&in); + return -ENOMEM; + } + + ret = source_get_data_s32(source, n * cd->out_frame_bytes, &src, NULL, NULL); + if (ret) { + av_frame_free(&in); + return ret; + } +#ifdef FFMPEG_AF_VFPU_CONV + /* + * Packed S32 -> float per channel. float.sx2(x, 31) converts both lanes + * as (float)x * 2^-31, fusing the /2^31 normalize. The interleaved + * source is strided, so gather two samples into an 8-byte aligned temp + * and load them packed; the planar float destination is contiguous and + * aligned, so store the packed result directly. The AE load/store + * intrinsics (not raw pointer deref) keep the packed value in the AE + * register file - the backend has no generic 64-bit-vector memory op. + * AE_L32X2/AE_S32X2 are mutual inverses, so lane order needs no + * reasoning about: temp slot k maps to plane slot k. + */ + for (c = 0; c < ch; c++) { + float *pl = (float *)in->data[c]; + const int32_t *sc = src + c; + + for (i = 0; i + 2 <= n; i += 2) { + int32_t tmp[2] __attribute__((aligned(8))); + ae_int32x2 p; + ae_xtfloatx2 r; + + tmp[0] = sc[i * ch]; + tmp[1] = sc[(i + 1) * ch]; + p = AE_L32X2_I((const ae_int32x2 *)tmp, 0); + r = XT_FLOAT_SX2(p, 31); + AE_S32X2_I((ae_int32x2)r, (ae_int32x2 *)&pl[i], 0); + } + for (; i < n; i++) + pl[i] = (float)sc[i * ch] / FFMPEG_AF_S32_SCALE; + } +#else + for (i = 0; i < n; i++) + for (c = 0; c < ch; c++) + ((float *)in->data[c])[i] = (float)src[i * ch + c] / FFMPEG_AF_S32_SCALE; +#endif + source_release_data(source, n * cd->out_frame_bytes); + + /* Run the graph. */ + out = av_frame_alloc(); + if (!out) { + av_frame_free(&in); + return -ENOMEM; + } + ret = ffmpeg_af_filter(cd->af_graph, in, out); + av_frame_free(&in); + if (ret == -EAGAIN) { /* filter still priming, no output yet */ + av_frame_free(&out); + return 0; + } + if (ret) { + av_frame_free(&out); + return ret; + } + + /* Interleave + denormalize float -> S32 into the sink. */ + m = MIN(out->nb_samples, sink_get_free_frames(sink)); + if (m > 0 && sink_get_buffer_s32(sink, m * cd->out_frame_bytes, &dst, NULL, NULL) == 0) { +#ifdef FFMPEG_AF_VFPU_CONV + /* + * Packed float -> S32 per channel. trunc.sx2(f, 31) converts both + * lanes as (int32_t)(f * 2^31) with int32 saturation, fusing the + * *2^31 denormalize and replacing the manual clamp in the + * vectorized body (the scalar tail keeps the explicit clamp). The + * planar float source is contiguous/aligned (packed load); the + * interleaved sink is strided, so spill the packed result to an + * aligned temp and scatter the two lanes. AE_L32X2/AE_S32X2 are + * mutual inverses, so temp slot k maps to plane slot k. + */ + for (c = 0; c < ch; c++) { + const float *pl = (const float *)out->data[c]; + int32_t *dc = dst + c; + + for (i = 0; i + 2 <= m; i += 2) { + int32_t tmp[2] __attribute__((aligned(8))); + ae_int32x2 f, q; + + f = AE_L32X2_I((const ae_int32x2 *)&pl[i], 0); + q = XT_TRUNC_SX2((ae_xtfloatx2)f, 31); + AE_S32X2_I(q, tmp, 0); + dc[i * ch] = tmp[0]; + dc[(i + 1) * ch] = tmp[1]; + } + for (; i < m; i++) { + float f = pl[i] * FFMPEG_AF_S32_SCALE; + + f = f > 2147483647.0f ? 2147483647.0f : + (f < -2147483648.0f ? -2147483648.0f : f); + dc[i * ch] = (int32_t)f; + } + } +#else + for (i = 0; i < m; i++) + for (c = 0; c < ch; c++) { + float f = ((const float *)out->data[c])[i] * FFMPEG_AF_S32_SCALE; + + f = f > 2147483647.0f ? 2147483647.0f : + (f < -2147483648.0f ? -2147483648.0f : f); + dst[i * ch + c] = (int32_t)f; + } +#endif + sink_commit_buffer(sink, m * cd->out_frame_bytes); + } + av_frame_free(&out); + return 0; +} + +int ffmpeg_af_mod_free(struct processing_module *mod) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + + ffmpeg_dec_libc_bind(mod); + if (cd->af_graph) { + ffmpeg_af_close(cd->af_graph); + mod_free(mod, cd->af_graph); + cd->af_graph = NULL; + } + return 0; +} +#endif /* CONFIG_FFMPEG_DEC_FILTER_MODE */ diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec-shims.c b/src/audio/ffmpeg_dec/ffmpeg_dec-shims.c new file mode 100644 index 000000000000..9cbc19c5dd2d --- /dev/null +++ b/src/audio/ffmpeg_dec/ffmpeg_dec-shims.c @@ -0,0 +1,452 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Local libc surface for the ffmpeg_dec libavcodec backend. +// +// libavcodec/libavutil reference a set of libc/libm symbols the SOF core does +// not export to LLEXT modules. This file defines them inside the module so it +// resolves at load. Categories: +// +// * stdio / time / env (file I/O, clock, getenv, stderr): NEVER used on a SOF +// DSP and never on the FLAC decode path -> no-op stubs. av_log()'s default +// stderr path is additionally bypassed by the Zephyr LOG wrapper installed in +// ffmpeg_dec-ffmpeg.c. +// * vsnprintf / snprintf: genuinely used (av_log formatting, string utils) -> +// a compact real implementation. +// * str/mem helpers not exported by the core (memchr, strchr, ...): real. +// * bsearch, __bswap*: real. +// * strto* : not on the FLAC decode path -> return-0 stubs. +// * libm (pow/sin/sqrt/...): FLAC is lossless integer and never calls these; +// SOF's fixed-point math is range-limited (sqrt 0-2, exp +/-8, 16-bit sin) so +// it cannot serve as a double libm. These are RESOLVE-ONLY STUBS that let the +// module load. THEY MUST BE REPLACED WITH A REAL DOUBLE libm before enabling +// any lossy codec (AAC/Opus/Vorbis), which would otherwise decode to garbage. +// +// NOTE: this file intentionally includes NO libc headers so these definitions +// cannot clash with newlib prototypes; only the symbol names matter to the linker. + +#include +#include +#include + +/* ============================ stdio / time / env ========================== */ + +/* stderr is referenced as a data object by FFmpeg's default log path. */ +void *stderr; + +int fprintf(void *stream, const char *fmt, ...) { (void)stream; (void)fmt; return 0; } +int fputs(const char *s, void *stream) { (void)s; (void)stream; return 0; } +unsigned long fread(void *p, unsigned long sz, unsigned long n, void *stream) +{ (void)p; (void)sz; (void)n; (void)stream; return 0; } +int fclose(void *stream) { (void)stream; return 0; } +void *fopen(const char *path, const char *mode) { (void)path; (void)mode; return NULL; } +void *fdopen(int fd, const char *mode) { (void)fd; (void)mode; return NULL; } +int setvbuf(void *s, char *buf, int mode, size_t size) +{ (void)s; (void)buf; (void)mode; (void)size; return 0; } +int open(const char *path, int flags, ...) { (void)path; (void)flags; return -1; } +int sscanf(const char *str, const char *fmt, ...) { (void)str; (void)fmt; return 0; } + +char *getenv(const char *name) { (void)name; return NULL; } +unsigned long clock(void) { return 0; } +void *gmtime_r(const void *timep, void *result) { (void)timep; (void)result; return NULL; } +void *localtime(const void *timep) { (void)timep; return NULL; } +void *localtime_r(const void *timep, void *result) { (void)timep; (void)result; return NULL; } + +/* iconv (avutil text/metadata charset conversion) - unused on the audio path. */ +void *iconv_open(const char *to, const char *from) { (void)to; (void)from; return (void *)-1; } +int iconv_close(void *cd) { (void)cd; return 0; } +size_t iconv(void *cd, char **in, size_t *inb, char **out, size_t *outb) +{ (void)cd; (void)in; (void)inb; (void)out; (void)outb; return (size_t)-1; } +int __xpg_strerror_r(int errnum, char *buf, size_t buflen) +{ (void)errnum; if (buflen) buf[0] = '\0'; return 0; } +long mktime(void *tm) { (void)tm; return -1; } +size_t strftime(char *s, size_t max, const char *fmt, const void *tm) +{ (void)fmt; (void)tm; if (max) s[0] = '\0'; return 0; } +void abort(void) { for (;;) ; } + +/* ============================ str / mem helpers =========================== */ + +void *memchr(const void *s, int c, size_t n) +{ + const unsigned char *p = s; + + while (n--) { + if (*p == (unsigned char)c) + return (void *)p; + p++; + } + return NULL; +} + +char *strchr(const char *s, int c) +{ + for (;; s++) { + if (*s == (char)c) + return (char *)s; + if (!*s) + return NULL; + } +} + +char *strrchr(const char *s, int c) +{ + const char *last = NULL; + + for (;; s++) { + if (*s == (char)c) + last = s; + if (!*s) + return (char *)last; + } +} + +char *strstr(const char *haystack, const char *needle) +{ + if (!*needle) + return (char *)haystack; + + for (; *haystack; haystack++) { + const char *h = haystack, *n = needle; + + while (*h && *n && *h == *n) { + h++; + n++; + } + if (!*n) + return (char *)haystack; + } + return NULL; +} + +static int ffmpeg_dec_in_set(char ch, const char *set) +{ + for (; *set; set++) + if (*set == ch) + return 1; + return 0; +} + +size_t strspn(const char *s, const char *accept) +{ + size_t n = 0; + + while (s[n] && ffmpeg_dec_in_set(s[n], accept)) + n++; + return n; +} + +size_t strcspn(const char *s, const char *reject) +{ + size_t n = 0; + + while (s[n] && !ffmpeg_dec_in_set(s[n], reject)) + n++; + return n; +} + +/* ============================ bsearch / bswap ============================= */ + +void *bsearch(const void *key, const void *base, size_t nmemb, size_t size, + int (*compar)(const void *, const void *)) +{ + size_t lo = 0, hi = nmemb; + + while (lo < hi) { + size_t mid = lo + (hi - lo) / 2; + const void *elem = (const char *)base + mid * size; + int r = compar(key, elem); + + if (r < 0) + hi = mid; + else if (r > 0) + lo = mid + 1; + else + return (void *)elem; + } + return NULL; +} + +uint32_t __bswapsi2(uint32_t x) +{ + return ((x & 0x000000ffu) << 24) | ((x & 0x0000ff00u) << 8) | + ((x & 0x00ff0000u) >> 8) | ((x & 0xff000000u) >> 24); +} + +uint64_t __bswapdi2(uint64_t x) +{ + return ((uint64_t)__bswapsi2((uint32_t)x) << 32) | + __bswapsi2((uint32_t)(x >> 32)); +} + +/* ============================ strto* (unused stubs) ======================= */ + +double strtod(const char *nptr, char **endptr) +{ if (endptr) *endptr = (char *)nptr; return 0; } +long strtol(const char *nptr, char **endptr, int base) +{ (void)base; if (endptr) *endptr = (char *)nptr; return 0; } +long long strtoll(const char *nptr, char **endptr, int base) +{ (void)base; if (endptr) *endptr = (char *)nptr; return 0; } +unsigned long strtoul(const char *nptr, char **endptr, int base) +{ (void)base; if (endptr) *endptr = (char *)nptr; return 0; } +unsigned long long strtoull(const char *nptr, char **endptr, int base) +{ (void)base; if (endptr) *endptr = (char *)nptr; return 0; } + +/* ============================ libm (RESOLVE-ONLY STUBS) =================== */ +/* FLAC never calls these. Replace with a real double libm for lossy codecs. */ + +double acos(double x) { (void)x; return 0; } +double asin(double x) { (void)x; return 0; } +double atan(double x) { (void)x; return 0; } +double atan2(double y, double x) { (void)y; (void)x; return 0; } +double ceil(double x) { (void)x; return 0; } +double cos(double x) { (void)x; return 0; } +double cosh(double x) { (void)x; return 0; } +double exp(double x) { (void)x; return 0; } +double exp2(double x) { (void)x; return 0; } +double fabs(double x) { return x < 0 ? -x : x; } +double floor(double x) { (void)x; return 0; } +double fmod(double x, double y) { (void)x; (void)y; return 0; } +double frexp(double x, int *e) { (void)x; if (e) *e = 0; return 0; } +double hypot(double x, double y) { (void)x; (void)y; return 0; } +long long llrint(double x) { (void)x; return 0; } +double log(double x) { (void)x; return 0; } +double pow(double x, double y) { (void)x; (void)y; return 0; } +double round(double x) { (void)x; return 0; } +double scalbn(double x, int n) { (void)x; (void)n; return 0; } +double sin(double x) { (void)x; return 0; } +double sinh(double x) { (void)x; return 0; } +double sqrt(double x) { (void)x; return 0; } +double tan(double x) { (void)x; return 0; } +double tanh(double x) { (void)x; return 0; } +double trunc(double x) { (void)x; return 0; } +double modf(double x, double *iptr) +{ long long i = (long long)x; if (iptr) *iptr = (double)i; return x - (double)i; } + +/* Small real libm helpers pulled in by libavfilter (afftdn). fmax/fmin/rint + * are exact; log10 is routed through the fast single-precision log2f (see + * fastmathf.c) so afftdn's dB math stays correct. */ +extern float sofm_log2f(float x); + +double fmax(double a, double b) { return a > b ? a : b; } +double fmin(double a, double b) { return a < b ? a : b; } +long lrint(double x) { return (long)(x < 0.0 ? x - 0.5 : x + 0.5); } +long long llrintf(float x) { return (long long)(x < 0.0f ? x - 0.5f : x + 0.5f); } +double log10(double x) { return (double)sofm_log2f((float)x) * 0.30102999566398120; } + +/* ============================ vsnprintf / snprintf ======================= */ + +static void ffmpeg_dec_putc(char *buf, size_t size, size_t *pos, char c) +{ + if (*pos < size) + buf[*pos] = c; + (*pos)++; +} + +int vsnprintf(char *buf, size_t size, const char *fmt, va_list ap) +{ + size_t pos = 0; + + for (; *fmt; fmt++) { + int left = 0, zero = 0, alt = 0, lng = 0, width = 0, prec = -1; + unsigned long long uv = 0; + int isnum = 0, base = 10, upper = 0, neg = 0; + const char *s = NULL; + char c; + + if (*fmt != '%') { + ffmpeg_dec_putc(buf, size, &pos, *fmt); + continue; + } + + fmt++; + /* flags */ + for (;; fmt++) { + if (*fmt == '-') + left = 1; + else if (*fmt == '0') + zero = 1; + else if (*fmt == '#') + alt = 1; + else if (*fmt == '+' || *fmt == ' ') + continue; + else + break; + } + /* width */ + if (*fmt == '*') { + width = va_arg(ap, int); + fmt++; + if (width < 0) { + left = 1; + width = -width; + } + } else { + while (*fmt >= '0' && *fmt <= '9') + width = width * 10 + (*fmt++ - '0'); + } + /* precision */ + if (*fmt == '.') { + fmt++; + prec = 0; + if (*fmt == '*') { + prec = va_arg(ap, int); + fmt++; + } else { + while (*fmt >= '0' && *fmt <= '9') + prec = prec * 10 + (*fmt++ - '0'); + } + } + /* length modifiers */ + for (;;) { + if (*fmt == 'l') { + lng++; + fmt++; + } else if (*fmt == 'z' || *fmt == 't' || *fmt == 'j') { + lng = 2; + fmt++; + } else if (*fmt == 'h' || *fmt == 'L') { + fmt++; + } else { + break; + } + } + + c = *fmt; + switch (c) { + case '%': + ffmpeg_dec_putc(buf, size, &pos, '%'); + continue; + case 'c': + ffmpeg_dec_putc(buf, size, &pos, (char)va_arg(ap, int)); + continue; + case 's': + s = va_arg(ap, const char *); + if (!s) + s = "(null)"; + break; + case 'd': + case 'i': { + long long v = lng >= 2 ? va_arg(ap, long long) : + lng == 1 ? va_arg(ap, long) : va_arg(ap, int); + if (v < 0) { + neg = 1; + uv = (unsigned long long)-v; + } else { + uv = (unsigned long long)v; + } + isnum = 1; + break; + } + case 'u': + uv = lng >= 2 ? va_arg(ap, unsigned long long) : + lng == 1 ? va_arg(ap, unsigned long) : va_arg(ap, unsigned int); + isnum = 1; + break; + case 'o': + uv = lng >= 2 ? va_arg(ap, unsigned long long) : + lng == 1 ? va_arg(ap, unsigned long) : va_arg(ap, unsigned int); + base = 8; + isnum = 1; + break; + case 'x': + case 'X': + uv = lng >= 2 ? va_arg(ap, unsigned long long) : + lng == 1 ? va_arg(ap, unsigned long) : va_arg(ap, unsigned int); + base = 16; + upper = (c == 'X'); + isnum = 1; + break; + case 'p': + uv = (unsigned long long)(uintptr_t)va_arg(ap, void *); + base = 16; + alt = 1; + isnum = 1; + break; + case 'e': + case 'f': + case 'g': + case 'E': + case 'F': + case 'G': + case 'a': + case 'A': + /* float formatting unsupported (see file header) */ + (void)va_arg(ap, double); + s = "0"; + break; + default: + ffmpeg_dec_putc(buf, size, &pos, '%'); + ffmpeg_dec_putc(buf, size, &pos, c); + continue; + } + + if (isnum) { + char digits[24]; + int di = 0, i; + const char *hx = upper ? "0123456789ABCDEF" : "0123456789abcdef"; + int len, pad; + + if (uv == 0) { + digits[di++] = '0'; + } else { + while (uv) { + digits[di++] = hx[uv % base]; + uv /= base; + } + } + + len = di + (neg ? 1 : 0) + (alt && base == 16 ? 2 : 0); + pad = width > len ? width - len : 0; + + if (!left && !zero) + while (pad-- > 0) + ffmpeg_dec_putc(buf, size, &pos, ' '); + if (neg) + ffmpeg_dec_putc(buf, size, &pos, '-'); + if (alt && base == 16) { + ffmpeg_dec_putc(buf, size, &pos, '0'); + ffmpeg_dec_putc(buf, size, &pos, upper ? 'X' : 'x'); + } + if (!left && zero) + while (pad-- > 0) + ffmpeg_dec_putc(buf, size, &pos, '0'); + for (i = di - 1; i >= 0; i--) + ffmpeg_dec_putc(buf, size, &pos, digits[i]); + if (left) + while (pad-- > 0) + ffmpeg_dec_putc(buf, size, &pos, ' '); + } else if (s) { + int len = 0, pad; + + while (s[len] && (prec < 0 || len < prec)) + len++; + pad = width > len ? width - len : 0; + + if (!left) + while (pad-- > 0) + ffmpeg_dec_putc(buf, size, &pos, ' '); + for (int i = 0; i < len; i++) + ffmpeg_dec_putc(buf, size, &pos, s[i]); + if (left) + while (pad-- > 0) + ffmpeg_dec_putc(buf, size, &pos, ' '); + } + } + + if (size) + buf[pos < size ? pos : size - 1] = '\0'; + + return (int)pos; +} + +int snprintf(char *buf, size_t size, const char *fmt, ...) +{ + va_list ap; + int n; + + va_start(ap, fmt); + n = vsnprintf(buf, size, fmt, ap); + va_end(ap); + return n; +} diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec-stub.c b/src/audio/ffmpeg_dec/ffmpeg_dec-stub.c new file mode 100644 index 000000000000..bb7cc7408d6a --- /dev/null +++ b/src/audio/ffmpeg_dec/ffmpeg_dec-stub.c @@ -0,0 +1,65 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Dependency-free stub backend for the ffmpeg_dec module. +// +// This lets the SOF module glue (init/prepare/process/config/reset/free, the +// LLEXT manifest and the topology wiring) be built and exercised in CI without +// pulling in libavcodec. It does not decode: it simply passes the input bytes +// through to the output, so the pipeline moves data end to end. The real +// libavcodec implementation lives in ffmpeg_dec-ffmpeg.c and provides the same +// struct ffmpeg_dec_backend symbol. + +#include +#include +#include +#include +#include "ffmpeg_dec.h" + +LOG_MODULE_DECLARE(ffmpeg_dec, CONFIG_SOF_LOG_LEVEL); + +static int ffmpeg_dec_stub_init(struct processing_module *mod) +{ + comp_info(mod->dev, "ffmpeg_dec stub backend: no decoder linked"); + return 0; +} + +static int ffmpeg_dec_stub_configure(struct processing_module *mod) +{ + return 0; +} + +static int ffmpeg_dec_stub_decode(struct processing_module *mod, + const uint8_t *in, size_t in_size, size_t *consumed, + uint8_t *out, size_t out_size, size_t *produced) +{ + /* Passthrough: copy as many bytes as fit, report exact consume/produce. */ + size_t n = MIN(in_size, out_size); + + if (n) + memcpy_s(out, out_size, in, n); + + *consumed = n; + *produced = n; + return 0; +} + +static int ffmpeg_dec_stub_reset(struct processing_module *mod) +{ + return 0; +} + +static int ffmpeg_dec_stub_free(struct processing_module *mod) +{ + return 0; +} + +const struct ffmpeg_dec_backend ffmpeg_dec_backend = { + .name = "stub", + .init = ffmpeg_dec_stub_init, + .configure = ffmpeg_dec_stub_configure, + .decode = ffmpeg_dec_stub_decode, + .reset = ffmpeg_dec_stub_reset, + .free = ffmpeg_dec_stub_free, +}; diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec.c b/src/audio/ffmpeg_dec/ffmpeg_dec.c new file mode 100644 index 000000000000..ff5f16a36a02 --- /dev/null +++ b/src/audio/ffmpeg_dec/ffmpeg_dec.c @@ -0,0 +1,763 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// FFmpeg (libavcodec) audio decoder wrapper - SOF module core. +// +// This translation unit contains the SOF module_interface glue only; the actual +// decoding is delegated to the backend selected at build time (see +// ffmpeg_dec-stub.c and ffmpeg_dec-ffmpeg.c). The input is a compressed audio +// elementary stream (raw data), the output is interleaved PCM. + +#include +#include +#include +#include +#include +#include +#include "ffmpeg_dec.h" + +#if CONFIG_IPC_MAJOR_4 +#include +#include +#include +#include +#endif + +/* UUID identifies the component. Registered in uuid-registry.txt. */ +SOF_DEFINE_REG_UUID(ffmpeg_dec); + +/* Creates logging data for the component */ +LOG_MODULE_REGISTER(ffmpeg_dec, CONFIG_SOF_LOG_LEVEL); + +/* Decoder-mode ops (compressed -> PCM). In filter mode the module uses the + * avfilter-graph PCM effect ops (ffmpeg_dec-filter.c); in encode mode the + * PCM->compressed ops (ffmpeg_dec-encode.c) instead. + */ +#if !CONFIG_FFMPEG_DEC_FILTER_MODE && !CONFIG_FFMPEG_DEC_ENCODE_MODE +int ffmpeg_dec_store_extradata(struct processing_module *mod, + const uint8_t *data, size_t size) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + struct comp_dev *dev = mod->dev; + uint8_t *buf; + + if (!size) + return 0; + + if (size > FFMPEG_DEC_MAX_EXTRADATA) { + comp_err(dev, "extradata too large: %zu", size); + return -EINVAL; + } + + /* Replace any previously stored setup header. */ + if (cd->extradata) { + mod_free(mod, cd->extradata); + cd->extradata = NULL; + cd->extradata_size = 0; + } + + buf = mod_alloc(mod, size); + if (!buf) + return -ENOMEM; + + memcpy_s(buf, size, data, size); + cd->extradata = buf; + cd->extradata_size = size; + comp_info(dev, "stored %zu bytes of codec extradata", size); + return 0; +} + +#if CONFIG_IPC_MAJOR_4 +/* + * Build the module notification the driver expects to unblock compress_drain(). + * The kernel's compress EOS handler (sof_ipc4_compr_drain_done) matches on the + * COMPR "magic" event id, so use the same template the Cadence decoder does. + */ +__cold static struct ipc_msg *ffmpeg_dec_eos_notification_init(struct processing_module *mod) +{ + struct comp_ipc_config *ipc_config = &mod->dev->ipc_config; + struct sof_ipc4_notify_module_data *msg_data; + union ipc4_notification_header primary; + struct ipc_msg *msg; + + primary.dat = 0; + primary.r.notif_type = SOF_IPC4_MODULE_NOTIFICATION; + primary.r.type = SOF_IPC4_GLB_NOTIFICATION; + primary.r.rsp = SOF_IPC4_MESSAGE_DIR_MSG_REQUEST; + primary.r.msg_tgt = SOF_IPC4_MESSAGE_TARGET_FW_GEN_MSG; + + msg = ipc_msg_w_ext_init(primary.dat, 0, sizeof(*msg_data)); + if (!msg) + return NULL; + + msg_data = (struct sof_ipc4_notify_module_data *)msg->tx_data; + msg_data->instance_id = IPC4_INST_ID(ipc_config->id); + msg_data->module_id = IPC4_MOD_ID(ipc_config->id); + msg_data->event_id = SOF_IPC4_NOTIFY_MODULE_EVENTID_COMPR_MAGIC_VAL; + msg_data->event_data_size = 0; + + return msg; +} + +/* + * Report that the compress drain is complete: send the EOS notification (which + * unblocks the host's compress_drain()) exactly once, and mark the sink so the + * end-of-stream state propagates downstream to the DAI. + */ +static void ffmpeg_dec_signal_eos(struct processing_module *mod, struct sof_sink *sink) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + + if (cd->eos_sent || !cd->eos_msg) + return; + + ipc_msg_send(cd->eos_msg, NULL, false); + cd->eos_sent = true; + audio_buffer_set_eos(sof_audio_buffer_from_sink(sink)); + comp_info(mod->dev, "EOS: compress drain complete"); +} +#endif /* CONFIG_IPC_MAJOR_4 */ + +/* + * Largest number of PCM samples-per-channel one decoded frame of @codec can + * yield. Used only to size the DP output ring buffer (see ffmpeg_dec_init); + * a generous value is harmless, an undersized one throttles playback. + */ +static uint32_t ffmpeg_dec_max_frame_samples(enum ffmpeg_dec_codec codec) +{ + switch (codec) { + case FFMPEG_DEC_CODEC_FLAC: return 4096; /* typical max block */ + case FFMPEG_DEC_CODEC_AAC: return 2048; /* HE-AAC (SBR) */ + case FFMPEG_DEC_CODEC_OPUS: return 2880; /* 60 ms @ 48k */ + case FFMPEG_DEC_CODEC_MP3: return 1152; /* MPEG-1 layer 3 */ + default: return 1152; + } +} + +/** + * ffmpeg_dec_init() - Initialize the ffmpeg_dec component. + * @mod: Pointer to module data. + * + * Allocates private data and hands off to the selected decode backend for its + * one-time initialization. __cold marks this non-critical path for slower DRAM. + * + * Return: Zero if success, otherwise error code. + */ +__cold static int ffmpeg_dec_init(struct processing_module *mod) +{ + struct module_data *md = &mod->priv; + struct comp_dev *dev = mod->dev; + struct ffmpeg_dec_comp_data *cd; + uint32_t frame_bytes; + int ret; + + comp_info(dev, "entry"); + + cd = mod_zalloc(mod, sizeof(*cd)); + if (!cd) + return -ENOMEM; + + md->private = cd; + cd->backend = &ffmpeg_dec_backend; + /* TODO: derive codec id from topology/IPC init config. Until then use the + * default from the Kconfig decoder selection (first enabled). + */ + cd->codec = FFMPEG_DEC_DEFAULT_CODEC; + + /* + * Advertise the decoder's output block size so the DP scheduler sizes the + * ring buffer between this (data-processing) module and the downstream LL + * chain to hold several decoded frames (bind reads mpd.out_buff_size -> + * ring = 3 x this, see ipc4 helper.c). A whole decoded frame (up to ~24 ms + * of audio for MP3) must fit in one drain, otherwise process() can only + * dribble a fraction per LL period and stalls waiting for the sink to + * drain at real time - serialising decode behind playback and running the + * stream slow. Size for ~2 frames (worst case, stereo S32), capped so the + * 3x ring stays bounded; clamp up to at least one frame for large blocks. + */ + frame_bytes = ffmpeg_dec_max_frame_samples(cd->codec) * 2 /* ch */ * 4 /* S32 */; + md->mpd.out_buff_size = 2 * frame_bytes; + if (md->mpd.out_buff_size > 32768) /* cap the 3x ring */ + md->mpd.out_buff_size = 32768; + if (md->mpd.out_buff_size < frame_bytes) /* but always hold >=1 frame */ + md->mpd.out_buff_size = frame_bytes; + comp_info(dev, "DP out ring block=%u (frame=%u)", md->mpd.out_buff_size, frame_bytes); + + comp_info(dev, "backend '%s'", cd->backend->name); + + if (cd->backend->init) { + ret = cd->backend->init(mod); + if (ret) { + comp_err(dev, "backend init failed %d", ret); + mod_free(mod, cd); + return ret; + } + } + +#if CONFIG_IPC_MAJOR_4 + /* Pre-build the compress end-of-stream notification (used at drain). */ + cd->eos_msg = ffmpeg_dec_eos_notification_init(mod); + if (!cd->eos_msg) { + comp_err(dev, "failed to allocate EOS notification"); + if (cd->backend->free) + cd->backend->free(mod); + mod_free(mod, cd); + return -ENOMEM; + } +#endif + + return 0; +} + +/** + * ffmpeg_dec_prepare() - Prepare the component for processing. + * @mod: Pointer to module data. + * @sources: Unused (input is a raw compressed byte stream). + * @sinks: Output PCM sink array; sinks[0] provides the target PCM format. + * + * Caches the decoded PCM output format and opens the backend decoder. + * + * Return: Zero if success, otherwise error code. + */ +static int ffmpeg_dec_prepare(struct processing_module *mod, + struct sof_source **sources, int num_of_sources, + struct sof_sink **sinks, int num_of_sinks) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + struct comp_dev *dev = mod->dev; + struct comp_buffer *sinkb; + const struct audio_stream *stream; + int ret; + + comp_dbg(dev, "entry"); + + /* + * The PCM format the decoder must produce is that of the sink (data + * consumer) buffer. Read it from the component device rather than the + * sinks[] arg so the audio_stream getters (rate/channels/frame fmt) are + * all available in one place. + */ + sinkb = comp_dev_get_first_data_consumer(dev); + if (!sinkb) { + comp_err(dev, "no sink buffer connected"); + return -EINVAL; + } + stream = &sinkb->stream; + + /* Cache the PCM format the decoder must produce for the pipeline. */ + cd->out_rate = audio_stream_get_rate(stream); + cd->out_channels = audio_stream_get_channels(stream); + cd->out_frame_fmt = audio_stream_get_frm_fmt(stream); + cd->out_frame_bytes = audio_stream_frame_bytes(stream); + + comp_info(dev, "out rate %u ch %u fmt %d frame_bytes %u", + cd->out_rate, cd->out_channels, cd->out_frame_fmt, + cd->out_frame_bytes); + + /* + * NB: the backend decoder is NOT opened here. prepare() is dispatched to + * this component's core over IDC and runs on that core's single IDC worker + * thread; the codec open (avcodec_open2) builds large trig tables and can + * take hundreds of ms (~580 ms measured for mp3). Doing that here would + * monopolise the IDC worker, so a cross-core prepare/trigger on the + * initiator core stalls or times out (-EAGAIN) and crashes on teardown. + * Defer the open to the first process() call, which runs on the module's + * own DP thread (deep 64 KiB stack, off the IDC critical path). See the + * lazy-open block in ffmpeg_dec_process(). + */ + + /* + * Linear scratch buffers used by process(): a padded input bounce buffer + * (compressed source -> contiguous parser input) and a PCM staging buffer + * (one decoded frame, drained into the sink over several cycles). Keep any + * allocation from a previous prepare() to survive reset()/re-prepare(). + */ + if (!cd->in_buf) { + cd->in_buf = mod_alloc(mod, FFMPEG_DEC_IN_BLOCK_SIZE + + FFMPEG_DEC_INPUT_PADDING); + if (!cd->in_buf) + return -ENOMEM; + cd->in_buf_size = FFMPEG_DEC_IN_BLOCK_SIZE; + } + if (!cd->pcm_buf) { + cd->pcm_buf = mod_alloc(mod, FFMPEG_DEC_PCM_BUF_SIZE); + if (!cd->pcm_buf) + return -ENOMEM; + cd->pcm_buf_size = FFMPEG_DEC_PCM_BUF_SIZE; + } + cd->pcm_avail = 0; + cd->pcm_rd = 0; + cd->hdr_done = false; + + return 0; +} + +static inline size_t ffmpeg_dec_min(size_t a, size_t b) +{ + return a < b ? a : b; +} + +/* Copy @bytes out of a circular source buffer (starting at @src) into the + * linear @dst, wrapping at the buffer end if necessary. + */ +static void ffmpeg_dec_copy_from_circular(void *dst, const void *src, + const void *buf_start, size_t buf_size, + size_t bytes) +{ + size_t to_end = (size_t)((const uint8_t *)buf_start + buf_size - + (const uint8_t *)src); + + if (to_end >= bytes) { + memcpy_s(dst, bytes, src, bytes); + return; + } + memcpy_s(dst, to_end, src, to_end); + memcpy_s((uint8_t *)dst + to_end, bytes - to_end, + buf_start, bytes - to_end); +} + +/* Copy @bytes from the linear @src into a circular sink buffer (starting at + * @dst), wrapping at the buffer end if necessary. + */ +static void ffmpeg_dec_copy_to_circular(void *dst, const void *buf_start, + size_t buf_size, const void *src, + size_t bytes) +{ + size_t to_end = (size_t)((uint8_t *)buf_start + buf_size - + (uint8_t *)dst); + + if (to_end >= bytes) { + memcpy_s(dst, bytes, src, bytes); + return; + } + memcpy_s(dst, to_end, src, to_end); + memcpy_s(buf_start, bytes - to_end, + (const uint8_t *)src + to_end, bytes - to_end); +} + +/* Drain as much staged PCM as the sink can currently accept. */ +static void ffmpeg_dec_drain_pcm(struct ffmpeg_dec_comp_data *cd, + struct sof_sink *sink) +{ + void *dst, *buf_start; + size_t buf_size, n; + int ret; + + n = ffmpeg_dec_min(cd->pcm_avail, sink_get_free_size(sink)); + if (!n) + return; + + ret = sink_get_buffer(sink, n, &dst, &buf_start, &buf_size); + if (ret) + return; + + ffmpeg_dec_copy_to_circular(dst, buf_start, buf_size, + cd->pcm_buf + cd->pcm_rd, n); + sink_commit_buffer(sink, n); + cd->pcm_rd += n; + cd->pcm_avail -= n; +} + +/* + * Consume and discard a container header from the source so the decoder is fed a + * bare frame elementary stream. Handles two prefixes: + * - "fLaC" : FLAC magic + metadata blocks (STREAMINFO reaches the decoder + * separately as extradata, so the whole header is skipped here). + * - "ID3" : an ID3v2 tag prepended to an MP3 elementary stream. The mpegaudio + * parser syncs on the 0xFFEx frame header and would mis-parse the tag, so the + * syncsafe-encoded tag length is skipped. + * + * cplay/tinycompress hand the firmware the whole *file*, but libavcodec's raw + * parsers sync on the frame marker and error ("Error buffering data") if fed the + * container/tag prefix. + * + * The header is far smaller than in_buf_size and the entire clip is already + * resident in the source ring, so a single pass suffices. Returns 0 once skipped + * (or when there is no container marker), -ENODATA while the full header is not yet + * buffered, or a negative errno on a malformed/oversized header. + */ +static int ffmpeg_dec_skip_container(struct ffmpeg_dec_comp_data *cd, + struct comp_dev *dev, struct sof_source *src) +{ + const void *sp, *sstart; + size_t sbytes, avail, req, off; + int ret; + + avail = source_get_data_available(src); + if (avail < 4) + return -ENODATA; + + req = ffmpeg_dec_min(avail, cd->in_buf_size); + ret = source_get_data(src, req, &sp, &sstart, &sbytes); + if (ret) + return ret; + ffmpeg_dec_copy_from_circular(cd->in_buf, sp, sstart, sbytes, req); + + /* + * ID3v2 tag (common on real-world MP3 files): 10-byte header ("ID3", + * 2-byte version, 1-byte flags, 4-byte syncsafe size), then the tag body, + * then (if flags bit 0x10) a 10-byte footer. Skip the whole tag so the + * mpegaudio parser sees frames. + */ + if (memcmp(cd->in_buf, "ID3", 3) == 0) { + uint32_t tag; + + if (req < 10) { + source_release_data(src, 0); + return -ENODATA; + } + /* syncsafe: 7 bits per byte */ + tag = ((uint32_t)(cd->in_buf[6] & 0x7f) << 21) | + ((uint32_t)(cd->in_buf[7] & 0x7f) << 14) | + ((uint32_t)(cd->in_buf[8] & 0x7f) << 7) | + (uint32_t)(cd->in_buf[9] & 0x7f); + off = 10 + tag + ((cd->in_buf[5] & 0x10) ? 10 : 0); + if (off > req) { + source_release_data(src, 0); + comp_err(dev, "ID3v2 tag (%zu) exceeds input block (%zu)", off, req); + return -EINVAL; + } + source_release_data(src, off); + comp_info(dev, "skipped %zu-byte ID3v2 tag", off); + return 0; + } + + /* No container marker: the stream already starts with frames. */ + if (memcmp(cd->in_buf, "fLaC", 4) != 0) { + source_release_data(src, 0); + return 0; + } + + /* Walk metadata blocks: 1 byte (last<<7 | type) + 3-byte big-endian len. */ + off = 4; + for (;;) { + uint8_t hdr; + uint32_t len; + + if (off + 4 > req) { + source_release_data(src, 0); + comp_err(dev, "FLAC header exceeds %zu-byte input block", req); + return -EINVAL; + } + hdr = cd->in_buf[off]; + len = ((uint32_t)cd->in_buf[off + 1] << 16) | + ((uint32_t)cd->in_buf[off + 2] << 8) | + (uint32_t)cd->in_buf[off + 3]; + off += 4 + len; + if (hdr & 0x80) /* last-metadata-block flag */ + break; + } + + if (off > req) { + source_release_data(src, 0); + comp_err(dev, "FLAC header (%zu) exceeds input block (%zu)", off, req); + return -EINVAL; + } + + source_release_data(src, off); + comp_info(dev, "skipped %zu-byte FLAC container header", off); + return 0; +} + +/** + * ffmpeg_dec_is_ready_to_process() - Gate the DP process() call. + * + * Ready when there is staged PCM still to drain, or fresh compressed input to + * decode - in either case the sink must have room to receive something. + */ +static bool ffmpeg_dec_is_ready_to_process(struct processing_module *mod, + struct sof_source **sources, + int num_of_sources, + struct sof_sink **sinks, + int num_of_sinks) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + + if (num_of_sources < 1 || num_of_sinks < 1) + return false; + + if (sink_get_free_size(sinks[0]) == 0) + return false; + + if (cd->pcm_avail) + return true; + + return source_get_data_available(sources[0]) > 0; +} + +/** + * ffmpeg_dec_process() - Decode compressed input into PCM (DP sink/source). + * @mod: Pointer to module data. + * @sources: sources[0] is the compressed elementary-stream input. + * @sinks: sinks[0] receives interleaved PCM. + * + * As a DP (data-processing domain) module the decoder uses the sink/source + * circular-buffer API. Each cycle either drains previously decoded PCM into the + * sink, or pulls a block of compressed bytes into a linear bounce buffer, has + * the backend decode one frame into the PCM staging buffer, and drains what + * fits. Decoded PCM larger than the sink's free space is carried to the next + * cycle rather than dropped. + * + * Return: Zero if success, otherwise error code. + */ +static int ffmpeg_dec_process(struct processing_module *mod, + struct sof_source **sources, int num_of_sources, + struct sof_sink **sinks, int num_of_sinks) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + struct comp_dev *dev = mod->dev; + struct sof_source *src; + struct sof_sink *sink; + const void *sp, *sstart; + size_t sbytes, avail, req; + size_t consumed = 0, produced = 0; + int ret; + + if (num_of_sources < 1 || num_of_sinks < 1) + return -EINVAL; + + src = sources[0]; + sink = sinks[0]; + + /* + * Lazy backend open, one-time, on the DP thread. prepare() deliberately + * skips avcodec_open2 because it runs on the core's single IDC worker where + * a ~580 ms mp3 table build would stall/timeout a cross-core prepare or + * trigger. Here we are on the module's own DP thread with a deep stack, so + * the open is off the IDC critical path. reset() flushes but keeps the + * decoder open (configured stays true), so this runs only on the first + * process() after a fresh prepare(). + */ + if (!cd->configured) { + if (cd->backend->configure) { + ret = cd->backend->configure(mod); + if (ret) { + comp_err(dev, "backend configure failed %d", ret); + return ret; + } + } + cd->configured = true; + return 0; /* decode on the next cycle */ + } + + /* Drain leftover PCM from a previous decode before consuming more input. */ + if (cd->pcm_avail) { + ffmpeg_dec_drain_pcm(cd, sink); + return 0; + } + + /* One-time: strip the FLAC container header so the parser sees frames. */ + if (!cd->hdr_done) { + ret = ffmpeg_dec_skip_container(cd, dev, src); + if (ret < 0) + return ret; /* -ENODATA: wait for the rest of the header */ + cd->hdr_done = true; + return 0; /* decode frames on subsequent cycles */ + } + + avail = source_get_data_available(src); + if (!avail) { +#if CONFIG_IPC_MAJOR_4 + /* + * Under a compress drain the host stops delivering data once the + * file has been fully consumed. With no input left and no staged + * PCM, the stream is flushed - complete the drain. + */ + if (dev->pipeline->expect_eos) + ffmpeg_dec_signal_eos(mod, sink); +#endif + return -ENODATA; + } + + /* Pull one contiguous, padded block of compressed input. */ + req = ffmpeg_dec_min(avail, cd->in_buf_size); + ret = source_get_data(src, req, &sp, &sstart, &sbytes); + if (ret) + return ret; + + ffmpeg_dec_copy_from_circular(cd->in_buf, sp, sstart, sbytes, req); + memset(cd->in_buf + req, 0, FFMPEG_DEC_INPUT_PADDING); + + ret = cd->backend->decode(mod, cd->in_buf, req, &consumed, + cd->pcm_buf, cd->pcm_buf_size, &produced); + source_release_data(src, consumed); + if (ret) { + comp_err(dev, "decode failed %d", ret); + return ret; + } + + cd->pcm_rd = 0; + cd->pcm_avail = produced; + + /* Drain whatever fits this cycle; the rest goes out on the next one. */ + if (cd->pcm_avail) { + ffmpeg_dec_drain_pcm(cd, sink); +#if CONFIG_IPC_MAJOR_4 + } else if (dev->pipeline->expect_eos) { + /* + * Draining and the decoder yielded no more PCM (the host is now + * feeding only end-of-stream silence): the stream has ended. + */ + ffmpeg_dec_signal_eos(mod, sink); +#endif + } + + return 0; +} + +/** + * ffmpeg_dec_set_config() - Receive the codec setup header (extradata). + * + * Reassembles a possibly fragmented binary configuration via the common + * module_set_configuration() helper, then stores the whole reassembled blob as + * codec extradata (e.g. FLAC STREAMINFO). Mirrors the DTS codec config path. + */ +__cold static int +ffmpeg_dec_set_config(struct processing_module *mod, uint32_t config_id, + enum module_cfg_fragment_position pos, uint32_t data_offset_size, + const uint8_t *fragment, size_t fragment_size, uint8_t *response, + size_t response_size) +{ + struct comp_dev *dev = mod->dev; + struct module_config *config = &mod->priv.cfg; + int ret; + + assert_can_be_cold(); + + ret = module_set_configuration(mod, config_id, pos, data_offset_size, fragment, + fragment_size, response, response_size); + if (ret < 0) { + comp_err(dev, "module_set_configuration failed %d", ret); + return ret; + } + + /* Wait until the whole (possibly fragmented) blob has been received. */ + if (pos != MODULE_CFG_FRAGMENT_LAST && pos != MODULE_CFG_FRAGMENT_SINGLE) + return 0; + + /* + * module_load_config() sets config->size to the payload size and + * config->data to the payload itself (the kernel has already stripped + * the sof_abi_hdr) - there is no inline header to skip. The payload is + * the raw codec setup blob (e.g. the 34-byte FLAC STREAMINFO). + */ + if (!config->size || !config->data) { + comp_warn(dev, "empty codec config"); + return 0; + } + + return ffmpeg_dec_store_extradata(mod, config->data, config->size); +} + +/** + * ffmpeg_dec_reset() - Reset the component to a re-preparable state. + * @mod: Pointer to module data. + * + * Flushes decoder state but keeps allocations so the pipeline can restart. + * + * Return: Zero if success, otherwise error code. + */ +static int ffmpeg_dec_reset(struct processing_module *mod) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + + comp_dbg(mod->dev, "entry"); + + /* Discard any PCM staged mid-stream; the pipeline is restarting. */ + cd->pcm_avail = 0; + cd->pcm_rd = 0; + cd->hdr_done = false; +#if CONFIG_IPC_MAJOR_4 + cd->eos_sent = false; +#endif + + if (cd->backend->reset) + return cd->backend->reset(mod); + + return 0; +} + +/** + * ffmpeg_dec_free() - Free dynamic allocations. + * @mod: Pointer to module data. + * + * Return: Zero if success, otherwise error code. + */ +__cold static int ffmpeg_dec_free(struct processing_module *mod) +{ + struct ffmpeg_dec_comp_data *cd = module_get_private_data(mod); + + assert_can_be_cold(); + comp_dbg(mod->dev, "entry"); + + if (cd->backend->free) + cd->backend->free(mod); + +#if CONFIG_IPC_MAJOR_4 + if (cd->eos_msg) + ipc_msg_free(cd->eos_msg); +#endif + + if (cd->pcm_buf) + mod_free(mod, cd->pcm_buf); + if (cd->in_buf) + mod_free(mod, cd->in_buf); + if (cd->extradata) + mod_free(mod, cd->extradata); + + mod_free(mod, cd); + return 0; +} +#endif /* !CONFIG_FFMPEG_DEC_FILTER_MODE && !CONFIG_FFMPEG_DEC_ENCODE_MODE */ + +/* This defines the module operations */ +#if CONFIG_FFMPEG_DEC_ENCODE_MODE +/* PCM -> compressed encoder (ffmpeg_dec-encode.c). */ +static const struct module_interface ffmpeg_dec_interface = { + .init = ffmpeg_enc_mod_init, + .prepare = ffmpeg_enc_mod_prepare, + .process_raw_data = ffmpeg_enc_mod_process, + .free = ffmpeg_enc_mod_free +}; +#elif CONFIG_FFMPEG_DEC_FILTER_MODE +/* PCM source/sink effect driving an avfilter graph (ffmpeg_dec-filter.c). */ +static const struct module_interface ffmpeg_dec_interface = { + .init = ffmpeg_af_mod_init, + .prepare = ffmpeg_af_mod_prepare, + .process = ffmpeg_af_mod_process, + .free = ffmpeg_af_mod_free +}; +#else +static const struct module_interface ffmpeg_dec_interface = { + .init = ffmpeg_dec_init, + .prepare = ffmpeg_dec_prepare, + .process = ffmpeg_dec_process, + .is_ready_to_process = ffmpeg_dec_is_ready_to_process, + .set_configuration = ffmpeg_dec_set_config, + .reset = ffmpeg_dec_reset, + .free = ffmpeg_dec_free +}; +#endif + +/* If COMP_FFMPEG_DEC is =m in Kconfig this is built as a loadable LLEXT module. */ +#if CONFIG_COMP_FFMPEG_DEC_MODULE + +#include +#include +#include + +static const struct sof_man_module_manifest mod_manifest __section(".module") __used = + SOF_LLEXT_MODULE_MANIFEST("FFMPGDEC", &ffmpeg_dec_interface, 1, + SOF_REG_UUID(ffmpeg_dec), 40); + +SOF_LLEXT_BUILDINFO; + +#else + +/* Only used for the module adapter trace context, soon to be deprecated */ +DECLARE_TR_CTX(ffmpeg_dec_tr, SOF_UUID(ffmpeg_dec_uuid), LOG_LEVEL_INFO); +DECLARE_MODULE_ADAPTER(ffmpeg_dec_interface, ffmpeg_dec_uuid, ffmpeg_dec_tr); +SOF_MODULE_INIT(ffmpeg_dec, sys_comp_module_ffmpeg_dec_interface_init); + +#endif diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec.h b/src/audio/ffmpeg_dec/ffmpeg_dec.h new file mode 100644 index 000000000000..ad5ab22ed3f1 --- /dev/null +++ b/src/audio/ffmpeg_dec/ffmpeg_dec.h @@ -0,0 +1,204 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright(c) 2026 Intel Corporation. + * + * FFmpeg (libavcodec) audio decoder wrapper for SOF. + * + * This module adapts SOF's module_interface to libavcodec's send-packet / + * receive-frame decode API. Compressed elementary-stream bytes arrive on the + * input (raw data) buffer, are parsed into codec frames, decoded to interleaved + * PCM and written to the output buffer. + * + * The actual decode work is delegated to a pluggable "backend" so the SOF glue + * can be validated with a dependency-free stub (ffmpeg_dec-stub.c) before the + * real libavcodec backend (ffmpeg_dec-ffmpeg.c) is linked in. + */ +#ifndef __SOF_AUDIO_FFMPEG_DEC_H__ +#define __SOF_AUDIO_FFMPEG_DEC_H__ + +#include +#include +#include +#include + +/* Maximum codec setup header (extradata) we accept, e.g. FLAC STREAMINFO. */ +#define FFMPEG_DEC_MAX_EXTRADATA 4096 + +/* + * Compressed input is pulled from the circular source buffer into a linear, + * padded bounce buffer one block at a time (the parser needs contiguous bytes + * and libavcodec over-reads past the packet by AV_INPUT_BUFFER_PADDING_SIZE). + */ +#define FFMPEG_DEC_IN_BLOCK_SIZE 4096 +#define FFMPEG_DEC_INPUT_PADDING 64 + +/* + * A single decoded frame (e.g. a 4096-sample FLAC block, stereo S32 = 32 KiB) + * can exceed the sink's free space, so decode into a linear staging buffer and + * drain it into the circular sink across as many process() cycles as needed. + */ +#define FFMPEG_DEC_PCM_BUF_SIZE 65536 + +/* Which codec this instance decodes. Kept as an explicit enum so it can be + * carried in topology/IPC config and mapped to an AVCodecID by the backend. + */ +enum ffmpeg_dec_codec { + FFMPEG_DEC_CODEC_NONE = 0, + FFMPEG_DEC_CODEC_FLAC, + FFMPEG_DEC_CODEC_AAC, + FFMPEG_DEC_CODEC_OPUS, + FFMPEG_DEC_CODEC_MP3, +}; + +/* Default codec for a new instance, picked from the Kconfig selection until the + * codec is carried per-instance from topology/IPC config. First enabled wins. + */ +#if defined(CONFIG_FFMPEG_DEC_FLAC) +#define FFMPEG_DEC_DEFAULT_CODEC FFMPEG_DEC_CODEC_FLAC +#elif defined(CONFIG_FFMPEG_DEC_AAC) +#define FFMPEG_DEC_DEFAULT_CODEC FFMPEG_DEC_CODEC_AAC +#elif defined(CONFIG_FFMPEG_DEC_OPUS) +#define FFMPEG_DEC_DEFAULT_CODEC FFMPEG_DEC_CODEC_OPUS +#elif defined(CONFIG_FFMPEG_DEC_MP3) +#define FFMPEG_DEC_DEFAULT_CODEC FFMPEG_DEC_CODEC_MP3 +#else +#define FFMPEG_DEC_DEFAULT_CODEC FFMPEG_DEC_CODEC_NONE +#endif + +struct ffmpeg_dec_comp_data; +struct ipc_msg; + +/** + * struct ffmpeg_dec_backend - decode backend operations. + * + * A backend owns the real decoder state (allocated by the backend itself and + * stored in ffmpeg_dec_comp_data.backend_data). All ops return 0 on success or + * a negative errno. + */ +struct ffmpeg_dec_backend { + const char *name; + + /* One-time backend init, called from module init(). */ + int (*init)(struct processing_module *mod); + + /* Open/configure the decoder once codec, extradata and the output PCM + * format (rate/channels/frame format) are known. Called from prepare(). + */ + int (*configure)(struct processing_module *mod); + + /* + * Decode from @in (in_size bytes of compressed stream) into @out (up to + * out_size bytes of interleaved PCM). On return *consumed holds input + * bytes eaten and *produced holds PCM bytes written. + */ + int (*decode)(struct processing_module *mod, + const uint8_t *in, size_t in_size, size_t *consumed, + uint8_t *out, size_t out_size, size_t *produced); + + /* Flush decoder state, keep it configured. Called from reset(). */ + int (*reset)(struct processing_module *mod); + + /* Tear down decoder state allocated by init()/configure(). */ + int (*free)(struct processing_module *mod); +}; + +/** + * struct ffmpeg_dec_comp_data - ffmpeg_dec module private data. + * @backend: Selected decode backend ops. + * @backend_data: Backend-private decoder state. + * @codec: Codec id for this instance. + * @extradata: Codec setup header (e.g. FLAC STREAMINFO), NULL if none. + * @extradata_size: Size of @extradata in bytes. + * @out_rate: Decoded PCM sample rate (Hz), from the sink stream params. + * @out_channels: Decoded PCM channel count. + * @out_frame_fmt: Decoded PCM sample format (enum sof_ipc_frame). + * @out_frame_bytes: Bytes per PCM frame (all channels). + * @configured: True once the backend decoder has been opened. + * @in_buf: Linear+padded bounce buffer for compressed input. + * @in_buf_size: Usable capacity of @in_buf (excludes trailing padding). + * @pcm_buf: Linear staging buffer for one decoded frame's PCM. + * @pcm_buf_size: Capacity of @pcm_buf. + * @pcm_avail: Staged PCM bytes not yet drained into the sink. + * @pcm_rd: Read offset of the next byte to drain from @pcm_buf. + */ +struct ffmpeg_dec_comp_data { + const struct ffmpeg_dec_backend *backend; + void *backend_data; + enum ffmpeg_dec_codec codec; + uint8_t *extradata; + size_t extradata_size; + uint32_t out_rate; + uint32_t out_channels; + int out_frame_fmt; + uint32_t out_frame_bytes; + bool configured; + uint8_t *in_buf; + size_t in_buf_size; + uint8_t *pcm_buf; + size_t pcm_buf_size; + size_t pcm_avail; + size_t pcm_rd; + bool hdr_done; +#if CONFIG_IPC_MAJOR_4 + /* + * Compress end-of-stream (drain) support. On a DRAIN trigger the kernel + * sets pipeline->expect_eos and blocks in compress_drain() until the + * module reports the drain is complete. @eos_msg is the pre-built module + * notification sent once the last PCM has been flushed; @eos_sent guards + * it against being sent more than once. + */ + struct ipc_msg *eos_msg; + bool eos_sent; +#endif + /* Filter mode (CONFIG_FFMPEG_DEC_FILTER_MODE): avfilter graph handle + * (struct ffmpeg_af_graph *, see ffmpeg_dec-filter.c). Unused for decode. + */ + void *af_graph; +}; + +/* Encode-mode module ops (PCM -> compressed), in ffmpeg_dec-encode.c. */ +int ffmpeg_enc_mod_init(struct processing_module *mod); +int ffmpeg_enc_mod_prepare(struct processing_module *mod, + struct sof_source **sources, int num_of_sources, + struct sof_sink **sinks, int num_of_sinks); +int ffmpeg_enc_mod_process(struct processing_module *mod, + struct input_stream_buffer *input_buffers, int num_input_buffers, + struct output_stream_buffer *output_buffers, int num_output_buffers); +int ffmpeg_enc_mod_free(struct processing_module *mod); + +/* Filter-mode module ops (PCM source/sink effect), in ffmpeg_dec-filter.c. */ +int ffmpeg_af_mod_init(struct processing_module *mod); +int ffmpeg_af_mod_prepare(struct processing_module *mod, + struct sof_source **sources, int num_of_sources, + struct sof_sink **sinks, int num_of_sinks); +int ffmpeg_af_mod_process(struct processing_module *mod, + struct sof_source **sources, int num_of_sources, + struct sof_sink **sinks, int num_of_sinks); +int ffmpeg_af_mod_free(struct processing_module *mod); + +/* Backend instance provided by the selected backend translation unit + * (ffmpeg_dec-stub.c or ffmpeg_dec-ffmpeg.c). + */ +extern const struct ffmpeg_dec_backend ffmpeg_dec_backend; + +/** + * ffmpeg_dec_store_extradata() - Copy a codec setup header into private data. + * @mod: Pointer to module data. + * @data: Setup header bytes (e.g. FLAC STREAMINFO). + * @size: Number of bytes. + * + * Return: Zero if success, otherwise error code. + */ +int ffmpeg_dec_store_extradata(struct processing_module *mod, + const uint8_t *data, size_t size); + +/** + * ffmpeg_dec_libc_bind() - Bind the module whose heap backs malloc/free/realloc. + * @mod: Pointer to module data (see ffmpeg_dec-alloc.c). + * + * Called at the entry of every libavcodec backend op so allocations made by + * FFmpeg are drawn from the current instance's SOF heap. + */ +void ffmpeg_dec_libc_bind(struct processing_module *mod); + +#endif /* __SOF_AUDIO_FFMPEG_DEC_H__ */ diff --git a/src/audio/ffmpeg_dec/ffmpeg_dec.toml b/src/audio/ffmpeg_dec/ffmpeg_dec.toml new file mode 100644 index 000000000000..6c58fcb8b731 --- /dev/null +++ b/src/audio/ffmpeg_dec/ffmpeg_dec.toml @@ -0,0 +1,25 @@ +#ifndef LOAD_TYPE +#define LOAD_TYPE "0" +#endif + + REM # FFmpeg audio decoder module config + [[module.entry]] + REM # NOTE: module name is limited to 8 chars (manifest name field); + REM # it must match the SOF_LLEXT_MODULE_MANIFEST() name in ffmpeg_dec.c. + name = "FFMPGDEC" + uuid = UUIDREG_STR_FFMPEG_DEC + affinity_mask = "0x1" + instance_count = "40" + domain_types = "0" + load_type = LOAD_TYPE + module_type = "9" + auto_start = "0" + sched_caps = [1, 0x00008000] + REM # pin = [dir, type, sample rate, size, container, channel-cfg] + pin = [0, 0, 0xfeef, 0xf, 0xf, 0x45ff, 1, 0, 0xfeef, 0xf, 0xf, 0x1ff] + REM # mod_cfg [PAR_0 PAR_1 PAR_2 PAR_3 IS_BYTES CPS IBS OBS MOD_FLAGS CPC OBLS] + REM # Decoder: input is a small compressed chunk, output is larger PCM, so + REM # OBS (output buffer size) is sized well above IBS (input buffer size). + mod_cfg = [0, 0, 0, 0, 8192, 5000000, 4096, 16384, 0, 5000, 0] + + index = __COUNTER__ diff --git a/src/audio/ffmpeg_dec/libm/README b/src/audio/ffmpeg_dec/libm/README new file mode 100644 index 000000000000..c532127a7cb5 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/README @@ -0,0 +1,74 @@ +newlib libm (double + float math) — vendored for ffmpeg_dec's built-in (=y) build +================================================================================= + +Why this is here +---------------- +libavcodec/libavutil reference the C double math library (sin/cos/exp/log/pow/ +scalbn/...); the lossy decoders (AAC/Opus/Vorbis) actually call it at runtime. +The SOF base image is built with CONFIG_MINIMAL_LIBC, which ships NO double libm, +so the math has to come from newlib. + +The Zephyr SDK's *pre-built* newlib math cannot be linked into this image: + + * The image is compiled -mlongcalls and linked --no-relax. The SDK's pre-built + math objects use direct call8 (there is no -mlongcalls multilib), so an + out-of-range inter-object call (e.g. -> memset) fails at link time with + "dangerous relocation: call8: call target out of range". + * (SDK >= 1.0 also ships an *empty* libm.a and folds math into libc.a, which we + cannot whole-link -- it would multiply-define malloc/str*/memcpy against the + minimal libc.) + +So we compile newlib's math from source with -mlongcalls -mtext-section-literals +(the compiler then emits callx8 = L32R+CALLX, unlimited range) into a private +archive, libnewlib_m.a (see ../ffmpeg.cmake, section 3c). Linked as an archive it +pulls only the math objects a decoder actually calls; the whole set's external +tail is compiler soft-float builtins (__adddf3/__muldf3/... comparisons/ +conversions) + memset -- all already in the image -- plus __errno on the error +paths, bridged to Zephyr's per-thread errno in ffmpeg_dec-builtin-libc.c. + +FLAC is integer-only and never calls libm at runtime, but the references still +resolve to real kernels now (not return-0 stubs), so lossy codecs decode with +correct math instead of garbage. + +Provenance +---------- +Source: Zephyr's picolibc module, newlib/libm/ + https://github.com/zephyrproject-rtos/picolibc + commit 01254932e8e81085817ed61fd858648584ffe37c (git describe: 1.8.9-492-g01254932e) + +Only the double + float math is vendored: + math/ 86 .c (fdlibm kernels: s_sin, k_rem_pio2, e_pow, ...) + common/ 124 .c (ARM-optimized routines + fdlibm.h/math_config.h etc.) + common/ 5 .h (fdlibm.h, fma_inc.h, local.h, math_config.h, sincosf.h) + +Excluded on purpose: libm/complex/ (complex.h -- FFmpeg audio uses its own FFT, +never libc complex), libm/ld/ (long double -- FFmpeg audio never calls *l +functions; the double impls carry their own long-double aliases), libm/fenv/, +libm/machine/, libm/test/. The math/ + common/ subset is self-contained for +FFmpeg: verified that the double-math symbol closure resolves with no dangling +references into ld/ or complex/. + +Which files are compiled +------------------------ +newlib_math_sources.cmake lists the exact set to COMPILE (197 files) -- this is +the same member set the SDK's newlib libc.a selects, so there are no duplicate +definitions. 13 additional common/*.c (cosf/exp/exp2/log/log2/pow/sf_*/sincosf/ +sinf) are present in this tree but NOT in that list: they are only textually +#included by their math/s_*.c or common/s_*.c wrappers (e.g. math/s_exp.c does +#include "../common/exp.c"). Compiling them standalone would multiply-define +exp/log/pow/..., so ffmpeg.cmake compiles only newlib_math_sources.cmake. + +Regenerating (when bumping the picolibc revision) +------------------------------------------------- +From a checked-out picolibc newlib libm (LM) and this core's SDK libc.a (LIBC), +using the SDK ar (AR): + + # the member set newlib itself selected, mapped back to sources: + "$AR" t "$LIBC" | sed 's/^libm_//;s/\.c\.o$//' \ + | awk -F_ '$1=="math"||$1=="common"{s=$1; sub("^"s"_","");print s"/"$0".c"}' \ + > newlib_math_sources.list + # copy those .c + every .c they textually #include + all common/*.h into + # math/ and common/, then rebuild newlib_math_sources.cmake from the .list. + +Keep math/ + common/ only; do not add ld/complex/fenv unless a future codec +genuinely needs long-double or complex math (then re-verify the symbol closure). diff --git a/src/audio/ffmpeg_dec/libm/common/copysignl.c b/src/audio/ffmpeg_dec/libm/common/copysignl.c new file mode 100644 index 000000000000..0074eb2a971e --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/copysignl.c @@ -0,0 +1,42 @@ +/* +(C) Copyright IBM Corp. 2009 + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +* Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +* Neither the name of IBM nor the names of its contributors may be +used to endorse or promote products derived from this software without +specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +*/ + +#include "math_config.h" + +#if defined(_NEED_FLOAT_HUGE) && __HAVE_BUILTIN_COPYSIGNL + +/* On platforms where long double is as wide as double. */ +long double +copysignl (long double x, long double y) +{ + return __builtin_copysignl(x, y); +} + +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/cosf.c b/src/audio/ffmpeg_dec/libm/common/cosf.c new file mode 100644 index 000000000000..61a1aacef1f8 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/cosf.c @@ -0,0 +1,89 @@ +/* Single-precision cos function. + Copyright (c) 2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "fdlibm.h" +#if !__OBSOLETE_MATH_FLOAT + +#include +#include +#include "math_config.h" +#include "sincosf.h" + +/* Fast cosf implementation. Worst-case ULP is 0.5607, maximum relative + error is 0.5303 * 2^-23. A single-step range reduction is used for + small values. Large inputs have their range reduced using fast integer + arithmetic. */ +float +cosf (float y) +{ + double x = (double) y; + double s; + int n; + const sincos_t *p = &__sincosf_table[0]; + + if (abstop12 (y) < abstop12 (pio4)) + { + double x2 = x * x; + + if (unlikely (abstop12 (y) < abstop12 (0x1p-12f))) + return 1.0f; + + return sinf_poly (x, x2, p, 1); + } + else if (likely (abstop12 (y) < abstop12 (120.0f))) + { + x = reduce_fast (x, p, &n); + + /* Setup the signs for sin and cos. */ + s = p->sign[n & 3]; + + if (n & 2) + p = &__sincosf_table[1]; + + return sinf_poly (x * s, x * x, p, n ^ 1); + } + else if (abstop12 (y) < abstop12 (INFINITY)) + { + uint32_t xi = asuint (y); + int sign = xi >> 31; + + x = reduce_large (xi, &n); + + /* Setup signs for sin and cos - include original sign. */ + s = p->sign[(n + sign) & 3]; + + if ((n + sign) & 2) + p = &__sincosf_table[1]; + + return sinf_poly (x * s, x * x, p, n ^ 1); + } + else + return __math_invalidf (y); +} + +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/exp.c b/src/audio/ffmpeg_dec/libm/common/exp.c new file mode 100644 index 000000000000..4698e73b299e --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/exp.c @@ -0,0 +1,182 @@ +/* Double-precision e^x function. + Copyright (c) 2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "fdlibm.h" +#if !__OBSOLETE_MATH_DOUBLE + +#include +#include +#include "math_config.h" + +#define N (1 << EXP_TABLE_BITS) +#define InvLn2N __exp_data.invln2N +#define NegLn2hiN __exp_data.negln2hiN +#define NegLn2loN __exp_data.negln2loN +#define Shift __exp_data.shift +#define T __exp_data.tab +#define C2 __exp_data.poly[5 - EXP_POLY_ORDER] +#define C3 __exp_data.poly[6 - EXP_POLY_ORDER] +#define C4 __exp_data.poly[7 - EXP_POLY_ORDER] +#define C5 __exp_data.poly[8 - EXP_POLY_ORDER] +#define C6 __exp_data.poly[9 - EXP_POLY_ORDER] + +/* Handle cases that may overflow or underflow when computing the result that + is scale*(1+TMP) without intermediate rounding. The bit representation of + scale is in SBITS, however it has a computed exponent that may have + overflown into the sign bit so that needs to be adjusted before using it as + a double. (int32_t)KI is the k used in the argument reduction and exponent + adjustment of scale, positive k here means the result may overflow and + negative k means the result may underflow. */ +static inline double +specialcase (double_t tmp, uint64_t sbits, uint64_t ki) +{ + double_t scale, y; + + if ((ki & 0x80000000) == 0) + { + /* k > 0, the exponent of scale might have overflowed by <= 460. */ + sbits -= 1009ull << 52; + scale = asfloat64 (sbits); + y = 0x1p1009 * (scale + scale * tmp); + return check_oflow (y); + } + /* k < 0, need special care in the subnormal range. */ + sbits += 1022ull << 52; + scale = asfloat64 (sbits); + y = scale + scale * tmp; + if (y < 1.0) + { + /* Round y to the right precision before scaling it into the subnormal + range to avoid double rounding that can cause 0.5+E/2 ulp error where + E is the worst-case ulp error outside the subnormal range. So this + is only useful if the goal is better than 1 ulp worst-case error. */ + double_t hi, lo; + lo = scale - y + scale * tmp; + hi = 1.0 + y; + lo = 1.0 - hi + y + lo; + y = eval_as_double (hi + lo) - 1.0; + /* Avoid -0.0 with downward rounding. */ +#if WANT_ROUNDING + if (y == 0.0) + y = 0.0; +#endif + /* The underflow exception needs to be signaled explicitly. */ + force_eval_double (opt_barrier_double (0x1p-1022) * 0x1p-1022); + } + y = 0x1p-1022 * y; + return check_uflow (y); +} + +/* Top 12 bits of a double (sign and exponent bits). */ +static inline uint32_t +top12 (double x) +{ + return asuint64 (x) >> 52; +} + +double +exp (double x) +{ + uint32_t abstop; + uint64_t ki, idx, top, sbits; + /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ + double_t kd, z, r, r2, scale, tail, tmp; + + abstop = top12 (x) & 0x7ff; + if (unlikely (abstop - top12 (0x1p-54) >= top12 (512.0) - top12 (0x1p-54))) + { + if (abstop - top12 (0x1p-54) >= 0x80000000) + /* Avoid spurious underflow for tiny x. */ + /* Note: 0 is common input. */ +#if WANT_ROUNDING + return 1.0 + x; +#else + return 1.0; +#endif + if (abstop >= top12 (1024.0)) + { + if (asuint64 (x) == asuint64 ((double) -INFINITY)) + return 0.0; + if (abstop >= top12 ((double) INFINITY)) + return 1.0 + x; + if (asuint64 (x) >> 63) + return __math_uflow (0); + else + return __math_oflow (0); + } + /* Large x is special cased below. */ + abstop = 0; + } + + /* exp(x) = 2^(k/N) * exp(r), with exp(r) in [2^(-1/2N),2^(1/2N)]. */ + /* x = ln2/N*k + r, with int k and r in [-ln2/2N, ln2/2N]. */ + z = InvLn2N * x; +#if TOINT_INTRINSICS + kd = roundtoint (z); + ki = converttoint (z); +#elif EXP_USE_TOINT_NARROW + /* z - kd is in [-0.5-2^-16, 0.5] in all rounding modes. */ + kd = eval_as_double (z + Shift); + ki = asuint64 (kd) >> 16; + kd = (double_t) (int32_t) ki; +#else + /* z - kd is in [-1, 1] in non-nearest rounding modes. */ + kd = eval_as_double (z + Shift); + ki = asuint64 (kd); + kd -= Shift; +#endif + r = x + kd * NegLn2hiN + kd * NegLn2loN; + /* 2^(k/N) ~= scale * (1 + tail). */ + idx = 2 * (ki % N); + top = ki << (52 - EXP_TABLE_BITS); + tail = asfloat64 (T[idx]); + /* This is only a valid scale when -1023*N < k < 1024*N. */ + sbits = T[idx + 1] + top; + /* exp(x) = 2^(k/N) * exp(r) ~= scale + scale * (tail + exp(r) - 1). */ + /* Evaluation is optimized assuming superscalar pipelined execution. */ + r2 = r * r; + /* Without fma the worst case error is 0.25/N ulp larger. */ + /* Worst case error is less than 0.5+1.11/N+(abs poly error * 2^53) ulp. */ +#if EXP_POLY_ORDER == 4 + tmp = tail + r + r2 * C2 + r * r2 * (C3 + r * C4); +#elif EXP_POLY_ORDER == 5 + tmp = tail + r + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5); +#elif EXP_POLY_ORDER == 6 + tmp = tail + r + r2 * (0.5 + r * C3) + r2 * r2 * (C4 + r * C5 + r2 * C6); +#endif + if (unlikely (abstop == 0)) + return specialcase (tmp, sbits, ki); + scale = asfloat64 (sbits); + /* Note: tmp == 0 or |tmp| > 2^-65 and scale > 2^-739, so there + is no spurious underflow here even without fma. */ + return scale + scale * tmp; +} + +_MATH_ALIAS_d_d(exp) + +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/exp10l.c b/src/audio/ffmpeg_dec/libm/common/exp10l.c new file mode 100644 index 000000000000..19f99bc15d34 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/exp10l.c @@ -0,0 +1,61 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright © 2021 Keith Packard + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#define _GNU_SOURCE +#include "math_config.h" + +#if defined(__HAVE_LONG_DOUBLE_MATH) && defined(_NEED_FLOAT_HUGE) + +long double +exp10l (long double x) +{ + return _powl(10.0l, x); +} + +#ifdef __strong_reference +#if defined(__GNUCLIKE_PRAGMA_DIAGNOSTIC) && !defined(__clang__) +#pragma GCC diagnostic ignored "-Wmissing-attributes" +#endif +__strong_reference(exp10l, pow10l); +#else + +long double +pow10l (long double x) +{ + return exp10l(x); +} +#endif + +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/exp2.c b/src/audio/ffmpeg_dec/libm/common/exp2.c new file mode 100644 index 000000000000..41a608dbe455 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/exp2.c @@ -0,0 +1,169 @@ +/* Double-precision 2^x function. + Copyright (c) 2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "fdlibm.h" +#if !__OBSOLETE_MATH_DOUBLE + +#include +#include +#include "math_config.h" + +#define N (1 << EXP_TABLE_BITS) +#define Shift __exp_data.exp2_shift +#define T __exp_data.tab +#define C1 __exp_data.exp2_poly[0] +#define C2 __exp_data.exp2_poly[1] +#define C3 __exp_data.exp2_poly[2] +#define C4 __exp_data.exp2_poly[3] +#define C5 __exp_data.exp2_poly[4] +#define C6 __exp_data.exp2_poly[5] + +/* Handle cases that may overflow or underflow when computing the result that + is scale*(1+TMP) without intermediate rounding. The bit representation of + scale is in SBITS, however it has a computed exponent that may have + overflown into the sign bit so that needs to be adjusted before using it as + a double. (int32_t)KI is the k used in the argument reduction and exponent + adjustment of scale, positive k here means the result may overflow and + negative k means the result may underflow. */ +static inline double +specialcase (double_t tmp, uint64_t sbits, uint64_t ki) +{ + double_t scale, y; + + if ((ki & 0x80000000) == 0) + { + /* k > 0, the exponent of scale might have overflowed by 1. */ + sbits -= 1ull << 52; + scale = asfloat64 (sbits); + y = 2 * (scale + scale * tmp); + return check_oflow (y); + } + /* k < 0, need special care in the subnormal range. */ + sbits += 1022ull << 52; + scale = asfloat64 (sbits); + y = scale + scale * tmp; + if (y < 1.0) + { + /* Round y to the right precision before scaling it into the subnormal + range to avoid double rounding that can cause 0.5+E/2 ulp error where + E is the worst-case ulp error outside the subnormal range. So this + is only useful if the goal is better than 1 ulp worst-case error. */ + double_t hi, lo; + lo = scale - y + scale * tmp; + hi = 1.0 + y; + lo = 1.0 - hi + y + lo; + y = eval_as_double (hi + lo) - 1.0; + /* Avoid -0.0 with downward rounding. */ +#if WANT_ROUNDING + if (y == 0.0) + y = 0.0; +#endif + /* The underflow exception needs to be signaled explicitly. */ + force_eval_double (opt_barrier_double (0x1p-1022) * 0x1p-1022); + } + y = 0x1p-1022 * y; + return check_uflow (y); +} + +/* Top 12 bits of a double (sign and exponent bits). */ +static inline uint32_t +top12 (double x) +{ + return asuint64 (x) >> 52; +} + +double +exp2 (double x) +{ + uint32_t abstop; + uint64_t ki, idx, top, sbits; + /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ + double_t kd, r, r2, scale, tail, tmp; + + abstop = top12 (x) & 0x7ff; + if (unlikely (abstop - top12 (0x1p-54) >= top12 (512.0) - top12 (0x1p-54))) + { + if (abstop - top12 (0x1p-54) >= 0x80000000) + /* Avoid spurious underflow for tiny x. */ + /* Note: 0 is common input. */ +#if WANT_ROUNDING + return 1.0 + x; +#else + return 1.0; +#endif + if (abstop >= top12 (1024.0)) + { + if (asuint64 (x) == asuint64 ((double) -INFINITY)) + return 0.0; + if (abstop >= top12 ((double) INFINITY)) + return 1.0 + x; + if (!(asuint64 (x) >> 63)) + return __math_oflow (0); + else if (asuint64 (x) >= asuint64 (-1075.0)) + return __math_uflow (0); + } + if (2 * asuint64 (x) > 2 * asuint64 (928.0)) + /* Large x is special cased below. */ + abstop = 0; + } + + /* exp2(x) = 2^(k/N) * 2^r, with 2^r in [2^(-1/2N),2^(1/2N)]. */ + /* x = k/N + r, with int k and r in [-1/2N, 1/2N]. */ + kd = eval_as_double (x + Shift); + ki = asuint64 (kd); /* k. */ + kd -= Shift; /* k/N for int k. */ + r = x - kd; + /* 2^(k/N) ~= scale * (1 + tail). */ + idx = 2 * (ki % N); + top = ki << (52 - EXP_TABLE_BITS); + tail = asfloat64 (T[idx]); + /* This is only a valid scale when -1023*N < k < 1024*N. */ + sbits = T[idx + 1] + top; + /* exp2(x) = 2^(k/N) * 2^r ~= scale + scale * (tail + 2^r - 1). */ + /* Evaluation is optimized assuming superscalar pipelined execution. */ + r2 = r * r; + /* Without fma the worst case error is 0.5/N ulp larger. */ + /* Worst case error is less than 0.5+0.86/N+(abs poly error * 2^53) ulp. */ +#if EXP2_POLY_ORDER == 4 + tmp = tail + r * C1 + r2 * C2 + r * r2 * (C3 + r * C4); +#elif EXP2_POLY_ORDER == 5 + tmp = tail + r * C1 + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5); +#elif EXP2_POLY_ORDER == 6 + tmp = tail + r * C1 + r2 * (0.5 + r * C3) + r2 * r2 * (C4 + r * C5 + r2 * C6); +#endif + if (unlikely (abstop == 0)) + return specialcase (tmp, sbits, ki); + scale = asfloat64 (sbits); + /* Note: tmp == 0 or |tmp| > 2^-65 and scale > 2^-928, so there + is no spurious underflow here even without fma. */ + return scale + scale * tmp; +} + +_MATH_ALIAS_d_d(exp2) + +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/exp_data.c b/src/audio/ffmpeg_dec/libm/common/exp_data.c new file mode 100644 index 000000000000..e5a32241e108 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/exp_data.c @@ -0,0 +1,611 @@ +/* Shared data between exp, exp2 and pow. + Copyright (c) 2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "fdlibm.h" +#if !__OBSOLETE_MATH_DOUBLE + +#include "math_config.h" + +#define N (1 << EXP_TABLE_BITS) + +const struct exp_data __exp_data = { +// N/ln2 +.invln2N = 0x1.71547652b82fep0 * N, +// -ln2/N +#if N == 64 +.negln2hiN = -0x1.62e42fefa0000p-7, +.negln2loN = -0x1.cf79abc9e3b3ap-46, +#elif N == 128 +.negln2hiN = -0x1.62e42fefa0000p-8, +.negln2loN = -0x1.cf79abc9e3b3ap-47, +#elif N == 256 +.negln2hiN = -0x1.62e42fefc0000p-9, +.negln2loN = 0x1.c610ca86c3899p-45, +#endif +// Used for rounding when !TOINT_INTRINSICS +#if EXP_USE_TOINT_NARROW +.shift = 0x1800000000.8p0, +#else +.shift = 0x1.8p52, +#endif +// exp polynomial coefficients. +.poly = { +#if N == 64 && EXP_POLY_ORDER == 5 && !EXP_POLY_WIDE +// abs error: 1.5543*2^-60 +// ulp error: 0.529 (0.533 without fma) +// if |x| < ln2/128+eps +// abs error if |x| < ln2/64: 1.7157*2^-50 +0x1.fffffffffdbcdp-2, +0x1.555555555444cp-3, +0x1.555573c6a9f7dp-5, +0x1.1111266d28935p-7, +#elif N == 64 && EXP_POLY_ORDER == 6 && EXP_POLY_WIDE +// abs error: 1.6735*2^-64 +// ulp error: 0.518 (0.522 without fma) +// if |x| < ln2/64 +0x1.5555555548f9ap-3, +0x1.555555554bf5dp-5, +0x1.11115b75f0f4dp-7, +0x1.6c171a6b6303ep-10, +#elif N == 128 && EXP_POLY_ORDER == 5 && !EXP_POLY_WIDE +// abs error: 1.555*2^-66 +// ulp error: 0.509 (0.511 without fma) +// if |x| < ln2/256+eps +// abs error if |x| < ln2/256+0x1p-15: 1.09*2^-65 +// abs error if |x| < ln2/128: 1.7145*2^-56 +0x1.ffffffffffdbdp-2, +0x1.555555555543cp-3, +0x1.55555cf172b91p-5, +0x1.1111167a4d017p-7, +#elif N == 128 && EXP_POLY_ORDER == 5 && EXP_POLY_WIDE +// abs error: 1.5542*2^-60 +// ulp error: 0.521 (0.523 without fma) +// if |x| < ln2/128 +0x1.fffffffffdbcep-2, +0x1.55555555543c2p-3, +0x1.555573c64f2e3p-5, +0x1.111126b4eff73p-7, +#elif N == 128 && EXP_POLY_ORDER == 6 && EXP_POLY_WIDE +// abs error: 1.6861*2^-71 +// ulp error: 0.509 (0.511 without fma) +// if |x| < ln2/128 +0x1.55555555548fdp-3, +0x1.555555555658fp-5, +0x1.111123a859bb6p-7, +0x1.6c16ba6920cabp-10, +#elif N == 256 && EXP_POLY_ORDER == 4 && !EXP_POLY_WIDE +// abs error: 1.43*2^-58 +// ulp error: 0.549 (0.550 without fma) +// if |x| < ln2/512 +0x1p0, // unused +0x1.fffffffffffd4p-2, +0x1.5555571d6ef9p-3, +0x1.5555576a5adcep-5, +#elif N == 256 && EXP_POLY_ORDER == 5 && EXP_POLY_WIDE +// abs error: 1.5547*2^-66 +// ulp error: 0.505 (0.506 without fma) +// if |x| < ln2/256 +0x1.ffffffffffdbdp-2, +0x1.555555555543cp-3, +0x1.55555cf16e1edp-5, +0x1.1111167a4b553p-7, +#endif +}, +.exp2_shift = 0x1.8p52 / N, +// exp2 polynomial coefficients. +.exp2_poly = { +#if N == 64 && EXP2_POLY_ORDER == 6 && EXP2_POLY_WIDE +// abs error: 1.3054*2^-63 +// ulp error: 0.515 +// if |x| < 1/64 +0x1.62e42fefa39efp-1, +0x1.ebfbdff82c58fp-3, +0x1.c6b08d7045cf1p-5, +0x1.3b2ab6fb8fd0ep-7, +0x1.5d884afec48d7p-10, +0x1.43097dc684ae1p-13, +#elif N == 128 && EXP2_POLY_ORDER == 5 && !EXP2_POLY_WIDE +// abs error: 1.2195*2^-65 +// ulp error: 0.507 (0.511 without fma) +// if |x| < 1/256 +// abs error if |x| < 1/128: 1.9941*2^-56 +0x1.62e42fefa39efp-1, +0x1.ebfbdff82c424p-3, +0x1.c6b08d70cf4b5p-5, +0x1.3b2abd24650ccp-7, +0x1.5d7e09b4e3a84p-10, +#elif N == 256 && EXP2_POLY_ORDER == 5 && EXP2_POLY_WIDE +// abs error: 1.2195*2^-65 +// ulp error: 0.504 (0.508 without fma) +// if |x| < 1/256 +0x1.62e42fefa39efp-1, +0x1.ebfbdff82c424p-3, +0x1.c6b08d70cf4b5p-5, +0x1.3b2abd24650ccp-7, +0x1.5d7e09b4e3a84p-10, +#endif +}, +// 2^(k/N) ~= H[k]*(1 + T[k]) for int k in [0,N) +// tab[2*k] = asuint64(T[k]) +// tab[2*k+1] = asuint64(H[k]) - (k << 52)/N +.tab = { +#if N == 64 +0x0, 0x3ff0000000000000, +0xbc7160139cd8dc5d, 0x3fefec9a3e778061, +0x3c8cd2523567f613, 0x3fefd9b0d3158574, +0x3c60f74e61e6c861, 0x3fefc74518759bc8, +0x3c979aa65d837b6d, 0x3fefb5586cf9890f, +0x3c3ebe3d702f9cd1, 0x3fefa3ec32d3d1a2, +0xbc9556522a2fbd0e, 0x3fef9301d0125b51, +0xbc91c923b9d5f416, 0x3fef829aaea92de0, +0xbc801b15eaa59348, 0x3fef72b83c7d517b, +0x3c8b898c3f1353bf, 0x3fef635beb6fcb75, +0x3c9aecf73e3a2f60, 0x3fef54873168b9aa, +0x3c8a6f4144a6c38d, 0x3fef463b88628cd6, +0x3c968efde3a8a894, 0x3fef387a6e756238, +0x3c80472b981fe7f2, 0x3fef2b4565e27cdd, +0x3c82f7e16d09ab31, 0x3fef1e9df51fdee1, +0x3c8b3782720c0ab4, 0x3fef1285a6e4030b, +0x3c834d754db0abb6, 0x3fef06fe0a31b715, +0x3c8fdd395dd3f84a, 0x3feefc08b26416ff, +0xbc924aedcc4b5068, 0x3feef1a7373aa9cb, +0xbc71d1e83e9436d2, 0x3feee7db34e59ff7, +0x3c859f48a72a4c6d, 0x3feedea64c123422, +0xbc58a78f4817895b, 0x3feed60a21f72e2a, +0x3c4363ed60c2ac11, 0x3feece086061892d, +0x3c6ecce1daa10379, 0x3feec6a2b5c13cd0, +0x3c7690cebb7aafb0, 0x3feebfdad5362a27, +0xbc8f94340071a38e, 0x3feeb9b2769d2ca7, +0xbc78dec6bd0f385f, 0x3feeb42b569d4f82, +0x3c93350518fdd78e, 0x3feeaf4736b527da, +0x3c9063e1e21c5409, 0x3feeab07dd485429, +0x3c9432e62b64c035, 0x3feea76f15ad2148, +0xbc8c33c53bef4da8, 0x3feea47eb03a5585, +0xbc93cedd78565858, 0x3feea23882552225, +0xbc93b3efbf5e2228, 0x3feea09e667f3bcd, +0xbc6367efb86da9ee, 0x3fee9fb23c651a2f, +0xbc781f647e5a3ecf, 0x3fee9f75e8ec5f74, +0xbc8619321e55e68a, 0x3fee9feb564267c9, +0xbc7b32dcb94da51d, 0x3feea11473eb0187, +0x3c65ebe1abd66c55, 0x3feea2f336cf4e62, +0xbc9369b6f13b3734, 0x3feea589994cce13, +0xbc94d450d872576e, 0x3feea8d99b4492ed, +0x3c8db72fc1f0eab4, 0x3feeace5422aa0db, +0x3c7bf68359f35f44, 0x3feeb1ae99157736, +0xbc5da9b88b6c1e29, 0x3feeb737b0cdc5e5, +0xbc92434322f4f9aa, 0x3feebd829fde4e50, +0x3c71affc2b91ce27, 0x3feec49182a3f090, +0xbc87c50422622263, 0x3feecc667b5de565, +0xbc91bbd1d3bcbb15, 0x3feed503b23e255d, +0x3c8469846e735ab3, 0x3feede6b5579fdbf, +0x3c8c1a7792cb3387, 0x3feee89f995ad3ad, +0xbc55c3d956dcaeba, 0x3feef3a2b84f15fb, +0xbc68d6f438ad9334, 0x3feeff76f2fb5e47, +0x3c74ffd70a5fddcd, 0x3fef0c1e904bc1d2, +0x3c736eae30af0cb3, 0x3fef199bdd85529c, +0x3c84e08fd10959ac, 0x3fef27f12e57d14b, +0x3c676b2c6c921968, 0x3fef3720dcef9069, +0xbc8fad5d3ffffa6f, 0x3fef472d4a07897c, +0x3c74a385a63d07a7, 0x3fef5818dcfba487, +0x3c8e5a50d5c192ac, 0x3fef69e603db3285, +0xbc82d52107b43e1f, 0x3fef7c97337b9b5f, +0x3c74b604603a88d3, 0x3fef902ee78b3ff6, +0xbc8ff7128fd391f0, 0x3fefa4afa2a490da, +0x3c8ec3bc41aa2008, 0x3fefba1bee615a27, +0x3c8a64a931d185ee, 0x3fefd0765b6e4540, +0x3c77893b4d91cd9d, 0x3fefe7c1819e90d8, +#elif N == 128 +0x0, 0x3ff0000000000000, +0x3c9b3b4f1a88bf6e, 0x3feff63da9fb3335, +0xbc7160139cd8dc5d, 0x3fefec9a3e778061, +0xbc905e7a108766d1, 0x3fefe315e86e7f85, +0x3c8cd2523567f613, 0x3fefd9b0d3158574, +0xbc8bce8023f98efa, 0x3fefd06b29ddf6de, +0x3c60f74e61e6c861, 0x3fefc74518759bc8, +0x3c90a3e45b33d399, 0x3fefbe3ecac6f383, +0x3c979aa65d837b6d, 0x3fefb5586cf9890f, +0x3c8eb51a92fdeffc, 0x3fefac922b7247f7, +0x3c3ebe3d702f9cd1, 0x3fefa3ec32d3d1a2, +0xbc6a033489906e0b, 0x3fef9b66affed31b, +0xbc9556522a2fbd0e, 0x3fef9301d0125b51, +0xbc5080ef8c4eea55, 0x3fef8abdc06c31cc, +0xbc91c923b9d5f416, 0x3fef829aaea92de0, +0x3c80d3e3e95c55af, 0x3fef7a98c8a58e51, +0xbc801b15eaa59348, 0x3fef72b83c7d517b, +0xbc8f1ff055de323d, 0x3fef6af9388c8dea, +0x3c8b898c3f1353bf, 0x3fef635beb6fcb75, +0xbc96d99c7611eb26, 0x3fef5be084045cd4, +0x3c9aecf73e3a2f60, 0x3fef54873168b9aa, +0xbc8fe782cb86389d, 0x3fef4d5022fcd91d, +0x3c8a6f4144a6c38d, 0x3fef463b88628cd6, +0x3c807a05b0e4047d, 0x3fef3f49917ddc96, +0x3c968efde3a8a894, 0x3fef387a6e756238, +0x3c875e18f274487d, 0x3fef31ce4fb2a63f, +0x3c80472b981fe7f2, 0x3fef2b4565e27cdd, +0xbc96b87b3f71085e, 0x3fef24dfe1f56381, +0x3c82f7e16d09ab31, 0x3fef1e9df51fdee1, +0xbc3d219b1a6fbffa, 0x3fef187fd0dad990, +0x3c8b3782720c0ab4, 0x3fef1285a6e4030b, +0x3c6e149289cecb8f, 0x3fef0cafa93e2f56, +0x3c834d754db0abb6, 0x3fef06fe0a31b715, +0x3c864201e2ac744c, 0x3fef0170fc4cd831, +0x3c8fdd395dd3f84a, 0x3feefc08b26416ff, +0xbc86a3803b8e5b04, 0x3feef6c55f929ff1, +0xbc924aedcc4b5068, 0x3feef1a7373aa9cb, +0xbc9907f81b512d8e, 0x3feeecae6d05d866, +0xbc71d1e83e9436d2, 0x3feee7db34e59ff7, +0xbc991919b3ce1b15, 0x3feee32dc313a8e5, +0x3c859f48a72a4c6d, 0x3feedea64c123422, +0xbc9312607a28698a, 0x3feeda4504ac801c, +0xbc58a78f4817895b, 0x3feed60a21f72e2a, +0xbc7c2c9b67499a1b, 0x3feed1f5d950a897, +0x3c4363ed60c2ac11, 0x3feece086061892d, +0x3c9666093b0664ef, 0x3feeca41ed1d0057, +0x3c6ecce1daa10379, 0x3feec6a2b5c13cd0, +0x3c93ff8e3f0f1230, 0x3feec32af0d7d3de, +0x3c7690cebb7aafb0, 0x3feebfdad5362a27, +0x3c931dbdeb54e077, 0x3feebcb299fddd0d, +0xbc8f94340071a38e, 0x3feeb9b2769d2ca7, +0xbc87deccdc93a349, 0x3feeb6daa2cf6642, +0xbc78dec6bd0f385f, 0x3feeb42b569d4f82, +0xbc861246ec7b5cf6, 0x3feeb1a4ca5d920f, +0x3c93350518fdd78e, 0x3feeaf4736b527da, +0x3c7b98b72f8a9b05, 0x3feead12d497c7fd, +0x3c9063e1e21c5409, 0x3feeab07dd485429, +0x3c34c7855019c6ea, 0x3feea9268a5946b7, +0x3c9432e62b64c035, 0x3feea76f15ad2148, +0xbc8ce44a6199769f, 0x3feea5e1b976dc09, +0xbc8c33c53bef4da8, 0x3feea47eb03a5585, +0xbc845378892be9ae, 0x3feea34634ccc320, +0xbc93cedd78565858, 0x3feea23882552225, +0x3c5710aa807e1964, 0x3feea155d44ca973, +0xbc93b3efbf5e2228, 0x3feea09e667f3bcd, +0xbc6a12ad8734b982, 0x3feea012750bdabf, +0xbc6367efb86da9ee, 0x3fee9fb23c651a2f, +0xbc80dc3d54e08851, 0x3fee9f7df9519484, +0xbc781f647e5a3ecf, 0x3fee9f75e8ec5f74, +0xbc86ee4ac08b7db0, 0x3fee9f9a48a58174, +0xbc8619321e55e68a, 0x3fee9feb564267c9, +0x3c909ccb5e09d4d3, 0x3feea0694fde5d3f, +0xbc7b32dcb94da51d, 0x3feea11473eb0187, +0x3c94ecfd5467c06b, 0x3feea1ed0130c132, +0x3c65ebe1abd66c55, 0x3feea2f336cf4e62, +0xbc88a1c52fb3cf42, 0x3feea427543e1a12, +0xbc9369b6f13b3734, 0x3feea589994cce13, +0xbc805e843a19ff1e, 0x3feea71a4623c7ad, +0xbc94d450d872576e, 0x3feea8d99b4492ed, +0x3c90ad675b0e8a00, 0x3feeaac7d98a6699, +0x3c8db72fc1f0eab4, 0x3feeace5422aa0db, +0xbc65b6609cc5e7ff, 0x3feeaf3216b5448c, +0x3c7bf68359f35f44, 0x3feeb1ae99157736, +0xbc93091fa71e3d83, 0x3feeb45b0b91ffc6, +0xbc5da9b88b6c1e29, 0x3feeb737b0cdc5e5, +0xbc6c23f97c90b959, 0x3feeba44cbc8520f, +0xbc92434322f4f9aa, 0x3feebd829fde4e50, +0xbc85ca6cd7668e4b, 0x3feec0f170ca07ba, +0x3c71affc2b91ce27, 0x3feec49182a3f090, +0x3c6dd235e10a73bb, 0x3feec86319e32323, +0xbc87c50422622263, 0x3feecc667b5de565, +0x3c8b1c86e3e231d5, 0x3feed09bec4a2d33, +0xbc91bbd1d3bcbb15, 0x3feed503b23e255d, +0x3c90cc319cee31d2, 0x3feed99e1330b358, +0x3c8469846e735ab3, 0x3feede6b5579fdbf, +0xbc82dfcd978e9db4, 0x3feee36bbfd3f37a, +0x3c8c1a7792cb3387, 0x3feee89f995ad3ad, +0xbc907b8f4ad1d9fa, 0x3feeee07298db666, +0xbc55c3d956dcaeba, 0x3feef3a2b84f15fb, +0xbc90a40e3da6f640, 0x3feef9728de5593a, +0xbc68d6f438ad9334, 0x3feeff76f2fb5e47, +0xbc91eee26b588a35, 0x3fef05b030a1064a, +0x3c74ffd70a5fddcd, 0x3fef0c1e904bc1d2, +0xbc91bdfbfa9298ac, 0x3fef12c25bd71e09, +0x3c736eae30af0cb3, 0x3fef199bdd85529c, +0x3c8ee3325c9ffd94, 0x3fef20ab5fffd07a, +0x3c84e08fd10959ac, 0x3fef27f12e57d14b, +0x3c63cdaf384e1a67, 0x3fef2f6d9406e7b5, +0x3c676b2c6c921968, 0x3fef3720dcef9069, +0xbc808a1883ccb5d2, 0x3fef3f0b555dc3fa, +0xbc8fad5d3ffffa6f, 0x3fef472d4a07897c, +0xbc900dae3875a949, 0x3fef4f87080d89f2, +0x3c74a385a63d07a7, 0x3fef5818dcfba487, +0xbc82919e2040220f, 0x3fef60e316c98398, +0x3c8e5a50d5c192ac, 0x3fef69e603db3285, +0x3c843a59ac016b4b, 0x3fef7321f301b460, +0xbc82d52107b43e1f, 0x3fef7c97337b9b5f, +0xbc892ab93b470dc9, 0x3fef864614f5a129, +0x3c74b604603a88d3, 0x3fef902ee78b3ff6, +0x3c83c5ec519d7271, 0x3fef9a51fbc74c83, +0xbc8ff7128fd391f0, 0x3fefa4afa2a490da, +0xbc8dae98e223747d, 0x3fefaf482d8e67f1, +0x3c8ec3bc41aa2008, 0x3fefba1bee615a27, +0x3c842b94c3a9eb32, 0x3fefc52b376bba97, +0x3c8a64a931d185ee, 0x3fefd0765b6e4540, +0xbc8e37bae43be3ed, 0x3fefdbfdad9cbe14, +0x3c77893b4d91cd9d, 0x3fefe7c1819e90d8, +0x3c5305c14160cc89, 0x3feff3c22b8f71f1, +#elif N == 256 +0x0, 0x3ff0000000000000, +0xbc84e82fc61851ac, 0x3feffb1afa5abcbf, +0x3c9b3b4f1a88bf6e, 0x3feff63da9fb3335, +0xbc82985dd8521d32, 0x3feff168143b0281, +0xbc7160139cd8dc5d, 0x3fefec9a3e778061, +0x3c651e617061bfbd, 0x3fefe7d42e11bbcc, +0xbc905e7a108766d1, 0x3fefe315e86e7f85, +0x3c845fad437fa426, 0x3fefde5f72f654b1, +0x3c8cd2523567f613, 0x3fefd9b0d3158574, +0xbc954529642b232f, 0x3fefd50a0e3c1f89, +0xbc8bce8023f98efa, 0x3fefd06b29ddf6de, +0x3c8293708ef5c32e, 0x3fefcbd42b72a836, +0x3c60f74e61e6c861, 0x3fefc74518759bc8, +0xbc95b9280905b2a4, 0x3fefc2bdf66607e0, +0x3c90a3e45b33d399, 0x3fefbe3ecac6f383, +0x3c84f31f32c4b7e7, 0x3fefb9c79b1f3919, +0x3c979aa65d837b6d, 0x3fefb5586cf9890f, +0x3c9407fb30d06420, 0x3fefb0f145e46c85, +0x3c8eb51a92fdeffc, 0x3fefac922b7247f7, +0xbc9a5d04b3b9911b, 0x3fefa83b23395dec, +0x3c3ebe3d702f9cd1, 0x3fefa3ec32d3d1a2, +0xbc937a01f0739546, 0x3fef9fa55fdfa9c5, +0xbc6a033489906e0b, 0x3fef9b66affed31b, +0x3c8b8268b04ef0a5, 0x3fef973028d7233e, +0xbc9556522a2fbd0e, 0x3fef9301d0125b51, +0xbc9ac46e44a2ebcc, 0x3fef8edbab5e2ab6, +0xbc5080ef8c4eea55, 0x3fef8abdc06c31cc, +0xbc65704e90c9f860, 0x3fef86a814f204ab, +0xbc91c923b9d5f416, 0x3fef829aaea92de0, +0xbc897cea57e46280, 0x3fef7e95934f312e, +0x3c80d3e3e95c55af, 0x3fef7a98c8a58e51, +0x3c56f01429e2b9d2, 0x3fef76a45471c3c2, +0xbc801b15eaa59348, 0x3fef72b83c7d517b, +0x3c6e653b2459034b, 0x3fef6ed48695bbc0, +0xbc8f1ff055de323d, 0x3fef6af9388c8dea, +0x3c92cc7ea345b7dc, 0x3fef672658375d2f, +0x3c8b898c3f1353bf, 0x3fef635beb6fcb75, +0x3c957bfb2876ea9e, 0x3fef5f99f8138a1c, +0xbc96d99c7611eb26, 0x3fef5be084045cd4, +0x3c8cdc1873af2155, 0x3fef582f95281c6b, +0x3c9aecf73e3a2f60, 0x3fef54873168b9aa, +0xbc9493684653a131, 0x3fef50e75eb44027, +0xbc8fe782cb86389d, 0x3fef4d5022fcd91d, +0xbc98e2899077520a, 0x3fef49c18438ce4d, +0x3c8a6f4144a6c38d, 0x3fef463b88628cd6, +0x3c9120fcd4f59273, 0x3fef42be3578a819, +0x3c807a05b0e4047d, 0x3fef3f49917ddc96, +0x3c89b788c188c9b8, 0x3fef3bdda27912d1, +0x3c968efde3a8a894, 0x3fef387a6e756238, +0x3c877afbca90ef84, 0x3fef351ffb82140a, +0x3c875e18f274487d, 0x3fef31ce4fb2a63f, +0x3c91512f082876ee, 0x3fef2e85711ece75, +0x3c80472b981fe7f2, 0x3fef2b4565e27cdd, +0x3c9a02f0c7d75ec6, 0x3fef280e341ddf29, +0xbc96b87b3f71085e, 0x3fef24dfe1f56381, +0xbc803297e78260bf, 0x3fef21ba7591bb70, +0x3c82f7e16d09ab31, 0x3fef1e9df51fdee1, +0xbc95b77e5ccd9fbf, 0x3fef1b8a66d10f13, +0xbc3d219b1a6fbffa, 0x3fef187fd0dad990, +0xbc91e75c40b4251e, 0x3fef157e39771b2f, +0x3c8b3782720c0ab4, 0x3fef1285a6e4030b, +0x3c98a911f1f7785a, 0x3fef0f961f641589, +0x3c6e149289cecb8f, 0x3fef0cafa93e2f56, +0xbc61e7c998db7dbb, 0x3fef09d24abd886b, +0x3c834d754db0abb6, 0x3fef06fe0a31b715, +0x3c85425c11faadf4, 0x3fef0432edeeb2fd, +0x3c864201e2ac744c, 0x3fef0170fc4cd831, +0xbc979517a03e2847, 0x3feefeb83ba8ea32, +0x3c8fdd395dd3f84a, 0x3feefc08b26416ff, +0xbc800e2a46da4bee, 0x3feef96266e3fa2d, +0xbc86a3803b8e5b04, 0x3feef6c55f929ff1, +0xbc87430803972b34, 0x3feef431a2de883b, +0xbc924aedcc4b5068, 0x3feef1a7373aa9cb, +0xbc954de30ae02d94, 0x3feeef26231e754a, +0xbc9907f81b512d8e, 0x3feeecae6d05d866, +0xbc94f2487e1c03ec, 0x3feeea401b7140ef, +0xbc71d1e83e9436d2, 0x3feee7db34e59ff7, +0x3c914a5432fcb2f4, 0x3feee57fbfec6cf4, +0xbc991919b3ce1b15, 0x3feee32dc313a8e5, +0x3c79c3bba5562a2f, 0x3feee0e544ede173, +0x3c859f48a72a4c6d, 0x3feedea64c123422, +0xbc85a71612e21658, 0x3feedc70df1c5175, +0xbc9312607a28698a, 0x3feeda4504ac801c, +0x3c86421f6f1d24d6, 0x3feed822c367a024, +0xbc58a78f4817895b, 0x3feed60a21f72e2a, +0xbc9348a6815fce65, 0x3feed3fb2709468a, +0xbc7c2c9b67499a1b, 0x3feed1f5d950a897, +0x3c835c43984d9871, 0x3feecffa3f84b9d4, +0x3c4363ed60c2ac11, 0x3feece086061892d, +0xbc632afc8d9473a0, 0x3feecc2042a7d232, +0x3c9666093b0664ef, 0x3feeca41ed1d0057, +0xbc95fc5e44de020e, 0x3feec86d668b3237, +0x3c6ecce1daa10379, 0x3feec6a2b5c13cd0, +0xbc7ea0148327c42f, 0x3feec4e1e192aed2, +0x3c93ff8e3f0f1230, 0x3feec32af0d7d3de, +0xbc7a843ad1a88022, 0x3feec17dea6db7d7, +0x3c7690cebb7aafb0, 0x3feebfdad5362a27, +0x3c892ca3bf144e63, 0x3feebe41b817c114, +0x3c931dbdeb54e077, 0x3feebcb299fddd0d, +0xbc902c99b04aa8b0, 0x3feebb2d81d8abff, +0xbc8f94340071a38e, 0x3feeb9b2769d2ca7, +0x3c73e34f67e67118, 0x3feeb8417f4531ee, +0xbc87deccdc93a349, 0x3feeb6daa2cf6642, +0xbc75a3b1197ba0f0, 0x3feeb57de83f4eef, +0xbc78dec6bd0f385f, 0x3feeb42b569d4f82, +0x3c81bd2888075068, 0x3feeb2e2f4f6ad27, +0xbc861246ec7b5cf6, 0x3feeb1a4ca5d920f, +0xbc896be8ae89ef8f, 0x3feeb070dde910d2, +0x3c93350518fdd78e, 0x3feeaf4736b527da, +0xbc88e6ac90348602, 0x3feeae27dbe2c4cf, +0x3c7b98b72f8a9b05, 0x3feead12d497c7fd, +0xbc91af7f1365c3ac, 0x3feeac0827ff07cc, +0x3c9063e1e21c5409, 0x3feeab07dd485429, +0xbc943a3540d1898a, 0x3feeaa11fba87a03, +0x3c34c7855019c6ea, 0x3feea9268a5946b7, +0xbc951f58ddaa8090, 0x3feea84590998b93, +0x3c9432e62b64c035, 0x3feea76f15ad2148, +0xbc82e1648e50a17c, 0x3feea6a320dceb71, +0xbc8ce44a6199769f, 0x3feea5e1b976dc09, +0x3c95f30eda98a575, 0x3feea52ae6cdf6f4, +0xbc8c33c53bef4da8, 0x3feea47eb03a5585, +0x3c917ecda8a72159, 0x3feea3dd1d1929fd, +0xbc845378892be9ae, 0x3feea34634ccc320, +0xbc9345f3cee1ae6e, 0x3feea2b9febc8fb7, +0xbc93cedd78565858, 0x3feea23882552225, +0xbc85c33fdf910406, 0x3feea1c1c70833f6, +0x3c5710aa807e1964, 0x3feea155d44ca973, +0x3c81079ab5789604, 0x3feea0f4b19e9538, +0xbc93b3efbf5e2228, 0x3feea09e667f3bcd, +0x3c727df161cd7778, 0x3feea052fa75173e, +0xbc6a12ad8734b982, 0x3feea012750bdabf, +0x3c93f9924a05b767, 0x3fee9fdcddd47645, +0xbc6367efb86da9ee, 0x3fee9fb23c651a2f, +0xbc87557939a8b5ef, 0x3fee9f9298593ae5, +0xbc80dc3d54e08851, 0x3fee9f7df9519484, +0x3c51ed2f56fa9d1a, 0x3fee9f7466f42e87, +0xbc781f647e5a3ecf, 0x3fee9f75e8ec5f74, +0xbc88e67a9006c909, 0x3fee9f8286ead08a, +0xbc86ee4ac08b7db0, 0x3fee9f9a48a58174, +0x3c86597566977ac8, 0x3fee9fbd35d7cbfd, +0xbc8619321e55e68a, 0x3fee9feb564267c9, +0x3c92c0b7028a5c3a, 0x3feea024b1ab6e09, +0x3c909ccb5e09d4d3, 0x3feea0694fde5d3f, +0x3c8a30faf49cc78c, 0x3feea0b938ac1cf6, +0xbc7b32dcb94da51d, 0x3feea11473eb0187, +0xbc92dad3519d7b5b, 0x3feea17b0976cfdb, +0x3c94ecfd5467c06b, 0x3feea1ed0130c132, +0x3c87d51410fd15c2, 0x3feea26a62ff86f0, +0x3c65ebe1abd66c55, 0x3feea2f336cf4e62, +0xbc760a3629969871, 0x3feea3878491c491, +0xbc88a1c52fb3cf42, 0x3feea427543e1a12, +0x3c8b18c6e3fdef5d, 0x3feea4d2add106d9, +0xbc9369b6f13b3734, 0x3feea589994cce13, +0x3c90ec1ddcb1390a, 0x3feea64c1eb941f7, +0xbc805e843a19ff1e, 0x3feea71a4623c7ad, +0xbc522cea4f3afa1e, 0x3feea7f4179f5b21, +0xbc94d450d872576e, 0x3feea8d99b4492ed, +0x3c7c88549b958471, 0x3feea9cad931a436, +0x3c90ad675b0e8a00, 0x3feeaac7d98a6699, +0x3c931143962f7877, 0x3feeabd0a478580f, +0x3c8db72fc1f0eab4, 0x3feeace5422aa0db, +0x3c93e9e96f112479, 0x3feeae05bad61778, +0xbc65b6609cc5e7ff, 0x3feeaf3216b5448c, +0xbc8dac42a4a38df0, 0x3feeb06a5e0866d9, +0x3c7bf68359f35f44, 0x3feeb1ae99157736, +0x3c8b99dd98b1ed84, 0x3feeb2fed0282c8a, +0xbc93091fa71e3d83, 0x3feeb45b0b91ffc6, +0xbc7885ad50cbb750, 0x3feeb5c353aa2fe2, +0xbc5da9b88b6c1e29, 0x3feeb737b0cdc5e5, +0xbc82d5e85f3e0301, 0x3feeb8b82b5f98e5, +0xbc6c23f97c90b959, 0x3feeba44cbc8520f, +0xbc51669428996971, 0x3feebbdd9a7670b3, +0xbc92434322f4f9aa, 0x3feebd829fde4e50, +0x3c71f2b2c1c4c014, 0x3feebf33e47a22a2, +0xbc85ca6cd7668e4b, 0x3feec0f170ca07ba, +0xbc9294f304f166b6, 0x3feec2bb4d53fe0d, +0x3c71affc2b91ce27, 0x3feec49182a3f090, +0xbc8a1e58414c07d3, 0x3feec674194bb8d5, +0x3c6dd235e10a73bb, 0x3feec86319e32323, +0xbc79740b58a20091, 0x3feeca5e8d07f29e, +0xbc87c50422622263, 0x3feecc667b5de565, +0x3c9165830a2b96c2, 0x3feece7aed8eb8bb, +0x3c8b1c86e3e231d5, 0x3feed09bec4a2d33, +0xbc903d5cbe27874b, 0x3feed2c980460ad8, +0xbc91bbd1d3bcbb15, 0x3feed503b23e255d, +0x3c5986178980fce0, 0x3feed74a8af46052, +0x3c90cc319cee31d2, 0x3feed99e1330b358, +0xbc89472975b1f2a5, 0x3feedbfe53c12e59, +0x3c8469846e735ab3, 0x3feede6b5579fdbf, +0x3c7d8157a34b7e7f, 0x3feee0e521356eba, +0xbc82dfcd978e9db4, 0x3feee36bbfd3f37a, +0x3c8c8a4e231ebb7d, 0x3feee5ff3a3c2774, +0x3c8c1a7792cb3387, 0x3feee89f995ad3ad, +0xbc888c8d11a142e5, 0x3feeeb4ce622f2ff, +0xbc907b8f4ad1d9fa, 0x3feeee07298db666, +0x3c889c2ea41433c7, 0x3feef0ce6c9a8952, +0xbc55c3d956dcaeba, 0x3feef3a2b84f15fb, +0xbc7274aedac8ff80, 0x3feef68415b749b1, +0xbc90a40e3da6f640, 0x3feef9728de5593a, +0x3c85c620ce76df06, 0x3feefc6e29f1c52a, +0xbc68d6f438ad9334, 0x3feeff76f2fb5e47, +0xbc8fda52e1b51e41, 0x3fef028cf22749e4, +0xbc91eee26b588a35, 0x3fef05b030a1064a, +0xbc32141a7b3e2cd8, 0x3fef08e0b79a6f1f, +0x3c74ffd70a5fddcd, 0x3fef0c1e904bc1d2, +0xbc302899507554e5, 0x3fef0f69c3f3a207, +0xbc91bdfbfa9298ac, 0x3fef12c25bd71e09, +0xbc80dda2d4c0010c, 0x3fef16286141b33d, +0x3c736eae30af0cb3, 0x3fef199bdd85529c, +0xbc8a007daadf8d68, 0x3fef1d1cd9fa652c, +0x3c8ee3325c9ffd94, 0x3fef20ab5fffd07a, +0x3c836909391181d3, 0x3fef244778fafb22, +0x3c84e08fd10959ac, 0x3fef27f12e57d14b, +0xbc811cd7dbdf9547, 0x3fef2ba88988c933, +0x3c63cdaf384e1a67, 0x3fef2f6d9406e7b5, +0xbc7ac28b7bef6621, 0x3fef33405751c4db, +0x3c676b2c6c921968, 0x3fef3720dcef9069, +0xbc7030587207b9e1, 0x3fef3b0f2e6d1675, +0xbc808a1883ccb5d2, 0x3fef3f0b555dc3fa, +0xbc8cc734592af7fc, 0x3fef43155b5bab74, +0xbc8fad5d3ffffa6f, 0x3fef472d4a07897c, +0x3c87752a44f587e8, 0x3fef4b532b08c968, +0xbc900dae3875a949, 0x3fef4f87080d89f2, +0x3c85b66fefeef52e, 0x3fef53c8eacaa1d6, +0x3c74a385a63d07a7, 0x3fef5818dcfba487, +0x3c5159d9d908a96e, 0x3fef5c76e862e6d3, +0xbc82919e2040220f, 0x3fef60e316c98398, +0x3c8c254d16117a68, 0x3fef655d71ff6075, +0x3c8e5a50d5c192ac, 0x3fef69e603db3285, +0xbc8d8c329fbd0e03, 0x3fef6e7cd63a8315, +0x3c843a59ac016b4b, 0x3fef7321f301b460, +0xbc8ea6e6fbd5f2a6, 0x3fef77d5641c0658, +0xbc82d52107b43e1f, 0x3fef7c97337b9b5f, +0xbc63e8e3eab2cbb4, 0x3fef81676b197d17, +0xbc892ab93b470dc9, 0x3fef864614f5a129, +0xbc8b7966cd0d2cd9, 0x3fef8b333b16ee12, +0x3c74b604603a88d3, 0x3fef902ee78b3ff6, +0xbc776caa4c2ff1cf, 0x3fef953924676d76, +0x3c83c5ec519d7271, 0x3fef9a51fbc74c83, +0xbc81d5fc525d9940, 0x3fef9f7977cdb740, +0xbc8ff7128fd391f0, 0x3fefa4afa2a490da, +0x3c855cd8aaea3d21, 0x3fefa9f4867cca6e, +0xbc8dae98e223747d, 0x3fefaf482d8e67f1, +0x3c8269947c2bed4a, 0x3fefb4aaa2188510, +0x3c8ec3bc41aa2008, 0x3fefba1bee615a27, +0xbc83b6137e9afe9e, 0x3fefbf9c1cb6412a, +0x3c842b94c3a9eb32, 0x3fefc52b376bba97, +0xbc69fa74878ba7c7, 0x3fefcac948dd7274, +0x3c8a64a931d185ee, 0x3fefd0765b6e4540, +0x3c901f3a75ee0efe, 0x3fefd632798844f8, +0xbc8e37bae43be3ed, 0x3fefdbfdad9cbe14, +0xbc516a9ce6ed84fa, 0x3fefe1d802243c89, +0x3c77893b4d91cd9d, 0x3fefe7c1819e90d8, +0xbc699c7db2effc76, 0x3fefedba3692d514, +0x3c5305c14160cc89, 0x3feff3c22b8f71f1, +0x3c64b458677f9840, 0x3feff9d96b2a23d9, +#endif +}, +}; +#endif /* __OBSOLETE_MATH_DOUBLE */ diff --git a/src/audio/ffmpeg_dec/libm/common/fabsl.c b/src/audio/ffmpeg_dec/libm/common/fabsl.c new file mode 100644 index 000000000000..a11fcfc18f2e --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/fabsl.c @@ -0,0 +1,47 @@ +/* +(C) Copyright IBM Corp. 2009 + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +* Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +* Neither the name of IBM nor the names of its contributors may be +used to endorse or promote products derived from this software without +specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +*/ + +#include +#include "local.h" + +#ifdef _NEED_FLOAT_HUGE + +long double +fabsl (long double x) +{ + if (x == 0.0l) + return 0.0l; + if (x < 0.0l) + return -x; + return x; +} + +#endif + diff --git a/src/audio/ffmpeg_dec/libm/common/fdlibm.h b/src/audio/ffmpeg_dec/libm/common/fdlibm.h new file mode 100644 index 000000000000..2e49795ac1c5 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/fdlibm.h @@ -0,0 +1,404 @@ + +/* @(#)fdlibm.h 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#ifndef _FDLIBM_H_ +#define _FDLIBM_H_ + +/* REDHAT LOCAL: Include files. */ +#ifndef _DEFAULT_SOURCE +#define _DEFAULT_SOURCE +#endif +#include +#include +#include +#include +#include "math_config.h" + +/* Most routines need to check whether a float is finite, infinite, or not a + number, and many need to know whether the result of an operation will + overflow. These conditions depend on whether the largest exponent is + used for NaNs & infinities, or whether it's used for finite numbers. The + macros below wrap up that kind of information: + + FLT_UWORD_IS_FINITE(X) + True if a positive float with bitmask X is finite. + + FLT_UWORD_IS_NAN(X) + True if a positive float with bitmask X is not a number. + + FLT_UWORD_IS_INFINITE(X) + True if a positive float with bitmask X is +infinity. + + FLT_UWORD_MAX + The bitmask of FLT_MAX. + + FLT_UWORD_HALF_MAX + The bitmask of FLT_MAX/2. + + FLT_UWORD_EXP_MAX + The bitmask of the largest finite exponent (129 if the largest + exponent is used for finite numbers, 128 otherwise). + + FLT_UWORD_LOG_MAX + The bitmask of log(FLT_MAX), rounded down. This value is the largest + input that can be passed to exp() without producing overflow. + + FLT_UWORD_LOG_2MAX + The bitmask of log(2*FLT_MAX), rounded down. This value is the + largest input than can be passed to cosh() without producing + overflow. + + FLT_LARGEST_EXP + The largest biased exponent that can be used for finite numbers + (255 if the largest exponent is used for finite numbers, 254 + otherwise) */ + +#ifdef _FLT_LARGEST_EXPONENT_IS_NORMAL +#define FLT_UWORD_IS_FINITE(x) 1 +#define FLT_UWORD_IS_NAN(x) 0 +#define FLT_UWORD_IS_INFINITE(x) 0 +#define FLT_UWORD_MAX 0x7fffffff +#define FLT_UWORD_EXP_MAX 0x43010000 +#define FLT_UWORD_LOG_MAX 0x42b2d4fc +#define FLT_UWORD_LOG_2MAX 0x42b437e0 +#define HUGE ((float)0X1.FFFFFEP128) +#else +#define FLT_UWORD_IS_FINITE(x) ((x)<0x7f800000L) +#define FLT_UWORD_IS_NAN(x) ((x)>0x7f800000L) +#define FLT_UWORD_IS_INFINITE(x) ((x)==0x7f800000L) +#define FLT_UWORD_MAX 0x7f7fffffL +#define FLT_UWORD_EXP_MAX 0x43000000 +#define FLT_UWORD_LOG_MAX 0x42b17217 +#define FLT_UWORD_LOG_2MAX 0x42b2d4fc +#define HUGE ((float)3.40282346638528860e+38) +#endif +#define FLT_UWORD_HALF_MAX (FLT_UWORD_MAX-(1L<<23)) +#define FLT_LARGEST_EXP (FLT_UWORD_MAX>>23) + +/* rounding mode tests; nearest if not set. Assumes hardware + * without rounding mode support uses nearest + */ + +/* If there are rounding modes other than FE_TONEAREST defined, then + * add code to check which is active + */ +#if (defined(FE_UPWARD) + defined(FE_DOWNWARD) + defined(FE_TOWARDZERO)) >= 1 +#define FE_DECL_ROUND(v) int v = fegetround() +#define __is_nearest(r) ((r) == FE_TONEAREST) +#else +#define FE_DECL_ROUND(v) +#define __is_nearest(r) 1 +#endif + +#ifdef FE_UPWARD +#define __is_upward(r) ((r) == FE_UPWARD) +#else +#define __is_upward(r) 0 +#endif + +#ifdef FE_DOWNWARD +#define __is_downward(r) ((r) == FE_DOWNWARD) +#else +#define __is_downward(r) 0 +#endif + +#ifdef FE_TOWARDZERO +#define __is_towardzero(r) ((r) == FE_TOWARDZERO) +#else +#define __is_towardzero(r) 0 +#endif + +/* Many routines check for zero and subnormal numbers. Such things depend + on whether the target supports denormals or not: + + FLT_UWORD_IS_ZERO(X) + True if a positive float with bitmask X is +0. Without denormals, + any float with a zero exponent is a +0 representation. With + denormals, the only +0 representation is a 0 bitmask. + + FLT_UWORD_IS_SUBNORMAL(X) + True if a non-zero positive float with bitmask X is subnormal. + (Routines should check for zeros first.) + + FLT_UWORD_MIN + The bitmask of the smallest float above +0. Call this number + REAL_FLT_MIN... + + FLT_UWORD_EXP_MIN + The bitmask of the float representation of REAL_FLT_MIN's exponent. + + FLT_UWORD_LOG_MIN + The bitmask of |log(REAL_FLT_MIN)|, rounding down. + + FLT_SMALLEST_EXP + REAL_FLT_MIN's exponent - EXP_BIAS (1 if denormals are not supported, + -22 if they are). +*/ + +#ifdef _FLT_NO_DENORMALS +#define FLT_UWORD_IS_ZERO(x) ((x)<0x00800000L) +#define FLT_UWORD_IS_SUBNORMAL(x) 0 +#define FLT_UWORD_MIN 0x00800000 +#define FLT_UWORD_EXP_MIN 0x42fc0000 +#define FLT_UWORD_LOG_MIN 0x42aeac50 +#define FLT_SMALLEST_EXP 1 +#else +#define FLT_UWORD_IS_ZERO(x) ((x)==0) +#define FLT_UWORD_IS_SUBNORMAL(x) ((x)<0x00800000L) +#define FLT_UWORD_MIN 0x00000001 +#define FLT_UWORD_EXP_MIN 0x43160000 +#define FLT_UWORD_LOG_MIN 0x42cff1b5 +#define FLT_SMALLEST_EXP -22 +#endif + +/* + * set X_TLOSS = pi*2**52, which is possibly defined in + * (one may replace the following line by "#include ") + */ + +#define X_TLOSS 1.41484755040568800000e+16 + +#ifdef _NEED_FLOAT64 +extern __int32_t __rem_pio2 (__float64,__float64*); + +/* fdlibm kernel function */ +extern __float64 __kernel_sin (__float64,__float64,int); +extern __float64 __kernel_cos (__float64,__float64); +extern __float64 __kernel_tan (__float64,__float64,int); +extern int __kernel_rem_pio2 (__float64*,__float64*,int,int,int,const __int32_t*); +#endif + +extern __int32_t __rem_pio2f (float,float*); + +/* float versions of fdlibm kernel functions */ +extern float __kernel_sinf (float,float,int); +extern float __kernel_cosf (float,float); +extern float __kernel_tanf (float,float,int); +extern int __kernel_rem_pio2f (float*,float*,int,int,int,const __int32_t*); + +/* The original code used statements like + n0 = ((*(int*)&one)>>29)^1; * index of high word * + ix0 = *(n0+(int*)&x); * high word of x * + ix1 = *((1-n0)+(int*)&x); * low word of x * + to dig two 32 bit words out of the 64 bit IEEE floating point + value. That is non-ANSI, and, moreover, the gcc instruction + scheduler gets it wrong. We instead use the following macros. + Unlike the original code, we determine the endianness at compile + time, not at run time; I don't see much benefit to selecting + endianness at run time. */ + +#ifndef __IEEE_BIG_ENDIAN +#ifndef __IEEE_LITTLE_ENDIAN + #error Must define endianness +#endif +#endif + +/* A union which permits us to convert between a double and two 32 bit + ints. */ + +#ifdef __IEEE_BIG_ENDIAN + +typedef union +{ + uint64_t bits; + struct + { + __uint32_t msw; + __uint32_t lsw; + } parts; +} ieee_double_shape_type; + +#endif + +#ifdef __IEEE_LITTLE_ENDIAN + +typedef union +{ + uint64_t bits; + struct + { + __uint32_t lsw; + __uint32_t msw; + } parts; +} ieee_double_shape_type; + +#endif + +/* Get two 32 bit ints from a double. */ + +#define EXTRACT_WORDS(ix0,ix1,d) \ +do { \ + ieee_double_shape_type ew_u; \ + ew_u.bits = asuint64(d); \ + (ix0) = ew_u.parts.msw; \ + (ix1) = ew_u.parts.lsw; \ +} while (0) + +/* Get the more significant 32 bit int from a double. */ + +#define GET_HIGH_WORD(i,d) \ +do { \ + ieee_double_shape_type gh_u; \ + gh_u.bits = asuint64(d); \ + (i) = gh_u.parts.msw; \ +} while (0) + +/* Get the less significant 32 bit int from a double. */ + +#define GET_LOW_WORD(i,d) \ +do { \ + ieee_double_shape_type gl_u; \ + gl_u.bits = asuint64(d); \ + (i) = gl_u.parts.lsw; \ +} while (0) + +/* Set a double from two 32 bit ints. */ + +#define INSERT_WORDS(d,ix0,ix1) \ +do { \ + ieee_double_shape_type iw_u; \ + iw_u.parts.msw = (ix0); \ + iw_u.parts.lsw = (ix1); \ + (d) = asfloat64(iw_u.bits); \ +} while (0) + +/* Set the more significant 32 bits of a double from an int. */ + +#define SET_HIGH_WORD(d,v) \ +do { \ + ieee_double_shape_type sh_u; \ + sh_u.bits = asuint64(d); \ + sh_u.parts.msw = (v); \ + (d) = asfloat64(sh_u.bits); \ +} while (0) + +/* Set the less significant 32 bits of a double from an int. */ + +#define SET_LOW_WORD(d,v) \ +do { \ + ieee_double_shape_type sl_u; \ + sl_u.bits = asuint64(d); \ + sl_u.parts.lsw = (v); \ + (d) = asfloat64(sl_u.bits); \ +} while (0) + +/* A union which permits us to convert between a float and a 32 bit + int. */ + +/* Get a 32 bit int from a float. */ + +#define GET_FLOAT_WORD(i,d) ((i) = asuint(d)) + +/* Set a float from a 32 bit int. */ + +#define SET_FLOAT_WORD(d,i) ((d) = asfloat(i)) + +/* Macros to avoid undefined behaviour that can arise if the amount + of a shift is exactly equal to the size of the shifted operand. */ + +#define SAFE_LEFT_SHIFT(op,amt) \ + (((amt) < (int) (8 * sizeof(op))) ? ((op) << (amt)) : 0) + +#define SAFE_RIGHT_SHIFT(op,amt) \ + (((amt) < (int) (8 * sizeof(op))) ? ((op) >> (amt)) : 0) + +int +__undefined_shift_size(int x, int s); + +/* + * Shift left of negative integers is UB. Cast to unsigned, shift, + * cast back. + * + * Shift right of negative integers is ID. When we want arithmetic + * shifts, we use the shift operator directly when the sanitizer is + * disabled because the alternative is a whole lotta code. + */ +#define lsl(__x,__s) ((sizeof(__x) == sizeof(char)) ? \ + (__typeof(__x)) ((unsigned char) (__x) << (__s)) : \ + (sizeof(__x) == sizeof(short)) ? \ + (__typeof(__x)) ((unsigned short) (__x) << (__s)) : \ + (sizeof(__x) == sizeof(int)) ? \ + (__typeof(__x)) ((unsigned int) (__x) << (__s)) : \ + (sizeof(__x) == sizeof(long)) ? \ + (__typeof(__x)) ((unsigned long) (__x) << (__s)) : \ + (sizeof(__x) == sizeof(long long)) ? \ + (__typeof(__x)) ((unsigned long long) (__x) << (__s)) : \ + __undefined_shift_size(__x, __s)) + +#if defined(__has_feature) && __has_feature(undefined_behavior_sanitizer) + +/* + * Compute arithmetic right shift. For negative values, flip the bits, + * shift and flip back. Otherwise, just shift. Thanks to Per Vognsen + * for this version which both gcc and clang both currently optimize + * to a single asr instruction in small tests. + */ +#define __asr(t, n) \ + static inline t \ + __asr_ ## n(t x, int s) { \ + return x < 0 ? ~(~x >> s) : x >> s; \ + } \ + +__asr(signed char, signed_char) +__asr(short, short) +__asr(int, int) +__asr(long, long) +__asr(long long, long_long) + +#define asr(__x,__s) ((sizeof(__x) == sizeof(char)) ? \ + (__typeof(__x))__asr_signed_char(__x, __s) : \ + (sizeof(__x) == sizeof(short)) ? \ + (__typeof(__x))__asr_short(__x, __s) : \ + (sizeof(__x) == sizeof(int)) ? \ + (__typeof(__x))__asr_int(__x, __s) : \ + (sizeof(__x) == sizeof(long)) ? \ + (__typeof(__x))__asr_long(__x, __s) : \ + (sizeof(__x) == sizeof(long long)) ? \ + (__typeof(__x))__asr_long_long(__x, __s): \ + __undefined_shift_size(__x, __s)) +#else +#define asr(__x, __s) ((__x) >> (__s)) +#endif + +#ifdef _COMPLEX_H + +/* + * Quoting from ISO/IEC 9899:TC2: + * + * 6.2.5.13 Types + * Each complex type has the same representation and alignment requirements as + * an array type containing exactly two elements of the corresponding real type; + * the first element is equal to the real part, and the second element to the + * imaginary part, of the complex number. + */ +typedef union { + float complex z; + float parts[2]; +} float_complex; + +typedef union { + double complex z; + double parts[2]; +} double_complex; + +typedef union { + long double complex z; + long double parts[2]; +} long_double_complex; + +#define REAL_PART(z) ((z).parts[0]) +#define IMAG_PART(z) ((z).parts[1]) + +#endif /* _COMPLEX_H */ + +#endif /* _FDLIBM_H_ */ diff --git a/src/audio/ffmpeg_dec/libm/common/fma_inc.h b/src/audio/ffmpeg_dec/libm/common/fma_inc.h new file mode 100644 index 000000000000..cc01e6e187b0 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/fma_inc.h @@ -0,0 +1,304 @@ +/*- + * Copyright (c) 2005-2011 David Schultz + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + + +/* + * Denorms usually have an exponent biased by 1 so that they flow + * smoothly into the smallest normal value with an exponent of + * 1. However, m68k 80-bit long doubles includes exponent of zero for + * normal values, so denorms use the same value, eliminating the + * bias. That is set in s_fmal.c. + */ + +#ifndef FLOAT_DENORM_BIAS +#define FLOAT_DENORM_BIAS 1 +#endif + +/* + * A struct dd represents a floating-point number with twice the precision + * of a FLOAT_T. We maintain the invariant that "hi" stores the high-order + * bits of the result. + */ +struct dd { + FLOAT_T hi; + FLOAT_T lo; +}; + +/* + * Compute a+b exactly, returning the exact result in a struct dd. We assume + * that both a and b are finite, but make no assumptions about their relative + * magnitudes. + */ +static inline struct dd +dd_add(FLOAT_T a, FLOAT_T b) +{ + struct dd ret; + FLOAT_T s; + + ret.hi = a + b; + s = ret.hi - a; + ret.lo = (a - (ret.hi - s)) + (b - s); + return (ret); +} + +/* + * Compute a+b, with a small tweak: The least significant bit of the + * result is adjusted into a sticky bit summarizing all the bits that + * were lost to rounding. This adjustment negates the effects of double + * rounding when the result is added to another number with a higher + * exponent. For an explanation of round and sticky bits, see any reference + * on FPU design, e.g., + * + * J. Coonen. An Implementation Guide to a Proposed Standard for + * Floating-Point Arithmetic. Computer, vol. 13, no. 1, Jan 1980. + */ +static inline FLOAT_T +add_adjusted(FLOAT_T a, FLOAT_T b) +{ + struct dd sum; + + sum = dd_add(a, b); + if (sum.lo != 0) { + if (!odd_mant(sum.hi)) + sum.hi = NEXTAFTER(sum.hi, (FLOAT_T)INFINITY * sum.lo); + } + return (sum.hi); +} + +/* + * Compute ldexp(a+b, scale) with a single rounding error. It is assumed + * that the result will be subnormal, and care is taken to ensure that + * double rounding does not occur. + */ +static inline FLOAT_T +add_and_denormalize(FLOAT_T a, FLOAT_T b, int scale) +{ + struct dd sum; + int bits_lost; + + sum = dd_add(a, b); + + /* + * If we are losing at least two bits of accuracy to denormalization, + * then the first lost bit becomes a round bit, and we adjust the + * lowest bit of sum.hi to make it a sticky bit summarizing all the + * bits in sum.lo. With the sticky bit adjusted, the hardware will + * break any ties in the correct direction. + * + * If we are losing only one bit to denormalization, however, we must + * break the ties manually. + */ + if (sum.lo != 0) { + bits_lost = -EXPONENT(sum.hi) - scale + FLOAT_DENORM_BIAS; + if ((bits_lost != 1) ^ (int)odd_mant(sum.hi)) + sum.hi = NEXTAFTER(sum.hi, (FLOAT_T)INFINITY * sum.lo); + } + return (LDEXP(sum.hi, scale)); +} + +/* + * Compute a*b exactly, returning the exact result in a struct dd. We assume + * that both a and b are normalized, so no underflow or overflow will occur. + * The current rounding mode must be round-to-nearest. + */ +static inline struct dd +dd_mul(FLOAT_T a, FLOAT_T b) +{ + static const FLOAT_T split = SPLIT; + struct dd ret; + FLOAT_T ha, hb, la, lb, p, q; + + p = a * split; + ha = a - p; + ha += p; + la = a - ha; + + p = b * split; + hb = b - p; + hb += p; + lb = b - hb; + + p = ha * hb; + q = ha * lb + la * hb; + + ret.hi = p + q; + ret.lo = p - ret.hi + q + la * lb; + return (ret); +} + +#ifdef __MATH_ERRNO +static FLOAT_T +_scalbn_no_errno(FLOAT_T x, int n) +{ + int save_errno = errno; + x = SCALBN(x, n); + errno = save_errno; + return x; +} +#else +#define _scalbn_no_errno(a,b) SCALBN(a,b) +#endif + +#ifdef __clang__ +#pragma STDC FP_CONTRACT OFF +#endif + +#if defined(FE_UPWARD) || defined(FE_DOWNWARD) || defined(FE_TOWARDZERO) +#define HAS_ROUNDING +#endif + +/* + * Fused multiply-add: Compute x * y + z with a single rounding error. + * + * We use scaling to avoid overflow/underflow, along with the + * canonical precision-doubling technique adapted from: + * + * Dekker, T. A Floating-Point Technique for Extending the + * Available Precision. Numer. Math. 18, 224-242 (1971). + */ +FLOAT_T +FMA(FLOAT_T x, FLOAT_T y, FLOAT_T z) +{ + FLOAT_T xs, ys, zs, adj; + struct dd xy, r; + int ex, ey, ez; + int spread; + + /* + * Handle special cases. The order of operations and the particular + * return values here are crucial in handling special cases involving + * infinities, NaNs, overflows, and signed zeroes correctly. + */ + if (!isfinite(z) && isfinite(x) && isfinite(y)) + return z + z; + if (!isfinite(x) || !isfinite(y) || !isfinite(z)) + return (x * y + z); + if (x == (FLOAT_T) 0.0 || y == (FLOAT_T) 0.0) + return (x * y + z); + if (z == (FLOAT_T) 0.0) + return (x * y); + + xs = FREXP(x, &ex); + ys = FREXP(y, &ey); + zs = FREXP(z, &ez); +#ifdef HAS_ROUNDING + int oround = fegetround(); +#endif + spread = ex + ey - ez; + + /* + * If x * y and z are many orders of magnitude apart, the scaling + * will overflow, so we handle these cases specially. Rounding + * modes other than FE_TONEAREST are painful. + */ + if (spread < -FLOAT_MANT_DIG) { +#ifdef FE_INEXACT + feraiseexcept(FE_INEXACT); +#endif +#ifdef FE_UNDERFLOW + if (!isnormal(z)) + feraiseexcept(FE_UNDERFLOW); +#endif +#ifdef HAS_ROUNDING + switch (oround) { + default: + break; +#ifdef FE_TOWARDZERO + case FE_TOWARDZERO: + if ((x > (FLOAT_T) 0.0) ^ (y < (FLOAT_T) 0.0) ^ (z < (FLOAT_T) 0.0)) + break; + else + return (NEXTAFTER(z, 0)); +#endif +#ifdef FE_DOWNWARD + case FE_DOWNWARD: + if ((x > (FLOAT_T) 0.0) ^ (y < (FLOAT_T) 0.0)) + break; + else + return (NEXTAFTER(z, -(FLOAT_T)INFINITY)); +#endif +#ifdef FE_UPWARD + case FE_UPWARD: + if ((x > (FLOAT_T) 0.0) ^ (y < (FLOAT_T) 0.0)) + return (NEXTAFTER(z, (FLOAT_T)INFINITY)); + break; +#endif + } +#endif + return (z); + } + if (spread <= FLOAT_MANT_DIG * 2) + zs = _scalbn_no_errno(zs, -spread); + else + zs = COPYSIGN(FLOAT_MIN, zs); + +#ifdef HAS_ROUNDING + fesetround(FE_TONEAREST); +#endif + + /* + * Basic approach for round-to-nearest: + * + * (xy.hi, xy.lo) = x * y (exact) + * (r.hi, r.lo) = xy.hi + z (exact) + * adj = xy.lo + r.lo (inexact; low bit is sticky) + * result = r.hi + adj (correctly rounded) + */ + xy = dd_mul(xs, ys); + r = dd_add(xy.hi, zs); + + spread = ex + ey; + + if (r.hi == (FLOAT_T) 0.0) { + /* + * When the addends cancel to 0, ensure that the result has + * the correct sign. + */ +#ifdef HAS_ROUNDING + fesetround(oround); +#endif + volatile FLOAT_T vzs = zs; /* XXX gcc CSE bug workaround */ + return (xy.hi + vzs + _scalbn_no_errno(xy.lo, spread)); + } + +#ifdef HAS_ROUNDING + if (oround != FE_TONEAREST) { + /* + * There is no need to worry about double rounding in directed + * rounding modes. + */ + fesetround(oround); + adj = r.lo + xy.lo; + return (_scalbn_no_errno(r.hi + adj, spread)); + } +#endif + + adj = add_adjusted(r.lo, xy.lo); + if (spread + ILOGB(r.hi) > -(FLOAT_MAX_EXP - FLOAT_DENORM_BIAS)) + return (_scalbn_no_errno(r.hi + adj, spread)); + else + return (add_and_denormalize(r.hi, adj, spread)); +} diff --git a/src/audio/ffmpeg_dec/libm/common/frexpl.c b/src/audio/ffmpeg_dec/libm/common/frexpl.c new file mode 100644 index 000000000000..31fbb214ca7d --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/frexpl.c @@ -0,0 +1,177 @@ +/* +(C) Copyright IBM Corp. 2009 + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +* Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +* Neither the name of IBM nor the names of its contributors may be +used to endorse or promote products derived from this software without +specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +*/ + +#include "math_config.h" + +#if defined(_NEED_FLOAT_HUGE) && !defined(__HAVE_LONG_DOUBLE_MATH) +# if (LDBL_MANT_DIG == 53) /* 64-bit long double */ +static const long double scale = 0x1p54; + +union ldbl { + long double x; + struct { +# ifdef __IEEE_LITTLE_ENDIAN /* for Intel CPU */ + __uint32_t fracl; + __uint32_t frach:20; + __uint32_t exp:11; + __uint32_t sign:1; +# endif +# ifdef __IEEE_BIG_ENDIAN + __uint32_t sign:1; + __uint32_t exp:11; + __uint32_t frach:20; +# ifndef ___IEEE_BYTES_LITTLE_ENDIAN +# else /* ARMEL without __VFP_FP__ */ + __uint32_t frach:20; + __uint32_t exp:11; + __uint32_t sign:1; +# endif + __uint32_t fracl; +# endif + } u32; +}; +# elif (LDBL_MANT_DIG == 64) /* 80-bit long double */ +static const double scale = 0x1p65; + +union ldbl { + long double x; + struct { +# ifdef __IEEE_LITTLE_ENDIAN /* for Intel CPU */ + __uint32_t fracl; + __uint32_t frach; + __uint32_t exp:15; + __uint32_t sign:1; + __uint32_t pad:16; +# endif +# ifdef __IEEE_BIG_ENDIAN +# ifndef ___IEEE_BYTES_LITTLE_ENDIAN /* for m86k */ + __uint32_t sign:1; + __uint32_t exp:15; + __uint32_t pad:16; +# else /* ARM FPA10 math copprocessor */ + __uint32_t exp:15; + __uint32_t pad:16; + __uint32_t sign:1; +# endif + __uint32_t frach; + __uint32_t fracl; +# endif + } u32; +}; +# elif (LDBL_MANT_DIG == 106) /* 128-bit double double */ +static const long double scale = 0x1p107l; + +#define EXP_EXTRA_BIAS 100 + +union ldbl { + long double x; + struct { +# ifdef __IEEE_LITTLE_ENDIAN + __uint32_t frachm; + __uint32_t frach:20; + __uint32_t exp:11; + __uint32_t sign:1; + __uint32_t fracl; + __uint32_t fraclm:20; + __uint32_t exp_extra:11; + __uint32_t sign_extra:1; +# endif +# ifdef __IEEE_BIG_ENDIAN + __uint32_t sign:1; + __uint32_t exp:11; + __uint32_t frach:20; + __uint32_t frachm; + __uint32_t sign_extra:1; + __uint32_t exp_extra:11; + __uint32_t fraclm:20; + __uint32_t fracl; +# endif + } u32; +}; +# elif (LDBL_MANT_DIG == 113) /* 128-bit long double */ +static const long double scale = 0x1p114l; + +union ldbl { + long double x; + struct { +# ifdef __IEEE_LITTLE_ENDIAN + __uint32_t fracl; + __uint32_t fraclm; + __uint32_t frachm; + __uint32_t frach:16; + __uint32_t exp:15; + __uint32_t sign:1; +# endif +# ifdef __IEEE_BIG_ENDIAN +# ifndef ___IEEE_BYTES_LITTLE_ENDIAN + __uint32_t sign:1; + __uint32_t exp:15; + __uint32_t frach:16; +# else /* ARMEL without __VFP_FP__ */ + __uint32_t frach:16; + __uint32_t exp:15; + __uint32_t sign:1; +# endif + __uint32_t frachm; + __uint32_t fraclm; + __uint32_t fracl; +# endif + } u32; +}; +# else +# error Unsupported long double format. +# endif + +static const int scale_exp = LDBL_MANT_DIG + 1; + +long double +frexpl (long double x, int *eptr) +{ + union ldbl u; + u.x = x; + int e = u.u32.exp; + *eptr = 0; + if (e == (LDBL_MAX_EXP*2 - 1) || x == 0) + return x; /* inf,nan,0 */ + if (e == 0) /* subnormal */ + { + u.x *= (long double) scale; + e = u.u32.exp; + *eptr -= scale_exp; + } + *eptr += e - (LDBL_MAX_EXP - 2); + u.u32.exp = LDBL_MAX_EXP - 2; /* -1 */ +#ifdef EXP_EXTRA_BIAS + if (u.u32.exp_extra != 0) + u.u32.exp_extra = u.u32.exp - EXP_EXTRA_BIAS; +#endif + return u.x; +} +#endif /* _NEED_FLOAT_HUGE */ diff --git a/src/audio/ffmpeg_dec/libm/common/local.h b/src/audio/ffmpeg_dec/libm/common/local.h new file mode 100644 index 000000000000..8461d02152cf --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/local.h @@ -0,0 +1 @@ +/* placeholder for future usage. */ diff --git a/src/audio/ffmpeg_dec/libm/common/log.c b/src/audio/ffmpeg_dec/libm/common/log.c new file mode 100644 index 000000000000..e539773d372a --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/log.c @@ -0,0 +1,184 @@ +/* Double-precision log(x) function. + Copyright (c) 2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "fdlibm.h" +#if !__OBSOLETE_MATH_DOUBLE + +#include +#include +#include "math_config.h" + +#define T __log_data.tab +#define T2 __log_data.tab2 +#define B __log_data.poly1 +#define A __log_data.poly +#define Ln2hi __log_data.ln2hi +#define Ln2lo __log_data.ln2lo +#define N (1 << LOG_TABLE_BITS) +#define OFF 0x3fe6000000000000 + +/* Top 16 bits of a double. */ +static inline uint32_t +top16 (double x) +{ + return asuint64 (x) >> 48; +} + +double +log (double x) +{ + /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ + double_t w, z, r, r2, r3, y, invc, logc, kd, hi, lo; + uint64_t ix, iz, tmp; + uint32_t top; + int k, i; + + ix = asuint64 (x); + top = top16 (x); + +#if LOG_POLY1_ORDER == 10 || LOG_POLY1_ORDER == 11 +# define LO asuint64 (1.0 - 0x1p-5) +# define HI asuint64 (1.0 + 0x1.1p-5) +#elif LOG_POLY1_ORDER == 12 +# define LO asuint64 (1.0 - 0x1p-4) +# define HI asuint64 (1.0 + 0x1.09p-4) +#endif + if (unlikely (ix - LO < HI - LO)) + { + /* Handle close to 1.0 inputs separately. */ + /* Fix sign of zero with downward rounding when x==1. */ +#if WANT_ROUNDING + if (unlikely (ix == asuint64 (1.0))) + return 0; +#endif + r = x - 1.0; + r2 = r * r; + r3 = r * r2; +#if LOG_POLY1_ORDER == 10 + /* Worst-case error is around 0.516 ULP. */ + y = r3 * (B[1] + r * B[2] + r2 * B[3] + + r3 * (B[4] + r * B[5] + r2 * B[6] + r3 * (B[7] + r * B[8]))); + w = B[0] * r2; /* B[0] == -0.5. */ + hi = r + w; + y += r - hi + w; + y += hi; +#elif LOG_POLY1_ORDER == 11 + /* Worst-case error is around 0.516 ULP. */ + y = r3 * (B[1] + r * B[2] + + r2 * (B[3] + r * B[4] + r2 * B[5] + + r3 * (B[6] + r * B[7] + r2 * B[8] + r3 * B[9]))); + w = B[0] * r2; /* B[0] == -0.5. */ + hi = r + w; + y += r - hi + w; + y += hi; +#elif LOG_POLY1_ORDER == 12 + y = r3 * (B[1] + r * B[2] + r2 * B[3] + + r3 * (B[4] + r * B[5] + r2 * B[6] + + r3 * (B[7] + r * B[8] + r2 * B[9] + r3 * B[10]))); +# if N <= 64 + /* Worst-case error is around 0.532 ULP. */ + w = B[0] * r2; /* B[0] == -0.5. */ + hi = r + w; + y += r - hi + w; + y += hi; +# else + /* Worst-case error is around 0.507 ULP. */ + w = r * 0x1p27; + double_t rhi = r + w - w; + double_t rlo = r - rhi; + w = rhi * rhi * B[0]; /* B[0] == -0.5. */ + hi = r + w; + lo = r - hi + w; + lo += B[0] * rlo * (rhi + r); + y += lo; + y += hi; +# endif +#endif + return y; + } + if (unlikely (top - 0x0010 >= 0x7ff0 - 0x0010)) + { + /* x < 0x1p-1022 or inf or nan. */ + if (ix * 2 == 0) + return __math_divzero (1); + if (ix == asuint64 ((double) INFINITY)) /* log(inf) == inf. */ + return x; + if ((top & 0x8000) || (top & 0x7ff0) == 0x7ff0) + return __math_invalid (x); + /* x is subnormal, normalize it. */ + ix = asuint64 (x * 0x1p52); + ix -= 52ULL << 52; + } + + /* x = 2^k z; where z is in range [OFF,2*OFF) and exact. + The range is split into N subintervals. + The ith subinterval contains z and c is near its center. */ + tmp = ix - OFF; + i = (tmp >> (52 - LOG_TABLE_BITS)) % N; + k = (int64_t) tmp >> 52; /* arithmetic shift */ + iz = ix - (tmp & 0xfffULL << 52); + invc = T[i].invc; + logc = T[i].logc; + z = asfloat64 (iz); + + /* log(x) = log1p(z/c-1) + log(c) + k*Ln2. */ + /* r ~= z/c - 1, |r| < 1/(2*N). */ +#if __HAVE_FAST_FMA + /* rounding error: 0x1p-55/N. */ + r = fma (z, invc, -1.0); +#else + /* rounding error: 0x1p-55/N + 0x1p-66. */ + r = (z - T2[i].chi - T2[i].clo) * invc; +#endif + kd = (double_t) k; + + /* hi + lo = r + log(c) + k*Ln2. */ + w = kd * Ln2hi + logc; + hi = w + r; + lo = w - hi + r + kd * Ln2lo; + + /* log(x) = lo + (log1p(r) - r) + hi. */ + r2 = r * r; /* rounding error: 0x1p-54/N^2. */ + /* Worst case error if |y| > 0x1p-5: + 0.5 + 4.13/N + abs-poly-error*2^57 ULP (+ 0.002 ULP without fma) + Worst case error if |y| > 0x1p-4: + 0.5 + 2.06/N + abs-poly-error*2^56 ULP (+ 0.001 ULP without fma). */ +#if LOG_POLY_ORDER == 6 + y = lo + r2 * A[0] + r * r2 * (A[1] + r * A[2] + r2 * (A[3] + r * A[4])) + hi; +#elif LOG_POLY_ORDER == 7 + y = lo + + r2 * (A[0] + r * A[1] + r2 * (A[2] + r * A[3]) + + r2 * r2 * (A[4] + r * A[5])) + + hi; +#endif + return y; +} + +_MATH_ALIAS_d_d(log) + +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/log2.c b/src/audio/ffmpeg_dec/libm/common/log2.c new file mode 100644 index 000000000000..e05967c94d8b --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/log2.c @@ -0,0 +1,163 @@ +/* Double-precision log2(x) function. + Copyright (c) 2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "fdlibm.h" +#if !__OBSOLETE_MATH_DOUBLE + +#include +#include +#include "math_config.h" + +#define T __log2_data.tab +#define T2 __log2_data.tab2 +#define B __log2_data.poly1 +#define A __log2_data.poly +#define InvLn2hi __log2_data.invln2hi +#define InvLn2lo __log2_data.invln2lo +#define N (1 << LOG2_TABLE_BITS) +#define OFF 0x3fe6000000000000 + +/* Top 16 bits of a double. */ +static inline uint32_t +top16 (double x) +{ + return asuint64 (x) >> 48; +} + +double +(log2) (double x) +{ + /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ + double_t z, r, r2, r4, y, invc, logc, kd, hi, lo, t1, t2, t3, p; + uint64_t ix, iz, tmp; + uint32_t top; + int k, i; + + ix = asuint64 (x); + top = top16 (x); + +#if LOG2_POLY1_ORDER == 11 +# define LO asuint64 (1.0 - 0x1.5b51p-5) +# define HI asuint64 (1.0 + 0x1.6ab2p-5) +#endif + if (unlikely (ix - LO < HI - LO)) + { + /* Handle close to 1.0 inputs separately. */ + /* Fix sign of zero with downward rounding when x==1. */ +#if WANT_ROUNDING + if (unlikely (ix == asuint64 (1.0))) + return 0; +#endif + r = x - 1.0; +#if __HAVE_FAST_FMA + hi = r * InvLn2hi; + lo = r * InvLn2lo + fma (r, InvLn2hi, -hi); +#else + double_t rhi, rlo; + rhi = asfloat64 (asuint64 (r) & -1ULL << 32); + rlo = r - rhi; + hi = rhi * InvLn2hi; + lo = rlo * InvLn2hi + r * InvLn2lo; +#endif + r2 = r * r; /* rounding error: 0x1p-62. */ + r4 = r2 * r2; +#if LOG2_POLY1_ORDER == 11 + /* Worst-case error is less than 0.54 ULP (0.55 ULP without fma). */ + p = r2 * (B[0] + r * B[1]); + y = hi + p; + lo += hi - y + p; + lo += r4 * (B[2] + r * B[3] + r2 * (B[4] + r * B[5]) + + r4 * (B[6] + r * B[7] + r2 * (B[8] + r * B[9]))); + y += lo; +#endif + return y; + } + if (unlikely (top - 0x0010 >= 0x7ff0 - 0x0010)) + { + /* x < 0x1p-1022 or inf or nan. */ + if (ix * 2 == 0) + return __math_divzero (1); + if (ix == asuint64 ((double) INFINITY)) /* log(inf) == inf. */ + return x; + if ((top & 0x8000) || (top & 0x7ff0) == 0x7ff0) + return __math_invalid (x); + /* x is subnormal, normalize it. */ + ix = asuint64 (x * 0x1p52); + ix -= 52ULL << 52; + } + + /* x = 2^k z; where z is in range [OFF,2*OFF) and exact. + The range is split into N subintervals. + The ith subinterval contains z and c is near its center. */ + tmp = ix - OFF; + i = (tmp >> (52 - LOG2_TABLE_BITS)) % N; + k = (int64_t) tmp >> 52; /* arithmetic shift */ + iz = ix - (tmp & 0xfffULL << 52); + invc = T[i].invc; + logc = T[i].logc; + z = asfloat64 (iz); + kd = (double_t) k; + + /* log2(x) = log2(z/c) + log2(c) + k. */ + /* r ~= z/c - 1, |r| < 1/(2*N). */ +#if __HAVE_FAST_FMA + /* rounding error: 0x1p-55/N. */ + r = fma (z, invc, -1.0); + t1 = r * InvLn2hi; + t2 = r * InvLn2lo + fma (r, InvLn2hi, -t1); +#else + double_t rhi, rlo; + /* rounding error: 0x1p-55/N + 0x1p-65. */ + r = (z - T2[i].chi - T2[i].clo) * invc; + rhi = asfloat64 (asuint64 (r) & -1ULL << 32); + rlo = r - rhi; + t1 = rhi * InvLn2hi; + t2 = rlo * InvLn2hi + r * InvLn2lo; +#endif + + /* hi + lo = r/ln2 + log2(c) + k. */ + t3 = kd + logc; + hi = t3 + t1; + lo = t3 - hi + t1 + t2; + + /* log2(r+1) = r/ln2 + r^2*poly(r). */ + /* Evaluation is optimized assuming superscalar pipelined execution. */ + r2 = r * r; /* rounding error: 0x1p-54/N^2. */ + r4 = r2 * r2; +#if LOG2_POLY_ORDER == 7 + /* Worst-case error if |y| > 0x1p-4: 0.547 ULP (0.550 ULP without fma). + ~ 0.5 + 2/N/ln2 + abs-poly-error*0x1p56 ULP (+ 0.003 ULP without fma). */ + p = A[0] + r * A[1] + r2 * (A[2] + r * A[3]) + r4 * (A[4] + r * A[5]); + y = lo + r2 * p + hi; +#endif + return y; +} + +_MATH_ALIAS_d_d(log2) + +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/log2_data.c b/src/audio/ffmpeg_dec/libm/common/log2_data.c new file mode 100644 index 000000000000..40314371e905 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/log2_data.c @@ -0,0 +1,234 @@ +/* Data for log2. + Copyright (c) 2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "fdlibm.h" +#if !__OBSOLETE_MATH_DOUBLE + +#include "math_config.h" + +#define N (1 << LOG2_TABLE_BITS) + +const struct log2_data __log2_data = { +// First coefficient: 0x1.71547652b82fe1777d0ffda0d24p0 +.invln2hi = 0x1.7154765200000p+0, +.invln2lo = 0x1.705fc2eefa200p-33, +.poly1 = { +#if LOG2_POLY1_ORDER == 11 +// relative error: 0x1.2fad8188p-63 +// in -0x1.5b51p-5 0x1.6ab2p-5 +-0x1.71547652b82fep-1, +0x1.ec709dc3a03f7p-2, +-0x1.71547652b7c3fp-2, +0x1.2776c50f05be4p-2, +-0x1.ec709dd768fe5p-3, +0x1.a61761ec4e736p-3, +-0x1.7153fbc64a79bp-3, +0x1.484d154f01b4ap-3, +-0x1.289e4a72c383cp-3, +0x1.0b32f285aee66p-3, +#endif +}, +.poly = { +#if N == 64 && LOG2_POLY_ORDER == 7 +// relative error: 0x1.a72c2bf8p-58 +// abs error: 0x1.67a552c8p-66 +// in -0x1.f45p-8 0x1.f45p-8 +-0x1.71547652b8339p-1, +0x1.ec709dc3a04bep-2, +-0x1.7154764702ffbp-2, +0x1.2776c50034c48p-2, +-0x1.ec7b328ea92bcp-3, +0x1.a6225e117f92ep-3, +#endif +}, +/* Algorithm: + + x = 2^k z + log2(x) = k + log2(c) + log2(z/c) + log2(z/c) = poly(z/c - 1) + +where z is in [1.6p-1; 1.6p0] which is split into N subintervals and z falls +into the ith one, then table entries are computed as + + tab[i].invc = 1/c + tab[i].logc = (double)log2(c) + tab2[i].chi = (double)c + tab2[i].clo = (double)(c - (double)c) + +where c is near the center of the subinterval and is chosen by trying +-2^29 +floating point invc candidates around 1/center and selecting one for which + + 1) the rounding error in 0x1.8p10 + logc is 0, + 2) the rounding error in z - chi - clo is < 0x1p-64 and + 3) the rounding error in (double)log2(c) is minimized (< 0x1p-68). + +Note: 1) ensures that k + logc can be computed without rounding error, 2) +ensures that z/c - 1 can be computed as (z - chi - clo)*invc with close to a +single rounding error when there is no fast fma for z*invc - 1, 3) ensures +that logc + poly(z/c - 1) has small error, however near x == 1 when +|log2(x)| < 0x1p-4, this is not enough so that is special cased. */ +.tab = { +#if N == 64 +{0x1.724286bb1acf8p+0, -0x1.1095feecdb000p-1}, +{0x1.6e1f766d2cca1p+0, -0x1.08494bd76d000p-1}, +{0x1.6a13d0e30d48ap+0, -0x1.00143aee8f800p-1}, +{0x1.661ec32d06c85p+0, -0x1.efec5360b4000p-2}, +{0x1.623fa951198f8p+0, -0x1.dfdd91ab7e000p-2}, +{0x1.5e75ba4cf026cp+0, -0x1.cffae0cc79000p-2}, +{0x1.5ac055a214fb8p+0, -0x1.c043811fda000p-2}, +{0x1.571ed0f166e1ep+0, -0x1.b0b67323ae000p-2}, +{0x1.53909590bf835p+0, -0x1.a152f5a2db000p-2}, +{0x1.5014fed61adddp+0, -0x1.9217f5af86000p-2}, +{0x1.4cab88e487bd0p+0, -0x1.8304db0719000p-2}, +{0x1.49539b4334feep+0, -0x1.74189f9a9e000p-2}, +{0x1.460cbdfafd569p+0, -0x1.6552bb5199000p-2}, +{0x1.42d664ee4b953p+0, -0x1.56b23a29b1000p-2}, +{0x1.3fb01111dd8a6p+0, -0x1.483650f5fa000p-2}, +{0x1.3c995b70c5836p+0, -0x1.39de937f6a000p-2}, +{0x1.3991c4ab6fd4ap+0, -0x1.2baa1538d6000p-2}, +{0x1.3698e0ce099b5p+0, -0x1.1d98340ca4000p-2}, +{0x1.33ae48213e7b2p+0, -0x1.0fa853a40e000p-2}, +{0x1.30d191985bdb1p+0, -0x1.01d9c32e73000p-2}, +{0x1.2e025cab271d7p+0, -0x1.e857da2fa6000p-3}, +{0x1.2b404cf13cd82p+0, -0x1.cd3c8633d8000p-3}, +{0x1.288b02c7ccb50p+0, -0x1.b26034c14a000p-3}, +{0x1.25e2263944de5p+0, -0x1.97c1c2f4fe000p-3}, +{0x1.234563d8615b1p+0, -0x1.7d6023f800000p-3}, +{0x1.20b46e33eaf38p+0, -0x1.633a71a05e000p-3}, +{0x1.1e2eefdcda3ddp+0, -0x1.494f5e9570000p-3}, +{0x1.1bb4a580b3930p+0, -0x1.2f9e424e0a000p-3}, +{0x1.19453847f2200p+0, -0x1.162595afdc000p-3}, +{0x1.16e06c0d5d73cp+0, -0x1.f9c9a75bd8000p-4}, +{0x1.1485f47b7e4c2p+0, -0x1.c7b575bf9c000p-4}, +{0x1.12358ad0085d1p+0, -0x1.960c60ff48000p-4}, +{0x1.0fef00f532227p+0, -0x1.64ce247b60000p-4}, +{0x1.0db2077d03a8fp+0, -0x1.33f78b2014000p-4}, +{0x1.0b7e6d65980d9p+0, -0x1.0387d1a42c000p-4}, +{0x1.0953efe7b408dp+0, -0x1.a6f9208b50000p-5}, +{0x1.07325cac53b83p+0, -0x1.47a954f770000p-5}, +{0x1.05197e40d1b5cp+0, -0x1.d23a8c50c0000p-6}, +{0x1.03091c1208ea2p+0, -0x1.16a2629780000p-6}, +{0x1.0101025b37e21p+0, -0x1.720f8d8e80000p-8}, +{0x1.fc07ef9caa76bp-1, 0x1.6fe53b1500000p-7}, +{0x1.f4465d3f6f184p-1, 0x1.11ccce10f8000p-5}, +{0x1.ecc079f84107fp-1, 0x1.c4dfc8c8b8000p-5}, +{0x1.e573a99975ae8p-1, 0x1.3aa321e574000p-4}, +{0x1.de5d6f0bd3de6p-1, 0x1.918a0d08b8000p-4}, +{0x1.d77b681ff38b3p-1, 0x1.e72e9da044000p-4}, +{0x1.d0cb5724de943p-1, 0x1.1dcd2507f6000p-3}, +{0x1.ca4b2dc0e7563p-1, 0x1.476ab03dea000p-3}, +{0x1.c3f8ee8d6cb51p-1, 0x1.7074377e22000p-3}, +{0x1.bdd2b4f020c4cp-1, 0x1.98ede8ba94000p-3}, +{0x1.b7d6c006015cap-1, 0x1.c0db86ad2e000p-3}, +{0x1.b20366e2e338fp-1, 0x1.e840aafcee000p-3}, +{0x1.ac57026295039p-1, 0x1.0790ab4678000p-2}, +{0x1.a6d01bc2731ddp-1, 0x1.1ac056801c000p-2}, +{0x1.a16d3bc3ff18bp-1, 0x1.2db11d4fee000p-2}, +{0x1.9c2d14967feadp-1, 0x1.406464ec58000p-2}, +{0x1.970e4f47c9902p-1, 0x1.52dbe093af000p-2}, +{0x1.920fb3982bcf2p-1, 0x1.651902050d000p-2}, +{0x1.8d30187f759f1p-1, 0x1.771d2cdeaf000p-2}, +{0x1.886e5ebb9f66dp-1, 0x1.88e9c857d9000p-2}, +{0x1.83c97b658b994p-1, 0x1.9a80155e16000p-2}, +{0x1.7f405ffc61022p-1, 0x1.abe186ed3d000p-2}, +{0x1.7ad22181415cap-1, 0x1.bd0f2aea0e000p-2}, +{0x1.767dcf99eff8cp-1, 0x1.ce0a43dbf4000p-2}, +#endif +}, +#if !__HAVE_FAST_FMA +.tab2 = { +# if N == 64 +{0x1.6200012b90a8ep-1, 0x1.904ab0644b605p-55}, +{0x1.66000045734a6p-1, 0x1.1ff9bea62f7a9p-57}, +{0x1.69fffc325f2c5p-1, 0x1.27ecfcb3c90bap-55}, +{0x1.6e00038b95a04p-1, 0x1.8ff8856739326p-55}, +{0x1.71fffe09994e3p-1, 0x1.afd40275f82b1p-55}, +{0x1.7600015590e1p-1, -0x1.2fd75b4238341p-56}, +{0x1.7a00012655bd5p-1, 0x1.808e67c242b76p-56}, +{0x1.7e0003259e9a6p-1, -0x1.208e426f622b7p-57}, +{0x1.81fffedb4b2d2p-1, -0x1.402461ea5c92fp-55}, +{0x1.860002dfafcc3p-1, 0x1.df7f4a2f29a1fp-57}, +{0x1.89ffff78c6b5p-1, -0x1.e0453094995fdp-55}, +{0x1.8e00039671566p-1, -0x1.a04f3bec77b45p-55}, +{0x1.91fffe2bf1745p-1, -0x1.7fa34400e203cp-56}, +{0x1.95fffcc5c9fd1p-1, -0x1.6ff8005a0695dp-56}, +{0x1.9a0003bba4767p-1, 0x1.0f8c4c4ec7e03p-56}, +{0x1.9dfffe7b92da5p-1, 0x1.e7fd9478c4602p-55}, +{0x1.a1fffd72efdafp-1, -0x1.a0c554dcdae7ep-57}, +{0x1.a5fffde04ff95p-1, 0x1.67da98ce9b26bp-55}, +{0x1.a9fffca5e8d2bp-1, -0x1.284c9b54c13dep-55}, +{0x1.adfffddad03eap-1, 0x1.812c8ea602e3cp-58}, +{0x1.b1ffff10d3d4dp-1, -0x1.efaddad27789cp-55}, +{0x1.b5fffce21165ap-1, 0x1.3cb1719c61237p-58}, +{0x1.b9fffd950e674p-1, 0x1.3f7d94194cep-56}, +{0x1.be000139ca8afp-1, 0x1.50ac4215d9bcp-56}, +{0x1.c20005b46df99p-1, 0x1.beea653e9c1c9p-57}, +{0x1.c600040b9f7aep-1, -0x1.c079f274a70d6p-56}, +{0x1.ca0006255fd8ap-1, -0x1.a0b4076e84c1fp-56}, +{0x1.cdfffd94c095dp-1, 0x1.8f933f99ab5d7p-55}, +{0x1.d1ffff975d6cfp-1, -0x1.82c08665fe1bep-58}, +{0x1.d5fffa2561c93p-1, -0x1.b04289bd295f3p-56}, +{0x1.d9fff9d228b0cp-1, 0x1.70251340fa236p-55}, +{0x1.de00065bc7e16p-1, -0x1.5011e16a4d80cp-56}, +{0x1.e200002f64791p-1, 0x1.9802f09ef62ep-55}, +{0x1.e600057d7a6d8p-1, -0x1.e0b75580cf7fap-56}, +{0x1.ea00027edc00cp-1, -0x1.c848309459811p-55}, +{0x1.ee0006cf5cb7cp-1, -0x1.f8027951576f4p-55}, +{0x1.f2000782b7dccp-1, -0x1.f81d97274538fp-55}, +{0x1.f6000260c450ap-1, -0x1.071002727ffdcp-59}, +{0x1.f9fffe88cd533p-1, -0x1.81bdce1fda8bp-58}, +{0x1.fdfffd50f8689p-1, 0x1.7f91acb918e6ep-55}, +{0x1.0200004292367p+0, 0x1.b7ff365324681p-54}, +{0x1.05fffe3e3d668p+0, 0x1.6fa08ddae957bp-55}, +{0x1.0a0000a85a757p+0, -0x1.7e2de80d3fb91p-58}, +{0x1.0e0001a5f3fccp+0, -0x1.1823305c5f014p-54}, +{0x1.11ffff8afbaf5p+0, -0x1.bfabb6680bac2p-55}, +{0x1.15fffe54d91adp+0, -0x1.d7f121737e7efp-54}, +{0x1.1a00011ac36e1p+0, 0x1.c000a0516f5ffp-54}, +{0x1.1e00019c84248p+0, -0x1.082fbe4da5dap-54}, +{0x1.220000ffe5e6ep+0, -0x1.8fdd04c9cfb43p-55}, +{0x1.26000269fd891p+0, 0x1.cfe2a7994d182p-55}, +{0x1.2a00029a6e6dap+0, -0x1.00273715e8bc5p-56}, +{0x1.2dfffe0293e39p+0, 0x1.b7c39dab2a6f9p-54}, +{0x1.31ffff7dcf082p+0, 0x1.df1336edc5254p-56}, +{0x1.35ffff05a8b6p+0, -0x1.e03564ccd31ebp-54}, +{0x1.3a0002e0eaeccp+0, 0x1.5f0e74bd3a477p-56}, +{0x1.3e000043bb236p+0, 0x1.c7dcb149d8833p-54}, +{0x1.4200002d187ffp+0, 0x1.e08afcf2d3d28p-56}, +{0x1.460000d387cb1p+0, 0x1.20837856599a6p-55}, +{0x1.4a00004569f89p+0, -0x1.9fa5c904fbcd2p-55}, +{0x1.4e000043543f3p+0, -0x1.81125ed175329p-56}, +{0x1.51fffcc027f0fp+0, 0x1.883d8847754dcp-54}, +{0x1.55ffffd87b36fp+0, -0x1.709e731d02807p-55}, +{0x1.59ffff21df7bap+0, 0x1.7f79f68727b02p-55}, +{0x1.5dfffebfc3481p+0, -0x1.180902e30e93ep-54}, +# endif +}, +#endif /* !__HAVE_FAST_FMA */ +}; +#endif /* __OBSOLETE_MATH_DOUBLE */ diff --git a/src/audio/ffmpeg_dec/libm/common/log_data.c b/src/audio/ffmpeg_dec/libm/common/log_data.c new file mode 100644 index 000000000000..b7a9a8171e38 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/log_data.c @@ -0,0 +1,536 @@ +/* Data for log. + Copyright (c) 2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "fdlibm.h" +#if !__OBSOLETE_MATH_DOUBLE + +#include "math_config.h" + +#define N (1 << LOG_TABLE_BITS) + +const struct log_data __log_data = { +.ln2hi = 0x1.62e42fefa3800p-1, +.ln2lo = 0x1.ef35793c76730p-45, +.poly1 = { +#if LOG_POLY1_ORDER == 10 +// relative error: 0x1.32eccc6p-62 +// in -0x1p-5 0x1.1p-5 (|log(1+x)| > 0x1p-5 outside this interval) +-0x1p-1, +0x1.55555555554e5p-2, +-0x1.0000000000af2p-2, +0x1.9999999bbe436p-3, +-0x1.55555537f9cdep-3, +0x1.24922fc8127cfp-3, +-0x1.0000b7d6bb612p-3, +0x1.c806ee1ddbcafp-4, +-0x1.972335a9c2d6ep-4, +#elif LOG_POLY1_ORDER == 11 +// relative error: 0x1.52c8b708p-68 +// in -0x1p-5 0x1.1p-5 (|log(1+x)| > 0x1p-5 outside this interval) +-0x1p-1, +0x1.5555555555555p-2, +-0x1.ffffffffffea9p-3, +0x1.999999999c4d4p-3, +-0x1.55555557f5541p-3, +0x1.249248fbe33e4p-3, +-0x1.ffffc9a3c825bp-4, +0x1.c71e1f204435dp-4, +-0x1.9a7f26377d06ep-4, +0x1.71c30cf8f7364p-4, +#elif LOG_POLY1_ORDER == 12 +// relative error: 0x1.c04d76cp-63 +// in -0x1p-4 0x1.09p-4 (|log(1+x)| > 0x1p-4 outside the interval) +-0x1p-1, +0x1.5555555555577p-2, +-0x1.ffffffffffdcbp-3, +0x1.999999995dd0cp-3, +-0x1.55555556745a7p-3, +0x1.24924a344de3p-3, +-0x1.fffffa4423d65p-4, +0x1.c7184282ad6cap-4, +-0x1.999eb43b068ffp-4, +0x1.78182f7afd085p-4, +-0x1.5521375d145cdp-4, +#endif +}, +.poly = { +#if N == 64 && LOG_POLY_ORDER == 7 +// relative error: 0x1.906eb8ap-58 +// abs error: 0x1.d2cad5a8p-67 +// in -0x1.fp-8 0x1.fp-8 +-0x1.0000000000027p-1, +0x1.555555555556ap-2, +-0x1.fffffff0440bap-3, +0x1.99999991906c3p-3, +-0x1.555c8d7e8201ep-3, +0x1.24978c59151fap-3, +#elif N == 128 && LOG_POLY_ORDER == 6 +// relative error: 0x1.926199e8p-56 +// abs error: 0x1.882ff33p-65 +// in -0x1.fp-9 0x1.fp-9 +-0x1.0000000000001p-1, +0x1.555555551305bp-2, +-0x1.fffffffeb459p-3, +0x1.999b324f10111p-3, +-0x1.55575e506c89fp-3, +#elif N == 128 && LOG_POLY_ORDER == 7 +// relative error: 0x1.649fc4bp-64 +// abs error: 0x1.c3b5769p-74 +// in -0x1.fp-9 0x1.fp-9 +-0x1.0000000000001p-1, +0x1.5555555555556p-2, +-0x1.fffffffea1a8p-3, +0x1.99999998e9139p-3, +-0x1.555776801b968p-3, +0x1.2493c29331a5cp-3, +#endif +}, +/* Algorithm: + + x = 2^k z + log(x) = k ln2 + log(c) + log(z/c) + log(z/c) = poly(z/c - 1) + +where z is in [1.6p-1; 1.6p0] which is split into N subintervals and z falls +into the ith one, then table entries are computed as + + tab[i].invc = 1/c + tab[i].logc = (double)log(c) + tab2[i].chi = (double)c + tab2[i].clo = (double)(c - (double)c) + +where c is near the center of the subinterval and is chosen by trying +-2^29 +floating point invc candidates around 1/center and selecting one for which + + 1) the rounding error in 0x1.8p9 + logc is 0, + 2) the rounding error in z - chi - clo is < 0x1p-66 and + 3) the rounding error in (double)log(c) is minimized (< 0x1p-66). + +Note: 1) ensures that k*ln2hi + logc can be computed without rounding error, +2) ensures that z/c - 1 can be computed as (z - chi - clo)*invc with close to +a single rounding error when there is no fast fma for z*invc - 1, 3) ensures +that logc + poly(z/c - 1) has small error, however near x == 1 when +|log(x)| < 0x1p-4, this is not enough so that is special cased. */ +.tab = { +#if N == 64 +{0x1.7242886495cd8p+0, -0x1.79e267bdfe000p-2}, +{0x1.6e1f769340dc9p+0, -0x1.6e60ee0ecb000p-2}, +{0x1.6a13ccc8f195cp+0, -0x1.63002fdbf6000p-2}, +{0x1.661ec72e86f3ap+0, -0x1.57bf76c597000p-2}, +{0x1.623fa6c447b16p+0, -0x1.4c9e07f0d2000p-2}, +{0x1.5e75bbca31702p+0, -0x1.419b42f027000p-2}, +{0x1.5ac05655adb10p+0, -0x1.36b67660e6000p-2}, +{0x1.571ed3e940191p+0, -0x1.2bef0839e4800p-2}, +{0x1.539094ac0fbbfp+0, -0x1.21445727cb000p-2}, +{0x1.5015007e7fc42p+0, -0x1.16b5ca3c3d000p-2}, +{0x1.4cab877c31cf9p+0, -0x1.0c42d3805f800p-2}, +{0x1.49539e76a88d3p+0, -0x1.01eae61b60800p-2}, +{0x1.460cbc12211dap+0, -0x1.ef5adb9fb0000p-3}, +{0x1.42d6624debe3ap+0, -0x1.db13daab99000p-3}, +{0x1.3fb0144f0d462p+0, -0x1.c6ffbe896e000p-3}, +{0x1.3c995a1f9a9b4p+0, -0x1.b31d84722d000p-3}, +{0x1.3991c23952500p+0, -0x1.9f6c3cf6eb000p-3}, +{0x1.3698df35eaa14p+0, -0x1.8beafe7f13000p-3}, +{0x1.33ae463091760p+0, -0x1.7898db878d000p-3}, +{0x1.30d190aae3d72p+0, -0x1.6574efe4ec000p-3}, +{0x1.2e025c9203c89p+0, -0x1.527e620845000p-3}, +{0x1.2b404a7244988p+0, -0x1.3fb457d798000p-3}, +{0x1.288b01dc19544p+0, -0x1.2d1615a077000p-3}, +{0x1.25e2268085f69p+0, -0x1.1aa2b431e5000p-3}, +{0x1.23456812abb74p+0, -0x1.08598f1d2b000p-3}, +{0x1.20b4703174157p+0, -0x1.ec738fee40000p-4}, +{0x1.1e2ef308b4e9bp+0, -0x1.c885768862000p-4}, +{0x1.1bb4a36b70a3fp+0, -0x1.a4e75b6a46000p-4}, +{0x1.194538e960658p+0, -0x1.8197efba9a000p-4}, +{0x1.16e0692a10ac8p+0, -0x1.5e95ad734e000p-4}, +{0x1.1485f1ba1568bp+0, -0x1.3bdf67117c000p-4}, +{0x1.12358e123ed6fp+0, -0x1.1973b744f0000p-4}, +{0x1.0fef01de37c8dp+0, -0x1.eea33446bc000p-5}, +{0x1.0db20b82be414p+0, -0x1.aaef4ab304000p-5}, +{0x1.0b7e6f67f69b3p+0, -0x1.67c962fd2c000p-5}, +{0x1.0953f342fc108p+0, -0x1.252f29acf8000p-5}, +{0x1.0732604ec956bp+0, -0x1.c63d19e9c0000p-6}, +{0x1.051980117f9b0p+0, -0x1.432ab6a388000p-6}, +{0x1.03091aa6810f1p+0, -0x1.8244357f50000p-7}, +{0x1.01010152cf066p+0, -0x1.0080a711c0000p-8}, +{0x1.fc07ef6b6e30bp-1, 0x1.fe03018e80000p-8}, +{0x1.f4465aa1024afp-1, 0x1.7b91986450000p-6}, +{0x1.ecc07a8fd3f5ep-1, 0x1.39e88608c8000p-5}, +{0x1.e573ad856b537p-1, 0x1.b42dc6e624000p-5}, +{0x1.de5d6dc7b8057p-1, 0x1.165372ec20000p-4}, +{0x1.d77b6498bddf7p-1, 0x1.51b07a0170000p-4}, +{0x1.d0cb580315c0fp-1, 0x1.8c3465c7ea000p-4}, +{0x1.ca4b30d1cf449p-1, 0x1.c5e544a290000p-4}, +{0x1.c3f8ef4810d8ep-1, 0x1.fec91aa0a6000p-4}, +{0x1.bdd2b8b311f44p-1, 0x1.1b72acdc5c000p-3}, +{0x1.b7d6c2eeac054p-1, 0x1.371fc65a98000p-3}, +{0x1.b20363474c8f5p-1, 0x1.526e61c1aa000p-3}, +{0x1.ac570165eeab1p-1, 0x1.6d60ffc240000p-3}, +{0x1.a6d019f331df4p-1, 0x1.87fa08a013000p-3}, +{0x1.a16d3ebc9e3c3p-1, 0x1.a23bc630c3000p-3}, +{0x1.9c2d14567ef45p-1, 0x1.bc286a3512000p-3}, +{0x1.970e4efae9169p-1, 0x1.d5c2195697000p-3}, +{0x1.920fb3bd0b802p-1, 0x1.ef0ae132d3000p-3}, +{0x1.8d3018b58699ap-1, 0x1.040259974e000p-2}, +{0x1.886e5ff170ee6p-1, 0x1.1058bd40e2000p-2}, +{0x1.83c977ad35d27p-1, 0x1.1c898c1137800p-2}, +{0x1.7f405ed16c520p-1, 0x1.2895a3e65b000p-2}, +{0x1.7ad220d0335c4p-1, 0x1.347dd8f6bd000p-2}, +{0x1.767dce53474fdp-1, 0x1.4043083cb3800p-2}, +#elif N == 128 +{0x1.734f0c3e0de9fp+0, -0x1.7cc7f79e69000p-2}, +{0x1.713786a2ce91fp+0, -0x1.76feec20d0000p-2}, +{0x1.6f26008fab5a0p+0, -0x1.713e31351e000p-2}, +{0x1.6d1a61f138c7dp+0, -0x1.6b85b38287800p-2}, +{0x1.6b1490bc5b4d1p+0, -0x1.65d5590807800p-2}, +{0x1.69147332f0cbap+0, -0x1.602d076180000p-2}, +{0x1.6719f18224223p+0, -0x1.5a8ca86909000p-2}, +{0x1.6524f99a51ed9p+0, -0x1.54f4356035000p-2}, +{0x1.63356aa8f24c4p+0, -0x1.4f637c36b4000p-2}, +{0x1.614b36b9ddc14p+0, -0x1.49da7fda85000p-2}, +{0x1.5f66452c65c4cp+0, -0x1.445923989a800p-2}, +{0x1.5d867b5912c4fp+0, -0x1.3edf439b0b800p-2}, +{0x1.5babccb5b90dep+0, -0x1.396ce448f7000p-2}, +{0x1.59d61f2d91a78p+0, -0x1.3401e17bda000p-2}, +{0x1.5805612465687p+0, -0x1.2e9e2ef468000p-2}, +{0x1.56397cee76bd3p+0, -0x1.2941b3830e000p-2}, +{0x1.54725e2a77f93p+0, -0x1.23ec58cda8800p-2}, +{0x1.52aff42064583p+0, -0x1.1e9e129279000p-2}, +{0x1.50f22dbb2bddfp+0, -0x1.1956d2b48f800p-2}, +{0x1.4f38f4734ded7p+0, -0x1.141679ab9f800p-2}, +{0x1.4d843cfde2840p+0, -0x1.0edd094ef9800p-2}, +{0x1.4bd3ec078a3c8p+0, -0x1.09aa518db1000p-2}, +{0x1.4a27fc3e0258ap+0, -0x1.047e65263b800p-2}, +{0x1.4880524d48434p+0, -0x1.feb224586f000p-3}, +{0x1.46dce1b192d0bp+0, -0x1.f474a7517b000p-3}, +{0x1.453d9d3391854p+0, -0x1.ea4443d103000p-3}, +{0x1.43a2744b4845ap+0, -0x1.e020d44e9b000p-3}, +{0x1.420b54115f8fbp+0, -0x1.d60a22977f000p-3}, +{0x1.40782da3ef4b1p+0, -0x1.cc00104959000p-3}, +{0x1.3ee8f5d57fe8fp+0, -0x1.c202956891000p-3}, +{0x1.3d5d9a00b4ce9p+0, -0x1.b81178d811000p-3}, +{0x1.3bd60c010c12bp+0, -0x1.ae2c9ccd3d000p-3}, +{0x1.3a5242b75dab8p+0, -0x1.a45402e129000p-3}, +{0x1.38d22cd9fd002p+0, -0x1.9a877681df000p-3}, +{0x1.3755bc5847a1cp+0, -0x1.90c6d69483000p-3}, +{0x1.35dce49ad36e2p+0, -0x1.87120a645c000p-3}, +{0x1.34679984dd440p+0, -0x1.7d68fb4143000p-3}, +{0x1.32f5cceffcb24p+0, -0x1.73cb83c627000p-3}, +{0x1.3187775a10d49p+0, -0x1.6a39a9b376000p-3}, +{0x1.301c8373e3990p+0, -0x1.60b3154b7a000p-3}, +{0x1.2eb4ebb95f841p+0, -0x1.5737d76243000p-3}, +{0x1.2d50a0219a9d1p+0, -0x1.4dc7b8fc23000p-3}, +{0x1.2bef9a8b7fd2ap+0, -0x1.4462c51d20000p-3}, +{0x1.2a91c7a0c1babp+0, -0x1.3b08abc830000p-3}, +{0x1.293726014b530p+0, -0x1.31b996b490000p-3}, +{0x1.27dfa5757a1f5p+0, -0x1.2875490a44000p-3}, +{0x1.268b39b1d3bbfp+0, -0x1.1f3b9f879a000p-3}, +{0x1.2539d838ff5bdp+0, -0x1.160c8252ca000p-3}, +{0x1.23eb7aac9083bp+0, -0x1.0ce7f57f72000p-3}, +{0x1.22a012ba940b6p+0, -0x1.03cdc49fea000p-3}, +{0x1.2157996cc4132p+0, -0x1.f57bdbc4b8000p-4}, +{0x1.201201dd2fc9bp+0, -0x1.e370896404000p-4}, +{0x1.1ecf4494d480bp+0, -0x1.d17983ef94000p-4}, +{0x1.1d8f5528f6569p+0, -0x1.bf9674ed8a000p-4}, +{0x1.1c52311577e7cp+0, -0x1.adc79202f6000p-4}, +{0x1.1b17c74cb26e9p+0, -0x1.9c0c3e7288000p-4}, +{0x1.19e010c2c1ab6p+0, -0x1.8a646b372c000p-4}, +{0x1.18ab07bb670bdp+0, -0x1.78d01b3ac0000p-4}, +{0x1.1778a25efbcb6p+0, -0x1.674f145380000p-4}, +{0x1.1648d354c31dap+0, -0x1.55e0e6d878000p-4}, +{0x1.151b990275fddp+0, -0x1.4485cdea1e000p-4}, +{0x1.13f0ea432d24cp+0, -0x1.333d94d6aa000p-4}, +{0x1.12c8b7210f9dap+0, -0x1.22079f8c56000p-4}, +{0x1.11a3028ecb531p+0, -0x1.10e4698622000p-4}, +{0x1.107fbda8434afp+0, -0x1.ffa6c6ad20000p-5}, +{0x1.0f5ee0f4e6bb3p+0, -0x1.dda8d4a774000p-5}, +{0x1.0e4065d2a9fcep+0, -0x1.bbcece4850000p-5}, +{0x1.0d244632ca521p+0, -0x1.9a1894012c000p-5}, +{0x1.0c0a77ce2981ap+0, -0x1.788583302c000p-5}, +{0x1.0af2f83c636d1p+0, -0x1.5715e67d68000p-5}, +{0x1.09ddb98a01339p+0, -0x1.35c8a49658000p-5}, +{0x1.08cabaf52e7dfp+0, -0x1.149e364154000p-5}, +{0x1.07b9f2f4e28fbp+0, -0x1.e72c082eb8000p-6}, +{0x1.06ab58c358f19p+0, -0x1.a55f152528000p-6}, +{0x1.059eea5ecf92cp+0, -0x1.63d62cf818000p-6}, +{0x1.04949cdd12c90p+0, -0x1.228fb8caa0000p-6}, +{0x1.038c6c6f0ada9p+0, -0x1.c317b20f90000p-7}, +{0x1.02865137932a9p+0, -0x1.419355daa0000p-7}, +{0x1.0182427ea7348p+0, -0x1.81203c2ec0000p-8}, +{0x1.008040614b195p+0, -0x1.0040979240000p-9}, +{0x1.fe01ff726fa1ap-1, 0x1.feff384900000p-9}, +{0x1.fa11cc261ea74p-1, 0x1.7dc41353d0000p-7}, +{0x1.f6310b081992ep-1, 0x1.3cea3c4c28000p-6}, +{0x1.f25f63ceeadcdp-1, 0x1.b9fc114890000p-6}, +{0x1.ee9c8039113e7p-1, 0x1.1b0d8ce110000p-5}, +{0x1.eae8078cbb1abp-1, 0x1.58a5bd001c000p-5}, +{0x1.e741aa29d0c9bp-1, 0x1.95c8340d88000p-5}, +{0x1.e3a91830a99b5p-1, 0x1.d276aef578000p-5}, +{0x1.e01e009609a56p-1, 0x1.07598e598c000p-4}, +{0x1.dca01e577bb98p-1, 0x1.253f5e30d2000p-4}, +{0x1.d92f20b7c9103p-1, 0x1.42edd8b380000p-4}, +{0x1.d5cac66fb5ccep-1, 0x1.606598757c000p-4}, +{0x1.d272caa5ede9dp-1, 0x1.7da76356a0000p-4}, +{0x1.cf26e3e6b2ccdp-1, 0x1.9ab434e1c6000p-4}, +{0x1.cbe6da2a77902p-1, 0x1.b78c7bb0d6000p-4}, +{0x1.c8b266d37086dp-1, 0x1.d431332e72000p-4}, +{0x1.c5894bd5d5804p-1, 0x1.f0a3171de6000p-4}, +{0x1.c26b533bb9f8cp-1, 0x1.067152b914000p-3}, +{0x1.bf583eeece73fp-1, 0x1.147858292b000p-3}, +{0x1.bc4fd75db96c1p-1, 0x1.2266ecdca3000p-3}, +{0x1.b951e0c864a28p-1, 0x1.303d7a6c55000p-3}, +{0x1.b65e2c5ef3e2cp-1, 0x1.3dfc33c331000p-3}, +{0x1.b374867c9888bp-1, 0x1.4ba366b7a8000p-3}, +{0x1.b094b211d304ap-1, 0x1.5933928d1f000p-3}, +{0x1.adbe885f2ef7ep-1, 0x1.66acd2418f000p-3}, +{0x1.aaf1d31603da2p-1, 0x1.740f8ec669000p-3}, +{0x1.a82e63fd358a7p-1, 0x1.815c0f51af000p-3}, +{0x1.a5740ef09738bp-1, 0x1.8e92954f68000p-3}, +{0x1.a2c2a90ab4b27p-1, 0x1.9bb3602f84000p-3}, +{0x1.a01a01393f2d1p-1, 0x1.a8bed1c2c0000p-3}, +{0x1.9d79f24db3c1bp-1, 0x1.b5b515c01d000p-3}, +{0x1.9ae2505c7b190p-1, 0x1.c2967ccbcc000p-3}, +{0x1.9852ef297ce2fp-1, 0x1.cf635d5486000p-3}, +{0x1.95cbaeea44b75p-1, 0x1.dc1bd3446c000p-3}, +{0x1.934c69de74838p-1, 0x1.e8c01b8cfe000p-3}, +{0x1.90d4f2f6752e6p-1, 0x1.f5509c0179000p-3}, +{0x1.8e6528effd79dp-1, 0x1.00e6c121fb800p-2}, +{0x1.8bfce9fcc007cp-1, 0x1.071b80e93d000p-2}, +{0x1.899c0dabec30ep-1, 0x1.0d46b9e867000p-2}, +{0x1.87427aa2317fbp-1, 0x1.13687334bd000p-2}, +{0x1.84f00acb39a08p-1, 0x1.1980d67234800p-2}, +{0x1.82a49e8653e55p-1, 0x1.1f8ffe0cc8000p-2}, +{0x1.8060195f40260p-1, 0x1.2595fd7636800p-2}, +{0x1.7e22563e0a329p-1, 0x1.2b9300914a800p-2}, +{0x1.7beb377dcb5adp-1, 0x1.3187210436000p-2}, +{0x1.79baa679725c2p-1, 0x1.377266dec1800p-2}, +{0x1.77907f2170657p-1, 0x1.3d54ffbaf3000p-2}, +{0x1.756cadbd6130cp-1, 0x1.432eee32fe000p-2}, +#endif +}, +#if !__HAVE_FAST_FMA +.tab2 = { +# if N == 64 +{0x1.61ffff94c4fecp-1, -0x1.9fe4fc998f325p-56}, +{0x1.66000020377ddp-1, 0x1.e804c7a9519f2p-55}, +{0x1.6a00004c41678p-1, 0x1.902c675d9ecfep-55}, +{0x1.6dffff7384f87p-1, -0x1.2fd6b95e55043p-56}, +{0x1.720000b37216ep-1, 0x1.802bc8d437043p-55}, +{0x1.75ffffbeb3c9dp-1, 0x1.6047ad0a0d4e4p-57}, +{0x1.7a0000628daep-1, -0x1.e00434b49313dp-56}, +{0x1.7dffffd7abd1ap-1, -0x1.6015f8a083576p-56}, +{0x1.81ffffdf40c54p-1, 0x1.7f54bf76a42c9p-57}, +{0x1.860000f334e11p-1, 0x1.60054cb5344d7p-56}, +{0x1.8a0001238aca7p-1, 0x1.c03c9bd132f55p-57}, +{0x1.8dffffb81d212p-1, -0x1.001e519f2764fp-55}, +{0x1.92000086adc7cp-1, 0x1.1fe40f88f49c6p-55}, +{0x1.960000135d8eap-1, -0x1.f832268dc3095p-55}, +{0x1.99ffff9435acp-1, 0x1.7031d8b835edcp-56}, +{0x1.9e00003478565p-1, -0x1.0030b221ce3eep-58}, +{0x1.a20000b592948p-1, 0x1.8fd2f1dbd4639p-55}, +{0x1.a600000ad0bcfp-1, 0x1.901d6a974e6bep-55}, +{0x1.a9ffff55953a5p-1, 0x1.a07556192db98p-57}, +{0x1.adffff29ce03dp-1, -0x1.fff0717ec71c2p-56}, +{0x1.b1ffff34f3ac8p-1, 0x1.8005573de89d1p-57}, +{0x1.b60000894c55bp-1, -0x1.ff2fb51b044c7p-57}, +{0x1.b9fffef45ec7dp-1, -0x1.9ff7c4e8730fp-56}, +{0x1.be0000cda7b2ap-1, 0x1.57d058dbf3c1dp-55}, +{0x1.c1ffff2c57917p-1, 0x1.7e66d7e48dbc9p-58}, +{0x1.c60000ea5b82ap-1, -0x1.47f5e132ed4bep-55}, +{0x1.ca0001121ae98p-1, -0x1.40958c8d5e00ap-58}, +{0x1.ce0000f9241cbp-1, -0x1.7da063caa81c8p-59}, +{0x1.d1fffe8be95a4p-1, -0x1.82e3a411afcd9p-59}, +{0x1.d5ffff035932bp-1, -0x1.00f901b3fe87dp-58}, +{0x1.d9fffe8b54ba7p-1, 0x1.ffef55d6e3a4p-55}, +{0x1.de0000ad95d19p-1, 0x1.5feb2efd4c7c7p-55}, +{0x1.e1fffe925ce47p-1, 0x1.c8085484eaf08p-55}, +{0x1.e5fffe3ddf853p-1, -0x1.fd5ed02c5cadp-60}, +{0x1.e9fffed0a0e5fp-1, -0x1.a80aaef411586p-55}, +{0x1.ee00008f82eep-1, -0x1.b000aeaf97276p-55}, +{0x1.f20000a22d2f4p-1, -0x1.8f8906e13eba3p-56}, +{0x1.f5fffee35b57dp-1, 0x1.1fdd33b2d3714p-57}, +{0x1.fa00014eec3a6p-1, -0x1.3ee0b7a18c1a5p-58}, +{0x1.fdffff5daa89fp-1, -0x1.c1e24c8e3b503p-58}, +{0x1.0200005b93349p+0, -0x1.50197fe6bedcap-54}, +{0x1.05ffff9d597acp+0, 0x1.20160d062d0dcp-55}, +{0x1.0a00005687a63p+0, -0x1.27f3f9307696ep-54}, +{0x1.0dffff779164ep+0, 0x1.b7eb40bb9c4f4p-54}, +{0x1.12000044a0aa8p+0, 0x1.efbc914d512c4p-55}, +{0x1.16000069685bcp+0, -0x1.c0bea3eb2d82cp-57}, +{0x1.1a000093f0d78p+0, 0x1.1fecbf1e8c52p-54}, +{0x1.1dffffb2b1457p+0, -0x1.3fc91365637d6p-55}, +{0x1.2200008824a1p+0, -0x1.dff7e9feb578ap-54}, +{0x1.25ffffeef953p+0, -0x1.b00a61ec912f7p-55}, +{0x1.2a0000a1e7783p+0, 0x1.60048318b0483p-56}, +{0x1.2e0000853d4c7p+0, -0x1.77fbedf2c8cf3p-54}, +{0x1.320000324c55bp+0, 0x1.f81983997354fp-54}, +{0x1.360000594f796p+0, -0x1.cfe4beff900a9p-54}, +{0x1.3a0000a4c1c0fp+0, 0x1.07dbb2e268d0ep-54}, +{0x1.3e0000751c61bp+0, 0x1.80583ed1c566ep-56}, +{0x1.42000069e8a9fp+0, 0x1.f01f1edf82045p-54}, +{0x1.460000b5a1e34p+0, -0x1.dfdf0cf45c14ap-55}, +{0x1.4a0000187e513p+0, 0x1.401306b83a98dp-55}, +{0x1.4dffff3ba420bp+0, 0x1.9fc6539a6454ep-56}, +{0x1.51fffffe391c9p+0, -0x1.601ef3353ac83p-54}, +{0x1.560000e342455p+0, 0x1.3fb7fac8ac151p-55}, +{0x1.59ffffc39676fp+0, 0x1.4fe7dd6659cc2p-55}, +{0x1.5dfffff10ef42p+0, -0x1.48154cb592bcbp-54}, +# elif N == 128 +{0x1.61000014fb66bp-1, 0x1.e026c91425b3cp-56}, +{0x1.63000034db495p-1, 0x1.dbfea48005d41p-55}, +{0x1.650000d94d478p-1, 0x1.e7fa786d6a5b7p-55}, +{0x1.67000074e6fadp-1, 0x1.1fcea6b54254cp-57}, +{0x1.68ffffedf0faep-1, -0x1.c7e274c590efdp-56}, +{0x1.6b0000763c5bcp-1, -0x1.ac16848dcda01p-55}, +{0x1.6d0001e5cc1f6p-1, 0x1.33f1c9d499311p-55}, +{0x1.6efffeb05f63ep-1, -0x1.e80041ae22d53p-56}, +{0x1.710000e86978p-1, 0x1.bff6671097952p-56}, +{0x1.72ffffc67e912p-1, 0x1.c00e226bd8724p-55}, +{0x1.74fffdf81116ap-1, -0x1.e02916ef101d2p-57}, +{0x1.770000f679c9p-1, -0x1.7fc71cd549c74p-57}, +{0x1.78ffffa7ec835p-1, 0x1.1bec19ef50483p-55}, +{0x1.7affffe20c2e6p-1, -0x1.07e1729cc6465p-56}, +{0x1.7cfffed3fc9p-1, -0x1.08072087b8b1cp-55}, +{0x1.7efffe9261a76p-1, 0x1.dc0286d9df9aep-55}, +{0x1.81000049ca3e8p-1, 0x1.97fd251e54c33p-55}, +{0x1.8300017932c8fp-1, -0x1.afee9b630f381p-55}, +{0x1.850000633739cp-1, 0x1.9bfbf6b6535bcp-55}, +{0x1.87000204289c6p-1, -0x1.bbf65f3117b75p-55}, +{0x1.88fffebf57904p-1, -0x1.9006ea23dcb57p-55}, +{0x1.8b00022bc04dfp-1, -0x1.d00df38e04b0ap-56}, +{0x1.8cfffe50c1b8ap-1, -0x1.8007146ff9f05p-55}, +{0x1.8effffc918e43p-1, 0x1.3817bd07a7038p-55}, +{0x1.910001efa5fc7p-1, 0x1.93e9176dfb403p-55}, +{0x1.9300013467bb9p-1, 0x1.f804e4b980276p-56}, +{0x1.94fffe6ee076fp-1, -0x1.f7ef0d9ff622ep-55}, +{0x1.96fffde3c12d1p-1, -0x1.082aa962638bap-56}, +{0x1.98ffff4458a0dp-1, -0x1.7801b9164a8efp-55}, +{0x1.9afffdd982e3ep-1, -0x1.740e08a5a9337p-55}, +{0x1.9cfffed49fb66p-1, 0x1.fce08c19bep-60}, +{0x1.9f00020f19c51p-1, -0x1.a3faa27885b0ap-55}, +{0x1.a10001145b006p-1, 0x1.4ff489958da56p-56}, +{0x1.a300007bbf6fap-1, 0x1.cbeab8a2b6d18p-55}, +{0x1.a500010971d79p-1, 0x1.8fecadd78793p-55}, +{0x1.a70001df52e48p-1, -0x1.f41763dd8abdbp-55}, +{0x1.a90001c593352p-1, -0x1.ebf0284c27612p-55}, +{0x1.ab0002a4f3e4bp-1, -0x1.9fd043cff3f5fp-57}, +{0x1.acfffd7ae1ed1p-1, -0x1.23ee7129070b4p-55}, +{0x1.aefffee510478p-1, 0x1.a063ee00edea3p-57}, +{0x1.b0fffdb650d5bp-1, 0x1.a06c8381f0ab9p-58}, +{0x1.b2ffffeaaca57p-1, -0x1.9011e74233c1dp-56}, +{0x1.b4fffd995badcp-1, -0x1.9ff1068862a9fp-56}, +{0x1.b7000249e659cp-1, 0x1.aff45d0864f3ep-55}, +{0x1.b8ffff987164p-1, 0x1.cfe7796c2c3f9p-56}, +{0x1.bafffd204cb4fp-1, -0x1.3ff27eef22bc4p-57}, +{0x1.bcfffd2415c45p-1, -0x1.cffb7ee3bea21p-57}, +{0x1.beffff86309dfp-1, -0x1.14103972e0b5cp-55}, +{0x1.c0fffe1b57653p-1, 0x1.bc16494b76a19p-55}, +{0x1.c2ffff1fa57e3p-1, -0x1.4feef8d30c6edp-57}, +{0x1.c4fffdcbfe424p-1, -0x1.43f68bcec4775p-55}, +{0x1.c6fffed54b9f7p-1, 0x1.47ea3f053e0ecp-55}, +{0x1.c8fffeb998fd5p-1, 0x1.383068df992f1p-56}, +{0x1.cb0002125219ap-1, -0x1.8fd8e64180e04p-57}, +{0x1.ccfffdd94469cp-1, 0x1.e7ebe1cc7ea72p-55}, +{0x1.cefffeafdc476p-1, 0x1.ebe39ad9f88fep-55}, +{0x1.d1000169af82bp-1, 0x1.57d91a8b95a71p-56}, +{0x1.d30000d0ff71dp-1, 0x1.9c1906970c7dap-55}, +{0x1.d4fffea790fc4p-1, -0x1.80e37c558fe0cp-58}, +{0x1.d70002edc87e5p-1, -0x1.f80d64dc10f44p-56}, +{0x1.d900021dc82aap-1, -0x1.47c8f94fd5c5cp-56}, +{0x1.dafffd86b0283p-1, 0x1.c7f1dc521617ep-55}, +{0x1.dd000296c4739p-1, 0x1.8019eb2ffb153p-55}, +{0x1.defffe54490f5p-1, 0x1.e00d2c652cc89p-57}, +{0x1.e0fffcdabf694p-1, -0x1.f8340202d69d2p-56}, +{0x1.e2fffdb52c8ddp-1, 0x1.b00c1ca1b0864p-56}, +{0x1.e4ffff24216efp-1, 0x1.2ffa8b094ab51p-56}, +{0x1.e6fffe88a5e11p-1, -0x1.7f673b1efbe59p-58}, +{0x1.e9000119eff0dp-1, -0x1.4808d5e0bc801p-55}, +{0x1.eafffdfa51744p-1, 0x1.80006d54320b5p-56}, +{0x1.ed0001a127fa1p-1, -0x1.002f860565c92p-58}, +{0x1.ef00007babcc4p-1, -0x1.540445d35e611p-55}, +{0x1.f0ffff57a8d02p-1, -0x1.ffb3139ef9105p-59}, +{0x1.f30001ee58ac7p-1, 0x1.a81acf2731155p-55}, +{0x1.f4ffff5823494p-1, 0x1.a3f41d4d7c743p-55}, +{0x1.f6ffffca94c6bp-1, -0x1.202f41c987875p-57}, +{0x1.f8fffe1f9c441p-1, 0x1.77dd1f477e74bp-56}, +{0x1.fafffd2e0e37ep-1, -0x1.f01199a7ca331p-57}, +{0x1.fd0001c77e49ep-1, 0x1.181ee4bceacb1p-56}, +{0x1.feffff7e0c331p-1, -0x1.e05370170875ap-57}, +{0x1.00ffff465606ep+0, -0x1.a7ead491c0adap-55}, +{0x1.02ffff3867a58p+0, -0x1.77f69c3fcb2ep-54}, +{0x1.04ffffdfc0d17p+0, 0x1.7bffe34cb945bp-54}, +{0x1.0700003cd4d82p+0, 0x1.20083c0e456cbp-55}, +{0x1.08ffff9f2cbe8p+0, -0x1.dffdfbe37751ap-57}, +{0x1.0b000010cda65p+0, -0x1.13f7faee626ebp-54}, +{0x1.0d00001a4d338p+0, 0x1.07dfa79489ff7p-55}, +{0x1.0effffadafdfdp+0, -0x1.7040570d66bcp-56}, +{0x1.110000bbafd96p+0, 0x1.e80d4846d0b62p-55}, +{0x1.12ffffae5f45dp+0, 0x1.dbffa64fd36efp-54}, +{0x1.150000dd59ad9p+0, 0x1.a0077701250aep-54}, +{0x1.170000f21559ap+0, 0x1.dfdf9e2e3deeep-55}, +{0x1.18ffffc275426p+0, 0x1.10030dc3b7273p-54}, +{0x1.1b000123d3c59p+0, 0x1.97f7980030188p-54}, +{0x1.1cffff8299eb7p+0, -0x1.5f932ab9f8c67p-57}, +{0x1.1effff48ad4p+0, 0x1.37fbf9da75bebp-54}, +{0x1.210000c8b86a4p+0, 0x1.f806b91fd5b22p-54}, +{0x1.2300003854303p+0, 0x1.3ffc2eb9fbf33p-54}, +{0x1.24fffffbcf684p+0, 0x1.601e77e2e2e72p-56}, +{0x1.26ffff52921d9p+0, 0x1.ffcbb767f0c61p-56}, +{0x1.2900014933a3cp+0, -0x1.202ca3c02412bp-56}, +{0x1.2b00014556313p+0, -0x1.2808233f21f02p-54}, +{0x1.2cfffebfe523bp+0, -0x1.8ff7e384fdcf2p-55}, +{0x1.2f0000bb8ad96p+0, -0x1.5ff51503041c5p-55}, +{0x1.30ffffb7ae2afp+0, -0x1.10071885e289dp-55}, +{0x1.32ffffeac5f7fp+0, -0x1.1ff5d3fb7b715p-54}, +{0x1.350000ca66756p+0, 0x1.57f82228b82bdp-54}, +{0x1.3700011fbf721p+0, 0x1.000bac40dd5ccp-55}, +{0x1.38ffff9592fb9p+0, -0x1.43f9d2db2a751p-54}, +{0x1.3b00004ddd242p+0, 0x1.57f6b707638e1p-55}, +{0x1.3cffff5b2c957p+0, 0x1.a023a10bf1231p-56}, +{0x1.3efffeab0b418p+0, 0x1.87f6d66b152bp-54}, +{0x1.410001532aff4p+0, 0x1.7f8375f198524p-57}, +{0x1.4300017478b29p+0, 0x1.301e672dc5143p-55}, +{0x1.44fffe795b463p+0, 0x1.9ff69b8b2895ap-55}, +{0x1.46fffe80475ep+0, -0x1.5c0b19bc2f254p-54}, +{0x1.48fffef6fc1e7p+0, 0x1.b4009f23a2a72p-54}, +{0x1.4afffe5bea704p+0, -0x1.4ffb7bf0d7d45p-54}, +{0x1.4d000171027dep+0, -0x1.9c06471dc6a3dp-54}, +{0x1.4f0000ff03ee2p+0, 0x1.77f890b85531cp-54}, +{0x1.5100012dc4bd1p+0, 0x1.004657166a436p-57}, +{0x1.530001605277ap+0, -0x1.6bfcece233209p-54}, +{0x1.54fffecdb704cp+0, -0x1.902720505a1d7p-55}, +{0x1.56fffef5f54a9p+0, 0x1.bbfe60ec96412p-54}, +{0x1.5900017e61012p+0, 0x1.87ec581afef9p-55}, +{0x1.5b00003c93e92p+0, -0x1.f41080abf0ccp-54}, +{0x1.5d0001d4919bcp+0, -0x1.8812afb254729p-54}, +{0x1.5efffe7b87a89p+0, -0x1.47eb780ed6904p-54}, +#endif +}, +#endif /* !__HAVE_FAST_FMA */ +}; +#endif /* __OBSOLETE_MATH_DOUBLE */ diff --git a/src/audio/ffmpeg_dec/libm/common/math_config.h b/src/audio/ffmpeg_dec/libm/common/math_config.h new file mode 100644 index 000000000000..f72b901ce3dd --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/math_config.h @@ -0,0 +1,1320 @@ +/* Configuration for math routines. + Copyright (c) 2017-2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#ifndef _MATH_CONFIG_H +#define _MATH_CONFIG_H + +#include +#include +#include +#include +#include + +#ifndef WANT_ROUNDING +/* Correct special case results in non-nearest rounding modes. */ +# define WANT_ROUNDING 1 +#endif +#ifdef __IEEE_LIBM +# define WANT_ERRNO 0 +# define _LIB_VERSION _IEEE_ +#else +/* Set errno according to ISO C with (math_errhandling & MATH_ERRNO) != 0. */ +# define WANT_ERRNO 1 +# define _LIB_VERSION _POSIX_ +#endif +#ifndef WANT_ERRNO_UFLOW +/* Set errno to ERANGE if result underflows to 0 (in all rounding modes). */ +# define WANT_ERRNO_UFLOW (WANT_ROUNDING && WANT_ERRNO) +#endif + +#define _IEEE_ -1 +#define _POSIX_ 0 + +# define likely(x) __builtin_expect (x, 1) +# define unlikely(x) __builtin_expect (x, 0) + +/* Compiler can inline round as a single instruction. */ +#ifndef HAVE_FAST_ROUND +# if __aarch64__ +# define HAVE_FAST_ROUND 1 +# else +# define HAVE_FAST_ROUND 0 +# endif +#endif + +/* Compiler can inline lround, but not (long)round(x). */ +#ifndef HAVE_FAST_LROUND +# if __aarch64__ && (100*__GNUC__ + __GNUC_MINOR__) >= 408 && __NO_MATH_ERRNO__ +# define HAVE_FAST_LROUND 1 +# else +# define HAVE_FAST_LROUND 0 +# endif +#endif + +#if HAVE_FAST_ROUND +/* When set, the roundtoint and converttoint functions are provided with + the semantics documented below. */ +# define TOINT_INTRINSICS 1 + +/* Round x to nearest int in all rounding modes, ties have to be rounded + consistently with converttoint so the results match. If the result + would be outside of [-2^31, 2^31-1] then the semantics is unspecified. */ +static __always_inline double_t +roundtoint (double_t x) +{ + return round (x); +} + +/* Convert x to nearest int in all rounding modes, ties have to be rounded + consistently with roundtoint. If the result is not representible in an + int32_t then the semantics is unspecified. */ +static __always_inline int32_t +converttoint (double_t x) +{ +# if HAVE_FAST_LROUND + return lround (x); +# else + return (long) round (x); +# endif +} +#endif + +#ifndef TOINT_INTRINSICS +# define TOINT_INTRINSICS 0 +#endif + +#if __SIZEOF_DOUBLE__ == 8 +typedef double __float64; +# define _NEED_FLOAT64 +#elif __SIZEOF_LONG_DOUBLE__ == 8 +typedef long double __float64; +# define _NEED_FLOAT64 +#endif + +static __always_inline uint32_t +asuint (float f) +{ +#if defined(__riscv_flen) && __riscv_flen >= 32 + uint32_t result; + __asm__("fmv.x.w\t%0, %1" : "=r" (result) : "f" (f)); + return result; +#else + union + { + float f; + uint32_t i; + } u = {f}; + return u.i; +#endif +} + +static __always_inline float +asfloat (uint32_t i) +{ +#if defined(__riscv_flen) && __riscv_flen >= 32 + float result; + __asm__("fmv.w.x\t%0, %1" : "=f" (result) : "r" (i)); + return result; +#else + union + { + uint32_t i; + float f; + } u = {i}; + return u.f; +#endif +} + +static __always_inline int32_t +_asint32 (float f) +{ + return (int32_t) asuint(f); +} + +static __always_inline int +_sign32(int32_t ix) +{ + return ((uint32_t) ix) >> 31; +} + +static __always_inline int +_exponent32(int32_t ix) +{ + return (ix >> 23) & 0xff; +} + +static __always_inline int32_t +_significand32(int32_t ix) +{ + return ix & 0x7fffff; +} + +static __always_inline float +_asfloat(int32_t i) +{ + return asfloat((uint32_t) i); +} + +static __always_inline int +issignalingf_inline (float x) +{ + uint32_t ix = asuint (x); + if (!_IEEE_754_2008_SNAN) + return (ix & 0x7fc00000u) == 0x7fc00000u; + return 2 * (ix ^ 0x00400000u) > 0xFF800000u; +} + +#ifdef _NEED_FLOAT64 +static __always_inline int +_sign64(int64_t ix) +{ + return ((uint64_t) ix) >> 63; +} + +static __always_inline int +_exponent64(int64_t ix) +{ + return (ix >> 52) & 0x7ff; +} + +static __always_inline int64_t +_significand64(int64_t ix) +{ + return ix & 0xfffffffffffffLL; +} + +static __always_inline uint64_t +asuint64 (__float64 f) +{ +#if defined(__riscv_flen) && __riscv_flen >= 64 && __riscv_xlen >= 64 + uint64_t result; + __asm__("fmv.x.d\t%0, %1" : "=r" (result) : "f" (f)); + return result; +#else + union + { + __float64 f; + uint64_t i; + } u = {f}; + return u.i; +#endif +} + +static __always_inline __float64 +asfloat64 (uint64_t i) +{ +#if defined(__riscv_flen) && __riscv_flen >= 64 && __riscv_xlen >= 64 + __float64 result; + __asm__("fmv.d.x\t%0, %1" : "=f" (result) : "r" (i)); + return result; +#else + union + { + uint64_t i; + __float64 f; + } u = {i}; + return u.f; +#endif +} + +static __always_inline int64_t +_asint64(__float64 f) +{ + return (int64_t) asuint64(f); +} + +static __always_inline __float64 +_asfloat64(int64_t i) +{ + return asfloat64((uint64_t) i); +} + +static __always_inline int +issignaling64_inline (__float64 x) +{ + uint64_t ix = asuint64 (x); + if (!_IEEE_754_2008_SNAN) + return (ix & 0x7ff8000000000000ULL) == 0x7ff8000000000000ULL; + return 2 * (ix ^ 0x0008000000000000ULL) > 2 * 0x7ff8000000000000ULL; +} + +#endif /* _NEED_FLOAT64 */ +/* + * gcc older than version 13 places 'const volatile' variables in + * .data while clang places them in .rodata. gcc never optimizes away + * math operations which might generate exceptions while clang will + * happily do so. Therefore, we don't need volatile with gcc, but we + * do need it for clang. And, we don't want volatile with gcc to avoid + * placing these constants in the .data section with older gcc versions + */ + +#ifdef __clang__ +#define CONST_FORCE_FLOAT_MODIFIER volatile +#else +#define CONST_FORCE_FLOAT_MODIFIER +#endif + +#ifdef __FLOAT_NOEXCEPT +#define FORCE_FLOAT float +#define CONST_FORCE_FLOAT const float +#define pick_float_except(expr,val) (val) +#else +#define FORCE_FLOAT volatile float +#define CONST_FORCE_FLOAT const CONST_FORCE_FLOAT_MODIFIER float +#define pick_float_except(expr,val) (expr) +#endif + +#ifdef __DOUBLE_NOEXCEPT +#define FORCE_DOUBLE double +#define CONST_FORCE_DOUBLE const double +#define pick_double_except(expr,val) (val) +#else +#define FORCE_DOUBLE volatile double +#define CONST_FORCE_DOUBLE const CONST_FORCE_FLOAT_MODIFIER double +#define pick_double_except(expr,val) (expr) +#endif + +#ifdef __LONG_DOUBLE_NOEXCEPT +#define FORCE_LONG_DOUBLE long double +#define CONST_FORCE_LONG_DOUBLE const long double +#define pick_long_double_except(expr,val) (val) +#else +#define FORCE_LONG_DOUBLE volatile long double +#define CONST_FORCE_LONG_DOUBLE const CONST_FORCE_FLOAT_MODIFIER long double +#define pick_long_double_except(expr,val) (expr) +#endif + +static __always_inline float +opt_barrier_float (float x) +{ + FORCE_FLOAT y = x; + return y; +} + +static __always_inline double +opt_barrier_double (double x) +{ + FORCE_DOUBLE y = x; + return y; +} + +static __always_inline void +force_eval_float (float x) +{ + FORCE_FLOAT y = x; + (void) y; +} + +static __always_inline void +force_eval_double (double x) +{ + FORCE_DOUBLE y = x; + (void) y; +} + +#ifdef __HAVE_LONG_DOUBLE +static __always_inline long double +opt_barrier_long_double (long double x) +{ + FORCE_LONG_DOUBLE y = x; + return y; +} + +static __always_inline void +force_eval_long_double (long double x) +{ + FORCE_LONG_DOUBLE y = x; + (void) y; +} +#endif + +/* Clang doesn't appear to suppor precise exceptions on + * many targets. We introduce barriers for that compiler + * to force evaluation order where needed + */ +#ifdef __clang__ +#define clang_barrier_long_double(x) opt_barrier_long_double(x) +#define clang_barrier_double(x) opt_barrier_double(x) +#define clang_barrier_float(x) opt_barrier_float(x) +#define clang_force_double(x) force_eval_double(x) +#define clang_force_float(x) force_eval_float(x) +#else +#define clang_barrier_long_double(x) (x) +#define clang_barrier_double(x) (x) +#define clang_barrier_float(x) (x) +#define clang_force_double(x) (x) +#define clang_force_float(x) (x) +#endif + +#ifdef _ADD_UNDER_R_TO_FUNCS + +#define __FLOAT_NAME(x) x ## f_r +#define _FLOAT_NAME(x) __FLOAT_NAME(x) +#define _FLOAT_ALIAS(x) # x "f_r" + +#define __LD_NAME(x) x ## l_r +#define _LD_NAME(x) __LD_NAME(x) +#define _LD_ALIAS(x) # x "l_r" + +#else + +#define __FLOAT_NAME(x) x ## f +#define _FLOAT_NAME(x) __FLOAT_NAME(x) +#define _FLOAT_ALIAS(x) # x "f" + +#define __LD_NAME(x) x ## l +#define _LD_NAME(x) __LD_NAME(x) +#define _LD_ALIAS(x) # x "l" + +#endif + +#define __FLOAT_NAME_REG(x) x ## f +#define _FLOAT_NAME_REG(x) __FLOAT_NAME_REG(x) + +#define __LD_NAME_REG(x) x ## l +#define _LD_NAME_REG(x) __LD_NAME_REG(x) + +#ifdef _ADD_D_TO_DOUBLE_FUNCS + +#define __D_NAME(x) x ## d +#define _D_NAME(x) __D_NAME(x) +#define _D_ALIAS(x) # x "d" + +#elif defined(_ADD_UNDER_R_TO_FUNCS) + +#define __D_NAME(x) x ## _r +#define _D_NAME(x) __D_NAME(x) +#define _D_ALIAS(x) # x "_r" + +#else + +#define __D_NAME(x) x +#define _D_NAME(x) __D_NAME(x) +#define _D_ALIAS(x) # x + +#endif + +#define __D_NAME_REG(x) x +#define _D_NAME_REG(x) __D_NAME_REG(x) + +/* + * Figure out how to map the 32- and 64- bit functions to + * the three C float types (float, double, long double). + * + * 'float' is assumed to be a 32-bit IEEE value. + * + * Internal names for 64-bit funcs are unqualified, + * Internal names for 32-bit funcs have a trailing 'f' + * Internal names for longer funcs have a trailing 'l' + * + * When types are the same size, they are assumed to have the same + * representation. Aliases are generated in this case to eliminate + * overhead. + * + * 32-bit functions are always 'float' + * 64-bit functions may either be 'double' or 'long double', + * if at least one of those is 64 bits in size + * + * There is limited support for long double greater than 64 bits + */ + +#ifdef __GNUCLIKE_PRAGMA_DIAGNOSTIC +#pragma GCC diagnostic ignored "-Wpragmas" +#pragma GCC diagnostic ignored "-Wunknown-warning-option" +#pragma GCC diagnostic ignored "-Wattribute-alias=" +#pragma GCC diagnostic ignored "-Wmissing-attributes" +#endif + +#ifdef _DOUBLE_IS_32BITS +# ifdef __strong_reference +# define _MATH_ALIAS_d_to_f(name) __strong_reference_dup(_FLOAT_NAME(name), _D_NAME(name)); +# define _MATH_ALIAS_d_d_to_f(name) _MATH_ALIAS_d_to_f(name) +# define _MATH_ALIAS_d_D_to_f(name) _MATH_ALIAS_d_to_f(name) +# define _MATH_ALIAS_d_s_to_f(name) _MATH_ALIAS_d_to_f(name) +# define _MATH_ALIAS_d_dd_to_f(name) _MATH_ALIAS_d_to_f(name) +# define _MATH_ALIAS_d_dl_to_f(name) _MATH_ALIAS_d_to_f(name) +# define _MATH_ALIAS_d_dD_to_f(name) _MATH_ALIAS_d_to_f(name) +# define _MATH_ALIAS_d_ddd_to_f(name) _MATH_ALIAS_d_to_f(name) +# define _MATH_ALIAS_d_dI_to_f(name) _MATH_ALIAS_d_to_f(name) +# define _MATH_ALIAS_d_ddI_to_f(name) _MATH_ALIAS_d_to_f(name) +# define _MATH_ALIAS_d_id_to_f(name) _MATH_ALIAS_d_to_f(name) +# define _MATH_ALIAS_d_di_to_f(name) _MATH_ALIAS_d_to_f(name) +# define _MATH_ALIAS_d_dj_to_f(name) _MATH_ALIAS_d_to_f(name) +# define _MATH_ALIAS_i_d_to_f(name) _MATH_ALIAS_d_to_f(name) +# define _MATH_ALIAS_j_d_to_f(name) _MATH_ALIAS_d_to_f(name) +# define _MATH_ALIAS_k_d_to_f(name) _MATH_ALIAS_d_to_f(name) +# define _MATH_ALIAS_i_dd_to_f(name) _MATH_ALIAS_d_to_f(name) +# define _MATH_ALIAS_v_dDD_to_f(name) _MATH_ALIAS_d_to_f(name) +# else +# define _MATH_ALIAS_d_to_f(name) double _D_NAME(name)(void) { return (double) __FLOAT_NAME(name)(); } +# define _MATH_ALIAS_d_d_to_f(name) double _D_NAME(name)(double x) { return (double) __FLOAT_NAME(name)((float) x); } +# define _MATH_ALIAS_d_D_to_f(name) double _D_NAME(name)(const double *x) { return (double) __FLOAT_NAME(name)((float *) x); } +# define _MATH_ALIAS_d_s_to_f(name) double _D_NAME(name)(const char *x) { return (double) __FLOAT_NAME(name)(x); } +# define _MATH_ALIAS_d_dd_to_f(name) double _D_NAME(name)(double x, double y) { return (double) __FLOAT_NAME(name)((float) x, (float) y); } +# define _MATH_ALIAS_d_dl_to_f(name) double _D_NAME(name)(double x, long double y) { return (double) __FLOAT_NAME(name)((float) x, y); } +# define _MATH_ALIAS_d_dD_to_f(name) double _D_NAME(name)(double x, double *y) { return (double) __FLOAT_NAME(name)((float) x, (float *) y); } +# define _MATH_ALIAS_d_ddd_to_f(name) double _D_NAME(name)(double x, double y, double z) { return (double) __FLOAT_NAME(name)((float) x, (float) y, (float) z); } +# define _MATH_ALIAS_d_dI_to_f(name) double _D_NAME(name)(double x, int *y) { return (double) __FLOAT_NAME(name)((float) x, y); } +# define _MATH_ALIAS_d_ddI_to_f(name) double _D_NAME(name)(double x, double y, int *z) { return (double) __FLOAT_NAME(name)((float) x, (float) y, z); } +# define _MATH_ALIAS_d_id_to_f(name) double _D_NAME(name)(int n, double x) { return (double) __FLOAT_NAME(name)(n, (float) x); } +# define _MATH_ALIAS_d_di_to_f(name) double _D_NAME(name)(double x, int n) { return (double) __FLOAT_NAME(name)((float) x, n); } +# define _MATH_ALIAS_d_dj_to_f(name) double _D_NAME(name)(double x, long n) { return (double) __FLOAT_NAME(name)((float) x, n); } +# define _MATH_ALIAS_i_d_to_f(name) int _D_NAME(name)(double x) { return __FLOAT_NAME(name)((float) x); } +# define _MATH_ALIAS_j_d_to_f(name) long _D_NAME(name)(double x) { return __FLOAT_NAME(name)((float) x); } +# define _MATH_ALIAS_k_d_to_f(name) long long _D_NAME(name)(double x) { return __FLOAT_NAME(name)((float) x); } +# define _MATH_ALIAS_i_dd_to_f(name) int _D_NAME(name)(double x, double y) { return __FLOAT_NAME(name)((float) x, (float) y); } +# define _MATH_ALIAS_v_dDD_to_f(name) void _D_NAME(name)(double x, double *y, double *z) { return __FLOAT_NAME(name)((float) x, (float *) y, (float *) z); } +# endif +#else +# define _MATH_ALIAS_d_to_f(name) +# define _MATH_ALIAS_d_d_to_f(name) +# define _MATH_ALIAS_d_D_to_f(name) +# define _MATH_ALIAS_d_s_to_f(name) +# define _MATH_ALIAS_d_dd_to_f(name) +# define _MATH_ALIAS_d_dl_to_f(name) +# define _MATH_ALIAS_d_dD_to_f(name) +# define _MATH_ALIAS_d_ddd_to_f(name) +# define _MATH_ALIAS_d_dI_to_f(name) +# define _MATH_ALIAS_d_ddI_to_f(name) +# define _MATH_ALIAS_d_id_to_f(name) +# define _MATH_ALIAS_d_di_to_f(name) +# define _MATH_ALIAS_d_dj_to_f(name) +# define _MATH_ALIAS_i_d_to_f(name) +# define _MATH_ALIAS_j_d_to_f(name) +# define _MATH_ALIAS_k_d_to_f(name) +# define _MATH_ALIAS_i_dd_to_f(name) +# define _MATH_ALIAS_v_dDD_to_f(name) +# ifdef __DOUBLE_NOEXCEPT +# define __FLOAT64_NOEXCEPT +# endif +# ifdef __DOUBLE_NOROUND +# define __FLOAT64_NOROUND +# endif +#endif + +#ifdef _LDBL_EQ_DBL +# ifdef _DOUBLE_IS_32BITS +# ifdef __strong_reference +# define _MATH_ALIAS_l_to_f(name) __strong_reference_dup(_FLOAT_NAME(name), _LD_NAME(name)); +# define _MATH_ALIAS_l_l_to_f(name) _MATH_ALIAS_l_to_f(name) +# define _MATH_ALIAS_l_L_to_f(name) _MATH_ALIAS_l_to_f(name) +# define _MATH_ALIAS_l_s_to_f(name) _MATH_ALIAS_l_to_f(name) +# define _MATH_ALIAS_l_ll_to_f(name) _MATH_ALIAS_l_to_f(name) +# define _MATH_ALIAS_l_lL_to_f(name) _MATH_ALIAS_l_to_f(name) +# define _MATH_ALIAS_l_lll_to_f(name) _MATH_ALIAS_l_to_f(name) +# define _MATH_ALIAS_l_lI_to_f(name) _MATH_ALIAS_l_to_f(name) +# define _MATH_ALIAS_l_llI_to_f(name) _MATH_ALIAS_l_to_f(name) +# define _MATH_ALIAS_l_il_to_f(name) _MATH_ALIAS_l_to_f(name) +# define _MATH_ALIAS_l_li_to_f(name) _MATH_ALIAS_l_to_f(name) +# define _MATH_ALIAS_l_lj_to_f(name) _MATH_ALIAS_l_to_f(name) +# define _MATH_ALIAS_i_l_to_f(name) _MATH_ALIAS_l_to_f(name) +# define _MATH_ALIAS_j_l_to_f(name) _MATH_ALIAS_l_to_f(name) +# define _MATH_ALIAS_k_l_to_f(name) _MATH_ALIAS_l_to_f(name) +# define _MATH_ALIAS_i_ll_to_f(name) _MATH_ALIAS_l_to_f(name) +# define _MATH_ALIAS_v_lLL_to_f(name) _MATH_ALIAS_l_to_f(name) +# else +# define _MATH_ALIAS_l_to_f(name) long double _LD_NAME(name)(void) { return (long double) _FLOAT_NAME(name)(); } +# define _MATH_ALIAS_l_l_to_f(name) long double _LD_NAME(name)(long double x) { return (long double) _FLOAT_NAME(name)((float) x); } +# define _MATH_ALIAS_l_L_to_f(name) long double _LD_NAME(name)(const long double *x) { return (long double) _FLOAT_NAME(name)((float *) x); } +# define _MATH_ALIAS_l_s_to_f(name) long double _LD_NAME(name)(const char *x) { return (long double) _FLOAT_NAME(name)(x); } +# define _MATH_ALIAS_l_ll_to_f(name) long double _LD_NAME(name)(long double x, long double y) { return (long double) _FLOAT_NAME(name)((float) x, (float) y); } +# define _MATH_ALIAS_l_lL_to_f(name) long double _LD_NAME(name)(long double x, long double *y) { return (long double) _FLOAT_NAME(name)((float) x, (float *) y); } +# define _MATH_ALIAS_l_lll_to_f(name) long double _LD_NAME(name)(long double x, long double y, long double z) { return (long double) _FLOAT_NAME(name)((float) x, (float) y, (float) z); } +# define _MATH_ALIAS_l_lI_to_f(name) long double _LD_NAME(name)(long double x, int *y) { return (long double) _FLOAT_NAME(name)((float) x, y); } +# define _MATH_ALIAS_l_llI_to_f(name) long double _LD_NAME(name)(long double x, long double y, int *z) { return (long double) _FLOAT_NAME(name)((float) x, (float) y, z); } +# define _MATH_ALIAS_l_il_to_f(name) long double _LD_NAME(name)(int n, long double x) { return (long double) _FLOAT_NAME(name)(n, (float) x); } +# define _MATH_ALIAS_l_li_to_f(name) long double _LD_NAME(name)(long double x, int n) { return (long double) _FLOAT_NAME(name)((float) x, n); } +# define _MATH_ALIAS_l_lj_to_f(name) long double _LD_NAME(name)(long double x, long n) { return (long double) _FLOAT_NAME(name)((float) x, n); } +# define _MATH_ALIAS_i_l_to_f(name) int _LD_NAME(name)(long double x) { return _FLOAT_NAME(name)((float) x); } +# define _MATH_ALIAS_j_l_to_f(name) long _LD_NAME(name)(long double x) { return _FLOAT_NAME(name)((float) x); } +# define _MATH_ALIAS_k_l_to_f(name) long long _LD_NAME(name)(long double x) { return _FLOAT_NAME(name)((float) x); } +# define _MATH_ALIAS_i_ll_to_f(name) int _LD_NAME(name)(long double x, long double y) { return _FLOAT_NAME(name)((float) x, (float) y); } +# define _MATH_ALIAS_v_lLL_to_f(name) void _LD_NAME(name)(long double x, long double *y, long double *z) { return _FLOAT_NAME(name)((float) x, (float *) y, (float *) z); } +# endif +# define _MATH_ALIAS_l_to_d(name) +# define _MATH_ALIAS_l_l_to_d(name) +# define _MATH_ALIAS_l_L_to_d(name) +# define _MATH_ALIAS_l_s_to_d(name) +# define _MATH_ALIAS_l_ll_to_d(name) +# define _MATH_ALIAS_l_lL_to_d(name) +# define _MATH_ALIAS_l_lll_to_d(name) +# define _MATH_ALIAS_l_lI_to_d(name) +# define _MATH_ALIAS_l_llI_to_d(name) +# define _MATH_ALIAS_l_il_to_d(name) +# define _MATH_ALIAS_l_li_to_d(name) +# define _MATH_ALIAS_l_lj_to_d(name) +# define _MATH_ALIAS_i_l_to_d(name) +# define _MATH_ALIAS_j_l_to_d(name) +# define _MATH_ALIAS_k_l_to_d(name) +# define _MATH_ALIAS_i_ll_to_d(name) +# else +# define _MATH_ALIAS_l_to_f(name) +# define _MATH_ALIAS_l_l_to_f(name) +# define _MATH_ALIAS_l_L_to_f(name) +# define _MATH_ALIAS_l_s_to_f(name) +# define _MATH_ALIAS_l_ll_to_f(name) +# define _MATH_ALIAS_l_lL_to_f(name) +# define _MATH_ALIAS_l_lll_to_f(name) +# define _MATH_ALIAS_l_lI_to_f(name) +# define _MATH_ALIAS_l_llI_to_f(name) +# define _MATH_ALIAS_l_il_to_f(name) +# define _MATH_ALIAS_l_li_to_f(name) +# define _MATH_ALIAS_l_lj_to_f(name) +# define _MATH_ALIAS_i_l_to_f(name) +# define _MATH_ALIAS_j_l_to_f(name) +# define _MATH_ALIAS_k_l_to_f(name) +# define _MATH_ALIAS_i_ll_to_f(name) +# define _MATH_ALIAS_v_lLL_to_f(name) +# ifdef __strong_reference +# define _MATH_ALIAS_l_to_d(name) __strong_reference_dup(_D_NAME(name), _LD_NAME(name)); +# define _MATH_ALIAS_l_l_to_d(name) _MATH_ALIAS_l_to_d(name) +# define _MATH_ALIAS_l_L_to_d(name) _MATH_ALIAS_l_to_d(name) +# define _MATH_ALIAS_l_s_to_d(name) _MATH_ALIAS_l_to_d(name) +# define _MATH_ALIAS_l_ll_to_d(name) _MATH_ALIAS_l_to_d(name) +# define _MATH_ALIAS_l_lL_to_d(name) _MATH_ALIAS_l_to_d(name) +# define _MATH_ALIAS_l_lll_to_d(name) _MATH_ALIAS_l_to_d(name) +# define _MATH_ALIAS_l_lI_to_d(name) _MATH_ALIAS_l_to_d(name) +# define _MATH_ALIAS_l_llI_to_d(name) _MATH_ALIAS_l_to_d(name) +# define _MATH_ALIAS_l_il_to_d(name) _MATH_ALIAS_l_to_d(name) +# define _MATH_ALIAS_l_li_to_d(name) _MATH_ALIAS_l_to_d(name) +# define _MATH_ALIAS_l_lj_to_d(name) _MATH_ALIAS_l_to_d(name) +# define _MATH_ALIAS_i_l_to_d(name) _MATH_ALIAS_l_to_d(name) +# define _MATH_ALIAS_j_l_to_d(name) _MATH_ALIAS_l_to_d(name) +# define _MATH_ALIAS_k_l_to_d(name) _MATH_ALIAS_l_to_d(name) +# define _MATH_ALIAS_i_ll_to_d(name) _MATH_ALIAS_l_to_d(name) +# define _MATH_ALIAS_v_lLL_to_d(name) _MATH_ALIAS_l_to_d(name) +# else +# define _MATH_ALIAS_l_to_d(name) long double _LD_NAME(name)(void) { return (long double) _D_NAME(name)(); } +# define _MATH_ALIAS_l_l_to_d(name) long double _LD_NAME(name)(long double x) { return (long double) _D_NAME(name)((double) x); } +# define _MATH_ALIAS_l_L_to_d(name) long double _LD_NAME(name)(const long double *x) { return (long double) _D_NAME(name)((double *) x); } +# define _MATH_ALIAS_l_s_to_d(name) long double _LD_NAME(name)(const char *x) { return (long double) _D_NAME(name)(x); } +# define _MATH_ALIAS_l_ll_to_d(name) long double _LD_NAME(name)(long double x, long double y) { return (long double) _D_NAME(name)((double) x, (double) y); } +# define _MATH_ALIAS_l_lL_to_d(name) long double _LD_NAME(name)(long double x, long double *y) { return (long double) _D_NAME(name)((double) x, (double *) y); } +# define _MATH_ALIAS_l_lll_to_d(name) long double _LD_NAME(name)(long double x, long double y, long double z) { return (long double) _D_NAME(name)((double) x, (double) y, (double) z); } +# define _MATH_ALIAS_l_lI_to_d(name) long double _LD_NAME(name)(long double x, int *y) { return (long double) _D_NAME(name)((double) x, y); } +# define _MATH_ALIAS_l_llI_to_d(name) long double _LD_NAME(name)(long double x, long double y, int *z) { return (long double) _D_NAME(name)((double) x, (double) y, z); } +# define _MATH_ALIAS_l_il_to_d(name) long double _LD_NAME(name)(int n, long double x) { return (long double) _D_NAME(name)(n, (double) x); } +# define _MATH_ALIAS_l_li_to_d(name) long double _LD_NAME(name)(long double x, int n) { return (long double) _D_NAME(name)((double) x, n); } +# define _MATH_ALIAS_l_lj_to_d(name) long double _LD_NAME(name)(long double x, long j) { return (long double) _D_NAME(name)((double) x, j); } +# define _MATH_ALIAS_i_l_to_d(name) int _LD_NAME(name)(long double x) { return _D_NAME(name)((double) x); } +# define _MATH_ALIAS_j_l_to_d(name) long _LD_NAME(name)(long double x) { return _D_NAME(name)((double) x); } +# define _MATH_ALIAS_k_l_to_d(name) long long _LD_NAME(name)(long double x) { return _D_NAME(name)((double) x); } +# define _MATH_ALIAS_i_ll_to_d(name) int _LD_NAME(name)(long double x, long double y) { return _D_NAME(name)((double) x, (double) y); } +# define _MATH_ALIAS_v_lLL_to_d(name) void _LD_NAME(name)(long double x, long double *y, long double *z) { return _D_NAME(name)((double) x, (double *) y, (double *) z); } +# endif +# endif +#else +# if __SIZEOF_LONG_DOUBLE__ == 8 && !defined(_LDBL_EQ_DBL) +# define _NAME_64(x) _LD_NAME_REG(x) +# define _NAME_64_SPECIAL(d,l) l +# define FORCE_FLOAT64 FORCE_LONG_DOUBLE +# define CONST_FORCE_FLOAT64 CONST_FORCE_LONG_DOUBLE +# define pick_float64_except(a,b) pick_long_double_except(a,b) +# define _NEED_FLOAT64 +# ifdef __LONG_DOUBLE_NOEXCEPT +# define __FLOAT64_NOEXCEPT +# endif +# ifdef __LONG_DOUBLE_NOROUND +# define __FLOAT64_NOROUND +# endif +# define __F_64(x) x ## L +# define _F_64(x) __F_64(x) +# define _FLOAT64_MIN LDBL_MIN +# define _FLOAT64_MAX LDBL_MAX +# define _FLOAT64_MAX_EXP LDBL_MAX_EXP +# define _FLOAT64_DENORM_MIN __LDBL_DENORM_MIN__ +# define _FLOAT64_MANT_DIG __LDBL_MANT_DIG__ +# define force_eval_float64 force_eval_long_double +# define opt_barrier_float64 opt_barrier_long_double +# endif +# define _MATH_ALIAS_l_to_f(name) +# define _MATH_ALIAS_l_l_to_f(name) +# define _MATH_ALIAS_l_L_to_f(name) +# define _MATH_ALIAS_l_s_to_f(name) +# define _MATH_ALIAS_l_ll_to_f(name) +# define _MATH_ALIAS_l_lL_to_f(name) +# define _MATH_ALIAS_l_lll_to_f(name) +# define _MATH_ALIAS_l_lI_to_f(name) +# define _MATH_ALIAS_l_llI_to_f(name) +# define _MATH_ALIAS_l_il_to_f(name) +# define _MATH_ALIAS_l_li_to_f(name) +# define _MATH_ALIAS_l_lj_to_f(name) +# define _MATH_ALIAS_i_l_to_f(name) +# define _MATH_ALIAS_j_l_to_f(name) +# define _MATH_ALIAS_k_l_to_f(name) +# define _MATH_ALIAS_i_ll_to_f(name) +# define _MATH_ALIAS_v_lLL_to_f(name) +# define _MATH_ALIAS_l_to_d(name) +# define _MATH_ALIAS_l_l_to_d(name) +# define _MATH_ALIAS_l_L_to_d(name) +# define _MATH_ALIAS_l_s_to_d(name) +# define _MATH_ALIAS_l_ll_to_d(name) +# define _MATH_ALIAS_l_lL_to_d(name) +# define _MATH_ALIAS_l_lll_to_d(name) +# define _MATH_ALIAS_l_lI_to_d(name) +# define _MATH_ALIAS_l_llI_to_d(name) +# define _MATH_ALIAS_l_il_to_d(name) +# define _MATH_ALIAS_l_li_to_d(name) +# define _MATH_ALIAS_l_lj_to_d(name) +# define _MATH_ALIAS_i_l_to_d(name) +# define _MATH_ALIAS_j_l_to_d(name) +# define _MATH_ALIAS_k_l_to_d(name) +# define _MATH_ALIAS_i_ll_to_d(name) +# define _MATH_ALIAS_v_lLL_to_d(name) +#endif + +#ifndef _NAME_64 +# define _NAME_64(x) _D_NAME_REG(x) +# define _NAME_64_SPECIAL(d,l) d +# define _F_64(x) x +# define _FLOAT64_MIN DBL_MIN +# define _FLOAT64_MAX DBL_MAX +# define _FLOAT64_MAX_EXP DBL_MAX_EXP +# define _FLOAT64_DENORM_MIN __DBL_DENORM_MIN__ +# define _FLOAT64_MANT_DIG __DBL_MANT_DIG__ +# define FORCE_FLOAT64 FORCE_DOUBLE +# define CONST_FORCE_FLOAT64 CONST_FORCE_DOUBLE +# define pick_float64_except(a,b) pick_double_except(a,b) +# define force_eval_float64 force_eval_double +# define opt_barrier_float64 opt_barrier_double +#endif + +#if __SIZEOF_LONG_DOUBLE__ > 8 +# define _NEED_FLOAT_HUGE +/* Guess long double layout based on compiler defines */ + +#if __LDBL_MANT_DIG__ == 64 && 16383 <= __LDBL_MAX_EXP__ && __LDBL_MAX_EXP__ <= 16384 +#define _INTEL80_FLOAT +#ifdef __LP64__ +#define _INTEL80_FLOAT_PAD +#endif +#endif + +#if __LDBL_MANT_DIG__ == 113 && 16383 <= __LDBL_MAX_EXP__ && __LDBL_MAX_EXP__ <= 16384 +#define _IEEE128_FLOAT +#endif + +#if __LDBL_MANT_DIG__ == 106 && __LDBL_MAX_EXP__ == 1024 +#define _DOUBLE_DOUBLE_FLOAT +#endif + +#ifndef __FLOAT_WORD_ORDER__ +#define __FLOAT_WORD_ORDER__ __BYTE_ORDER__ +#endif + +#endif + +#define _MATH_ALIAS_f(name) \ + _MATH_ALIAS_d_to_f(name) \ + _MATH_ALIAS_l_to_f(name) + +#define _MATH_ALIAS_f_f(name) \ + _MATH_ALIAS_d_d_to_f(name) \ + _MATH_ALIAS_l_l_to_f(name) + +#define _MATH_ALIAS_f_F(name) \ + _MATH_ALIAS_d_D_to_f(name) \ + _MATH_ALIAS_l_L_to_f(name) + +#define _MATH_ALIAS_f_s(name) \ + _MATH_ALIAS_d_s_to_f(name) \ + _MATH_ALIAS_l_s_to_f(name) + +#define _MATH_ALIAS_f_ff(name) \ + _MATH_ALIAS_d_dd_to_f(name) \ + _MATH_ALIAS_l_ll_to_f(name) + +#define _MATH_ALIAS_f_fl(name) \ + _MATH_ALIAS_d_dl_to_f(name) \ + _MATH_ALIAS_l_ll_to_f(name) + +#define _MATH_ALIAS_f_fF(name) \ + _MATH_ALIAS_d_dD_to_f(name) \ + _MATH_ALIAS_l_lL_to_f(name) + +#define _MATH_ALIAS_f_fff(name) \ + _MATH_ALIAS_d_ddd_to_f(name) \ + _MATH_ALIAS_l_lll_to_f(name) + +#define _MATH_ALIAS_f_fI(name) \ + _MATH_ALIAS_d_dI_to_f(name) \ + _MATH_ALIAS_l_lI_to_f(name) + +#define _MATH_ALIAS_f_ffI(name) \ + _MATH_ALIAS_d_ddI_to_f(name) \ + _MATH_ALIAS_l_llI_to_f(name) + +#define _MATH_ALIAS_f_if(name) \ + _MATH_ALIAS_d_id_to_f(name) \ + _MATH_ALIAS_l_il_to_f(name) + +#define _MATH_ALIAS_f_fi(name) \ + _MATH_ALIAS_d_di_to_f(name) \ + _MATH_ALIAS_l_li_to_f(name) + +#define _MATH_ALIAS_f_fj(name) \ + _MATH_ALIAS_d_dj_to_f(name) \ + _MATH_ALIAS_l_lj_to_f(name) + +#define _MATH_ALIAS_i_f(name) \ + _MATH_ALIAS_i_d_to_f(name) \ + _MATH_ALIAS_i_l_to_f(name) + +#define _MATH_ALIAS_j_f(name) \ + _MATH_ALIAS_j_d_to_f(name) \ + _MATH_ALIAS_j_l_to_f(name) + +#define _MATH_ALIAS_k_f(name) \ + _MATH_ALIAS_k_d_to_f(name) \ + _MATH_ALIAS_k_l_to_f(name) + +#define _MATH_ALIAS_i_ff(name) \ + _MATH_ALIAS_i_dd_to_f(name) \ + _MATH_ALIAS_i_ll_to_f(name) + +#define _MATH_ALIAS_v_fFF(name) \ + _MATH_ALIAS_v_dDD_to_f(name) \ + _MATH_ALIAS_v_lLL_to_f(name) + +#define _MATH_ALIAS_d(name) \ + _MATH_ALIAS_l_to_d(name) + +#define _MATH_ALIAS_d_d(name) \ + _MATH_ALIAS_l_l_to_d(name) + +#define _MATH_ALIAS_d_D(name) \ + _MATH_ALIAS_l_L_to_d(name) + +#define _MATH_ALIAS_d_s(name) \ + _MATH_ALIAS_l_s_to_d(name) + +#define _MATH_ALIAS_d_dd(name) \ + _MATH_ALIAS_l_ll_to_d(name) + +#define _MATH_ALIAS_d_dl(name) \ + _MATH_ALIAS_l_ll_to_d(name) + +#define _MATH_ALIAS_d_dD(name) \ + _MATH_ALIAS_l_lL_to_d(name) + +#define _MATH_ALIAS_d_ddd(name) \ + _MATH_ALIAS_l_lll_to_d(name) + +#define _MATH_ALIAS_d_dI(name) \ + _MATH_ALIAS_l_lI_to_d(name) + +#define _MATH_ALIAS_d_ddI(name) \ + _MATH_ALIAS_l_llI_to_d(name) + +#define _MATH_ALIAS_d_id(name) \ + _MATH_ALIAS_l_il_to_d(name) + +#define _MATH_ALIAS_d_di(name) \ + _MATH_ALIAS_l_li_to_d(name) + +#define _MATH_ALIAS_d_dj(name) \ + _MATH_ALIAS_l_lj_to_d(name) + +#define _MATH_ALIAS_i_d(name) \ + _MATH_ALIAS_i_l_to_d(name) + +#define _MATH_ALIAS_j_d(name) \ + _MATH_ALIAS_j_l_to_d(name) + +#define _MATH_ALIAS_k_d(name) \ + _MATH_ALIAS_k_l_to_d(name) + +#define _MATH_ALIAS_i_dd(name) \ + _MATH_ALIAS_i_ll_to_d(name) + +#define _MATH_ALIAS_v_dDD(name) \ + _MATH_ALIAS_v_lLL_to_d(name) + +/* Evaluate an expression as the specified type, normally a type + cast should be enough, but compilers implement non-standard + excess-precision handling, so when FLT_EVAL_METHOD != 0 then + these functions may need to be customized. */ +static __always_inline float +eval_as_float (float x) +{ + return x; +} +static __always_inline double +eval_as_double (double x) +{ + return x; +} + +/* gcc emitting PE/COFF doesn't support visibility */ +#if defined (__GNUC__) && !defined (__CYGWIN__) +# define HIDDEN __attribute__ ((__visibility__ ("hidden"))) +#else +# define HIDDEN +#endif + +/* Error handling tail calls for special cases, with a sign argument. + The sign of the return value is set if the argument is non-zero. */ + +/* The result overflows. */ +HIDDEN float __math_oflowf (uint32_t); +/* The result underflows to 0 in nearest rounding mode. */ +HIDDEN float __math_uflowf (uint32_t); +/* The result underflows to 0 in some directed rounding mode only. */ +HIDDEN float __math_may_uflowf (uint32_t); +/* Division by zero. */ +HIDDEN float __math_divzerof (uint32_t); + +/* Invalid input unless it is a quiet NaN. */ +HIDDEN float __math_invalidf (float); +/* set invalid exception */ +#if defined(FE_INVALID) && !defined(__FLOAT_NOEXCEPT) +HIDDEN void __math_set_invalidf(void); +#else +#define __math_set_invalidf() ((void) 0) +#endif + +/* Check if the result overflowed to infinity. */ +HIDDEN float __math_check_oflowf (float); + +static inline float +check_oflowf (float x) +{ + return WANT_ERRNO ? __math_check_oflowf (x) : x; +} + +/* Check if the result underflowed to 0. */ +HIDDEN float __math_check_uflowf (float); + +static inline float +check_uflowf (float x) +{ + return WANT_ERRNO ? __math_check_uflowf (x) : x; +} + +#if defined(FE_INEXACT) && !defined(__FLOAT_NOEXCEPT) +float __math_inexactf(float val); +void __math_set_inexactf(void); +#else +#define __math_inexactf(val) (val) +#define __math_set_inexactf() ((void) 0) +#endif + +#if WANT_ERRNO +HIDDEN float +__math_with_errnof (float y, int e); +#else +#define __math_with_errnof(x, e) (x) +#endif + +/* Check if the result is a denorm. */ +#if (defined(FE_UNDERFLOW) && !defined(__FLOAT_NOEXCEPT)) || WANT_ERRNO +float __math_denormf (float x); +#else +#define __math_denormf(x) (x) +#endif + +#ifdef _NEED_FLOAT64 +/* The result overflows. */ +HIDDEN __float64 __math_oflow (uint32_t); +/* The result underflows to 0 in nearest rounding mode. */ +HIDDEN __float64 __math_uflow (uint32_t); +/* The result underflows to 0 in some directed rounding mode only. */ +HIDDEN __float64 __math_may_uflow (uint32_t); +/* Division by zero. */ +HIDDEN __float64 __math_divzero (uint32_t); + +/* Error handling using input checking. */ + +/* Invalid input unless it is a quiet NaN. */ +HIDDEN __float64 __math_invalid (__float64); +/* set invalid exception */ +#if defined(FE_INVALID) && !defined(__DOUBLE_NOEXCEPT) +HIDDEN void __math_set_invalid(void); +#else +#define __math_set_invalid() ((void) 0) +#endif + +/* Check if the result overflowed to infinity. */ +HIDDEN __float64 __math_check_oflow (__float64); + +static inline __float64 +check_oflow (__float64 x) +{ + return WANT_ERRNO ? __math_check_oflow (x) : x; +} + +/* Check if the result underflowed to 0. */ +HIDDEN __float64 __math_check_uflow (__float64); + +static inline __float64 +check_uflow (__float64 x) +{ + return WANT_ERRNO ? __math_check_uflow (x) : x; +} + +/* Set inexact exception */ +#if defined(FE_INEXACT) && !defined(__FLOAT64_NOEXCEPT) +__float64 __math_inexact64(__float64 x); +void __math_set_inexact64(void); +#else +#define __math_inexact64(val) (val) +#define __math_set_inexact64() ((void) 0) +#endif + +#if WANT_ERRNO +HIDDEN __float64 +__math_with_errno (__float64 y, int e); +#else +#define __math_with_errno(x, e) (x) +#endif + +#if (defined(FE_UNDERFLOW) && !defined(__FLOAT64_NOEXCEPT)) || WANT_ERRNO +__float64 __math_denorm (__float64 x); +#else +#define __math_denorm(x) (x) +#endif + +#endif /* _NEED_FLOAT64 */ + +#ifdef __HAVE_LONG_DOUBLE +HIDDEN long double __math_oflowl (uint32_t); +/* The result underflows to 0 in nearest rounding mode. */ +HIDDEN long double __math_uflowl (uint32_t); +/* The result underflows to 0 in some directed rounding mode only. */ +HIDDEN long double __math_may_uflowl (uint32_t); +/* Division by zero. */ +HIDDEN long double __math_divzerol (uint32_t); +/* Invalid input unless it is a quiet NaN. */ +HIDDEN long double __math_invalidl (long double); +/* set invalid exception */ +#if defined(FE_INVALID) && !defined(__LONG_DOUBLE_NOEXCEPT) +HIDDEN void __math_set_invalidl(void); +#else +#define __math_set_invalidl() ((void) 0) +#endif + +/* Check if the result overflowed to infinity. */ +HIDDEN long double __math_check_oflowl(long double); + +static inline long double +check_oflowl (long double x) +{ + return WANT_ERRNO ? __math_check_oflowl (x) : x; +} + +/* Check if the result underflowed to 0. */ +HIDDEN long double __math_check_uflowl(long double); + +static inline long double +check_uflowl (long double x) +{ + return WANT_ERRNO ? __math_check_uflowl (x) : x; +} + +#if defined(FE_INEXACT) && !defined(__LONG_DOUBLE_NOEXCEPT) +long double __math_inexactl(long double val); +void __math_set_inexactl(void); +#else +#define __math_inexactl(val) (val) +#define __math_set_inexactl() ((void) 0) +#endif + +#if WANT_ERRNO +HIDDEN long double +__math_with_errnol (long double y, int e); +#else +#define __math_with_errnol(x, e) (x) +#endif + +#if (defined(FE_UNDERFLOW) && !defined(__LONG_DOUBLE_NOEXCEPT)) || WANT_ERRNO +long double __math_denorml(long double x); +#else +#define __math_denorml(x) (x) +#endif + +#endif /* __HAVE_LONG_DOUBLE */ + +/* Shared between expf, exp2f and powf. */ +#define EXP2F_TABLE_BITS 5 +#define EXP2F_POLY_ORDER 3 +extern const struct exp2f_data +{ + uint64_t tab[1 << EXP2F_TABLE_BITS]; + double shift_scaled; + double poly[EXP2F_POLY_ORDER]; + double shift; + double invln2_scaled; + double poly_scaled[EXP2F_POLY_ORDER]; +} __exp2f_data HIDDEN; + +#define LOGF_TABLE_BITS 4 +#define LOGF_POLY_ORDER 4 +extern const struct logf_data +{ + struct + { + double invc, logc; + } tab[1 << LOGF_TABLE_BITS]; + double ln2; + double poly[LOGF_POLY_ORDER - 1]; /* First order coefficient is 1. */ +} __logf_data HIDDEN; + +#define LOG2F_TABLE_BITS 4 +#define LOG2F_POLY_ORDER 4 +extern const struct log2f_data +{ + struct + { + double invc, logc; + } tab[1 << LOG2F_TABLE_BITS]; + double poly[LOG2F_POLY_ORDER]; +} __log2f_data HIDDEN; + +#define POWF_LOG2_TABLE_BITS 4 +#define POWF_LOG2_POLY_ORDER 5 +#if TOINT_INTRINSICS +# define POWF_SCALE_BITS EXP2F_TABLE_BITS +#else +# define POWF_SCALE_BITS 0 +#endif +#define POWF_SCALE ((double) (1 << POWF_SCALE_BITS)) +extern const struct powf_log2_data +{ + struct + { + double invc, logc; + } tab[1 << POWF_LOG2_TABLE_BITS]; + double poly[POWF_LOG2_POLY_ORDER]; +} __powf_log2_data HIDDEN; + +#define EXP_TABLE_BITS 7 +#define EXP_POLY_ORDER 5 +/* Use polynomial that is optimized for a wider input range. This may be + needed for good precision in non-nearest rounding and !TOINT_INTRINSICS. */ +#define EXP_POLY_WIDE 0 +/* Use close to nearest rounding toint when !TOINT_INTRINSICS. This may be + needed for good precision in non-nearest rouning and !EXP_POLY_WIDE. */ +#define EXP_USE_TOINT_NARROW 0 +#define EXP2_POLY_ORDER 5 +#define EXP2_POLY_WIDE 0 +extern const struct exp_data +{ + double invln2N; + double shift; + double negln2hiN; + double negln2loN; + double poly[4]; /* Last four coefficients. */ + double exp2_shift; + double exp2_poly[EXP2_POLY_ORDER]; + uint64_t tab[2*(1 << EXP_TABLE_BITS)]; +} __exp_data HIDDEN; + +#define LOG_TABLE_BITS 7 +#define LOG_POLY_ORDER 6 +#define LOG_POLY1_ORDER 12 +extern const struct log_data +{ + double ln2hi; + double ln2lo; + double poly[LOG_POLY_ORDER - 1]; /* First coefficient is 1. */ + double poly1[LOG_POLY1_ORDER - 1]; + struct {double invc, logc;} tab[1 << LOG_TABLE_BITS]; +#if !__HAVE_FAST_FMA + struct {double chi, clo;} tab2[1 << LOG_TABLE_BITS]; +#endif +} __log_data HIDDEN; + +#define LOG2_TABLE_BITS 6 +#define LOG2_POLY_ORDER 7 +#define LOG2_POLY1_ORDER 11 +extern const struct log2_data +{ + double invln2hi; + double invln2lo; + double poly[LOG2_POLY_ORDER - 1]; + double poly1[LOG2_POLY1_ORDER - 1]; + struct {double invc, logc;} tab[1 << LOG2_TABLE_BITS]; +#if !__HAVE_FAST_FMA + struct {double chi, clo;} tab2[1 << LOG2_TABLE_BITS]; +#endif +} __log2_data HIDDEN; + +#define POW_LOG_TABLE_BITS 7 +#define POW_LOG_POLY_ORDER 8 +extern const struct pow_log_data +{ + double ln2hi; + double ln2lo; + double poly[POW_LOG_POLY_ORDER - 1]; /* First coefficient is 1. */ + /* Note: the pad field is unused, but allows slightly faster indexing. */ + struct {double invc, pad, logc, logctail;} tab[1 << POW_LOG_TABLE_BITS]; +} __pow_log_data HIDDEN; + + +HIDDEN float +__math_lgammaf_r (float y, int *signgamp, int *divzero); + +#ifdef _NEED_FLOAT64 +HIDDEN __float64 +__math_lgamma_r (__float64 y, int *signgamp, int *divzero); +#endif + +#ifdef __weak_reference +extern int __signgam; +#else +#define __signgam signgam +#endif + +/* Make aliases for 64-bit functions to the correct name */ +#define acos64 _NAME_64(acos) +#define acosh64 _NAME_64(acosh) +#define asin64 _NAME_64(asin) +#define asinh64 _NAME_64(asinh) +#define atan64 _NAME_64(atan) +#define atanh64 _NAME_64(atanh) +#define atan264 _NAME_64(atan2) +#define cbrt64 _NAME_64(cbrt) +#define ceil64 _NAME_64(ceil) +#define copysign64 _NAME_64(copysign) +#define cos64 _NAME_64(cos) +#define _cos64 _NAME_64(_cos) +#define cosh64 _NAME_64(cosh) +#define drem64 _NAME_64(drem) +#define erf64 _NAME_64(erf) +#define erfc64 _NAME_64(erfc) +#define exp64 _NAME_64(exp) +#define exp264 _NAME_64(exp2) +#define exp1064 _NAME_64(exp10) +#define expm164 _NAME_64(expm1) +#define fabs64 _NAME_64(fabs) +#define fdim64 _NAME_64(fdim) +#define finite64 _NAME_64(finite) +#define __finite64 _NAME_64(__finite) +#define floor64 _NAME_64(floor) +#define fma64 _NAME_64(fma) +#define fmax64 _NAME_64(fmax) +#define fmin64 _NAME_64(fmin) +#define fmod64 _NAME_64(fmod) +#define __fpclassify64 _NAME_64_SPECIAL(__fpclassifyd, __fpclassifyl) +#define frexp64 _NAME_64(frexp) +#define gamma64 _NAME_64(gamma) +#define getpayload64 _NAME_64(getpayload) +#define hypot64 _NAME_64(hypot) +#define ilogb64 _NAME_64(ilogb) +#define infinity64 _NAME_64(infinity) +#define __iseqsig64 _NAME_64_SPECIAL(__iseqsigd, __iseqsigl) +#define isfinite64 _NAME_64(isfinite) +#define isinf64 _NAME_64(isinf) +#define __isinf64 _NAME_64_SPECIAL(__isinfd, __isinfl) +#define isnan64 _NAME_64(isnan) +#define __isnan64 _NAME_64_SPECIAL(__isnand, __isnanl) +#define __issignaling64 _NAME_64(__issignaling) +#define ldexp64 _NAME_64(ldexp) +#define j064 _NAME_64(j0) +#define y064 _NAME_64(y0) +#define j164 _NAME_64(j1) +#define y164 _NAME_64(y1) +#define jn64 _NAME_64(jn) +#define yn64 _NAME_64(yn) +#define lgamma64 _NAME_64(lgamma) +#define lgamma64_r _NAME_64_SPECIAL(lgamma_r, lgammal_r) +#define llrint64 _NAME_64(llrint) +#define llround64 _NAME_64(llround) +#define log64 _NAME_64(log) +#define log1064 _NAME_64(log10) +#define log1p64 _NAME_64(log1p) +#define log264 _NAME_64(log2) +#define logb64 _NAME_64(logb) +#define lrint64 _NAME_64(lrint) +#define lround64 _NAME_64(lround) +#define modf64 _NAME_64(modf) +#define nan64 _NAME_64(nan) +#define nearbyint64 _NAME_64(nearbyint) +#define nextafter64 _NAME_64(nextafter) +#define nexttoward64 _NAME_64(nexttoward) +#define pow64 _NAME_64(pow) +#define _pow64 _NAME_64(_pow) +#define pow1064 _NAME_64(pow10) +#define remainder64 _NAME_64(remainder) +#define remquo64 _NAME_64(remquo) +#define rint64 _NAME_64(rint) +#define round64 _NAME_64(round) +#define scalb64 _NAME_64(scalb) +#define scalbn64 _NAME_64(scalbn) +#define scalbln64 _NAME_64(scalbln) +#define __signbit64 _NAME_64_SPECIAL(__signbitd, __signbitl) +#define significand64 _NAME_64(significand) +#define sin64 _NAME_64(sin) +#define _sin64 _NAME_64(_sin) +#define sincos64 _NAME_64(sincos) +#define sinh64 _NAME_64(sinh) +#define sqrt64 _NAME_64(sqrt) +#define tan64 _NAME_64(tan) +#define tanh64 _NAME_64(tanh) +#define tgamma64 _NAME_64(tgamma) +#define trunc64 _NAME_64(trunc) + +#ifdef __strong_reference +float _powf(float, float); +float _sinf(float); +float _cosf(float); + +#ifdef _NEED_FLOAT64 +__float64 _pow64(__float64, __float64); +__float64 _sin64(__float64); +__float64 _cos64(__float64); +#endif + +#ifdef __HAVE_LONG_DOUBLE_MATH +long double _powl(long double, long double); +long double _sinl(long double); +long double _cosl(long double); +#endif + +#else + +#define _powf(x,y) powf(x,y) +#define _sinf(x) sinf(x) +#define _cosf(x) cosf(x) + +#undef _pow64 +#undef _sin64 +#undef _cos64 +#define _pow64(x,y) pow64(x,y) +#define _sin64(x) sin64(x) +#define _cos64(x) cos64(x) + +#ifdef __HAVE_LONG_DOUBLE_MATH +#define _powl(x,y) powl(x,y) +#define _sinl(x) sinl(x) +#define _cosl(x) cosl(x) +#endif /* __HAVE_LONG_DOUBLE_MATH */ + +#endif /* __strong_reference */ + +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/math_denorm.c b/src/audio/ffmpeg_dec/libm/common/math_denorm.c new file mode 100644 index 000000000000..b18556f9f641 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/math_denorm.c @@ -0,0 +1,48 @@ +/* Double-precision math error handling. + Copyright (c) 2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "math_config.h" + +#ifdef _NEED_FLOAT64 + +#ifndef __math_denorm + +#ifndef __FLOAT64_NOEXCEPT +static CONST_FORCE_FLOAT64 VAL = _FLOAT64_MIN; +#endif + +HIDDEN __float64 +__math_denorm (__float64 x) +{ + (void) pick_float64_except(force_eval_float64(VAL*VAL), 0.0); + return __math_with_errno(x, ERANGE); +} + +#endif + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/math_denormf.c b/src/audio/ffmpeg_dec/libm/common/math_denormf.c new file mode 100644 index 000000000000..91216a7d8539 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/math_denormf.c @@ -0,0 +1,44 @@ +/* Single-precision math error handling. + Copyright (c) 2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "math_config.h" + +#ifndef __math_denormf + +#ifndef __FLOAT_NOEXCEPT +static CONST_FORCE_FLOAT VAL = __FLT_MIN__; +#endif + +HIDDEN float +__math_denormf (float x) +{ + (void) pick_float_except(force_eval_float(VAL * VAL), 0.0f); + return __math_with_errnof(x, ERANGE); +} + +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/math_denorml.c b/src/audio/ffmpeg_dec/libm/common/math_denorml.c new file mode 100644 index 000000000000..e8f535ec4706 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/math_denorml.c @@ -0,0 +1,48 @@ +/* Long double math error handling. + Copyright (c) 2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "math_config.h" + +#ifdef _NEED_FLOAT_HUGE + +#ifndef __math_denorml + +#ifndef __LONG_DOUBLE_NOEXCEPT +static CONST_FORCE_LONG_DOUBLE VAL = __LDBL_MIN__; +#endif + +HIDDEN long double +__math_denorml (long double x) +{ + (void) pick_long_double_except(force_eval_long_double(VAL * VAL), 0.0L); + return __math_with_errnol(x, ERANGE); +} + +#endif + +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/math_err_check_oflow.c b/src/audio/ffmpeg_dec/libm/common/math_err_check_oflow.c new file mode 100644 index 000000000000..988970e5f018 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/math_err_check_oflow.c @@ -0,0 +1,41 @@ +/* Double-precision math error handling. + Copyright (c) 2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "math_config.h" + +#ifdef _NEED_FLOAT64 + +/* Check result and set errno if necessary. */ + +HIDDEN __float64 +__math_check_oflow (__float64 y) +{ + return isinf (y) ? __math_with_errno (y, ERANGE) : y; +} + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/math_err_check_uflow.c b/src/audio/ffmpeg_dec/libm/common/math_err_check_uflow.c new file mode 100644 index 000000000000..ad3ee9b8a8e9 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/math_err_check_uflow.c @@ -0,0 +1,41 @@ +/* Double-precision math error handling. + Copyright (c) 2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "math_config.h" + +#ifdef _NEED_FLOAT64 + +/* Check result and set errno if necessary. */ + +HIDDEN __float64 +__math_check_uflow (__float64 y) +{ + return y == 0.0 ? __math_with_errno (y, ERANGE) : y; +} + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/math_err_divzero.c b/src/audio/ffmpeg_dec/libm/common/math_err_divzero.c new file mode 100644 index 000000000000..21ace7290dd1 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/math_err_divzero.c @@ -0,0 +1,42 @@ +/* Double-precision math error handling. + Copyright (c) 2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "math_config.h" + +#ifdef _NEED_FLOAT64 + +static CONST_FORCE_FLOAT64 VAL = pick_float64_except(_F_64(0.0), (__float64) INFINITY); + +HIDDEN __float64 +__math_divzero (uint32_t sign) +{ + __float64 y = pick_float64_except((sign ? _F_64(-1.0) : _F_64(1.0)) / VAL, sign ? (-VAL) : VAL); + return __math_with_errno (y, ERANGE); +} + +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/math_err_invalid.c b/src/audio/ffmpeg_dec/libm/common/math_err_invalid.c new file mode 100644 index 000000000000..79013e100255 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/math_err_invalid.c @@ -0,0 +1,52 @@ +/* Double-precision math error handling. + Copyright (c) 2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "math_config.h" + +#ifdef _NEED_FLOAT64 + +static CONST_FORCE_FLOAT64 VAL = pick_float64_except(_F_64(0.0), (__float64) NAN); + +HIDDEN __float64 +__math_invalid (__float64 x) +{ + if (isnan(x)) + return pick_float64_except(x + x, VAL); + x = pick_float64_except(VAL / VAL, VAL); + return __math_with_errno (x, EDOM); +} + +#ifndef __math_set_invalid +HIDDEN void +__math_set_invalid(void) +{ + force_eval_float64(pick_float64_except(VAL / VAL, VAL)); +} +#endif + +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/math_err_may_uflow.c b/src/audio/ffmpeg_dec/libm/common/math_err_may_uflow.c new file mode 100644 index 000000000000..cb73718a8226 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/math_err_may_uflow.c @@ -0,0 +1,43 @@ +/* Double-precision math error handling. + Copyright (c) 2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "math_config.h" + +#if WANT_ERRNO_UFLOW && defined(_NEED_FLOAT64) + +static CONST_FORCE_FLOAT64 VAL = pick_float64_except(_F_64(0x1.8p-538), _F_64(0.0)); + +/* Underflows to zero in some non-nearest rounding mode, setting errno + is valid even if the result is non-zero, but in the subnormal range. */ +HIDDEN __float64 +__math_may_uflow (uint32_t sign) +{ + __float64 y = pick_float64_except((sign ? -VAL : VAL) * VAL, sign ? -VAL : VAL); + return __math_with_errno (y, ERANGE); +} +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/math_err_oflow.c b/src/audio/ffmpeg_dec/libm/common/math_err_oflow.c new file mode 100644 index 000000000000..d17cb0bd8ac2 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/math_err_oflow.c @@ -0,0 +1,42 @@ +/* Double-precision math error handling. + Copyright (c) 2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "math_config.h" + +#ifdef _NEED_FLOAT64 + +static CONST_FORCE_FLOAT64 VAL = pick_float64_except(_FLOAT64_MAX, (__float64) INFINITY); + +HIDDEN __float64 +__math_oflow (uint32_t sign) +{ + __float64 y = pick_float64_except((sign ? -VAL : VAL) * VAL, sign ? -VAL : VAL); + return __math_with_errno (y, ERANGE); +} + +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/math_err_uflow.c b/src/audio/ffmpeg_dec/libm/common/math_err_uflow.c new file mode 100644 index 000000000000..814351589b42 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/math_err_uflow.c @@ -0,0 +1,42 @@ +/* Double-precision math error handling. + Copyright (c) 2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "math_config.h" + +#ifdef _NEED_FLOAT64 + +static CONST_FORCE_FLOAT64 VAL = pick_float64_except(_FLOAT64_MIN, _F_64(0.0)); + +HIDDEN __float64 +__math_uflow (uint32_t sign) +{ + __float64 y = pick_float64_except((sign ? -VAL : VAL) * VAL, sign ? -VAL : VAL); + return __math_with_errno (y, ERANGE); +} + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/math_err_with_errno.c b/src/audio/ffmpeg_dec/libm/common/math_err_with_errno.c new file mode 100644 index 000000000000..dd09189d4bec --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/math_err_with_errno.c @@ -0,0 +1,45 @@ +/* Double-precision math error handling. + Copyright (c) 2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "math_config.h" + +#ifdef _NEED_FLOAT64 + +#if WANT_ERRNO +#include +/* __noinline reduces code size and avoids making math functions non-leaf + when the error handling is inlined. */ +__noinline HIDDEN __float64 +__math_with_errno (__float64 y, int e) +{ + errno = e; + return y; +} +#endif + +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/math_errf_check_oflowf.c b/src/audio/ffmpeg_dec/libm/common/math_errf_check_oflowf.c new file mode 100644 index 000000000000..ecc365ccb73a --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/math_errf_check_oflowf.c @@ -0,0 +1,37 @@ +/* Single-precision math error handling. + Copyright (c) 2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "math_config.h" + +/* Check result and set errno if necessary. */ + +HIDDEN float +__math_check_oflowf (float y) +{ + return isinf (y) ? __math_with_errnof (y, ERANGE) : y; +} diff --git a/src/audio/ffmpeg_dec/libm/common/math_errf_check_uflowf.c b/src/audio/ffmpeg_dec/libm/common/math_errf_check_uflowf.c new file mode 100644 index 000000000000..b7766ad41b5c --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/math_errf_check_uflowf.c @@ -0,0 +1,37 @@ +/* Single-precision math error handling. + Copyright (c) 2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "math_config.h" + +/* Check result and set errno if necessary. */ + +HIDDEN float +__math_check_uflowf (float y) +{ + return y == 0.0f ? __math_with_errnof (y, ERANGE) : y; +} diff --git a/src/audio/ffmpeg_dec/libm/common/math_errf_divzerof.c b/src/audio/ffmpeg_dec/libm/common/math_errf_divzerof.c new file mode 100644 index 000000000000..dbd2a073fb8b --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/math_errf_divzerof.c @@ -0,0 +1,39 @@ +/* Single-precision math error handling. + Copyright (c) 2017-2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "fdlibm.h" +#include "math_config.h" + +static CONST_FORCE_FLOAT VAL = pick_float_except(0.0f, (float) INFINITY); + +HIDDEN float +__math_divzerof (uint32_t sign) +{ + float y = pick_float_except((sign ? -1.0f : 1.0f) / VAL, (sign ? -(VAL) : VAL)); + return __math_with_errnof (y, ERANGE); +} diff --git a/src/audio/ffmpeg_dec/libm/common/math_errf_invalidf.c b/src/audio/ffmpeg_dec/libm/common/math_errf_invalidf.c new file mode 100644 index 000000000000..036cdd06f0ed --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/math_errf_invalidf.c @@ -0,0 +1,49 @@ +/* Single-precision math error handling. + Copyright (c) 2017-2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "fdlibm.h" +#include "math_config.h" + +static CONST_FORCE_FLOAT VAL = pick_float_except(0.0f, (float) NAN); + +HIDDEN float +__math_invalidf (float x) +{ + if (isnan(x)) + return pick_float_except(x + x, (float) NAN); + x = pick_float_except(VAL / VAL, VAL); + return __math_with_errnof (x, EDOM); +} + +#ifndef __math_set_invalidf +HIDDEN void +__math_set_invalidf(void) +{ + force_eval_float(pick_float_except(VAL / VAL, VAL)); +} +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/math_errf_may_uflowf.c b/src/audio/ffmpeg_dec/libm/common/math_errf_may_uflowf.c new file mode 100644 index 000000000000..188d57b9e7b5 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/math_errf_may_uflowf.c @@ -0,0 +1,45 @@ +/* Single-precision math error handling. + Copyright (c) 2017-2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "fdlibm.h" +#include "math_config.h" + +#if WANT_ERRNO_UFLOW + +static CONST_FORCE_FLOAT VAL = pick_float_except(0x1.4p-75f, 0.0f); + +/* Underflows to zero in some non-nearest rounding mode, setting errno + is valid even if the result is non-zero, but in the subnormal range. */ +HIDDEN float +__math_may_uflowf (uint32_t sign) +{ + float y = pick_float_except((sign ? -VAL : VAL) * VAL, sign ? -VAL : VAL); + return __math_with_errnof (y, ERANGE); +} + +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/math_errf_oflowf.c b/src/audio/ffmpeg_dec/libm/common/math_errf_oflowf.c new file mode 100644 index 000000000000..ca254483a7da --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/math_errf_oflowf.c @@ -0,0 +1,39 @@ +/* Single-precision math error handling. + Copyright (c) 2017-2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "fdlibm.h" +#include "math_config.h" + +static CONST_FORCE_FLOAT VAL = pick_float_except(FLT_MAX, (float) INFINITY); + +HIDDEN float +__math_oflowf (uint32_t sign) +{ + float y = pick_float_except((sign ? -VAL : VAL) * VAL, sign ? -VAL : VAL); + return __math_with_errnof (y, ERANGE); +} diff --git a/src/audio/ffmpeg_dec/libm/common/math_errf_uflowf.c b/src/audio/ffmpeg_dec/libm/common/math_errf_uflowf.c new file mode 100644 index 000000000000..0e9a2158f4db --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/math_errf_uflowf.c @@ -0,0 +1,39 @@ +/* Single-precision math error handling. + Copyright (c) 2017-2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "fdlibm.h" +#include "math_config.h" + +static CONST_FORCE_FLOAT VAL = pick_float_except(FLT_MIN, 0.0f); + +HIDDEN float +__math_uflowf (uint32_t sign) +{ + float y = pick_float_except((sign ? -VAL : VAL) * VAL, sign ? -VAL : VAL); + return __math_with_errnof (y, ERANGE); +} diff --git a/src/audio/ffmpeg_dec/libm/common/math_errf_with_errnof.c b/src/audio/ffmpeg_dec/libm/common/math_errf_with_errnof.c new file mode 100644 index 000000000000..9c0fa06cf189 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/math_errf_with_errnof.c @@ -0,0 +1,41 @@ +/* Single-precision math error handling. + Copyright (c) 2017-2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "fdlibm.h" +#include "math_config.h" + +#if WANT_ERRNO +#include +HIDDEN float +__math_with_errnof (float y, int e) +{ + errno = e; + return y; +} +#endif + diff --git a/src/audio/ffmpeg_dec/libm/common/math_inexact.c b/src/audio/ffmpeg_dec/libm/common/math_inexact.c new file mode 100644 index 000000000000..3f5aa9f3f376 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/math_inexact.c @@ -0,0 +1,55 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright © 2021 Keith Packard + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "math_config.h" + +#if FE_INEXACT && !defined(__FLOAT64_NOEXCEPT) + +static CONST_FORCE_FLOAT64 VAL = pick_float64_except(_FLOAT64_MIN, _F_64(0.0)); + +HIDDEN void +__math_set_inexact64(void) +{ + force_eval_float64(_F_64(1.0) + VAL); +} + +HIDDEN __float64 +__math_inexact64(__float64 val) +{ + force_eval_float64(_F_64(1.0) + VAL); + return val; +} + +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/math_inexactf.c b/src/audio/ffmpeg_dec/libm/common/math_inexactf.c new file mode 100644 index 000000000000..d1a278fff0e1 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/math_inexactf.c @@ -0,0 +1,55 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright © 2021 Keith Packard + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "math_config.h" + +#if FE_INEXACT && !defined(__FLOAT_NOEXCEPT) + +static CONST_FORCE_FLOAT VAL = pick_float_except(FLT_MIN, 0.0f); + +HIDDEN void +__math_set_inexactf(void) +{ + force_eval_float(1.0f + VAL); +} + +HIDDEN float +__math_inexactf(float val) +{ + force_eval_float(1.0f + VAL); + return val; +} + +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/math_inexactl.c b/src/audio/ffmpeg_dec/libm/common/math_inexactl.c new file mode 100644 index 000000000000..c4f0b1603a11 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/math_inexactl.c @@ -0,0 +1,62 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright © 2021 Keith Packard + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "math_config.h" + +#if defined(FE_INEXACT) && !defined(__LONG_DOUBLE_NOEXCEPT) && defined(_NEED_FLOAT_HUGE) + +#ifdef _DOUBLE_DOUBLE_FLOAT +static CONST_FORCE_LONG_DOUBLE VAL = pick_long_double_except(LDBL_MIN, 0.0L); +static CONST_FORCE_LONG_DOUBLE VAL1 = pick_long_double_except(LDBL_MAX, 0.0L); +#define eqn (1.0L + VAL + VAL1) +#else +static CONST_FORCE_LONG_DOUBLE VAL = pick_long_double_except(LDBL_MIN, 0.0L); +#define eqn (1.0L + VAL) +#endif + +HIDDEN void +__math_set_inexactl(void) +{ + force_eval_long_double(eqn); +} + +HIDDEN long double +__math_inexactl(long double val) +{ + force_eval_long_double(eqn); + return val; +} + +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/nanl.c b/src/audio/ffmpeg_dec/libm/common/nanl.c new file mode 100644 index 000000000000..906fe205cdab --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/nanl.c @@ -0,0 +1,41 @@ +/* +(C) Copyright IBM Corp. 2009 + +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, +this list of conditions and the following disclaimer. +* Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. +* Neither the name of IBM nor the names of its contributors may be +used to endorse or promote products derived from this software without +specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +*/ + +#include +#include "local.h" + +/* On platforms where long double is as wide as double. */ +#if defined(_NEED_FLOAT_HUGE) && __HAVE_BUILTIN_NANL +long double +nanl (const char *tagp) +{ + return __builtin_nanl(""); +} +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/nexttoward.c b/src/audio/ffmpeg_dec/libm/common/nexttoward.c new file mode 100644 index 000000000000..e8ca3465807e --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/nexttoward.c @@ -0,0 +1,39 @@ +/* +Copyright (c) 2014 Mentor Graphics, Inc. +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of Mentor Graphics nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY CODESOURCERY, INC. ``AS IS'' AND ANY +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL CODESOURCERY BE LIABLE FOR ANY +DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include "math_config.h" + +#if defined(_NEED_FLOAT64) && !defined(_NEED_FLOAT_HUGE) && defined(__HAVE_LONG_DOUBLE) +__float64 +nexttoward64 (__float64 x, long double y) +{ + return nextafter64(x, y); +} + +_MATH_ALIAS_d_dl(nexttoward) +#endif + diff --git a/src/audio/ffmpeg_dec/libm/common/nexttowardf.c b/src/audio/ffmpeg_dec/libm/common/nexttowardf.c new file mode 100644 index 000000000000..d26d76704e24 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/nexttowardf.c @@ -0,0 +1,83 @@ +/* + * Copyright © 2005-2014 Rich Felker, et al. + * + * 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: + * + * 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. + */ + +#include "math_config.h" + +// This is only necessary because the implementation of isnan only works +// properly when long double == double. +// See: https://sourceware.org/ml/newlib/2014/msg00684.html + +#if !defined(_NEED_FLOAT_HUGE) && defined(__HAVE_LONG_DOUBLE) + +float +nexttowardf (float x, long double y) +{ + uint32_t ux; + uint32_t e; + + /* + * We can't do this if y isn't nan as that might raise INEXACT doing + * long double -> float conversion, and we don't want to do this + * in long double for machines without long double HW as we won't + * get any exceptions in that case. + */ + if (isnan(y)) + return x + (issignaling(y) ? __builtin_nansf("") : (float) y); + if (isnan(x)) + return x + x; + + if ((long double) x == y) + return (float) y; + ux = asuint(x); + if (x == 0) { + ux = 1; + if (signbit(y)) + ux |= 0x80000000; + x = asfloat(ux); + force_eval_float(opt_barrier_float(x) * x); + return x; + } else if ((long double) x < y) { + if (signbit(x)) + ux--; + else + ux++; + } else { + if (signbit(x)) + ux++; + else + ux--; + } + e = ux & 0x7f800000; + /* raise overflow if ux.value is infinite and x is finite */ + if (e == 0x7f800000) + return check_oflowf(opt_barrier_float(x+x)); + /* raise underflow if ux.value is subnormal or zero */ + x = asfloat(ux); + if (e == 0) + return __math_denormf(x); + return x; +} + +_MATH_ALIAS_f_fl(nexttoward) + +#endif /* _NEED_FLOAT_HUGE */ diff --git a/src/audio/ffmpeg_dec/libm/common/pow.c b/src/audio/ffmpeg_dec/libm/common/pow.c new file mode 100644 index 000000000000..4c2cf25ff62f --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/pow.c @@ -0,0 +1,414 @@ +/* Double-precision x^y function. + Copyright (c) 2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "fdlibm.h" +#if !__OBSOLETE_MATH_DOUBLE + +#include +#include +#include "math_config.h" + +/* +Worst-case error: 0.54 ULP (~= ulperr_exp + 1024*Ln2*relerr_log*2^53) +relerr_log: 1.3 * 2^-68 (Relative error of log, 1.5 * 2^-68 without fma) +ulperr_exp: 0.509 ULP (ULP error of exp, 0.511 ULP without fma) +*/ + +#define T __pow_log_data.tab +#define A __pow_log_data.poly +#define Ln2hi __pow_log_data.ln2hi +#define Ln2lo __pow_log_data.ln2lo +#define N (1 << POW_LOG_TABLE_BITS) +#define OFF 0x3fe6955500000000 + +/* Top 12 bits of a double (sign and exponent bits). */ +static inline uint32_t +top12 (double x) +{ + return asuint64 (x) >> 52; +} + +/* Compute y+TAIL = log(x) where the rounded result is y and TAIL has about + additional 15 bits precision. IX is the bit representation of x, but + normalized in the subnormal range using the sign bit for the exponent. */ +static inline double_t +log_inline (uint64_t ix, double_t *tail) +{ + /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ + double_t z, r, y, invc, logc, logctail, kd, hi, t1, t2, lo, lo1, lo2, p; + uint64_t iz, tmp; + int k, i; + + /* x = 2^k z; where z is in range [OFF,2*OFF) and exact. + The range is split into N subintervals. + The ith subinterval contains z and c is near its center. */ + tmp = ix - OFF; + i = (tmp >> (52 - POW_LOG_TABLE_BITS)) % N; + k = (int64_t) tmp >> 52; /* arithmetic shift */ + iz = ix - (tmp & 0xfffULL << 52); + z = asfloat64 (iz); + kd = (double_t) k; + + /* log(x) = k*Ln2 + log(c) + log1p(z/c-1). */ + invc = T[i].invc; + logc = T[i].logc; + logctail = T[i].logctail; + + /* Note: 1/c is j/N or j/N/2 where j is an integer in [N,2N) and + |z/c - 1| < 1/N, so r = z/c - 1 is exactly representible. */ +#if __HAVE_FAST_FMA + r = fma (z, invc, -1.0); +#else + /* Split z such that rhi, rlo and rhi*rhi are exact and |rlo| <= |r|. */ + double_t zhi = asfloat64 ((iz + (1ULL << 31)) & (-1ULL << 32)); + double_t zlo = z - zhi; + double_t rhi = zhi * invc - 1.0; + double_t rlo = zlo * invc; + r = rhi + rlo; +#endif + + /* k*Ln2 + log(c) + r. */ + t1 = kd * Ln2hi + logc; + t2 = t1 + r; + lo1 = kd * Ln2lo + logctail; + lo2 = t1 - t2 + r; + + /* Evaluation is optimized assuming superscalar pipelined execution. */ + double_t ar, ar2, ar3, lo3, lo4; + ar = A[0] * r; /* A[0] = -0.5. */ + ar2 = r * ar; + ar3 = r * ar2; + /* k*Ln2 + log(c) + r + A[0]*r*r. */ +#if __HAVE_FAST_FMA + hi = t2 + ar2; + lo3 = fma (ar, r, -ar2); + lo4 = t2 - hi + ar2; +#else + double_t arhi = A[0] * rhi; + double_t arhi2 = rhi * arhi; + hi = t2 + arhi2; + lo3 = rlo * (ar + arhi); + lo4 = t2 - hi + arhi2; +#endif + /* p = log1p(r) - r - A[0]*r*r. */ +#if POW_LOG_POLY_ORDER == 8 + p = (ar3 + * (A[1] + r * A[2] + ar2 * (A[3] + r * A[4] + ar2 * (A[5] + r * A[6])))); +#endif + lo = lo1 + lo2 + lo3 + lo4 + p; + y = hi + lo; + *tail = hi - y + lo; + return y; +} + +#undef N +#undef T +#define N (1 << EXP_TABLE_BITS) +#define InvLn2N __exp_data.invln2N +#define NegLn2hiN __exp_data.negln2hiN +#define NegLn2loN __exp_data.negln2loN +#define Shift __exp_data.shift +#define T __exp_data.tab +#define C2 __exp_data.poly[5 - EXP_POLY_ORDER] +#define C3 __exp_data.poly[6 - EXP_POLY_ORDER] +#define C4 __exp_data.poly[7 - EXP_POLY_ORDER] +#define C5 __exp_data.poly[8 - EXP_POLY_ORDER] +#define C6 __exp_data.poly[9 - EXP_POLY_ORDER] + +/* Handle cases that may overflow or underflow when computing the result that + is scale*(1+TMP) without intermediate rounding. The bit representation of + scale is in SBITS, however it has a computed exponent that may have + overflown into the sign bit so that needs to be adjusted before using it as + a double. (int32_t)KI is the k used in the argument reduction and exponent + adjustment of scale, positive k here means the result may overflow and + negative k means the result may underflow. */ +static inline double +specialcase (double_t tmp, uint64_t sbits, uint64_t ki) +{ + double_t scale, y; + + if ((ki & 0x80000000) == 0) + { + /* k > 0, the exponent of scale might have overflowed by <= 460. */ + sbits -= 1009ull << 52; + scale = asfloat64 (sbits); + y = 0x1p1009 * (scale + scale * tmp); + return check_oflow (y); + } + /* k < 0, need special care in the subnormal range. */ + sbits += 1022ull << 52; + /* Note: sbits is signed scale. */ + scale = asfloat64 (sbits); + y = scale + scale * tmp; +#if FLT_EVAL_METHOD == 2 +#define fabs(x) fabsl(x) +#endif + if (fabs (y) < 1.0) +#undef fabs + { + /* Round y to the right precision before scaling it into the subnormal + range to avoid double rounding that can cause 0.5+E/2 ulp error where + E is the worst-case ulp error outside the subnormal range. So this + is only useful if the goal is better than 1 ulp worst-case error. */ + double_t hi, lo, one = 1.0; + if (y < 0.0) + one = -1.0; + lo = scale - y + scale * tmp; + hi = one + y; + lo = one - hi + y + lo; + y = eval_as_double (hi + lo) - one; + /* Fix the sign of 0. */ + if (y == 0.0) + y = asfloat64 (sbits & 0x8000000000000000); + /* The underflow exception needs to be signaled explicitly. */ + force_eval_double (opt_barrier_double (0x1p-1022) * 0x1p-1022); + } + y = 0x1p-1022 * y; + return check_uflow (y); +} + +#define SIGN_BIAS ((int32_t) 0x800 << EXP_TABLE_BITS) + +/* Computes sign*exp(x+xtail) where |xtail| < 2^-8/N and |xtail| <= |x|. + The sign_bias argument is SIGN_BIAS or 0 and sets the sign to -1 or 1. */ +static inline double +exp_inline (double x, double xtail, uint32_t sign_bias) +{ + uint32_t abstop; + uint64_t ki, idx, top, sbits; + /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ + double_t kd, z, r, r2, scale, tail, tmp; + + abstop = top12 (x) & 0x7ff; + if (unlikely (abstop - top12 (0x1p-54) >= top12 (512.0) - top12 (0x1p-54))) + { + if (abstop - top12 (0x1p-54) >= 0x80000000) + { + /* Avoid spurious underflow for tiny x. */ + /* Note: 0 is common input. */ +#if WANT_ROUNDING + double_t one = 1.0 + x; +#else + double_t one = 1.0; +#endif + return sign_bias ? -one : one; + } + if (abstop >= top12 (1024.0)) + { + /* Note: inf and nan are already handled. */ + if (asuint64 (x) >> 63) + return __math_uflow (sign_bias); + else + return __math_oflow (sign_bias); + } + /* Large x is special cased below. */ + abstop = 0; + } + + /* exp(x) = 2^(k/N) * exp(r), with exp(r) in [2^(-1/2N),2^(1/2N)]. */ + /* x = ln2/N*k + r, with int k and r in [-ln2/2N, ln2/2N]. */ + z = InvLn2N * x; +#if TOINT_INTRINSICS + kd = roundtoint (z); + ki = converttoint (z); +#elif EXP_USE_TOINT_NARROW + /* z - kd is in [-0.5-2^-16, 0.5] in all rounding modes. */ + kd = eval_as_double (z + Shift); + ki = asuint64 (kd) >> 16; + kd = (double_t) (int32_t) ki; +#else + /* z - kd is in [-1, 1] in non-nearest rounding modes. */ + kd = eval_as_double (z + Shift); + ki = asuint64 (kd); + kd -= Shift; +#endif + r = x + kd * NegLn2hiN + kd * NegLn2loN; + /* The code assumes 2^-200 < |xtail| < 2^-8/N. */ + r += xtail; + /* 2^(k/N) ~= scale * (1 + tail). */ + idx = 2 * (ki % N); + top = (ki + sign_bias) << (52 - EXP_TABLE_BITS); + tail = asfloat64 (T[idx]); + /* This is only a valid scale when -1023*N < k < 1024*N. */ + sbits = T[idx + 1] + top; + /* exp(x) = 2^(k/N) * exp(r) ~= scale + scale * (tail + exp(r) - 1). */ + /* Evaluation is optimized assuming superscalar pipelined execution. */ + r2 = r * r; + /* Without fma the worst case error is 0.25/N ulp larger. */ + /* Worst case error is less than 0.5+1.11/N+(abs poly error * 2^53) ulp. */ +#if EXP_POLY_ORDER == 4 + tmp = tail + r + r2 * C2 + r * r2 * (C3 + r * C4); +#elif EXP_POLY_ORDER == 5 + tmp = tail + r + r2 * (C2 + r * C3) + r2 * r2 * (C4 + r * C5); +#elif EXP_POLY_ORDER == 6 + tmp = tail + r + r2 * (0.5 + r * C3) + r2 * r2 * (C4 + r * C5 + r2 * C6); +#endif + if (unlikely (abstop == 0)) + return specialcase (tmp, sbits, ki); + scale = asfloat64 (sbits); + /* Note: tmp == 0 or |tmp| > 2^-200 and scale > 2^-739, so there + is no spurious underflow here even without fma. */ + return scale + scale * tmp; +} + +/* Returns 0 if not int, 1 if odd int, 2 if even int. The argument is + the bit representation of a non-zero finite floating-point value. */ +static inline int +checkint (uint64_t iy) +{ + int e = iy >> 52 & 0x7ff; + if (e < 0x3ff) + return 0; + if (e > 0x3ff + 52) + return 2; + if (iy & ((1ULL << (0x3ff + 52 - e)) - 1)) + return 0; + if (iy & (1ULL << (0x3ff + 52 - e))) + return 1; + return 2; +} + +/* Returns 1 if input is the bit representation of 0, infinity or nan. */ +static inline int +zeroinfnan (uint64_t i) +{ + return 2 * i - 1 >= 2 * asuint64 ((double) INFINITY) - 1; +} + +double +pow (double x, double y) +{ + uint32_t sign_bias = 0; + uint64_t ix, iy; + uint32_t topx, topy; + + ix = asuint64 (x); + iy = asuint64 (y); + topx = top12 (x); + topy = top12 (y); + if (unlikely (topx - 0x001 >= 0x7ff - 0x001 + || (topy & 0x7ff) - 0x3be >= 0x43e - 0x3be)) + { + /* Note: if |y| > 1075 * ln2 * 2^53 ~= 0x1.749p62 then pow(x,y) = inf/0 + and if |y| < 2^-54 / 1075 ~= 0x1.e7b6p-65 then pow(x,y) = +-1. */ + /* Special cases: (x < 0x1p-126 or inf or nan) or + (|y| < 0x1p-65 or |y| >= 0x1p63 or nan). */ + if (unlikely (zeroinfnan (iy))) + { + if (2 * iy == 0) + return issignaling64_inline (x) ? x + y : 1.0; + if (ix == asuint64 (1.0)) + return issignaling64_inline (y) ? x + y : 1.0; + if (2 * ix > 2 * asuint64 ((double) INFINITY) + || 2 * iy > 2 * asuint64 ((double) INFINITY)) + return x + y; + if (2 * ix == 2 * asuint64 (1.0)) + return 1.0; + if ((2 * ix < 2 * asuint64 (1.0)) == !(iy >> 63)) + return 0.0; /* |x|<1 && y==inf or |x|>1 && y==-inf. */ + return y * y; + } + if (unlikely (zeroinfnan (ix))) + { + double_t x2 = x * x; + if (ix >> 63 && checkint (iy) == 1) + { + x2 = -x2; + sign_bias = 1; + } + if (WANT_ERRNO && 2 * ix == 0 && iy >> 63) + return __math_divzero (sign_bias); + /* Without the barrier some versions of clang hoist the 1/x2 and + thus division by zero exception can be signaled spuriously. */ + return iy >> 63 ? opt_barrier_double (1 / x2) : x2; + } + /* Here x and y are non-zero finite. */ + if (ix >> 63) + { + /* Finite x < 0. */ + int yint = checkint (iy); + if (yint == 0) + return __math_invalid (x); + if (yint == 1) + sign_bias = SIGN_BIAS; + ix &= 0x7fffffffffffffff; + topx &= 0x7ff; + } + if ((topy & 0x7ff) - 0x3be >= 0x43e - 0x3be) + { + /* Note: sign_bias == 0 here because y is not odd. */ + if (ix == asuint64 (1.0)) + return 1.0; + if ((topy & 0x7ff) < 0x3be) + { + /* |y| < 2^-65, x^y ~= 1 + y*log(x). */ +#if WANT_ROUNDING + return ix > asuint64 (1.0) ? 1.0 + y : 1.0 - y; +#else + return 1.0; +#endif + } + return (ix > asuint64 (1.0)) == (topy < 0x800) ? __math_oflow (0) + : __math_uflow (0); + } + if (topx == 0) + { + /* Normalize subnormal x so exponent becomes negative. */ + ix = asuint64 (x * 0x1p52); + ix &= 0x7fffffffffffffff; + ix -= 52ULL << 52; + } + } + + double_t lo; + double_t hi = log_inline (ix, &lo); + double_t ehi, elo; +#if __HAVE_FAST_FMA + ehi = y * hi; + elo = y * lo + fma (y, hi, -ehi); +#else + double_t yhi = asfloat64 (iy & -1ULL << 27); + double_t ylo = y - yhi; + double_t lhi = asfloat64 (asuint64 (hi) & -1ULL << 27); + double_t llo = hi - lhi + lo; + ehi = yhi * lhi; + elo = ylo * lhi + y * llo; /* |elo| < |ehi| * 2^-25. */ +#endif + return exp_inline (ehi, elo, sign_bias); +} + +#ifdef __strong_reference +#if defined(__GNUCLIKE_PRAGMA_DIAGNOSTIC) && !defined(__clang__) +#pragma GCC diagnostic ignored "-Wmissing-attributes" +#endif +__strong_reference(pow, _pow); +#endif + +_MATH_ALIAS_d_dd(pow) + +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/pow_log_data.c b/src/audio/ffmpeg_dec/libm/common/pow_log_data.c new file mode 100644 index 000000000000..4045cb886247 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/pow_log_data.c @@ -0,0 +1,209 @@ +/* Data for the log part of pow. + Copyright (c) 2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "fdlibm.h" +#if !__OBSOLETE_MATH_DOUBLE + +#include "math_config.h" + +#define N (1 << POW_LOG_TABLE_BITS) + +const struct pow_log_data __pow_log_data = { +.ln2hi = 0x1.62e42fefa3800p-1, +.ln2lo = 0x1.ef35793c76730p-45, +.poly = { +#if N == 128 && POW_LOG_POLY_ORDER == 8 +// relative error: 0x1.11922ap-70 +// in -0x1.6bp-8 0x1.6bp-8 +// Coefficients are scaled to match the scaling during evaluation. +-0x1p-1, +0x1.555555555556p-2 * -2, +-0x1.0000000000006p-2 * -2, +0x1.999999959554ep-3 * 4, +-0x1.555555529a47ap-3 * 4, +0x1.2495b9b4845e9p-3 * -8, +-0x1.0002b8b263fc3p-3 * -8, +#endif +}, +/* Algorithm: + + x = 2^k z + log(x) = k ln2 + log(c) + log(z/c) + log(z/c) = poly(z/c - 1) + +where z is in [0x1.69555p-1; 0x1.69555p0] which is split into N subintervals +and z falls into the ith one, then table entries are computed as + + tab[i].invc = 1/c + tab[i].logc = round(0x1p43*log(c))/0x1p43 + tab[i].logctail = (double)(log(c) - logc) + +where c is chosen near the center of the subinterval such that 1/c has only a +few precision bits so z/c - 1 is exactly representible as double: + + 1/c = center < 1 ? round(N/center)/N : round(2*N/center)/N/2 + +Note: |z/c - 1| < 1/N for the chosen c, |log(c) - logc - logctail| < 0x1p-97, +the last few bits of logc are rounded away so k*ln2hi + logc has no rounding +error and the interval for z is selected such that near x == 1, where log(x) +is tiny, large cancellation error is avoided in logc + poly(z/c - 1). */ +.tab = { +#if N == 128 +#define A(a,b,c) {a,0,b,c}, +A(0x1.6a00000000000p+0, -0x1.62c82f2b9c800p-2, 0x1.ab42428375680p-48) +A(0x1.6800000000000p+0, -0x1.5d1bdbf580800p-2, -0x1.ca508d8e0f720p-46) +A(0x1.6600000000000p+0, -0x1.5767717455800p-2, -0x1.362a4d5b6506dp-45) +A(0x1.6400000000000p+0, -0x1.51aad872df800p-2, -0x1.684e49eb067d5p-49) +A(0x1.6200000000000p+0, -0x1.4be5f95777800p-2, -0x1.41b6993293ee0p-47) +A(0x1.6000000000000p+0, -0x1.4618bc21c6000p-2, 0x1.3d82f484c84ccp-46) +A(0x1.5e00000000000p+0, -0x1.404308686a800p-2, 0x1.c42f3ed820b3ap-50) +A(0x1.5c00000000000p+0, -0x1.3a64c55694800p-2, 0x1.0b1c686519460p-45) +A(0x1.5a00000000000p+0, -0x1.347dd9a988000p-2, 0x1.5594dd4c58092p-45) +A(0x1.5800000000000p+0, -0x1.2e8e2bae12000p-2, 0x1.67b1e99b72bd8p-45) +A(0x1.5600000000000p+0, -0x1.2895a13de8800p-2, 0x1.5ca14b6cfb03fp-46) +A(0x1.5600000000000p+0, -0x1.2895a13de8800p-2, 0x1.5ca14b6cfb03fp-46) +A(0x1.5400000000000p+0, -0x1.22941fbcf7800p-2, -0x1.65a242853da76p-46) +A(0x1.5200000000000p+0, -0x1.1c898c1699800p-2, -0x1.fafbc68e75404p-46) +A(0x1.5000000000000p+0, -0x1.1675cababa800p-2, 0x1.f1fc63382a8f0p-46) +A(0x1.4e00000000000p+0, -0x1.1058bf9ae4800p-2, -0x1.6a8c4fd055a66p-45) +A(0x1.4c00000000000p+0, -0x1.0a324e2739000p-2, -0x1.c6bee7ef4030ep-47) +A(0x1.4a00000000000p+0, -0x1.0402594b4d000p-2, -0x1.036b89ef42d7fp-48) +A(0x1.4a00000000000p+0, -0x1.0402594b4d000p-2, -0x1.036b89ef42d7fp-48) +A(0x1.4800000000000p+0, -0x1.fb9186d5e4000p-3, 0x1.d572aab993c87p-47) +A(0x1.4600000000000p+0, -0x1.ef0adcbdc6000p-3, 0x1.b26b79c86af24p-45) +A(0x1.4400000000000p+0, -0x1.e27076e2af000p-3, -0x1.72f4f543fff10p-46) +A(0x1.4200000000000p+0, -0x1.d5c216b4fc000p-3, 0x1.1ba91bbca681bp-45) +A(0x1.4000000000000p+0, -0x1.c8ff7c79aa000p-3, 0x1.7794f689f8434p-45) +A(0x1.4000000000000p+0, -0x1.c8ff7c79aa000p-3, 0x1.7794f689f8434p-45) +A(0x1.3e00000000000p+0, -0x1.bc286742d9000p-3, 0x1.94eb0318bb78fp-46) +A(0x1.3c00000000000p+0, -0x1.af3c94e80c000p-3, 0x1.a4e633fcd9066p-52) +A(0x1.3a00000000000p+0, -0x1.a23bc1fe2b000p-3, -0x1.58c64dc46c1eap-45) +A(0x1.3a00000000000p+0, -0x1.a23bc1fe2b000p-3, -0x1.58c64dc46c1eap-45) +A(0x1.3800000000000p+0, -0x1.9525a9cf45000p-3, -0x1.ad1d904c1d4e3p-45) +A(0x1.3600000000000p+0, -0x1.87fa06520d000p-3, 0x1.bbdbf7fdbfa09p-45) +A(0x1.3400000000000p+0, -0x1.7ab890210e000p-3, 0x1.bdb9072534a58p-45) +A(0x1.3400000000000p+0, -0x1.7ab890210e000p-3, 0x1.bdb9072534a58p-45) +A(0x1.3200000000000p+0, -0x1.6d60fe719d000p-3, -0x1.0e46aa3b2e266p-46) +A(0x1.3000000000000p+0, -0x1.5ff3070a79000p-3, -0x1.e9e439f105039p-46) +A(0x1.3000000000000p+0, -0x1.5ff3070a79000p-3, -0x1.e9e439f105039p-46) +A(0x1.2e00000000000p+0, -0x1.526e5e3a1b000p-3, -0x1.0de8b90075b8fp-45) +A(0x1.2c00000000000p+0, -0x1.44d2b6ccb8000p-3, 0x1.70cc16135783cp-46) +A(0x1.2c00000000000p+0, -0x1.44d2b6ccb8000p-3, 0x1.70cc16135783cp-46) +A(0x1.2a00000000000p+0, -0x1.371fc201e9000p-3, 0x1.178864d27543ap-48) +A(0x1.2800000000000p+0, -0x1.29552f81ff000p-3, -0x1.48d301771c408p-45) +A(0x1.2600000000000p+0, -0x1.1b72ad52f6000p-3, -0x1.e80a41811a396p-45) +A(0x1.2600000000000p+0, -0x1.1b72ad52f6000p-3, -0x1.e80a41811a396p-45) +A(0x1.2400000000000p+0, -0x1.0d77e7cd09000p-3, 0x1.a699688e85bf4p-47) +A(0x1.2400000000000p+0, -0x1.0d77e7cd09000p-3, 0x1.a699688e85bf4p-47) +A(0x1.2200000000000p+0, -0x1.fec9131dbe000p-4, -0x1.575545ca333f2p-45) +A(0x1.2000000000000p+0, -0x1.e27076e2b0000p-4, 0x1.a342c2af0003cp-45) +A(0x1.2000000000000p+0, -0x1.e27076e2b0000p-4, 0x1.a342c2af0003cp-45) +A(0x1.1e00000000000p+0, -0x1.c5e548f5bc000p-4, -0x1.d0c57585fbe06p-46) +A(0x1.1c00000000000p+0, -0x1.a926d3a4ae000p-4, 0x1.53935e85baac8p-45) +A(0x1.1c00000000000p+0, -0x1.a926d3a4ae000p-4, 0x1.53935e85baac8p-45) +A(0x1.1a00000000000p+0, -0x1.8c345d631a000p-4, 0x1.37c294d2f5668p-46) +A(0x1.1a00000000000p+0, -0x1.8c345d631a000p-4, 0x1.37c294d2f5668p-46) +A(0x1.1800000000000p+0, -0x1.6f0d28ae56000p-4, -0x1.69737c93373dap-45) +A(0x1.1600000000000p+0, -0x1.51b073f062000p-4, 0x1.f025b61c65e57p-46) +A(0x1.1600000000000p+0, -0x1.51b073f062000p-4, 0x1.f025b61c65e57p-46) +A(0x1.1400000000000p+0, -0x1.341d7961be000p-4, 0x1.c5edaccf913dfp-45) +A(0x1.1400000000000p+0, -0x1.341d7961be000p-4, 0x1.c5edaccf913dfp-45) +A(0x1.1200000000000p+0, -0x1.16536eea38000p-4, 0x1.47c5e768fa309p-46) +A(0x1.1000000000000p+0, -0x1.f0a30c0118000p-5, 0x1.d599e83368e91p-45) +A(0x1.1000000000000p+0, -0x1.f0a30c0118000p-5, 0x1.d599e83368e91p-45) +A(0x1.0e00000000000p+0, -0x1.b42dd71198000p-5, 0x1.c827ae5d6704cp-46) +A(0x1.0e00000000000p+0, -0x1.b42dd71198000p-5, 0x1.c827ae5d6704cp-46) +A(0x1.0c00000000000p+0, -0x1.77458f632c000p-5, -0x1.cfc4634f2a1eep-45) +A(0x1.0c00000000000p+0, -0x1.77458f632c000p-5, -0x1.cfc4634f2a1eep-45) +A(0x1.0a00000000000p+0, -0x1.39e87b9fec000p-5, 0x1.502b7f526feaap-48) +A(0x1.0a00000000000p+0, -0x1.39e87b9fec000p-5, 0x1.502b7f526feaap-48) +A(0x1.0800000000000p+0, -0x1.f829b0e780000p-6, -0x1.980267c7e09e4p-45) +A(0x1.0800000000000p+0, -0x1.f829b0e780000p-6, -0x1.980267c7e09e4p-45) +A(0x1.0600000000000p+0, -0x1.7b91b07d58000p-6, -0x1.88d5493faa639p-45) +A(0x1.0400000000000p+0, -0x1.fc0a8b0fc0000p-7, -0x1.f1e7cf6d3a69cp-50) +A(0x1.0400000000000p+0, -0x1.fc0a8b0fc0000p-7, -0x1.f1e7cf6d3a69cp-50) +A(0x1.0200000000000p+0, -0x1.fe02a6b100000p-8, -0x1.9e23f0dda40e4p-46) +A(0x1.0200000000000p+0, -0x1.fe02a6b100000p-8, -0x1.9e23f0dda40e4p-46) +A(0x1.0000000000000p+0, 0x0.0000000000000p+0, 0x0.0000000000000p+0) +A(0x1.0000000000000p+0, 0x0.0000000000000p+0, 0x0.0000000000000p+0) +A(0x1.fc00000000000p-1, 0x1.0101575890000p-7, -0x1.0c76b999d2be8p-46) +A(0x1.f800000000000p-1, 0x1.0205658938000p-6, -0x1.3dc5b06e2f7d2p-45) +A(0x1.f400000000000p-1, 0x1.8492528c90000p-6, -0x1.aa0ba325a0c34p-45) +A(0x1.f000000000000p-1, 0x1.0415d89e74000p-5, 0x1.111c05cf1d753p-47) +A(0x1.ec00000000000p-1, 0x1.466aed42e0000p-5, -0x1.c167375bdfd28p-45) +A(0x1.e800000000000p-1, 0x1.894aa149fc000p-5, -0x1.97995d05a267dp-46) +A(0x1.e400000000000p-1, 0x1.ccb73cdddc000p-5, -0x1.a68f247d82807p-46) +A(0x1.e200000000000p-1, 0x1.eea31c006c000p-5, -0x1.e113e4fc93b7bp-47) +A(0x1.de00000000000p-1, 0x1.1973bd1466000p-4, -0x1.5325d560d9e9bp-45) +A(0x1.da00000000000p-1, 0x1.3bdf5a7d1e000p-4, 0x1.cc85ea5db4ed7p-45) +A(0x1.d600000000000p-1, 0x1.5e95a4d97a000p-4, -0x1.c69063c5d1d1ep-45) +A(0x1.d400000000000p-1, 0x1.700d30aeac000p-4, 0x1.c1e8da99ded32p-49) +A(0x1.d000000000000p-1, 0x1.9335e5d594000p-4, 0x1.3115c3abd47dap-45) +A(0x1.cc00000000000p-1, 0x1.b6ac88dad6000p-4, -0x1.390802bf768e5p-46) +A(0x1.ca00000000000p-1, 0x1.c885801bc4000p-4, 0x1.646d1c65aacd3p-45) +A(0x1.c600000000000p-1, 0x1.ec739830a2000p-4, -0x1.dc068afe645e0p-45) +A(0x1.c400000000000p-1, 0x1.fe89139dbe000p-4, -0x1.534d64fa10afdp-45) +A(0x1.c000000000000p-1, 0x1.1178e8227e000p-3, 0x1.1ef78ce2d07f2p-45) +A(0x1.be00000000000p-1, 0x1.1aa2b7e23f000p-3, 0x1.ca78e44389934p-45) +A(0x1.ba00000000000p-1, 0x1.2d1610c868000p-3, 0x1.39d6ccb81b4a1p-47) +A(0x1.b800000000000p-1, 0x1.365fcb0159000p-3, 0x1.62fa8234b7289p-51) +A(0x1.b400000000000p-1, 0x1.4913d8333b000p-3, 0x1.5837954fdb678p-45) +A(0x1.b200000000000p-1, 0x1.527e5e4a1b000p-3, 0x1.633e8e5697dc7p-45) +A(0x1.ae00000000000p-1, 0x1.6574ebe8c1000p-3, 0x1.9cf8b2c3c2e78p-46) +A(0x1.ac00000000000p-1, 0x1.6f0128b757000p-3, -0x1.5118de59c21e1p-45) +A(0x1.aa00000000000p-1, 0x1.7898d85445000p-3, -0x1.c661070914305p-46) +A(0x1.a600000000000p-1, 0x1.8beafeb390000p-3, -0x1.73d54aae92cd1p-47) +A(0x1.a400000000000p-1, 0x1.95a5adcf70000p-3, 0x1.7f22858a0ff6fp-47) +A(0x1.a000000000000p-1, 0x1.a93ed3c8ae000p-3, -0x1.8724350562169p-45) +A(0x1.9e00000000000p-1, 0x1.b31d8575bd000p-3, -0x1.c358d4eace1aap-47) +A(0x1.9c00000000000p-1, 0x1.bd087383be000p-3, -0x1.d4bc4595412b6p-45) +A(0x1.9a00000000000p-1, 0x1.c6ffbc6f01000p-3, -0x1.1ec72c5962bd2p-48) +A(0x1.9600000000000p-1, 0x1.db13db0d49000p-3, -0x1.aff2af715b035p-45) +A(0x1.9400000000000p-1, 0x1.e530effe71000p-3, 0x1.212276041f430p-51) +A(0x1.9200000000000p-1, 0x1.ef5ade4dd0000p-3, -0x1.a211565bb8e11p-51) +A(0x1.9000000000000p-1, 0x1.f991c6cb3b000p-3, 0x1.bcbecca0cdf30p-46) +A(0x1.8c00000000000p-1, 0x1.07138604d5800p-2, 0x1.89cdb16ed4e91p-48) +A(0x1.8a00000000000p-1, 0x1.0c42d67616000p-2, 0x1.7188b163ceae9p-45) +A(0x1.8800000000000p-1, 0x1.1178e8227e800p-2, -0x1.c210e63a5f01cp-45) +A(0x1.8600000000000p-1, 0x1.16b5ccbacf800p-2, 0x1.b9acdf7a51681p-45) +A(0x1.8400000000000p-1, 0x1.1bf99635a6800p-2, 0x1.ca6ed5147bdb7p-45) +A(0x1.8200000000000p-1, 0x1.214456d0eb800p-2, 0x1.a87deba46baeap-47) +A(0x1.7e00000000000p-1, 0x1.2bef07cdc9000p-2, 0x1.a9cfa4a5004f4p-45) +A(0x1.7c00000000000p-1, 0x1.314f1e1d36000p-2, -0x1.8e27ad3213cb8p-45) +A(0x1.7a00000000000p-1, 0x1.36b6776be1000p-2, 0x1.16ecdb0f177c8p-46) +A(0x1.7800000000000p-1, 0x1.3c25277333000p-2, 0x1.83b54b606bd5cp-46) +A(0x1.7600000000000p-1, 0x1.419b423d5e800p-2, 0x1.8e436ec90e09dp-47) +A(0x1.7400000000000p-1, 0x1.4718dc271c800p-2, -0x1.f27ce0967d675p-45) +A(0x1.7200000000000p-1, 0x1.4c9e09e173000p-2, -0x1.e20891b0ad8a4p-45) +A(0x1.7000000000000p-1, 0x1.522ae0738a000p-2, 0x1.ebe708164c759p-45) +A(0x1.6e00000000000p-1, 0x1.57bf753c8d000p-2, 0x1.fadedee5d40efp-46) +A(0x1.6c00000000000p-1, 0x1.5d5bddf596000p-2, -0x1.a0b2a08a465dcp-47) +#endif +}, +}; +#endif /* __OBSOLETE_MATH_DOUBLE */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_cbrt.c b/src/audio/ffmpeg_dec/libm/common/s_cbrt.c new file mode 100644 index 000000000000..4aa8ba887252 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_cbrt.c @@ -0,0 +1,108 @@ + +/* @(#)s_cbrt.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + * + */ + +/* +FUNCTION + <>, <>---cube root + +INDEX + cbrt +INDEX + cbrtf + +SYNOPSIS + #include + double cbrt(double <[x]>); + float cbrtf(float <[x]>); + +DESCRIPTION + <> computes the cube root of the argument. + +RETURNS + The cube root is returned. + +PORTABILITY + <> is in System V release 4. <> is an extension. +*/ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +/* cbrt(x) + * Return cube root of x + */ +static const __uint32_t + B1 = 715094163, /* B1 = (682-0.03306235651)*2**20 */ + B2 = 696219795; /* B2 = (664-0.03306235651)*2**20 */ + +static const __float64 + C = _F_64(5.42857142857142815906e-01), /* 19/35 = 0x3FE15F15, 0xF15F15F1 */ + D = _F_64(-7.05306122448979611050e-01), /* -864/1225 = 0xBFE691DE, 0x2532C834 */ + E = _F_64(1.41428571428571436819e+00), /* 99/70 = 0x3FF6A0EA, 0x0EA0EA0F */ + F = _F_64(1.60714285714285720630e+00), /* 45/28 = 0x3FF9B6DB, 0x6DB6DB6E */ + G = _F_64(3.57142857142857150787e-01); /* 5/14 = 0x3FD6DB6D, 0xB6DB6DB7 */ + +__float64 +cbrt64(__float64 x) +{ + __int32_t hx; + __float64 r,s,t=_F_64(0.0),w; + __uint32_t sign; + __uint32_t high,low; + + GET_HIGH_WORD(hx,x); + sign=hx&0x80000000; /* sign= sign(x) */ + hx ^=sign; + if(hx>=0x7ff00000) return(x+x); /* cbrt(NaN,INF) is itself */ + GET_LOW_WORD(low,x); + if((hx|low)==0) + return(x); /* cbrt(0) is itself */ + + SET_HIGH_WORD(x,hx); /* x <- |x| */ + /* rough cbrt to 5 bits */ + if(hx<0x00100000) /* subnormal number */ + {SET_HIGH_WORD(t,0x43500000); /* set t= 2**54 */ + t*=x; GET_HIGH_WORD(high,t); SET_HIGH_WORD(t,high/3+B2); + } + else + SET_HIGH_WORD(t,hx/3+B1); + + + /* new cbrt to 23 bits, may be implemented in single precision */ + r=t*t/x; + s=C+r*t; + t*=G+F/(s+E+D/s); + + /* chopped to 20 bits and make it larger than cbrt(x) */ + GET_HIGH_WORD(high,t); + INSERT_WORDS(t,high+0x00000001,0); + + + /* one step newton iteration to 53 bits with error less than 0.667 ulps */ + s=t*t; /* t*t is exact */ + r=x/s; + w=t+t; + r=(r-t)/(w+r); /* r-s is exact */ + t=t+t*r; + + /* retore the sign bit */ + GET_HIGH_WORD(high,t); + SET_HIGH_WORD(t,high|sign); + return(t); +} + +_MATH_ALIAS_d_d(cbrt) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_copysign.c b/src/audio/ffmpeg_dec/libm/common/s_copysign.c new file mode 100644 index 000000000000..d8710dc8fc14 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_copysign.c @@ -0,0 +1,70 @@ + +/* @(#)s_copysign.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* +FUNCTION +<>, <>---sign of <[y]>, magnitude of <[x]> + +INDEX + copysign +INDEX + copysignf + +SYNOPSIS + #include + double copysign (double <[x]>, double <[y]>); + float copysignf (float <[x]>, float <[y]>); + +DESCRIPTION +<> constructs a number with the magnitude (absolute value) +of its first argument, <[x]>, and the sign of its second argument, +<[y]>. + +<> does the same thing; the two functions differ only in +the type of their arguments and result. + +RETURNS +<> returns a <> with the magnitude of +<[x]> and the sign of <[y]>. +<> returns a <> with the magnitude of +<[x]> and the sign of <[y]>. + +PORTABILITY +<> is not required by either ANSI C or the System V Interface +Definition (Issue 2). + +*/ + +/* + * copysign(double x, double y) + * copysign(x,y) returns a value with the magnitude of x and + * with the sign bit of y. + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +__float64 +copysign64(__float64 x, __float64 y) +{ + __uint32_t hx,hy; + GET_HIGH_WORD(hx,x); + GET_HIGH_WORD(hy,y); + SET_HIGH_WORD(x,(hx&0x7fffffff)|(hy&0x80000000)); + return x; +} + +_MATH_ALIAS_d_dd(copysign) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_exp10.c b/src/audio/ffmpeg_dec/libm/common/s_exp10.c new file mode 100644 index 000000000000..94066cc6f5c5 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_exp10.c @@ -0,0 +1,68 @@ +/* @(#)s_exp10.c 5.1 93/09/24 */ +/* Modified from s_exp2.c by Yaakov Selkowitz 2007. */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* +FUNCTION + <>, <>---exponential, base 10 +INDEX + exp10 +INDEX + exp10f + +SYNOPSIS + #include + double exp10(double <[x]>); + float exp10f(float <[x]>); + +DESCRIPTION + <> and <> calculate 10 ^ <[x]>, that is, + @ifnottex + 10 raised to the power <[x]>. + @end ifnottex + @tex + $10^x$ + @end tex + +RETURNS + On success, <> and <> return the calculated value. + If the result underflows, the returned value is <<0>>. If the + result overflows, the returned value is <>. In + either case, <> is set to <>. + +PORTABILITY + <> and <> are GNU extensions. + +*/ + +/* + * wrapper exp10(x) + */ + +#define _GNU_SOURCE +#undef exp10 +#include "fdlibm.h" +#include +#include + +#ifdef _NEED_FLOAT64 + +__float64 +exp1064(__float64 x) /* wrapper exp10 */ +{ + return _pow64(_F_64(10.0), x); +} + +_MATH_ALIAS_d_d(exp10); + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_expm1.c b/src/audio/ffmpeg_dec/libm/common/s_expm1.c new file mode 100644 index 000000000000..3326f304f703 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_expm1.c @@ -0,0 +1,257 @@ + +/* @(#)s_expm1.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* +FUNCTION + <>, <>---exponential minus 1 +INDEX + expm1 +INDEX + expm1f + +SYNOPSIS + #include + double expm1(double <[x]>); + float expm1f(float <[x]>); + +DESCRIPTION + <> and <> calculate the exponential of <[x]> + and subtract 1, that is, + @ifnottex + e raised to the power <[x]> minus 1 (where e + @end ifnottex + @tex + $e^x - 1$ (where $e$ + @end tex + is the base of the natural system of logarithms, approximately + 2.71828). The result is accurate even for small values of + <[x]>, where using <)-1>> would lose many + significant digits. + +RETURNS + e raised to the power <[x]>, minus 1. + +PORTABILITY + Neither <> nor <> is required by ANSI C or by + the System V Interface Definition (Issue 2). +*/ + +/* expm1(x) + * Returns exp(x)-1, the exponential of x minus 1. + * + * Method + * 1. Argument reduction: + * Given x, find r and integer k such that + * + * x = k*ln2 + r, |r| <= 0.5*ln2 ~ 0.34658 + * + * Here a correction term c will be computed to compensate + * the error in r when rounded to a floating-point number. + * + * 2. Approximating expm1(r) by a special rational function on + * the interval [0,0.34658]: + * Since + * r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 - r^4/360 + ... + * we define R1(r*r) by + * r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 * R1(r*r) + * That is, + * R1(r**2) = 6/r *((exp(r)+1)/(exp(r)-1) - 2/r) + * = 6/r * ( 1 + 2.0*(1/(exp(r)-1) - 1/r)) + * = 1 - r^2/60 + r^4/2520 - r^6/100800 + ... + * We use a special Remez algorithm on [0,0.347] to generate + * a polynomial of degree 5 in r*r to approximate R1. The + * maximum error of this polynomial approximation is bounded + * by 2**-61. In other words, + * R1(z) ~ 1.0 + Q1*z + Q2*z**2 + Q3*z**3 + Q4*z**4 + Q5*z**5 + * where Q1 = -1.6666666666666567384E-2, + * Q2 = 3.9682539681370365873E-4, + * Q3 = -9.9206344733435987357E-6, + * Q4 = 2.5051361420808517002E-7, + * Q5 = -6.2843505682382617102E-9; + * (where z=r*r, and the values of Q1 to Q5 are listed below) + * with error bounded by + * | 5 | -61 + * | 1.0+Q1*z+...+Q5*z - R1(z) | <= 2 + * | | + * + * expm1(r) = exp(r)-1 is then computed by the following + * specific way which minimize the accumulation rounding error: + * 2 3 + * r r [ 3 - (R1 + R1*r/2) ] + * expm1(r) = r + --- + --- * [--------------------] + * 2 2 [ 6 - r*(3 - R1*r/2) ] + * + * To compensate the error in the argument reduction, we use + * expm1(r+c) = expm1(r) + c + expm1(r)*c + * ~ expm1(r) + c + r*c + * Thus c+r*c will be added in as the correction terms for + * expm1(r+c). Now rearrange the term to avoid optimization + * screw up: + * ( 2 2 ) + * ({ ( r [ R1 - (3 - R1*r/2) ] ) } r ) + * expm1(r+c)~r - ({r*(--- * [--------------------]-c)-c} - --- ) + * ({ ( 2 [ 6 - r*(3 - R1*r/2) ] ) } 2 ) + * ( ) + * + * = r - E + * 3. Scale back to obtain expm1(x): + * From step 1, we have + * expm1(x) = either 2^k*[expm1(r)+1] - 1 + * = or 2^k*[expm1(r) + (1-2^-k)] + * 4. Implementation notes: + * (A). To save one multiplication, we scale the coefficient Qi + * to Qi*2^i, and replace z by (x^2)/2. + * (B). To achieve maximum accuracy, we compute expm1(x) by + * (i) if x < -56*ln2, return -1.0, (raise inexact if x!=inf) + * (ii) if k=0, return r-E + * (iii) if k=-1, return 0.5*(r-E)-0.5 + * (iv) if k=1 if r < -0.25, return 2*((r+0.5)- E) + * else return 1.0+2.0*(r-E); + * (v) if (k<-2||k>56) return 2^k(1-(E-r)) - 1 (or exp(x)-1) + * (vi) if k <= 20, return 2^k((1-2^-k)-(E-r)), else + * (vii) return 2^k(1-((E+2^-k)-r)) + * + * Special cases: + * expm1(INF) is INF, expm1(NaN) is NaN; + * expm1(-INF) is -1, and + * for finite argument, only expm1(0)=0 is exact. + * + * Accuracy: + * according to an error analysis, the error is always less than + * 1 ulp (unit in the last place). + * + * Misc. info. + * For IEEE double + * if x > 7.09782712893383973096e+02 then expm1(x) overflow + * + * Constants: + * The hexadecimal values are the intended ones for the following + * constants. The decimal values may be used, provided that the + * compiler will convert from decimal to binary accurately enough + * to produce the hexadecimal values shown. + */ + +#include "fdlibm.h" +#include "math_config.h" + +#ifdef _NEED_FLOAT64 + +static const __float64 + one = _F_64(1.0), + huge = _F_64(1.0e+300), + tiny = _F_64(1.0e-300), + o_threshold = _F_64(7.09782712893383973096e+02),/* 0x40862E42, 0xFEFA39EF */ + ln2_hi = _F_64(6.93147180369123816490e-01),/* 0x3fe62e42, 0xfee00000 */ + ln2_lo = _F_64(1.90821492927058770002e-10),/* 0x3dea39ef, 0x35793c76 */ + invln2 = _F_64(1.44269504088896338700e+00),/* 0x3ff71547, 0x652b82fe */ + /* scaled coefficients related to expm1 */ + Q1 = _F_64(-3.33333333333331316428e-02), /* BFA11111 111110F4 */ + Q2 = _F_64(1.58730158725481460165e-03), /* 3F5A01A0 19FE5585 */ + Q3 = _F_64(-7.93650757867487942473e-05), /* BF14CE19 9EAADBB7 */ + Q4 = _F_64(4.00821782732936239552e-06), /* 3ED0CFCA 86E65239 */ + Q5 = _F_64(-2.01099218183624371326e-07); /* BE8AFDB7 6E09C32D */ + +__float64 +_NAME_64(expm1)(__float64 x) +{ + __float64 y,hi,lo,c,t,e,hxs,hfx,r1; + __int32_t k,xsb; + __uint32_t hx; + + GET_HIGH_WORD(hx,x); + xsb = hx&0x80000000; /* sign bit of x */ + if(xsb==0) y=x; else y= -x; /* y = |x| */ + hx &= 0x7fffffff; /* high word of |x| */ + + /* filter out huge and non-finite argument */ + if(hx >= 0x4043687A) { /* if |x|>=56*ln2 */ + if(hx >= 0x40862E42) { /* if |x|>=709.78... */ + if(hx>=0x7ff00000) { + __uint32_t low; + GET_LOW_WORD(low,x); + if(((hx&0xfffff)|low)!=0) + return x+x; /* NaN */ + else return (xsb==0)? x:_F_64(-1.0);/* exp(+-inf)={inf,-1} */ + } + if(x > o_threshold) return __math_oflow (0); /* overflow */ + } + if(xsb!=0) /* x < -56*ln2, return -1.0 with inexact */ + return __math_inexact64(tiny-one); /* return -1 */ + } + + /* argument reduction */ + if(hx > 0x3fd62e42) { /* if |x| > 0.5 ln2 */ + if(hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */ + if(xsb==0) + {hi = x - ln2_hi; lo = ln2_lo; k = 1;} + else + {hi = x + ln2_hi; lo = -ln2_lo; k = -1;} + } else { + k = invln2*x+((xsb==0)?_F_64(0.5):_F_64(-0.5)); + t = k; + hi = x - t*ln2_hi; /* t*ln2_hi is exact here */ + lo = t*ln2_lo; + } + x = hi - lo; + c = (hi-x)-lo; + } + else if(hx < 0x3c900000) { /* when |x|<2**-54, return x */ + t = huge+x; /* return x with inexact flags when x!=0 */ + return x - (t-(huge+x)); + } + else k = 0; + + /* x is now in primary range */ + hfx = _F_64(0.5)*x; + hxs = x*hfx; + r1 = one+hxs*(Q1+hxs*(Q2+hxs*(Q3+hxs*(Q4+hxs*Q5)))); + t = _F_64(3.0)-r1*hfx; + e = hxs*((r1-t)/(_F_64(6.0) - x*t)); + if(k==0) return x - (x*e-hxs); /* c is 0 */ + else { + e = (x*(e-c)-c); + e -= hxs; + if(k== -1) return _F_64(0.5)*(x-e)_F_64(-0.5); + if(k==1) { + if(x < _F_64(-0.25)) return _F_64(-2.0)*(e-(x+_F_64(0.5))); + else return one+_F_64(2.0)*(x-e); + } + if (k <= -2 || k>56) { /* suffice to return exp(x)-1 */ + __uint32_t high; + y = one-(e-x); + GET_HIGH_WORD(high,y); + SET_HIGH_WORD(y,high+lsl(k, 20)); /* add k to y's exponent */ + return y-one; + } + t = one; + if(k<20) { + __uint32_t high; + SET_HIGH_WORD(t,0x3ff00000 - (0x200000>>k)); /* t=1-2^-k */ + y = t-(e-x); + GET_HIGH_WORD(high,y); + SET_HIGH_WORD(y,high+lsl(k, 20)); /* add k to y's exponent */ + } else { + __uint32_t high; + SET_HIGH_WORD(t,lsl((0x3ff-k), 20)); /* 2^-k */ + y = x-(e+t); + y += one; + GET_HIGH_WORD(high,y); + SET_HIGH_WORD(y,high+lsl(k, 20)); /* add k to y's exponent */ + } + } + return y; +} + +_MATH_ALIAS_d_d(expm1) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_fdim.c b/src/audio/ffmpeg_dec/libm/common/s_fdim.c new file mode 100644 index 000000000000..d99a93a6cab3 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_fdim.c @@ -0,0 +1,57 @@ +/* Copyright (C) 2002 by Red Hat, Incorporated. All rights reserved. + * + * Permission to use, copy, modify, and distribute this software + * is freely granted, provided that this notice is preserved. + */ +/* +FUNCTION +<>, <>---positive difference +INDEX + fdim +INDEX + fdimf + +SYNOPSIS + #include + double fdim(double <[x]>, double <[y]>); + float fdimf(float <[x]>, float <[y]>); + +DESCRIPTION +The <> functions determine the positive difference between their +arguments, returning: +. <[x]> - <[y]> if <[x]> > <[y]>, or + @ifnottex +. +0 if <[x]> <= <[y]>, or + @end ifnottex + @tex +. +0 if <[x]> $\leq$ <[y]>, or + @end tex +. NAN if either argument is NAN. +A range error may occur. + +RETURNS +The <> functions return the positive difference value. + +PORTABILITY +ANSI C, POSIX. + +*/ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +__float64 +fdim64(__float64 x, __float64 y) +{ + if (isnan(x) || isnan(y)) return(x+y); + + __float64 z = x > y ? x - y : _F_64(0.0); + if (!isinf(x) && !isinf(y)) + z = check_oflow(z); + return z; +} + +_MATH_ALIAS_d_dd(fdim) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_finite.c b/src/audio/ffmpeg_dec/libm/common/s_finite.c new file mode 100644 index 000000000000..82b5fd40f40c --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_finite.c @@ -0,0 +1,40 @@ + +/* @(#)s_finite.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * finite(x) returns 1 is x is finite, else 0; + * no branching! + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +int +finite64(__float64 x) +{ + __int32_t hx; + GET_HIGH_WORD(hx,x); + return (int)((__uint32_t)((hx&0x7fffffff)-0x7ff00000)>>31); +} + +#ifdef __strong_reference +__strong_reference(finite64, __finite64); +#else +int __finite64(__float64 x) { return finite64(x); } +#endif + +_MATH_ALIAS_i_d(finite) +_MATH_ALIAS_i_d(__finite) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_fma.c b/src/audio/ffmpeg_dec/libm/common/s_fma.c new file mode 100644 index 000000000000..ddf61d9e5e7e --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_fma.c @@ -0,0 +1,100 @@ +/* +Copyright (C) 2002 by Red Hat, Incorporated. All rights reserved. + +Permission to use, copy, modify, and distribute this software +is freely granted, provided that this notice is preserved. + */ +/* +FUNCTION +<>, <>---floating multiply add +INDEX + fma +INDEX + fmaf + +SYNOPSIS + #include + double fma(double <[x]>, double <[y]>, double <[z]>); + float fmaf(float <[x]>, float <[y]>, float <[z]>); + +DESCRIPTION +The <> functions compute (<[x]> * <[y]>) + <[z]>, rounded as one ternary +operation: they compute the value (as if) to infinite precision and round once +to the result format, according to the rounding mode characterized by the value +of FLT_ROUNDS. That is, they are supposed to do this: see below. + +RETURNS +The <> functions return (<[x]> * <[y]>) + <[z]>, rounded as one ternary +operation. + +BUGS +This implementation does not provide the function that it should, purely +returning "(<[x]> * <[y]>) + <[z]>;" with no attempt at all to provide the +simulated infinite precision intermediates which are required. DO NOT USE THEM. + +If double has enough more precision than float, then <> should provide +the expected numeric results, as it does use double for the calculation. But +since this is not the case for all platforms, this manual cannot determine +if it is so for your case. + +PORTABILITY +ANSI C, POSIX. + +*/ + +#include "fdlibm.h" + +#if !__HAVE_FAST_FMA + +#ifdef _NEED_FLOAT64 + +#if __FLT_EVAL_METHOD__ == 2 && defined(__HAVE_LONG_DOUBLE) + +__float64 +fma64(__float64 x, __float64 y, __float64 z) +{ + return (__float64) fmal((long double) x, (long double) y, (long double) z); +} + +#else + +typedef __float64 FLOAT_T; + +#define FMA fma64 +#define NEXTAFTER nextafter64 +#define LDEXP ldexp64 +#define FREXP frexp64 +#define SCALBN scalbn64 +#define ILOGB ilogb64 +#define COPYSIGN copysign64 + +#define SPLIT ((FLOAT_T) 0x1p26 + (FLOAT_T) 1.0) +#define FLOAT_MANT_DIG _FLOAT64_MANT_DIG +#define FLOAT_MAX_EXP _FLOAT64_MAX_EXP +#define FLOAT_MIN _FLOAT64_MIN + +static inline int +odd_mant(FLOAT_T x) +{ + return asuint64(x) & 1; +} + +static unsigned int +EXPONENT(FLOAT_T x) +{ + return _exponent64(asuint64(x)); +} + +#ifdef __FLOAT64_NOEXCEPT +#define feraiseexcept(x) ((void) (x)) +#endif + +#include "fma_inc.h" + +#endif + +_MATH_ALIAS_d_ddd(fma) + +#endif /* _NEED_FLOAT64 */ + +#endif /* !__HAVE_FAST_FMA */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_fmax.c b/src/audio/ffmpeg_dec/libm/common/s_fmax.c new file mode 100644 index 000000000000..e012154f9f59 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_fmax.c @@ -0,0 +1,53 @@ +/* Copyright (C) 2002 by Red Hat, Incorporated. All rights reserved. + * + * Permission to use, copy, modify, and distribute this software + * is freely granted, provided that this notice is preserved. + */ +/* +FUNCTION +<>, <>---maximum +INDEX + fmax +INDEX + fmaxf + +SYNOPSIS + #include + double fmax(double <[x]>, double <[y]>); + float fmaxf(float <[x]>, float <[y]>); + +DESCRIPTION +The <> functions determine the maximum numeric value of their arguments. +NaN arguments are treated as missing data: if one argument is a NaN and the +other numeric, then the <> functions choose the numeric value. + +RETURNS +The <> functions return the maximum numeric value of their arguments. + +PORTABILITY +ANSI C, POSIX. + +*/ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +__float64 +fmax64(__float64 x, __float64 y) +{ + if (issignaling(x) || issignaling(y)) + return x + y; + + if (isnan(x)) + return y; + + if (isnan(y)) + return x; + + return x > y ? x : y; +} + +_MATH_ALIAS_d_dd(fmax) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_fmin.c b/src/audio/ffmpeg_dec/libm/common/s_fmin.c new file mode 100644 index 000000000000..3647dc513033 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_fmin.c @@ -0,0 +1,53 @@ +/* Copyright (C) 2002 by Red Hat, Incorporated. All rights reserved. + * + * Permission to use, copy, modify, and distribute this software + * is freely granted, provided that this notice is preserved. + */ +/* +FUNCTION +<>, <>---minimum +INDEX + fmin +INDEX + fminf + +SYNOPSIS + #include + double fmin(double <[x]>, double <[y]>); + float fminf(float <[x]>, float <[y]>); + +DESCRIPTION +The <> functions determine the minimum numeric value of their arguments. +NaN arguments are treated as missing data: if one argument is a NaN and the +other numeric, then the <> functions choose the numeric value. + +RETURNS +The <> functions return the minimum numeric value of their arguments. + +PORTABILITY +ANSI C, POSIX. + +*/ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +__float64 +fmin64(__float64 x, __float64 y) +{ + if (issignaling(x) || issignaling(y)) + return x + y; + + if (isnan(x)) + return y; + + if (isnan(y)) + return x; + + return x < y ? x : y; +} + +_MATH_ALIAS_d_dd(fmin) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_fpclassify.c b/src/audio/ffmpeg_dec/libm/common/s_fpclassify.c new file mode 100644 index 000000000000..46db6203942e --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_fpclassify.c @@ -0,0 +1,36 @@ +/* Copyright (C) 2002, 2007 by Red Hat, Incorporated. All rights reserved. + * + * Permission to use, copy, modify, and distribute this software + * is freely granted, provided that this notice is preserved. + */ + +#define _ADD_D_TO_DOUBLE_FUNCS + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +int +__fpclassify64 (__float64 x) +{ + __uint32_t msw, lsw; + + EXTRACT_WORDS(msw,lsw,x); + + msw &= 0x7fffffff; + if (msw == 0x00000000 && lsw == 0x00000000) + return FP_ZERO; + else if (msw <= 0x000fffff) + /* zero is already handled above */ + return FP_SUBNORMAL; + else if (msw <= 0x7fefffff) + return FP_NORMAL; + else if (msw == 0x7ff00000 && lsw == 0x00000000) + return FP_INFINITE; + else + return FP_NAN; +} + +_MATH_ALIAS_i_d(__fpclassify) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_getpayload.c b/src/audio/ffmpeg_dec/libm/common/s_getpayload.c new file mode 100644 index 000000000000..ca0e6cd283f8 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_getpayload.c @@ -0,0 +1,55 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright © 2021 Keith Packard + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +__float64 +getpayload64(const __float64 *x) +{ + __int32_t hx, lx; + EXTRACT_WORDS(hx, lx, *x); + + if ((hx & 0x7ff00000) != 0x7ff00000 || + ((hx & 0xfffff) | lx) == 0) + return -1; + hx &= 0x7ffff; + return (__float64) ((((int64_t) hx) << 32) | (uint32_t) lx); +} + +_MATH_ALIAS_d_D(getpayload) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_ilogb.c b/src/audio/ffmpeg_dec/libm/common/s_ilogb.c new file mode 100644 index 000000000000..a0cc7c290054 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_ilogb.c @@ -0,0 +1,102 @@ + +/* @(#)s_ilogb.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* +FUNCTION + <>, <>---get exponent of floating-point number +INDEX + ilogb +INDEX + ilogbf + +SYNOPSIS + #include + int ilogb(double <[val]>); + int ilogbf(float <[val]>); + +DESCRIPTION + + All nonzero, normal numbers can be described as <[m]> * + 2**<[p]>. <> and <> examine the argument + <[val]>, and return <[p]>. The functions <> and + <> are similar to <> and <>, but also + return <[m]>. + +RETURNS + +<> and <> return the power of two used to form the +floating-point argument. +If <[val]> is <<0>>, they return <>. +If <[val]> is infinite, they return <>. +If <[val]> is NaN, they return <>. +(<> and <> are defined in math.h, but in turn are +defined as INT_MIN or INT_MAX from limits.h. The value of FP_ILOGB0 may be +either INT_MIN or -INT_MAX. The value of FP_ILOGBNAN may be either INT_MAX or +INT_MIN.) + +@comment The bugs might not be worth noting, given the mass non-C99/POSIX +@comment behavior of much of the Newlib math library. +@comment BUGS +@comment On errors, errno is not set per C99 and POSIX requirements even if +@comment (math_errhandling & MATH_ERRNO) is non-zero. + +PORTABILITY +C99, POSIX +*/ + +/* ilogb(double x) + * return the binary exponent of non-zero x + * ilogb(0) = 0x80000001 + * ilogb(inf/NaN) = 0x7fffffff (no signal is raised) + */ + +#include +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +int +ilogb64(__float64 x) +{ + __int32_t hx,lx,ix; + + EXTRACT_WORDS(hx,lx,x); + hx &= 0x7fffffff; + if(hx<0x00100000) { + if((hx|lx)==0) { + (void) __math_invalid(_F_64(0.0)); + return FP_ILOGB0; /* ilogb(0) = special case error */ + } else /* subnormal x */ + if(hx==0) { + for (ix = -1043; lx>0; lx = lsl(lx, 1)) ix -=1; + } else { + for (ix = -1022, hx = lsl(hx, 11); hx>0; hx = lsl(hx, 1)) ix -=1; + } + return ix; + } + else if (hx<0x7ff00000) return (hx>>20)-1023; +#if FP_ILOGBNAN != INT_MAX + else if (hx>0x7ff00000) { + (void) __math_invalid(_F_64(0.0)); + return FP_ILOGBNAN; /* NAN */ + } +#endif + else { + (void) __math_invalid(_F_64(0.0)); + return INT_MAX; /* infinite (or, possibly, NAN) */ + } +} + +_MATH_ALIAS_i_d(ilogb) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_infinity.c b/src/audio/ffmpeg_dec/libm/common/s_infinity.c new file mode 100644 index 000000000000..e680e473b613 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_infinity.c @@ -0,0 +1,60 @@ +/* +Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + +Developed at SunPro, a Sun Microsystems, Inc. business. +Permission to use, copy, modify, and distribute this +software is freely granted, provided that this notice +is preserved. + */ +/* + * infinity () returns the representation of infinity. + * Added by Cygnus Support. + */ + +/* +FUNCTION + <>, <>---representation of infinity + +INDEX + infinity +INDEX + infinityf + +SYNOPSIS + #include + double infinity(void); + float infinityf(void); + +DESCRIPTION + <> and <> return the special number IEEE + infinity in double- and single-precision arithmetic + respectively. + +PORTABILITY +<> and <> are neither standard C nor POSIX. C and +POSIX require macros HUGE_VAL and HUGE_VALF to be defined in math.h, which +Newlib defines to be infinities corresponding to these archaic infinity() +and infinityf() functions in floating-point implementations which do have +infinities. + +QUICKREF + infinity - pure + +*/ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +__float64 +infinity64(void) +{ + __float64 x; + + INSERT_WORDS(x,0x7ff00000,0); + return x; +} + +_MATH_ALIAS_d(infinity) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_iseqsig.c b/src/audio/ffmpeg_dec/libm/common/s_iseqsig.c new file mode 100644 index 000000000000..d1079e30dd45 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_iseqsig.c @@ -0,0 +1,54 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright © 2022 Keith Packard + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#define _ADD_D_TO_DOUBLE_FUNCS + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +int +__iseqsig64(__float64 x, __float64 y) +{ + if (isnan(x)) + return __math_invalid(x); + if (isnan(y)) + return __math_invalid(y); + return x == y; +} + +_MATH_ALIAS_i_dd(__iseqsig) + +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/s_isinf.c b/src/audio/ffmpeg_dec/libm/common/s_isinf.c new file mode 100644 index 000000000000..46f4627d6f60 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_isinf.c @@ -0,0 +1,53 @@ +/* +Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + +Developed at SunPro, a Sun Microsystems, Inc. business. +Permission to use, copy, modify, and distribute this +software is freely granted, provided that this notice +is preserved. + */ +/* + * isinf(x) returns 1 if x is infinity, else 0; + * no branching! + * + * isinf is a macro in the C99 standard. It was previously + * implemented as a function by newlib and is declared as such in + * . Newlib supplies it here as a function if the user + * chooses to use it instead of the C99 macro. + */ + +#define _ADD_D_TO_DOUBLE_FUNCS +#define isinfd isinf + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +#undef isinf +#undef isinfl + +int +isinf64(__float64 x) +{ + __uint32_t hx,lx; + EXTRACT_WORDS(hx,lx,x); + hx &= 0x7fffffff; + hx |= (__uint32_t)(lx|(-lx))>>31; + hx = 0x7ff00000 - hx; + return 1 - (int)((__uint32_t)(hx|(-hx))>>31); +} + +#ifdef __strong_reference +__strong_reference(isinf64, __isinf64); +#else +int +__isinf64(float x) +{ + return isinf64(x); +} +#endif + +_MATH_ALIAS_i_d(isinf) +_MATH_ALIAS_i_d(__isinf) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_isnan.c b/src/audio/ffmpeg_dec/libm/common/s_isnan.c new file mode 100644 index 000000000000..caa9ea51cfc9 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_isnan.c @@ -0,0 +1,222 @@ + +/* @(#)s_isnan.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* +FUNCTION +<>, <>, <>, <>, and <>---floating-point classification macros; <>, <>, <>, <>, <>, <>---test for exceptional numbers + +@c C99 (start +INDEX + fpclassify +INDEX + isfinite +INDEX + isinf +INDEX + isnan +INDEX + isnormal +@c C99 end) +@c SUSv2 (start +INDEX + isnan +INDEX + isinf +INDEX + finite + +INDEX + isnanf +INDEX + isinff +INDEX + finitef +@c SUSv2 end) + +SYNOPSIS + [C99 standard macros:] + #include + int fpclassify(real-floating <[x]>); + int isfinite(real-floating <[x]>); + int isinf(real-floating <[x]>); + int isnan(real-floating <[x]>); + int isnormal(real-floating <[x]>); + + [Archaic SUSv2 functions:] + #include + int isnan(double <[arg]>); + int isinf(double <[arg]>); + int finite(double <[arg]>); + int isnanf(float <[arg]>); + int isinff(float <[arg]>); + int finitef(float <[arg]>); + +DESCRIPTION +<>, <>, <>, <>, and <> are macros +defined for use in classifying floating-point numbers. This is a help because +of special "values" like NaN and infinities. In the synopses shown, +"real-floating" indicates that the argument is an expression of real floating +type. These function-like macros are C99 and POSIX-compliant, and should be +used instead of the now-archaic SUSv2 functions. + +The <> macro classifies its argument value as NaN, infinite, normal, +subnormal, zero, or into another implementation-defined category. First, an +argument represented in a format wider than its semantic type is converted to +its semantic type. Then classification is based on the type of the argument. +The <> macro returns the value of the number classification macro +appropriate to the value of its argument: + +o+ +o FP_INFINITE + <[x]> is either plus or minus infinity; +o FP_NAN + <[x]> is "Not A Number" (plus or minus); +o FP_NORMAL + <[x]> is a "normal" number (i.e. is none of the other special forms); +o FP_SUBNORMAL + <[x]> is too small be stored as a regular normalized number (i.e. loss of precision is likely); or +o FP_ZERO + <[x]> is 0 (either plus or minus). +o- + +The "<>" set of macros provide a useful set of shorthand ways for +classifying floating-point numbers, providing the following equivalent +relations: + +o+ +o <>(<[x]>) +returns non-zero if <[x]> is finite. (It is equivalent to +(<>(<[x]>) != FP_INFINITE && <>(<[x]>) != FP_NAN).) + +o <>(<[x]>) +returns non-zero if <[x]> is infinite. (It is equivalent to +(<>(<[x]>) == FP_INFINITE).) + +o <>(<[x]>) +returns non-zero if <[x]> is NaN. (It is equivalent to +(<>(<[x]>) == FP_NAN).) + +o <>(<[x]>) +returns non-zero if <[x]> is normal. (It is equivalent to +(<>(<[x]>) == FP_NORMAL).) +o- + + The archaic SUSv2 functions provide information on the floating-point + argument supplied. + + There are five major number formats ("exponent" referring to the + biased exponent in the binary-encoded number): + o+ + o zero + A number which contains all zero bits, excluding the sign bit. + o subnormal + A number with a zero exponent but a nonzero fraction. + o normal + A number with an exponent and a fraction. + o infinity + A number with an all 1's exponent and a zero fraction. + o NAN + A number with an all 1's exponent and a nonzero fraction. + + o- + + <> returns 1 if the argument is a nan. <> + returns 1 if the argument is infinity. <> returns 1 if the + argument is zero, subnormal or normal. + + The <>, <> and <> functions perform the same + operations as their <>, <> and <> + counterparts, but on single-precision floating-point numbers. + + It should be noted that the C99 standard dictates that <> + and <> are macros that operate on multiple types of + floating-point. The SUSv2 standard declares <> as + a function taking double. Newlib has decided to declare + them both as functions and as macros in math.h to + maintain backward compatibility. + +RETURNS +@comment Formatting note: "$@" forces a new line +The fpclassify macro returns the value corresponding to the appropriate FP_ macro.@* +The isfinite macro returns nonzero if <[x]> is finite, else 0.@* +The isinf macro returns nonzero if <[x]> is infinite, else 0.@* +The isnan macro returns nonzero if <[x]> is an NaN, else 0.@* +The isnormal macro returns nonzero if <[x]> has a normal value, else 0. + +PORTABILITY +math.h macros are C99, POSIX.1-2001. + +The functions originate from BSD; isnan was listed in the X/Open +Portability Guide and Single Unix Specification, but was dropped when +the macro was standardized in POSIX.1-2001. + +QUICKREF + isnan - pure +QUICKREF + isinf - pure +QUICKREF + finite - pure +QUICKREF + isnan - pure +QUICKREF + isinf - pure +QUICKREF + finite - pure +*/ + +/* + * isnan(x) returns 1 is x is nan, else 0; + * no branching! + * + * The C99 standard dictates that isnan is a macro taking + * multiple floating-point types while the SUSv2 standard + * notes it is a function taking a double argument. Newlib + * has chosen to declare it both as a function and as a macro in + * for compatibility. + */ + +#define _ADD_D_TO_DOUBLE_FUNCS +#define isnand isnan + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +#undef isnan +#undef isnanl + +int +isnan64(__float64 x) +{ + __uint32_t hx,lx; + EXTRACT_WORDS(hx,lx,x); + hx &= 0x7fffffff; + hx |= (lx|(-lx))>>31; + hx = 0x7ff00000 - hx; + return (int)(hx>>31); +} + +#ifdef __strong_reference +__strong_reference(isnan64, __isnan64); +#else +int +__isnan64(float x) +{ + return isnan64(x); +} +#endif + +_MATH_ALIAS_i_d(isnan) +_MATH_ALIAS_i_d(__isnan) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_issignaling.c b/src/audio/ffmpeg_dec/libm/common/s_issignaling.c new file mode 100644 index 000000000000..a70b31318843 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_issignaling.c @@ -0,0 +1,48 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright © 2019 Keith Packard + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "math_config.h" + +#ifdef _NEED_FLOAT64 + +int +__issignaling64(__float64 x) +{ + return issignaling64_inline(x); +} + +_MATH_ALIAS_i_d(__issignaling) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_llrint.c b/src/audio/ffmpeg_dec/libm/common/s_llrint.c new file mode 100644 index 000000000000..146c9c773ddc --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_llrint.c @@ -0,0 +1,121 @@ +/* lrint adapted to be llrint for Newlib, 2009 by Craig Howland. */ +/* @(#)s_lrint.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * llrint(x) + * Return x rounded to integral value according to the prevailing + * rounding mode. + * Method: + * Using floating addition. + * Exception: + * Inexact flag raised if x not equal to llrint(x). + */ + +#include "fdlibm.h" +#include + +#ifdef _NEED_FLOAT64 + +static const double +/* Adding a double, x, to 2^52 will cause the result to be rounded based on + the fractional part of x, according to the implementation's current rounding + mode. 2^52 is the smallest double that can be represented using all 52 significant + digits. */ +TWO52[2]={ + _F_64(4.50359962737049600000e+15), /* 0x43300000, 0x00000000 */ + _F_64(-4.50359962737049600000e+15), /* 0xC3300000, 0x00000000 */ +}; + +long long int +llrint64(__float64 x) +{ + __int32_t i0,j0,sx; + __uint32_t i1; + __float64 t; + volatile __float64 w; + long long int result; + + EXTRACT_WORDS(i0,i1,x); + + /* Extract sign bit. */ + sx = (i0>>31)&1; + + /* Extract exponent field. */ + j0 = ((i0 & 0x7ff00000) >> 20) - 1023; + /* j0 in [-1023,1024] */ + + if(j0 < 20) + { + /* j0 in [-1023,19] */ + w = TWO52[sx] + x; + t = w - TWO52[sx]; + GET_HIGH_WORD(i0, t); + /* Detect the all-zeros representation of plus and + minus zero, which fails the calculation below. */ + if ((i0 & ~lsl((__int32_t) 1, 31)) == 0) + return 0; + j0 = ((i0 & 0x7ff00000) >> 20) - 1023; + i0 &= 0x000fffff; + i0 |= 0x00100000; + result = (j0 < 0 ? 0 : i0 >> (20 - j0)); + } + else if (j0 < (int)(8 * sizeof (long long int)) - 1) + { + /* 64bit return: j0 in [20,62] */ + if (j0 >= 52) + /* 64bit return: j0 in [52,62] */ + /* 64bit return: left shift amt in [32,42] */ + result = lsl((long long int) ((i0 & 0x000fffff) | 0x00100000), (j0 - 20)) | + /* 64bit return: right shift amt in [0,10] */ + lsl((long long int) i1, (j0 - 52)); + else + { + /* 64bit return: j0 in [20,51] */ + w = TWO52[sx] + x; + t = w - TWO52[sx]; + EXTRACT_WORDS (i0, i1, t); + j0 = ((i0 & 0x7ff00000) >> 20) - 1023; + i0 &= 0x000fffff; + i0 |= 0x00100000; + /* After round: + * 64bit return: j0 in [20,52] */ + /* 64bit return: left shift amt in [0,32] */ + /* ***64bit return: right shift amt in [32,0] */ + result = lsl((long long int) i0, (j0 - 20)) + | SAFE_RIGHT_SHIFT (i1, (52 - j0)); + } + } + else + { + if (sizeof (long long) == 4 && (__float64) LLONG_MIN - _F_64(1.0) < x && x < (__float64) LLONG_MIN) { + if (nearbyint(x) == LLONG_MIN) + __math_set_inexact64(); + else + __math_set_invalid(); + return LLONG_MIN; + } + else if (x != LLONG_MIN) + { + __math_set_invalid(); + return sx ? LLONG_MIN : LLONG_MAX; + } + return (long long) x; + } + + + return sx ? -result : result; +} + +_MATH_ALIAS_k_d(llrint) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_llround.c b/src/audio/ffmpeg_dec/libm/common/s_llround.c new file mode 100644 index 000000000000..b875edea427f --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_llround.c @@ -0,0 +1,96 @@ +/* lround adapted to be llround for Newlib, 2009 by Craig Howland. */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" +#include + +#ifdef _NEED_FLOAT64 + +long long int +llround64(__float64 x) +{ + __int32_t sign, exponent_less_1023; + /* Most significant word, least significant word. */ + __uint32_t msw, lsw; + long long int result; + + EXTRACT_WORDS(msw, lsw, x); + + /* Extract sign. */ + sign = ((msw & 0x80000000) ? -1 : 1); + /* Extract exponent field. */ + exponent_less_1023 = ((msw & 0x7ff00000) >> 20) - 1023; + msw &= 0x000fffff; + msw |= 0x00100000; + + /* exponent_less_1023 in [-1023,1024] */ + if (exponent_less_1023 < 20) + { + /* exponent_less_1023 in [-1023,19] */ + if (exponent_less_1023 < 0) + { + if (exponent_less_1023 < -1) + return 0; + else + return sign; + } + else + { + /* exponent_less_1023 in [0,19] */ + /* shift amt in [0,19] */ + msw += 0x80000 >> exponent_less_1023; + /* shift amt in [20,1] */ + result = msw >> (20 - exponent_less_1023); + } + } + else if (exponent_less_1023 < (__int32_t) ((8 * sizeof (long long int)) - 1)) + { + /* 64bit longlong: exponent_less_1023 in [20,62] */ + if (exponent_less_1023 >= 52) + /* 64bit longlong: exponent_less_1023 in [52,62] */ + /* 64bit longlong: shift amt in [32,42] */ + result = ((long long int) msw << (exponent_less_1023 - 20)) + /* 64bit longlong: shift amt in [0,10] */ + | ((long long int) lsw << (exponent_less_1023 - 52)); + else + { + /* 64bit longlong: exponent_less_1023 in [20,51] */ + unsigned int tmp = lsw + /* 64bit longlong: shift amt in [0,31] */ + + (0x80000000 >> (exponent_less_1023 - 20)); + if (tmp < lsw) + ++msw; + /* 64bit longlong: shift amt in [0,31] */ + result = ((long long int) msw << (exponent_less_1023 - 20)) + /* ***64bit longlong: shift amt in [32,1] */ + | SAFE_RIGHT_SHIFT (tmp, (52 - exponent_less_1023)); + } + } + else + { + /* Result is too large to be represented by a long long int. */ + if (sign == 1 || + !((sizeof(long long) == 4 && x > LLONG_MIN - 0.5) || + (sizeof(long long) > 4 && x >= LLONG_MIN))) + { + __math_set_invalid(); + return sign == 1 ? LLONG_MAX : LLONG_MIN; + } + return (long long)x; + } + + return sign * result; +} + +_MATH_ALIAS_k_d(llround) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_log1p.c b/src/audio/ffmpeg_dec/libm/common/s_log1p.c new file mode 100644 index 000000000000..a9a2f272b3d5 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_log1p.c @@ -0,0 +1,200 @@ + +/* @(#)s_log1p.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* +FUNCTION +<>, <>---log of <<1 + <[x]>>> + +INDEX + log1p +INDEX + log1pf + +SYNOPSIS + #include + double log1p(double <[x]>); + float log1pf(float <[x]>); + +DESCRIPTION +<> calculates +@tex +$ln(1+x)$, +@end tex +the natural logarithm of <<1+<[x]>>>. You can use <> rather +than `<)>>' for greater precision when <[x]> is very +small. + +<> calculates the same thing, but accepts and returns +<> values rather than <>. + +RETURNS +<> returns a <>, the natural log of <<1+<[x]>>>. +<> returns a <>, the natural log of <<1+<[x]>>>. + +PORTABILITY +Neither <> nor <> is required by ANSI C or by the System V +Interface Definition (Issue 2). + +*/ + +/* double log1p(double x) + * + * Method : + * 1. Argument Reduction: find k and f such that + * 1+x = 2^k * (1+f), + * where sqrt(2)/2 < 1+f < sqrt(2) . + * + * Note. If k=0, then f=x is exact. However, if k!=0, then f + * may not be representable exactly. In that case, a correction + * term is need. Let u=1+x rounded. Let c = (1+x)-u, then + * log(1+x) - log(u) ~ c/u. Thus, we proceed to compute log(u), + * and add back the correction term c/u. + * (Note: when x > 2**53, one can simply return log(x)) + * + * 2. Approximation of log1p(f). + * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s) + * = 2s + 2/3 s**3 + 2/5 s**5 + ....., + * = 2s + s*R + * We use a special Remez algorithm on [0,0.1716] to generate + * a polynomial of degree 14 to approximate R The maximum error + * of this polynomial approximation is bounded by 2**-58.45. In + * other words, + * 2 4 6 8 10 12 14 + * R(z) ~ Lp1*s +Lp2*s +Lp3*s +Lp4*s +Lp5*s +Lp6*s +Lp7*s + * (the values of Lp1 to Lp7 are listed in the program) + * and + * | 2 14 | -58.45 + * | Lp1*s +...+Lp7*s - R(z) | <= 2 + * | | + * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2. + * In order to guarantee error in log below 1ulp, we compute log + * by + * log1p(f) = f - (hfsq - s*(hfsq+R)). + * + * 3. Finally, log1p(x) = k*ln2 + log1p(f). + * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo))) + * Here ln2 is split into two floating point number: + * ln2_hi + ln2_lo, + * where n*ln2_hi is always exact for |n| < 2000. + * + * Special cases: + * log1p(x) is NaN with signal if x < -1 (including -INF) ; + * log1p(+INF) is +INF; log1p(-1) is -INF with signal; + * log1p(NaN) is that NaN with no signal. + * + * Accuracy: + * according to an error analysis, the error is always less than + * 1 ulp (unit in the last place). + * + * Constants: + * The hexadecimal values are the intended ones for the following + * constants. The decimal values may be used, provided that the + * compiler will convert from decimal to binary accurately enough + * to produce the hexadecimal values shown. + * + * Note: Assuming log() return accurate answer, the following + * algorithm can be used to compute log1p(x) to within a few ULP: + * + * u = 1+x; + * if(u==1.0) return x ; else + * return log(u)*(x/(u-1.0)); + * + * See HP-15C Advanced Functions Handbook, p.193. + */ + +#include "fdlibm.h" +#include "math_config.h" + +#ifdef _NEED_FLOAT64 + +static const __float64 +ln2_hi = _F_64(6.93147180369123816490e-01), /* 3fe62e42 fee00000 */ +ln2_lo = _F_64(1.90821492927058770002e-10), /* 3dea39ef 35793c76 */ +Lp1 = _F_64(6.666666666666735130e-01), /* 3FE55555 55555593 */ +Lp2 = _F_64(3.999999999940941908e-01), /* 3FD99999 9997FA04 */ +Lp3 = _F_64(2.857142874366239149e-01), /* 3FD24924 94229359 */ +Lp4 = _F_64(2.222219843214978396e-01), /* 3FCC71C5 1D8E78AF */ +Lp5 = _F_64(1.818357216161805012e-01), /* 3FC74664 96CB03DE */ +Lp6 = _F_64(1.531383769920937332e-01), /* 3FC39A09 D078C69F */ +Lp7 = _F_64(1.479819860511658591e-01); /* 3FC2F112 DF3E5244 */ + +static const __float64 zero = _F_64(0.0); + +__float64 +log1p64(__float64 x) +{ + __float64 hfsq,f,c=0,s,z,R,u; + __int32_t k,hx,hu,ax; + + GET_HIGH_WORD(hx,x); + ax = hx&0x7fffffff; + + k = 1; + if (hx < 0x3FDA827A) { /* x < 0.41422 */ + if(ax>=0x3ff00000) { /* x <= -1.0 */ + if(x==_F_64(-1.0)) + return __math_divzero (1); /* log1p(-1)=-inf */ + else + return __math_invalid (x); /* log1p(x<-1)=NaN */ + } + if(ax<0x3e200000) { /* |x| < 2**-29 */ + if(ax<0x3c900000) /* |x| < 2**-54 */ + return __math_inexact64(x); + else + return __math_inexact64(x - x*x*_F_64(0.5)); + } + if(hx>0||hx<=((__int32_t)0xbfd2bec3)) { + k=0;f=x;hu=1;} /* -0.2929= 0x7ff00000) return x+x; + if(k!=0) { + if(hx<0x43400000) { + u = _F_64(1.0)+x; + GET_HIGH_WORD(hu,u); + k = (hu>>20)-1023; + c = (k>0)? _F_64(1.0)-(u-x):x-(u-_F_64(1.0));/* correction term */ + c /= u; + } else { + u = x; + GET_HIGH_WORD(hu,u); + k = (hu>>20)-1023; + c = 0; + } + hu &= 0x000fffff; + if(hu<0x6a09e) { + SET_HIGH_WORD(u,hu|0x3ff00000); /* normalize u */ + } else { + k += 1; + SET_HIGH_WORD(u,hu|0x3fe00000); /* normalize u/2 */ + hu = (0x00100000-hu)>>2; + } + f = u - _F_64(1.0); + } + hfsq=_F_64(0.5)*f*f; + if(hu==0) { /* |f| < 2**-20 */ + if(f==zero) { if(k==0) return zero; + else {c += k*ln2_lo; return k*ln2_hi+c;}} + R = hfsq*(_F_64(1.0)-_F_64(0.66666666666666666)*f); + if(k==0) return f-R; else + return k*ln2_hi-((R-(k*ln2_lo+c))-f); + } + s = f/(_F_64(2.0)+f); + z = s*s; + R = z*(Lp1+z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7)))))); + if(k==0) return f-(hfsq-s*(hfsq+R)); else + return k*ln2_hi-((hfsq-(s*(hfsq+R)+(k*ln2_lo+c)))-f); +} + +_MATH_ALIAS_d_d(log1p) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_log2.c b/src/audio/ffmpeg_dec/libm/common/s_log2.c new file mode 100644 index 000000000000..8d8484cdfab7 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_log2.c @@ -0,0 +1,82 @@ +/* @(#)s_log2.c 5.1 93/09/24 */ +/* Modification from s_exp10.c Yaakov Selkowitz 2009. */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* +FUNCTION + <>, <>---base 2 logarithm +INDEX + log2 +INDEX + log2f + +SYNOPSIS + #include + double log2(double <[x]>); + float log2f(float <[x]>); + +DESCRIPTION +The <> functions compute the base-2 logarithm of <[x]>. A domain error +occurs if the argument is less than zero. A range error occurs if the +argument is zero. + +The Newlib implementations are not full, intrinisic calculations, but +rather are derivatives based on <>. (Accuracy might be slightly off from +a direct calculation.) In addition to functions, they are also implemented as +macros defined in math.h: +. #define log2(x) (log (x) / _M_LN2) +. #define log2f(x) (logf (x) / (float) _M_LN2) +To use the functions instead, just undefine the macros first. + +RETURNS +The <> functions return +@ifnottex +<)>> +@end ifnottex +@tex +$log_2(x)$ +@end tex +on success. +When <[x]> is zero, the +returned value is <<-HUGE_VAL>> and <> is set to <>. +When <[x]> is negative, the returned value is NaN (not a number) and +<> is set to <>. + +PORTABILITY +C99, POSIX, System V Interface Definition (Issue 6). +*/ + +/* + * wrapper log2(x) + */ + +#include "fdlibm.h" +#if __OBSOLETE_MATH_DOUBLE +#include +#include +#undef log2 + +#ifdef _NEED_FLOAT64 + +__float64 +log264(__float64 x) /* wrapper log2 */ +{ + return (log64(x) / _F_64(_M_LN2)); +} + +_MATH_ALIAS_d_d(log2) + +#endif /* _NEED_FLOAT64 */ +#else +#include "log2.c" +#endif /* __OBSOLETE_MATH_DOUBLE */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_logb.c b/src/audio/ffmpeg_dec/libm/common/s_logb.c new file mode 100644 index 000000000000..f8a3aa1567f2 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_logb.c @@ -0,0 +1,105 @@ +/* 2009 for Newlib: Sun's s_ilogb.c converted to be s_logb.c. */ +/* @(#)s_ilogb.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ +/* +FUNCTION + <>, <>---get exponent of floating-point number +INDEX + logb +INDEX + logbf + +SYNOPSIS + #include + double logb(double <[x]>); + float logbf(float <[x]>); + +DESCRIPTION +The <> functions extract the exponent of <[x]>, as a signed integer value +in floating-point format. If <[x]> is subnormal it is treated as though it were +normalized; thus, for positive finite <[x]>, +@ifnottex +1 <= (<[x]> * FLT_RADIX to the power (-logb(<[x]>))) < FLT_RADIX. +@end ifnottex +@tex +$1 \leq ( x \cdot FLT\_RADIX ^ {-logb(x)} ) < FLT\_RADIX$. +@end tex +A domain error may occur if the argument is zero. +In this floating-point implementation, FLT_RADIX is 2. Which also means +that for finite <[x]>, <>(<[x]>) = <>(<>(<>(<[x]>))). + +All nonzero, normal numbers can be described as +@ifnottex +<[m]> * 2**<[p]>, where 1.0 <= <[m]> < 2.0. +@end ifnottex +@tex +$m \cdot 2^p$, where $1.0 \leq m < 2.0$. +@end tex +The <> functions examine the argument <[x]>, and return <[p]>. +The <> functions are similar to the <> functions, but +returning <[m]> adjusted to the interval [.5, 1) or 0, and <[p]>+1. + +RETURNS +@comment Formatting note: "$@" forces a new line +When <[x]> is:@* ++inf or -inf, +inf is returned;@* +NaN, NaN is returned;@* +0, -inf is returned, and the divide-by-zero exception is raised;@* +otherwise, the <> functions return the signed exponent of <[x]>. + +PORTABILITY +ANSI C, POSIX + +SEEALSO +frexp, ilogb +*/ + +/* double logb(double x) + * return the binary exponent of non-zero x + * logb(0) = -inf, raise divide-by-zero floating point exception + * logb(+inf|-inf) = +inf (no signal is raised) + * logb(NaN) = NaN (no signal is raised) + * Per C99 recommendation, a NaN argument is returned unchanged. + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +__float64 +logb64(__float64 x) +{ + __int32_t hx,lx,ix; + + EXTRACT_WORDS(hx,lx,x); + hx &= 0x7fffffff; /* high |x| */ + if(hx<0x00100000) { /* 0 or subnormal */ + if((hx|lx)==0) { + /* arg==0: return -inf and raise divide-by-zero exception */ + return -1./fabs64(x); /* logb(0) = -inf */ + } + else /* subnormal x */ + if(hx==0) { + for (ix = -1043; lx>0; lx = lsl(lx, 1)) ix -=1; + } else { + for (ix = -1022, hx = lsl(hx, 11); hx>0; hx = lsl(hx, 1)) ix -=1; + } + return (__float64) ix; + } + else if (hx<0x7ff00000) return (hx>>20)-1023; /* normal # */ + else if (hx>0x7ff00000 || lx) return x+x; /* x==NaN */ + else return HUGE_VAL; /* x==inf (+ or -) */ +} + +_MATH_ALIAS_d_d(logb) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_lrint.c b/src/audio/ffmpeg_dec/libm/common/s_lrint.c new file mode 100644 index 000000000000..8aa2e2e84898 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_lrint.c @@ -0,0 +1,175 @@ + +/* @(#)s_lrint.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ +/* +FUNCTION +<>, <>, <>, <>---round to integer +INDEX + lrint +INDEX + lrintf +INDEX + llrint +INDEX + llrintf + +SYNOPSIS + #include + long int lrint(double <[x]>); + long int lrintf(float <[x]>); + long long int llrint(double <[x]>); + long long int llrintf(float <[x]>); + +DESCRIPTION +The <> and <> functions round their argument to the nearest +integer value, using the current rounding direction. If the rounded value is +outside the range of the return type, the numeric result is unspecified. A +range error may occur if the magnitude of <[x]> is too large. +The "inexact" floating-point exception is raised in implementations that +support it when the result differs in value from the argument (i.e., when +a fraction actually has been truncated). + +RETURNS +<[x]> rounded to an integral value, using the current rounding direction. + +SEEALSO +<> + +PORTABILITY +ANSI C, POSIX + +*/ + +/* + * lrint(x) + * Return x rounded to integral value according to the prevailing + * rounding mode. + * Method: + * Using floating addition. + * Exception: + * Inexact flag raised if x not equal to lrint(x). + */ + +#include "fdlibm.h" +#include + +#ifdef _NEED_FLOAT64 + +static const __float64 + +/* Adding a double, x, to 2^52 will cause the result to be rounded based on + the fractional part of x, according to the implementation's current rounding + mode. 2^52 is the smallest double that can be represented using all 52 significant + digits. */ +TWO52[2]={ + _F_64(4.50359962737049600000e+15), /* 0x43300000, 0x00000000 */ + _F_64(-4.50359962737049600000e+15), /* 0xC3300000, 0x00000000 */ +}; + +#include +long int +lrint64(__float64 x) +{ + __int32_t i0,j0,sx; + __uint32_t i1; + __float64 t; + volatile __float64 w; + long int result; + + EXTRACT_WORDS(i0,i1,x); + + /* Extract sign bit. */ + sx = (i0>>31)&1; + + /* Extract exponent field. */ + j0 = ((i0 & 0x7ff00000) >> 20) - 1023; + /* j0 in [-1023,1024] */ + + if(j0 < 20) + { + /* j0 in [-1023,19] */ + /* shift amt in [0,19] */ + w = TWO52[sx] + x; + t = w - TWO52[sx]; + GET_HIGH_WORD(i0, t); + /* After round: j0 in [0,20] */ + j0 = ((i0 & 0x7ff00000) >> 20) - 1023; + i0 &= 0x000fffff; + i0 |= 0x00100000; + /* shift amt in [20,0] */ + if (j0 < 0) + result = 0; + else + result = i0 >> (20 - j0); + } + else if (j0 < (int)(8 * sizeof (long int)) - 1) + { + /* 32bit return: j0 in [20,30] */ + /* 64bit return: j0 in [20,62] */ + if (j0 >= 52) + /* 64bit return: j0 in [52,62] */ + /* 64bit return: left shift amt in [32,42] */ + result = lsl((long int) ((i0 & 0x000fffff) | 0x00100000), (j0 - 20)) | + /* 64bit return: right shift amt in [0,10] */ + lsl((long int) i1, (j0 - 52)); + else + { + if (sizeof (long) == 4 && x > LONG_MAX) { + t = nearbyint(x); + if (t == LONG_MAX) + __math_set_inexact64(); + else + __math_set_invalid(); + } else { + /* 32bit return: j0 in [20,30] */ + /* 64bit return: j0 in [20,51] */ + w = TWO52[sx] + x; + t = w - TWO52[sx]; + } + EXTRACT_WORDS (i0, i1, t); + j0 = ((i0 & 0x7ff00000) >> 20) - 1023; + i0 &= 0x000fffff; + i0 |= 0x00100000; + /* After round: + * 32bit return: j0 in [20,31]; + * 64bit return: j0 in [20,52] */ + /* 32bit return: left shift amt in [0,11] */ + /* 64bit return: left shift amt in [0,32] */ + /* ***32bit return: right shift amt in [32,21] */ + /* ***64bit return: right shift amt in [32,0] */ + result = (lsl((long int) i0, (j0 - 20))) + | SAFE_RIGHT_SHIFT (i1, (52 - j0)); + } + } + else + { + if (sizeof (long) == 4 && (__float64) LONG_MIN - _F_64(1.0) < x && x < (__float64) LONG_MIN) { + if (nearbyint(x) == LONG_MIN) + __math_set_inexact64(); + else + __math_set_invalid(); + return LONG_MIN; + } + else if (x != LONG_MIN) + { + __math_set_invalid(); + return sx ? LONG_MIN : LONG_MAX; + } + return (long int) x; + } + + return sx ? -result : result; +} + +_MATH_ALIAS_j_d(lrint) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_lround.c b/src/audio/ffmpeg_dec/libm/common/s_lround.c new file mode 100644 index 000000000000..30739aabd1eb --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_lround.c @@ -0,0 +1,140 @@ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ +/* +FUNCTION +<>, <>, <>, <>---round to integer, to nearest +INDEX + lround +INDEX + lroundf +INDEX + llround +INDEX + llroundf + +SYNOPSIS + #include + long int lround(double <[x]>); + long int lroundf(float <[x]>); + long long int llround(double <[x]>); + long long int llroundf(float <[x]>); + +DESCRIPTION + The <> and <> functions round their argument to the + nearest integer value, rounding halfway cases away from zero, regardless + of the current rounding direction. If the rounded value is outside the + range of the return type, the numeric result is unspecified (depending + upon the floating-point implementation, not the library). A range + error may occur if the magnitude of x is too large. + +RETURNS +<[x]> rounded to an integral value as an integer. + +SEEALSO +See the <> functions for the return being the same floating-point type +as the argument. <>, <>. + +PORTABILITY +ANSI C, POSIX + +*/ + +#include "fdlibm.h" +#include + +#ifdef _NEED_FLOAT64 + +long int +lround64(__float64 x) +{ + __int32_t sign, exponent_less_1023; + /* Most significant word, least significant word. */ + __uint32_t msw, lsw; + long int result; + + EXTRACT_WORDS(msw, lsw, x); + + /* Extract sign. */ + sign = ((msw & 0x80000000) ? -1 : 1); + /* Extract exponent field. */ + exponent_less_1023 = ((msw & 0x7ff00000) >> 20) - 1023; + msw &= 0x000fffff; + msw |= 0x00100000; + /* exponent_less_1023 in [-1023,1024] */ + if (exponent_less_1023 < 20) + { + /* exponent_less_1023 in [-1023,19] */ + if (exponent_less_1023 < 0) + { + if (exponent_less_1023 < -1) + return 0; + else + return sign; + } + else + { + /* exponent_less_1023 in [0,19] */ + /* shift amt in [0,19] */ + msw += 0x80000 >> exponent_less_1023; + /* shift amt in [20,1] */ + result = msw >> (20 - exponent_less_1023); + } + } + else if (exponent_less_1023 < (__int32_t) ((8 * sizeof (long int)) - 1)) + { + /* 32bit long: exponent_less_1023 in [20,30] */ + /* 64bit long: exponent_less_1023 in [20,62] */ + if (exponent_less_1023 >= 52) + /* 64bit long: exponent_less_1023 in [52,62] */ + /* 64bit long: shift amt in [32,42] */ + result = lsl((long int) msw, (exponent_less_1023 - 20)) + /* 64bit long: shift amt in [0,10] */ + | lsl((long int) lsw, (exponent_less_1023 - 52)); + else + { + /* 32bit long: exponent_less_1023 in [20,30] */ + /* 64bit long: exponent_less_1023 in [20,51] */ + __uint32_t tmp = lsw + /* 32bit long: shift amt in [0,10] */ + /* 64bit long: shift amt in [0,31] */ + + (0x80000000 >> (exponent_less_1023 - 20)); + if (tmp < lsw) + ++msw; + /* 32bit long: shift amt in [0,10] */ + /* 64bit long: shift amt in [0,31] */ + result = lsl((long int) msw, (exponent_less_1023 - 20)) + /* ***32bit long: shift amt in [32,22] */ + /* ***64bit long: shift amt in [32,1] */ + | SAFE_RIGHT_SHIFT (tmp, (52 - exponent_less_1023)); + } + } + else + { + /* Result is too large to be represented by a long int. */ + if (sign == 1 || + !((sizeof(long) == 4 && x > LONG_MIN - _F_64(0.5)) || + (sizeof(long) > 4 && x >= LONG_MIN))) + { + __math_set_invalid(); + return sign == 1 ? LONG_MAX : LONG_MIN; + } + return (long int)x; + } + + if (sizeof (long) == 4 && sign == 1 && result == LONG_MIN) + __math_set_invalid(); + + return sign * result; +} + +_MATH_ALIAS_j_d(lround) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_modf.c b/src/audio/ffmpeg_dec/libm/common/s_modf.c new file mode 100644 index 000000000000..4e2fa11d39ae --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_modf.c @@ -0,0 +1,108 @@ + +/* @(#)s_modf.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* +FUNCTION + <>, <>---split fractional and integer parts + +INDEX + modf +INDEX + modff + +SYNOPSIS + #include + double modf(double <[val]>, double *<[ipart]>); + float modff(float <[val]>, float *<[ipart]>); + +DESCRIPTION + <> splits the double <[val]> apart into an integer part + and a fractional part, returning the fractional part and + storing the integer part in <<*<[ipart]>>>. No rounding + whatsoever is done; the sum of the integer and fractional + parts is guaranteed to be exactly equal to <[val]>. That + is, if <[realpart]> = modf(<[val]>, &<[intpart]>); then + `<<<[realpart]>+<[intpart]>>>' is the same as <[val]>. + <> is identical, save that it takes and returns + <> rather than <> values. + +RETURNS + The fractional part is returned. Each result has the same + sign as the supplied argument <[val]>. + +PORTABILITY + <> is ANSI C. <> is an extension. + +QUICKREF + modf ansi pure + modff - pure + +*/ + +/* + * modf(double x, double *iptr) + * return fraction part of x, and return x's integral part in *iptr. + * Method: + * Bit twiddling. + * + * Exception: + * No exception. + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +__float64 +modf64(__float64 x, __float64 *iptr) +{ + __int32_t i0,i1,j0; + __uint32_t i; + EXTRACT_WORDS(i0,i1,x); + j0 = ((i0>>20)&0x7ff)-0x3ff; /* exponent of x */ + if(j0<20) { /* integer part in high x */ + if(j0<0) { /* |x|<1 */ + INSERT_WORDS(*iptr,i0&0x80000000,0); /* *iptr = +-0 */ + return x; + } else { + i = (0x000fffff)>>j0; + if(((i0&i)|i1)==0) { /* x is integral */ + *iptr = x; + INSERT_WORDS(x,i0&0x80000000,0); /* return +-0 */ + return x; + } else { + INSERT_WORDS(*iptr,i0&(~i),0); + return x - *iptr; + } + } + } else if (j0>51) { /* no fraction part */ + *iptr = x; + if (__fpclassifyd(x) == FP_NAN) return *iptr = x+x; /* x is NaN, return NaN */ + INSERT_WORDS(x,i0&0x80000000,0); /* return +-0 */ + return x; + } else { /* fraction part in low x */ + i = ((__uint32_t)(0xffffffff))>>(j0-20); + if((i1&i)==0) { /* x is integral */ + *iptr = x; + INSERT_WORDS(x,i0&0x80000000,0); /* return +-0 */ + return x; + } else { + INSERT_WORDS(*iptr,i0,i1&(~i)); + return x - *iptr; + } + } +} + +_MATH_ALIAS_d_dD(modf) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_nan.c b/src/audio/ffmpeg_dec/libm/common/s_nan.c new file mode 100644 index 000000000000..93a5aa18b491 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_nan.c @@ -0,0 +1,59 @@ +/* +Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + +Developed at SunPro, a Sun Microsystems, Inc. business. +Permission to use, copy, modify, and distribute this +software is freely granted, provided that this notice +is preserved. + */ +/* + * nan () returns a nan. + * Added by Cygnus Support. + */ + +/* +FUNCTION + <>, <>---representation of ``Not a Number'' + +INDEX + nan +INDEX + nanf + +SYNOPSIS + #include + double nan(const char *<[unused]>); + float nanf(const char *<[unused]>); + + +DESCRIPTION + <> and <> return an IEEE NaN (Not a Number) in + double- and single-precision arithmetic respectively. The + argument is currently disregarded. + +QUICKREF + nan - pure + +*/ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +__float64 +nan64(const char *unused) +{ + __float64 x; + + (void) unused; +#if __GNUC_PREREQ (3, 3) + x = __builtin_nan(""); +#else + INSERT_WORDS(x,0x7ff80000,0); +#endif + return x; +} + +_MATH_ALIAS_d_s(nan) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_nearbyint.c b/src/audio/ffmpeg_dec/libm/common/s_nearbyint.c new file mode 100644 index 000000000000..59fffc84ddf4 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_nearbyint.c @@ -0,0 +1,71 @@ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ +/* +FUNCTION +<>, <>---round to integer +INDEX + nearbyint +INDEX + nearbyintf + +SYNOPSIS + #include + double nearbyint(double <[x]>); + float nearbyintf(float <[x]>); + +DESCRIPTION +The <> functions round their argument to an integer value in +floating-point format, using the current rounding direction and +(supposedly) without raising the "inexact" floating-point exception. +See the <> functions for the same function with the "inexact" +floating-point exception being raised when appropriate. + +BUGS +Newlib does not support the floating-point exception model, so that +the floating-point exception control is not present and thereby what may +be seen will be compiler and hardware dependent in this regard. +The Newlib <> functions are identical to the <> +functions with respect to the floating-point exception behavior, and +will cause the "inexact" exception to be raised for most targets. + +RETURNS +<[x]> rounded to an integral value, using the current rounding direction. + +PORTABILITY +ANSI C, POSIX + +SEEALSO +<>, <> +*/ + +#include +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +__float64 +nearbyint64(__float64 x) +{ + if (isnan(x)) return x + x; +#if defined(FE_INEXACT) && !defined(__DOUBLE_NOEXCEPT) + fenv_t env; + fegetenv(&env); +#endif + x = rint64(x); +#if defined(FE_INEXACT) && !defined(__DOUBLE_NOEXCEPT) + fesetenv(&env); +#endif + return x; +} + +_MATH_ALIAS_d_d(nearbyint) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_nextafter.c b/src/audio/ffmpeg_dec/libm/common/s_nextafter.c new file mode 100644 index 000000000000..87bb27a68abb --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_nextafter.c @@ -0,0 +1,103 @@ + +/* @(#)s_nextafter.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* +FUNCTION + <>, <>---get next number + +INDEX + nextafter +INDEX + nextafterf + +SYNOPSIS + #include + double nextafter(double <[val]>, double <[dir]>); + float nextafterf(float <[val]>, float <[dir]>); + +DESCRIPTION +<> returns the double-precision floating-point number +closest to <[val]> in the direction toward <[dir]>. <> +performs the same operation in single precision. For example, +<> returns the smallest positive number which is +representable in double precision. + +RETURNS +Returns the next closest number to <[val]> in the direction toward +<[dir]>. + +PORTABILITY + Neither <> nor <> is required by ANSI C + or by the System V Interface Definition (Issue 2). +*/ + +/* IEEE functions + * nextafter(x,y) + * return the next machine floating-point number of x in the + * direction toward y. + * Special cases: + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +__float64 +nextafter64(__float64 x, __float64 y) +{ + __int32_t hx,hy,ix,iy; + __uint32_t lx,ly; + + EXTRACT_WORDS(hx,lx,x); + EXTRACT_WORDS(hy,ly,y); + ix = hx&0x7fffffff; /* |x| */ + iy = hy&0x7fffffff; /* |y| */ + + if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) || /* x is nan */ + ((iy>=0x7ff00000)&&((iy-0x7ff00000)|ly)!=0)) /* y is nan */ + return x+y; + if(x==y) return y; /* x=y, return y */ + if((ix|lx)==0) { /* x == 0 */ + INSERT_WORDS(x,hy&0x80000000,1); /* return +-minsubnormal */ + force_eval_float64(opt_barrier_float64(x)*x); + return x; + } + if(hx>=0) { /* x > 0 */ + if(hx>hy||((hx==hy)&&(lx>ly))) { /* x > y, x -= ulp */ + if(lx==0) hx -= 1; + lx -= 1; + } else { /* x < y, x += ulp */ + lx += 1; + if(lx==0) hx += 1; + } + } else { /* x < 0 */ + if(hy>=0||hx>hy||((hx==hy)&&(lx>ly))){/* x < y, x -= ulp */ + if(lx==0) hx -= 1; + lx -= 1; + } else { /* x > y, x += ulp */ + lx += 1; + if(lx==0) hx += 1; + } + } + hy = hx&0x7ff00000; + if(hy>=0x7ff00000) + return __math_oflow(hx<0); /* overflow */ + INSERT_WORDS(x,hx,lx); + if(hy<0x00100000) /* underflow */ + return __math_denorm(x); + return (x); +} + +_MATH_ALIAS_d_dd(nextafter) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_pow10.c b/src/audio/ffmpeg_dec/libm/common/s_pow10.c new file mode 100644 index 000000000000..0e356cfd1157 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_pow10.c @@ -0,0 +1,66 @@ +/* @(#)s_pow10.c 5.1 93/09/24 */ +/* Modification from s_exp10.c Yaakov Selkowitz 2007. */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* +FUNCTION + <>, <>---base 10 power functions +INDEX + pow10 +INDEX + pow10f + +SYNOPSIS + #include + double pow10(double <[x]>); + float pow10f(float <[x]>); + +DESCRIPTION + <> and <> calculate 10 ^ <[x]>, that is, + @ifnottex + 10 raised to the power <[x]>. + @end ifnottex + @tex + $10^x$ + @end tex + +RETURNS + On success, <> and <> return the calculated value. + If the result underflows, the returned value is <<0>>. If the + result overflows, the returned value is <>. In + either case, <> is set to <>. + +PORTABILITY + <> and <> are GNU extensions. +*/ + +/* + * wrapper pow10(x) + */ + +#define _GNU_SOURCE +#undef pow10 +#include "fdlibm.h" +#include +#include + +#ifdef _NEED_FLOAT64 + +__float64 pow1064(__float64 x) /* wrapper pow10 */ +{ + return _pow64(_F_64(10.0), x); +} + +_MATH_ALIAS_d_d(pow10) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_remquo.c b/src/audio/ffmpeg_dec/libm/common/s_remquo.c new file mode 100644 index 000000000000..6c41a3b3dd04 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_remquo.c @@ -0,0 +1,215 @@ +/* Adapted for Newlib, 2009. (Allow for int < 32 bits; return *quo=0 during + * errors to make test scripts easier.) */ +/* @(#)e_fmod.c 1.3 95/01/18 */ +/*- + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunSoft, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ +/* +FUNCTION +<>, <>---remainder and part of quotient +INDEX + remquo +INDEX + remquof + +SYNOPSIS + #include + double remquo(double <[x]>, double <[y]>, int *<[quo]>); + float remquof(float <[x]>, float <[y]>, int *<[quo]>); + +DESCRIPTION +The <> functions compute the same remainder as the <> +functions; this value is in the range -<[y]>/2 ... +<[y]>/2. In the object +pointed to by <> they store a value whose sign is the sign of <>/<> +and whose magnitude is congruent modulo 2**n to the magnitude of the integral +quotient of <>/<>. (That is, <> is given the n lsbs of the +quotient, not counting the sign.) This implementation uses n=31 if int is 32 +bits or more, otherwise, n is 1 less than the width of int. + +For example: +. remquo(-29.0, 3.0, &<[quo]>) +returns -1.0 and sets <[quo]>=10, and +. remquo(-98307.0, 3.0, &<[quo]>) +returns -0.0 and sets <[quo]>=-32769, although for 16-bit int, <[quo]>=-1. In +the latter case, the actual quotient of -(32769=0x8001) is reduced to -1 +because of the 15-bit limitation for the quotient. + +RETURNS +When either argument is NaN, NaN is returned. If <[y]> is 0 or <[x]> is +infinite (and neither is NaN), a domain error occurs (i.e. the "invalid" +floating point exception is raised or errno is set to EDOM), and NaN is +returned. +Otherwise, the <> functions return <[x]> REM <[y]>. + +BUGS +IEEE754-2008 calls for <>(subnormal, inf) to cause the "underflow" +floating-point exception. This implementation does not. + +PORTABILITY +C99, POSIX. + +*/ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +#include +#include + +/* For quotient, return either all 31 bits that can from calculation (using + * int32_t), or as many as can fit into an int that is smaller than 32 bits. */ +#if INT_MAX > 0x7FFFFFFFL + #define QUO_MASK 0x7FFFFFFF +# else + #define QUO_MASK INT_MAX +#endif + +static const __float64 Zero[] = {_F_64(0.0), _F_64(-0.0),}; + +/* + * Return the IEEE remainder and set *quo to the last n bits of the + * quotient, rounded to the nearest integer. We choose n=31--if that many fit-- + * because we wind up computing all the integer bits of the quotient anyway as + * a side-effect of computing the remainder by the shift and subtract + * method. In practice, this is far more bits than are needed to use + * remquo in reduction algorithms. + */ +__float64 +remquo64(__float64 x, __float64 y, int *quo) +{ + __int32_t n,hx,hy,hz,ix,iy,sx,i; + __uint32_t lx,ly,lz,q,sxy; + + EXTRACT_WORDS(hx,lx,x); + EXTRACT_WORDS(hy,ly,y); + sxy = (hx ^ hy) & 0x80000000; + sx = hx&0x80000000; /* sign of x */ + hx ^=sx; /* |x| */ + hy &= 0x7fffffff; /* |y| */ + + /* purge off exception values */ + if((hy|ly)==0||(hx>=0x7ff00000)|| /* y=0,or x not finite */ + ((hy|((ly|-ly)>>31))>0x7ff00000)) { /* or y is NaN */ + *quo = 0; /* Not necessary, but return consistent value */ + return (x*y)/(x*y); + } + if(hx<=hy) { + if((hx>31]; /* |x|=|y| return x*0 */ + } + } + + /* determine ix = ilogb(x) */ + if(hx<0x00100000) { /* subnormal x */ + if(hx==0) { + for (ix = -1043, i=lx; i>0; i = lsl(i, 1)) ix -=1; + } else { + for (ix = -1022,i=lsl(hx, 11); i>0; i = lsl(i, 1)) ix -=1; + } + } else ix = (hx>>20)-1023; + + /* determine iy = ilogb(y) */ + if(hy<0x00100000) { /* subnormal y */ + if(hy==0) { + for (iy = -1043, i=ly; i>0; i = lsl(i, 1)) iy -=1; + } else { + for (iy = -1022,i= lsl(hy, 11); i>0; i = lsl(i, 1)) iy -=1; + } + } else iy = (hy>>20)-1023; + + /* set up {hx,lx}, {hy,ly} and align y to x */ + if(ix >= -1022) + hx = 0x00100000|(0x000fffff&hx); + else { /* subnormal x, shift x to normal */ + n = -1022-ix; + if(n<=31) { + hx = lsl(hx, n)|(lx>>(32-n)); + lx <<= n; + } else { + hx = lx<<(n-32); + lx = 0; + } + } + if(iy >= -1022) + hy = 0x00100000|(0x000fffff&hy); + else { /* subnormal y, shift y to normal */ + n = -1022-iy; + if(n<=31) { + hy = lsl(hy, n)|(ly>>(32-n)); + ly <<= n; + } else { + hy = ly<<(n-32); + ly = 0; + } + } + + /* fix point fmod */ + n = ix - iy; + q = 0; + while(n--) { + hz=hx-hy;lz=lx-ly; if(lx>31); lx = lx+lx;} + else {hx = hz+hz+(lz>>31); lx = lz+lz; q++;} + q <<= 1; + } + hz=hx-hy;lz=lx-ly; if(lx=0) {hx=hz;lx=lz;q++;} + + /* convert back to floating value and restore the sign */ + if((hx|lx)==0) { /* return sign(x)*0 */ + q &= QUO_MASK; + *quo = (sxy ? -q : q); + return Zero[(__uint32_t)sx>>31]; + } + while(hx<0x00100000) { /* normalize x */ + hx = hx+hx+(lx>>31); lx = lx+lx; + iy -= 1; + } + if(iy>= -1022) { /* normalize output */ + hx = ((hx-0x00100000)|lsl((iy+1023), 20)); + } else { /* subnormal output */ + n = -1022 - iy; + if(n<=20) { + lx = (lx>>n)|((__uint32_t)hx<<(32-n)); + hx >>= n; + } else if (n<=31) { + lx = (hx<<(32-n))|(lx>>n); hx = sx; + } else { + lx = hx>>(n-32); hx = sx; + } + } +fixup: + INSERT_WORDS(x,hx,lx); + y = fabs64(y); + if (y < _F_64(0x1p-1021)) { + if (x+x>y || (x+x==y && (q & 1))) { + q++; + x-=y; + } + } else if (x>_F_64(0.5)*y || (x==_F_64(0.5)*y && (q & 1))) { + q++; + x-=y; + } + GET_HIGH_WORD(hx,x); + SET_HIGH_WORD(x,hx^sx); + q &= QUO_MASK; + *quo = (sxy ? -q : q); + return x; +} + +_MATH_ALIAS_d_ddI(remquo) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_rint.c b/src/audio/ffmpeg_dec/libm/common/s_rint.c new file mode 100644 index 000000000000..32fe9798c7bc --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_rint.c @@ -0,0 +1,132 @@ + +/* @(#)s_rint.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ +/* +FUNCTION +<>, <>---round to integer +INDEX + rint +INDEX + rintf + +SYNOPSIS + #include + double rint(double <[x]>); + float rintf(float <[x]>); + +DESCRIPTION + The <> functions round their argument to an integer value in + floating-point format, using the current rounding direction. They + raise the "inexact" floating-point exception if the result differs + in value from the argument. See the <> functions for the + same function with the "inexact" floating-point exception never being + raised. Newlib does not directly support floating-point exceptions. + The <> functions are written so that the "inexact" exception is + raised in hardware implementations that support it, even though Newlib + does not provide access. + +RETURNS +<[x]> rounded to an integral value, using the current rounding direction. + +PORTABILITY +ANSI C, POSIX + +SEEALSO +<>, <> + +*/ + +/* + * rint(x) + * Return x rounded to integral value according to the prevailing + * rounding mode. + * Method: + * Using floating addition. + * Whenever a fraction is present, if the second or any following bit after + * the radix point is set, limit to the second radix point to avoid + * possible double rounding in the TWO52 +- steps (in case guard bits are + * used). Specifically, if have any, chop off bits past the 2nd place and + * set the second place. + * (e.g. 2.0625=0b10.0001 => 0b10.01=2.25; + * 2.3125=0b10.011 => 0b10.01=2.25; + * 1.5625= 0b1.1001 => 0b1.11=1.75; + * 1.9375= 0b1.1111 => 0b1.11=1.75. + * Pseudo-code: if(x.frac & ~0b0.10) x.frac = (x.frac & 0b0.11) | 0b0.01;). + * Exception: + * Inexact flag raised if x not equal to rint(x). + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +static const __float64 +TWO52[2]={ + _F_64(4.50359962737049600000e+15), /* 0x43300000, 0x00000000 */ + _F_64(-4.50359962737049600000e+15), /* 0xC3300000, 0x00000000 */ +}; + +__float64 +rint64(__float64 x) +{ + __int32_t i0,j0,sx; + __uint32_t i,i1; + __float64 t; + volatile __float64 w; + EXTRACT_WORDS(i0,i1,x); + sx = (i0>>31)&1; /* sign */ + j0 = ((i0>>20)&0x7ff)-0x3ff; /* exponent */ + if(j0<20) { /* no integral bits in LS part */ + if(j0<0) { /* x is fractional or 0 */ + if(((i0&0x7fffffff)|i1)==0) return x; /* x == 0 */ + i1 |= (i0&0x0fffff); + i0 &= 0xfffe0000; + i0 |= ((i1|-i1)>>12)&0x80000; + SET_HIGH_WORD(x,i0); + w = TWO52[sx]+x; + t = w-TWO52[sx]; + GET_HIGH_WORD(i0,t); + SET_HIGH_WORD(t,(i0&0x7fffffff)|lsl(sx, 31)); + return t; + } else { /* x has integer and maybe fraction */ + i = (0x000fffff)>>j0; + if(((i0&i)|i1)==0) return x; /* x is integral */ + i>>=1; + if(((i0&i)|i1)!=0) { + /* 2nd or any later bit after radix is set */ + if(j0==19) i1 = 0x80000000; else i1 = 0; + i0 = (i0&(~i))|((0x40000)>>j0); + } + } + } else if (j0>51) { + /* + * Use barrier to avoid overflow on clang which would + * otherwise always do this add on arm and use a + * conditional move instead of a branch for the if + */ + if (j0 == 0x400) + return opt_barrier_double(x+x); + return x; + } else { + i = ((__uint32_t)(0xffffffff))>>(j0-20); + if((i1&i)==0) return x; /* x is integral */ + i>>=1; + if((i1&i)!=0) i1 = (i1&(~i))|((0x40000000)>>(j0-20)); + } + INSERT_WORDS(x,i0,i1); + w = TWO52[sx]+x; + return w-TWO52[sx]; +} + +_MATH_ALIAS_d_d(rint) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_round.c b/src/audio/ffmpeg_dec/libm/common/s_round.c new file mode 100644 index 000000000000..c2eaebbfa39f --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_round.c @@ -0,0 +1,113 @@ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ +/* +FUNCTION +<>, <>---round to integer, to nearest +INDEX + round +INDEX + roundf + +SYNOPSIS + #include + double round(double <[x]>); + float roundf(float <[x]>); + +DESCRIPTION + The <> functions round their argument to the nearest integer + value in floating-point format, rounding halfway cases away from zero, + regardless of the current rounding direction. (While the "inexact" + floating-point exception behavior is unspecified by the C standard, the + <> functions are written so that "inexact" is not raised if the + result does not equal the argument, which behavior is as recommended by + IEEE 754 for its related functions.) + +RETURNS +<[x]> rounded to an integral value. + +PORTABILITY +ANSI C, POSIX + +SEEALSO +<>, <> + +*/ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +__float64 +round64(__float64 x) +{ + /* Most significant word, least significant word. */ + __int32_t msw, exponent_less_1023; + __uint32_t lsw; + + EXTRACT_WORDS(msw, lsw, x); + + /* Extract exponent field. */ + exponent_less_1023 = ((msw & 0x7ff00000) >> 20) - 1023; + + if (exponent_less_1023 < 20) + { + if (exponent_less_1023 < 0) + { + msw &= 0x80000000; + if (exponent_less_1023 == -1) + /* Result is +1.0 or -1.0. */ + msw |= ((__int32_t)1023 << 20); + lsw = 0; + } + else + { + __uint32_t exponent_mask = 0x000fffff >> exponent_less_1023; + if ((msw & exponent_mask) == 0 && lsw == 0) + /* x in an integral value. */ + return x; + + msw += 0x00080000 >> exponent_less_1023; + msw &= ~exponent_mask; + lsw = 0; + } + } + else if (exponent_less_1023 > 51) + { + if (exponent_less_1023 == 1024) + /* x is NaN or infinite. */ + return x + x; + else + return x; + } + else + { + __uint32_t exponent_mask = (__uint32_t) 0xffffffff >> (exponent_less_1023 - 20); + __uint32_t tmp; + + if ((lsw & exponent_mask) == 0) + /* x is an integral value. */ + return x; + + tmp = lsw + ((__uint32_t) 1 << (51 - exponent_less_1023)); + if (tmp < lsw) + msw += 1; + lsw = tmp; + + lsw &= ~exponent_mask; + } + INSERT_WORDS(x, msw, lsw); + + return x; +} + +_MATH_ALIAS_d_d(round) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_scalbln.c b/src/audio/ffmpeg_dec/libm/common/s_scalbln.c new file mode 100644 index 000000000000..97658a36a872 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_scalbln.c @@ -0,0 +1,59 @@ +/* @(#)s_scalbn.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * scalbn (double x, int n) + * scalbn(x,n) returns x* 2**n computed by exponent + * manipulation rather than by actually performing an + * exponentiation or a multiplication. + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +static const __float64 +two54 = _F_64(1.80143985094819840000e+16), /* 0x43500000, 0x00000000 */ +twom54 = _F_64(5.55111512312578270212e-17); /* 0x3C900000, 0x00000000 */ + +__float64 +scalbln64 (__float64 x, long int n) +{ + __int32_t hx,lx; + long int k; + EXTRACT_WORDS(hx,lx,x); + k = (hx&0x7ff00000)>>20; /* extract exponent */ + if (k==0) { /* 0 or subnormal x */ + if ((lx|(hx&0x7fffffff))==0) return x; /* +-0 */ + x *= two54; + GET_HIGH_WORD(hx,x); + k = ((hx&0x7ff00000)>>20) - 54; + if (n< -50000) return __math_uflow(hx < 0); /*underflow*/ + } + if (k==0x7ff) return x+x; /* NaN or Inf */ + if (n > 50000) + return __math_oflow(hx<0); /* overflow */ + k = k+n; + if (k > 0x7fe) + return __math_oflow(hx<0); /* overflow */ + if (k > 0) /* normal result */ + {SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20)); return x;} + if (k <= -54) + return __math_uflow(hx < 0); /*underflow*/ + k += 54; /* subnormal result */ + SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20)); + return check_uflow(x*twom54); +} + +_MATH_ALIAS_d_dj(scalbln) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_scalbn.c b/src/audio/ffmpeg_dec/libm/common/s_scalbn.c new file mode 100644 index 000000000000..c27846d435ce --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_scalbn.c @@ -0,0 +1,121 @@ + +/* @(#)s_scalbn.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* +FUNCTION +<>, <>, <>, <>---scale by power of FLT_RADIX (=2) +INDEX + scalbn +INDEX + scalbnf +INDEX + scalbln +INDEX + scalblnf + +SYNOPSIS + #include + double scalbn(double <[x]>, int <[n]>); + float scalbnf(float <[x]>, int <[n]>); + double scalbln(double <[x]>, long int <[n]>); + float scalblnf(float <[x]>, long int <[n]>); + +DESCRIPTION +The <> and <> functions compute + @ifnottex + <[x]> times FLT_RADIX to the power <[n]>. + @end ifnottex + @tex + $x \cdot FLT\_RADIX^n$. + @end tex +efficiently. The result is computed by manipulating the exponent, rather than +by actually performing an exponentiation or multiplication. In this +floating-point implementation FLT_RADIX=2, which makes the <> +functions equivalent to the <> functions. + +RETURNS +<[x]> times 2 to the power <[n]>. A range error may occur. + +PORTABILITY +ANSI C, POSIX + +SEEALSO +<> + +*/ + +/* + * scalbn (double x, int n) + * scalbn(x,n) returns x* 2**n computed by exponent + * manipulation rather than by actually performing an + * exponentiation or a multiplication. + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +static const __float64 +two54 = _F_64(1.80143985094819840000e+16), /* 0x43500000, 0x00000000 */ +twom54 = _F_64(5.55111512312578270212e-17); /* 0x3C900000, 0x00000000 */ + +__float64 +scalbn64(__float64 x, int n) +{ + __int32_t k,hx,lx; + EXTRACT_WORDS(hx,lx,x); + k = (hx&0x7ff00000)>>20; /* extract exponent */ + if (k==0) { /* 0 or subnormal x */ + if ((lx|(hx&0x7fffffff))==0) return x; /* +-0 */ + x *= two54; + GET_HIGH_WORD(hx,x); + k = ((hx&0x7ff00000)>>20) - 54; +#if __SIZEOF_INT__ > 2 + if (n< -50000) return __math_uflow(hx<0); /*underflow*/ +#endif + } + if (k==0x7ff) return x+x; /* NaN or Inf */ +#if __SIZEOF_INT__ > 2 + if (n > 50000) /* in case integer overflow in n+k */ + return __math_oflow(hx<0); /*overflow*/ +#endif + k = k+n; + if (k > 0x7fe) return __math_oflow(hx<0); /* overflow */ + if (k > 0) /* normal result */ + {SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20)); return x;} + if (k <= -54) + return __math_uflow(hx<0); /*underflow*/ + k += 54; /* subnormal result */ + SET_HIGH_WORD(x,(hx&0x800fffff)|(k<<20)); + return check_uflow(x*twom54); +} + +#ifdef __strong_reference +#if defined(__GNUCLIKE_PRAGMA_DIAGNOSTIC) && !defined(__clang__) +#pragma GCC diagnostic ignored "-Wmissing-attributes" +#endif +__strong_reference(scalbn64, ldexp64); +#else + +__float64 +ldexp64(__float64 value, int exp) +{ + return scalbn64(value, exp); +} + +#endif + +_MATH_ALIAS_d_di(scalbn) +_MATH_ALIAS_d_di(ldexp) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/s_signbit.c b/src/audio/ffmpeg_dec/libm/common/s_signbit.c new file mode 100644 index 000000000000..c9d807034c18 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_signbit.c @@ -0,0 +1,64 @@ +/* Copyright (C) 2002 by Red Hat, Incorporated. All rights reserved. + * + * Permission to use, copy, modify, and distribute this software + * is freely granted, provided that this notice is preserved. + */ +/* +FUNCTION +<>---Does floating-point number have negative sign? + +INDEX + signbit + +SYNOPSIS + #include + int signbit(real-floating <[x]>); + +DESCRIPTION +The <> macro determines whether the sign of its argument value is +negative. The macro reports the sign of all values, including infinities, +zeros, and NaNs. If zero is unsigned, it is treated as positive. As shown in +the synopsis, the argument is "real-floating," meaning that any of the real +floating-point types (float, double, etc.) may be given to it. + +Note that because of the possibilities of signed 0 and NaNs, the expression +"<[x]> < 0.0" does not give the same result as <> in all cases. + +RETURNS +The <> macro returns a nonzero value if and only if the sign of its +argument value is negative. + +PORTABILITY +C99, POSIX. + +*/ + +#define _ADD_D_TO_DOUBLE_FUNCS + +#include "fdlibm.h" + +int +__signbitf (float x) +{ + __uint32_t w; + + GET_FLOAT_WORD(w,x); + + return (w & 0x80000000) != 0; +} + +_MATH_ALIAS_i_f(__signbit) + +#ifdef _NEED_FLOAT64 +int +__signbit64(__float64 x) +{ + __uint32_t msw; + + GET_HIGH_WORD(msw, x); + + return (msw & 0x80000000) != 0; +} + +_MATH_ALIAS_i_d(__signbit) +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/s_trunc.c b/src/audio/ffmpeg_dec/libm/common/s_trunc.c new file mode 100644 index 000000000000..60656f300423 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/s_trunc.c @@ -0,0 +1,75 @@ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ +/* +FUNCTION +<>, <>---round to integer, towards zero +INDEX + trunc +INDEX + truncf + +SYNOPSIS + #include + double trunc(double <[x]>); + float truncf(float <[x]>); + +DESCRIPTION + The <> functions round their argument to the integer value, in + floating format, nearest to but no larger in magnitude than the + argument, regardless of the current rounding direction. (While the + "inexact" floating-point exception behavior is unspecified by the C + standard, the <> functions are written so that "inexact" is not + raised if the result does not equal the argument, which behavior is as + recommended by IEEE 754 for its related functions.) + +RETURNS +<[x]> truncated to an integral value. + +PORTABILITY +ANSI C, POSIX + +*/ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +__float64 +trunc64(__float64 x) +{ + int64_t ix = _asint64(x); + int64_t mask; + int exp; + + /* Un-biased exponent */ + exp = _exponent64(ix) - 1023; + + /* Inf/NaN, evaluate value */ + if (unlikely(exp == 1024)) + return x + x; + + /* compute portion of value with useful bits */ + if (exp < 0) + /* less than one, save sign bit */ + mask = 0x8000000000000000LL; + else { + /* otherwise, save sign, exponent and any useful bits */ + if (exp >= 64) + exp = 63; + mask = ~(0x000fffffffffffffLL >> exp); + } + + return _asfloat64(ix & mask); +} + +_MATH_ALIAS_d_d(trunc) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/common/sf_cbrt.c b/src/audio/ffmpeg_dec/libm/common/sf_cbrt.c new file mode 100644 index 000000000000..542073496e0e --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_cbrt.c @@ -0,0 +1,69 @@ +/* sf_cbrt.c -- float version of s_cbrt.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + * + */ + +#include "fdlibm.h" + +/* cbrtf(x) + * Return cube root of x + */ +static const __uint32_t + B1 = 709958130, /* B1 = (84+2/3-0.03306235651)*2**23 */ + B2 = 642849266; /* B2 = (76+2/3-0.03306235651)*2**23 */ + +static const float + C = 5.4285717010e-01, /* 19/35 = 0x3f0af8b0 */ + D = -7.0530611277e-01, /* -864/1225 = 0xbf348ef1 */ + E = 1.4142856598e+00, /* 99/70 = 0x3fb50750 */ + F = 1.6071428061e+00, /* 45/28 = 0x3fcdb6db */ + G = 3.5714286566e-01; /* 5/14 = 0x3eb6db6e */ + +float cbrtf(float x) +{ + __int32_t hx; + float r,s,t; + __uint32_t sign; + __uint32_t high; + + GET_FLOAT_WORD(hx,x); + sign=hx&0x80000000; /* sign= sign(x) */ + hx ^=sign; + if(!FLT_UWORD_IS_FINITE(hx)) + return(x+x); /* cbrt(NaN,INF) is itself */ + if(FLT_UWORD_IS_ZERO(hx)) + return(x); /* cbrt(0) is itself */ + + SET_FLOAT_WORD(x,hx); /* x <- |x| */ + /* rough cbrt to 5 bits */ + if(FLT_UWORD_IS_SUBNORMAL(hx)) /* subnormal number */ + {SET_FLOAT_WORD(t,0x4b800000); /* set t= 2**24 */ + t*=x; GET_FLOAT_WORD(high,t); SET_FLOAT_WORD(t,high/3+B2); + } + else + SET_FLOAT_WORD(t,hx/3+B1); + + + /* new cbrt to 23 bits */ + r=t*t/x; + s=C+r*t; + t*=G+F/(s+E+D/s); + + /* retore the sign bit */ + GET_FLOAT_WORD(high,t); + SET_FLOAT_WORD(t,high|sign); + return(t); +} + +_MATH_ALIAS_f_f(cbrt) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_copysign.c b/src/audio/ffmpeg_dec/libm/common/sf_copysign.c new file mode 100644 index 000000000000..f01711b41074 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_copysign.c @@ -0,0 +1,34 @@ +/* sf_copysign.c -- float version of s_copysign.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * copysignf(float x, float y) + * copysignf(x,y) returns a value with the magnitude of x and + * with the sign bit of y. + */ + +#include "fdlibm.h" + +float +copysignf(float x, float y) +{ + __uint32_t ix,iy; + GET_FLOAT_WORD(ix,x); + GET_FLOAT_WORD(iy,y); + SET_FLOAT_WORD(x,(ix&0x7fffffff)|(iy&0x80000000)); + return x; +} + +_MATH_ALIAS_f_ff(copysign) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_exp.c b/src/audio/ffmpeg_dec/libm/common/sf_exp.c new file mode 100644 index 000000000000..5a6b6961267a --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_exp.c @@ -0,0 +1,112 @@ +/* Single-precision e^x function. + Copyright (c) 2017 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "fdlibm.h" +#if !__OBSOLETE_MATH_FLOAT + +#include +#include +#include "math_config.h" + +/* +EXP2F_TABLE_BITS = 5 +EXP2F_POLY_ORDER = 3 + +ULP error: 0.502 (nearest rounding.) +Relative error: 1.69 * 2^-34 in [-ln2/64, ln2/64] (before rounding.) +Wrong count: 170635 (all nearest rounding wrong results with fma.) +Non-nearest ULP error: 1 (rounded ULP error) +*/ + +#define N (1 << EXP2F_TABLE_BITS) +#define InvLn2N __exp2f_data.invln2_scaled +#define T __exp2f_data.tab +#define C __exp2f_data.poly_scaled + +static inline uint32_t +top12 (float x) +{ + return asuint (x) >> 20; +} + +float +expf (float x) +{ + uint32_t abstop; + uint64_t ki, t; + /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ + double_t kd, xd, z, r, r2, y, s; + + xd = (double_t) x; + abstop = top12 (x) & 0x7ff; + if (__builtin_expect (abstop >= top12 (88.0f), 0)) + { + /* |x| >= 88 or x is nan. */ + if (asuint (x) == asuint (-INFINITY)) + return 0.0f; + if (abstop >= top12 (INFINITY)) + return x + x; + if (x > 0x1.62e42ep6f) /* x > log(0x1p128) ~= 88.72 */ + return __math_oflowf (0); + if (x < -0x1.9fe368p6f) /* x < log(0x1p-150) ~= -103.97 */ + return __math_uflowf (0); +#if WANT_ERRNO_UFLOW + if (x < -0x1.9d1d9ep6f) /* x < log(0x1p-149) ~= -103.28 */ + return __math_may_uflowf (0); +#endif + } + + /* x*N/Ln2 = k + r with r in [-1/2, 1/2] and int k. */ + z = InvLn2N * xd; + + /* Round and convert z to int, the result is in [-150*N, 128*N] and + ideally ties-to-even rule is used, otherwise the magnitude of r + can be bigger which gives larger approximation error. */ +#if TOINT_INTRINSICS + kd = roundtoint (z); + ki = converttoint (z); +#else +# define SHIFT __exp2f_data.shift + kd = (double) (z + SHIFT); /* Rounding to double precision is required. */ + ki = asuint64 (kd); + kd -= SHIFT; +#endif + r = z - kd; + + /* exp(x) = 2^(k/N) * 2^(r/N) ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */ + t = T[ki % N]; + t += ki << (52 - EXP2F_TABLE_BITS); + s = asfloat64 (t); + z = C[0] * r + C[1]; + r2 = r * r; + y = C[2] * r + 1; + y = z * r2 + y; + y = y * s; + return (float) y; +} +#endif /* !__OBSOLETE_MATH_FLOAT */ diff --git a/src/audio/ffmpeg_dec/libm/common/sf_exp10.c b/src/audio/ffmpeg_dec/libm/common/sf_exp10.c new file mode 100644 index 000000000000..1e5ff9bb2f67 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_exp10.c @@ -0,0 +1,31 @@ +/* sf_exp10.c -- float version of s_exp10.c. + * Modification of sf_exp2.c by Yaakov Selkowitz 2007. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * wrapper exp10f(x) + */ + +#define _GNU_SOURCE +#undef exp10f +#include "fdlibm.h" +#include +#include + +float exp10f(float x) /* wrapper exp10f */ +{ + return _powf(10.0, x); +} + +_MATH_ALIAS_f_f(exp10) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_exp2.c b/src/audio/ffmpeg_dec/libm/common/sf_exp2.c new file mode 100644 index 000000000000..6df2376f60a7 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_exp2.c @@ -0,0 +1,101 @@ +/* Single-precision 2^x function. + Copyright (c) 2017 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "fdlibm.h" +#if !__OBSOLETE_MATH_FLOAT + +#include +#include +#include "math_config.h" + +/* +EXP2F_TABLE_BITS = 5 +EXP2F_POLY_ORDER = 3 + +ULP error: 0.502 (nearest rounding.) +Relative error: 1.69 * 2^-34 in [-1/64, 1/64] (before rounding.) +Wrong count: 168353 (all nearest rounding wrong results with fma.) +Non-nearest ULP error: 1 (rounded ULP error) +*/ + +#define N (1 << EXP2F_TABLE_BITS) +#define T __exp2f_data.tab +#define C __exp2f_data.poly +#define SHIFT __exp2f_data.shift_scaled + +static inline uint32_t +top12 (float x) +{ + return asuint (x) >> 20; +} + +float +exp2f (float x) +{ + uint32_t abstop; + uint64_t ki, t; + /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ + double_t kd, xd, z, r, r2, y, s; + + xd = (double_t) x; + abstop = top12 (x) & 0x7ff; + if (__builtin_expect (abstop >= top12 (128.0f), 0)) + { + /* |x| >= 128 or x is nan. */ + if (asuint (x) == asuint (-INFINITY)) + return 0.0f; + if (abstop >= top12 (INFINITY)) + return x + x; + if (x > 0.0f) + return __math_oflowf (0); + if (x <= -150.0f) + return __math_uflowf (0); +#if WANT_ERRNO_UFLOW + if (x < -149.0f) + return __math_may_uflowf (0); +#endif + } + + /* x = k/N + r with r in [-1/(2N), 1/(2N)] and int k. */ + kd = (double) (xd + SHIFT); /* Rounding to double precision is required. */ + ki = asuint64 (kd); + kd -= SHIFT; /* k/N for int k. */ + r = xd - kd; + + /* exp2(x) = 2^(k/N) * 2^r ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */ + t = T[ki % N]; + t += ki << (52 - EXP2F_TABLE_BITS); + s = asfloat64 (t); + z = C[0] * r + C[1]; + r2 = r * r; + y = C[2] * r + 1; + y = z * r2 + y; + y = y * s; + return (float) y; +} +#endif /* !__OBSOLETE_MATH_FLOAT */ diff --git a/src/audio/ffmpeg_dec/libm/common/sf_exp2_data.c b/src/audio/ffmpeg_dec/libm/common/sf_exp2_data.c new file mode 100644 index 000000000000..daf12a5d7844 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_exp2_data.c @@ -0,0 +1,58 @@ +/* Shared data between expf, exp2f and powf. + Copyright (c) 2017 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "fdlibm.h" +#if !__OBSOLETE_MATH_FLOAT + +#include "math_config.h" + +#define N (1 << EXP2F_TABLE_BITS) + +const struct exp2f_data __exp2f_data = { + /* tab[i] = uint(2^(i/N)) - (i << 52-BITS) + used for computing 2^(k/N) for an int |k| < 150 N as + double(tab[k%N] + (k << 52-BITS)) */ + .tab = { +0x3ff0000000000000, 0x3fefd9b0d3158574, 0x3fefb5586cf9890f, 0x3fef9301d0125b51, +0x3fef72b83c7d517b, 0x3fef54873168b9aa, 0x3fef387a6e756238, 0x3fef1e9df51fdee1, +0x3fef06fe0a31b715, 0x3feef1a7373aa9cb, 0x3feedea64c123422, 0x3feece086061892d, +0x3feebfdad5362a27, 0x3feeb42b569d4f82, 0x3feeab07dd485429, 0x3feea47eb03a5585, +0x3feea09e667f3bcd, 0x3fee9f75e8ec5f74, 0x3feea11473eb0187, 0x3feea589994cce13, +0x3feeace5422aa0db, 0x3feeb737b0cdc5e5, 0x3feec49182a3f090, 0x3feed503b23e255d, +0x3feee89f995ad3ad, 0x3feeff76f2fb5e47, 0x3fef199bdd85529c, 0x3fef3720dcef9069, +0x3fef5818dcfba487, 0x3fef7c97337b9b5f, 0x3fefa4afa2a490da, 0x3fefd0765b6e4540, + }, + .shift_scaled = 0x1.8p+52 / N, + .poly = { 0x1.c6af84b912394p-5, 0x1.ebfce50fac4f3p-3, 0x1.62e42ff0c52d6p-1 }, + .shift = 0x1.8p+52, + .invln2_scaled = 0x1.71547652b82fep+0 * N, + .poly_scaled = { +0x1.c6af84b912394p-5/N/N/N, 0x1.ebfce50fac4f3p-3/N/N, 0x1.62e42ff0c52d6p-1/N + }, +}; +#endif /* !__OBSOLETE_MATH_FLOAT */ diff --git a/src/audio/ffmpeg_dec/libm/common/sf_expm1.c b/src/audio/ffmpeg_dec/libm/common/sf_expm1.c new file mode 100644 index 000000000000..43785e82b20e --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_expm1.c @@ -0,0 +1,123 @@ +/* sf_expm1.c -- float version of s_expm1.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" +#include "math_config.h" + +#ifdef __v810__ +#define const +#endif + +static const float +one = 1.0, +huge = 1.0e+30, +tiny = 1.0e-30, +ln2_hi = 6.9313812256e-01,/* 0x3f317180 */ +ln2_lo = 9.0580006145e-06,/* 0x3717f7d1 */ +invln2 = 1.4426950216e+00,/* 0x3fb8aa3b */ + /* scaled coefficients related to expm1 */ +Q1 = -3.3333335072e-02, /* 0xbd088889 */ +Q2 = 1.5873016091e-03, /* 0x3ad00d01 */ +Q3 = -7.9365076090e-05, /* 0xb8a670cd */ +Q4 = 4.0082177293e-06, /* 0x36867e54 */ +Q5 = -2.0109921195e-07; /* 0xb457edbb */ + +float expm1f(float x) +{ + float y,hi,lo,c,t,e,hxs,hfx,r1; + __int32_t k,xsb; + __uint32_t hx; + + GET_FLOAT_WORD(hx,x); + xsb = hx&0x80000000; /* sign bit of x */ + if(xsb==0) y=x; else y= -x; /* y = |x| */ + hx &= 0x7fffffff; /* high word of |x| */ + + /* filter out huge and non-finite argument */ + if(hx >= 0x4195b844) { /* if |x|>=27*ln2 */ + if(FLT_UWORD_IS_NAN(hx)) + return x+x; + if(FLT_UWORD_IS_INFINITE(hx)) + return (xsb==0)? x:-1.0f;/* exp(+-inf)={inf,-1} */ + if(xsb == 0 && hx > FLT_UWORD_LOG_MAX) /* if x>=o_threshold */ + return __math_oflowf (0); /* overflow */ + if(xsb!=0) /* x < -27*ln2, return -1.0 with inexact */ + return __math_inexactf(tiny-one); /* return -1 */ + } + + /* argument reduction */ + if(hx > 0x3eb17218) { /* if |x| > 0.5 ln2 */ + if(hx < 0x3F851592) { /* and |x| < 1.5 ln2 */ + if(xsb==0) + {hi = x - ln2_hi; lo = ln2_lo; k = 1;} + else + {hi = x + ln2_hi; lo = -ln2_lo; k = -1;} + } else { + k = invln2*x+((xsb==0)?0.5f:-0.5f); + t = k; + hi = x - t*ln2_hi; /* t*ln2_hi is exact here */ + lo = t*ln2_lo; + } + x = hi - lo; + c = (hi-x)-lo; + } + else if(hx < 0x33000000) { /* when |x|<2**-25, return x */ + t = huge+x; /* return x with inexact flags when x!=0 */ + return x - (t-(huge+x)); + } + else k = 0; + + /* x is now in primary range */ + hfx = 0.5f*x; + hxs = x*hfx; + r1 = one+hxs*(Q1+hxs*(Q2+hxs*(Q3+hxs*(Q4+hxs*Q5)))); + t = 3.0f-r1*hfx; + e = hxs*((r1-t)/(6.0f - x*t)); + if(k==0) return x - (x*e-hxs); /* c is 0 */ + else { + e = (x*(e-c)-c); + e -= hxs; + if(k== -1) return 0.5f*(x-e)-0.5f; + if(k==1) { + if(x < -0.25f) return -2.0f*(e-(x+0.5f)); + else return one+2.0f*(x-e); + } + if (k <= -2 || k>56) { /* suffice to return exp(x)-1 */ + __int32_t i; + y = one-(e-x); + GET_FLOAT_WORD(i,y); + SET_FLOAT_WORD(y,i+lsl(k, 23)); /* add k to y's exponent */ + return y-one; + } + t = one; + if(k<23) { + __int32_t i; + SET_FLOAT_WORD(t,0x3f800000 - (0x1000000>>k)); /* t=1-2^-k */ + y = t-(e-x); + GET_FLOAT_WORD(i,y); + SET_FLOAT_WORD(y,i+lsl(k, 23)); /* add k to y's exponent */ + } else { + __int32_t i; + SET_FLOAT_WORD(t,lsl((0x7f-k), 23)); /* 2^-k */ + y = x-(e+t); + y += one; + GET_FLOAT_WORD(i,y); + SET_FLOAT_WORD(y,i+lsl(k, 23)); /* add k to y's exponent */ + } + } + return y; +} + +_MATH_ALIAS_f_f(expm1) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_fdim.c b/src/audio/ffmpeg_dec/libm/common/sf_fdim.c new file mode 100644 index 000000000000..5179f7b898c2 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_fdim.c @@ -0,0 +1,19 @@ +/* Copyright (C) 2002 by Red Hat, Incorporated. All rights reserved. + * + * Permission to use, copy, modify, and distribute this software + * is freely granted, provided that this notice is preserved. + */ + +#include "fdlibm.h" + +float fdimf(float x, float y) +{ + if (isnanf(x) || isnanf(y)) return(x+y); + + float z = x > y ? x - y : 0.0f; + if (!isinf(x) && !isinf(y)) + z = check_oflowf(z); + return z; +} + +_MATH_ALIAS_f_ff(fdim) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_finite.c b/src/audio/ffmpeg_dec/libm/common/sf_finite.c new file mode 100644 index 000000000000..e5948a1b485e --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_finite.c @@ -0,0 +1,42 @@ +/* sf_finite.c -- float version of s_finite.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * finitef(x) returns 1 is x is finite, else 0; + * no branching! + */ + +#include "fdlibm.h" + +int finitef(float x) +{ + __int32_t ix; + GET_FLOAT_WORD(ix,x); + ix &= 0x7fffffff; + return (FLT_UWORD_IS_FINITE(ix)); +} + +#ifdef __strong_reference +__strong_reference(finitef, __finitef); +#else +int +__finitef(float x) +{ + return finitef(x); +} +#endif + +_MATH_ALIAS_i_f(finite) +_MATH_ALIAS_i_f(__finite) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_fma.c b/src/audio/ffmpeg_dec/libm/common/sf_fma.c new file mode 100644 index 000000000000..8a2f30fa383f --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_fma.c @@ -0,0 +1,53 @@ +/* Copyright (C) 2002 by Red Hat, Incorporated. All rights reserved. + * + * Permission to use, copy, modify, and distribute this software + * is freely granted, provided that this notice is preserved. + */ + +#include "fdlibm.h" + +#if !__HAVE_FAST_FMAF + +#if __FLT_EVAL_METHOD__ == 2 && defined(__HAVE_LONG_DOUBLE) + +float +fmaf(float x, float y, float z) +{ + return (float) fmal((long double) x, (long double) y, (long double) z); +} + +#else + +typedef float FLOAT_T; + +#define FMA fmaf +#define NEXTAFTER nextafterf +#define LDEXP ldexpf +#define FREXP frexpf +#define SCALBN scalbnf +#define SPLIT (0x1p12f + 1.0f) +#define COPYSIGN copysignf +#define FLOAT_MANT_DIG __FLT_MANT_DIG__ +#define FLOAT_MAX_EXP __FLT_MAX_EXP__ +#define FLOAT_MIN __FLT_MIN__ +#define ILOGB ilogbf + +static inline int +odd_mant(FLOAT_T x) +{ + return asuint(x) & 1; +} + +static unsigned int +EXPONENT(FLOAT_T x) +{ + return _exponent32(asuint(x)); +} + +#include "fma_inc.h" + +#endif + +_MATH_ALIAS_f_fff(fma) + +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/sf_fmax.c b/src/audio/ffmpeg_dec/libm/common/sf_fmax.c new file mode 100644 index 000000000000..a3ba48ef4faa --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_fmax.c @@ -0,0 +1,23 @@ +/* Copyright (C) 2002 by Red Hat, Incorporated. All rights reserved. + * + * Permission to use, copy, modify, and distribute this software + * is freely granted, provided that this notice is preserved. + */ + +#include "fdlibm.h" + +float fmaxf(float x, float y) +{ + if (issignaling(x) || issignaling(y)) + return x + y; + + if (isnan(x)) + return y; + + if (isnan(y)) + return x; + + return x > y ? x : y; +} + +_MATH_ALIAS_f_ff(fmax) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_fmin.c b/src/audio/ffmpeg_dec/libm/common/sf_fmin.c new file mode 100644 index 000000000000..6f8411542ad0 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_fmin.c @@ -0,0 +1,23 @@ +/* Copyright (C) 2002 by Red Hat, Incorporated. All rights reserved. + * + * Permission to use, copy, modify, and distribute this software + * is freely granted, provided that this notice is preserved. + */ + +#include "fdlibm.h" + +float fminf(float x, float y) +{ + if (issignaling(x) || issignaling(y)) + return x + y; + + if (isnan(x)) + return y; + + if (isnan(y)) + return x; + + return x < y ? x : y; +} + +_MATH_ALIAS_f_ff(fmin) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_fpclassify.c b/src/audio/ffmpeg_dec/libm/common/sf_fpclassify.c new file mode 100644 index 000000000000..d2a07440e969 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_fpclassify.c @@ -0,0 +1,32 @@ +/* Copyright (C) 2002,2007 by Red Hat, Incorporated. All rights reserved. + * + * Permission to use, copy, modify, and distribute this software + * is freely granted, provided that this notice is preserved. + */ + +#define _ADD_D_TO_DOUBLE_FUNCS + +#include "fdlibm.h" + +int +__fpclassifyf (float x) +{ + __uint32_t w; + + GET_FLOAT_WORD(w,x); + + if (w == 0x00000000 || w == 0x80000000) + return FP_ZERO; + else if ((w >= 0x00800000 && w <= 0x7f7fffff) || + (w >= 0x80800000 && w <= 0xff7fffff)) + return FP_NORMAL; + else if ((w >= 0x00000001 && w <= 0x007fffff) || + (w >= 0x80000001 && w <= 0x807fffff)) + return FP_SUBNORMAL; + else if (w == 0x7f800000 || w == 0xff800000) + return FP_INFINITE; + else + return FP_NAN; +} + +_MATH_ALIAS_i_f(__fpclassify) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_getpayload.c b/src/audio/ffmpeg_dec/libm/common/sf_getpayload.c new file mode 100644 index 000000000000..6166acb06037 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_getpayload.c @@ -0,0 +1,50 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright © 2021 Keith Packard + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "fdlibm.h" + +float getpayloadf(const float *x) +{ + __int32_t ix; + GET_FLOAT_WORD(ix, *x); + + if ((ix & 0x7f800000) != 0x7f800000 || + ((ix & 0x7fffff) == 0)) + return -1; + ix &= 0x3fffff; + return (float) ix; +} + +_MATH_ALIAS_f_F(getpayload) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_ilogb.c b/src/audio/ffmpeg_dec/libm/common/sf_ilogb.c new file mode 100644 index 000000000000..2c7eb66a7473 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_ilogb.c @@ -0,0 +1,46 @@ +/* sf_ilogb.c -- float version of s_ilogb.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "fdlibm.h" + +int ilogbf(float x) +{ + __int32_t hx,ix; + + GET_FLOAT_WORD(hx,x); + hx &= 0x7fffffff; + if(FLT_UWORD_IS_ZERO(hx)) { + (void) __math_invalidf(0.0); + return FP_ILOGB0; /* ilogb(0) = special case error */ + } + if(FLT_UWORD_IS_SUBNORMAL(hx)) { + for (ix = -126,hx = lsl(hx, 8); hx>0; hx = lsl(hx, 1)) ix -=1; + return ix; + } +#if FP_ILOGBNAN != INT_MAX + else if (FLT_UWORD_IS_NAN(hx)) { + (void) __math_invalidf(0.0); + return FP_ILOGBNAN; /* NAN */ + } +#endif + else if (!FLT_UWORD_IS_FINITE(hx)) { + (void) __math_invalidf(0.0); + return INT_MAX; + } + else return (hx>>23)-127; +} + +_MATH_ALIAS_i_f(ilogb) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_infinity.c b/src/audio/ffmpeg_dec/libm/common/sf_infinity.c new file mode 100644 index 000000000000..552ed7fba027 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_infinity.c @@ -0,0 +1,24 @@ +/* +Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + +Developed at SunPro, a Sun Microsystems, Inc. business. +Permission to use, copy, modify, and distribute this +software is freely granted, provided that this notice +is preserved. + */ +/* + * infinityf () returns the representation of infinity. + * Added by Cygnus Support. + */ + +#include "fdlibm.h" + +float infinityf(void) +{ + float x; + + SET_FLOAT_WORD(x,0x7f800000); + return x; +} + +_MATH_ALIAS_f(infinity) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_iseqsig.c b/src/audio/ffmpeg_dec/libm/common/sf_iseqsig.c new file mode 100644 index 000000000000..59feb8d8dba9 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_iseqsig.c @@ -0,0 +1,50 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright © 2022 Keith Packard + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#define _ADD_D_TO_DOUBLE_FUNCS + +#include "fdlibm.h" + +int +__iseqsigf(float x, float y) +{ + if (isnan(x)) + return __math_invalidf(x); + if (isnan(y)) + return __math_invalidf(y); + return x == y; +} + +_MATH_ALIAS_i_ff(__iseqsig) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_isinf.c b/src/audio/ffmpeg_dec/libm/common/sf_isinf.c new file mode 100644 index 000000000000..706a81bbffbc --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_isinf.c @@ -0,0 +1,45 @@ +/* +Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + +Developed at SunPro, a Sun Microsystems, Inc. business. +Permission to use, copy, modify, and distribute this +software is freely granted, provided that this notice +is preserved. + */ +/* + * isinff(x) returns 1 if x is +-infinity, else 0; + * + * isinf is a macro in the C99 standard. It was previously + * implemented as isinf and isinff functions by newlib and are still declared + * as such in . Newlib supplies it here as a function if the user + * chooses to use it instead of the C99 macro. + */ + +#define _ADD_D_TO_DOUBLE_FUNCS +#define isinfd isinf + +#include "fdlibm.h" + +#undef isinff + +int +isinff (float x) +{ + __int32_t ix; + GET_FLOAT_WORD(ix,x); + ix &= 0x7fffffff; + return FLT_UWORD_IS_INFINITE(ix); +} + +#ifdef __strong_reference +__strong_reference(isinff, __isinff); +#else +int +__isinff(float x) +{ + return isinff(x); +} +#endif + +_MATH_ALIAS_i_f(isinf) +_MATH_ALIAS_i_f(__isinf) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_isnan.c b/src/audio/ffmpeg_dec/libm/common/sf_isnan.c new file mode 100644 index 000000000000..768dca498bc1 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_isnan.c @@ -0,0 +1,48 @@ +/* sf_c_isnan.c -- float version of s_c_isnan.c. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * isnanf(x) returns 1 is x is nan, else 0; + * + * isnanf is an extension declared in . + */ + +#define _ADD_D_TO_DOUBLE_FUNCS +#define isnand isnan + +#include "fdlibm.h" + +#undef isnanf + +int +isnanf (float x) +{ + __int32_t ix; + GET_FLOAT_WORD(ix,x); + ix &= 0x7fffffff; + return FLT_UWORD_IS_NAN(ix); +} + +#ifdef __strong_reference +__strong_reference(isnanf, __isnanf); +#else +int +__isnanf(float x) +{ + return isnanf(x); +} +#endif + +_MATH_ALIAS_i_f(isnan) +_MATH_ALIAS_i_f(__isnan) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_issignaling.c b/src/audio/ffmpeg_dec/libm/common/sf_issignaling.c new file mode 100644 index 000000000000..d747baec2cb5 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_issignaling.c @@ -0,0 +1,43 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright © 2019 Keith Packard + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include "fdlibm.h" + +int +__issignalingf(float x) +{ + return issignalingf_inline(x); +} + +_MATH_ALIAS_i_f(__issignaling) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_llrint.c b/src/audio/ffmpeg_dec/libm/common/sf_llrint.c new file mode 100644 index 000000000000..6a257e6f0212 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_llrint.c @@ -0,0 +1,84 @@ +/* lrintf adapted to be llrintf for Newlib, 2009 by Craig Howland. */ +/* @(#)sf_lrint.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * llrintf(x) + * Return x rounded to integral value according to the prevailing + * rounding mode. + * Method: + * Using floating addition. + * Exception: + * Inexact flag raised if x not equal to llrintf(x). + */ + +#include "fdlibm.h" +#include + +static const float +/* Adding a float, x, to 2^23 will cause the result to be rounded based on + the fractional part of x, according to the implementation's current rounding + mode. 2^23 is the smallest float that can be represented using all 23 significant + digits. */ +TWO23[2]={ + 8.3886080000e+06, /* 0x4b000000 */ + -8.3886080000e+06, /* 0xcb000000 */ +}; + +long long int llrintf(float x) +{ + __int32_t j0,sx; + __uint32_t i0; + float t; + volatile float w; + long long int result; + + GET_FLOAT_WORD(i0,x); + + /* Extract sign bit. */ + sx = (i0 >> 31); + + /* Extract exponent field. */ + j0 = ((i0 & 0x7f800000) >> 23) - 127; + + if (j0 < (int)(sizeof (long long int) * 8) - 1) + { + if (j0 >= 23) + result = (long long int) ((i0 & 0x7fffff) | 0x800000) << (j0 - 23); + else + { + w = TWO23[sx] + x; + t = w - TWO23[sx]; + GET_FLOAT_WORD (i0, t); + /* Detect the all-zeros representation of plus and + minus zero, which fails the calculation below. */ + if ((i0 & ~((__uint32_t)1 << 31)) == 0) + return 0; + j0 = ((i0 >> 23) & 0xff) - 0x7f; + i0 &= 0x7fffff; + i0 |= 0x800000; + result = (j0 < 0 ? 0 : i0 >> (23 - j0)); + } + } + else + { + if (x != LLONG_MIN) + { + __math_set_invalidf(); + return sx ? LLONG_MIN : LLONG_MAX; + } + return (long long) x; + } + return sx ? -result : result; +} + +_MATH_ALIAS_k_f(llrint) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_llround.c b/src/audio/ffmpeg_dec/libm/common/sf_llround.c new file mode 100644 index 000000000000..355e27ce6878 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_llround.c @@ -0,0 +1,55 @@ +/* lroundf adapted to be llroundf for Newlib, 2009 by Craig Howland. */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" +#include + +long long int +llroundf(float x) +{ + __int32_t exponent_less_127; + __uint32_t w; + long long int result; + __int32_t sign; + + GET_FLOAT_WORD (w, x); + exponent_less_127 = ((w & 0x7f800000) >> 23) - 127; + sign = (w & 0x80000000) != 0 ? -1 : 1; + w &= 0x7fffff; + w |= 0x800000; + + if (exponent_less_127 < (int)((8 * sizeof (long long int)) - 1)) + { + if (exponent_less_127 < 0) + return exponent_less_127 < -1 ? 0 : sign; + else if (exponent_less_127 >= 23) + result = (long long int) w << (exponent_less_127 - 23); + else + { + w += 0x400000 >> exponent_less_127; + result = w >> (23 - exponent_less_127); + } + } + else + { + /* Result other than LONG_MIN is too large to be represented by + * a long int. + */ + if (x != (float) LLONG_MIN) + __math_set_invalidf(); + return sign == 1 ? LLONG_MAX : LLONG_MIN; + } + + return sign * result; +} + +_MATH_ALIAS_k_f(llround) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_log.c b/src/audio/ffmpeg_dec/libm/common/sf_log.c new file mode 100644 index 000000000000..f80883a8f7ab --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_log.c @@ -0,0 +1,100 @@ +/* Single-precision log function. + Copyright (c) 2017 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "fdlibm.h" +#if !__OBSOLETE_MATH_FLOAT + +#include +#include +#include "math_config.h" + +/* +LOGF_TABLE_BITS = 4 +LOGF_POLY_ORDER = 4 + +ULP error: 0.818 (nearest rounding.) +Relative error: 1.957 * 2^-26 (before rounding.) +*/ + +#define T __logf_data.tab +#define A __logf_data.poly +#define Ln2 __logf_data.ln2 +#define N (1 << LOGF_TABLE_BITS) +#define OFF 0x3f330000 + +float +logf (float x) +{ + /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ + double_t z, r, r2, y, y0, invc, logc; + uint32_t ix, iz, tmp; + int k, i; + + ix = asuint (x); +#if WANT_ROUNDING + /* Fix sign of zero with downward rounding when x==1. */ + if (__builtin_expect (ix == 0x3f800000, 0)) + return 0; +#endif + if (__builtin_expect (ix - 0x00800000 >= 0x7f800000 - 0x00800000, 0)) + { + /* x < 0x1p-126 or inf or nan. */ + if (ix * 2 == 0) + return __math_divzerof (1); + if (ix == 0x7f800000) /* log(inf) == inf. */ + return x; + if ((ix & 0x80000000) || ix * 2 >= 0xff000000) + return __math_invalidf (x); + /* x is subnormal, normalize it. */ + ix = asuint (x * 0x1p23f); + ix -= (int32_t) 23 << 23; + } + + /* x = 2^k z; where z is in range [OFF,2*OFF] and exact. + The range is split into N subintervals. + The ith subinterval contains z and c is near its center. */ + tmp = ix - OFF; + i = (tmp >> (23 - LOGF_TABLE_BITS)) % N; + k = (int32_t) tmp >> 23; /* arithmetic shift */ + iz = ix - (tmp & (uint32_t) 0x1ff << 23); + invc = T[i].invc; + logc = T[i].logc; + z = (double_t) asfloat (iz); + + /* log(x) = log1p(z/c-1) + log(c) + k*Ln2 */ + r = z * invc - 1; + y0 = logc + (double_t) k * Ln2; + + /* Pipelined polynomial evaluation to approximate log1p(r). */ + r2 = r * r; + y = A[1] * r + A[2]; + y = A[0] * r2 + y; + y = y * r2 + (y0 + r); + return (float) y; +} +#endif /* !__OBSOLETE_MATH_FLOAT */ diff --git a/src/audio/ffmpeg_dec/libm/common/sf_log1p.c b/src/audio/ffmpeg_dec/libm/common/sf_log1p.c new file mode 100644 index 000000000000..f48a64415be1 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_log1p.c @@ -0,0 +1,98 @@ +/* sf_log1p.c -- float version of s_log1p.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" +#include "math_config.h" + +static const float +ln2_hi = 6.9313812256e-01, /* 0x3f317180 */ +ln2_lo = 9.0580006145e-06, /* 0x3717f7d1 */ +Lp1 = 6.6666668653e-01, /* 3F2AAAAB */ +Lp2 = 4.0000000596e-01, /* 3ECCCCCD */ +Lp3 = 2.8571429849e-01, /* 3E924925 */ +Lp4 = 2.2222198546e-01, /* 3E638E29 */ +Lp5 = 1.8183572590e-01, /* 3E3A3325 */ +Lp6 = 1.5313838422e-01, /* 3E1CD04F */ +Lp7 = 1.4798198640e-01; /* 3E178897 */ + +static const float zero = 0.0; + +float +log1pf(float x) +{ + float hfsq,f,c=0,s,z,R,u; + __int32_t k,hx,hu,ax; + + GET_FLOAT_WORD(hx,x); + ax = hx&0x7fffffff; + + k = 1; + if (!FLT_UWORD_IS_FINITE(hx)) return x+x; + if (hx < 0x3ed413d7) { /* x < 0.41422 */ + if(ax>=0x3f800000) { /* x <= -1.0 */ + if(x==(float)-1.0) + return __math_divzerof (1); /* log1p(-1)=-inf */ + else + return __math_invalidf (x); /* log1p(x<-1)=NaN */ + } + if(ax<0x39000000) { /* |x| < 2**-13 */ + if(ax<0x32800000) /* |x| < 2**-26 */ + return __math_inexactf(x); + else + return __math_inexactf(x - x*x*(float)0.5); + } + if(hx>0||hx<=((__int32_t)0xbe95f61f)) { + k=0;f=x;hu=1;} /* -0.2929>23)-127; + /* correction term */ + c = (k>0)? (float)1.0-(u-x):x-(u-(float)1.0); + c /= u; + } else { + u = x; + GET_FLOAT_WORD(hu,u); + k = (hu>>23)-127; + c = 0; + } + hu &= 0x007fffff; + if(hu<0x3504f7) { + SET_FLOAT_WORD(u,hu|0x3f800000);/* normalize u */ + } else { + k += 1; + SET_FLOAT_WORD(u,hu|0x3f000000); /* normalize u/2 */ + hu = (0x00800000-hu)>>2; + } + f = u-(float)1.0; + } + hfsq=(float)0.5*f*f; + if(hu==0) { /* |f| < 2**-20 */ + if(f==zero) { if(k==0) return zero; + else {c += k*ln2_lo; return k*ln2_hi+c;}} + R = hfsq*((float)1.0-(float)0.66666666666666666*f); + if(k==0) return f-R; else + return k*ln2_hi-((R-(k*ln2_lo+c))-f); + } + s = f/((float)2.0+f); + z = s*s; + R = z*(Lp1+z*(Lp2+z*(Lp3+z*(Lp4+z*(Lp5+z*(Lp6+z*Lp7)))))); + if(k==0) return f-(hfsq-s*(hfsq+R)); else + return k*ln2_hi-((hfsq-(s*(hfsq+R)+(k*ln2_lo+c)))-f); +} + +_MATH_ALIAS_f_f(log1p) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_log2.c b/src/audio/ffmpeg_dec/libm/common/sf_log2.c new file mode 100644 index 000000000000..42659c0f0d04 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_log2.c @@ -0,0 +1,101 @@ +/* Single-precision log2 function. + Copyright (c) 2017 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "fdlibm.h" +#if !__OBSOLETE_MATH_FLOAT + +#include +#include +#include "math_config.h" + +/* +LOG2F_TABLE_BITS = 4 +LOG2F_POLY_ORDER = 4 + +ULP error: 0.752 (nearest rounding.) +Relative error: 1.9 * 2^-26 (before rounding.) +*/ + +#define N (1 << LOG2F_TABLE_BITS) +#define T __log2f_data.tab +#define A __log2f_data.poly +#define OFF 0x3f330000 + +float +log2f (float x) +{ + /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ + double_t z, r, r2, p, y, y0, invc, logc; + uint32_t ix, iz, top, tmp; + int k, i; + + ix = asuint (x); +#if WANT_ROUNDING + /* Fix sign of zero with downward rounding when x==1. */ + if (__builtin_expect (ix == 0x3f800000, 0)) + return 0; +#endif + if (__builtin_expect (ix - 0x00800000 >= 0x7f800000 - 0x00800000, 0)) + { + /* x < 0x1p-126 or inf or nan. */ + if (ix * 2 == 0) + return __math_divzerof (1); + if (ix == 0x7f800000) /* log2(inf) == inf. */ + return x; + if ((ix & 0x80000000) || ix * 2 >= 0xff000000) + return __math_invalidf (x); + /* x is subnormal, normalize it. */ + ix = asuint (x * 0x1p23f); + ix -= (uint32_t) 23 << 23; + } + + /* x = 2^k z; where z is in range [OFF,2*OFF] and exact. + The range is split into N subintervals. + The ith subinterval contains z and c is near its center. */ + tmp = ix - OFF; + i = (tmp >> (23 - LOG2F_TABLE_BITS)) % N; + top = tmp & 0xff800000; + iz = ix - top; + k = (int32_t) tmp >> 23; /* arithmetic shift */ + invc = T[i].invc; + logc = T[i].logc; + z = (double_t) asfloat (iz); + + /* log2(x) = log1p(z/c-1)/ln2 + log2(c) + k */ + r = z * invc - 1; + y0 = logc + (double_t) k; + + /* Pipelined polynomial evaluation to approximate log1p(r)/ln2. */ + r2 = r * r; + y = A[1] * r + A[2]; + y = A[0] * r2 + y; + p = A[3] * r + y0; + y = y * r2 + p; + return (float) y; +} +#endif /* !__OBSOLETE_MATH_FLOAT */ diff --git a/src/audio/ffmpeg_dec/libm/common/sf_log2_data.c b/src/audio/ffmpeg_dec/libm/common/sf_log2_data.c new file mode 100644 index 000000000000..f20da4f3ec3c --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_log2_data.c @@ -0,0 +1,58 @@ +/* Data definition for log2f. + Copyright (c) 2017 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "fdlibm.h" +#if !__OBSOLETE_MATH_FLOAT + +#include "math_config.h" + +const struct log2f_data __log2f_data = { + .tab = { + { 0x1.661ec79f8f3bep+0, -0x1.efec65b963019p-2 }, + { 0x1.571ed4aaf883dp+0, -0x1.b0b6832d4fca4p-2 }, + { 0x1.49539f0f010bp+0, -0x1.7418b0a1fb77bp-2 }, + { 0x1.3c995b0b80385p+0, -0x1.39de91a6dcf7bp-2 }, + { 0x1.30d190c8864a5p+0, -0x1.01d9bf3f2b631p-2 }, + { 0x1.25e227b0b8eap+0, -0x1.97c1d1b3b7afp-3 }, + { 0x1.1bb4a4a1a343fp+0, -0x1.2f9e393af3c9fp-3 }, + { 0x1.12358f08ae5bap+0, -0x1.960cbbf788d5cp-4 }, + { 0x1.0953f419900a7p+0, -0x1.a6f9db6475fcep-5 }, + { 0x1p+0, 0x0p+0 }, + { 0x1.e608cfd9a47acp-1, 0x1.338ca9f24f53dp-4 }, + { 0x1.ca4b31f026aap-1, 0x1.476a9543891bap-3 }, + { 0x1.b2036576afce6p-1, 0x1.e840b4ac4e4d2p-3 }, + { 0x1.9c2d163a1aa2dp-1, 0x1.40645f0c6651cp-2 }, + { 0x1.886e6037841edp-1, 0x1.88e9c2c1b9ff8p-2 }, + { 0x1.767dcf5534862p-1, 0x1.ce0a44eb17bccp-2 }, + }, + .poly = { + -0x1.712b6f70a7e4dp-2, 0x1.ecabf496832ep-2, -0x1.715479ffae3dep-1, + 0x1.715475f35c8b8p0, + } +}; +#endif /* !__OBSOLETE_MATH_FLOAT */ diff --git a/src/audio/ffmpeg_dec/libm/common/sf_log_data.c b/src/audio/ffmpeg_dec/libm/common/sf_log_data.c new file mode 100644 index 000000000000..cc4dea4126c7 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_log_data.c @@ -0,0 +1,58 @@ +/* Data definition for logf. + Copyright (c) 2017 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "fdlibm.h" +#if !__OBSOLETE_MATH_FLOAT + +#include "math_config.h" + +const struct logf_data __logf_data = { + .tab = { + { 0x1.661ec79f8f3bep+0, -0x1.57bf7808caadep-2 }, + { 0x1.571ed4aaf883dp+0, -0x1.2bef0a7c06ddbp-2 }, + { 0x1.49539f0f010bp+0, -0x1.01eae7f513a67p-2 }, + { 0x1.3c995b0b80385p+0, -0x1.b31d8a68224e9p-3 }, + { 0x1.30d190c8864a5p+0, -0x1.6574f0ac07758p-3 }, + { 0x1.25e227b0b8eap+0, -0x1.1aa2bc79c81p-3 }, + { 0x1.1bb4a4a1a343fp+0, -0x1.a4e76ce8c0e5ep-4 }, + { 0x1.12358f08ae5bap+0, -0x1.1973c5a611cccp-4 }, + { 0x1.0953f419900a7p+0, -0x1.252f438e10c1ep-5 }, + { 0x1p+0, 0x0p+0 }, + { 0x1.e608cfd9a47acp-1, 0x1.aa5aa5df25984p-5 }, + { 0x1.ca4b31f026aap-1, 0x1.c5e53aa362eb4p-4 }, + { 0x1.b2036576afce6p-1, 0x1.526e57720db08p-3 }, + { 0x1.9c2d163a1aa2dp-1, 0x1.bc2860d22477p-3 }, + { 0x1.886e6037841edp-1, 0x1.1058bc8a07ee1p-2 }, + { 0x1.767dcf5534862p-1, 0x1.4043057b6ee09p-2 }, + }, + .ln2 = 0x1.62e42fefa39efp-1, + .poly = { + -0x1.00ea348b88334p-2, 0x1.5575b0be00b6ap-2, -0x1.ffffef20a4123p-2, + } +}; +#endif /* !__OBSOLETE_MATH_FLOAT */ diff --git a/src/audio/ffmpeg_dec/libm/common/sf_logb.c b/src/audio/ffmpeg_dec/libm/common/sf_logb.c new file mode 100644 index 000000000000..95c27683e0cc --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_logb.c @@ -0,0 +1,43 @@ +/* 2009 for Newlib: Sun's sf_ilogb.c converted to be sf_logb.c. */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* float logb(float x) + * return the binary exponent of non-zero x + * logbf(0) = -inf, raise divide-by-zero floating point exception + * logbf(+inf|-inf) = +inf (no signal is raised) + * logbf(NaN) = NaN (no signal is raised) + * Per C99 recommendation, a NaN argument is returned unchanged. + */ + +#include "fdlibm.h" + +float +logbf(float x) +{ + __int32_t hx,ix; + + GET_FLOAT_WORD(hx,x); + hx &= 0x7fffffff; + if(FLT_UWORD_IS_ZERO(hx)) { + /* arg==0: return -inf and raise divide-by-zero exception */ + return -1.f/fabsf(x); /* logbf(0) = -inf */ + } + if(FLT_UWORD_IS_SUBNORMAL(hx)) { + for (ix = -126,hx = lsl(hx, 8); hx>0; hx = lsl(hx, 1)) ix -=1; + return (float) ix; + } + else if (FLT_UWORD_IS_INFINITE(hx)) return HUGE_VALF; /* x==+|-inf */ + else if (FLT_UWORD_IS_NAN(hx)) return x + x; + else return (float) ((hx>>23)-127); +} + +_MATH_ALIAS_f_f(logb) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_lrint.c b/src/audio/ffmpeg_dec/libm/common/sf_lrint.c new file mode 100644 index 000000000000..b0f491c1a0ff --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_lrint.c @@ -0,0 +1,83 @@ + +/* @(#)sf_lrint.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * lrintf(x) + * Return x rounded to integral value according to the prevailing + * rounding mode. + * Method: + * Using floating addition. + * Exception: + * Inexact flag raised if x not equal to lrintf(x). + */ + +#include "fdlibm.h" +#include + +static const float +/* Adding a float, x, to 2^23 will cause the result to be rounded based on + the fractional part of x, according to the implementation's current rounding + mode. 2^23 is the smallest float that can be represented using all 23 significant + digits. */ +TWO23[2]={ + 8.3886080000e+06, /* 0x4b000000 */ + -8.3886080000e+06, /* 0xcb000000 */ +}; + +long int lrintf(float x) +{ + __int32_t j0,sx; + __uint32_t i0; + float t; + volatile float w; + long int result; + + GET_FLOAT_WORD(i0,x); + + /* Extract sign bit. */ + sx = (i0 >> 31); + + /* Extract exponent field. */ + j0 = ((i0 & 0x7f800000) >> 23) - 127; + + if (j0 < (int)(sizeof (long int) * 8) - 1) + { + if (j0 >= 23) + result = (long int) ((i0 & 0x7fffff) | 0x800000) << (j0 - 23); + else + { + w = TWO23[sx] + x; + t = w - TWO23[sx]; + GET_FLOAT_WORD (i0, t); + j0 = ((i0 >> 23) & 0xff) - 0x7f; + i0 &= 0x7fffff; + i0 |= 0x800000; + if (j0 < 0) + result = 0; + else + result = i0 >> (23 - j0); + } + } + else + { + if (x != LONG_MIN) + { + __math_set_invalidf(); + return sx ? LONG_MIN : LONG_MAX; + } + return (long int) x; + } + return sx ? -result : result; +} + +_MATH_ALIAS_j_f(lrint) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_lround.c b/src/audio/ffmpeg_dec/libm/common/sf_lround.c new file mode 100644 index 000000000000..f38d1e9b9abe --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_lround.c @@ -0,0 +1,53 @@ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" +#include + +long int lroundf(float x) +{ + __int32_t exponent_less_127; + __uint32_t w; + long int result; + __int32_t sign; + + GET_FLOAT_WORD (w, x); + exponent_less_127 = ((w & 0x7f800000) >> 23) - 127; + sign = (w & 0x80000000) != 0 ? -1 : 1; + w &= 0x7fffff; + w |= 0x800000; + + if (exponent_less_127 < (__int32_t)((8 * sizeof (long int)) - 1)) + { + if (exponent_less_127 < 0) + return exponent_less_127 < -1 ? 0 : sign; + else if (exponent_less_127 >= 23) + result = (long int) w << (exponent_less_127 - 23); + else + { + w += 0x400000 >> exponent_less_127; + result = w >> (23 - exponent_less_127); + } + } + else + { + /* Result other than LONG_MIN is too large to be represented by + * a long int. + */ + if (x != (float) LONG_MIN) + __math_set_invalidf(); + return sign == 1 ? LONG_MAX : LONG_MIN; + } + + return sign * result; +} + +_MATH_ALIAS_j_f(lround) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_modf.c b/src/audio/ffmpeg_dec/libm/common/sf_modf.c new file mode 100644 index 000000000000..921f9baf4470 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_modf.c @@ -0,0 +1,48 @@ +/* sf_modf.c -- float version of s_modf.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" + +float +modff(float x, float *iptr) +{ + __int32_t i0,j0; + __uint32_t i; + GET_FLOAT_WORD(i0,x); + j0 = ((i0>>23)&0xff)-0x7f; /* exponent of x */ + if(j0<23) { /* integer part in x */ + if(j0<0) { /* |x|<1 */ + SET_FLOAT_WORD(*iptr,i0&0x80000000); /* *iptr = +-0 */ + return x; + } else { + i = (0x007fffff)>>j0; + if((i0&i)==0) { /* x is integral */ + *iptr = x; + SET_FLOAT_WORD(x,i0&0x80000000); /* return +-0 */ + return x; + } else { + SET_FLOAT_WORD(*iptr,i0&(~i)); + return x - *iptr; + } + } + } else { /* no fraction part */ + *iptr = x; + if (__fpclassifyf(x) == FP_NAN) return *iptr = x+x; /* x is NaN, return NaN */ + SET_FLOAT_WORD(x,i0&0x80000000); /* return +-0 */ + return x; + } +} + +_MATH_ALIAS_f_fF(modf) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_nan.c b/src/audio/ffmpeg_dec/libm/common/sf_nan.c new file mode 100644 index 000000000000..d07c72ec1a45 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_nan.c @@ -0,0 +1,29 @@ +/* +Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + +Developed at SunPro, a Sun Microsystems, Inc. business. +Permission to use, copy, modify, and distribute this +software is freely granted, provided that this notice +is preserved. + */ +/* + * nanf () returns a nan. + * Added by Cygnus Support. + */ + +#include "fdlibm.h" + + float nanf(const char *unused) +{ + float x; + + (void) unused; +#if __GNUC_PREREQ (3, 3) + x = __builtin_nanf(""); +#else + SET_FLOAT_WORD(x,0x7fc00000); +#endif + return x; +} + +_MATH_ALIAS_f_s(nan) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_nearbyint.c b/src/audio/ffmpeg_dec/libm/common/sf_nearbyint.c new file mode 100644 index 000000000000..5d8008ade675 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_nearbyint.c @@ -0,0 +1,30 @@ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + + +#include +#include "fdlibm.h" + +float nearbyintf(float x) +{ + if (isnan(x)) return x + x; +#if defined(FE_INEXACT) && !defined(__DOUBLE_NOEXCEPT) + fenv_t env; + fegetenv(&env); +#endif + x = rintf(x); +#if defined(FE_INEXACT) && !defined(__DOUBLE_NOEXCEPT) + fesetenv(&env); +#endif + return x; +} + +_MATH_ALIAS_f_f(nearbyint) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_nextafter.c b/src/audio/ffmpeg_dec/libm/common/sf_nextafter.c new file mode 100644 index 000000000000..d717a996355b --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_nextafter.c @@ -0,0 +1,57 @@ +/* sf_nextafter.c -- float version of s_nextafter.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" + +float nextafterf(float x, float y) +{ + __int32_t hx,hy,ix,iy; + + GET_FLOAT_WORD(hx,x); + GET_FLOAT_WORD(hy,y); + ix = hx&0x7fffffff; /* |x| */ + iy = hy&0x7fffffff; /* |y| */ + + if(FLT_UWORD_IS_NAN(ix) || + FLT_UWORD_IS_NAN(iy)) + return x+y; + if(x==y) return y; /* x=y, return y */ + if(FLT_UWORD_IS_ZERO(ix)) { /* x == 0 */ + SET_FLOAT_WORD(x,(hy&0x80000000)|FLT_UWORD_MIN); + force_eval_float(opt_barrier_float(x)*x); + return x; + } + if(hx>=0) { /* x > 0 */ + if(hx>hy) { /* x > y, x -= ulp */ + hx -= 1; + } else { /* x < y, x += ulp */ + hx += 1; + } + } else { /* x < 0 */ + if(hy>=0||hx>hy){ /* x < y, x -= ulp */ + hx -= 1; + } else { /* x > y, x += ulp */ + hx += 1; + } + } + hy = hx&0x7f800000; + if(hy>FLT_UWORD_MAX) return check_oflowf(x+x); /* overflow */ + SET_FLOAT_WORD(x,hx); + if(hy<0x00800000) /* underflow */ + return __math_denormf(x); + return x; +} + +_MATH_ALIAS_f_ff(nextafter) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_pow.c b/src/audio/ffmpeg_dec/libm/common/sf_pow.c new file mode 100644 index 000000000000..13c27531e378 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_pow.c @@ -0,0 +1,254 @@ +/* Single-precision pow function. + Copyright (c) 2017-2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "fdlibm.h" +#if !__OBSOLETE_MATH_FLOAT + +#include +#include +#include "math_config.h" + +/* +POWF_LOG2_POLY_ORDER = 5 +EXP2F_TABLE_BITS = 5 + +ULP error: 0.82 (~ 0.5 + relerr*2^24) +relerr: 1.27 * 2^-26 (Relative error ~= 128*Ln2*relerr_log2 + relerr_exp2) +relerr_log2: 1.83 * 2^-33 (Relative error of logx.) +relerr_exp2: 1.69 * 2^-34 (Relative error of exp2(ylogx).) +*/ + +#define N (1 << POWF_LOG2_TABLE_BITS) +#define T __powf_log2_data.tab +#define A __powf_log2_data.poly +#define OFF 0x3f330000 + +/* Subnormal input is normalized so ix has negative biased exponent. + Output is multiplied by N (POWF_SCALE) if TOINT_INTRINICS is set. */ +static inline double_t +log2_inline (uint32_t ix) +{ + /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ + double_t z, r, r2, r4, p, q, y, y0, invc, logc; + uint32_t iz, top, tmp; + int k, i; + + /* x = 2^k z; where z is in range [OFF,2*OFF] and exact. + The range is split into N subintervals. + The ith subinterval contains z and c is near its center. */ + tmp = ix - OFF; + i = (tmp >> (23 - POWF_LOG2_TABLE_BITS)) % N; + top = tmp & 0xff800000; + iz = ix - top; + k = (int32_t) top >> (23 - POWF_SCALE_BITS); /* arithmetic shift */ + invc = T[i].invc; + logc = T[i].logc; + z = (double_t) asfloat (iz); + + /* log2(x) = log1p(z/c-1)/ln2 + log2(c) + k */ + r = z * invc - 1; + y0 = logc + (double_t) k; + + /* Pipelined polynomial evaluation to approximate log1p(r)/ln2. */ + r2 = r * r; + y = A[0] * r + A[1]; + p = A[2] * r + A[3]; + r4 = r2 * r2; + q = A[4] * r + y0; + q = p * r2 + q; + y = y * r4 + q; + return y; +} + +#undef N +#undef T +#define N (1 << EXP2F_TABLE_BITS) +#define T __exp2f_data.tab +#define SIGN_BIAS ((uint32_t) 1 << (EXP2F_TABLE_BITS + 11)) + +/* The output of log2 and thus the input of exp2 is either scaled by N + (in case of fast toint intrinsics) or not. The unscaled xd must be + in [-1021,1023], sign_bias sets the sign of the result. */ +static inline double_t +exp2_inline (double_t xd, uint32_t sign_bias) +{ + uint64_t ki, ski, t; + /* double_t for better performance on targets with FLT_EVAL_METHOD==2. */ + double_t kd, z, r, r2, y, s; + +#if TOINT_INTRINSICS +# define C __exp2f_data.poly_scaled + /* N*x = k + r with r in [-1/2, 1/2] */ + kd = roundtoint (xd); /* k */ + ki = converttoint (xd); +#else +# define C __exp2f_data.poly +# define SHIFT __exp2f_data.shift_scaled + /* x = k/N + r with r in [-1/(2N), 1/(2N)] */ + kd = (double) (xd + SHIFT); /* Rounding to double precision is required. */ + ki = asuint64 (kd); + kd -= SHIFT; /* k/N */ +#endif + r = xd - kd; + + /* exp2(x) = 2^(k/N) * 2^r ~= s * (C0*r^3 + C1*r^2 + C2*r + 1) */ + t = T[ki % N]; + ski = ki + sign_bias; + t += ski << (52 - EXP2F_TABLE_BITS); + s = asfloat64 (t); + z = C[0] * r + C[1]; + r2 = r * r; + y = C[2] * r + 1; + y = z * r2 + y; + y = y * s; + return y; +} + +/* Returns 0 if not int, 1 if odd int, 2 if even int. The argument is + the bit representation of a non-zero finite floating-point value. */ +static inline int +checkint (uint32_t iy) +{ + int e = iy >> 23 & 0xff; + if (e < 0x7f) + return 0; + if (e > 0x7f + 23) + return 2; + if (iy & (((uint32_t) 1 << (0x7f + 23 - e)) - 1)) + return 0; + if (iy & ((uint32_t) 1 << (0x7f + 23 - e))) + return 1; + return 2; +} + +static inline int +zeroinfnan (uint32_t ix) +{ + return 2 * ix - 1 >= 2u * (uint32_t) 0x7f800000 - 1; +} + +float +powf (float x, float y) +{ + uint32_t sign_bias = 0; + uint32_t ix, iy; + + ix = asuint (x); + iy = asuint (y); + if (__builtin_expect (ix - 0x00800000 >= 0x7f800000 - 0x00800000 + || zeroinfnan (iy), + 0)) + { + /* Either (x < 0x1p-126 or inf or nan) or (y is 0 or inf or nan). */ + if (__builtin_expect (zeroinfnan (iy), 0)) + { + if (2 * iy == 0) + return issignalingf_inline (x) ? x + y : 1.0f; + if (ix == 0x3f800000) + return issignalingf_inline (y) ? x + y : 1.0f; + if (2 * ix > 2u * (uint32_t) 0x7f800000 || 2 * iy > 2u * (uint32_t) 0x7f800000) + return x + y; + if (2 * ix == 2 * (uint32_t) 0x3f800000) + return 1.0f; + if ((2 * ix < 2 * (uint32_t) 0x3f800000) == !(iy & (uint32_t) 0x80000000)) + return 0.0f; /* |x|<1 && y==inf or |x|>1 && y==-inf. */ + return y * y; + } + if (__builtin_expect (zeroinfnan (ix), 0)) + { + float_t x2 = x * x; + if (ix & 0x80000000 && checkint (iy) == 1) + { + x2 = -x2; + sign_bias = 1; + } + if (!(iy & 0x80000000)) + return opt_barrier_float(x2); +#if WANT_ERRNO + if (2 * ix == 0) + return __math_divzerof (sign_bias); +#endif + return 1 / x2; + } + /* x and y are non-zero finite. */ + if (ix & 0x80000000) + { + /* Finite x < 0. */ + int yint = checkint (iy); + if (yint == 0) + return __math_invalidf (x); + if (yint == 1) + sign_bias = SIGN_BIAS; + ix &= 0x7fffffff; + } + if (ix < 0x00800000) + { + /* Normalize subnormal x so exponent becomes negative. */ + ix = asuint (x * 0x1p23f); + ix &= 0x7fffffff; + ix -= (uint32_t) 23 << 23; + } + } + double_t logx = log2_inline (ix); + double_t ylogx = (double) y * logx; /* Note: cannot overflow, y is single prec. */ + if (__builtin_expect ((asuint64 (ylogx) >> 47 & 0xffff) + >= asuint64 (126.0 * POWF_SCALE) >> 47, + 0)) + { + /* |y*log(x)| >= 126. */ + if (ylogx > 0x1.fffffffd1d571p+6 * POWF_SCALE) + /* |x^y| > 0x1.ffffffp127. */ + return __math_oflowf (sign_bias); +#if WANT_ROUNDING && WANT_ERRNO + if (ylogx > 0x1.fffffffa3aae2p+6 * POWF_SCALE) + /* |x^y| > 0x1.fffffep127, check if we round away from 0. */ + if ((!sign_bias + && eval_as_float (1.0f + opt_barrier_float (0x1p-25f)) != 1.0f) + || (sign_bias + && eval_as_float (-1.0f - opt_barrier_float (0x1p-25f)) + != -1.0f)) + return __math_oflowf (sign_bias); +#endif + if (ylogx <= -150.0 * POWF_SCALE) + return __math_uflowf (sign_bias); +#if WANT_ERRNO_UFLOW + if (ylogx < -149.0 * POWF_SCALE) + return __math_may_uflowf (sign_bias); +#endif + } + return (float) exp2_inline (ylogx, sign_bias); +} + +#ifdef __strong_reference +#if defined(__GNUCLIKE_PRAGMA_DIAGNOSTIC) && !defined(__clang__) +#pragma GCC diagnostic ignored "-Wmissing-attributes" +#endif +__strong_reference(powf, _powf); +#endif + +#endif /* !__OBSOLETE_MATH_FLOAT */ diff --git a/src/audio/ffmpeg_dec/libm/common/sf_pow10.c b/src/audio/ffmpeg_dec/libm/common/sf_pow10.c new file mode 100644 index 000000000000..06cb22b637ef --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_pow10.c @@ -0,0 +1,35 @@ +/* sf_pow10.c -- float version of s_pow10.c. + * Modification of sf_pow10.c by Yaakov Selkowitz 2007. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * wrapper pow10f(x) + */ + +#define _GNU_SOURCE +#undef pow10f +#include "fdlibm.h" +#include +#include + +float +pow10f(float x) /* wrapper pow10f */ +{ + return _powf(10.0, x); +} + +#undef pow10 +#undef pow10l + +_MATH_ALIAS_f_f(pow10) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_pow_log2_data.c b/src/audio/ffmpeg_dec/libm/common/sf_pow_log2_data.c new file mode 100644 index 000000000000..98f58a595f9e --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_pow_log2_data.c @@ -0,0 +1,59 @@ +/* Data definition for powf. + Copyright (c) 2017 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "fdlibm.h" +#if !__OBSOLETE_MATH_FLOAT + +#include "math_config.h" + +const struct powf_log2_data __powf_log2_data = { + .tab = { + { 0x1.661ec79f8f3bep+0, -0x1.efec65b963019p-2 * POWF_SCALE }, + { 0x1.571ed4aaf883dp+0, -0x1.b0b6832d4fca4p-2 * POWF_SCALE }, + { 0x1.49539f0f010bp+0, -0x1.7418b0a1fb77bp-2 * POWF_SCALE }, + { 0x1.3c995b0b80385p+0, -0x1.39de91a6dcf7bp-2 * POWF_SCALE }, + { 0x1.30d190c8864a5p+0, -0x1.01d9bf3f2b631p-2 * POWF_SCALE }, + { 0x1.25e227b0b8eap+0, -0x1.97c1d1b3b7afp-3 * POWF_SCALE }, + { 0x1.1bb4a4a1a343fp+0, -0x1.2f9e393af3c9fp-3 * POWF_SCALE }, + { 0x1.12358f08ae5bap+0, -0x1.960cbbf788d5cp-4 * POWF_SCALE }, + { 0x1.0953f419900a7p+0, -0x1.a6f9db6475fcep-5 * POWF_SCALE }, + { 0x1p+0, 0x0p+0 * POWF_SCALE }, + { 0x1.e608cfd9a47acp-1, 0x1.338ca9f24f53dp-4 * POWF_SCALE }, + { 0x1.ca4b31f026aap-1, 0x1.476a9543891bap-3 * POWF_SCALE }, + { 0x1.b2036576afce6p-1, 0x1.e840b4ac4e4d2p-3 * POWF_SCALE }, + { 0x1.9c2d163a1aa2dp-1, 0x1.40645f0c6651cp-2 * POWF_SCALE }, + { 0x1.886e6037841edp-1, 0x1.88e9c2c1b9ff8p-2 * POWF_SCALE }, + { 0x1.767dcf5534862p-1, 0x1.ce0a44eb17bccp-2 * POWF_SCALE }, + }, + .poly = { + 0x1.27616c9496e0bp-2 * POWF_SCALE, -0x1.71969a075c67ap-2 * POWF_SCALE, + 0x1.ec70a6ca7baddp-2 * POWF_SCALE, -0x1.7154748bef6c8p-1 * POWF_SCALE, + 0x1.71547652ab82bp0 * POWF_SCALE, + } +}; +#endif /* !__OBSOLETE_MATH_FLOAT */ diff --git a/src/audio/ffmpeg_dec/libm/common/sf_remquo.c b/src/audio/ffmpeg_dec/libm/common/sf_remquo.c new file mode 100644 index 000000000000..fb71f9836bca --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_remquo.c @@ -0,0 +1,132 @@ +/* Adapted for Newlib, 2009. (Allow for int < 32 bits; return *quo=0 during + * errors to make test scripts easier.) */ +/* @(#)e_fmod.c 1.3 95/01/18 */ +/*- + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunSoft, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include +#include "fdlibm.h" + +/* For quotient, return either all 31 bits that can from calculation (using + * int32_t), or as many as can fit into an int that is smaller than 32 bits. */ +#if INT_MAX > 0x7FFFFFFFL + #define QUO_MASK 0x7FFFFFFF +# else + #define QUO_MASK INT_MAX +#endif + +static const float Zero[] = {0.0, -0.0,}; + +/* + * Return the IEEE remainder and set *quo to the last n bits of the + * quotient, rounded to the nearest integer. We choose n=31--if that many fit-- + * we wind up computing all the integer bits of the quotient anyway as + * a side-effect of computing the remainder by the shift and subtract + * method. In practice, this is far more bits than are needed to use + * remquo in reduction algorithms. + */ +float +remquof(float x, float y, int *quo) +{ + __int32_t n,hx,hy,hz,ix,iy,sx,i; + __uint32_t q,sxy; + + GET_FLOAT_WORD(hx,x); + GET_FLOAT_WORD(hy,y); + sxy = (hx ^ hy) & 0x80000000; + sx = hx&0x80000000; /* sign of x */ + hx ^=sx; /* |x| */ + hy &= 0x7fffffff; /* |y| */ + + /* purge off exception values */ + if(hy==0||hx>=0x7f800000||hy>0x7f800000) { /* y=0,NaN;or x not finite */ + *quo = 0; /* Not necessary, but return consistent value */ + return (x*y)/(x*y); + } + if(hx>31]; /* |x|=|y| return x*0*/ + } + + /* determine ix = ilogb(x) */ + if(hx<0x00800000) { /* subnormal x */ + for (ix = -126,i=lsl(hx, 8); i>0; i = lsl(i, 1)) ix -=1; + } else ix = (hx>>23)-127; + + /* determine iy = ilogb(y) */ + if(hy<0x00800000) { /* subnormal y */ + for (iy = -126,i=(lsl(hy, 8)); i>0; i = lsl(i, 1)) iy -=1; + } else iy = (hy>>23)-127; + + /* set up {hx,lx}, {hy,ly} and align y to x */ + if(ix >= -126) + hx = 0x00800000|(0x007fffff&hx); + else { /* subnormal x, shift x to normal */ + n = -126-ix; + hx = lsl(hx, n); + } + if(iy >= -126) + hy = 0x00800000|(0x007fffff&hy); + else { /* subnormal y, shift y to normal */ + n = -126-iy; + hy = lsl(hy, n); + } + + /* fix point fmod */ + n = ix - iy; + q = 0; + while(n--) { + hz=hx-hy; + if(hz<0) hx = lsl(hx, 1); + else {hx = lsl(hz, 1); q++;} + q <<= 1; + } + hz=hx-hy; + if(hz>=0) {hx=hz;q++;} + + /* convert back to floating value and restore the sign */ + if(hx==0) { /* return sign(x)*0 */ + *quo = (sxy ? -q : q); + return Zero[(__uint32_t)sx>>31]; + } + while(hx<0x00800000) { /* normalize x */ + hx = lsl(hx, 1); + iy -= 1; + } + if(iy>= -126) { /* normalize output */ + hx = ((hx-0x00800000)|lsl((iy+127), 23)); + } else { /* subnormal output */ + n = -126 - iy; + hx >>= n; + } +fixup: + SET_FLOAT_WORD(x,hx); + y = fabsf(y); + if (y < 0x1p-125f) { + if (x+x>y || (x+x==y && (q & 1))) { + q++; + x-=y; + } + } else if (x>0.5f*y || (x==0.5f*y && (q & 1))) { + q++; + x-=y; + } + GET_FLOAT_WORD(hx,x); + SET_FLOAT_WORD(x,hx^sx); + q &= 0x7fffffff; + *quo = (sxy ? -q : q); + return x; +} + +_MATH_ALIAS_f_ffI(remquo) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_rint.c b/src/audio/ffmpeg_dec/libm/common/sf_rint.c new file mode 100644 index 000000000000..4722b30975de --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_rint.c @@ -0,0 +1,65 @@ +/* sf_rint.c -- float version of s_rint.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" + +static const float +TWO23[2]={ + 8.3886080000e+06, /* 0x4b000000 */ + -8.3886080000e+06, /* 0xcb000000 */ +}; + +float +rintf(float x) +{ + __int32_t i0,j0,sx; + __uint32_t i,i1,ix; + float t; + volatile float w; + GET_FLOAT_WORD(i0,x); + sx = (i0>>31)&1; + ix = (i0&0x7fffffff); + j0 = (ix>>23)-0x7f; + if(j0<23) { + if(FLT_UWORD_IS_ZERO(ix)) + return x; + if(j0<0) { + i1 = (i0&0x07fffff); + i0 &= 0xfff00000; + i0 |= ((i1|-i1)>>9)&0x400000; + SET_FLOAT_WORD(x,i0); + w = TWO23[sx]+x; + t = w-TWO23[sx]; + GET_FLOAT_WORD(i0,t); + SET_FLOAT_WORD(t,(i0&0x7fffffff)|lsl(sx, 31)); + return t; + } else { + i = (0x007fffff)>>j0; + if((i0&i)==0) return x; /* x is integral */ + i>>=1; + if((i0&i)!=0) i0 = (i0&(~i))|((0x200000)>>j0); + } + } else { + if(!FLT_UWORD_IS_FINITE(ix)) + return opt_barrier_float(x+x); /* inf or NaN */ + else + return x; /* x is integral */ + } + SET_FLOAT_WORD(x,i0); + w = TWO23[sx]+x; + return w-TWO23[sx]; +} + +_MATH_ALIAS_f_f(rint) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_round.c b/src/audio/ffmpeg_dec/libm/common/sf_round.c new file mode 100644 index 000000000000..63142beb7bca --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_round.c @@ -0,0 +1,58 @@ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" + +float +roundf(float x) +{ + __uint32_t w; + /* Most significant word, least significant word. */ + int exponent_less_127; + + GET_FLOAT_WORD(w, x); + + /* Extract exponent field. */ + exponent_less_127 = (int)((w & 0x7f800000) >> 23) - 127; + + if (exponent_less_127 < 23) + { + if (exponent_less_127 < 0) + { + w &= 0x80000000; + if (exponent_less_127 == -1) + /* Result is +1.0 or -1.0. */ + w |= ((__uint32_t)127 << 23); + } + else + { + __uint32_t exponent_mask = 0x007fffff >> exponent_less_127; + if ((w & exponent_mask) == 0) + /* x has an integral value. */ + return x; + + w += 0x00400000 >> exponent_less_127; + w &= ~exponent_mask; + } + } + else + { + if (exponent_less_127 == 128) + /* x is NaN or infinite. */ + return x + x; + else + return x; + } + SET_FLOAT_WORD(x, w); + return x; +} + +_MATH_ALIAS_f_f(round) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_scalbln.c b/src/audio/ffmpeg_dec/libm/common/sf_scalbln.c new file mode 100644 index 000000000000..25f04ebac8d2 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_scalbln.c @@ -0,0 +1,55 @@ +/* s_scalbnf.c -- float version of s_scalbn.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" + +static const float +two25 = 3.355443200e+07, /* 0x4c000000 */ +twom25 = 2.9802322388e-08; /* 0x33000000 */ + +float +scalblnf (float x, long int n) +{ + __int32_t ix; + uint32_t hx; + long int k; + + GET_FLOAT_WORD(ix,x); + hx = ix&0x7fffffff; + k = hx>>23; /* extract exponent */ + if (k==0) { /* 0 or subnormal x */ + if (hx == 0) return x; /* +-0 */ + x *= two25; + GET_FLOAT_WORD(ix,x); + k = ((ix&0x7f800000)>>23) - 25; + if (n< -50000) + return __math_uflowf(ix<0); /*underflow*/ + } + if (k==0xff) return x+x; /* NaN or Inf */ + if (n > 50000) + return __math_oflowf(ix < 0); /* overflow */ + k = k+n; + if (k > 0xfe) + return __math_oflowf(ix < 0); /* overflow */ + if (k > 0) /* normal result */ + {SET_FLOAT_WORD(x,(ix&0x807fffff)|(k<<23)); return x;} + if (k <= -25) + return __math_uflowf(ix < 0); /*underflow*/ + k += 25; /* subnormal result */ + SET_FLOAT_WORD(x,(ix&0x807fffff)|(k<<23)); + return check_uflowf(x*twom25); +} + +_MATH_ALIAS_f_fj(scalbln) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_scalbn.c b/src/audio/ffmpeg_dec/libm/common/sf_scalbn.c new file mode 100644 index 000000000000..886cfe2f1853 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_scalbn.c @@ -0,0 +1,79 @@ +/* sf_scalbn.c -- float version of s_scalbn.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" +#include +#include + +#if INT_MAX > 50000 +#define OVERFLOW_INT 50000 +#else +#define OVERFLOW_INT 30000 +#endif + +static const float +two25 = 3.355443200e+07, /* 0x4c000000 */ +twom25 = 2.9802322388e-08; /* 0x33000000 */ + +float scalbnf (float x, int n) +{ + __int32_t k,ix; + __uint32_t hx; + + GET_FLOAT_WORD(ix,x); + hx = ix&0x7fffffff; + k = hx>>23; /* extract exponent */ + if (k == 0) { + if (hx == 0) return x; + x *= two25; + GET_FLOAT_WORD(ix,x); + k = ((ix&0x7f800000)>>23) - 25; +#if __SIZEOF_INT__ > 2 + if (n< -50000) + return __math_uflowf(ix<0); /*underflow*/ +#endif + } + if (k == 0xff) return x + x; /* NaN or Inf */ + if (n > OVERFLOW_INT) /* in case integer overflow in n+k */ + return __math_oflowf(ix<0); /*overflow*/ + k = k+n; + if (k > FLT_LARGEST_EXP) + return __math_oflowf(ix<0); /* overflow */ + if (k > 0) /* normal result */ + {SET_FLOAT_WORD(x,(ix&0x807fffff)|(k<<23)); return x;} + if (k <= -25) + return __math_uflowf(ix<0); /*underflow*/ + k += 25; /* subnormal result */ + SET_FLOAT_WORD(x,(ix&0x807fffff)|(k<<23)); + return check_uflowf(x*twom25); +} + +#ifdef __strong_reference +#if defined(__GNUCLIKE_PRAGMA_DIAGNOSTIC) && !defined(__clang__) +#pragma GCC diagnostic ignored "-Wmissing-attributes" +#endif +__strong_reference(scalbnf, ldexpf); +#else + +float +ldexpf(float value, int exp) +{ + return scalbnf(value, exp); +} + +#endif + +_MATH_ALIAS_f_fi(scalbn) +_MATH_ALIAS_f_fi(ldexp) diff --git a/src/audio/ffmpeg_dec/libm/common/sf_trunc.c b/src/audio/ffmpeg_dec/libm/common/sf_trunc.c new file mode 100644 index 000000000000..98b0cb2bef07 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sf_trunc.c @@ -0,0 +1,40 @@ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" + +float +truncf(float x) +{ + int32_t ix = _asint32 (x); + int32_t mask; + int exp; + + exp = _exponent32(ix) - 127; + + if (unlikely(exp == 128)) + return x + x; + + /* compute portion of value with useful bits */ + if (exp < 0) + /* less than one, save sign bit */ + mask = 0x80000000; + else { + /* otherwise, save sign, exponent and any useful bits */ + if (exp >= 32) + exp = 31; + mask = ~(0x007fffff >> exp); + } + + return _asfloat(ix & mask); +} + +_MATH_ALIAS_f_f(trunc) diff --git a/src/audio/ffmpeg_dec/libm/common/signgam.c b/src/audio/ffmpeg_dec/libm/common/signgam.c new file mode 100644 index 000000000000..f4848df028da --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/signgam.c @@ -0,0 +1,43 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright © 2019 Keith Packard + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +#ifdef __weak_reference +int __signgam; +__weak_reference(__signgam, signgam); +#else +int signgam; +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/sincosf.c b/src/audio/ffmpeg_dec/libm/common/sincosf.c new file mode 100644 index 000000000000..f9db51ea9087 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sincosf.c @@ -0,0 +1,105 @@ +/* Single-precision sincos function. + Copyright (c) 2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "fdlibm.h" +#if !__OBSOLETE_MATH_FLOAT + +#include +#include +#include "math_config.h" +#include "sincosf.h" + +/* Fast sincosf implementation. Worst-case ULP is 0.5607, maximum relative + error is 0.5303 * 2^-23. A single-step range reduction is used for + small values. Large inputs have their range reduced using fast integer + arithmetic. */ +void +sincosf (float y, float *sinp, float *cosp) +{ + double x = (double) y; + double s; + int n; + const sincos_t *p = &__sincosf_table[0]; + + if (abstop12 (y) < abstop12 (pio4)) + { + double x2 = x * x; + + if (unlikely (abstop12 (y) < abstop12 (0x1p-12f))) + { + if (unlikely (abstop12 (y) < abstop12 (0x1p-126f))) + /* Force underflow for tiny y. */ + force_eval_float (x2); + *sinp = y; + *cosp = 1.0f; + return; + } + + sincosf_poly (x, x2, p, 0, sinp, cosp); + } + else if (abstop12 (y) < abstop12 (120.0f)) + { + x = reduce_fast (x, p, &n); + + /* Setup the signs for sin and cos. */ + s = p->sign[n & 3]; + + if (n & 2) + p = &__sincosf_table[1]; + + sincosf_poly (x * s, x * x, p, n, sinp, cosp); + } + else if (likely (abstop12 (y) < abstop12 (INFINITY))) + { + uint32_t xi = asuint (y); + int sign = xi >> 31; + + x = reduce_large (xi, &n); + + /* Setup signs for sin and cos - include original sign. */ + s = p->sign[(n + sign) & 3]; + + if ((n + sign) & 2) + p = &__sincosf_table[1]; + + sincosf_poly (x * s, x * x, p, n, sinp, cosp); + } + else + { + /* Return NaN if Inf or NaN for both sin and cos. */ + *sinp = *cosp = y - y; +#if WANT_ERRNO + /* Needed to set errno for +-Inf, the add is a hack to work + around a gcc register allocation issue: just passing y + affects code generation in the fast path. */ + __math_invalidf (y + y); +#endif + } +} + +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/sincosf.h b/src/audio/ffmpeg_dec/libm/common/sincosf.h new file mode 100644 index 000000000000..46902a367f75 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sincosf.h @@ -0,0 +1,174 @@ +/* Header for single-precision sin/cos/sincos functions. + Copyright (c) 2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include +#include +#include "math_config.h" + +/* 2PI * 2^-64. */ +static const double pi63 = 0x1.921FB54442D18p-62; +/* PI / 4. */ +static const double pio4 = 0x1.921FB54442D18p-1; + +/* The constants and polynomials for sine and cosine. */ +typedef struct +{ + double sign[4]; /* Sign of sine in quadrants 0..3. */ + double hpi_inv; /* 2 / PI ( * 2^24 if !TOINT_INTRINSICS). */ + double hpi; /* PI / 2. */ + double c0, c1, c2, c3, c4; /* Cosine polynomial. */ + double s1, s2, s3; /* Sine polynomial. */ +} sincos_t; + +/* Polynomial data (the cosine polynomial is negated in the 2nd entry). */ +extern const sincos_t __sincosf_table[2] HIDDEN; + +/* Table with 4/PI to 192 bit precision. */ +extern const uint32_t __inv_pio4[] HIDDEN; + +/* Top 12 bits of the float representation with the sign bit cleared. */ +static inline uint32_t +abstop12 (float x) +{ + return (asuint (x) >> 20) & 0x7ff; +} + +/* Compute the sine and cosine of inputs X and X2 (X squared), using the + polynomial P and store the results in SINP and COSP. N is the quadrant, + if odd the cosine and sine polynomials are swapped. */ +static inline void +sincosf_poly (double x, double x2, const sincos_t *p, int n, float *sinp, + float *cosp) +{ + double x3, x4, x5, x6, s, c, c1, c2, s1; + + x4 = x2 * x2; + x3 = x2 * x; + c2 = p->c3 + x2 * p->c4; + s1 = p->s2 + x2 * p->s3; + + /* Swap sin/cos result based on quadrant. */ + float *tmp = (n & 1 ? cosp : sinp); + cosp = (n & 1 ? sinp : cosp); + sinp = tmp; + + c1 = p->c0 + x2 * p->c1; + x5 = x3 * x2; + x6 = x4 * x2; + + s = x + x3 * p->s1; + c = c1 + x4 * p->c2; + + *sinp = s + x5 * s1; + *cosp = c + x6 * c2; +} + +/* Return the sine of inputs X and X2 (X squared) using the polynomial P. + N is the quadrant, and if odd the cosine polynomial is used. */ +static inline float +sinf_poly (double x, double x2, const sincos_t *p, int n) +{ + double x3, x4, x6, x7, s, c, c1, c2, s1; + + if ((n & 1) == 0) + { + x3 = x * x2; + s1 = p->s2 + x2 * p->s3; + + x7 = x3 * x2; + s = x + x3 * p->s1; + + return s + x7 * s1; + } + else + { + x4 = x2 * x2; + c2 = p->c3 + x2 * p->c4; + c1 = p->c0 + x2 * p->c1; + + x6 = x4 * x2; + c = c1 + x4 * p->c2; + + return c + x6 * c2; + } +} + +/* Fast range reduction using single multiply-subtract. Return the modulo of + X as a value between -PI/4 and PI/4 and store the quadrant in NP. + The values for PI/2 and 2/PI are accessed via P. Since PI/2 as a double + is accurate to 55 bits and the worst-case cancellation happens at 6 * PI/4, + the result is accurate for |X| <= 120.0. */ +static inline double +reduce_fast (double x, const sincos_t *p, int *np) +{ + double r; +#if TOINT_INTRINSICS + /* Use fast round and lround instructions when available. */ + r = x * p->hpi_inv; + *np = converttoint (r); + return x - roundtoint (r) * p->hpi; +#else + /* Use scaled float to int conversion with explicit rounding. + hpi_inv is prescaled by 2^24 so the quadrant ends up in bits 24..31. + This avoids inaccuracies introduced by truncating negative values. */ + r = x * p->hpi_inv; + int n = ((int32_t)r + 0x800000) >> 24; + *np = n; + return x - n * p->hpi; +#endif +} + +/* Reduce the range of XI to a multiple of PI/2 using fast integer arithmetic. + XI is a reinterpreted float and must be >= 2.0f (the sign bit is ignored). + Return the modulo between -PI/4 and PI/4 and store the quadrant in NP. + Reduction uses a table of 4/PI with 192 bits of precision. A 32x96->128 bit + multiply computes the exact 2.62-bit fixed-point modulo. Since the result + can have at most 29 leading zeros after the binary point, the double + precision result is accurate to 33 bits. */ +static inline double +reduce_large (uint32_t xi, int *np) +{ + const uint32_t *arr = &__inv_pio4[(xi >> 26) & 15]; + int shift = (xi >> 23) & 7; + uint64_t n, res0, res1, res2; + + xi = (xi & 0xffffff) | 0x800000; + xi <<= shift; + + res0 = xi * arr[0]; + res1 = (uint64_t)xi * arr[4]; + res2 = (uint64_t)xi * arr[8]; + res0 = (res2 >> 32) | (res0 << 32); + res0 += res1; + + n = (res0 + (1ULL << 61)) >> 62; + res0 -= n << 62; + double x = (int64_t)res0; + *np = n; + return x * pi63; +} diff --git a/src/audio/ffmpeg_dec/libm/common/sincosf_data.c b/src/audio/ffmpeg_dec/libm/common/sincosf_data.c new file mode 100644 index 000000000000..96afd2698e2d --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sincosf_data.c @@ -0,0 +1,89 @@ +/* Data definitions for sinf, cosf and sincosf. + Copyright (c) 2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "fdlibm.h" +#if !__OBSOLETE_MATH_FLOAT + +#include +#include +#include "math_config.h" +#include "sincosf.h" + +/* The constants and polynomials for sine and cosine. The 2nd entry + computes -cos (x) rather than cos (x) to get negation for free. */ +const sincos_t __sincosf_table[2] = +{ + { + { 1.0, -1.0, -1.0, 1.0 }, +#if TOINT_INTRINSICS + 0x1.45F306DC9C883p-1, +#else + 0x1.45F306DC9C883p+23, +#endif + 0x1.921FB54442D18p0, + 0x1p0, + -0x1.ffffffd0c621cp-2, + 0x1.55553e1068f19p-5, + -0x1.6c087e89a359dp-10, + 0x1.99343027bf8c3p-16, + -0x1.555545995a603p-3, + 0x1.1107605230bc4p-7, + -0x1.994eb3774cf24p-13 + }, + { + { 1.0, -1.0, -1.0, 1.0 }, +#if TOINT_INTRINSICS + 0x1.45F306DC9C883p-1, +#else + 0x1.45F306DC9C883p+23, +#endif + 0x1.921FB54442D18p0, + -0x1p0, + 0x1.ffffffd0c621cp-2, + -0x1.55553e1068f19p-5, + 0x1.6c087e89a359dp-10, + -0x1.99343027bf8c3p-16, + -0x1.555545995a603p-3, + 0x1.1107605230bc4p-7, + -0x1.994eb3774cf24p-13 + } +}; + +/* Table with 4/PI to 192 bit precision. To avoid unaligned accesses + only 8 new bits are added per entry, making the table 4 times larger. */ +const uint32_t __inv_pio4[24] = +{ + 0xa2, 0xa2f9, 0xa2f983, 0xa2f9836e, + 0xf9836e4e, 0x836e4e44, 0x6e4e4415, 0x4e441529, + 0x441529fc, 0x1529fc27, 0x29fc2757, 0xfc2757d1, + 0x2757d1f5, 0x57d1f534, 0xd1f534dd, 0xf534ddc0, + 0x34ddc0db, 0xddc0db62, 0xc0db6295, 0xdb629599, + 0x6295993c, 0x95993c43, 0x993c4390, 0x3c439041 +}; + +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/sinf.c b/src/audio/ffmpeg_dec/libm/common/sinf.c new file mode 100644 index 000000000000..c09b8ef78e70 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sinf.c @@ -0,0 +1,93 @@ +/* Single-precision sin function. + Copyright (c) 2018 Arm Ltd. All rights reserved. + + SPDX-License-Identifier: BSD-3-Clause + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the company may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY ARM LTD ``AS IS'' AND ANY EXPRESS OR IMPLIED + WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL ARM LTD BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ + +#include "fdlibm.h" +#if !__OBSOLETE_MATH_FLOAT + +#include +#include "math_config.h" +#include "sincosf.h" + +/* Fast sinf implementation. Worst-case ULP is 0.5607, maximum relative + error is 0.5303 * 2^-23. A single-step range reduction is used for + small values. Large inputs have their range reduced using fast integer + arithmetic. */ +float +sinf (float y) +{ + double x = (double) y; + double s; + int n; + const sincos_t *p = &__sincosf_table[0]; + + if (abstop12 (y) < abstop12 (pio4)) + { + s = x * x; + + if (unlikely (abstop12 (y) < abstop12 (0x1p-12f))) + { + if (unlikely (abstop12 (y) < abstop12 (0x1p-126f))) + /* Force underflow for tiny y. */ + force_eval_float (s); + return y; + } + + return sinf_poly (x, s, p, 0); + } + else if (likely (abstop12 (y) < abstop12 (120.0f))) + { + x = reduce_fast (x, p, &n); + + /* Setup the signs for sin and cos. */ + s = p->sign[n & 3]; + + if (n & 2) + p = &__sincosf_table[1]; + + return sinf_poly (x * s, x * x, p, n); + } + else if (abstop12 (y) < abstop12 (INFINITY)) + { + uint32_t xi = asuint (y); + int sign = xi >> 31; + + x = reduce_large (xi, &n); + + /* Setup signs for sin and cos - include original sign. */ + s = p->sign[(n + sign) & 3]; + + if ((n + sign) & 2) + p = &__sincosf_table[1]; + + return sinf_poly (x * s, x * x, p, n); + } + else + return __math_invalidf (y); +} + +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/sl_iseqsig.c b/src/audio/ffmpeg_dec/libm/common/sl_iseqsig.c new file mode 100644 index 000000000000..0792299545f2 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sl_iseqsig.c @@ -0,0 +1,50 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright © 2022 Keith Packard + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "fdlibm.h" + +#ifdef _NEED_MATH_HUGE + +int +__iseqsigl(long double x, long double y) +{ + if (isnan(x)) + return __math_invalid(x); + if (isnan(y)) + return __math_invalid(y); + return x == y; +} + +#endif diff --git a/src/audio/ffmpeg_dec/libm/common/sl_issignaling.c b/src/audio/ffmpeg_dec/libm/common/sl_issignaling.c new file mode 100644 index 000000000000..6f5db0a57126 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/common/sl_issignaling.c @@ -0,0 +1,44 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright © 2019 Keith Packard + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "math_config.h" + +#if defined(_NEED_FLOAT_HUGE) && __HAVE_BUILTIN_ISSIGNALINGL +int +__issignalingl (long double x) +{ + return __builtin_issignalingl(x); +} +#endif diff --git a/src/audio/ffmpeg_dec/libm/math/k_cos.c b/src/audio/ffmpeg_dec/libm/math/k_cos.c new file mode 100644 index 000000000000..a42ef35b77f6 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/k_cos.c @@ -0,0 +1,87 @@ + +/* @(#)k_cos.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * __kernel_cos( x, y ) + * kernel cos function on [-pi/4, pi/4], pi/4 ~ 0.785398164 + * Input x is assumed to be bounded by ~pi/4 in magnitude. + * Input y is the tail of x. + * + * Algorithm + * 1. Since cos(-x) = cos(x), we need only to consider positive x. + * 2. if x < 2^-27 (hx<0x3e400000 0), return 1 with inexact if x!=0. + * 3. cos(x) is approximated by a polynomial of degree 14 on + * [0,pi/4] + * 4 14 + * cos(x) ~ 1 - x*x/2 + C1*x + ... + C6*x + * where the remez error is + * + * | 2 4 6 8 10 12 14 | -58 + * |cos(x)-(1-.5*x +C1*x +C2*x +C3*x +C4*x +C5*x +C6*x )| <= 2 + * | | + * + * 4 6 8 10 12 14 + * 4. let r = C1*x +C2*x +C3*x +C4*x +C5*x +C6*x , then + * cos(x) = 1 - x*x/2 + r + * since cos(x+y) ~ cos(x) - sin(x)*y + * ~ cos(x) - x*y, + * a correction term is necessary in cos(x) and hence + * cos(x+y) = 1 - (x*x/2 - (r - x*y)) + * For better accuracy when x > 0.3, let qx = |x|/4 with + * the last 32 bits mask off, and if x > 0.78125, let qx = 0.28125. + * Then + * cos(x+y) = (1-qx) - ((x*x/2-qx) - (r-x*y)). + * Note that 1-qx and (x*x/2-qx) is EXACT here, and the + * magnitude of the latter is at least a quarter of x*x/2, + * thus, reducing the rounding error in the subtraction. + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +static const __float64 + one = _F_64(1.00000000000000000000e+00), /* 0x3FF00000, 0x00000000 */ + C1 = _F_64(4.16666666666666019037e-02), /* 0x3FA55555, 0x5555554C */ + C2 = _F_64(-1.38888888888741095749e-03), /* 0xBF56C16C, 0x16C15177 */ + C3 = _F_64(2.48015872894767294178e-05), /* 0x3EFA01A0, 0x19CB1590 */ + C4 = _F_64(-2.75573143513906633035e-07), /* 0xBE927E4F, 0x809C52AD */ + C5 = _F_64(2.08757232129817482790e-09), /* 0x3E21EE9E, 0xBDB4B1C4 */ + C6 = _F_64(-1.13596475577881948265e-11); /* 0xBDA8FAE9, 0xBE8838D4 */ + +__float64 +__kernel_cos(__float64 x, __float64 y) +{ + __float64 a, hz, z, r, qx; + __int32_t ix; + GET_HIGH_WORD(ix, x); + ix &= 0x7fffffff; /* ix = |x|'s high word*/ + if (ix < 0x3e400000) /* if x < 2**27 */ + return one; + z = x * x; + r = z * (C1 + z * (C2 + z * (C3 + z * (C4 + z * (C5 + z * C6))))); + if (ix < 0x3FD33333) /* if |x| < 0.3 */ + return one - (_F_64(0.5) * z - (z * r - x * y)); + else { + if (ix > 0x3fe90000) { /* x > 0.78125 */ + qx = _F_64(0.28125); + } else { + INSERT_WORDS(qx, ix - 0x00200000, 0); /* x/4 */ + } + hz = _F_64(0.5) * z - qx; + a = one - qx; + return a - (hz - (z * r - x * y)); + } +} + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/k_rem_pio2.c b/src/audio/ffmpeg_dec/libm/math/k_rem_pio2.c new file mode 100644 index 000000000000..05930d384b45 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/k_rem_pio2.c @@ -0,0 +1,344 @@ + +/* @(#)k_rem_pio2.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * __kernel_rem_pio2(x,y,e0,nx,prec,ipio2) + * double x[],y[]; int e0,nx,prec; int ipio2[]; + * + * __kernel_rem_pio2 return the last three digits of N with + * y = x - N*pi/2 + * so that |y| < pi/2. + * + * The method is to compute the integer (mod 8) and fraction parts of + * (2/pi)*x without doing the full multiplication. In general we + * skip the part of the product that are known to be a huge integer ( + * more accurately, = 0 mod 8 ). Thus the number of operations are + * independent of the exponent of the input. + * + * (2/pi) is represented by an array of 24-bit integers in ipio2[]. + * + * Input parameters: + * x[] The input value (must be positive) is broken into nx + * pieces of 24-bit integers in double precision format. + * x[i] will be the i-th 24 bit of x. The scaled exponent + * of x[0] is given in input parameter e0 (i.e., x[0]*2^e0 + * match x's up to 24 bits. + * + * Example of breaking a double positive z into x[0]+x[1]+x[2]: + * e0 = ilogb(z)-23 + * z = scalbn(z,-e0) + * for i = 0,1,2 + * x[i] = floor(z) + * z = (z-x[i])*2**24 + * + * + * y[] ouput result in an array of double precision numbers. + * The dimension of y[] is: + * 24-bit precision 1 + * 53-bit precision 2 + * 64-bit precision 2 + * 113-bit precision 3 + * The actual value is the sum of them. Thus for 113-bit + * precison, one may have to do something like: + * + * long double t,w,r_head, r_tail; + * t = (long double)y[2] + (long double)y[1]; + * w = (long double)y[0]; + * r_head = t+w; + * r_tail = w - (r_head - t); + * + * e0 The exponent of x[0] + * + * nx dimension of x[] + * + * prec an integer indicating the precision: + * 0 24 bits (single) + * 1 53 bits (double) + * 2 64 bits (extended) + * 3 113 bits (quad) + * + * ipio2[] + * integer array, contains the (24*i)-th to (24*i+23)-th + * bit of 2/pi after binary point. The corresponding + * floating value is + * + * ipio2[i] * 2^(-24(i+1)). + * + * External function: + * double scalbn(), floor(); + * + * + * Here is the description of some local variables: + * + * jk jk+1 is the initial number of terms of ipio2[] needed + * in the computation. The recommended value is 2,3,4, + * 6 for single, double, extended,and quad. + * + * jz local integer variable indicating the number of + * terms of ipio2[] used. + * + * jx nx - 1 + * + * jv index for pointing to the suitable ipio2[] for the + * computation. In general, we want + * ( 2^e0*x[0] * ipio2[jv-1]*2^(-24jv) )/8 + * is an integer. Thus + * e0-3-24*jv >= 0 or (e0-3)/24 >= jv + * Hence jv = max(0,(e0-3)/24). + * + * jp jp+1 is the number of terms in PIo2[] needed, jp = jk. + * + * q[] double array with integral value, representing the + * 24-bits chunk of the product of x and 2/pi. + * + * q0 the corresponding exponent of q[0]. Note that the + * exponent for q[i] would be q0-24*i. + * + * PIo2[] double precision array, obtained by cutting pi/2 + * into 24 bits chunks. + * + * f[] ipio2[] in floating point + * + * iq[] integer array by breaking up q[] in 24-bits chunk. + * + * fq[] final product of x*(2/pi) in fq[0],..,fq[jk] + * + * ih integer. If >0 it indicates q[] is >= 0.5, hence + * it also indicates the *sign* of the result. + * + */ + +/* + * Constants: + * The hexadecimal values are the intended ones for the following + * constants. The decimal values may be used, provided that the + * compiler will convert from decimal to binary accurately enough + * to produce the hexadecimal values shown. + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +static const int init_jk[] = { 2, 3, 4, 6 }; /* initial value for jk */ + +static const __float64 PIo2[] = { + _F_64(1.57079625129699707031e+00), /* 0x3FF921FB, 0x40000000 */ + _F_64(7.54978941586159635335e-08), /* 0x3E74442D, 0x00000000 */ + _F_64(5.39030252995776476554e-15), /* 0x3CF84698, 0x80000000 */ + _F_64(3.28200341580791294123e-22), /* 0x3B78CC51, 0x60000000 */ + _F_64(1.27065575308067607349e-29), /* 0x39F01B83, 0x80000000 */ + _F_64(1.22933308981111328932e-36), /* 0x387A2520, 0x40000000 */ + _F_64(2.73370053816464559624e-44), /* 0x36E38222, 0x80000000 */ + _F_64(2.16741683877804819444e-51), /* 0x3569F31D, 0x00000000 */ +}; + +static const __float64 + zero = _F_64(0.0), one = _F_64(1.0), + two24 = _F_64(1.67772160000000000000e+07), /* 0x41700000, 0x00000000 */ + twon24 = _F_64(5.96046447753906250000e-08); /* 0x3E700000, 0x00000000 */ + +#ifdef __GNUCLIKE_PRAGMA_DIAGNOSTIC +#pragma GCC diagnostic ignored "-Wpragmas" +#pragma GCC diagnostic ignored "-Wunknown-warning-option" +#pragma GCC diagnostic ignored "-Wmaybe-uninitialized" +/* GCC analyzer gets confused about the use of 'iq' here */ +#pragma GCC diagnostic ignored "-Wanalyzer-use-of-uninitialized-value" +#endif + +int +__kernel_rem_pio2(__float64 *x, __float64 *y, int e0, int nx, int prec, + const __int32_t *ipio2) +{ + __int32_t jz, jx, jv, jp, jk, carry, n, iq[20], i, j, k, m, q0, ih; + __float64 z, fw, f[20], fq[20], q[20]; + + /* initialize jk*/ + jk = init_jk[prec]; + jp = jk; + + /* determine jx,jv,q0, note that 3>q0 */ + jx = nx - 1; + jv = (e0 - 3) / 24; + if (jv < 0) + jv = 0; + q0 = e0 - 24 * (jv + 1); + + /* set up f[0] to f[jx+jk] where f[jx+jk] = ipio2[jv+jk] */ + j = jv - jx; + m = jx + jk; + for (i = 0; i <= m; i++, j++) + f[i] = (j < 0) ? zero : (__float64)ipio2[j]; + + /* compute q[0],q[1],...q[jk] */ + for (i = 0; i <= jk; i++) { + for (j = 0, fw = _F_64(0.0); j <= jx; j++) + fw += x[j] * f[jx + i - j]; + q[i] = fw; + } + + jz = jk; +recompute: + /* distill q[] into iq[] reversingly */ + for (i = 0, j = jz, z = q[jz]; j > 0; i++, j--) { + fw = (__float64)((__int32_t)(twon24 * z)); + iq[i] = (__int32_t)(z - two24 * fw); + z = q[j - 1] + fw; + } + + /* compute n */ + z = scalbn(z, (int)q0); /* actual value of z */ + z -= _F_64(8.0) * floor(z * _F_64(0.125)); /* trim off integer >= 8 */ + n = (__int32_t)z; + z -= (__float64)n; + ih = 0; + if (q0 > 0) { /* need iq[jz-1] to determine n */ + i = (iq[jz - 1] >> (24 - q0)); + n += i; + iq[jz - 1] -= i << (24 - q0); + ih = iq[jz - 1] >> (23 - q0); + } else if (q0 == 0) + ih = iq[jz - 1] >> 23; + else if (z >= _F_64(0.5)) + ih = 2; + + if (ih > 0) { /* q > 0.5 */ + n += 1; + carry = 0; + for (i = 0; i < jz; i++) { /* compute 1-q */ + j = iq[i]; + if (carry == 0) { + if (j != 0) { + carry = 1; + iq[i] = 0x1000000 - j; + } + } else + iq[i] = 0xffffff - j; + } + if (q0 > 0) { /* rare case: chance is 1 in 12 */ + switch (q0) { + case 1: + iq[jz - 1] &= 0x7fffff; + break; + case 2: + iq[jz - 1] &= 0x3fffff; + break; + } + } + if (ih == 2) { + z = one - z; + if (carry != 0) + z -= scalbn(one, (int)q0); + } + } + + /* check if recomputation is needed */ + if (z == zero) { + j = 0; + for (i = jz - 1; i >= jk; i--) + j |= iq[i]; + if (j == 0) { /* need recomputation */ + for (k = 1; iq[jk - k] == 0; k++) + ; /* k = no. of terms needed */ + + for (i = jz + 1; i <= jz + k; i++) { /* add q[jz+1] to q[jz+k] */ + f[jx + i] = (__float64)ipio2[jv + i]; + for (j = 0, fw = _F_64(0.0); j <= jx; j++) + fw += x[j] * f[jx + i - j]; + q[i] = fw; + } + jz += k; + goto recompute; + } + } + + /* chop off zero terms */ + if (z == _F_64(0.0)) { + jz -= 1; + q0 -= 24; + while (iq[jz] == 0) { + jz--; + q0 -= 24; + } + } else { /* break z into 24-bit if necessary */ + z = scalbn(z, -(int)q0); + if (z >= two24) { + fw = (__float64)((__int32_t)(twon24 * z)); + iq[jz] = (__int32_t)(z - two24 * fw); + jz += 1; + q0 += 24; + iq[jz] = (__int32_t)fw; + } else + iq[jz] = (__int32_t)z; + } + + /* convert integer "bit" chunk to floating-point value */ + fw = scalbn(one, (int)q0); + for (i = jz; i >= 0; i--) { + q[i] = fw * (__float64)iq[i]; + fw *= twon24; + } + + /* compute PIo2[0,...,jp]*q[jz,...,0] */ + for (i = jz; i >= 0; i--) { + for (fw = _F_64(0.0), k = 0; k <= jp && k <= jz - i; k++) + fw += PIo2[k] * q[i + k]; + fq[jz - i] = fw; + } + + /* compress fq[] into y[] */ + switch (prec) { + case 0: + fw = _F_64(0.0); + for (i = jz; i >= 0; i--) + fw += fq[i]; + y[0] = (ih == 0) ? fw : -fw; + break; + case 1: + case 2: + fw = _F_64(0.0); + for (i = jz; i >= 0; i--) + fw += fq[i]; + y[0] = (ih == 0) ? fw : -fw; + fw = fq[0] - fw; + for (i = 1; i <= jz; i++) + fw += fq[i]; + y[1] = (ih == 0) ? fw : -fw; + break; + case 3: /* painful */ + for (i = jz; i > 0; i--) { + fw = fq[i - 1] + fq[i]; + fq[i] += fq[i - 1] - fw; + fq[i - 1] = fw; + } + for (i = jz; i > 1; i--) { + fw = fq[i - 1] + fq[i]; + fq[i] += fq[i - 1] - fw; + fq[i - 1] = fw; + } + for (fw = _F_64(0.0), i = jz; i >= 2; i--) + fw += fq[i]; + if (ih == 0) { + y[0] = fq[0]; + y[1] = fq[1]; + y[2] = fw; + } else { + y[0] = -fq[0]; + y[1] = -fq[1]; + y[2] = -fw; + } + } + return n & 7; +} + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/k_sin.c b/src/audio/ffmpeg_dec/libm/math/k_sin.c new file mode 100644 index 000000000000..328b5610dfe6 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/k_sin.c @@ -0,0 +1,73 @@ + +/* @(#)k_sin.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* __kernel_sin( x, y, iy) + * kernel sin function on [-pi/4, pi/4], pi/4 ~ 0.7854 + * Input x is assumed to be bounded by ~pi/4 in magnitude. + * Input y is the tail of x. + * Input iy indicates whether y is 0. (if iy=0, y assume to be 0). + * + * Algorithm + * 1. Since sin(-x) = -sin(x), we need only to consider positive x. + * 2. if x < 2^-27 (hx<0x3e400000 0), return x with inexact if x!=0. + * 3. sin(x) is approximated by a polynomial of degree 13 on + * [0,pi/4] + * 3 13 + * sin(x) ~ x + S1*x + ... + S6*x + * where + * + * |sin(x) 2 4 6 8 10 12 | -58 + * |----- - (1+S1*x +S2*x +S3*x +S4*x +S5*x +S6*x )| <= 2 + * | x | + * + * 4. sin(x+y) = sin(x) + sin'(x')*y + * ~ sin(x) + (1-x*x/2)*y + * For better accuracy, let + * 3 2 2 2 2 + * r = x *(S2+x *(S3+x *(S4+x *(S5+x *S6)))) + * then 3 2 + * sin(x) = x + (S1*x + (x *(r-y/2)+y)) + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +static const __float64 + half = _F_64(5.00000000000000000000e-01), /* 0x3FE00000, 0x00000000 */ + S1 = _F_64(-1.66666666666666324348e-01), /* 0xBFC55555, 0x55555549 */ + S2 = _F_64(8.33333333332248946124e-03), /* 0x3F811111, 0x1110F8A6 */ + S3 = _F_64(-1.98412698298579493134e-04), /* 0xBF2A01A0, 0x19C161D5 */ + S4 = _F_64(2.75573137070700676789e-06), /* 0x3EC71DE3, 0x57B1FE7D */ + S5 = _F_64(-2.50507602534068634195e-08), /* 0xBE5AE5E6, 0x8A2B9CEB */ + S6 = _F_64(1.58969099521155010221e-10); /* 0x3DE5D93A, 0x5ACFD57C */ + +__float64 +__kernel_sin(__float64 x, __float64 y, int iy) +{ + __float64 z, r, v; + __int32_t ix; + GET_HIGH_WORD(ix, x); + ix &= 0x7fffffff; /* high word of x */ + if (ix < 0x3e400000) /* |x| < 2**-27 */ + return __math_inexact64(x); /* generate inexact */ + z = x * x; + v = z * x; + r = S2 + z * (S3 + z * (S4 + z * (S5 + z * S6))); + if (iy == 0) + return x + v * (S1 + z * r); + else + return x - ((z * (half * y - v * r) - y) - v * S1); +} + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/k_tan.c b/src/audio/ffmpeg_dec/libm/math/k_tan.c new file mode 100644 index 000000000000..00d14a6cc901 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/k_tan.c @@ -0,0 +1,145 @@ + +/* @(#)k_tan.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* __kernel_tan( x, y, k ) + * kernel tan function on [-pi/4, pi/4], pi/4 ~ 0.7854 + * Input x is assumed to be bounded by ~pi/4 in magnitude. + * Input y is the tail of x. + * Input k indicates whether tan (if k=1) or + * -1/tan (if k= -1) is returned. + * + * Algorithm + * 1. Since tan(-x) = -tan(x), we need only to consider positive x. + * 2. if x < 2^-28 (hx<0x3e300000 0), return x with inexact if x!=0. + * 3. tan(x) is approximated by a odd polynomial of degree 27 on + * [0,0.67434] + * 3 27 + * tan(x) ~ x + T1*x + ... + T13*x + * where + * + * |tan(x) 2 4 26 | -59.2 + * |----- - (1+T1*x +T2*x +.... +T13*x )| <= 2 + * | x | + * + * Note: tan(x+y) = tan(x) + tan'(x)*y + * ~ tan(x) + (1+x*x)*y + * Therefore, for better accuracy in computing tan(x+y), let + * 3 2 2 2 2 + * r = x *(T2+x *(T3+x *(...+x *(T12+x *T13)))) + * then + * 3 2 + * tan(x+y) = x + (T1*x + (x *(r+y)+y)) + * + * 4. For x in [0.67434,pi/4], let y = pi/4 - x, then + * tan(x) = tan(pi/4-y) = (1-tan(y))/(1+tan(y)) + * = 1 - 2*(tan(y) - (tan(y)^2)/(1+tan(y))) + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +static const __float64 + one = _F_64(1.00000000000000000000e+00), /* 0x3FF00000, 0x00000000 */ + pio4 = _F_64(7.85398163397448278999e-01), /* 0x3FE921FB, 0x54442D18 */ + pio4lo = _F_64(3.06161699786838301793e-17), /* 0x3C81A626, 0x33145C07 */ + T[] = { + _F_64(3.33333333333334091986e-01), /* 0x3FD55555, 0x55555563 */ + _F_64(1.33333333333201242699e-01), /* 0x3FC11111, 0x1110FE7A */ + _F_64(5.39682539762260521377e-02), /* 0x3FABA1BA, 0x1BB341FE */ + _F_64(2.18694882948595424599e-02), /* 0x3F9664F4, 0x8406D637 */ + _F_64(8.86323982359930005737e-03), /* 0x3F8226E3, 0xE96E8493 */ + _F_64(3.59207910759131235356e-03), /* 0x3F6D6D22, 0xC9560328 */ + _F_64(1.45620945432529025516e-03), /* 0x3F57DBC8, 0xFEE08315 */ + _F_64(5.88041240820264096874e-04), /* 0x3F4344D8, 0xF2F26501 */ + _F_64(2.46463134818469906812e-04), /* 0x3F3026F7, 0x1A8D1068 */ + _F_64(7.81794442939557092300e-05), /* 0x3F147E88, 0xA03792A6 */ + _F_64(7.14072491382608190305e-05), /* 0x3F12B80F, 0x32F0A7E9 */ + _F_64(-1.85586374855275456654e-05), /* 0xBEF375CB, 0xDB605373 */ + _F_64(2.59073051863633712884e-05), /* 0x3EFB2A70, 0x74BF7AD4 */ + }; + +__float64 +__kernel_tan(__float64 x, __float64 y, int iy) +{ + __float64 z, r, v, w, s; + __int32_t ix, hx; + GET_HIGH_WORD(hx, x); + ix = hx & 0x7fffffff; /* high word of |x| */ + if (ix < 0x3e300000) { /* x < 2**-28 */ + if ((int)x == 0) { /* generate inexact */ + __uint32_t low; + GET_LOW_WORD(low, x); + if (((ix | low) | (iy + 1)) == 0) + return one / fabs64(x); + else { + if (iy == 1) + return x; + else { + __float64 a, t; + z = w = x + y; + SET_LOW_WORD(z, 0); + v = y - (z - x); + t = a = -one / w; + SET_LOW_WORD(t, 0); + s = one + t * z; + return t + a * (s + t * v); + } + } + } + } + if (ix >= 0x3FE59428) { /* |x|>=0.6744 */ + if (hx < 0) { + x = -x; + y = -y; + } + z = pio4 - x; + w = pio4lo - y; + x = z + w; + y = _F_64(0.0); + } + z = x * x; + w = z * z; + /* Break x^5*(T[1]+x^2*T[2]+...) into + * x^5(T[1]+x^4*T[3]+...+x^20*T[11]) + + * x^5(x^2*(T[2]+x^4*T[4]+...+x^22*[T12])) + */ + r = T[1] + w * (T[3] + w * (T[5] + w * (T[7] + w * (T[9] + w * T[11])))); + v = z * + (T[2] + w * (T[4] + w * (T[6] + w * (T[8] + w * (T[10] + w * T[12]))))); + s = z * x; + r = y + z * (s * (r + v) + y); + r += T[0] * s; + w = x + r; + if (ix >= 0x3FE59428) { + v = (__float64)iy; + return (__float64)(1 - ((hx >> 30) & 2)) * + (v - _F_64(2.0) * (x - (w * w / (w + v) - r))); + } + if (iy == 1) + return w; + else { /* if allow error up to 2 ulp, + simply return -1.0/(x+r) here */ + /* compute -1.0/(x+r) accurately */ + __float64 a, t; + z = w; + SET_LOW_WORD(z, 0); + v = r - (z - x); /* z+v = r+x */ + t = a = _F_64(-1.0) / w; /* a = _F_64(-1.0)/w */ + SET_LOW_WORD(t, 0); + s = _F_64(1.0) + t * z; + return t + a * (s + t * v); + } +} + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/kf_cos.c b/src/audio/ffmpeg_dec/libm/math/kf_cos.c new file mode 100644 index 000000000000..a55e5d0010dc --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/kf_cos.c @@ -0,0 +1,49 @@ +/* kf_cos.c -- float version of k_cos.c + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" + +static const float one = 1.0000000000e+00, /* 0x3f800000 */ + C1 = 4.1666667908e-02, /* 0x3d2aaaab */ + C2 = -1.3888889225e-03, /* 0xbab60b61 */ + C3 = 2.4801587642e-05, /* 0x37d00d01 */ + C4 = -2.7557314297e-07, /* 0xb493f27c */ + C5 = 2.0875723372e-09, /* 0x310f74f6 */ + C6 = -1.1359647598e-11; /* 0xad47d74e */ + +float +__kernel_cosf(float x, float y) +{ + float a, hz, z, r, qx; + __int32_t ix; + GET_FLOAT_WORD(ix, x); + ix &= 0x7fffffff; /* ix = |x|'s high word*/ + if (ix < 0x32000000) /* if x < 2**27 */ + return one; + z = x * x; + r = z * (C1 + z * (C2 + z * (C3 + z * (C4 + z * (C5 + z * C6))))); + if (ix < 0x3e99999a) /* if |x| < 0.3 */ + return one - ((float)0.5 * z - (z * r - x * y)); + else { + if (ix > 0x3f480000) { /* x > 0.78125 */ + qx = (float)0.28125; + } else { + SET_FLOAT_WORD(qx, ix - 0x01000000); /* x/4 */ + } + hz = (float)0.5 * z - qx; + a = one - qx; + return a - (hz - (z * r - x * y)); + } +} diff --git a/src/audio/ffmpeg_dec/libm/math/kf_rem_pio2.c b/src/audio/ffmpeg_dec/libm/math/kf_rem_pio2.c new file mode 100644 index 000000000000..5b4c2ecfec55 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/kf_rem_pio2.c @@ -0,0 +1,232 @@ +/* kf_rem_pio2.c -- float version of k_rem_pio2.c + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" + +/* In the float version, the input parameter x contains 8 bit + integers, not 24 bit integers. 113 bit precision is not supported. */ + +static const int init_jk[] = { 4, 7, 9 }; /* initial value for jk */ + +static const float PIo2[] = { + 1.5703125000e+00, /* 0x3fc90000 */ + 4.5776367188e-04, /* 0x39f00000 */ + 2.5987625122e-05, /* 0x37da0000 */ + 7.5437128544e-08, /* 0x33a20000 */ + 6.0026650317e-11, /* 0x2e840000 */ + 7.3896444519e-13, /* 0x2b500000 */ + 5.3845816694e-15, /* 0x27c20000 */ + 5.6378512969e-18, /* 0x22d00000 */ + 8.3009228831e-20, /* 0x1fc40000 */ + 3.2756352257e-22, /* 0x1bc60000 */ + 6.3331015649e-25, /* 0x17440000 */ +}; + +static const float zero = 0.0, one = 1.0, + two8 = 2.5600000000e+02, /* 0x43800000 */ + twon8 = 3.9062500000e-03; /* 0x3b800000 */ + +#ifdef __GNUCLIKE_PRAGMA_DIAGNOSTIC +#pragma GCC diagnostic ignored "-Wpragmas" +#pragma GCC diagnostic ignored "-Wunknown-warning-option" +#pragma GCC diagnostic ignored "-Wmaybe-uninitialized" +/* GCC analyzer gets confused about the use of 'iq' here */ +#pragma GCC diagnostic ignored "-Wanalyzer-use-of-uninitialized-value" +#endif + +int +__kernel_rem_pio2f(float *x, float *y, int e0, int nx, int prec, + const __int32_t *ipio2) +{ + __int32_t jz, jx, jv, jp, jk, carry, n, iq[20], i, j, k, m, q0, ih; + float z, fw, f[20], fq[20], q[20]; + + /* initialize jk*/ + jk = init_jk[prec]; + jp = jk; + + /* determine jx,jv,q0, note that 3>q0 */ + jx = nx - 1; + jv = (e0 - 3) / 8; + if (jv < 0) + jv = 0; + q0 = e0 - 8 * (jv + 1); + + /* set up f[0] to f[jx+jk] where f[jx+jk] = ipio2[jv+jk] */ + j = jv - jx; + m = jx + jk; + for (i = 0; i <= m; i++, j++) + f[i] = (j < 0) ? zero : (float)ipio2[j]; + + /* compute q[0],q[1],...q[jk] */ + for (i = 0; i <= jk; i++) { + for (j = 0, fw = 0.0; j <= jx; j++) + fw += x[j] * f[jx + i - j]; + q[i] = fw; + } + + jz = jk; +recompute: + /* distill q[] into iq[] reversingly */ + for (i = 0, j = jz, z = q[jz]; j > 0; i++, j--) { + fw = (float)((__int32_t)(twon8 * z)); + iq[i] = (__int32_t)(z - two8 * fw); + z = q[j - 1] + fw; + } + + /* compute n */ + z = scalbnf(z, (int)q0); /* actual value of z */ + z -= (float)8.0 * floorf(z * (float)0.125); /* trim off integer >= 8 */ + n = (__int32_t)z; + z -= (float)n; + ih = 0; + if (q0 > 0) { /* need iq[jz-1] to determine n */ + i = (iq[jz - 1] >> (8 - q0)); + n += i; + iq[jz - 1] -= i << (8 - q0); + ih = iq[jz - 1] >> (7 - q0); + } else if (q0 == 0) + ih = iq[jz - 1] >> 7; + else if (z >= (float)0.5) + ih = 2; + + if (ih > 0) { /* q > 0.5 */ + n += 1; + carry = 0; + for (i = 0; i < jz; i++) { /* compute 1-q */ + j = iq[i]; + if (carry == 0) { + if (j != 0) { + carry = 1; + iq[i] = 0x100 - j; + } + } else + iq[i] = 0xff - j; + } + if (q0 > 0) { /* rare case: chance is 1 in 12 */ + switch (q0) { + case 1: + iq[jz - 1] &= 0x7f; + break; + case 2: + iq[jz - 1] &= 0x3f; + break; + } + } + if (ih == 2) { + z = one - z; + if (carry != 0) + z -= scalbnf(one, (int)q0); + } + } + + /* check if recomputation is needed */ + if (z == zero) { + j = 0; + for (i = jz - 1; i >= jk; i--) + j |= iq[i]; + if (j == 0) { /* need recomputation */ + for (k = 1; iq[jk - k] == 0; k++) + ; /* k = no. of terms needed */ + + for (i = jz + 1; i <= jz + k; i++) { /* add q[jz+1] to q[jz+k] */ + f[jx + i] = (float)ipio2[jv + i]; + for (j = 0, fw = 0.0; j <= jx; j++) + fw += x[j] * f[jx + i - j]; + q[i] = fw; + } + jz += k; + goto recompute; + } + } + + /* chop off zero terms */ + if (z == (float)0.0) { + jz -= 1; + q0 -= 8; + while (iq[jz] == 0) { + jz--; + q0 -= 8; + } + } else { /* break z into 8-bit if necessary */ + z = scalbnf(z, -(int)q0); + if (z >= two8) { + fw = (float)((__int32_t)(twon8 * z)); + iq[jz] = (__int32_t)(z - two8 * fw); + jz += 1; + q0 += 8; + iq[jz] = (__int32_t)fw; + } else + iq[jz] = (__int32_t)z; + } + + /* convert integer "bit" chunk to floating-point value */ + fw = scalbnf(one, (int)q0); + for (i = jz; i >= 0; i--) { + q[i] = fw * (float)iq[i]; + fw *= twon8; + } + + /* compute PIo2[0,...,jp]*q[jz,...,0] */ + for (i = jz; i >= 0; i--) { + for (fw = 0.0, k = 0; k <= jp && k <= jz - i; k++) + fw += PIo2[k] * q[i + k]; + fq[jz - i] = fw; + } + + /* compress fq[] into y[] */ + switch (prec) { + case 0: + fw = 0.0; + for (i = jz; i >= 0; i--) + fw += fq[i]; + y[0] = (ih == 0) ? fw : -fw; + break; + case 1: + case 2: + fw = 0.0; + for (i = jz; i >= 0; i--) + fw += fq[i]; + y[0] = (ih == 0) ? fw : -fw; + fw = fq[0] - fw; + for (i = 1; i <= jz; i++) + fw += fq[i]; + y[1] = (ih == 0) ? fw : -fw; + break; + case 3: /* painful */ + for (i = jz; i > 0; i--) { + fw = fq[i - 1] + fq[i]; + fq[i] += fq[i - 1] - fw; + fq[i - 1] = fw; + } + for (i = jz; i > 1; i--) { + fw = fq[i - 1] + fq[i]; + fq[i] += fq[i - 1] - fw; + fq[i - 1] = fw; + } + for (fw = 0.0, i = jz; i >= 2; i--) + fw += fq[i]; + if (ih == 0) { + y[0] = fq[0]; + y[1] = fq[1]; + y[2] = fw; + } else { + y[0] = -fq[0]; + y[1] = -fq[1]; + y[2] = -fw; + } + } + return n & 7; +} diff --git a/src/audio/ffmpeg_dec/libm/math/kf_sin.c b/src/audio/ffmpeg_dec/libm/math/kf_sin.c new file mode 100644 index 000000000000..caa9c0591c96 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/kf_sin.c @@ -0,0 +1,42 @@ +/* kf_sin.c -- float version of k_sin.c + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" + +static const float half = 5.0000000000e-01, /* 0x3f000000 */ + S1 = -1.6666667163e-01, /* 0xbe2aaaab */ + S2 = 8.3333337680e-03, /* 0x3c088889 */ + S3 = -1.9841270114e-04, /* 0xb9500d01 */ + S4 = 2.7557314297e-06, /* 0x3638ef1b */ + S5 = -2.5050759689e-08, /* 0xb2d72f34 */ + S6 = 1.5896910177e-10; /* 0x2f2ec9d3 */ + +float +__kernel_sinf(float x, float y, int iy) +{ + float z, r, v; + __int32_t ix; + GET_FLOAT_WORD(ix, x); + ix &= 0x7fffffff; /* high word of x */ + if (ix < 0x32000000) /* |x| < 2**-27 */ + return __math_inexactf(x); /* generate inexact */ + z = x * x; + v = z * x; + r = S2 + z * (S3 + z * (S4 + z * (S5 + z * S6))); + if (iy == 0) + return x + v * (S1 + z * r); + else + return x - ((z * (half * y - v * r) - y) - v * S1); +} diff --git a/src/audio/ffmpeg_dec/libm/math/kf_tan.c b/src/audio/ffmpeg_dec/libm/math/kf_tan.c new file mode 100644 index 000000000000..5707e2475efb --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/kf_tan.c @@ -0,0 +1,97 @@ +/* kf_tan.c -- float version of k_tan.c + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" +static const float one = 1.0000000000e+00, /* 0x3f800000 */ + pio4 = 7.8539812565e-01, /* 0x3f490fda */ + pio4lo = 3.7748947079e-08, /* 0x33222168 */ + T[] = { + 3.3333334327e-01, /* 0x3eaaaaab */ + 1.3333334029e-01, /* 0x3e088889 */ + 5.3968254477e-02, /* 0x3d5d0dd1 */ + 2.1869488060e-02, /* 0x3cb327a4 */ + 8.8632395491e-03, /* 0x3c11371f */ + 3.5920790397e-03, /* 0x3b6b6916 */ + 1.4562094584e-03, /* 0x3abede48 */ + 5.8804126456e-04, /* 0x3a1a26c8 */ + 2.4646313977e-04, /* 0x398137b9 */ + 7.8179444245e-05, /* 0x38a3f445 */ + 7.1407252108e-05, /* 0x3895c07a */ + -1.8558637748e-05, /* 0xb79bae5f */ + 2.5907305826e-05, /* 0x37d95384 */ + }; + +float +__kernel_tanf(float x, float y, int iy) +{ + float z, r, v, w, s; + __int32_t ix, hx; + GET_FLOAT_WORD(hx, x); + ix = hx & 0x7fffffff; /* high word of |x| */ + if (ix < 0x31800000) /* x < 2**-28 */ + { + if ((int)x == 0) { /* generate inexact */ + if ((ix | (iy + 1)) == 0) + return one / fabsf(x); + else + return (iy == 1) ? x : -one / x; + } + } + if (ix >= 0x3f2ca140) { /* |x|>=0.6744 */ + if (hx < 0) { + x = -x; + y = -y; + } + z = pio4 - x; + w = pio4lo - y; + x = z + w; + y = 0.0; + } + z = x * x; + w = z * z; + /* Break x^5*(T[1]+x^2*T[2]+...) into + * x^5(T[1]+x^4*T[3]+...+x^20*T[11]) + + * x^5(x^2*(T[2]+x^4*T[4]+...+x^22*[T12])) + */ + r = T[1] + w * (T[3] + w * (T[5] + w * (T[7] + w * (T[9] + w * T[11])))); + v = z * + (T[2] + w * (T[4] + w * (T[6] + w * (T[8] + w * (T[10] + w * T[12]))))); + s = z * x; + r = y + z * (s * (r + v) + y); + r += T[0] * s; + w = x + r; + if (ix >= 0x3f2ca140) { + v = (float)iy; + return (float)(1 - ((hx >> 30) & 2)) * + (v - (float)2.0 * (x - (w * w / (w + v) - r))); + } + if (iy == 1) + return w; + else { /* if allow error up to 2 ulp, + simply return -1.0/(x+r) here */ + /* compute -1.0/(x+r) accurately */ + float a, t; + __int32_t i; + z = w; + GET_FLOAT_WORD(i, z); + SET_FLOAT_WORD(z, i & 0xfffff000); + v = r - (z - x); /* z+v = r+x */ + t = a = -(float)1.0 / w; /* a = -1.0/w */ + GET_FLOAT_WORD(i, t); + SET_FLOAT_WORD(t, i & 0xfffff000); + s = (float)1.0 + t * z; + return t + a * (s + t * v); + } +} diff --git a/src/audio/ffmpeg_dec/libm/math/s_acos.c b/src/audio/ffmpeg_dec/libm/math/s_acos.c new file mode 100644 index 000000000000..fa1cda3aaa96 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_acos.c @@ -0,0 +1,108 @@ + +/* @(#)e_acos.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* acos(x) + * Method : + * acos(x) = pi/2 - asin(x) + * acos(-x) = pi/2 + asin(x) + * For |x|<=0.5 + * acos(x) = pi/2 - (x + x*x^2*R(x^2)) (see asin.c) + * For x>0.5 + * acos(x) = pi/2 - (pi/2 - 2asin(sqrt((1-x)/2))) + * = 2asin(sqrt((1-x)/2)) + * = 2s + 2s*z*R(z) ...z=(1-x)/2, s=sqrt(z) + * = 2f + (2c + 2s*z*R(z)) + * where f=hi part of s, and c = (z-f*f)/(s+f) is the correction term + * for f so that f+c ~ sqrt(z). + * For x<-0.5 + * acos(x) = pi - 2asin(sqrt((1-|x|)/2)) + * = pi - 0.5*(s+s*z*R(z)), where z=(1-|x|)/2,s=sqrt(z) + * + * Special cases: + * if x is NaN, return x itself; + * if |x|>1, return NaN with invalid signal. + * + * Function needed: sqrt + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +static const __float64 + one = _F_64(1.00000000000000000000e+00), /* 0x3FF00000, 0x00000000 */ + pi = _F_64(3.14159265358979311600e+00), /* 0x400921FB, 0x54442D18 */ + pio2_hi = _F_64(1.57079632679489655800e+00), /* 0x3FF921FB, 0x54442D18 */ + pio2_lo = _F_64(6.12323399573676603587e-17), /* 0x3C91A626, 0x33145C07 */ + pS0 = _F_64(1.66666666666666657415e-01), /* 0x3FC55555, 0x55555555 */ + pS1 = _F_64(-3.25565818622400915405e-01), /* 0xBFD4D612, 0x03EB6F7D */ + pS2 = _F_64(2.01212532134862925881e-01), /* 0x3FC9C155, 0x0E884455 */ + pS3 = _F_64(-4.00555345006794114027e-02), /* 0xBFA48228, 0xB5688F3B */ + pS4 = _F_64(7.91534994289814532176e-04), /* 0x3F49EFE0, 0x7501B288 */ + pS5 = _F_64(3.47933107596021167570e-05), /* 0x3F023DE1, 0x0DFDF709 */ + qS1 = _F_64(-2.40339491173441421878e+00), /* 0xC0033A27, 0x1C8A2D4B */ + qS2 = _F_64(2.02094576023350569471e+00), /* 0x40002AE5, 0x9C598AC8 */ + qS3 = _F_64(-6.88283971605453293030e-01), /* 0xBFE6066C, 0x1B8D0159 */ + qS4 = _F_64(7.70381505559019352791e-02); /* 0x3FB3B8C5, 0xB12E9282 */ + +__float64 +acos64(__float64 x) +{ + __float64 z, p, q, r, w, s, c, df; + __int32_t hx, ix; + GET_HIGH_WORD(hx, x); + ix = hx & 0x7fffffff; + if (ix >= 0x3ff00000) { /* |x| >= 1 */ + __uint32_t lx; + GET_LOW_WORD(lx, x); + if ((((__uint32_t) ix - 0x3ff00000) | lx) == 0) { /* |x|==1 */ + if (hx > 0) + return _F_64(0.0); /* acos(1) = 0 */ + else + return pi + _F_64(2.0) * pio2_lo; /* acos(-1)= pi */ + } + return __math_invalid(x); /* acos(|x|>1) is NaN */ + } + if (ix < 0x3fe00000) { /* |x| < 0.5 */ + if (ix <= 0x3c600000) + return pio2_hi + pio2_lo; /*if|x|<2**-57*/ + z = x * x; + p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5))))); + q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4))); + r = p / q; + return pio2_hi - (x - (pio2_lo - x * r)); + } else if (hx < 0) { /* x < -0.5 */ + z = (one + x) * _F_64(0.5); + p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5))))); + q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4))); + s = sqrt64(z); + r = p / q; + w = r * s - pio2_lo; + return pi - _F_64(2.0) * (s + w); + } else { /* x > 0.5 */ + z = (one - x) * _F_64(0.5); + s = sqrt64(z); + df = s; + SET_LOW_WORD(df, 0); + c = (z - df * df) / (s + df); + p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5))))); + q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4))); + r = p / q; + w = r * s + c; + return _F_64(2.0) * (df + w); + } +} + +_MATH_ALIAS_d_d(acos) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_acosh.c b/src/audio/ffmpeg_dec/libm/math/s_acosh.c new file mode 100644 index 000000000000..9c3188961b99 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_acosh.c @@ -0,0 +1,64 @@ + +/* @(#)e_acosh.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + * + */ + +/* acosh(x) + * Method : + * Based on + * acosh(x) = log [ x + sqrt(x*x-1) ] + * we have + * acosh(x) := log(x)+ln2, if x is large; else + * acosh(x) := log(2x-1/(sqrt(x*x-1)+x)) if x>2; else + * acosh(x) := log1p(t+sqrt(2.0*t+t*t)); where t=x-1. + * + * Special cases: + * acosh(x) is NaN with signal if x<1. + * acosh(NaN) is NaN without signal. + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +static const __float64 + one = _F_64(1.0), + ln2 = _F_64(6.93147180559945286227e-01); /* 0x3FE62E42, 0xFEFA39EF */ + +__float64 +acosh64(__float64 x) +{ + __float64 t; + __int32_t hx; + __uint32_t lx; + EXTRACT_WORDS(hx, lx, x); + if (hx < 0x3ff00000) { /* x < 1 */ + return __math_invalid(x); + } else if (hx >= 0x41b00000) { /* x > 2**28 */ + if (hx >= 0x7ff00000) { /* x is inf of NaN */ + return x + x; + } else + return log64(x) + ln2; /* acosh(huge)=log(2x) */ + } else if ((((__uint32_t) hx - 0x3ff00000) | lx) == 0) { + return _F_64(0.0); /* acosh(1) = 0 */ + } else if (hx > 0x40000000) { /* 2**28 > x > 2 */ + t = x * x; + return log64(_F_64(2.0) * x - one / (x + sqrt64(t - one))); + } else { /* 10.98 + * asin(x) = pi/2 - 2*(s+s*z*R(z)) + * = pio2_hi - (2*(s+s*z*R(z)) - pio2_lo) + * For x<=0.98, let pio4_hi = pio2_hi/2, then + * f = hi part of s; + * c = sqrt(z) - f = (z-f*f)/(s+f) ...f+c=sqrt(z) + * and + * asin(x) = pi/2 - 2*(s+s*z*R(z)) + * = pio4_hi+(pio4-2s)-(2s*z*R(z)-pio2_lo) + * = pio4_hi+(pio4-2f)-(2s*z*R(z)-(pio2_lo+2c)) + * + * Special cases: + * if x is NaN, return x itself; + * if |x|>1, return NaN with invalid signal. + * + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +static const __float64 + one = _F_64(1.00000000000000000000e+00), /* 0x3FF00000, 0x00000000 */ + huge = _F_64(1.000e+300), + pio2_hi = _F_64(1.57079632679489655800e+00), /* 0x3FF921FB, 0x54442D18 */ + pio2_lo = _F_64(6.12323399573676603587e-17), /* 0x3C91A626, 0x33145C07 */ + pio4_hi = _F_64(7.85398163397448278999e-01), /* 0x3FE921FB, 0x54442D18 */ + /* coefficient for R(x^2) */ + pS0 = _F_64(1.66666666666666657415e-01), /* 0x3FC55555, 0x55555555 */ + pS1 = _F_64(-3.25565818622400915405e-01), /* 0xBFD4D612, 0x03EB6F7D */ + pS2 = _F_64(2.01212532134862925881e-01), /* 0x3FC9C155, 0x0E884455 */ + pS3 = _F_64(-4.00555345006794114027e-02), /* 0xBFA48228, 0xB5688F3B */ + pS4 = _F_64(7.91534994289814532176e-04), /* 0x3F49EFE0, 0x7501B288 */ + pS5 = _F_64(3.47933107596021167570e-05), /* 0x3F023DE1, 0x0DFDF709 */ + qS1 = _F_64(-2.40339491173441421878e+00), /* 0xC0033A27, 0x1C8A2D4B */ + qS2 = _F_64(2.02094576023350569471e+00), /* 0x40002AE5, 0x9C598AC8 */ + qS3 = _F_64(-6.88283971605453293030e-01), /* 0xBFE6066C, 0x1B8D0159 */ + qS4 = _F_64(7.70381505559019352791e-02); /* 0x3FB3B8C5, 0xB12E9282 */ + +__float64 +asin64(__float64 x) +{ + __float64 t, w, p, q, c, r, s; + __int32_t hx, ix; + GET_HIGH_WORD(hx, x); + ix = hx & 0x7fffffff; + if (ix >= 0x3ff00000) { /* |x|>= 1 */ + __uint32_t lx; + GET_LOW_WORD(lx, x); + if ((((__uint32_t) ix - 0x3ff00000) | lx) == 0) + /* asin(1)=+-pi/2 with inexact */ + return x * pio2_hi + x * pio2_lo; + return __math_invalid(x); /* asin(|x|>1) is NaN */ + } else if (ix < 0x3fe00000) { /* |x|<0.5 */ + if (ix < 0x3e400000) { /* if |x| < 2**-27 */ + if (huge + x > one) + return x; /* return x with inexact if x!=0*/ + } else { + t = x * x; + p = t * + (pS0 + t * (pS1 + t * (pS2 + t * (pS3 + t * (pS4 + t * pS5))))); + q = one + t * (qS1 + t * (qS2 + t * (qS3 + t * qS4))); + w = p / q; + return x + x * w; + } + } + /* 1> |x|>= 0.5 */ + w = one - fabs64(x); + t = w * _F_64(0.5); + p = t * (pS0 + t * (pS1 + t * (pS2 + t * (pS3 + t * (pS4 + t * pS5))))); + q = one + t * (qS1 + t * (qS2 + t * (qS3 + t * qS4))); + s = sqrt(t); + if (ix >= 0x3FEF3333) { /* if |x| > 0.975 */ + w = p / q; + t = pio2_hi - (_F_64(2.0) * (s + s * w) - pio2_lo); + } else { + w = s; + SET_LOW_WORD(w, 0); + c = (t - w * w) / (s + w); + r = p / q; + p = _F_64(2.0) * s * r - (pio2_lo - _F_64(2.0) * c); + q = pio4_hi - _F_64(2.0) * w; + t = pio4_hi - (p - q); + } + if (hx > 0) + return t; + else + return -t; +} + +_MATH_ALIAS_d_d(asin) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_asinh.c b/src/audio/ffmpeg_dec/libm/math/s_asinh.c new file mode 100644 index 000000000000..4896351c3260 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_asinh.c @@ -0,0 +1,98 @@ + +/* @(#)s_asinh.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* +FUNCTION + <>, <>---inverse hyperbolic sine + +INDEX + asinh +INDEX + asinhf + +SYNOPSIS + #include + double asinh(double <[x]>); + float asinhf(float <[x]>); + +DESCRIPTION +<> calculates the inverse hyperbolic sine of <[x]>. +<> is defined as +@ifnottex +. sgn(<[x]>) * log(abs(<[x]>) + sqrt(1+<[x]>*<[x]>)) +@end ifnottex +@tex +$$sign(x) \times ln\Bigl(|x| + \sqrt{1+x^2}\Bigr)$$ +@end tex + +<> is identical, other than taking and returning floats. + +RETURNS +<> and <> return the calculated value. + +PORTABILITY +Neither <> nor <> are ANSI C. + +*/ + +/* asinh(x) + * Method : + * Based on + * asinh(x) = sign(x) * log [ |x| + sqrt(x*x+1) ] + * we have + * asinh(x) := x if 1+x*x=1, + * := sign(x)*(log(x)+ln2)) for large |x|, else + * := sign(x)*log(2|x|+1/(|x|+sqrt(x*x+1))) if|x|>2, else + * := sign(x)*log1p(|x| + x^2/(1 + sqrt(1+x^2))) + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +static const __float64 + one = _F_64(1.00000000000000000000e+00), /* 0x3FF00000, 0x00000000 */ + ln2 = _F_64(6.93147180559945286227e-01), /* 0x3FE62E42, 0xFEFA39EF */ + huge = _F_64(1.00000000000000000000e+300); + +__float64 +asinh64(__float64 x) +{ + __float64 t, w; + __int32_t hx, ix; + GET_HIGH_WORD(hx, x); + ix = hx & 0x7fffffff; + if (ix >= 0x7ff00000) + return x + x; /* x is inf or NaN */ + if (ix < 0x3e300000) { /* |x|<2**-28 */ + if (huge + x > one) + return x; /* return x inexact except 0 */ + } + if (ix > 0x41b00000) { /* |x| > 2**28 */ + w = log64(fabs64(x)) + ln2; + } else if (ix > 0x40000000) { /* 2**28 > |x| > 2.0 */ + t = fabs64(x); + w = log64(_F_64(2.0) * t + one / (sqrt64(x * x + one) + t)); + } else { /* 2.0 > |x| > 2**-28 */ + t = x * x; + w = log1p64(fabs64(x) + t / (one + sqrt64(one + t))); + } + if (hx > 0) + return w; + else + return -w; +} + +_MATH_ALIAS_d_d(asinh) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_atan.c b/src/audio/ffmpeg_dec/libm/math/s_atan.c new file mode 100644 index 000000000000..360322cb25c1 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_atan.c @@ -0,0 +1,162 @@ + +/* @(#)s_atan.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + * + */ + +/* +FUNCTION + <>, <>---arc tangent + +INDEX + atan +INDEX + atanf + +SYNOPSIS + #include + double atan(double <[x]>); + float atanf(float <[x]>); + +DESCRIPTION + +<> computes the inverse tangent (arc tangent) of the input value. + +<> is identical to <>, save that it operates on <>. + +RETURNS +@ifnottex +<> returns a value in radians, in the range of -pi/2 to pi/2. +@end ifnottex +@tex +<> returns a value in radians, in the range of $-\pi/2$ to $\pi/2$. +@end tex + +PORTABILITY +<> is ANSI C. <> is an extension. + +*/ + +/* atan(x) + * Method + * 1. Reduce x to positive by atan(x) = -atan(-x). + * 2. According to the integer k=4t+0.25 chopped, t=x, the argument + * is further reduced to one of the following intervals and the + * arctangent of t is evaluated by the corresponding formula: + * + * [0,7/16] atan(x) = t-t^3*(a1+t^2*(a2+...(a10+t^2*a11)...) + * [7/16,11/16] atan(x) = atan(1/2) + atan( (t-0.5)/(1+t/2) ) + * [11/16.19/16] atan(x) = atan( 1 ) + atan( (t-1)/(1+t) ) + * [19/16,39/16] atan(x) = atan(3/2) + atan( (t-1.5)/(1+1.5t) ) + * [39/16,INF] atan(x) = atan(INF) + atan( -1/t ) + * + * Constants: + * The hexadecimal values are the intended ones for the following + * constants. The decimal values may be used, provided that the + * compiler will convert from decimal to binary accurately enough + * to produce the hexadecimal values shown. + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +static const __float64 atanhi[] = { + _F_64(4.63647609000806093515e-01), /* atan(0.5)hi 0x3FDDAC67, 0x0561BB4F */ + _F_64(7.85398163397448278999e-01), /* atan(1.0)hi 0x3FE921FB, 0x54442D18 */ + _F_64(9.82793723247329054082e-01), /* atan(1.5)hi 0x3FEF730B, 0xD281F69B */ + _F_64(1.57079632679489655800e+00), /* atan(inf)hi 0x3FF921FB, 0x54442D18 */ +}; + +static const __float64 atanlo[] = { + _F_64(2.26987774529616870924e-17), /* atan(0.5)lo 0x3C7A2B7F, 0x222F65E2 */ + _F_64(3.06161699786838301793e-17), /* atan(1.0)lo 0x3C81A626, 0x33145C07 */ + _F_64(1.39033110312309984516e-17), /* atan(1.5)lo 0x3C700788, 0x7AF0CBBD */ + _F_64(6.12323399573676603587e-17), /* atan(inf)lo 0x3C91A626, 0x33145C07 */ +}; + +static const __float64 aT[] = { + _F_64(3.33333333333329318027e-01), /* 0x3FD55555, 0x5555550D */ + _F_64(-1.99999999998764832476e-01), /* 0xBFC99999, 0x9998EBC4 */ + _F_64(1.42857142725034663711e-01), /* 0x3FC24924, 0x920083FF */ + _F_64(-1.11111104054623557880e-01), /* 0xBFBC71C6, 0xFE231671 */ + _F_64(9.09088713343650656196e-02), /* 0x3FB745CD, 0xC54C206E */ + _F_64(-7.69187620504482999495e-02), /* 0xBFB3B0F2, 0xAF749A6D */ + _F_64(6.66107313738753120669e-02), /* 0x3FB10D66, 0xA0D03D51 */ + _F_64(-5.83357013379057348645e-02), /* 0xBFADDE2D, 0x52DEFD9A */ + _F_64(4.97687799461593236017e-02), /* 0x3FA97B4B, 0x24760DEB */ + _F_64(-3.65315727442169155270e-02), /* 0xBFA2B444, 0x2C6A6C2F */ + _F_64(1.62858201153657823623e-02), /* 0x3F90AD3A, 0xE322DA11 */ +}; + +static const __float64 one = _F_64(1.0); + +__float64 +atan64(__float64 x) +{ + __float64 w, s1, s2, z; + __int32_t ix, hx, id; + + GET_HIGH_WORD(hx, x); + ix = hx & 0x7fffffff; + if (ix >= 0x44100000) { /* if |x| >= 2^66 */ + __uint32_t low; + GET_LOW_WORD(low, x); + if (ix > 0x7ff00000 || (ix == 0x7ff00000 && (low != 0))) + return x + x; /* NaN */ + if (hx > 0) + return atanhi[3] + atanlo[3]; + else + return -atanhi[3] - atanlo[3]; + } + if (ix < 0x3fdc0000) { /* |x| < 0.4375 */ + if (ix < 0x3e200000) /* |x| < 2^-29 */ + return __math_inexact64(x); + id = -1; + } else { + x = fabs64(x); + if (ix < 0x3ff30000) { /* |x| < 1.1875 */ + if (ix < 0x3fe60000) { /* 7/16 <=|x|<11/16 */ + id = 0; + x = (_F_64(2.0) * x - one) / (_F_64(2.0) + x); + } else { /* 11/16<=|x|< 19/16 */ + id = 1; + x = (x - one) / (x + one); + } + } else { + if (ix < 0x40038000) { /* |x| < 2.4375 */ + id = 2; + x = (x - _F_64(1.5)) / (one + 1.5 * x); + } else { /* 2.4375 <= |x| < 2^66 */ + id = 3; + x = _F_64(-1.0) / x; + } + } + } + /* end of argument reduction */ + z = x * x; + w = z * z; + /* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */ + s1 = z * + (aT[0] + + w * (aT[2] + w * (aT[4] + w * (aT[6] + w * (aT[8] + w * aT[10]))))); + s2 = w * (aT[1] + w * (aT[3] + w * (aT[5] + w * (aT[7] + w * aT[9])))); + if (id < 0) + return x - x * (s1 + s2); + else { + z = atanhi[id] - ((x * (s1 + s2) - atanlo[id]) - x); + return (hx < 0) ? -z : z; + } +} + +_MATH_ALIAS_d_d(atan) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_atan2.c b/src/audio/ffmpeg_dec/libm/math/s_atan2.c new file mode 100644 index 000000000000..bde5ae8a3558 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_atan2.c @@ -0,0 +1,143 @@ + +/* @(#)e_atan2.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + * + */ + +/* atan2(y,x) + * Method : + * 1. Reduce y to positive by atan2(y,x)=-atan2(-y,x). + * 2. Reduce x to positive by (if x and y are unexceptional): + * ARG (x+iy) = arctan(y/x) ... if x > 0, + * ARG (x+iy) = pi - arctan[y/(-x)] ... if x < 0, + * + * Special cases: + * + * ATAN2((anything), NaN ) is NaN; + * ATAN2(NAN , (anything) ) is NaN; + * ATAN2(+-0, +(anything but NaN)) is +-0 ; + * ATAN2(+-0, -(anything but NaN)) is +-pi ; + * ATAN2(+-(anything but 0 and NaN), 0) is +-pi/2; + * ATAN2(+-(anything but INF and NaN), +INF) is +-0 ; + * ATAN2(+-(anything but INF and NaN), -INF) is +-pi; + * ATAN2(+-INF,+INF ) is +-pi/4 ; + * ATAN2(+-INF,-INF ) is +-3pi/4; + * ATAN2(+-INF, (anything but,0,NaN, and INF)) is +-pi/2; + * + * Constants: + * The hexadecimal values are the intended ones for the following + * constants. The decimal values may be used, provided that the + * compiler will convert from decimal to binary accurately enough + * to produce the hexadecimal values shown. + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +static const __float64 + tiny = _F_64(1.0e-300), zero = _F_64(0.0), + pi_o_4 = _F_64(7.8539816339744827900E-01), /* 0x3FE921FB, 0x54442D18 */ + pi_o_2 = _F_64(1.5707963267948965580E+00), /* 0x3FF921FB, 0x54442D18 */ + pi = _F_64(3.1415926535897931160E+00), /* 0x400921FB, 0x54442D18 */ + pi_lo = _F_64(1.2246467991473531772E-16); /* 0x3CA1A626, 0x33145C07 */ + +__float64 +atan264(__float64 y, __float64 x) +{ + __float64 z; + __int32_t k, m, hx, hy, ix, iy; + __uint32_t lx, ly; + + EXTRACT_WORDS(hx, lx, x); + ix = hx & 0x7fffffff; + EXTRACT_WORDS(hy, ly, y); + iy = hy & 0x7fffffff; + if (((ix | ((lx | -lx) >> 31)) > 0x7ff00000) || + ((iy | ((ly | -ly) >> 31)) > 0x7ff00000)) /* x or y is NaN */ + return x + y; + if ((((__uint32_t) hx - 0x3ff00000) | lx) == 0) + return atan64(y); /* x=1.0 */ + m = ((hy >> 31) & 1) | ((hx >> 30) & 2); /* 2*sign(x)+sign(y) */ + + /* when y = 0 */ + if ((iy | ly) == 0) { + switch (m) { + case 0: + case 1: + return y; /* atan(+-0,+anything)=+-0 */ + case 2: + return pi + tiny; /* atan(+0,-anything) = pi */ + case 3: + return -pi - tiny; /* atan(-0,-anything) =-pi */ + } + } + /* when x = 0 */ + if ((ix | lx) == 0) + return (hy < 0) ? -pi_o_2 - tiny : pi_o_2 + tiny; + + /* when x is INF */ + if (ix == 0x7ff00000) { + if (iy == 0x7ff00000) { + switch (m) { + case 0: + return pi_o_4 + tiny; /* atan(+INF,+INF) */ + case 1: + return -pi_o_4 - tiny; /* atan(-INF,+INF) */ + case 2: + return _F_64(3.0) * pi_o_4 + tiny; /*atan(+INF,-INF)*/ + case 3: + return _F_64(-3.0) * pi_o_4 - tiny; /*atan(-INF,-INF)*/ + } + } else { + switch (m) { + case 0: + return zero; /* atan(+...,+INF) */ + case 1: + return -zero; /* atan(-...,+INF) */ + case 2: + return pi + tiny; /* atan(+...,-INF) */ + case 3: + return -pi - tiny; /* atan(-...,-INF) */ + } + } + } + /* when y is INF */ + if (iy == 0x7ff00000) + return (hy < 0) ? -pi_o_2 - tiny : pi_o_2 + tiny; + + /* compute y/x */ + k = (iy - ix) >> 20; + if (k > 60) + z = pi_o_2 + _F_64(0.5) * pi_lo; /* |y/x| > 2**60 */ + else if (hx < 0 && k < -60) + z = _F_64(0.0); /* |y|/x < -2**60 */ + else + z = atan64(check_uflow(fabs64(y / x))); /* safe to do y/x */ + switch (m) { + case 0: + return z; /* atan(+,+) */ + case 1: { + __uint32_t zh; + GET_HIGH_WORD(zh, z); + SET_HIGH_WORD(z, zh ^ 0x80000000); + } + return z; /* atan(-,+) */ + case 2: + return pi - (z - pi_lo); /* atan(+,-) */ + default: /* case 3 */ + return (z - pi_lo) - pi; /* atan(-,-) */ + } +} + +_MATH_ALIAS_d_dd(atan2) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_atanh.c b/src/audio/ffmpeg_dec/libm/math/s_atanh.c new file mode 100644 index 000000000000..2467dbd68f19 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_atanh.c @@ -0,0 +1,67 @@ + +/* @(#)e_atanh.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + * + */ + +/* atanh(x) + * Method : + * 1.Reduced x to positive by atanh(-x) = -atanh(x) + * 2.For x>=0.5 + * 1 2x x + * atanh(x) = --- * log(1 + -------) = 0.5 * log1p(2 * --------) + * 2 1 - x 1 - x + * + * For x<0.5 + * atanh(x) = 0.5*log1p(2x+2x*x/(1-x)) + * + * Special cases: + * atanh(x) is NaN if |x| > 1 with signal; + * atanh(NaN) is that NaN with no signal; + * atanh(+-1) is +-INF with signal. + * + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +static const __float64 one = _F_64(1.0); + +__float64 +atanh64(__float64 x) +{ + __float64 t; + __int32_t hx, ix; + __uint32_t lx; + EXTRACT_WORDS(hx, lx, x); + ix = hx & 0x7fffffff; + if ((ix | ((lx | (-lx)) >> 31)) > 0x3ff00000) /* |x|>1 */ + return __math_invalid(x); + if (ix == 0x3ff00000) + return __math_divzero(x < 0); + if (ix < 0x3e300000) /* x<2**-28 */ + return __math_inexact64(x); + SET_HIGH_WORD(x, ix); + if (ix < 0x3fe00000) { /* x < 0.5 */ + t = x + x; + t = _F_64(0.5) * log1p64(t + t * x / (one - x)); + } else + t = _F_64(0.5) * log1p64((x + x) / (one - x)); + if (hx >= 0) + return t; + else + return -t; +} + +_MATH_ALIAS_d_d(atanh) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_ceil.c b/src/audio/ffmpeg_dec/libm/math/s_ceil.c new file mode 100644 index 000000000000..ff88dbc62fc6 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_ceil.c @@ -0,0 +1,79 @@ + +/* @(#)s_ceil.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * ceil(x) + * Return x rounded toward -inf to integral value + * Method: + * Bit twiddling. + * Exception: + * Inexact flag raised if x not equal to ceil(x). + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +__float64 +ceil64(__float64 x) +{ + __int32_t i0, i1, j0, j; + __uint32_t i; + EXTRACT_WORDS(i0, i1, x); + j0 = ((i0 >> 20) & 0x7ff) - 0x3ff; + if (j0 < 20) { + if (j0 < 0) { + if (i0 < 0) { + i0 = 0x80000000; + i1 = 0; + } else if ((i0 | i1) != 0) { + i0 = 0x3ff00000; + i1 = 0; + } + } else { + i = (0x000fffff) >> j0; + if (((i0 & i) | i1) == 0) + return x; /* x is integral */ + if (i0 > 0) + i0 += (0x00100000) >> j0; + i0 &= (~i); + i1 = 0; + } + } else if (j0 > 51) { + if (j0 == 0x400) + return x + x; /* inf or NaN */ + else + return x; /* x is integral */ + } else { + i = ((__uint32_t)(0xffffffffUL)) >> (j0 - 20); + if ((i1 & i) == 0) + return x; /* x is integral */ + if (i0 > 0) { + if (j0 == 20) + i0 += 1; + else { + j = i1 + ((__uint32_t) 1 << (52 - j0)); + if ((__uint32_t) j < (__uint32_t) i1) + i0 += 1; /* got a carry */ + i1 = j; + } + } + i1 &= (~i); + } + INSERT_WORDS(x, i0, i1); + return x; +} + +_MATH_ALIAS_d_d(ceil) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_cos.c b/src/audio/ffmpeg_dec/libm/math/s_cos.c new file mode 100644 index 000000000000..fe3cbc99e792 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_cos.c @@ -0,0 +1,92 @@ + +/* @(#)s_cos.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* cos(x) + * Return cosine function of x. + * + * kernel function: + * __kernel_sin ... sine function on [-pi/4,pi/4] + * __kernel_cos ... cosine function on [-pi/4,pi/4] + * __rem_pio2 ... argument reduction routine + * + * Method. + * Let S,C and T denote the sin, cos and tan respectively on + * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2 + * in [-pi/4 , +pi/4], and let n = k mod 4. + * We have + * + * n sin(x) cos(x) tan(x) + * ---------------------------------------------------------- + * 0 S C T + * 1 C -S -1/T + * 2 -S -C T + * 3 -C S -1/T + * ---------------------------------------------------------- + * + * Special cases: + * Let trig be any of sin, cos, or tan. + * trig(+-INF) is NaN, with signals; + * trig(NaN) is that NaN; + * + * Accuracy: + * TRIG(x) returns trig(x) nearly rounded + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +__float64 +cos64(__float64 x) +{ + __float64 y[2], z = 0.0; + __int32_t n, ix; + + /* High word of x. */ + GET_HIGH_WORD(ix, x); + + /* |x| ~< pi/4 */ + ix &= 0x7fffffff; + if (ix <= 0x3fe921fb) + return __kernel_cos(x, z); + + /* cos(Inf or NaN) is NaN */ + else if (ix >= 0x7ff00000) + return __math_invalid(x); + + /* argument reduction needed */ + else { + n = __rem_pio2(x, y); + switch (n & 3) { + case 0: + return __kernel_cos(y[0], y[1]); + case 1: + return -__kernel_sin(y[0], y[1], 1); + case 2: + return -__kernel_cos(y[0], y[1]); + default: + return __kernel_sin(y[0], y[1], 1); + } + } +} + +#ifdef __strong_reference +#if defined(__GNUCLIKE_PRAGMA_DIAGNOSTIC) && !defined(__clang__) +#pragma GCC diagnostic ignored "-Wmissing-attributes" +#endif +__strong_reference(_NAME_64(cos), _NAME_64(_cos)); +#endif + +_MATH_ALIAS_d_d(cos) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_cosh.c b/src/audio/ffmpeg_dec/libm/math/s_cosh.c new file mode 100644 index 000000000000..ca209898c719 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_cosh.c @@ -0,0 +1,91 @@ + +/* @(#)e_cosh.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* cosh(x) + * Method : + * mathematically cosh(x) if defined to be (exp(x)+exp(-x))/2 + * 1. Replace x by |x| (cosh(x) = cosh(-x)). + * 2. + * [ exp(x) - 1 ]^2 + * 0 <= x <= ln2/2 : cosh(x) := 1 + ------------------- + * 2*exp(x) + * + * exp(x) + 1/exp(x) + * ln2/2 <= x <= 22 : cosh(x) := ------------------- + * 2 + * 22 <= x <= lnovft : cosh(x) := exp(x)/2 + * lnovft <= x <= ln2ovft: cosh(x) := exp(x/2)/2 * exp(x/2) + * ln2ovft < x : cosh(x) := overflow + * + * Special cases: + * cosh(x) is |x| if x is +INF, -INF, or NaN. + * only cosh(0)=1 is exact for finite x. + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +static const __float64 one = _F_64(1.0), half = _F_64(0.5); + +__float64 +cosh64(__float64 x) +{ + __float64 t, w; + __int32_t ix; + __uint32_t lx; + + x = fabs64(x); + + /* High word of |x|. */ + GET_HIGH_WORD(ix, x); + ix &= 0x7fffffff; + + /* x is INF or NaN */ + if (ix >= 0x7ff00000) + return x + x; + + /* |x| in [0,0.5*ln2], return 1+expm1(|x|)^2/(2*exp(|x|)) */ + if (ix < 0x3fd62e43) { + t = expm1(x); + w = one + t; + if (ix < 0x3c800000) + return w; /* cosh(tiny) = 1 */ + return one + (t * t) / (w + w); + } + + /* |x| in [0.5*ln2,22], return (exp(|x|)+1/exp(|x|)/2; */ + if (ix < 0x40360000) { + t = exp(x); + return half * t + half / t; + } + + /* |x| in [22, log(maxdouble)] return half*exp(|x|) */ + if (ix < 0x40862E42) + return half * exp(x); + + /* |x| in [log(maxdouble), overflowthresold] */ + GET_LOW_WORD(lx, x); + if (ix < 0x408633CE || (ix == 0x408633ce && lx <= (__uint32_t)0x8fb9f87d)) { + w = exp(half * x); + t = half * w; + return t * w; + } + + /* |x| > overflowthresold, cosh(x) overflow */ + return __math_oflow(0); +} + +_MATH_ALIAS_d_d(cosh) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_drem.c b/src/audio/ffmpeg_dec/libm/math/s_drem.c new file mode 100644 index 000000000000..6e7775577604 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_drem.c @@ -0,0 +1,28 @@ +/* +Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + +Developed at SunPro, a Sun Microsystems, Inc. business. +Permission to use, copy, modify, and distribute this +software is freely granted, provided that this notice +is preserved. + */ +/* + * drem() wrapper for remainder(). + * + * Written by J.T. Conklin, + * Placed into the Public Domain, 1994. + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +__float64 +drem64(__float64 x, __float64 y) +{ + return remainder64(x, y); +} + +_MATH_ALIAS_d_dd(drem) + +#endif diff --git a/src/audio/ffmpeg_dec/libm/math/s_erf.c b/src/audio/ffmpeg_dec/libm/math/s_erf.c new file mode 100644 index 000000000000..a0eaf29eb074 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_erf.c @@ -0,0 +1,391 @@ + +/* @(#)s_erf.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* +FUNCTION + <>, <>, <>, <>---error function +INDEX + erf +INDEX + erff +INDEX + erfc +INDEX + erfcf + +SYNOPSIS + #include + double erf(double <[x]>); + float erff(float <[x]>); + double erfc(double <[x]>); + float erfcf(float <[x]>); + +DESCRIPTION + <> calculates an approximation to the ``error function'', + which estimates the probability that an observation will fall within + <[x]> standard deviations of the mean (assuming a normal + distribution). + @tex + The error function is defined as + $${2\over\sqrt\pi}\times\int_0^x e^{-t^2}dt$$ + @end tex + + <> calculates the complementary probability; that is, + <)>> is <<1 - erf(<[x]>)>>. <> is computed directly, + so that you can use it to avoid the loss of precision that would + result from subtracting large probabilities (on large <[x]>) from 1. + + <> and <> differ from <> and <> only in the + argument and result types. + +RETURNS + For positive arguments, <> and all its variants return a + probability---a number between 0 and 1. + +PORTABILITY + None of the variants of <> are ANSI C. +*/ + +/* double erf(double x) + * double erfc(double x) + * x + * 2 |\ + * erf(x) = --------- | exp(-t*t)dt + * sqrt(pi) \| + * 0 + * + * erfc(x) = 1-erf(x) + * Note that + * erf(-x) = -erf(x) + * erfc(-x) = 2 - erfc(x) + * + * Method: + * 1. For |x| in [0, 0.84375] + * erf(x) = x + x*R(x^2) + * erfc(x) = 1 - erf(x) if x in [-.84375,0.25] + * = 0.5 + ((0.5-x)-x*R) if x in [0.25,0.84375] + * where R = P/Q where P is an odd poly of degree 8 and + * Q is an odd poly of degree 10. + * -57.90 + * | R - (erf(x)-x)/x | <= 2 + * + * + * Remark. The formula is derived by noting + * erf(x) = (2/sqrt(pi))*(x - x^3/3 + x^5/10 - x^7/42 + ....) + * and that + * 2/sqrt(pi) = 1.128379167095512573896158903121545171688 + * is close to one. The interval is chosen because the fix + * point of erf(x) is near 0.6174 (i.e., erf(x)=x when x is + * near 0.6174), and by some experiment, 0.84375 is chosen to + * guarantee the error is less than one ulp for erf. + * + * 2. For |x| in [0.84375,1.25], let s = |x| - 1, and + * c = 0.84506291151 rounded to single (24 bits) + * erf(x) = sign(x) * (c + P1(s)/Q1(s)) + * erfc(x) = (1-c) - P1(s)/Q1(s) if x > 0 + * 1+(c+P1(s)/Q1(s)) if x < 0 + * |P1/Q1 - (erf(|x|)-c)| <= 2**-59.06 + * Remark: here we use the taylor series expansion at x=1. + * erf(1+s) = erf(1) + s*Poly(s) + * = 0.845.. + P1(s)/Q1(s) + * That is, we use rational approximation to approximate + * erf(1+s) - (c = (single)0.84506291151) + * Note that |P1/Q1|< 0.078 for x in [0.84375,1.25] + * where + * P1(s) = degree 6 poly in s + * Q1(s) = degree 6 poly in s + * + * 3. For x in [1.25,1/0.35(~2.857143)], + * erfc(x) = (1/x)*exp(-x*x-0.5625+R1/S1) + * erf(x) = 1 - erfc(x) + * where + * R1(z) = degree 7 poly in z, (z=1/x^2) + * S1(z) = degree 8 poly in z + * + * 4. For x in [1/0.35,28] + * erfc(x) = (1/x)*exp(-x*x-0.5625+R2/S2) if x > 0 + * = 2.0 - (1/x)*exp(-x*x-0.5625+R2/S2) if -6 x >= 28 + * erf(x) = sign(x) *(1 - tiny) (raise inexact) + * erfc(x) = tiny*tiny (raise underflow) if x > 0 + * = 2 - tiny if x<0 + * + * 7. Special case: + * erf(0) = 0, erf(inf) = 1, erf(-inf) = -1, + * erfc(0) = 1, erfc(inf) = 0, erfc(-inf) = 2, + * erfc/erf(NaN) is NaN + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +static const __float64 + tiny = _F_64(1e-300), + half = _F_64(5.00000000000000000000e-01), /* 0x3FE00000, 0x00000000 */ + one = _F_64(1.00000000000000000000e+00), /* 0x3FF00000, 0x00000000 */ + two = _F_64(2.00000000000000000000e+00), /* 0x40000000, 0x00000000 */ + /* c = (float)0.84506291151 */ + erx = _F_64(8.45062911510467529297e-01), /* 0x3FEB0AC1, 0x60000000 */ + /* + * Coefficients for approximation to erf on [0,0.84375] + */ + efx = _F_64(1.28379167095512586316e-01), /* 0x3FC06EBA, 0x8214DB69 */ + efx8 = _F_64(1.02703333676410069053e+00), /* 0x3FF06EBA, 0x8214DB69 */ + pp0 = _F_64(1.28379167095512558561e-01), /* 0x3FC06EBA, 0x8214DB68 */ + pp1 = _F_64(-3.25042107247001499370e-01), /* 0xBFD4CD7D, 0x691CB913 */ + pp2 = _F_64(-2.84817495755985104766e-02), /* 0xBF9D2A51, 0xDBD7194F */ + pp3 = _F_64(-5.77027029648944159157e-03), /* 0xBF77A291, 0x236668E4 */ + pp4 = _F_64(-2.37630166566501626084e-05), /* 0xBEF8EAD6, 0x120016AC */ + qq1 = _F_64(3.97917223959155352819e-01), /* 0x3FD97779, 0xCDDADC09 */ + qq2 = _F_64(6.50222499887672944485e-02), /* 0x3FB0A54C, 0x5536CEBA */ + qq3 = _F_64(5.08130628187576562776e-03), /* 0x3F74D022, 0xC4D36B0F */ + qq4 = _F_64(1.32494738004321644526e-04), /* 0x3F215DC9, 0x221C1A10 */ + qq5 = _F_64(-3.96022827877536812320e-06), /* 0xBED09C43, 0x42A26120 */ + /* + * Coefficients for approximation to erf in [0.84375,1.25] + */ + pa0 = _F_64(-2.36211856075265944077e-03), /* 0xBF6359B8, 0xBEF77538 */ + pa1 = _F_64(4.14856118683748331666e-01), /* 0x3FDA8D00, 0xAD92B34D */ + pa2 = _F_64(-3.72207876035701323847e-01), /* 0xBFD7D240, 0xFBB8C3F1 */ + pa3 = _F_64(3.18346619901161753674e-01), /* 0x3FD45FCA, 0x805120E4 */ + pa4 = _F_64(-1.10894694282396677476e-01), /* 0xBFBC6398, 0x3D3E28EC */ + pa5 = _F_64(3.54783043256182359371e-02), /* 0x3FA22A36, 0x599795EB */ + pa6 = _F_64(-2.16637559486879084300e-03), /* 0xBF61BF38, 0x0A96073F */ + qa1 = _F_64(1.06420880400844228286e-01), /* 0x3FBB3E66, 0x18EEE323 */ + qa2 = _F_64(5.40397917702171048937e-01), /* 0x3FE14AF0, 0x92EB6F33 */ + qa3 = _F_64(7.18286544141962662868e-02), /* 0x3FB2635C, 0xD99FE9A7 */ + qa4 = _F_64(1.26171219808761642112e-01), /* 0x3FC02660, 0xE763351F */ + qa5 = _F_64(1.36370839120290507362e-02), /* 0x3F8BEDC2, 0x6B51DD1C */ + qa6 = _F_64(1.19844998467991074170e-02), /* 0x3F888B54, 0x5735151D */ + /* + * Coefficients for approximation to erfc in [1.25,1/0.35] + */ + ra0 = _F_64(-9.86494403484714822705e-03), /* 0xBF843412, 0x600D6435 */ + ra1 = _F_64(-6.93858572707181764372e-01), /* 0xBFE63416, 0xE4BA7360 */ + ra2 = _F_64(-1.05586262253232909814e+01), /* 0xC0251E04, 0x41B0E726 */ + ra3 = _F_64(-6.23753324503260060396e+01), /* 0xC04F300A, 0xE4CBA38D */ + ra4 = _F_64(-1.62396669462573470355e+02), /* 0xC0644CB1, 0x84282266 */ + ra5 = _F_64(-1.84605092906711035994e+02), /* 0xC067135C, 0xEBCCABB2 */ + ra6 = _F_64(-8.12874355063065934246e+01), /* 0xC0545265, 0x57E4D2F2 */ + ra7 = _F_64(-9.81432934416914548592e+00), /* 0xC023A0EF, 0xC69AC25C */ + sa1 = _F_64(1.96512716674392571292e+01), /* 0x4033A6B9, 0xBD707687 */ + sa2 = _F_64(1.37657754143519042600e+02), /* 0x4061350C, 0x526AE721 */ + sa3 = _F_64(4.34565877475229228821e+02), /* 0x407B290D, 0xD58A1A71 */ + sa4 = _F_64(6.45387271733267880336e+02), /* 0x40842B19, 0x21EC2868 */ + sa5 = _F_64(4.29008140027567833386e+02), /* 0x407AD021, 0x57700314 */ + sa6 = _F_64(1.08635005541779435134e+02), /* 0x405B28A3, 0xEE48AE2C */ + sa7 = _F_64(6.57024977031928170135e+00), /* 0x401A47EF, 0x8E484A93 */ + sa8 = _F_64(-6.04244152148580987438e-02), /* 0xBFAEEFF2, 0xEE749A62 */ + /* + * Coefficients for approximation to erfc in [1/.35,28] + */ + rb0 = _F_64(-9.86494292470009928597e-03), /* 0xBF843412, 0x39E86F4A */ + rb1 = _F_64(-7.99283237680523006574e-01), /* 0xBFE993BA, 0x70C285DE */ + rb2 = _F_64(-1.77579549177547519889e+01), /* 0xC031C209, 0x555F995A */ + rb3 = _F_64(-1.60636384855821916062e+02), /* 0xC064145D, 0x43C5ED98 */ + rb4 = _F_64(-6.37566443368389627722e+02), /* 0xC083EC88, 0x1375F228 */ + rb5 = _F_64(-1.02509513161107724954e+03), /* 0xC0900461, 0x6A2E5992 */ + rb6 = _F_64(-4.83519191608651397019e+02), /* 0xC07E384E, 0x9BDC383F */ + sb1 = _F_64(3.03380607434824582924e+01), /* 0x403E568B, 0x261D5190 */ + sb2 = _F_64(3.25792512996573918826e+02), /* 0x40745CAE, 0x221B9F0A */ + sb3 = _F_64(1.53672958608443695994e+03), /* 0x409802EB, 0x189D5118 */ + sb4 = _F_64(3.19985821950859553908e+03), /* 0x40A8FFB7, 0x688C246A */ + sb5 = _F_64(2.55305040643316442583e+03), /* 0x40A3F219, 0xCEDF3BE6 */ + sb6 = _F_64(4.74528541206955367215e+02), /* 0x407DA874, 0xE79FE763 */ + sb7 = _F_64(-2.24409524465858183362e+01); /* 0xC03670E2, 0x42712D62 */ + +__float64 +erf64(__float64 x) +{ + __int32_t hx, ix, i; + __float64 R, S, P, Q, s, y, z, r; + GET_HIGH_WORD(hx, x); + ix = hx & 0x7fffffff; + if (ix >= 0x7ff00000) { /* erf(nan)=nan */ + i = ((__uint32_t)hx >> 31) << 1; + return (__float64)(1 - i) + one / x; /* erf(+-inf)=+-1 */ + } + + if (ix < 0x3feb0000) { /* |x|<0.84375 */ + if (ix < 0x3e300000) { /* |x|<2**-28 */ + if (ix < 0x00800000) + return _F_64(0.125) * (_F_64(8.0) * x + efx8 * x); /*avoid underflow */ + return x + efx * x; + } + z = x * x; + r = pp0 + z * (pp1 + z * (pp2 + z * (pp3 + z * pp4))); + s = one + z * (qq1 + z * (qq2 + z * (qq3 + z * (qq4 + z * qq5)))); + y = r / s; + return x + x * y; + } + if (ix < 0x3ff40000) { /* 0.84375 <= |x| < 1.25 */ + s = fabs64(x) - one; + P = pa0 + + s * (pa1 + s * (pa2 + s * (pa3 + s * (pa4 + s * (pa5 + s * pa6))))); + Q = one + + s * (qa1 + s * (qa2 + s * (qa3 + s * (qa4 + s * (qa5 + s * qa6))))); + if (hx >= 0) + return erx + P / Q; + else + return -erx - P / Q; + } + if (ix >= 0x40180000) { /* inf>|x|>=6 */ + if (hx >= 0) + return one - tiny; + else + return tiny - one; + } + x = fabs64(x); + s = one / (x * x); + if (ix < 0x4006DB6E) { /* |x| < 1/0.35 */ + R = ra0 + + s * (ra1 + + s * (ra2 + + s * (ra3 + s * (ra4 + s * (ra5 + s * (ra6 + s * ra7)))))); + S = one + + s * (sa1 + + s * (sa2 + + s * (sa3 + + s * (sa4 + + s * (sa5 + s * (sa6 + s * (sa7 + s * sa8))))))); + } else { /* |x| >= 1/0.35 */ + R = rb0 + + s * (rb1 + s * (rb2 + s * (rb3 + s * (rb4 + s * (rb5 + s * rb6))))); + S = one + + s * (sb1 + + s * (sb2 + + s * (sb3 + s * (sb4 + s * (sb5 + s * (sb6 + s * sb7)))))); + } + z = x; + SET_LOW_WORD(z, 0); + r = exp(-z * z - _F_64(0.5625)) * exp((z - x) * (z + x) + R / S); + if (hx >= 0) + return one - r / x; + else + return r / x - one; +} + +_MATH_ALIAS_d_d(erf) + +__float64 +erfc64(__float64 x) +{ + __int32_t hx, ix; + __float64 R, S, P, Q, s, y, z, r; + GET_HIGH_WORD(hx, x); + ix = hx & 0x7fffffff; + if (ix >= 0x7ff00000) { /* erfc(nan)=nan */ + /* erfc(+-inf)=0,2 */ + return (__float64)(((__uint32_t)hx >> 31) << 1) + one / x; + } + + if (ix < 0x3feb0000) { /* |x|<0.84375 */ + if (ix < 0x3c700000) /* |x|<2**-56 */ + return one - x; + z = x * x; + r = pp0 + z * (pp1 + z * (pp2 + z * (pp3 + z * pp4))); + s = one + z * (qq1 + z * (qq2 + z * (qq3 + z * (qq4 + z * qq5)))); + y = r / s; + if (hx < 0x3fd00000) { /* x<1/4 */ + return one - (x + x * y); + } else { + r = x * y; + r += (x - half); + return half - r; + } + } + if (ix < 0x3ff40000) { /* 0.84375 <= |x| < 1.25 */ + s = fabs64(x) - one; + P = pa0 + + s * (pa1 + s * (pa2 + s * (pa3 + s * (pa4 + s * (pa5 + s * pa6))))); + Q = one + + s * (qa1 + s * (qa2 + s * (qa3 + s * (qa4 + s * (qa5 + s * qa6))))); + if (hx >= 0) { + z = one - erx; + return z - P / Q; + } else { + z = erx + P / Q; + return one + z; + } + } + if (ix < 0x403c0000) { /* |x|<28 */ + x = fabs64(x); + s = one / (x * x); + if (ix < 0x4006DB6D) { /* |x| < 1/.35 ~ 2.857143*/ + R = ra0 + + s * (ra1 + + s * (ra2 + + s * (ra3 + + s * (ra4 + s * (ra5 + s * (ra6 + s * ra7)))))); + S = one + + s * (sa1 + + s * (sa2 + + s * (sa3 + + s * (sa4 + + s * (sa5 + + s * (sa6 + s * (sa7 + s * sa8))))))); + } else { /* |x| >= 1/.35 ~ 2.857143 */ + if (hx < 0 && ix >= 0x40180000) + return two - tiny; /* x < -6 */ + R = rb0 + + s * (rb1 + + s * (rb2 + s * (rb3 + s * (rb4 + s * (rb5 + s * rb6))))); + S = one + + s * (sb1 + + s * (sb2 + + s * (sb3 + + s * (sb4 + s * (sb5 + s * (sb6 + s * sb7)))))); + } + z = x; + SET_LOW_WORD(z, 0); + r = exp(-z * z - _F_64(0.5625)) * exp((z - x) * (z + x) + R / S); + if (hx > 0) + return r / x; + else + return two - r / x; + } else { + if (hx > 0) + return __math_uflow(0); + else + return two - tiny; + } +} + +_MATH_ALIAS_d_d(erfc) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_exp.c b/src/audio/ffmpeg_dec/libm/math/s_exp.c new file mode 100644 index 000000000000..84d464aa9c5a --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_exp.c @@ -0,0 +1,171 @@ + +/* @(#)e_exp.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* exp(x) + * Returns the exponential of x. + * + * Method + * 1. Argument reduction: + * Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658. + * Given x, find r and integer k such that + * + * x = k*ln2 + r, |r| <= 0.5*ln2. + * + * Here r will be represented as r = hi-lo for better + * accuracy. + * + * 2. Approximation of exp(r) by a special rational function on + * the interval [0,0.34658]: + * Write + * R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ... + * We use a special Reme algorithm on [0,0.34658] to generate + * a polynomial of degree 5 to approximate R. The maximum error + * of this polynomial approximation is bounded by 2**-59. In + * other words, + * R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5 + * (where z=r*r, and the values of P1 to P5 are listed below) + * and + * | 5 | -59 + * | 2.0+P1*z+...+P5*z - R(z) | <= 2 + * | | + * The computation of exp(r) thus becomes + * 2*r + * exp(r) = 1 + ------- + * R - r + * r*R1(r) + * = 1 + r + ----------- (for better accuracy) + * 2 - R1(r) + * where + * 2 4 10 + * R1(r) = r - (P1*r + P2*r + ... + P5*r ). + * + * 3. Scale back to obtain exp(x): + * From step 1, we have + * exp(x) = 2^k * exp(r) + * + * Special cases: + * exp(INF) is INF, exp(NaN) is NaN; + * exp(-INF) is 0, and + * for finite argument, only exp(0)=1 is exact. + * + * Accuracy: + * according to an error analysis, the error is always less than + * 1 ulp (unit in the last place). + * + * Misc. info. + * For IEEE double + * if x > 7.09782712893383973096e+02 then exp(x) overflow + * if x < -7.45133219101941108420e+02 then exp(x) underflow + * + * Constants: + * The hexadecimal values are the intended ones for the following + * constants. The decimal values may be used, provided that the + * compiler will convert from decimal to binary accurately enough + * to produce the hexadecimal values shown. + */ + +#include "fdlibm.h" +#include "math_config.h" +#if __OBSOLETE_MATH_DOUBLE + +#ifdef _NEED_FLOAT64 + +static const __float64 + one = _F_64(1.0), + halF[2] = {_F_64(0.5),_F_64(-0.5),}, + huge = _F_64(1.0e+300), + twom1000 = _F_64(9.33263618503218878990e-302), /* 2**-1000=0x01700000,0*/ + o_threshold = _F_64(7.09782712893383973096e+02), /* 0x40862E42, 0xFEFA39EF */ + u_threshold = _F_64(-7.45133219101941108420e+02), /* 0xc0874910, 0xD52D3051 */ + ln2HI[2] ={ _F_64(6.93147180369123816490e-01), /* 0x3fe62e42, 0xfee00000 */ + _F_64(-6.93147180369123816490e-01),},/* 0xbfe62e42, 0xfee00000 */ + ln2LO[2] ={ _F_64(1.90821492927058770002e-10), /* 0x3dea39ef, 0x35793c76 */ + _F_64(-1.90821492927058770002e-10),},/* 0xbdea39ef, 0x35793c76 */ + invln2 = _F_64(1.44269504088896338700e+00), /* 0x3ff71547, 0x652b82fe */ + P1 = _F_64(1.66666666666666019037e-01), /* 0x3FC55555, 0x5555553E */ + P2 = _F_64(-2.77777777770155933842e-03), /* 0xBF66C16C, 0x16BEBD93 */ + P3 = _F_64(6.61375632143793436117e-05), /* 0x3F11566A, 0xAF25DE2C */ + P4 = _F_64(-1.65339022054652515390e-06), /* 0xBEBBBD41, 0xC5D26BF1 */ + P5 = _F_64(4.13813679705723846039e-08); /* 0x3E663769, 0x72BEA4D0 */ + +__float64 +exp64(__float64 x) /* default IEEE double exp */ +{ + __float64 y, hi, lo, c, t; + __int32_t k = 0, xsb; + __uint32_t hx; + + GET_HIGH_WORD(hx, x); + xsb = (hx >> 31) & 1; /* sign bit of x */ + hx &= 0x7fffffff; /* high word of |x| */ + + /* filter out non-finite argument */ + if (hx >= 0x40862E42) { /* if |x|>=709.78... */ + if (hx >= 0x7ff00000) { + __uint32_t lx; + GET_LOW_WORD(lx, x); + if (((hx & 0xfffff) | lx) != 0) + return x + x; /* NaN */ + else + return (xsb == 0) ? x : _F_64(0.0); /* exp(+-inf)={inf,0} */ + } + if (x > o_threshold) + return __math_oflow(0); /* overflow */ + if (x < u_threshold) + return __math_uflow(0); /* underflow */ + } + + /* argument reduction */ + if (hx > 0x3fd62e42) { /* if |x| > 0.5 ln2 */ + if (hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */ + hi = x - ln2HI[xsb]; + lo = ln2LO[xsb]; + k = 1 - xsb - xsb; + } else { + k = invln2 * x + halF[xsb]; + t = k; + hi = x - t * ln2HI[0]; /* t*ln2HI is exact here */ + lo = t * ln2LO[0]; + } + x = hi - lo; + } else if (hx < 0x3df00000) { /* when |x|<2**-32 */ + if (huge + x > one) + return one + x; /* trigger inexact */ + } + + /* x is now in primary range */ + t = x * x; + c = x - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5)))); + if (k == 0) + return one - ((x * c) / (c - _F_64(2.0)) - x); + else + y = one - ((lo - (x * c) / (_F_64(2.0) - c)) - hi); + if (k >= -1021) { + __uint32_t hy; + GET_HIGH_WORD(hy, y); + SET_HIGH_WORD(y, hy + lsl(k, 20)); /* add k to y's exponent */ + return y; + } else { + __uint32_t hy; + GET_HIGH_WORD(hy, y); + SET_HIGH_WORD(y, hy + lsl((k + 1000), 20)); /* add k to y's exponent */ + return y * twom1000; + } +} + +_MATH_ALIAS_d_d(exp) + +#endif /* _NEED_FLOAT64 */ +#else +#include "../common/exp.c" +#endif /* __OBSOLETE_MATH_DOUBLE */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_exp2.c b/src/audio/ffmpeg_dec/libm/math/s_exp2.c new file mode 100644 index 000000000000..813b3a93defe --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_exp2.c @@ -0,0 +1,68 @@ + +/* @(#)w_exp2.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* +FUNCTION + <>, <>---exponential, base 2 +INDEX + exp2 +INDEX + exp2f + +SYNOPSIS + #include + double exp2(double <[x]>); + float exp2f(float <[x]>); + +DESCRIPTION + <> and <> calculate 2 ^ <[x]>, that is, + @ifnottex + 2 raised to the power <[x]>. + @end ifnottex + @tex + $2^x$ + @end tex + +RETURNS + On success, <> and <> return the calculated value. + If the result underflows, the returned value is <<0>>. If the + result overflows, the returned value is <>. In + either case, <> is set to <>. + +PORTABILITY + ANSI C, POSIX. + +*/ + +/* + * wrapper exp2(x) + */ + +#include "fdlibm.h" +#if __OBSOLETE_MATH_DOUBLE +#ifdef _NEED_FLOAT64 + +extern __float64 _pow64(__float64, __float64); + +__float64 __no_builtin +exp264(__float64 x) /* wrapper exp2 */ +{ + return _pow64(2.0, x); +} + +_MATH_ALIAS_d_d(exp2) + +#endif /* _NEED_FLOAT64 */ +#else +#include "../common/exp2.c" +#endif /* __OBSOLETE_MATH_DOUBLE */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_fabs.c b/src/audio/ffmpeg_dec/libm/math/s_fabs.c new file mode 100644 index 000000000000..5cc441b1497d --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_fabs.c @@ -0,0 +1,63 @@ + +/* @(#)s_fabs.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* +FUNCTION + <>, <>---absolute value (magnitude) +INDEX + fabs +INDEX + fabsf + +SYNOPSIS + #include + double fabs(double <[x]>); + float fabsf(float <[x]>); + +DESCRIPTION +<> and <> calculate +@tex +$|x|$, +@end tex +the absolute value (magnitude) of the argument <[x]>, by direct +manipulation of the bit representation of <[x]>. + +RETURNS +The calculated value is returned. No errors are detected. + +PORTABILITY +<> is ANSI. +<> is an extension. + +*/ + +/* + * fabs(x) returns the absolute value of x. + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +__float64 +fabs64(__float64 x) +{ + __uint32_t high; + GET_HIGH_WORD(high, x); + SET_HIGH_WORD(x, high & 0x7fffffff); + return x; +} + +_MATH_ALIAS_d_d(fabs) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_floor.c b/src/audio/ffmpeg_dec/libm/math/s_floor.c new file mode 100644 index 000000000000..ba4c6d2ef342 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_floor.c @@ -0,0 +1,120 @@ + +/* @(#)s_floor.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* +FUNCTION +<>, <>, <>, <>---floor and ceiling +INDEX + floor +INDEX + floorf +INDEX + ceil +INDEX + ceilf + +SYNOPSIS + #include + double floor(double <[x]>); + float floorf(float <[x]>); + double ceil(double <[x]>); + float ceilf(float <[x]>); + +DESCRIPTION +<> and <> find +@tex +$\lfloor x \rfloor$, +@end tex +the nearest integer less than or equal to <[x]>. +<> and <> find +@tex +$\lceil x\rceil$, +@end tex +the nearest integer greater than or equal to <[x]>. + +RETURNS +<> and <> return the integer result as a double. +<> and <> return the integer result as a float. + +PORTABILITY +<> and <> are ANSI. +<> and <> are extensions. + + +*/ + +/* + * floor(x) + * Return x rounded toward -inf to integral value + * Method: + * Bit twiddling. + * Exception: + * Inexact flag raised if x not equal to floor(x). + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +__float64 +floor64(__float64 x) +{ + __int32_t i0, i1, j0, j; + __uint32_t i; + EXTRACT_WORDS(i0, i1, x); + j0 = ((i0 >> 20) & 0x7ff) - 0x3ff; + if (j0 < 20) { + if (j0 < 0) { + if (i0 >= 0) { + i0 = i1 = 0; + } else if (((i0 & 0x7fffffff) | i1) != 0) { + i0 = 0xbff00000; + i1 = 0; + } + } else { + i = (0x000fffff) >> j0; + if (((i0 & i) | i1) == 0) + return x; /* x is integral */ + if (i0 < 0) + i0 += (0x00100000) >> j0; + i0 &= (~i); + i1 = 0; + } + } else if (j0 > 51) { + if (j0 == 0x400) + return x + x; /* inf or NaN */ + else + return x; /* x is integral */ + } else { + i = ((__uint32_t)(0xffffffff)) >> (j0 - 20); + if ((i1 & i) == 0) + return x; /* x is integral */ + if (i0 < 0) { + if (j0 == 20) + i0 += 1; + else { + j = i1 + ((__uint32_t) 1 << (52 - j0)); + if ((__uint32_t) j < (__uint32_t) i1) + i0 += 1; /* got a carry */ + i1 = j; + } + } + i1 &= (~i); + } + INSERT_WORDS(x, i0, i1); + return x; +} + +_MATH_ALIAS_d_d(floor) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_fmod.c b/src/audio/ffmpeg_dec/libm/math/s_fmod.c new file mode 100644 index 000000000000..ca02c4f2be24 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_fmod.c @@ -0,0 +1,164 @@ + +/* @(#)e_fmod.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * fmod(x,y) + * Return x mod y in exact arithmetic + * Method: shift and subtract + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +static const __float64 + one = _F_64(1.0), + Zero[] = { _F_64(0.0), _F_64(-0.0) }; + +__float64 +fmod64(__float64 x, __float64 y) +{ + __int32_t n, hx, hy, hz, ix, iy, sx, i; + __uint32_t lx, ly, lz; + + EXTRACT_WORDS(hx, lx, x); + EXTRACT_WORDS(hy, ly, y); + sx = hx & 0x80000000; /* sign of x */ + hx ^= sx; /* |x| */ + hy &= 0x7fffffff; /* |y| */ + + /* purge off exception values */ + if (isnan(x) || isnan(y)) /* x or y nan, return nan */ + return x + y; + + if (isinf(x)) /* x == inf, domain error */ + return __math_invalid(x); + + if ((hy | ly) == 0) /* y=0, domain error */ + return __math_invalid(y); + + if (hx <= hy) { + if ((hx < hy) || (lx < ly)) + return x; /* |x|<|y| return x */ + if (lx == ly) + return Zero[(__uint32_t)sx >> 31]; /* |x|=|y| return x*0*/ + } + + /* determine ix = ilogb(x) */ + if (hx < 0x00100000) { /* subnormal x */ + if (hx == 0) { + for (ix = -1043, i = lx; i > 0; i = lsl(i, 1)) + ix -= 1; + } else { + for (ix = -1022, i = lsl(hx, 11); i > 0; i = lsl(i, 1)) + ix -= 1; + } + } else + ix = (hx >> 20) - 1023; + + /* determine iy = ilogb(y) */ + if (hy < 0x00100000) { /* subnormal y */ + if (hy == 0) { + for (iy = -1043, i = ly; i > 0; i = lsl(i, 1)) + iy -= 1; + } else { + for (iy = -1022, i = lsl(hy, 11); i > 0; i = lsl(i, 1)) + iy -= 1; + } + } else + iy = (hy >> 20) - 1023; + + /* set up {hx,lx}, {hy,ly} and align y to x */ + if (ix >= -1022) + hx = 0x00100000 | (0x000fffff & hx); + else { /* subnormal x, shift x to normal */ + n = -1022 - ix; + if (n <= 31) { + hx = (hx << n) | (lx >> (32 - n)); + lx <<= n; + } else { + hx = lx << (n - 32); + lx = 0; + } + } + if (iy >= -1022) + hy = 0x00100000 | (0x000fffff & hy); + else { /* subnormal y, shift y to normal */ + n = -1022 - iy; + if (n <= 31) { + hy = (hy << n) | (ly >> (32 - n)); + ly <<= n; + } else { + hy = ly << (n - 32); + ly = 0; + } + } + + /* fix point fmod */ + n = ix - iy; + while (n--) { + hz = hx - hy; + lz = lx - ly; + if (lx < ly) + hz -= 1; + if (hz < 0) { + hx = hx + hx + (lx >> 31); + lx = lx + lx; + } else { + if ((hz | lz) == 0) /* return sign(x)*0 */ + return Zero[(__uint32_t)sx >> 31]; + hx = hz + hz + (lz >> 31); + lx = lz + lz; + } + } + hz = hx - hy; + lz = lx - ly; + if (lx < ly) + hz -= 1; + if (hz >= 0) { + hx = hz; + lx = lz; + } + + /* convert back to floating value and restore the sign */ + if ((hx | lx) == 0) /* return sign(x)*0 */ + return Zero[(__uint32_t)sx >> 31]; + while (hx < 0x00100000) { /* normalize x */ + hx = hx + hx + (lx >> 31); + lx = lx + lx; + iy -= 1; + } + if (iy >= -1022) { /* normalize output */ + hx = ((hx - 0x00100000) | ((iy + 1023) << 20)); + INSERT_WORDS(x, hx | sx, lx); + } else { /* subnormal output */ + n = -1022 - iy; + if (n <= 20) { + lx = (lx >> n) | ((__uint32_t)hx << (32 - n)); + hx >>= n; + } else if (n <= 31) { + lx = (hx << (32 - n)) | (lx >> n); + hx = sx; + } else { + lx = hx >> (n - 32); + hx = sx; + } + INSERT_WORDS(x, hx | sx, lx); + x *= one; /* create necessary signal */ + } + return x; /* exact output */ +} + +_MATH_ALIAS_d_dd(fmod) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_frexp.c b/src/audio/ffmpeg_dec/libm/math/s_frexp.c new file mode 100644 index 000000000000..292636abd29d --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_frexp.c @@ -0,0 +1,98 @@ + +/* @(#)s_frexp.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* +FUNCTION + <>, <>---split floating-point number +INDEX + frexp +INDEX + frexpf + +SYNOPSIS + #include + double frexp(double <[val]>, int *<[exp]>); + float frexpf(float <[val]>, int *<[exp]>); + +DESCRIPTION + All nonzero, normal numbers can be described as <[m]> * 2**<[p]>. + <> represents the double <[val]> as a mantissa <[m]> + and a power of two <[p]>. The resulting mantissa will always + be greater than or equal to <<0.5>>, and less than <<1.0>> (as + long as <[val]> is nonzero). The power of two will be stored + in <<*>><[exp]>. + +@ifnottex +<[m]> and <[p]> are calculated so that +<[val]> is <[m]> times <<2>> to the power <[p]>. +@end ifnottex +@tex +<[m]> and <[p]> are calculated so that +$ val = m \times 2^p $. +@end tex + +<> is identical, other than taking and returning +floats rather than doubles. + +RETURNS +<> returns the mantissa <[m]>. If <[val]> is <<0>>, infinity, +or Nan, <> will set <<*>><[exp]> to <<0>> and return <[val]>. + +PORTABILITY +<> is ANSI. +<> is an extension. + + +*/ + +/* + * for non-zero x + * x = frexp(arg,&exp); + * return a double fp quantity x such that 0.5 <= |x| <1.0 + * and the corresponding binary exponent "exp". That is + * arg = x*2^exp. + * If arg is inf, 0.0, or NaN, then frexp(arg,&exp) returns arg + * with *exp=0. + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +static const double two54 = + _F_64(1.80143985094819840000e+16); /* 0x43500000, 0x00000000 */ + +__float64 +frexp64(__float64 x, int *eptr) +{ + __int32_t hx, ix, lx; + EXTRACT_WORDS(hx, lx, x); + ix = 0x7fffffff & hx; + *eptr = 0; + if (ix >= 0x7ff00000 || ((ix | lx) == 0)) + return x + x; /* 0,inf,nan */ + if (ix < 0x00100000) { /* subnormal */ + x *= two54; + GET_HIGH_WORD(hx, x); + ix = hx & 0x7fffffff; + *eptr = -54; + } + *eptr += (ix >> 20) - 1022; + hx = (hx & 0x800fffff) | 0x3fe00000; + SET_HIGH_WORD(x, hx); + return x; +} + +_MATH_ALIAS_d_dI(frexp) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_hypot.c b/src/audio/ffmpeg_dec/libm/math/s_hypot.c new file mode 100644 index 000000000000..56408a2a8fb9 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_hypot.c @@ -0,0 +1,143 @@ + +/* @(#)e_hypot.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* hypot(x,y) + * + * Method : + * If (assume round-to-nearest) z=x*x+y*y + * has error less than sqrt(2)/2 ulp, than + * sqrt(z) has error less than 1 ulp (exercise). + * + * So, compute sqrt(x*x+y*y) with some care as + * follows to get the error below 1 ulp: + * + * Assume x>y>0; + * (if possible, set rounding to round-to-nearest) + * 1. if x > 2y use + * x1*x1+(y*y+(x2*(x+x1))) for x*x+y*y + * where x1 = x with lower 32 bits cleared, x2 = x-x1; else + * 2. if x <= 2y use + * t1*y1+((x-y)*(x-y)+(t1*y2+t2*y)) + * where t1 = 2x with lower 32 bits cleared, t2 = 2x-t1, + * y1= y with lower 32 bits chopped, y2 = y-y1. + * + * NOTE: scaling may be necessary if some argument is too + * large or too tiny + * + * Special cases: + * hypot(x,y) is INF if x or y is +INF or -INF; else + * hypot(x,y) is NAN if x or y is NAN. + * + * Accuracy: + * hypot(x,y) returns sqrt(x^2+y^2) with error less + * than 1 ulps (units in the last place) + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +__float64 +hypot64(__float64 x, __float64 y) +{ + __float64 a = x, b = y, t1, t2, y1, y2, w; + __int32_t j, k, ha, hb; + + GET_HIGH_WORD(ha, x); + ha &= 0x7fffffff; + GET_HIGH_WORD(hb, y); + hb &= 0x7fffffff; + if (hb > ha) { + a = y; + b = x; + j = ha; + ha = hb; + hb = j; + } else { + a = x; + b = y; + } + SET_HIGH_WORD(a, ha); /* a <- |a| */ + SET_HIGH_WORD(b, hb); /* b <- |b| */ + if ((ha - hb) > 0x3c00000) { + return a + b; + } /* x/y > 2**60 */ + k = 0; + if (ha > 0x5f300000) { /* a>2**500 */ + if (ha >= 0x7ff00000) { /* Inf or NaN */ + __uint32_t low; + w = a + b; /* for sNaN */ + GET_LOW_WORD(low, a); + if (((ha & 0xfffff) | low) == 0 && !issignaling(b)) + w = a; + GET_LOW_WORD(low, b); + if (((hb ^ 0x7ff00000) | low) == 0 && !issignaling(a)) + w = b; + return w; + } + /* scale a and b by 2**-600 */ + ha -= 0x25800000; + hb -= 0x25800000; + k += 600; + SET_HIGH_WORD(a, ha); + SET_HIGH_WORD(b, hb); + } + if (hb < 0x20b00000) { /* b < 2**-500 */ + if (hb <= 0x000fffff) { /* subnormal b or 0 */ + __uint32_t low; + GET_LOW_WORD(low, b); + if ((hb | low) == 0) + return a; + t1 = 0; + SET_HIGH_WORD(t1, 0x7fd00000); /* t1=2^1022 */ + b *= t1; + a *= t1; + k -= 1022; + } else { /* scale a and b by 2^600 */ + ha += 0x25800000; /* a *= 2^600 */ + hb += 0x25800000; /* b *= 2^600 */ + k -= 600; + SET_HIGH_WORD(a, ha); + SET_HIGH_WORD(b, hb); + } + } + /* medium size a and b */ + w = a - b; + if (w > b) { + t1 = 0; + SET_HIGH_WORD(t1, ha); + t2 = a - t1; + w = sqrt64(t1 * t1 - (b * (-b) - t2 * (a + t1))); + } else { + a = a + a; + y1 = 0; + SET_HIGH_WORD(y1, hb); + y2 = b - y1; + t1 = 0; + SET_HIGH_WORD(t1, ha + 0x00100000); + t2 = a - t1; + w = sqrt64(t1 * y1 - (w * (-w) - (t1 * y2 + t2 * b))); + } + if (k != 0) { + __uint32_t high; + t1 = _F_64(1.0); + GET_HIGH_WORD(high, t1); + SET_HIGH_WORD(t1, high + lsl(k, 20)); + w *= t1; + } + return check_oflow(w); +} + +_MATH_ALIAS_d_dd(hypot) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_j0.c b/src/audio/ffmpeg_dec/libm/math/s_j0.c new file mode 100644 index 000000000000..16ea9f16316a --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_j0.c @@ -0,0 +1,443 @@ + +/* @(#)e_j0.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* j0(x), y0(x) + * Bessel function of the first and second kinds of order zero. + * Method -- j0(x): + * 1. For tiny x, we use j0(x) = 1 - x^2/4 + x^4/64 - ... + * 2. Reduce x to |x| since j0(x)=j0(-x), and + * for x in (0,2) + * j0(x) = 1-z/4+ z^2*R0/S0, where z = x*x; + * (precision: |j0-1+z/4-z^2R0/S0 |<2**-63.67 ) + * for x in (2,inf) + * j0(x) = sqrt(2/(pi*x))*(p0(x)*cos(x0)-q0(x)*sin(x0)) + * where x0 = x-pi/4. It is better to compute sin(x0),cos(x0) + * as follow: + * cos(x0) = cos(x)cos(pi/4)+sin(x)sin(pi/4) + * = 1/sqrt(2) * (cos(x) + sin(x)) + * sin(x0) = sin(x)cos(pi/4)-cos(x)sin(pi/4) + * = 1/sqrt(2) * (sin(x) - cos(x)) + * (To avoid cancellation, use + * sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x)) + * to compute the worse one.) + * + * 3 Special cases + * j0(nan)= nan + * j0(0) = 1 + * j0(inf) = 0 + * + * Method -- y0(x): + * 1. For x<2. + * Since + * y0(x) = 2/pi*(j0(x)*(ln(x/2)+Euler) + x^2/4 - ...) + * therefore y0(x)-2/pi*j0(x)*ln(x) is an even function. + * We use the following function to approximate y0, + * y0(x) = U(z)/V(z) + (2/pi)*(j0(x)*ln(x)), z= x^2 + * where + * U(z) = u00 + u01*z + ... + u06*z^6 + * V(z) = 1 + v01*z + ... + v04*z^4 + * with absolute approximation error bounded by 2**-72. + * Note: For tiny x, U/V = u0 and j0(x)~1, hence + * y0(tiny) = u0 + (2/pi)*ln(tiny), (choose tiny<2**-27) + * 2. For x>=2. + * y0(x) = sqrt(2/(pi*x))*(p0(x)*cos(x0)+q0(x)*sin(x0)) + * where x0 = x-pi/4. It is better to compute sin(x0),cos(x0) + * by the method mentioned above. + * 3. Special cases: y0(0)=-inf, y0(x<0)=NaN, y0(inf)=0. + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +static __float64 pzero(__float64); +static __float64 qzero(__float64); + +static const __float64 + huge = _F_64(1e300), one = _F_64(1.0), + invsqrtpi = _F_64(5.64189583547756279280e-01), /* 0x3FE20DD7, 0x50429B6D */ + tpi = _F_64(6.36619772367581382433e-01), /* 0x3FE45F30, 0x6DC9C883 */ + /* R0/S0 on [0, 2.00] */ + R02 = _F_64(1.56249999999999947958e-02), /* 0x3F8FFFFF, 0xFFFFFFFD */ + R03 = _F_64(-1.89979294238854721751e-04), /* 0xBF28E6A5, 0xB61AC6E9 */ + R04 = _F_64(1.82954049532700665670e-06), /* 0x3EBEB1D1, 0x0C503919 */ + R05 = _F_64(-4.61832688532103189199e-09), /* 0xBE33D5E7, 0x73D63FCE */ + S01 = _F_64(1.56191029464890010492e-02), /* 0x3F8FFCE8, 0x82C8C2A4 */ + S02 = _F_64(1.16926784663337450260e-04), /* 0x3F1EA6D2, 0xDD57DBF4 */ + S03 = _F_64(5.13546550207318111446e-07), /* 0x3EA13B54, 0xCE84D5A9 */ + S04 = _F_64(1.16614003333790000205e-09); /* 0x3E1408BC, 0xF4745D8F */ + +static const __float64 zero = _F_64(0.0); + +__float64 +j064(__float64 x) +{ + __float64 z, s, c, ss, cc, r, u, v; + __int32_t hx, ix; + + if (isnan(x)) + return x + x; + + if (isinf(x)) + return _F_64(0.0); + + GET_HIGH_WORD(hx, x); + ix = hx & 0x7fffffff; + x = fabs64(x); + if (ix >= 0x40000000) { /* |x| >= 2.0 */ + s = sin64(x); + c = cos64(x); + ss = s - c; + cc = s + c; + if (ix < 0x7fe00000) { /* make sure x+x not overflow */ + z = -cos64(x + x); + if ((s * c) < zero) + cc = z / ss; + else + ss = z / cc; + } + /* + * j0(x) = 1/sqrt(pi) * (P(0,x)*cc - Q(0,x)*ss) / sqrt(x) + * y0(x) = 1/sqrt(pi) * (P(0,x)*ss + Q(0,x)*cc) / sqrt(x) + */ + if (ix > 0x48000000) + z = (invsqrtpi * cc) / sqrt64(x); + else { + u = pzero(x); + v = qzero(x); + z = invsqrtpi * (u * cc - v * ss) / sqrt64(x); + } + return z; + } + if (ix < 0x3f200000) { /* |x| < 2**-13 */ + if (huge + x > one) { /* raise inexact if x != 0 */ + if (ix < 0x3e400000) + return one; /* |x|<2**-27 */ + else + return one - _F_64(0.25) * x * x; + } + } + z = x * x; + r = z * (R02 + z * (R03 + z * (R04 + z * R05))); + s = one + z * (S01 + z * (S02 + z * (S03 + z * S04))); + if (ix < 0x3FF00000) { /* |x| < 1.00 */ + return one + z * (_F_64(-0.25) + (r / s)); + } else { + u = _F_64(0.5) * x; + return ((one + u) * (one - u) + z * (r / s)); + } +} + +_MATH_ALIAS_d_d(j0) + +static const __float64 u00 = + _F_64(-7.38042951086872317523e-02), /* 0xBFB2E4D6, 0x99CBD01F */ + u01 = _F_64(1.76666452509181115538e-01), /* 0x3FC69D01, 0x9DE9E3FC */ + u02 = _F_64(-1.38185671945596898896e-02), /* 0xBF8C4CE8, 0xB16CFA97 */ + u03 = _F_64(3.47453432093683650238e-04), /* 0x3F36C54D, 0x20B29B6B */ + u04 = _F_64(-3.81407053724364161125e-06), /* 0xBECFFEA7, 0x73D25CAD */ + u05 = _F_64(1.95590137035022920206e-08), /* 0x3E550057, 0x3B4EABD4 */ + u06 = _F_64(-3.98205194132103398453e-11), /* 0xBDC5E43D, 0x693FB3C8 */ + v01 = _F_64(1.27304834834123699328e-02), /* 0x3F8A1270, 0x91C9C71A */ + v02 = _F_64(7.60068627350353253702e-05), /* 0x3F13ECBB, 0xF578C6C1 */ + v03 = _F_64(2.59150851840457805467e-07), /* 0x3E91642D, 0x7FF202FD */ + v04 = _F_64(4.41110311332675467403e-10); /* 0x3DFE5018, 0x3BD6D9EF */ + +__float64 +y064(__float64 x) +{ + __float64 z, s, c, ss, cc, u, v; + __int32_t hx, ix, lx; + + EXTRACT_WORDS(hx, lx, x); + ix = 0x7fffffff & hx; + + if ((ix | lx) == 0) + return __math_divzero(1); + + if (isnan(x)) + return x + x; + + if (hx < 0) + return __math_invalid(x); + + if (ix >= 0x7ff00000) + return _F_64(0.0); + + if (ix >= 0x40000000) { /* |x| >= 2.0 */ + /* y0(x) = sqrt(2/(pi*x))*(p0(x)*sin(x0)+q0(x)*cos(x0)) + * where x0 = x-pi/4 + * Better formula: + * cos(x0) = cos(x)cos(pi/4)+sin(x)sin(pi/4) + * = 1/sqrt(2) * (sin(x) + cos(x)) + * sin(x0) = sin(x)cos(3pi/4)-cos(x)sin(3pi/4) + * = 1/sqrt(2) * (sin(x) - cos(x)) + * To avoid cancellation, use + * sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x)) + * to compute the worse one. + */ + s = sin64(x); + c = cos64(x); + ss = s - c; + cc = s + c; + /* + * j0(x) = 1/sqrt(pi) * (P(0,x)*cc - Q(0,x)*ss) / sqrt(x) + * y0(x) = 1/sqrt(pi) * (P(0,x)*ss + Q(0,x)*cc) / sqrt(x) + */ + if (ix < 0x7fe00000) { /* make sure x+x not overflow */ + z = -cos64(x + x); + if ((s * c) < zero) + cc = z / ss; + else + ss = z / cc; + } + if (ix > 0x48000000) + z = (invsqrtpi * ss) / sqrt64(x); + else { + u = pzero(x); + v = qzero(x); + z = invsqrtpi * (u * ss + v * cc) / sqrt64(x); + } + return z; + } + if (ix <= 0x3e400000) { /* x < 2**-27 */ + return (u00 + tpi * log64(x)); + } + z = x * x; + u = u00 + + z * (u01 + z * (u02 + z * (u03 + z * (u04 + z * (u05 + z * u06))))); + v = one + z * (v01 + z * (v02 + z * (v03 + z * v04))); + return (u / v + tpi * (j064(x) * log64(x))); +} + +_MATH_ALIAS_d_d(y0) + +/* The asymptotic expansions of pzero is + * 1 - 9/128 s^2 + 11025/98304 s^4 - ..., where s = 1/x. + * For x >= 2, We approximate pzero by + * pzero(x) = 1 + (R/S) + * where R = pR0 + pR1*s^2 + pR2*s^4 + ... + pR5*s^10 + * S = 1 + pS0*s^2 + ... + pS4*s^10 + * and + * | pzero(x)-1-R/S | <= 2 ** ( -60.26) + */ +static const __float64 pR8[6] = { + /* for x in [inf, 8]=1/[0,0.125] */ + _F_64(0.00000000000000000000e+00), /* 0x00000000, 0x00000000 */ + _F_64(-7.03124999999900357484e-02), /* 0xBFB1FFFF, 0xFFFFFD32 */ + _F_64(-8.08167041275349795626e+00), /* 0xC02029D0, 0xB44FA779 */ + _F_64(-2.57063105679704847262e+02), /* 0xC0701102, 0x7B19E863 */ + _F_64(-2.48521641009428822144e+03), /* 0xC0A36A6E, 0xCD4DCAFC */ + _F_64(-5.25304380490729545272e+03), /* 0xC0B4850B, 0x36CC643D */ +}; +static const __float64 pS8[5] = { + _F_64(1.16534364619668181717e+02), /* 0x405D2233, 0x07A96751 */ + _F_64(3.83374475364121826715e+03), /* 0x40ADF37D, 0x50596938 */ + _F_64(4.05978572648472545552e+04), /* 0x40E3D2BB, 0x6EB6B05F */ + _F_64(1.16752972564375915681e+05), /* 0x40FC810F, 0x8F9FA9BD */ + _F_64(4.76277284146730962675e+04), /* 0x40E74177, 0x4F2C49DC */ +}; + +static const __float64 pR5[6] = { + /* for x in [8,4.5454]=1/[0.125,0.22001] */ + _F_64(-1.14125464691894502584e-11), /* 0xBDA918B1, 0x47E495CC */ + _F_64(-7.03124940873599280078e-02), /* 0xBFB1FFFF, 0xE69AFBC6 */ + _F_64(-4.15961064470587782438e+00), /* 0xC010A370, 0xF90C6BBF */ + _F_64(-6.76747652265167261021e+01), /* 0xC050EB2F, 0x5A7D1783 */ + _F_64(-3.31231299649172967747e+02), /* 0xC074B3B3, 0x6742CC63 */ + _F_64(-3.46433388365604912451e+02), /* 0xC075A6EF, 0x28A38BD7 */ +}; +static const __float64 pS5[5] = { + _F_64(6.07539382692300335975e+01), /* 0x404E6081, 0x0C98C5DE */ + _F_64(1.05125230595704579173e+03), /* 0x40906D02, 0x5C7E2864 */ + _F_64(5.97897094333855784498e+03), /* 0x40B75AF8, 0x8FBE1D60 */ + _F_64(9.62544514357774460223e+03), /* 0x40C2CCB8, 0xFA76FA38 */ + _F_64(2.40605815922939109441e+03), /* 0x40A2CC1D, 0xC70BE864 */ +}; + +static const __float64 pR3[6] = { + /* for x in [4.547,2.8571]=1/[0.2199,0.35001] */ + _F_64(-2.54704601771951915620e-09), /* 0xBE25E103, 0x6FE1AA86 */ + _F_64(-7.03119616381481654654e-02), /* 0xBFB1FFF6, 0xF7C0E24B */ + _F_64(-2.40903221549529611423e+00), /* 0xC00345B2, 0xAEA48074 */ + _F_64(-2.19659774734883086467e+01), /* 0xC035F74A, 0x4CB94E14 */ + _F_64(-5.80791704701737572236e+01), /* 0xC04D0A22, 0x420A1A45 */ + _F_64(-3.14479470594888503854e+01), /* 0xC03F72AC, 0xA892D80F */ +}; +static const __float64 pS3[5] = { + _F_64(3.58560338055209726349e+01), /* 0x4041ED92, 0x84077DD3 */ + _F_64(3.61513983050303863820e+02), /* 0x40769839, 0x464A7C0E */ + _F_64(1.19360783792111533330e+03), /* 0x4092A66E, 0x6D1061D6 */ + _F_64(1.12799679856907414432e+03), /* 0x40919FFC, 0xB8C39B7E */ + _F_64(1.73580930813335754692e+02), /* 0x4065B296, 0xFC379081 */ +}; + +static const __float64 pR2[6] = { + /* for x in [2.8570,2]=1/[0.3499,0.5] */ + _F_64(-8.87534333032526411254e-08), /* 0xBE77D316, 0xE927026D */ + _F_64(-7.03030995483624743247e-02), /* 0xBFB1FF62, 0x495E1E42 */ + _F_64(-1.45073846780952986357e+00), /* 0xBFF73639, 0x8A24A843 */ + _F_64(-7.63569613823527770791e+00), /* 0xC01E8AF3, 0xEDAFA7F3 */ + _F_64(-1.11931668860356747786e+01), /* 0xC02662E6, 0xC5246303 */ + _F_64(-3.23364579351335335033e+00), /* 0xC009DE81, 0xAF8FE70F */ +}; +static const __float64 pS2[5] = { + _F_64(2.22202997532088808441e+01), /* 0x40363865, 0x908B5959 */ + _F_64(1.36206794218215208048e+02), /* 0x4061069E, 0x0EE8878F */ + _F_64(2.70470278658083486789e+02), /* 0x4070E786, 0x42EA079B */ + _F_64(1.53875394208320329881e+02), /* 0x40633C03, 0x3AB6FAFF */ + _F_64(1.46576176948256193810e+01), /* 0x402D50B3, 0x44391809 */ +}; + +static __float64 +pzero(__float64 x) +{ + const __float64 *p, *q; + __float64 z, r, s; + __int32_t ix; + GET_HIGH_WORD(ix, x); + ix &= 0x7fffffff; + if (ix >= 0x41b00000) { + return one; + } else if (ix >= 0x40200000) { + p = pR8; + q = pS8; + } else if (ix >= 0x40122E8B) { + p = pR5; + q = pS5; + } else if (ix >= 0x4006DB6D) { + p = pR3; + q = pS3; + } else { + p = pR2; + q = pS2; + } + z = one / (x * x); + r = p[0] + z * (p[1] + z * (p[2] + z * (p[3] + z * (p[4] + z * p[5])))); + s = one + z * (q[0] + z * (q[1] + z * (q[2] + z * (q[3] + z * q[4])))); + return one + r / s; +} + +/* For x >= 8, the asymptotic expansions of qzero is + * -1/8 s + 75/1024 s^3 - ..., where s = 1/x. + * We approximate qzero by + * qzero(x) = s*(-1.25 + (R/S)) + * where R = qR0 + qR1*s^2 + qR2*s^4 + ... + qR5*s^10 + * S = 1 + qS0*s^2 + ... + qS5*s^12 + * and + * | qzero(x)/s +1.25-R/S | <= 2 ** ( -61.22) + */ +static const __float64 qR8[6] = { + /* for x in [inf, 8]=1/[0,0.125] */ + _F_64(0.00000000000000000000e+00), /* 0x00000000, 0x00000000 */ + _F_64(7.32421874999935051953e-02), /* 0x3FB2BFFF, 0xFFFFFE2C */ + _F_64(1.17682064682252693899e+01), /* 0x40278952, 0x5BB334D6 */ + _F_64(5.57673380256401856059e+02), /* 0x40816D63, 0x15301825 */ + _F_64(8.85919720756468632317e+03), /* 0x40C14D99, 0x3E18F46D */ + _F_64(3.70146267776887834771e+04), /* 0x40E212D4, 0x0E901566 */ +}; +static const __float64 qS8[6] = { + _F_64(1.63776026895689824414e+02), /* 0x406478D5, 0x365B39BC */ + _F_64(8.09834494656449805916e+03), /* 0x40BFA258, 0x4E6B0563 */ + _F_64(1.42538291419120476348e+05), /* 0x41016652, 0x54D38C3F */ + _F_64(8.03309257119514397345e+05), /* 0x412883DA, 0x83A52B43 */ + _F_64(8.40501579819060512818e+05), /* 0x4129A66B, 0x28DE0B3D */ + _F_64(-3.43899293537866615225e+05), /* 0xC114FD6D, 0x2C9530C5 */ +}; + +static const __float64 qR5[6] = { + /* for x in [8,4.5454]=1/[0.125,0.22001] */ + _F_64(1.84085963594515531381e-11), /* 0x3DB43D8F, 0x29CC8CD9 */ + _F_64(7.32421766612684765896e-02), /* 0x3FB2BFFF, 0xD172B04C */ + _F_64(5.83563508962056953777e+00), /* 0x401757B0, 0xB9953DD3 */ + _F_64(1.35111577286449829671e+02), /* 0x4060E392, 0x0A8788E9 */ + _F_64(1.02724376596164097464e+03), /* 0x40900CF9, 0x9DC8C481 */ + _F_64(1.98997785864605384631e+03), /* 0x409F17E9, 0x53C6E3A6 */ +}; +static const __float64 qS5[6] = { + _F_64(8.27766102236537761883e+01), /* 0x4054B1B3, 0xFB5E1543 */ + _F_64(2.07781416421392987104e+03), /* 0x40A03BA0, 0xDA21C0CE */ + _F_64(1.88472887785718085070e+04), /* 0x40D267D2, 0x7B591E6D */ + _F_64(5.67511122894947329769e+04), /* 0x40EBB5E3, 0x97E02372 */ + _F_64(3.59767538425114471465e+04), /* 0x40E19118, 0x1F7A54A0 */ + _F_64(-5.35434275601944773371e+03), /* 0xC0B4EA57, 0xBEDBC609 */ +}; + +static const __float64 qR3[6] = { + /* for x in [4.547,2.8571]=1/[0.2199,0.35001] */ + _F_64(4.37741014089738620906e-09), /* 0x3E32CD03, 0x6ADECB82 */ + _F_64(7.32411180042911447163e-02), /* 0x3FB2BFEE, 0x0E8D0842 */ + _F_64(3.34423137516170720929e+00), /* 0x400AC0FC, 0x61149CF5 */ + _F_64(4.26218440745412650017e+01), /* 0x40454F98, 0x962DAEDD */ + _F_64(1.70808091340565596283e+02), /* 0x406559DB, 0xE25EFD1F */ + _F_64(1.66733948696651168575e+02), /* 0x4064D77C, 0x81FA21E0 */ +}; +static const __float64 qS3[6] = { + _F_64(4.87588729724587182091e+01), /* 0x40486122, 0xBFE343A6 */ + _F_64(7.09689221056606015736e+02), /* 0x40862D83, 0x86544EB3 */ + _F_64(3.70414822620111362994e+03), /* 0x40ACF04B, 0xE44DFC63 */ + _F_64(6.46042516752568917582e+03), /* 0x40B93C6C, 0xD7C76A28 */ + _F_64(2.51633368920368957333e+03), /* 0x40A3A8AA, 0xD94FB1C0 */ + _F_64(-1.49247451836156386662e+02), /* 0xC062A7EB, 0x201CF40F */ +}; + +static const __float64 qR2[6] = { + /* for x in [2.8570,2]=1/[0.3499,0.5] */ + _F_64(1.50444444886983272379e-07), /* 0x3E84313B, 0x54F76BDB */ + _F_64(7.32234265963079278272e-02), /* 0x3FB2BEC5, 0x3E883E34 */ + _F_64(1.99819174093815998816e+00), /* 0x3FFFF897, 0xE727779C */ + _F_64(1.44956029347885735348e+01), /* 0x402CFDBF, 0xAAF96FE5 */ + _F_64(3.16662317504781540833e+01), /* 0x403FAA8E, 0x29FBDC4A */ + _F_64(1.62527075710929267416e+01), /* 0x403040B1, 0x71814BB4 */ +}; +static const __float64 qS2[6] = { + _F_64(3.03655848355219184498e+01), /* 0x403E5D96, 0xF7C07AED */ + _F_64(2.69348118608049844624e+02), /* 0x4070D591, 0xE4D14B40 */ + _F_64(8.44783757595320139444e+02), /* 0x408A6645, 0x22B3BF22 */ + _F_64(8.82935845112488550512e+02), /* 0x408B977C, 0x9C5CC214 */ + _F_64(2.12666388511798828631e+02), /* 0x406A9553, 0x0E001365 */ + _F_64(-5.31095493882666946917e+00), /* 0xC0153E6A, 0xF8B32931 */ +}; + +static __float64 +qzero(__float64 x) +{ + const __float64 *p, *q; + __float64 s, r, z; + __int32_t ix; + GET_HIGH_WORD(ix, x); + ix &= 0x7fffffff; + if (ix >= 0x41b00000) { + return _F_64(-.125) / x; + } else if (ix >= 0x40200000) { + p = qR8; + q = qS8; + } else if (ix >= 0x40122E8B) { + p = qR5; + q = qS5; + } else if (ix >= 0x4006DB6D) { + p = qR3; + q = qS3; + } else { + p = qR2; + q = qS2; + } + z = one / (x * x); + r = p[0] + z * (p[1] + z * (p[2] + z * (p[3] + z * (p[4] + z * p[5])))); + s = one + + z * (q[0] + + z * (q[1] + z * (q[2] + z * (q[3] + z * (q[4] + z * q[5]))))); + return (_F_64(-.125) + r / s) / x; +} + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_j1.c b/src/audio/ffmpeg_dec/libm/math/s_j1.c new file mode 100644 index 000000000000..175d20601b0a --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_j1.c @@ -0,0 +1,439 @@ + +/* @(#)e_j1.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* j1(x), y1(x) + * Bessel function of the first and second kinds of order zero. + * Method -- j1(x): + * 1. For tiny x, we use j1(x) = x/2 - x^3/16 + x^5/384 - ... + * 2. Reduce x to |x| since j1(x)=-j1(-x), and + * for x in (0,2) + * j1(x) = x/2 + x*z*R0/S0, where z = x*x; + * (precision: |j1/x - 1/2 - R0/S0 |<2**-61.51 ) + * for x in (2,inf) + * j1(x) = sqrt(2/(pi*x))*(p1(x)*cos(x1)-q1(x)*sin(x1)) + * y1(x) = sqrt(2/(pi*x))*(p1(x)*sin(x1)+q1(x)*cos(x1)) + * where x1 = x-3*pi/4. It is better to compute sin(x1),cos(x1) + * as follow: + * cos(x1) = cos(x)cos(3pi/4)+sin(x)sin(3pi/4) + * = 1/sqrt(2) * (sin(x) - cos(x)) + * sin(x1) = sin(x)cos(3pi/4)-cos(x)sin(3pi/4) + * = -1/sqrt(2) * (sin(x) + cos(x)) + * (To avoid cancellation, use + * sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x)) + * to compute the worse one.) + * + * 3 Special cases + * j1(nan)= nan + * j1(0) = 0 + * j1(inf) = 0 + * + * Method -- y1(x): + * 1. screen out x<=0 cases: y1(0)=-inf, y1(x<0)=NaN + * 2. For x<2. + * Since + * y1(x) = 2/pi*(j1(x)*(ln(x/2)+Euler)-1/x-x/2+5/64*x^3-...) + * therefore y1(x)-2/pi*j1(x)*ln(x)-1/x is an odd function. + * We use the following function to approximate y1, + * y1(x) = x*U(z)/V(z) + (2/pi)*(j1(x)*ln(x)-1/x), z= x^2 + * where for x in [0,2] (abs err less than 2**-65.89) + * U(z) = U0[0] + U0[1]*z + ... + U0[4]*z^4 + * V(z) = 1 + v0[0]*z + ... + v0[4]*z^5 + * Note: For tiny x, 1/x dominate y1 and hence + * y1(tiny) = -2/pi/tiny, (choose tiny<2**-54) + * 3. For x>=2. + * y1(x) = sqrt(2/(pi*x))*(p1(x)*sin(x1)+q1(x)*cos(x1)) + * where x1 = x-3*pi/4. It is better to compute sin(x1),cos(x1) + * by method mentioned above. + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +static __float64 pone(__float64); +static __float64 qone(__float64); + +static const __float64 + one = _F_64(1.0), + invsqrtpi = + _F_64(5.64189583547756279280e-01), /* 0x3FE20DD7, 0x50429B6D */ + tpi = _F_64(6.36619772367581382433e-01), /* 0x3FE45F30, 0x6DC9C883 */ + /* R0/S0 on [0,2] */ + r00 = _F_64(-6.25000000000000000000e-02), /* 0xBFB00000, 0x00000000 */ + r01 = _F_64(1.40705666955189706048e-03), /* 0x3F570D9F, 0x98472C61 */ + r02 = _F_64(-1.59955631084035597520e-05), /* 0xBEF0C5C6, 0xBA169668 */ + r03 = _F_64(4.96727999609584448412e-08), /* 0x3E6AAAFA, 0x46CA0BD9 */ + s01 = _F_64(1.91537599538363460805e-02), /* 0x3F939D0B, 0x12637E53 */ + s02 = _F_64(1.85946785588630915560e-04), /* 0x3F285F56, 0xB9CDF664 */ + s03 = _F_64(1.17718464042623683263e-06), /* 0x3EB3BFF8, 0x333F8498 */ + s04 = _F_64(5.04636257076217042715e-09), /* 0x3E35AC88, 0xC97DFF2C */ + s05 = _F_64(1.23542274426137913908e-11); /* 0x3DAB2ACF, 0xCFB97ED8 */ + +static const __float64 zero = _F_64(0.0); + +__float64 +j164(__float64 x) +{ + __float64 z, s, c, ss, cc, r, u, v, y; + __int32_t hx, ix, lx; + + if (isnan(x)) + return x + x; + + if (isinf(x)) + return _F_64(0.0); + + GET_HIGH_WORD(hx, x); + ix = hx & 0x7fffffff; + y = fabs64(x); + if (ix >= 0x40000000) { /* |x| >= 2.0 */ + s = sin64(y); + c = cos64(y); + ss = -s - c; + cc = s - c; + if (ix < 0x7fe00000) { /* make sure y+y not overflow */ + z = cos64(y + y); + if ((s * c) > zero) + cc = z / ss; + else + ss = z / cc; + } + /* + * j1(x) = 1/sqrt(pi) * (P(1,x)*cc - Q(1,x)*ss) / sqrt(x) + * y1(x) = 1/sqrt(pi) * (P(1,x)*ss + Q(1,x)*cc) / sqrt(x) + */ + if (ix > 0x48000000) + z = (invsqrtpi * cc) / sqrt64(y); + else { + u = pone(y); + v = qone(y); + z = invsqrtpi * (u * cc - v * ss) / sqrt64(y); + } + if (hx < 0) + return -z; + else + return z; + } + if (ix < 0x3e400000) { /* |x|<2**-27 */ + GET_LOW_WORD(lx, x); + if (ix == 0 && lx == 0) + return x; + return check_uflow(_F_64(0.5) * x); /* inexact if x!=0 necessary */ + } + z = x * x; + r = z * (r00 + z * (r01 + z * (r02 + z * r03))); + s = one + z * (s01 + z * (s02 + z * (s03 + z * (s04 + z * s05)))); + r *= x; + return (x * _F_64(0.5) + r / s); +} + +_MATH_ALIAS_d_d(j1) + +static const __float64 U0[5] = { + _F_64(-1.96057090646238940668e-01), /* 0xBFC91866, 0x143CBC8A */ + _F_64(5.04438716639811282616e-02), /* 0x3FA9D3C7, 0x76292CD1 */ + _F_64(-1.91256895875763547298e-03), /* 0xBF5F55E5, 0x4844F50F */ + _F_64(2.35252600561610495928e-05), /* 0x3EF8AB03, 0x8FA6B88E */ + _F_64(-9.19099158039878874504e-08), /* 0xBE78AC00, 0x569105B8 */ +}; +static const __float64 V0[5] = { + _F_64(1.99167318236649903973e-02), /* 0x3F94650D, 0x3F4DA9F0 */ + _F_64(2.02552581025135171496e-04), /* 0x3F2A8C89, 0x6C257764 */ + _F_64(1.35608801097516229404e-06), /* 0x3EB6C05A, 0x894E8CA6 */ + _F_64(6.22741452364621501295e-09), /* 0x3E3ABF1D, 0x5BA69A86 */ + _F_64(1.66559246207992079114e-11), /* 0x3DB25039, 0xDACA772A */ +}; + +__float64 +y164(__float64 x) +{ + __float64 z, s, c, ss, cc, u, v; + __int32_t hx, ix, lx; + + EXTRACT_WORDS(hx, lx, x); + ix = 0x7fffffff & hx; + /* if Y1(NaN) is NaN, Y1(-inf) is NaN, Y1(inf) is 0 */ + if ((ix | lx) == 0) + return __math_divzero(1); + + if (isnan(x)) + return x + x; + + if (hx < 0) + return __math_invalid(x); + + if (ix >= 0x7ff00000) + return _F_64(0.0); + + if (ix >= 0x40000000) { /* |x| >= 2.0 */ + s = sin64(x); + c = cos64(x); + ss = -s - c; + cc = s - c; + if (ix < 0x7fe00000) { /* make sure x+x not overflow */ + z = cos64(x + x); + if ((s * c) > zero) + cc = z / ss; + else + ss = z / cc; + } + /* y1(x) = sqrt(2/(pi*x))*(p1(x)*sin(x0)+q1(x)*cos(x0)) + * where x0 = x-3pi/4 + * Better formula: + * cos(x0) = cos(x)cos(3pi/4)+sin(x)sin(3pi/4) + * = 1/sqrt(2) * (sin(x) - cos(x)) + * sin(x0) = sin(x)cos(3pi/4)-cos(x)sin(3pi/4) + * = -1/sqrt(2) * (cos(x) + sin(x)) + * To avoid cancellation, use + * sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x)) + * to compute the worse one. + */ + if (ix > 0x48000000) + z = (invsqrtpi * ss) / sqrt64(x); + else { + u = pone(x); + v = qone(x); + z = invsqrtpi * (u * ss + v * cc) / sqrt64(x); + } + return z; + } + if (ix <= 0x3c900000) { /* x < 2**-54 */ + return check_oflow(-tpi / x); + } + z = x * x; + u = U0[0] + z * (U0[1] + z * (U0[2] + z * (U0[3] + z * U0[4]))); + v = one + z * (V0[0] + z * (V0[1] + z * (V0[2] + z * (V0[3] + z * V0[4])))); + return (x * (u / v) + tpi * (j1(x) * log(x) - one / x)); +} + +_MATH_ALIAS_d_d(y1) + +/* For x >= 8, the asymptotic expansions of pone is + * 1 + 15/128 s^2 - 4725/2^15 s^4 - ..., where s = 1/x. + * We approximate pone by + * pone(x) = 1 + (R/S) + * where R = pr0 + pr1*s^2 + pr2*s^4 + ... + pr5*s^10 + * S = 1 + ps0*s^2 + ... + ps4*s^10 + * and + * | pone(x)-1-R/S | <= 2 ** ( -60.06) + */ + +static const __float64 pr8[6] = { + /* for x in [inf, 8]=1/[0,0.125] */ + _F_64(0.00000000000000000000e+00), /* 0x00000000, 0x00000000 */ + _F_64(1.17187499999988647970e-01), /* 0x3FBDFFFF, 0xFFFFFCCE */ + _F_64(1.32394806593073575129e+01), /* 0x402A7A9D, 0x357F7FCE */ + _F_64(4.12051854307378562225e+02), /* 0x4079C0D4, 0x652EA590 */ + _F_64(3.87474538913960532227e+03), /* 0x40AE457D, 0xA3A532CC */ + _F_64(7.91447954031891731574e+03), /* 0x40BEEA7A, 0xC32782DD */ +}; +static const __float64 ps8[5] = { + _F_64(1.14207370375678408436e+02), /* 0x405C8D45, 0x8E656CAC */ + _F_64(3.65093083420853463394e+03), /* 0x40AC85DC, 0x964D274F */ + _F_64(3.69562060269033463555e+04), /* 0x40E20B86, 0x97C5BB7F */ + _F_64(9.76027935934950801311e+04), /* 0x40F7D42C, 0xB28F17BB */ + _F_64(3.08042720627888811578e+04), /* 0x40DE1511, 0x697A0B2D */ +}; + +static const __float64 pr5[6] = { + /* for x in [8,4.5454]=1/[0.125,0.22001] */ + _F_64(1.31990519556243522749e-11), /* 0x3DAD0667, 0xDAE1CA7D */ + _F_64(1.17187493190614097638e-01), /* 0x3FBDFFFF, 0xE2C10043 */ + _F_64(6.80275127868432871736e+00), /* 0x401B3604, 0x6E6315E3 */ + _F_64(1.08308182990189109773e+02), /* 0x405B13B9, 0x452602ED */ + _F_64(5.17636139533199752805e+02), /* 0x40802D16, 0xD052D649 */ + _F_64(5.28715201363337541807e+02), /* 0x408085B8, 0xBB7E0CB7 */ +}; +static const __float64 ps5[5] = { + _F_64(5.92805987221131331921e+01), /* 0x404DA3EA, 0xA8AF633D */ + _F_64(9.91401418733614377743e+02), /* 0x408EFB36, 0x1B066701 */ + _F_64(5.35326695291487976647e+03), /* 0x40B4E944, 0x5706B6FB */ + _F_64(7.84469031749551231769e+03), /* 0x40BEA4B0, 0xB8A5BB15 */ + _F_64(1.50404688810361062679e+03), /* 0x40978030, 0x036F5E51 */ +}; + +static const __float64 pr3[6] = { + _F_64(3.02503916137373618024e-09), /* 0x3E29FC21, 0xA7AD9EDD */ + _F_64(1.17186865567253592491e-01), /* 0x3FBDFFF5, 0x5B21D17B */ + _F_64(3.93297750033315640650e+00), /* 0x400F76BC, 0xE85EAD8A */ + _F_64(3.51194035591636932736e+01), /* 0x40418F48, 0x9DA6D129 */ + _F_64(9.10550110750781271918e+01), /* 0x4056C385, 0x4D2C1837 */ + _F_64(4.85590685197364919645e+01), /* 0x4048478F, 0x8EA83EE5 */ +}; +static const __float64 ps3[5] = { + _F_64(3.47913095001251519989e+01), /* 0x40416549, 0xA134069C */ + _F_64(3.36762458747825746741e+02), /* 0x40750C33, 0x07F1A75F */ + _F_64(1.04687139975775130551e+03), /* 0x40905B7C, 0x5037D523 */ + _F_64(8.90811346398256432622e+02), /* 0x408BD67D, 0xA32E31E9 */ + _F_64(1.03787932439639277504e+02), /* 0x4059F26D, 0x7C2EED53 */ +}; + +static const __float64 pr2[6] = { + /* for x in [2.8570,2]=1/[0.3499,0.5] */ + _F_64(1.07710830106873743082e-07), /* 0x3E7CE9D4, 0xF65544F4 */ + _F_64(1.17176219462683348094e-01), /* 0x3FBDFF42, 0xBE760D83 */ + _F_64(2.36851496667608785174e+00), /* 0x4002F2B7, 0xF98FAEC0 */ + _F_64(1.22426109148261232917e+01), /* 0x40287C37, 0x7F71A964 */ + _F_64(1.76939711271687727390e+01), /* 0x4031B1A8, 0x177F8EE2 */ + _F_64(5.07352312588818499250e+00), /* 0x40144B49, 0xA574C1FE */ +}; +static const __float64 ps2[5] = { + _F_64(2.14364859363821409488e+01), /* 0x40356FBD, 0x8AD5ECDC */ + _F_64(1.25290227168402751090e+02), /* 0x405F5293, 0x14F92CD5 */ + _F_64(2.32276469057162813669e+02), /* 0x406D08D8, 0xD5A2DBD9 */ + _F_64(1.17679373287147100768e+02), /* 0x405D6B7A, 0xDA1884A9 */ + _F_64(8.36463893371618283368e+00), /* 0x4020BAB1, 0xF44E5192 */ +}; + +static __float64 +pone(__float64 x) +{ + const __float64 *p, *q; + __float64 z, r, s; + __int32_t ix; + GET_HIGH_WORD(ix, x); + ix &= 0x7fffffff; + if (ix >= 0x41b00000) { + return one; + } else if (ix >= 0x40200000) { + p = pr8; + q = ps8; + } else if (ix >= 0x40122E8B) { + p = pr5; + q = ps5; + } else if (ix >= 0x4006DB6D) { + p = pr3; + q = ps3; + } else { + p = pr2; + q = ps2; + } + z = one / (x * x); + r = p[0] + z * (p[1] + z * (p[2] + z * (p[3] + z * (p[4] + z * p[5])))); + s = one + z * (q[0] + z * (q[1] + z * (q[2] + z * (q[3] + z * q[4])))); + return one + r / s; +} + +/* For x >= 8, the asymptotic expansions of qone is + * 3/8 s - 105/1024 s^3 - ..., where s = 1/x. + * We approximate qone by + * qone(x) = s*(0.375 + (R/S)) + * where R = qr1*s^2 + qr2*s^4 + ... + qr5*s^10 + * S = 1 + qs1*s^2 + ... + qs6*s^12 + * and + * | qone(x)/s -0.375-R/S | <= 2 ** ( -61.13) + */ + +static const __float64 qr8[6] = { + /* for x in [inf, 8]=1/[0,0.125] */ + _F_64(0.00000000000000000000e+00), /* 0x00000000, 0x00000000 */ + _F_64(-1.02539062499992714161e-01), /* 0xBFBA3FFF, 0xFFFFFDF3 */ + _F_64(-1.62717534544589987888e+01), /* 0xC0304591, 0xA26779F7 */ + _F_64(-7.59601722513950107896e+02), /* 0xC087BCD0, 0x53E4B576 */ + _F_64(-1.18498066702429587167e+04), /* 0xC0C724E7, 0x40F87415 */ + _F_64(-4.84385124285750353010e+04), /* 0xC0E7A6D0, 0x65D09C6A */ +}; +static const __float64 qs8[6] = { + _F_64(1.61395369700722909556e+02), /* 0x40642CA6, 0xDE5BCDE5 */ + _F_64(7.82538599923348465381e+03), /* 0x40BE9162, 0xD0D88419 */ + _F_64(1.33875336287249578163e+05), /* 0x4100579A, 0xB0B75E98 */ + _F_64(7.19657723683240939863e+05), /* 0x4125F653, 0x72869C19 */ + _F_64(6.66601232617776375264e+05), /* 0x412457D2, 0x7719AD5C */ + _F_64(-2.94490264303834643215e+05), /* 0xC111F969, 0x0EA5AA18 */ +}; + +static const __float64 qr5[6] = { + /* for x in [8,4.5454]=1/[0.125,0.22001] */ + _F_64(-2.08979931141764104297e-11), /* 0xBDB6FA43, 0x1AA1A098 */ + _F_64(-1.02539050241375426231e-01), /* 0xBFBA3FFF, 0xCB597FEF */ + _F_64(-8.05644828123936029840e+00), /* 0xC0201CE6, 0xCA03AD4B */ + _F_64(-1.83669607474888380239e+02), /* 0xC066F56D, 0x6CA7B9B0 */ + _F_64(-1.37319376065508163265e+03), /* 0xC09574C6, 0x6931734F */ + _F_64(-2.61244440453215656817e+03), /* 0xC0A468E3, 0x88FDA79D */ +}; +static const __float64 qs5[6] = { + _F_64(8.12765501384335777857e+01), /* 0x405451B2, 0xFF5A11B2 */ + _F_64(1.99179873460485964642e+03), /* 0x409F1F31, 0xE77BF839 */ + _F_64(1.74684851924908907677e+04), /* 0x40D10F1F, 0x0D64CE29 */ + _F_64(4.98514270910352279316e+04), /* 0x40E8576D, 0xAABAD197 */ + _F_64(2.79480751638918118260e+04), /* 0x40DB4B04, 0xCF7C364B */ + _F_64(-4.71918354795128470869e+03), /* 0xC0B26F2E, 0xFCFFA004 */ +}; + +static const __float64 qr3[6] = { + _F_64(-5.07831226461766561369e-09), /* 0xBE35CFA9, 0xD38FC84F */ + _F_64(-1.02537829820837089745e-01), /* 0xBFBA3FEB, 0x51AEED54 */ + _F_64(-4.61011581139473403113e+00), /* 0xC01270C2, 0x3302D9FF */ + _F_64(-5.78472216562783643212e+01), /* 0xC04CEC71, 0xC25D16DA */ + _F_64(-2.28244540737631695038e+02), /* 0xC06C87D3, 0x4718D55F */ + _F_64(-2.19210128478909325622e+02), /* 0xC06B66B9, 0x5F5C1BF6 */ +}; +static const __float64 qs3[6] = { + _F_64(4.76651550323729509273e+01), /* 0x4047D523, 0xCCD367E4 */ + _F_64(6.73865112676699709482e+02), /* 0x40850EEB, 0xC031EE3E */ + _F_64(3.38015286679526343505e+03), /* 0x40AA684E, 0x448E7C9A */ + _F_64(5.54772909720722782367e+03), /* 0x40B5ABBA, 0xA61D54A6 */ + _F_64(1.90311919338810798763e+03), /* 0x409DBC7A, 0x0DD4DF4B */ + _F_64(-1.35201191444307340817e+02), /* 0xC060E670, 0x290A311F */ +}; + +static const __float64 qr2[6] = { + /* for x in [2.8570,2]=1/[0.3499,0.5] */ + _F_64(-1.78381727510958865572e-07), /* 0xBE87F126, 0x44C626D2 */ + _F_64(-1.02517042607985553460e-01), /* 0xBFBA3E8E, 0x9148B010 */ + _F_64(-2.75220568278187460720e+00), /* 0xC0060484, 0x69BB4EDA */ + _F_64(-1.96636162643703720221e+01), /* 0xC033A9E2, 0xC168907F */ + _F_64(-4.23253133372830490089e+01), /* 0xC04529A3, 0xDE104AAA */ + _F_64(-2.13719211703704061733e+01), /* 0xC0355F36, 0x39CF6E52 */ +}; +static const __float64 qs2[6] = { + _F_64(2.95333629060523854548e+01), /* 0x403D888A, 0x78AE64FF */ + _F_64(2.52981549982190529136e+02), /* 0x406F9F68, 0xDB821CBA */ + _F_64(7.57502834868645436472e+02), /* 0x4087AC05, 0xCE49A0F7 */ + _F_64(7.39393205320467245656e+02), /* 0x40871B25, 0x48D4C029 */ + _F_64(1.55949003336666123687e+02), /* 0x40637E5E, 0x3C3ED8D4 */ + _F_64(-4.95949898822628210127e+00), /* 0xC013D686, 0xE71BE86B */ +}; + +static __float64 +qone(__float64 x) +{ + const __float64 *p, *q; + __float64 s, r, z; + __int32_t ix; + GET_HIGH_WORD(ix, x); + ix &= 0x7fffffff; + if (ix >= 0x41b00000) { + return .375 / x; + } else if (ix >= 0x40200000) { + p = qr8; + q = qs8; + } else if (ix >= 0x40122E8B) { + p = qr5; + q = qs5; + } else if (ix >= 0x4006DB6D) { + p = qr3; + q = qs3; + } else { + p = qr2; + q = qs2; + } + z = one / (x * x); + r = p[0] + z * (p[1] + z * (p[2] + z * (p[3] + z * (p[4] + z * p[5])))); + s = one + + z * (q[0] + + z * (q[1] + z * (q[2] + z * (q[3] + z * (q[4] + z * q[5]))))); + return (.375 + r / s) / x; +} + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_jn.c b/src/audio/ffmpeg_dec/libm/math/s_jn.c new file mode 100644 index 000000000000..ce84096e9f22 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_jn.c @@ -0,0 +1,319 @@ +/* @(#)e_jn.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * jn(n, x), yn(n, x) + * floating point Bessel's function of the 1st and 2nd kind + * of order n + * + * Special cases: + * y0(0)=y1(0)=yn(n,0) = -inf with division by zero signal; + * y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal. + * Note 2. About jn(n,x), yn(n,x) + * For n=0, j0(x) is called, + * for n=1, j1(x) is called, + * for nx, a continued fraction approximation to + * j(n,x)/j(n-1,x) is evaluated and then backward + * recursion is used starting from a supposed value + * for j(n,x). The resulting value of j(0,x) is + * compared with the actual value to correct the + * supposed value of j(n,x). + * + * yn(n,x) is similar in all respects, except + * that forward recursion is used for all + * values of n>1. + * + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +static const __float64 + invsqrtpi = _F_64(5.64189583547756279280e-01), /* 0x3FE20DD7, 0x50429B6D */ + two = _F_64(2.00000000000000000000e+00), /* 0x40000000, 0x00000000 */ + one = _F_64(1.00000000000000000000e+00); /* 0x3FF00000, 0x00000000 */ + +static const __float64 zero = _F_64(0.00000000000000000000e+00); + +__float64 +jn64(int n, __float64 x) +{ + __int32_t i, hx, ix, lx, sgn; + __float64 a, b, temp, di; + __float64 z, w; + + if (isnan(x)) + return x + x; + + if (isinf(x)) + return _F_64(0.0); + + /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x) + * Thus, J(-n,x) = J(n,-x) + */ + EXTRACT_WORDS(hx, lx, x); + ix = 0x7fffffff & hx; + + if (n < 0) { + n = -n; + x = -x; + hx ^= 0x80000000; + } + if (n == 0) + return (j064(x)); + if (n == 1) + return (j164(x)); + sgn = (n & 1) & (hx >> 31); /* even n -- 0, odd n -- sign(x) */ + x = fabs64(x); + if ((ix | lx) == 0 || ix >= 0x7ff00000) /* if x is 0 or inf */ + b = zero; + else if ((__float64)n <= x) { + /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */ + if (ix >= 0x52D00000) { /* x > 2**302 */ + /* (x >> n**2) + * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi) + * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi) + * Let s=sin(x), c=cos(x), + * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then + * + * n sin(xn)*sqt2 cos(xn)*sqt2 + * ---------------------------------- + * 0 s-c c+s + * 1 -s-c -c+s + * 2 -s+c -c-s + * 3 s+c c-s + */ + switch (n & 3) { + case 0: + default: + temp = cos64(x) + sin64(x); + break; + case 1: + temp = -cos64(x) + sin64(x); + break; + case 2: + temp = -cos64(x) - sin64(x); + break; + case 3: + temp = cos64(x) - sin64(x); + break; + } + b = invsqrtpi * temp / sqrt64(x); + } else { + a = j064(x); + b = j164(x); + for (i = 1; i < n; i++) { + temp = b; + b = b * ((__float64)(i + i) / x) - a; /* avoid underflow */ + a = temp; + } + } + } else { + if (ix < 0x3e100000) { /* x < 2**-29 */ + /* x is tiny, return the first Taylor expansion of J(n,x) + * J(n,x) = 1/n!*(x/2)^n - ... + */ + if (n > 33) /* underflow */ + b = zero; + else { + temp = x * _F_64(0.5); + b = temp; + for (a = one, i = 2; i <= n; i++) { + a *= (__float64)i; /* a = n! */ + b *= temp; /* b = (x/2)^n */ + } + b = b / a; + } + } else { + /* use backward recurrence */ + /* x x^2 x^2 + * J(n,x)/J(n-1,x) = ---- ------ ------ ..... + * 2n - 2(n+1) - 2(n+2) + * + * 1 1 1 + * (for large x) = ---- ------ ------ ..... + * 2n 2(n+1) 2(n+2) + * -- - ------ - ------ - + * x x x + * + * Let w = 2n/x and h=2/x, then the above quotient + * is equal to the continued fraction: + * 1 + * = ----------------------- + * 1 + * w - ----------------- + * 1 + * w+h - --------- + * w+2h - ... + * + * To determine how many terms needed, let + * Q(0) = w, Q(1) = w(w+h) - 1, + * Q(k) = (w+k*h)*Q(k-1) - Q(k-2), + * When Q(k) > 1e4 good for single + * When Q(k) > 1e9 good for double + * When Q(k) > 1e17 good for quadruple + */ + /* determine k */ + __float64 t, v; + __float64 q0, q1, h, tmp; + __int32_t k, m; + w = (n + n) / (__float64)x; + h = _F_64(2.0) / (__float64)x; + q0 = w; + z = w + h; + q1 = w * z - _F_64(1.0); + k = 1; + while (q1 < _F_64(1.0e9)) { + k += 1; + z += h; + tmp = z * q1 - q0; + q0 = q1; + q1 = tmp; + } + m = n + n; + for (t = zero, i = 2 * (n + k); i >= m; i -= 2) + t = one / (i / x - t); + a = t; + b = one; + /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n) + * Hence, if n*(log(2n/x)) > ... + * single 8.8722839355e+01 + * double 7.09782712893383973096e+02 + * long double 1.1356523406294143949491931077970765006170e+04 + * then recurrent value may overflow and the result is + * likely underflow to zero + */ + tmp = n; + v = two / x; + tmp = tmp * log(fabs64(v * tmp)); + if (tmp < _F_64(7.09782712893383973096e+02)) { + for (i = n - 1, di = (__float64)(i + i); i > 0; i--) { + temp = b; + b *= di; + b = b / x - a; + a = temp; + di -= two; + } + } else { + for (i = n - 1, di = (__float64)(i + i); i > 0; i--) { + temp = b; + b *= di; + b = b / x - a; + a = temp; + di -= two; + /* scale b to avoid spurious overflow */ + if (b > _F_64(1e100)) { + a /= b; + t /= b; + b = one; + } + } + } + b = (t * j064(x) / b); + } + } + if (sgn == 1) + return -b; + else + return b; +} + +_MATH_ALIAS_d_id(jn) + +__float64 +yn64(int n, __float64 x) +{ + __int32_t i, hx, ix, lx; + __int32_t sign; + __float64 a, b, temp; + + EXTRACT_WORDS(hx, lx, x); + ix = 0x7fffffff & hx; + /* if Y(n,NaN) is NaN */ + + if ((ix | lx) == 0) + return __math_divzero(1); + + if (isnan(x)) + return x + x; + + if (hx < 0) + return __math_invalid(x); + + if (ix == 0x7ff00000) + return _F_64(0.0); + + sign = 1; + if (n < 0) { + n = -n; + sign = 1 - ((n & 1) << 1); + } + if (n == 0) + return (y064(x)); + if (n == 1) + return (sign * y164(x)); + + if (ix >= 0x52D00000) { /* x > 2**302 */ + /* (x >> n**2) + * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi) + * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi) + * Let s=sin(x), c=cos(x), + * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then + * + * n sin(xn)*sqt2 cos(xn)*sqt2 + * ---------------------------------- + * 0 s-c c+s + * 1 -s-c -c+s + * 2 -s+c -c-s + * 3 s+c c-s + */ + switch (n & 3) { + case 0: + default: + temp = sin64(x) - cos64(x); + break; + case 1: + temp = -sin64(x) - cos64(x); + break; + case 2: + temp = -sin64(x) + cos64(x); + break; + case 3: + temp = sin64(x) + cos64(x); + break; + } + b = invsqrtpi * temp / sqrt64(x); + } else { + __uint32_t high; + a = y064(x); + b = y164(x); + /* quit if b is -inf */ + GET_HIGH_WORD(high, b); + for (i = 1; i < n && high != 0xfff00000; i++) { + temp = b; + b = ((__float64)(i + i) / x) * b - a; + GET_HIGH_WORD(high, b); + a = temp; + } + } + if (sign > 0) + return b; + else + return -b; +} + +_MATH_ALIAS_d_id(yn) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_lgamma.c b/src/audio/ffmpeg_dec/libm/math/s_lgamma.c new file mode 100644 index 000000000000..f469bc55958c --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_lgamma.c @@ -0,0 +1,65 @@ +/* + * SPDX-License-Identifier: BSD-3-Clause + * + * Copyright © 2020 Keith Packard + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * 3. Neither the name of the copyright holder nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +__float64 +lgamma64(__float64 x) +{ + return lgamma64_r(x, &__signgam); +} + +# ifdef __strong_reference +#ifdef __GNUCLIKE_PRAGMA_DIAGNOSTIC +#pragma GCC diagnostic ignored "-Wpragmas" +#pragma GCC diagnostic ignored "-Wunknown-warning-option" +#pragma GCC diagnostic ignored "-Wattribute-alias=" +#pragma GCC diagnostic ignored "-Wmissing-attributes" +#endif +__strong_reference(lgamma64, gamma64); +#else +__float64 +gamma64(__float64 x) +{ + return lgamma64(x); +} +#endif + +_MATH_ALIAS_d_d(lgamma) +_MATH_ALIAS_d_d(gamma) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_log.c b/src/audio/ffmpeg_dec/libm/math/s_log.c new file mode 100644 index 000000000000..756333a3162c --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_log.c @@ -0,0 +1,156 @@ + +/* @(#)e_log.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* log(x) + * Return the logrithm of x + * + * Method : + * 1. Argument Reduction: find k and f such that + * x = 2^k * (1+f), + * where sqrt(2)/2 < 1+f < sqrt(2) . + * + * 2. Approximation of log(1+f). + * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s) + * = 2s + 2/3 s**3 + 2/5 s**5 + ....., + * = 2s + s*R + * We use a special Reme algorithm on [0,0.1716] to generate + * a polynomial of degree 14 to approximate R The maximum error + * of this polynomial approximation is bounded by 2**-58.45. In + * other words, + * 2 4 6 8 10 12 14 + * R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s +Lg6*s +Lg7*s + * (the values of Lg1 to Lg7 are listed in the program) + * and + * | 2 14 | -58.45 + * | Lg1*s +...+Lg7*s - R(z) | <= 2 + * | | + * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2. + * In order to guarantee error in log below 1ulp, we compute log + * by + * log(1+f) = f - s*(f - R) (if f is not too large) + * log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy) + * + * 3. Finally, log(x) = k*ln2 + log(1+f). + * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo))) + * Here ln2 is split into two floating point number: + * ln2_hi + ln2_lo, + * where n*ln2_hi is always exact for |n| < 2000. + * + * Special cases: + * log(x) is NaN with signal if x < 0 (including -INF) ; + * log(+INF) is +INF; log(0) is -INF with signal; + * log(NaN) is that NaN with no signal. + * + * Accuracy: + * according to an error analysis, the error is always less than + * 1 ulp (unit in the last place). + * + * Constants: + * The hexadecimal values are the intended ones for the following + * constants. The decimal values may be used, provided that the + * compiler will convert from decimal to binary accurately enough + * to produce the hexadecimal values shown. + */ + +#include "fdlibm.h" +#if __OBSOLETE_MATH_DOUBLE + +#ifdef _NEED_FLOAT64 + +static const __float64 ln2_hi = _F_64(6.93147180369123816490e-01), /* 3fe62e42 fee00000 */ + ln2_lo = _F_64(1.90821492927058770002e-10), /* 3dea39ef 35793c76 */ + two54 = _F_64(1.80143985094819840000e+16), /* 43500000 00000000 */ + Lg1 = _F_64(6.666666666666735130e-01), /* 3FE55555 55555593 */ + Lg2 = _F_64(3.999999999940941908e-01), /* 3FD99999 9997FA04 */ + Lg3 = _F_64(2.857142874366239149e-01), /* 3FD24924 94229359 */ + Lg4 = _F_64(2.222219843214978396e-01), /* 3FCC71C5 1D8E78AF */ + Lg5 = _F_64(1.818357216161805012e-01), /* 3FC74664 96CB03DE */ + Lg6 = _F_64(1.531383769920937332e-01), /* 3FC39A09 D078C69F */ + Lg7 = _F_64(1.479819860511658591e-01); /* 3FC2F112 DF3E5244 */ + +static const __float64 zero = _F_64(0.0); + +__float64 +log64(__float64 x) +{ + __float64 hfsq, f, s, z, R, w, t1, t2, dk; + __int32_t k, hx, i, j; + __uint32_t lx; + + EXTRACT_WORDS(hx, lx, x); + + k = 0; + if (hx < 0x00100000) { /* x < 2**-1022 */ + if (((hx & 0x7fffffff) | lx) == 0) + return __math_divzero(1); /* log(+-0)=-inf */ + if (hx < 0) + return __math_invalid(x); /* log(-#) = NaN */ + k -= 54; + x *= two54; /* subnormal number, scale up x */ + GET_HIGH_WORD(hx, x); + } + if (hx >= 0x7ff00000) + return x + x; + k += (hx >> 20) - 1023; + hx &= 0x000fffff; + i = (hx + 0x95f64) & 0x100000; + SET_HIGH_WORD(x, hx | (i ^ 0x3ff00000)); /* normalize x or x/2 */ + k += (i >> 20); + f = x - _F_64(1.0); + if ((0x000fffff & (2 + hx)) < 3) { /* |f| < 2**-20 */ + if (f == zero) { + if (k == 0) + return zero; + else { + dk = (__float64)k; + return dk * ln2_hi + dk * ln2_lo; + } + } + R = f * f * (_F_64(0.5) - _F_64(0.33333333333333333) * f); + if (k == 0) + return f - R; + else { + dk = (__float64)k; + return dk * ln2_hi - ((R - dk * ln2_lo) - f); + } + } + s = f / (_F_64(2.0) + f); + dk = (__float64)k; + z = s * s; + i = hx - 0x6147a; + w = z * z; + j = 0x6b851 - hx; + t1 = w * (Lg2 + w * (Lg4 + w * Lg6)); + t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7))); + i |= j; + R = t2 + t1; + if (i > 0) { + hfsq = _F_64(0.5) * f * f; + if (k == 0) + return f - (hfsq - s * (hfsq + R)); + else + return dk * ln2_hi - ((hfsq - (s * (hfsq + R) + dk * ln2_lo)) - f); + } else { + if (k == 0) + return f - s * (f - R); + else + return dk * ln2_hi - ((s * (f - R) - dk * ln2_lo) - f); + } +} + +_MATH_ALIAS_d_d(log) + +#endif /* _NEED_FLOAT64 */ +#else +#include "../common/log.c" +#endif /*__OBSOLETE_MATH_DOUBLE */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_log10.c b/src/audio/ffmpeg_dec/libm/math/s_log10.c new file mode 100644 index 000000000000..fca86a94d85f --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_log10.c @@ -0,0 +1,89 @@ + +/* @(#)e_log10.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* log10(x) + * Return the base 10 logarithm of x + * + * Method : + * Let log10_2hi = leading 40 bits of log10(2) and + * log10_2lo = log10(2) - log10_2hi, + * ivln10 = 1/log(10) rounded. + * Then + * n = ilogb(x), + * if(n<0) n = n+1; + * x = scalbn(x,-n); + * log10(x) := n*log10_2hi + (n*log10_2lo + ivln10*log(x)) + * + * Note 1: + * To guarantee log10(10**n)=n, where 10**n is normal, the rounding + * mode must set to Round-to-Nearest. + * Note 2: + * [1/log(10)] rounded to 53 bits has error .198 ulps; + * log10 is monotonic at all binary break points. + * + * Special cases: + * log10(x) is NaN with signal if x < 0; + * log10(+INF) is +INF with no signal; log10(0) is -INF with signal; + * log10(NaN) is that NaN with no signal; + * log10(10**N) = N for N=0,1,...,22. + * + * Constants: + * The hexadecimal values are the intended ones for the following constants. + * The decimal values may be used, provided that the compiler will convert + * from decimal to binary accurately enough to produce the hexadecimal values + * shown. + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +static const __float64 + two54 = _F_64(1.80143985094819840000e+16), /* 0x43500000, 0x00000000 */ + ivln10 = _F_64(4.34294481903251816668e-01), /* 0x3FDBCB7B, 0x1526E50E */ + log10_2hi = _F_64(3.01029995663611771306e-01), /* 0x3FD34413, 0x509F6000 */ + log10_2lo = _F_64(3.69423907715893078616e-13); /* 0x3D59FEF3, 0x11F12B36 */ + +__float64 +log1064(__float64 x) +{ + __float64 y, z; + __int32_t i, k, hx; + __uint32_t lx; + + EXTRACT_WORDS(hx, lx, x); + + k = 0; + if (hx < 0x00100000) { /* x < 2**-1022 */ + if (((hx & 0x7fffffff) | lx) == 0) + return __math_divzero(1); /* log(+-0)=-inf */ + if (hx < 0) + return __math_invalid(x); /* log(-#) = NaN */ + k -= 54; + x *= two54; /* subnormal number, scale up x */ + GET_HIGH_WORD(hx, x); + } + if (hx >= 0x7ff00000) + return x + x; + k += (hx >> 20) - 1023; + i = ((__uint32_t)k & 0x80000000) >> 31; + hx = (hx & 0x000fffff) | ((0x3ff - i) << 20); + y = (__float64)(k + i); + SET_HIGH_WORD(x, hx); + z = y * log10_2lo + ivln10 * log(x); + return z + y * log10_2hi; +} + +_MATH_ALIAS_d_d(log10) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_pow.c b/src/audio/ffmpeg_dec/libm/math/s_pow.c new file mode 100644 index 000000000000..6bdd6259721d --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_pow.c @@ -0,0 +1,368 @@ + +/* @(#)e_pow.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* pow(x,y) return x**y + * + * n + * Method: Let x = 2 * (1+f) + * 1. Compute and return log2(x) in two pieces: + * log2(x) = w1 + w2, + * where w1 has 53-24 = 29 bit trailing zeros. + * 2. Perform y*log2(x) = n+y' by simulating multi-precision + * arithmetic, where |y'|<=0.5. + * 3. Return x**y = 2**n*exp(y'*log2) + * + * Special cases: + * 1. (anything) ** 0 is 1 + * 2. (anything) ** 1 is itself + * 3a. (anything) ** NAN is NAN except + * 3b. +1 ** NAN is 1 + * 4. NAN ** (anything except 0) is NAN + * 5. +-(|x| > 1) ** +INF is +INF + * 6. +-(|x| > 1) ** -INF is +0 + * 7. +-(|x| < 1) ** +INF is +0 + * 8. +-(|x| < 1) ** -INF is +INF + * 9. +-1 ** +-INF is 1 + * 10. +0 ** (+anything except 0, NAN) is +0 + * 11. -0 ** (+anything except 0, NAN, odd integer) is +0 + * 12. +0 ** (-anything except 0, NAN) is +INF + * 13. -0 ** (-anything except 0, NAN, odd integer) is +INF + * 14. -0 ** (odd integer) = -( +0 ** (odd integer) ) + * 15. +INF ** (+anything except 0,NAN) is +INF + * 16. +INF ** (-anything except 0,NAN) is +0 + * 17. -INF ** (anything) = -0 ** (-anything) + * 18. (-anything) ** (integer) is (-1)**(integer)*(+anything**integer) + * 19. (-anything except 0 and inf) ** (non-integer) is NAN + * + * Accuracy: + * pow(x,y) returns x**y nearly rounded. In particular + * pow(integer,integer) + * always returns the correct integer provided it is + * representable. + * + * Constants : + * The hexadecimal values are the intended ones for the following + * constants. The decimal values may be used, provided that the + * compiler will convert from decimal to binary accurately enough + * to produce the hexadecimal values shown. + */ + +#include "fdlibm.h" + +#if __OBSOLETE_MATH_DOUBLE + +#ifdef _NEED_FLOAT64 + +static const __float64 +bp[] = {_F_64(1.0), _F_64(1.5),}, +dp_h[] = { _F_64(0.0), _F_64(5.84962487220764160156e-01),}, /* 0x3FE2B803, 0x40000000 */ +dp_l[] = { _F_64(0.0), _F_64(1.35003920212974897128e-08),}, /* 0x3E4CFDEB, 0x43CFD006 */ +zero = _F_64(0.0), +one = _F_64(1.0), +two = _F_64(2.0), +two53 = _F_64(9007199254740992.0), /* 0x43400000, 0x00000000 */ + /* poly coefs for (3/2)*(log(x)-2s-2/3*s**3 */ +L1 = _F_64(5.99999999999994648725e-01), /* 0x3FE33333, 0x33333303 */ +L2 = _F_64(4.28571428578550184252e-01), /* 0x3FDB6DB6, 0xDB6FABFF */ +L3 = _F_64(3.33333329818377432918e-01), /* 0x3FD55555, 0x518F264D */ +L4 = _F_64(2.72728123808534006489e-01), /* 0x3FD17460, 0xA91D4101 */ +L5 = _F_64(2.30660745775561754067e-01), /* 0x3FCD864A, 0x93C9DB65 */ +L6 = _F_64(2.06975017800338417784e-01), /* 0x3FCA7E28, 0x4A454EEF */ +P1 = _F_64(1.66666666666666019037e-01), /* 0x3FC55555, 0x5555553E */ +P2 = _F_64(-2.77777777770155933842e-03), /* 0xBF66C16C, 0x16BEBD93 */ +P3 = _F_64(6.61375632143793436117e-05), /* 0x3F11566A, 0xAF25DE2C */ +P4 = _F_64(-1.65339022054652515390e-06), /* 0xBEBBBD41, 0xC5D26BF1 */ +P5 = _F_64(4.13813679705723846039e-08), /* 0x3E663769, 0x72BEA4D0 */ +lg2 = _F_64(6.93147180559945286227e-01), /* 0x3FE62E42, 0xFEFA39EF */ +lg2_h = _F_64(6.93147182464599609375e-01), /* 0x3FE62E43, 0x00000000 */ +lg2_l = _F_64(-1.90465429995776804525e-09), /* 0xBE205C61, 0x0CA86C39 */ +ovt = _F_64(8.0085662595372944372e-0017), /* -(1024-log2(ovfl+.5ulp)) */ +cp = _F_64(9.61796693925975554329e-01), /* 0x3FEEC709, 0xDC3A03FD =2/(3ln2) */ +cp_h = _F_64(9.61796700954437255859e-01), /* 0x3FEEC709, 0xE0000000 =(float)cp */ +cp_l = _F_64(-7.02846165095275826516e-09), /* 0xBE3E2FE0, 0x145B01F5 =tail of cp_h*/ +ivln2 = _F_64(1.44269504088896338700e+00), /* 0x3FF71547, 0x652B82FE =1/ln2 */ +ivln2_h = _F_64(1.44269502162933349609e+00), /* 0x3FF71547, 0x60000000 =24b 1/ln2*/ +ivln2_l = _F_64(1.92596299112661746887e-08); /* 0x3E54AE0B, 0xF85DDF44 =1/ln2 tail*/ + +__float64 +pow64(__float64 x, __float64 y) +{ + __float64 z, ax, z_h, z_l, p_h, p_l; + __float64 y1, t1, t2, r, s, t, u, v, w; + __int32_t i, j, k, yisint, n; + __int32_t hx, hy, ix, iy; + __uint32_t lx, ly; + + EXTRACT_WORDS(hx, lx, x); + EXTRACT_WORDS(hy, ly, y); + ix = hx & 0x7fffffff; + iy = hy & 0x7fffffff; + + /* y==zero: x**0 = 1 unless x is snan */ + if ((iy | ly) == 0) { + if (issignaling64_inline(x)) + return x + y; + return one; + } + + /* x|y==NaN return NaN unless x==1 then return 1 */ + if (ix > 0x7ff00000 || ((ix == 0x7ff00000) && (lx != 0)) || + iy > 0x7ff00000 || ((iy == 0x7ff00000) && (ly != 0))) { + if ((((__uint32_t) hx - 0x3ff00000) | lx) == 0 && !issignaling64_inline(y)) + return one; + else + return x + y; + } + + /* determine if y is an odd int when x < 0 + * yisint = 0 ... y is not an integer + * yisint = 1 ... y is an odd int + * yisint = 2 ... y is an even int + */ + yisint = 0; + if (hx < 0) { + if (iy >= 0x43400000) + yisint = 2; /* even integer y */ + else if (iy >= 0x3ff00000) { + k = (iy >> 20) - 0x3ff; /* exponent */ + if (k > 20) { + __uint32_t uj = ly >> (52 - k); + if ((uj << (52 - k)) == ly) + yisint = 2 - (uj & 1); + } else if (ly == 0) { + j = iy >> (20 - k); + if ((j << (20 - k)) == iy) + yisint = 2 - (j & 1); + } + } + } + + /* special value of y */ + if (ly == 0) { + if (iy == 0x7ff00000) { /* y is +-inf */ + if ((((__uint32_t) ix - 0x3ff00000) | lx) == 0) + return one; /* +-1**+-inf = 1 */ + else if (ix >= 0x3ff00000) /* (|x|>1)**+-inf = inf,0 */ + return (hy >= 0) ? y : zero; + else /* (|x|<1)**-,+inf = inf,0 */ + return (hy < 0) ? -y : zero; + } + if (iy == 0x3ff00000) { /* y is +-1 */ + if (hy < 0) { + if (x == 0) + return __math_divzero(hx < 0); + return one / x; + } else + return x; + } + if (hy == 0x40000000 && ix < 0x5ff00000 && ix > 0x1e500000) + return x * x; /* y is 2 */ + if (hy == 0x3fe00000) { /* y is 0.5 */ + if (hx >= 0) /* x >= +0 */ + return sqrt(x); + } + } + + ax = fabs64(x); + /* special value of x */ + if (lx == 0) { + if (ix == 0x7ff00000 || ix == 0x3ff00000) { + z = ax; /*x is +-inf,+-1*/ + if (hy < 0) + z = one / z; /* z = (1/|x|) */ + if (hx < 0) { + if ((((__uint32_t) ix - 0x3ff00000) | yisint) == 0) { + return __math_invalid(x); /* (-1)**non-int is NaN */ + } else if (yisint == 1) + z = -z; /* (x<0)**odd = -(|x|**odd) */ + } + return z; + } + + if (ix == 0) { + if (hy < 0) + return __math_divzero(hx < 0 && yisint == 1); + if (yisint != 1) + x = ax; + return x; + } + } + + /* (x<0)**(non-int) is NaN */ + /* REDHAT LOCAL: This used to be + if((((hx>>31)+1)|yisint)==0) return __math_invalid(x); + but ANSI C says a right shift of a signed negative quantity is + implementation defined. */ + if (((((__uint32_t)hx >> 31) - 1) | yisint) == 0) + return __math_invalid(x); + + /* |y| is huge */ + if (iy > 0x42000000) { /* if |y| > ~2**33 */ + if (iy > 0x43f00000) { /* if |y| > ~2**64, must o/uflow */ + if (ix <= 0x3fefffff) + return (hy < 0) ? __math_oflow(0) : __math_uflow(0); + else + return (hy > 0) ? __math_oflow(0) : __math_uflow(0); + } + /* over/underflow if x is not close to one */ + if (ix < 0x3fefffff) { + int sign = yisint & ((__uint32_t)hx>>31); + return (hy < 0) ? __math_oflow(sign) : __math_uflow(sign); + } + if (ix > 0x3ff00000) { + int sign = yisint & ((__uint32_t)hx>>31); + return (hy > 0) ? __math_oflow(sign) : __math_uflow(sign); + } + /* now |1-x| is tiny <= 2**-20, suffice to compute + log(x) by x-x^2/2+x^3/3-x^4/4 */ + t = ax - 1; /* t has 20 trailing zeros */ + w = (t * t) * (_F_64(0.5) - t * (_F_64(0.3333333333333333333333) - t * _F_64(0.25))); + u = ivln2_h * t; /* ivln2_h has 21 sig. bits */ + v = t * ivln2_l - w * ivln2; + t1 = u + v; + SET_LOW_WORD(t1, 0); + t2 = v - (t1 - u); + } else { + __float64 s2, s_h, s_l, t_h, t_l; + n = 0; + /* take care subnormal number */ + if (ix < 0x00100000) { + ax *= two53; + n -= 53; + GET_HIGH_WORD(ix, ax); + } + n += ((ix) >> 20) - 0x3ff; + j = ix & 0x000fffff; + /* determine interval */ + ix = j | 0x3ff00000; /* normalize ix */ + if (j <= 0x3988E) + k = 0; /* |x|> 1) | 0x20000000) + 0x00080000 + (k << 18)); + t_l = ax - (t_h - bp[k]); + s_l = v * ((u - s_h * t_h) - s_h * t_l); + /* compute log(ax) */ + s2 = s * s; + r = s2 * s2 * + (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6))))); + r += s_l * (s_h + s); + s2 = s_h * s_h; + t_h = _F_64(3.0) + s2 + r; + SET_LOW_WORD(t_h, 0); + t_l = r - ((t_h - _F_64(3.0)) - s2); + /* u+v = s*(1+...) */ + u = s_h * t_h; + v = s_l * t_h + t_l * s; + /* 2/(3log2)*(s+...) */ + p_h = u + v; + SET_LOW_WORD(p_h, 0); + p_l = v - (p_h - u); + z_h = cp_h * p_h; /* cp_h+cp_l = 2/(3*log2) */ + z_l = cp_l * p_h + p_l * cp + dp_l[k]; + /* log2(ax) = (s+..)*2/(3*log2) = n + dp_h + z_h + z_l */ + t = (__float64)n; + t1 = (((z_h + z_l) + dp_h[k]) + t); + SET_LOW_WORD(t1, 0); + t2 = z_l - (((t1 - t) - dp_h[k]) - z_h); + } + + s = one; /* s (sign of result -ve**odd) = -1 else = 1 */ + if (((((__uint32_t)hx >> 31) - 1) | (yisint - 1)) == 0) + s = -one; /* (-ve)**(odd int) */ + + /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */ + y1 = y; + SET_LOW_WORD(y1, 0); + p_l = (y - y1) * t1 + y * t2; + p_h = y1 * t1; + z = p_l + p_h; + EXTRACT_WORDS(j, i, z); + if (j >= 0x40900000) { /* z >= 1024 */ + if (((j - 0x40900000) | i) != 0) /* if z > 1024 */ + return __math_oflow(s < 0); /* overflow */ + else { + if (p_l + ovt > z - p_h) + return __math_oflow(s < 0); /* overflow */ + } + } else if ((j & 0x7fffffff) >= 0x4090cc00) { /* z <= -1075 */ + if (((j - 0xc090cc00) | i) != 0) /* z < -1075 */ + return __math_uflow(s < 0); /* underflow */ + else { + if (p_l <= z - p_h) + return __math_uflow(s < 0); /* underflow */ + } + } + /* + * compute 2**(p_h+p_l) + */ + i = j & 0x7fffffff; + k = (i >> 20) - 0x3ff; + n = 0; + if (i > 0x3fe00000) { /* if |z| > 0.5, set n = [z+0.5] */ + n = j + (0x00100000 >> (k + 1)); + k = ((n & 0x7fffffff) >> 20) - 0x3ff; /* new k for n */ + t = zero; + SET_HIGH_WORD(t, n & ~(0x000fffff >> k)); + n = ((n & 0x000fffff) | 0x00100000) >> (20 - k); + if (j < 0) + n = -n; + p_h -= t; + } + t = p_l + p_h; + SET_LOW_WORD(t, 0); + u = t * lg2_h; + v = (p_l - (t - p_h)) * lg2 + t * lg2_l; + z = u + v; + w = v - (z - u); + t = z * z; + t1 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5)))); + r = (z * t1) / (t1 - two) - (w + z * w); + z = one - (r - z); + GET_HIGH_WORD(j, z); + j += lsl(n, 20); + if (asr(j, 20) <= 0) + z = scalbn(z, (int)n); /* subnormal output */ + else + SET_HIGH_WORD(z, j); + return s * z; +} + +#ifdef __strong_reference +#if defined(__GNUCLIKE_PRAGMA_DIAGNOSTIC) && !defined(__clang__) +#pragma GCC diagnostic ignored "-Wmissing-attributes" +#endif +__strong_reference(pow64, _pow64); +#endif + +_MATH_ALIAS_d_dd(pow) + +#endif /* _NEED_FLOAT64 */ +#else +#include "../common/pow.c" +#endif /* __OBSOLETE_MATH_DOUBLE */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_rem_pio2.c b/src/audio/ffmpeg_dec/libm/math/s_rem_pio2.c new file mode 100644 index 000000000000..0708e2afd246 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_rem_pio2.c @@ -0,0 +1,182 @@ + +/* @(#)e_rem_pio2.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + * + */ + +/* __rem_pio2(x,y) + * + * return the remainder of x rem pi/2 in y[0]+y[1] + * use __kernel_rem_pio2() + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +/* + * Table of constants for 2/pi, 396 Hex digits (476 decimal) of 2/pi + */ +static const __int32_t two_over_pi[] = { + 0xA2F983, 0x6E4E44, 0x1529FC, 0x2757D1, 0xF534DD, 0xC0DB62, 0x95993C, + 0x439041, 0xFE5163, 0xABDEBB, 0xC561B7, 0x246E3A, 0x424DD2, 0xE00649, + 0x2EEA09, 0xD1921C, 0xFE1DEB, 0x1CB129, 0xA73EE8, 0x8235F5, 0x2EBB44, + 0x84E99C, 0x7026B4, 0x5F7E41, 0x3991D6, 0x398353, 0x39F49C, 0x845F8B, + 0xBDF928, 0x3B1FF8, 0x97FFDE, 0x05980F, 0xEF2F11, 0x8B5A0A, 0x6D1F6D, + 0x367ECF, 0x27CB09, 0xB74F46, 0x3F669E, 0x5FEA2D, 0x7527BA, 0xC7EBE5, + 0xF17B3D, 0x0739F7, 0x8A5292, 0xEA6BFB, 0x5FB11F, 0x8D5D08, 0x560330, + 0x46FC7B, 0x6BABF0, 0xCFBC20, 0x9AF436, 0x1DA9E3, 0x91615E, 0xE61B08, + 0x659985, 0x5F14A0, 0x68408D, 0xFFD880, 0x4D7327, 0x310606, 0x1556CA, + 0x73A8C9, 0x60E27B, 0xC08C6B, +}; + +static const __int32_t npio2_hw[] = { + 0x3FF921FB, 0x400921FB, 0x4012D97C, 0x401921FB, 0x401F6A7A, 0x4022D97C, + 0x4025FDBB, 0x402921FB, 0x402C463A, 0x402F6A7A, 0x4031475C, 0x4032D97C, + 0x40346B9C, 0x4035FDBB, 0x40378FDB, 0x403921FB, 0x403AB41B, 0x403C463A, + 0x403DD85A, 0x403F6A7A, 0x40407E4C, 0x4041475C, 0x4042106C, 0x4042D97C, + 0x4043A28C, 0x40446B9C, 0x404534AC, 0x4045FDBB, 0x4046C6CB, 0x40478FDB, + 0x404858EB, 0x404921FB, +}; + +/* + * invpio2: 53 bits of 2/pi + * pio2_1: first 33 bit of pi/2 + * pio2_1t: pi/2 - pio2_1 + * pio2_2: second 33 bit of pi/2 + * pio2_2t: pi/2 - (pio2_1+pio2_2) + * pio2_3: third 33 bit of pi/2 + * pio2_3t: pi/2 - (pio2_1+pio2_2+pio2_3) + */ + +static const __float64 + zero = _F_64(0.00000000000000000000e+00), /* 0x00000000, 0x00000000 */ + half = _F_64(5.00000000000000000000e-01), /* 0x3FE00000, 0x00000000 */ + two24 = _F_64(1.67772160000000000000e+07), /* 0x41700000, 0x00000000 */ + invpio2 = _F_64(6.36619772367581382433e-01), /* 0x3FE45F30, 0x6DC9C883 */ + pio2_1 = _F_64(1.57079632673412561417e+00), /* 0x3FF921FB, 0x54400000 */ + pio2_1t = _F_64(6.07710050650619224932e-11), /* 0x3DD0B461, 0x1A626331 */ + pio2_2 = _F_64(6.07710050630396597660e-11), /* 0x3DD0B461, 0x1A600000 */ + pio2_2t = _F_64(2.02226624879595063154e-21), /* 0x3BA3198A, 0x2E037073 */ + pio2_3 = _F_64(2.02226624871116645580e-21), /* 0x3BA3198A, 0x2E000000 */ + pio2_3t = _F_64(8.47842766036889956997e-32); /* 0x397B839A, 0x252049C1 */ + +__int32_t +__rem_pio2(__float64 x, __float64 *y) +{ + __float64 z = _F_64(0.0), w, t, r, fn; + __float64 tx[3]; + __int32_t i, j, n, ix, hx; + int e0, nx; + __uint32_t low; + + GET_HIGH_WORD(hx, x); /* high word of x */ + ix = hx & 0x7fffffff; + if (ix <= 0x3fe921fb) /* |x| ~<= pi/4 , no need for reduction */ + { + y[0] = x; + y[1] = 0; + return 0; + } + if (ix < 0x4002d97c) { /* |x| < 3pi/4, special case with n=+-1 */ + if (hx > 0) { + z = x - pio2_1; + if (ix != 0x3ff921fb) { /* 33+53 bit pi is good enough */ + y[0] = z - pio2_1t; + y[1] = (z - y[0]) - pio2_1t; + } else { /* near pi/2, use 33+33+53 bit pi */ + z -= pio2_2; + y[0] = z - pio2_2t; + y[1] = (z - y[0]) - pio2_2t; + } + return 1; + } else { /* negative x */ + z = x + pio2_1; + if (ix != 0x3ff921fb) { /* 33+53 bit pi is good enough */ + y[0] = z + pio2_1t; + y[1] = (z - y[0]) + pio2_1t; + } else { /* near pi/2, use 33+33+53 bit pi */ + z += pio2_2; + y[0] = z + pio2_2t; + y[1] = (z - y[0]) + pio2_2t; + } + return -1; + } + } + if (ix <= 0x413921fb) { /* |x| ~<= 2^19*(pi/2), medium size */ + t = _NAME_64(fabs)(x); + n = (__int32_t)(t * invpio2 + half); + fn = (__float64)n; + r = t - fn * pio2_1; + w = fn * pio2_1t; /* 1st round good to 85 bit */ + if (n < 32 && ix != npio2_hw[n - 1]) { + y[0] = r - w; /* quick check no cancellation */ + } else { + __uint32_t high; + j = ix >> 20; + y[0] = r - w; + GET_HIGH_WORD(high, y[0]); + i = j - ((high >> 20) & 0x7ff); + if (i > 16) { /* 2nd iteration needed, good to 118 */ + t = r; + w = fn * pio2_2; + r = t - w; + w = fn * pio2_2t - ((t - r) - w); + y[0] = r - w; + GET_HIGH_WORD(high, y[0]); + i = j - ((high >> 20) & 0x7ff); + if (i > 49) { /* 3rd iteration need, 151 bits acc */ + t = r; /* will cover all possible cases */ + w = fn * pio2_3; + r = t - w; + w = fn * pio2_3t - ((t - r) - w); + y[0] = r - w; + } + } + } + y[1] = (r - y[0]) - w; + if (hx < 0) { + y[0] = -y[0]; + y[1] = -y[1]; + return -n; + } else + return n; + } + /* + * all other (large) arguments + */ + if (ix >= 0x7ff00000) { /* x is inf or NaN */ + y[0] = y[1] = x - x; + return 0; + } + /* set z = scalbn(|x|,ilogb(x)-23) */ + GET_LOW_WORD(low, x); + SET_LOW_WORD(z, low); + e0 = (int)((ix >> 20) - 1046); /* e0 = ilogb(z)-23; */ + SET_HIGH_WORD(z, ix - lsl((__int32_t)e0, 20)); + for (i = 0; i < 2; i++) { + tx[i] = (__float64)((__int32_t)(z)); + z = (z - tx[i]) * two24; + } + tx[2] = z; + nx = 3; + while (tx[nx - 1] == zero) + nx--; /* skip zero term */ + n = __kernel_rem_pio2(tx, y, e0, nx, 2, two_over_pi); + if (hx < 0) { + y[0] = -y[0]; + y[1] = -y[1]; + return -n; + } + return n; +} + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_remainder.c b/src/audio/ffmpeg_dec/libm/math/s_remainder.c new file mode 100644 index 000000000000..b1424bae7c24 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_remainder.c @@ -0,0 +1,80 @@ + +/* @(#)e_remainder.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* remainder(x,p) + * Return : + * returns x REM p = x - [x/p]*p as if in infinite + * precise arithmetic, where [x/p] is the (infinite bit) + * integer nearest x/p (in half way case choose the even one). + * Method : + * Based on fmod() return x-[x/p]chopped*p exactlp. + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +static const __float64 zero = _F_64(0.0); + +__float64 +remainder64(__float64 x, __float64 p) +{ + __int32_t hx, hp; + __uint32_t sx, lx, lp; + __float64 p_half; + + EXTRACT_WORDS(hx, lx, x); + EXTRACT_WORDS(hp, lp, p); + sx = hx & 0x80000000; + hp &= 0x7fffffff; + hx &= 0x7fffffff; + + /* purge off exception values */ + if (isnan(x) || isnan(p)) + return x + p; + + if (isinf(x) || (hp | lp) == 0) + return __math_invalid(x); + + if ((hp | lp) == 0) + return (x * p) / (x * p); /* p = 0 */ + + if (hp <= 0x7fdfffff) + x = fmod(x, p + p); /* now x < 2p */ + + if (((hx - hp) | (lx - lp)) == 0) + return zero * x; + x = fabs64(x); + p = fabs64(p); + if (hp < 0x00200000) { + if (x + x > p) { + x -= p; + if (x + x >= p) + x -= p; + } + } else { + p_half = _F_64(0.5) * p; + if (x > p_half) { + x -= p; + if (x >= p_half) + x -= p; + } + } + GET_HIGH_WORD(hx, x); + SET_HIGH_WORD(x, hx ^ sx); + return x; +} + +_MATH_ALIAS_d_dd(remainder) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_scalb.c b/src/audio/ffmpeg_dec/libm/math/s_scalb.c new file mode 100644 index 000000000000..b92905f57b79 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_scalb.c @@ -0,0 +1,53 @@ + +/* @(#)e_scalb.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * scalb(x, fn) is provide for + * passing various standard test suite. One + * should use scalbn() instead. + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +__float64 +scalb64(__float64 x, __float64 fn) +{ + if (isnan(fn) || isnan(x)) + return x + fn; + + if (isinf(fn)) { + if ((x == 0.0 && fn > 0.0) || (isinf(x) && fn < 0.0)) + return __math_invalid(fn); + if (fn > 0.0) + return fn*x; + else + return x/(-fn); + } + + if (rint64(fn) != fn) + return __math_invalid(fn); + + if (fn > 4 * _FLOAT64_MAX_EXP) + fn = 4 * _FLOAT64_MAX_EXP; + + if (fn < -4 * _FLOAT64_MAX_EXP) + fn = -4 * _FLOAT64_MAX_EXP; + + return scalbn64(x, (int)fn); +} + +_MATH_ALIAS_d_dd(scalb) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_signif.c b/src/audio/ffmpeg_dec/libm/math/s_signif.c new file mode 100644 index 000000000000..ca3643b9f7f2 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_signif.c @@ -0,0 +1,32 @@ + +/* @(#)s_signif.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * significand(x) computes just + * scalb(x, (double) -ilogb(x)), + * for exercising the fraction-part(F) IEEE 754-1985 test vector. + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +__float64 +significand64(__float64 x) +{ + return scalbn64(x, -ilogb64(x)); +} + +_MATH_ALIAS_d_d(significand) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_sin.c b/src/audio/ffmpeg_dec/libm/math/s_sin.c new file mode 100644 index 000000000000..ca5eab4e0851 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_sin.c @@ -0,0 +1,130 @@ + +/* @(#)s_sin.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* +FUNCTION + <>, <>, <>, <>---sine or cosine +INDEX +sin +INDEX +sinf +INDEX +cos +INDEX +cosf +SYNOPSIS + #include + double sin(double <[x]>); + float sinf(float <[x]>); + double cos(double <[x]>); + float cosf(float <[x]>); + +DESCRIPTION + <> and <> compute (respectively) the sine and cosine + of the argument <[x]>. Angles are specified in radians. + + <> and <> are identical, save that they take and + return <> values. + + +RETURNS + The sine or cosine of <[x]> is returned. + +PORTABILITY + <> and <> are ANSI C. + <> and <> are extensions. + +QUICKREF + sin ansi pure + sinf - pure +*/ + +/* sin(x) + * Return sine function of x. + * + * kernel function: + * __kernel_sin ... sine function on [-pi/4,pi/4] + * __kernel_cos ... cose function on [-pi/4,pi/4] + * __rem_pio2 ... argument reduction routine + * + * Method. + * Let S,C and T denote the sin, cos and tan respectively on + * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2 + * in [-pi/4 , +pi/4], and let n = k mod 4. + * We have + * + * n sin(x) cos(x) tan(x) + * ---------------------------------------------------------- + * 0 S C T + * 1 C -S -1/T + * 2 -S -C T + * 3 -C S -1/T + * ---------------------------------------------------------- + * + * Special cases: + * Let trig be any of sin, cos, or tan. + * trig(+-INF) is NaN, with signals; + * trig(NaN) is that NaN; + * + * Accuracy: + * TRIG(x) returns trig(x) nearly rounded + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +__float64 +sin64(__float64 x) +{ + __float64 y[2], z = _F_64(0.0); + __int32_t n, ix; + + /* High word of x. */ + GET_HIGH_WORD(ix, x); + + /* |x| ~< pi/4 */ + ix &= 0x7fffffff; + if (ix <= 0x3fe921fb) + return __kernel_sin(x, z, 0); + + /* sin(Inf or NaN) is NaN */ + else if (ix >= 0x7ff00000) + return __math_invalid(x); + + /* argument reduction needed */ + else { + n = __rem_pio2(x, y); + switch (n & 3) { + case 0: + return __kernel_sin(y[0], y[1], 1); + case 1: + return __kernel_cos(y[0], y[1]); + case 2: + return -__kernel_sin(y[0], y[1], 1); + default: + return -__kernel_cos(y[0], y[1]); + } + } +} + +#ifdef __strong_reference +#if defined(__GNUCLIKE_PRAGMA_DIAGNOSTIC) && !defined(__clang__) +#pragma GCC diagnostic ignored "-Wmissing-attributes" +#endif +__strong_reference(_NAME_64(sin), _NAME_64(_sin)); +#endif + +_MATH_ALIAS_d_d(sin) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_sincos.c b/src/audio/ffmpeg_dec/libm/math/s_sincos.c new file mode 100644 index 000000000000..7e25b654539e --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_sincos.c @@ -0,0 +1,21 @@ +/* Copyright (c) 2002 Thomas Fitzsimmons */ +/* sincos -- currently no more efficient than two separate calls to + sin and cos. */ + +#define _GNU_SOURCE +#include "fdlibm.h" +#include +#include + +#ifdef _NEED_FLOAT64 + +void __no_builtin +sincos64(__float64 x, __float64 *sinx, __float64 *cosx) +{ + *sinx = _sin64(x); + *cosx = _cos64(x); +} + +_MATH_ALIAS_v_dDD(sincos) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_sinh.c b/src/audio/ffmpeg_dec/libm/math/s_sinh.c new file mode 100644 index 000000000000..abe3d19a3e67 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_sinh.c @@ -0,0 +1,84 @@ + +/* @(#)e_sinh.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* sinh(x) + * Method : + * mathematically sinh(x) if defined to be (exp(x)-exp(-x))/2 + * 1. Replace x by |x| (sinh(-x) = -sinh(x)). + * 2. + * E + E/(E+1) + * 0 <= x <= 22 : sinh(x) := --------------, E=expm1(x) + * 2 + * + * 22 <= x <= lnovft : sinh(x) := exp(x)/2 + * lnovft <= x <= ln2ovft: sinh(x) := exp(x/2)/2 * exp(x/2) + * ln2ovft < x : sinh(x) := x*shuge (overflow) + * + * Special cases: + * sinh(x) is |x| if x is +INF, -INF, or NaN. + * only sinh(0)=0 is exact for finite x. + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +static const __float64 one = _F_64(1.0); + +__float64 +sinh64(__float64 x) +{ + __float64 t, w, h; + __int32_t ix, jx; + __uint32_t lx; + + /* High word of |x|. */ + GET_HIGH_WORD(jx, x); + ix = jx & 0x7fffffff; + + /* x is INF or NaN */ + if (ix >= 0x7ff00000) + return x + x; + + h = _F_64(0.5); + if (jx < 0) + h = -h; + /* |x| in [0,22], return sign(x)*0.5*(E+E/(E+1))) */ + if (ix < 0x40360000) { /* |x|<22 */ + if (ix < 0x3e300000) /* |x|<2**-28 */ + return __math_inexact64(x); /* sinh(tiny) = tiny with inexact */ + t = expm164(fabs64(x)); + if (ix < 0x3ff00000) + return h * (_F_64(2.0) * t - t * t / (t + one)); + return h * (t + t / (t + one)); + } + + /* |x| in [22, log(maxdouble)] return 0.5*exp(|x|) */ + if (ix < 0x40862E42) + return h * exp64(fabs64(x)); + + /* |x| in [log(maxdouble), overflowthresold] */ + GET_LOW_WORD(lx, x); + if (ix < 0x408633CE || (ix == 0x408633ce && lx <= (__uint32_t)0x8fb9f87d)) { + w = exp64(_F_64(0.5) * fabs64(x)); + t = h * w; + return t * w; + } + + /* |x| > overflowthresold, sinh(x) overflow */ + return __math_oflow(x < 0); +} + +_MATH_ALIAS_d_d(sinh) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_sqrt.c b/src/audio/ffmpeg_dec/libm/math/s_sqrt.c new file mode 100644 index 000000000000..fde6696bf447 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_sqrt.c @@ -0,0 +1,453 @@ + +/* @(#)e_sqrt.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* sqrt(x) + * Return correctly rounded sqrt. + * ------------------------------------------ + * | Use the hardware sqrt if you have one | + * ------------------------------------------ + * Method: + * Bit by bit method using integer arithmetic. (Slow, but portable) + * 1. Normalization + * Scale x to y in [1,4) with even powers of 2: + * find an integer k such that 1 <= (y=x*2^(2k)) < 4, then + * sqrt(x) = 2^k * sqrt(y) + * 2. Bit by bit computation + * Let q = sqrt(y) truncated to i bit after binary point (q = 1), + * i 0 + * i+1 2 + * s = 2*q , and y = 2 * ( y - q ). (1) + * i i i i + * + * To compute q from q , one checks whether + * i+1 i + * + * -(i+1) 2 + * (q + 2 ) <= y. (2) + * i + * -(i+1) + * If (2) is false, then q = q ; otherwise q = q + 2 . + * i+1 i i+1 i + * + * With some algebric manipulation, it is not difficult to see + * that (2) is equivalent to + * -(i+1) + * s + 2 <= y (3) + * i i + * + * The advantage of (3) is that s and y can be computed by + * i i + * the following recurrence formula: + * if (3) is false + * + * s = s , y = y ; (4) + * i+1 i i+1 i + * + * otherwise, + * -i -(i+1) + * s = s + 2 , y = y - s - 2 (5) + * i+1 i i+1 i i + * + * One may easily use induction to prove (4) and (5). + * Note. Since the left hand side of (3) contain only i+2 bits, + * it does not necessary to do a full (53-bit) comparison + * in (3). + * 3. Final rounding + * After generating the 53 bits result, we compute one more bit. + * Together with the remainder, we can decide whether the + * result is exact, bigger than 1/2ulp, or less than 1/2ulp + * (it will never equal to 1/2ulp). + * The rounding mode can be detected by checking whether + * huge + tiny is equal to huge, and whether huge - tiny is + * equal to huge for some floating point number "huge" and "tiny". + * + * Special cases: + * sqrt(+-0) = +-0 ... exact + * sqrt(inf) = inf + * sqrt(-ve) = NaN ... with invalid signal + * sqrt(NaN) = NaN ... with invalid signal for signaling NaN + * + * Other methods : see the appended file at the end of the program below. + *--------------- + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +__float64 +sqrt64(__float64 x) +{ + __float64 z; + __uint32_t sign = 0x80000000; + __uint32_t r, t1, s1, ix1, q1; + __int32_t ix0, s0, q, m, t, i; + + EXTRACT_WORDS(ix0, ix1, x); + + /* take care of Inf and NaN */ + if ((ix0 & 0x7ff00000) == 0x7ff00000) { + if (ix0 < 0 && !isnan(x)) + return __math_invalid(x); /* sqrt(-inf)=sNaN */ + return x + x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf */ + } + /* take care of zero */ + if (ix0 <= 0) { + if (((ix0 & (~sign)) | ix1) == 0) + return x; /* sqrt(+-0) = +-0 */ + else if (ix0 < 0) + return __math_invalid(x); /* sqrt(-ve) = sNaN */ + } + /* normalize x */ + m = (ix0 >> 20); + if (m == 0) { /* subnormal x */ + while (ix0 == 0) { + m -= 21; + ix0 |= (ix1 >> 11); + ix1 <<= 21; + } + for (i = 0; (ix0 & 0x00100000) == 0; i++) + ix0 <<= 1; + m -= i - 1; + if (i) + ix0 |= (ix1 >> (32 - i)); + ix1 <<= i; + } + m -= 1023; /* unbias exponent */ + ix0 = (ix0 & 0x000fffff) | 0x00100000; + if (m & 1) { /* odd m, double x to make it even */ + ix0 += ix0 + ((ix1 & sign) >> 31); + ix1 += ix1; + } + m >>= 1; /* m = [m/2] */ + + /* generate sqrt(x) bit by bit */ + ix0 += ix0 + ((ix1 & sign) >> 31); + ix1 += ix1; + q = q1 = s0 = s1 = 0; /* [q,q1] = sqrt(x) */ + r = 0x00200000; /* r = moving bit from right to left */ + + while (r != 0) { + t = s0 + r; + if (t <= ix0) { + s0 = t + r; + ix0 -= t; + q += r; + } + ix0 += ix0 + ((ix1 & sign) >> 31); + ix1 += ix1; + r >>= 1; + } + + r = sign; + while (r != 0) { + t1 = s1 + r; + t = s0; + if ((t < ix0) || ((t == ix0) && (t1 <= ix1))) { + s1 = t1 + r; + if (((t1 & sign) == sign) && (s1 & sign) == 0) + s0 += 1; + ix0 -= t; + if (ix1 < t1) + ix0 -= 1; + ix1 -= t1; + q1 += r; + } + ix0 += ix0 + ((ix1 & sign) >> 31); + ix1 += ix1; + r >>= 1; + } + + if ((ix0 | ix1) != 0) { + FE_DECL_ROUND(rnd); + if (__is_nearest(rnd) || __is_upward(rnd)) { + if (q1 == (__uint32_t)0xffffffff) { + q1 = 0; + q += 1; + } else if (__is_upward(rnd)) { + if (q1 == (__uint32_t)0xfffffffe) + q += 1; + q1 += 2; + } else + q1 += (q1 & 1); + } + } + ix0 = (q >> 1) + 0x3fe00000; + ix1 = q1 >> 1; + if ((q & 1) == 1) + ix1 |= sign; + ix0 += lsl(m, 20); + INSERT_WORDS(z, ix0, ix1); + return z; +} + +_MATH_ALIAS_d_d(sqrt) + +#endif /* _NEED_FLOAT64 */ + +/* +Other methods (use floating-point arithmetic) +------------- +(This is a copy of a drafted paper by Prof W. Kahan +and K.C. Ng, written in May, 1986) + + Two algorithms are given here to implement sqrt(x) + (IEEE double precision arithmetic) in software. + Both supply sqrt(x) correctly rounded. The first algorithm (in + Section A) uses newton iterations and involves four divisions. + The second one uses reciproot iterations to avoid division, but + requires more multiplications. Both algorithms need the ability + to chop results of arithmetic operations instead of round them, + and the INEXACT flag to indicate when an arithmetic operation + is executed exactly with no roundoff error, all part of the + standard (IEEE 754-1985). The ability to perform shift, add, + subtract and logical AND operations upon 32-bit words is needed + too, though not part of the standard. + +A. sqrt(x) by Newton Iteration + + (1) Initial approximation + + Let x0 and x1 be the leading and the trailing 32-bit words of + a floating point number x (in IEEE double format) respectively + + 1 11 52 ...widths + ------------------------------------------------------ + x: |s| e | f | + ------------------------------------------------------ + msb lsb msb lsb ...order + + + ------------------------ ------------------------ + x0: |s| e | f1 | x1: | f2 | + ------------------------ ------------------------ + + By performing shifts and subtracts on x0 and x1 (both regarded + as integers), we obtain an 8-bit approximation of sqrt(x) as + follows. + + k := (x0>>1) + 0x1ff80000; + y0 := k - T1[31&(k>>15)]. ... y ~ sqrt(x) to 8 bits + Here k is a 32-bit integer and T1[] is an integer array containing + correction terms. Now magically the floating value of y (y's + leading 32-bit word is y0, the value of its trailing word is 0) + approximates sqrt(x) to almost 8-bit. + + Value of T1: + static int T1[32]= { + 0, 1024, 3062, 5746, 9193, 13348, 18162, 23592, + 29598, 36145, 43202, 50740, 58733, 67158, 75992, 85215, + 83599, 71378, 60428, 50647, 41945, 34246, 27478, 21581, + 16499, 12183, 8588, 5674, 3403, 1742, 661, 130,}; + + (2) Iterative refinement + + Apply Heron's rule three times to y, we have y approximates + sqrt(x) to within 1 ulp (Unit in the Last Place): + + y := (y+x/y)/2 ... almost 17 sig. bits + y := (y+x/y)/2 ... almost 35 sig. bits + y := y-(y-x/y)/2 ... within 1 ulp + + + Remark 1. + Another way to improve y to within 1 ulp is: + + y := (y+x/y) ... almost 17 sig. bits to 2*sqrt(x) + y := y - 0x00100006 ... almost 18 sig. bits to sqrt(x) + + 2 + (x-y )*y + y := y + 2* ---------- ...within 1 ulp + 2 + 3y + x + + + This formula has one division fewer than the one above; however, + it requires more multiplications and additions. Also x must be + scaled in advance to avoid spurious overflow in evaluating the + expression 3y*y+x. Hence it is not recommended uless division + is slow. If division is very slow, then one should use the + reciproot algorithm given in section B. + + (3) Final adjustment + + By twiddling y's last bit it is possible to force y to be + correctly rounded according to the prevailing rounding mode + as follows. Let r and i be copies of the rounding mode and + inexact flag before entering the square root program. Also we + use the expression y+-ulp for the next representable floating + numbers (up and down) of y. Note that y+-ulp = either fixed + point y+-1, or multiply y by nextafter(1,+-inf) in chopped + mode. + + I := FALSE; ... reset INEXACT flag I + R := RZ; ... set rounding mode to round-toward-zero + z := x/y; ... chopped quotient, possibly inexact + If(not I) then { ... if the quotient is exact + if(z=y) { + I := i; ... restore inexact flag + R := r; ... restore rounded mode + return sqrt(x):=y. + } else { + z := z - ulp; ... special rounding + } + } + i := TRUE; ... sqrt(x) is inexact + If (r=RN) then z=z+ulp ... rounded-to-nearest + If (r=RP) then { ... round-toward-+inf + y = y+ulp; z=z+ulp; + } + y := y+z; ... chopped sum + y0:=y0-0x00100000; ... y := y/2 is correctly rounded. + I := i; ... restore inexact flag + R := r; ... restore rounded mode + return sqrt(x):=y. + + (4) Special cases + + Square root of +inf, +-0, or NaN is itself; + Square root of a negative number is NaN with invalid signal. + + +B. sqrt(x) by Reciproot Iteration + + (1) Initial approximation + + Let x0 and x1 be the leading and the trailing 32-bit words of + a floating point number x (in IEEE double format) respectively + (see section A). By performing shifs and subtracts on x0 and y0, + we obtain a 7.8-bit approximation of 1/sqrt(x) as follows. + + k := 0x5fe80000 - (x0>>1); + y0:= k - T2[63&(k>>14)]. ... y ~ 1/sqrt(x) to 7.8 bits + + Here k is a 32-bit integer and T2[] is an integer array + containing correction terms. Now magically the floating + value of y (y's leading 32-bit word is y0, the value of + its trailing word y1 is set to zero) approximates 1/sqrt(x) + to almost 7.8-bit. + + Value of T2: + static int T2[64]= { + 0x1500, 0x2ef8, 0x4d67, 0x6b02, 0x87be, 0xa395, 0xbe7a, 0xd866, + 0xf14a, 0x1091b,0x11fcd,0x13552,0x14999,0x15c98,0x16e34,0x17e5f, + 0x18d03,0x19a01,0x1a545,0x1ae8a,0x1b5c4,0x1bb01,0x1bfde,0x1c28d, + 0x1c2de,0x1c0db,0x1ba73,0x1b11c,0x1a4b5,0x1953d,0x18266,0x16be0, + 0x1683e,0x179d8,0x18a4d,0x19992,0x1a789,0x1b445,0x1bf61,0x1c989, + 0x1d16d,0x1d77b,0x1dddf,0x1e2ad,0x1e5bf,0x1e6e8,0x1e654,0x1e3cd, + 0x1df2a,0x1d635,0x1cb16,0x1be2c,0x1ae4e,0x19bde,0x1868e,0x16e2e, + 0x1527f,0x1334a,0x11051,0xe951, 0xbe01, 0x8e0d, 0x5924, 0x1edd,}; + + (2) Iterative refinement + + Apply Reciproot iteration three times to y and multiply the + result by x to get an approximation z that matches sqrt(x) + to about 1 ulp. To be exact, we will have + -1ulp < sqrt(x)-z<1.0625ulp. + + ... set rounding mode to Round-to-nearest + y := y*(1.5-0.5*x*y*y) ... almost 15 sig. bits to 1/sqrt(x) + y := y*((1.5-2^-30)+0.5*x*y*y)... about 29 sig. bits to 1/sqrt(x) + ... special arrangement for better accuracy + z := x*y ... 29 bits to sqrt(x), with z*y<1 + z := z + 0.5*z*(1-z*y) ... about 1 ulp to sqrt(x) + + Remark 2. The constant 1.5-2^-30 is chosen to bias the error so that + (a) the term z*y in the final iteration is always less than 1; + (b) the error in the final result is biased upward so that + -1 ulp < sqrt(x) - z < 1.0625 ulp + instead of |sqrt(x)-z|<1.03125ulp. + + (3) Final adjustment + + By twiddling y's last bit it is possible to force y to be + correctly rounded according to the prevailing rounding mode + as follows. Let r and i be copies of the rounding mode and + inexact flag before entering the square root program. Also we + use the expression y+-ulp for the next representable floating + numbers (up and down) of y. Note that y+-ulp = either fixed + point y+-1, or multiply y by nextafter(1,+-inf) in chopped + mode. + + R := RZ; ... set rounding mode to round-toward-zero + switch(r) { + case RN: ... round-to-nearest + if(x<= z*(z-ulp)...chopped) z = z - ulp; else + if(x<= z*(z+ulp)...chopped) z = z; else z = z+ulp; + break; + case RZ:case RM: ... round-to-zero or round-to--inf + R:=RP; ... reset rounding mod to round-to-+inf + if(x=(z+ulp)*(z+ulp) ...rounded up) z = z+ulp; + break; + case RP: ... round-to-+inf + if(x>(z+ulp)*(z+ulp)...chopped) z = z+2*ulp; else + if(x>z*z ...chopped) z = z+ulp; + break; + } + + Remark 3. The above comparisons can be done in fixed point. For + example, to compare x and w=z*z chopped, it suffices to compare + x1 and w1 (the trailing parts of x and w), regarding them as + two's complement integers. + + ...Is z an exact square root? + To determine whether z is an exact square root of x, let z1 be the + trailing part of z, and also let x0 and x1 be the leading and + trailing parts of x. + + If ((z1&0x03ffffff)!=0) ... not exact if trailing 26 bits of z!=0 + I := 1; ... Raise Inexact flag: z is not exact + else { + j := 1 - [(x0>>20)&1] ... j = logb(x) mod 2 + k := z1 >> 26; ... get z's 25-th and 26-th + fraction bits + I := i or (k&j) or ((k&(j+j+1))!=(x1&3)); + } + R:= r ... restore rounded mode + return sqrt(x):=z. + + If multiplication is cheaper then the foregoing red tape, the + Inexact flag can be evaluated by + + I := i; + I := (z*z!=x) or I. + + Note that z*z can overwrite I; this value must be sensed if it is + True. + + Remark 4. If z*z = x exactly, then bit 25 to bit 0 of z1 must be + zero. + + -------------------- + z1: | f2 | + -------------------- + bit 31 bit 0 + + Further more, bit 27 and 26 of z1, bit 0 and 1 of x1, and the odd + or even of logb(x) have the following relations: + + ------------------------------------------------- + bit 27,26 of z1 bit 1,0 of x1 logb(x) + ------------------------------------------------- + 00 00 odd and even + 01 01 even + 10 10 odd + 10 00 even + 11 01 even + ------------------------------------------------- + + (4) Special cases (see (4) of Section A). + + */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_tan.c b/src/audio/ffmpeg_dec/libm/math/s_tan.c new file mode 100644 index 000000000000..685e156d9ae6 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_tan.c @@ -0,0 +1,104 @@ + +/* @(#)s_tan.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + +FUNCTION + <>, <>---tangent + +INDEX +tan +INDEX +tanf + +SYNOPSIS + #include + double tan(double <[x]>); + float tanf(float <[x]>); + +DESCRIPTION +<> computes the tangent of the argument <[x]>. +Angles are specified in radians. + +<> is identical, save that it takes and returns <> values. + +RETURNS +The tangent of <[x]> is returned. + +PORTABILITY +<> is ANSI. <> is an extension. +*/ + +/* tan(x) + * Return tangent function of x. + * + * kernel function: + * __kernel_tan ... tangent function on [-pi/4,pi/4] + * __rem_pio2 ... argument reduction routine + * + * Method. + * Let S,C and T denote the sin, cos and tan respectively on + * [-PI/4, +PI/4]. Reduce the argument x to y1+y2 = x-k*pi/2 + * in [-pi/4 , +pi/4], and let n = k mod 4. + * We have + * + * n sin(x) cos(x) tan(x) + * ---------------------------------------------------------- + * 0 S C T + * 1 C -S -1/T + * 2 -S -C T + * 3 -C S -1/T + * ---------------------------------------------------------- + * + * Special cases: + * Let trig be any of sin, cos, or tan. + * trig(+-INF) is NaN, with signals; + * trig(NaN) is that NaN; + * + * Accuracy: + * TRIG(x) returns trig(x) nearly rounded + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +__float64 +tan64(__float64 x) +{ + __float64 y[2], z = _F_64(0.0); + __int32_t n, ix; + + /* High word of x. */ + GET_HIGH_WORD(ix, x); + + /* |x| ~< pi/4 */ + ix &= 0x7fffffff; + if (ix <= 0x3fe921fb) + return __kernel_tan(x, z, 1); + + /* tan(Inf or NaN) is NaN */ + else if (ix >= 0x7ff00000) + return __math_invalid(x); /* NaN */ + + /* argument reduction needed */ + else { + n = __rem_pio2(x, y); + return __kernel_tan(y[0], y[1], 1 - ((n & 1) << 1)); /* 1 -- n even + -1 -- n odd */ + } +} + +_MATH_ALIAS_d_d(tan) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_tanh.c b/src/audio/ffmpeg_dec/libm/math/s_tanh.c new file mode 100644 index 000000000000..2f2c5f04f6c8 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_tanh.c @@ -0,0 +1,115 @@ + +/* @(#)s_tanh.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + +FUNCTION + <>, <>---hyperbolic tangent + +INDEX +tanh +INDEX +tanhf + +SYNOPSIS + #include + double tanh(double <[x]>); + float tanhf(float <[x]>); + +DESCRIPTION + +<> computes the hyperbolic tangent of +the argument <[x]>. Angles are specified in radians. + +<)>> is defined as +. sinh(<[x]>)/cosh(<[x]>) + +<> is identical, save that it takes and returns <> values. + +RETURNS +The hyperbolic tangent of <[x]> is returned. + +PORTABILITY +<> is ANSI C. <> is an extension. + +*/ + +/* Tanh(x) + * Return the Hyperbolic Tangent of x + * + * Method : + * x -x + * e - e + * 0. tanh(x) is defined to be ----------- + * x -x + * e + e + * 1. reduce x to non-negative by tanh(-x) = -tanh(x). + * 2. 0 <= x <= 2**-55 : tanh(x) := x*(one+x) + * -t + * 2**-55 < x <= 1 : tanh(x) := -----; t = expm1(-2x) + * t + 2 + * 2 + * 1 <= x <= 22.0 : tanh(x) := 1- ----- ; t=expm1(2x) + * t + 2 + * 22.0 < x <= INF : tanh(x) := 1. + * + * Special cases: + * tanh(NaN) is NaN; + * only tanh(0)=0 is exact for finite argument. + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +static const __float64 one = _F_64(1.0), two = _F_64(2.0); + +__float64 +tanh64(__float64 x) +{ + __float64 t, z; + __int32_t jx, ix; + + /* High word of |x|. */ + GET_HIGH_WORD(jx, x); + ix = jx & 0x7fffffff; + + /* x is INF or NaN */ + if (ix >= 0x7ff00000) { + if (jx >= 0) + return one / x + one; /* tanh(+-inf)=+-1 */ + else + return one / x - one; /* tanh(NaN) = NaN */ + } + + /* |x| < 22 */ + if (ix < 0x40360000) { /* |x|<22 */ + if (ix < 0x3c800000) /* |x|<2**-55 */ + return x * (one + x); /* tanh(small) = small */ + if (ix >= 0x3ff00000) { /* |x|>=1 */ + t = expm164(two * fabs64(x)); + z = one - two / (t + two); + } else { + t = expm164(-two * fabs64(x)); + z = -t / (t + two); + } + /* |x| > 22, return +-1 */ + } else { + z = __math_inexact64(one); + } + return (jx >= 0) ? z : -z; +} + +_MATH_ALIAS_d_d(tanh) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/s_tgamma.c b/src/audio/ffmpeg_dec/libm/math/s_tgamma.c new file mode 100644 index 000000000000..4d5eae1c28bb --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/s_tgamma.c @@ -0,0 +1,44 @@ + +/* @(#)e_tgamma.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + * + */ + +/* tgamma(x) + * Gamma function. Returns gamma(x) + * + * Method: See lgamma_r + */ + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +__float64 +tgamma64(__float64 x) +{ + int signgam_local; + int divzero = 0; + + if (isless(x, 0.0) && clang_barrier_double(rint64(x)) == x) + return __math_invalid(x); + + __float64 y = exp64(__math_lgamma_r(x, &signgam_local, &divzero)); + if (signgam_local < 0) + y = -y; + if (isinf(y) && finite(x) && !divzero) + return __math_oflow(signgam_local < 0); + return y; +} + +_MATH_ALIAS_d_d(tgamma) + +#endif diff --git a/src/audio/ffmpeg_dec/libm/math/sf_acos.c b/src/audio/ffmpeg_dec/libm/math/sf_acos.c new file mode 100644 index 000000000000..8f573b64d7fd --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_acos.c @@ -0,0 +1,80 @@ +/* ef_acos.c -- float version of e_acos.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" + +static const float one = 1.0000000000e+00, /* 0x3F800000 */ + pi = 3.1415925026e+00, /* 0x40490fda */ + pio2_hi = 1.5707962513e+00, /* 0x3fc90fda */ + pio2_lo = 7.5497894159e-08, /* 0x33a22168 */ + pS0 = 1.6666667163e-01, /* 0x3e2aaaab */ + pS1 = -3.2556581497e-01, /* 0xbea6b090 */ + pS2 = 2.0121252537e-01, /* 0x3e4e0aa8 */ + pS3 = -4.0055535734e-02, /* 0xbd241146 */ + pS4 = 7.9153501429e-04, /* 0x3a4f7f04 */ + pS5 = 3.4793309169e-05, /* 0x3811ef08 */ + qS1 = -2.4033949375e+00, /* 0xc019d139 */ + qS2 = 2.0209457874e+00, /* 0x4001572d */ + qS3 = -6.8828397989e-01, /* 0xbf303361 */ + qS4 = 7.7038154006e-02; /* 0x3d9dc62e */ + +float +acosf(float x) +{ + float z, p, q, r, w, s, c, df; + __int32_t hx, ix; + GET_FLOAT_WORD(hx, x); + ix = hx & 0x7fffffff; + if (ix == 0x3f800000) { /* |x|==1 */ + if (hx > 0) + return 0.0; /* acos(1) = 0 */ + else + return pi + (float)2.0 * pio2_lo; /* acos(-1)= pi */ + } else if (ix > 0x3f800000) { /* |x| >= 1 */ + return __math_invalidf(x); /* acos(|x|>1) is NaN */ + } + if (ix < 0x3f000000) { /* |x| < 0.5 */ + if (ix <= 0x23000000) + return pio2_hi + pio2_lo; /*if|x|<2**-57*/ + z = x * x; + p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5))))); + q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4))); + r = p / q; + return pio2_hi - (x - (pio2_lo - x * r)); + } else if (hx < 0) { /* x < -0.5 */ + z = (one + x) * (float)0.5; + p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5))))); + q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4))); + s = sqrtf(z); + r = p / q; + w = r * s - pio2_lo; + return pi - (float)2.0 * (s + w); + } else { /* x > 0.5 */ + __int32_t idf; + z = (one - x) * (float)0.5; + s = sqrtf(z); + df = s; + GET_FLOAT_WORD(idf, df); + SET_FLOAT_WORD(df, idf & 0xfffff000); + c = (z - df * df) / (s + df); + p = z * (pS0 + z * (pS1 + z * (pS2 + z * (pS3 + z * (pS4 + z * pS5))))); + q = one + z * (qS1 + z * (qS2 + z * (qS3 + z * qS4))); + r = p / q; + w = r * s + c; + return (float)2.0 * (df + w); + } +} + +_MATH_ALIAS_f_f(acos) diff --git a/src/audio/ffmpeg_dec/libm/math/sf_acosh.c b/src/audio/ffmpeg_dec/libm/math/sf_acosh.c new file mode 100644 index 000000000000..4489449cea7f --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_acosh.c @@ -0,0 +1,45 @@ +/* ef_acosh.c -- float version of e_acosh.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + * + */ + +#include "fdlibm.h" + +static const float one = 1.0, ln2 = 6.9314718246e-01; /* 0x3f317218 */ + +float +acoshf(float x) +{ + float t; + __int32_t hx; + GET_FLOAT_WORD(hx, x); + if (hx < 0x3f800000) { /* x < 1 */ + return __math_invalidf(x); + } else if (hx >= 0x4d800000) { /* x > 2**28 */ + if (!FLT_UWORD_IS_FINITE(hx)) { /* x is inf of NaN */ + return x + x; + } else + return logf(x) + ln2; /* acosh(huge)=log(2x) */ + } else if (hx == 0x3f800000) { + return 0.0; /* acosh(1) = 0 */ + } else if (hx > 0x40000000) { /* 2**28 > x > 2 */ + t = x * x; + return logf((float)2.0 * x - one / (x + sqrtf(t - one))); + } else { /* 1 0x3f800000) { /* |x|>= 1 */ + return __math_invalidf(x); /* asin(|x|>1) is NaN */ + } else if (ix < 0x3f000000) { /* |x|<0.5 */ + if (ix < 0x32000000) { /* if |x| < 2**-27 */ + if (huge + x > one) + return x; /* return x with inexact if x!=0*/ + } else { + t = x * x; + p = t * + (pS0 + t * (pS1 + t * (pS2 + t * (pS3 + t * (pS4 + t * pS5))))); + q = one + t * (qS1 + t * (qS2 + t * (qS3 + t * qS4))); + w = p / q; + return x + x * w; + } + } + /* 1> |x|>= 0.5 */ + w = one - fabsf(x); + t = w * (float)0.5; + p = t * (pS0 + t * (pS1 + t * (pS2 + t * (pS3 + t * (pS4 + t * pS5))))); + q = one + t * (qS1 + t * (qS2 + t * (qS3 + t * qS4))); + s = sqrtf(t); + if (ix >= 0x3F79999A) { /* if |x| > 0.975 */ + w = p / q; + t = pio2_hi - ((float)2.0 * (s + s * w) - pio2_lo); + } else { + __int32_t iw; + w = s; + GET_FLOAT_WORD(iw, w); + SET_FLOAT_WORD(w, iw & 0xfffff000); + c = (t - w * w) / (s + w); + r = p / q; + p = (float)2.0 * s * r - (pio2_lo - (float)2.0 * c); + q = pio4_hi - (float)2.0 * w; + t = pio4_hi - (p - q); + } + if (hx > 0) + return t; + else + return -t; +} + +_MATH_ALIAS_f_f(asin) diff --git a/src/audio/ffmpeg_dec/libm/math/sf_asinh.c b/src/audio/ffmpeg_dec/libm/math/sf_asinh.c new file mode 100644 index 000000000000..d7656ac3376f --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_asinh.c @@ -0,0 +1,50 @@ +/* sf_asinh.c -- float version of s_asinh.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" + +static const float one = 1.0000000000e+00, /* 0x3F800000 */ + ln2 = 6.9314718246e-01, /* 0x3f317218 */ + huge = 1.0000000000e+30; + +float +asinhf(float x) +{ + float t, w; + __int32_t hx, ix; + GET_FLOAT_WORD(hx, x); + ix = hx & 0x7fffffff; + if (!FLT_UWORD_IS_FINITE(ix)) + return x + x; /* x is inf or NaN */ + if (ix < 0x31800000) { /* |x|<2**-28 */ + if (huge + x > one) + return x; /* return x inexact except 0 */ + } + if (ix > 0x4d800000) { /* |x| > 2**28 */ + w = logf(fabsf(x)) + ln2; + } else if (ix > 0x40000000) { /* 2**28 > |x| > 2.0 */ + t = fabsf(x); + w = logf((float)2.0 * t + one / (sqrtf(x * x + one) + t)); + } else { /* 2.0 > |x| > 2**-28 */ + t = x * x; + w = log1pf(fabsf(x) + t / (one + sqrtf(one + t))); + } + if (hx > 0) + return w; + else + return -w; +} + +_MATH_ALIAS_f_f(asinh) diff --git a/src/audio/ffmpeg_dec/libm/math/sf_atan.c b/src/audio/ffmpeg_dec/libm/math/sf_atan.c new file mode 100644 index 000000000000..65ff918a5dab --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_atan.c @@ -0,0 +1,105 @@ +/* sf_atan.c -- float version of s_atan.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + * + */ + +#include "fdlibm.h" + +static const float atanhi[] = { + 4.6364760399e-01, /* atan(0.5)hi 0x3eed6338 */ + 7.8539812565e-01, /* atan(1.0)hi 0x3f490fda */ + 9.8279368877e-01, /* atan(1.5)hi 0x3f7b985e */ + 1.5707962513e+00, /* atan(inf)hi 0x3fc90fda */ +}; + +static const float atanlo[] = { + 5.0121582440e-09, /* atan(0.5)lo 0x31ac3769 */ + 3.7748947079e-08, /* atan(1.0)lo 0x33222168 */ + 3.4473217170e-08, /* atan(1.5)lo 0x33140fb4 */ + 7.5497894159e-08, /* atan(inf)lo 0x33a22168 */ +}; + +static const float aT[] = { + 3.3333334327e-01, /* 0x3eaaaaaa */ + -2.0000000298e-01, /* 0xbe4ccccd */ + 1.4285714924e-01, /* 0x3e124925 */ + -1.1111110449e-01, /* 0xbde38e38 */ + 9.0908870101e-02, /* 0x3dba2e6e */ + -7.6918758452e-02, /* 0xbd9d8795 */ + 6.6610731184e-02, /* 0x3d886b35 */ + -5.8335702866e-02, /* 0xbd6ef16b */ + 4.9768779427e-02, /* 0x3d4bda59 */ + -3.6531571299e-02, /* 0xbd15a221 */ + 1.6285819933e-02, /* 0x3c8569d7 */ +}; + +static const float one = 1.0; + +float +atanf(float x) +{ + float w, s1, s2, z; + __int32_t ix, hx, id; + + GET_FLOAT_WORD(hx, x); + ix = hx & 0x7fffffff; + if (ix >= 0x50800000) { /* if |x| >= 2^34 */ + if (FLT_UWORD_IS_NAN(ix)) + return x + x; /* NaN */ + if (hx > 0) + return atanhi[3] + atanlo[3]; + else + return -atanhi[3] - atanlo[3]; + } + if (ix < 0x3ee00000) { /* |x| < 0.4375 */ + if (ix < 0x31000000) /* |x| < 2^-29 */ + return __math_inexactf(x); + id = -1; + } else { + x = fabsf(x); + if (ix < 0x3f980000) { /* |x| < 1.1875 */ + if (ix < 0x3f300000) { /* 7/16 <=|x|<11/16 */ + id = 0; + x = ((float)2.0 * x - one) / ((float)2.0 + x); + } else { /* 11/16<=|x|< 19/16 */ + id = 1; + x = (x - one) / (x + one); + } + } else { + if (ix < 0x401c0000) { /* |x| < 2.4375 */ + id = 2; + x = (x - (float)1.5) / (one + (float)1.5 * x); + } else { /* 2.4375 <= |x| < 2^66 */ + id = 3; + x = -(float)1.0 / x; + } + } + } + /* end of argument reduction */ + z = x * x; + w = z * z; + /* break sum from i=0 to 10 aT[i]z**(i+1) into odd and even poly */ + s1 = z * + (aT[0] + + w * (aT[2] + w * (aT[4] + w * (aT[6] + w * (aT[8] + w * aT[10]))))); + s2 = w * (aT[1] + w * (aT[3] + w * (aT[5] + w * (aT[7] + w * aT[9])))); + if (id < 0) + return x - x * (s1 + s2); + else { + z = atanhi[id] - ((x * (s1 + s2) - atanlo[id]) - x); + return (hx < 0) ? -z : z; + } +} + +_MATH_ALIAS_f_f(atan) diff --git a/src/audio/ffmpeg_dec/libm/math/sf_atan2.c b/src/audio/ffmpeg_dec/libm/math/sf_atan2.c new file mode 100644 index 000000000000..ba4d5528d845 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_atan2.c @@ -0,0 +1,111 @@ +/* ef_atan2.c -- float version of e_atan2.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + * + */ + +#include "fdlibm.h" + +static const float tiny = 1.0e-30, zero = 0.0, + pi_o_4 = 7.8539818525e-01, /* 0x3f490fdb */ + pi_o_2 = 1.5707963705e+00, /* 0x3fc90fdb */ + pi = 3.1415927410e+00, /* 0x40490fdb */ + pi_lo = -8.7422776573e-08; /* 0xb3bbbd2e */ + +float +atan2f(float y, float x) +{ + float z; + __int32_t k, m, hx, hy, ix, iy; + + GET_FLOAT_WORD(hx, x); + ix = hx & 0x7fffffff; + GET_FLOAT_WORD(hy, y); + iy = hy & 0x7fffffff; + if (FLT_UWORD_IS_NAN(ix) || FLT_UWORD_IS_NAN(iy)) /* x or y is NaN */ + return x + y; + if (hx == 0x3f800000) + return atanf(y); /* x=1.0 */ + m = ((hy >> 31) & 1) | ((hx >> 30) & 2); /* 2*sign(x)+sign(y) */ + + /* when y = 0 */ + if (FLT_UWORD_IS_ZERO(iy)) { + switch (m) { + case 0: + case 1: + return y; /* atan(+-0,+anything)=+-0 */ + case 2: + return pi + tiny; /* atan(+0,-anything) = pi */ + case 3: + return -pi - tiny; /* atan(-0,-anything) =-pi */ + } + } + /* when x = 0 */ + if (FLT_UWORD_IS_ZERO(ix)) + return (hy < 0) ? -pi_o_2 - tiny : pi_o_2 + tiny; + + /* when x is INF */ + if (FLT_UWORD_IS_INFINITE(ix)) { + if (FLT_UWORD_IS_INFINITE(iy)) { + switch (m) { + case 0: + return pi_o_4 + tiny; /* atan(+INF,+INF) */ + case 1: + return -pi_o_4 - tiny; /* atan(-INF,+INF) */ + case 2: + return (float)3.0 * pi_o_4 + tiny; /*atan(+INF,-INF)*/ + case 3: + return (float)-3.0 * pi_o_4 - tiny; /*atan(-INF,-INF)*/ + } + } else { + switch (m) { + case 0: + return zero; /* atan(+...,+INF) */ + case 1: + return -zero; /* atan(-...,+INF) */ + case 2: + return pi + tiny; /* atan(+...,-INF) */ + case 3: + return -pi - tiny; /* atan(-...,-INF) */ + } + } + } + /* when y is INF */ + if (FLT_UWORD_IS_INFINITE(iy)) + return (hy < 0) ? -pi_o_2 - tiny : pi_o_2 + tiny; + + /* compute y/x */ + k = (iy - ix) >> 23; + if (k > 60) + z = pi_o_2 + (float)0.5 * pi_lo; /* |y/x| > 2**60 */ + else if (hx < 0 && k < -60) + z = 0.0f; /* |y|/x < -2**60 */ + else + z = atanf(check_uflowf(fabsf(y / x))); /* safe to do y/x */ + switch (m) { + case 0: + return z; /* atan(+,+) */ + case 1: { + __uint32_t zh; + GET_FLOAT_WORD(zh, z); + SET_FLOAT_WORD(z, zh ^ 0x80000000); + } + return z; /* atan(-,+) */ + case 2: + return pi - (z - pi_lo); /* atan(+,-) */ + default: /* case 3 */ + return (z - pi_lo) - pi; /* atan(-,-) */ + } +} + +_MATH_ALIAS_f_ff(atan2) diff --git a/src/audio/ffmpeg_dec/libm/math/sf_atanh.c b/src/audio/ffmpeg_dec/libm/math/sf_atanh.c new file mode 100644 index 000000000000..87c2a2e681bd --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_atanh.c @@ -0,0 +1,46 @@ +/* ef_atanh.c -- float version of e_atanh.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + * + */ + +#include "fdlibm.h" + +static const float one = 1.0; + +float +atanhf(float x) +{ + float t; + __int32_t hx, ix; + GET_FLOAT_WORD(hx, x); + ix = hx & 0x7fffffff; + if (ix > 0x3f800000) /* |x|>1 */ + return __math_invalidf(x); + if (ix == 0x3f800000) + return __math_divzerof(x < 0); + if (ix < 0x31800000) /* x<2**-28 */ + return __math_inexactf(x); + SET_FLOAT_WORD(x, ix); + if (ix < 0x3f000000) { /* x < 0.5 */ + t = x + x; + t = (float)0.5 * log1pf(t + t * x / (one - x)); + } else + t = (float)0.5 * log1pf((x + x) / (one - x)); + if (hx >= 0) + return t; + else + return -t; +} + +_MATH_ALIAS_f_f(atanh) diff --git a/src/audio/ffmpeg_dec/libm/math/sf_ceil.c b/src/audio/ffmpeg_dec/libm/math/sf_ceil.c new file mode 100644 index 000000000000..e665c36d5d30 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_ceil.c @@ -0,0 +1,51 @@ +/* sf_ceil.c -- float version of s_ceil.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" + +float +ceilf(float x) +{ + __int32_t i0, j0; + __uint32_t i, ix; + GET_FLOAT_WORD(i0, x); + ix = (i0 & 0x7fffffff); + j0 = (ix >> 23) - 0x7f; + if (j0 < 23) { + if (j0 < 0) { + if (i0 < 0) { + i0 = 0x80000000; + } else if (!FLT_UWORD_IS_ZERO(ix)) { + i0 = 0x3f800000; + } + } else { + i = (0x007fffff) >> j0; + if ((i0 & i) == 0) + return x; /* x is integral */ + if (i0 > 0) + i0 += (0x00800000) >> j0; + i0 &= (~i); + } + } else { + if (!FLT_UWORD_IS_FINITE(ix)) + return x + x; /* inf or NaN */ + else + return x; /* x is integral */ + } + SET_FLOAT_WORD(x, i0); + return x; +} + +_MATH_ALIAS_f_f(ceil) diff --git a/src/audio/ffmpeg_dec/libm/math/sf_cos.c b/src/audio/ffmpeg_dec/libm/math/sf_cos.c new file mode 100644 index 000000000000..4cc88ae97c7a --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_cos.c @@ -0,0 +1,62 @@ +/* sf_cos.c -- float version of s_cos.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" +#if __OBSOLETE_MATH_FLOAT + +float +cosf(float x) +{ + float y[2], z = 0.0; + __int32_t n, ix; + + GET_FLOAT_WORD(ix, x); + + /* |x| ~< pi/4 */ + ix &= 0x7fffffff; + if (ix <= 0x3f490fd8) + return __kernel_cosf(x, z); + + /* cos(Inf or NaN) is NaN */ + else if (!FLT_UWORD_IS_FINITE(ix)) + return __math_invalidf(x); + + /* argument reduction needed */ + else { + n = __rem_pio2f(x, y); + switch (n & 3) { + case 0: + return __kernel_cosf(y[0], y[1]); + case 1: + return -__kernel_sinf(y[0], y[1], 1); + case 2: + return -__kernel_cosf(y[0], y[1]); + default: + return __kernel_sinf(y[0], y[1], 1); + } + } +} + +#ifdef __strong_reference +#if defined(__GNUCLIKE_PRAGMA_DIAGNOSTIC) && !defined(__clang__) +#pragma GCC diagnostic ignored "-Wmissing-attributes" +#endif +__strong_reference(cosf, _cosf); +#endif + +_MATH_ALIAS_f_f(cos) +#else +#include "../common/cosf.c" +#endif /* __OBSOLETE_MATH_FLOAT */ diff --git a/src/audio/ffmpeg_dec/libm/math/sf_cosh.c b/src/audio/ffmpeg_dec/libm/math/sf_cosh.c new file mode 100644 index 000000000000..387dceab4da7 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_cosh.c @@ -0,0 +1,70 @@ +/* ef_cosh.c -- float version of e_cosh.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" +#include "math_config.h" + +#ifdef __v810__ +#define const +#endif + +static const float one = 1.0, half = 0.5; + +float +coshf(float x) +{ + float t, w; + __int32_t ix; + + GET_FLOAT_WORD(ix, x); + ix &= 0x7fffffff; + + x = fabsf(x); + + /* x is INF or NaN */ + if (!FLT_UWORD_IS_FINITE(ix)) + return x + x; + + /* |x| in [0,0.5*ln2], return 1+expm1(|x|)^2/(2*exp(|x|)) */ + if (ix < 0x3eb17218) { + t = expm1f(x); + w = one + t; + if (ix < 0x24000000) + return w; /* cosh(tiny) = 1 */ + return one + (t * t) / (w + w); + } + + /* |x| in [0.5*ln2,22], return (exp(|x|)+1/exp(|x|)/2; */ + if (ix < 0x41b00000) { + t = expf(x); + return half * t + half / t; + } + + /* |x| in [22, log(maxdouble)] return half*exp(|x|) */ + if (ix <= FLT_UWORD_LOG_MAX) + return half * expf(x); + + /* |x| in [log(maxdouble), overflowthresold] */ + if (ix <= FLT_UWORD_LOG_2MAX) { + w = expf(half * x); + t = half * w; + return t * w; + } + + /* |x| > overflowthresold, cosh(x) overflow */ + return __math_oflowf(0); +} + +_MATH_ALIAS_f_f(cosh) diff --git a/src/audio/ffmpeg_dec/libm/math/sf_drem.c b/src/audio/ffmpeg_dec/libm/math/sf_drem.c new file mode 100644 index 000000000000..b4c6c7444fc9 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_drem.c @@ -0,0 +1,24 @@ +/* +Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + +Developed at SunPro, a Sun Microsystems, Inc. business. +Permission to use, copy, modify, and distribute this +software is freely granted, provided that this notice +is preserved. + */ +/* + * dremf() wrapper for remainderf(). + * + * Written by J.T. Conklin, + * Placed into the Public Domain, 1994. + */ + +#include "fdlibm.h" + +float +dremf(float x, float y) +{ + return remainderf(x, y); +} + +_MATH_ALIAS_f_ff(drem) diff --git a/src/audio/ffmpeg_dec/libm/math/sf_erf.c b/src/audio/ffmpeg_dec/libm/math/sf_erf.c new file mode 100644 index 000000000000..b5f226764ef2 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_erf.c @@ -0,0 +1,254 @@ +/* sf_erf.c -- float version of s_erf.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" +#include "math_config.h" + +#ifdef __v810__ +#define const +#endif + +static const float tiny = 1e-30, half = 5.0000000000e-01, /* 0x3F000000 */ + one = 1.0000000000e+00, /* 0x3F800000 */ + two = 2.0000000000e+00, /* 0x40000000 */ + /* c = (subfloat)0.84506291151 */ + erx = 8.4506291151e-01, /* 0x3f58560b */ + /* + * Coefficients for approximation to erf on [0,0.84375] + */ + efx = 1.2837916613e-01, /* 0x3e0375d4 */ + efx8 = 1.0270333290e+00, /* 0x3f8375d4 */ + pp0 = 1.2837916613e-01, /* 0x3e0375d4 */ + pp1 = -3.2504209876e-01, /* 0xbea66beb */ + pp2 = -2.8481749818e-02, /* 0xbce9528f */ + pp3 = -5.7702702470e-03, /* 0xbbbd1489 */ + pp4 = -2.3763017452e-05, /* 0xb7c756b1 */ + qq1 = 3.9791721106e-01, /* 0x3ecbbbce */ + qq2 = 6.5022252500e-02, /* 0x3d852a63 */ + qq3 = 5.0813062117e-03, /* 0x3ba68116 */ + qq4 = 1.3249473704e-04, /* 0x390aee49 */ + qq5 = -3.9602282413e-06, /* 0xb684e21a */ + /* + * Coefficients for approximation to erf in [0.84375,1.25] + */ + pa0 = -2.3621185683e-03, /* 0xbb1acdc6 */ + pa1 = 4.1485610604e-01, /* 0x3ed46805 */ + pa2 = -3.7220788002e-01, /* 0xbebe9208 */ + pa3 = 3.1834661961e-01, /* 0x3ea2fe54 */ + pa4 = -1.1089469492e-01, /* 0xbde31cc2 */ + pa5 = 3.5478305072e-02, /* 0x3d1151b3 */ + pa6 = -2.1663755178e-03, /* 0xbb0df9c0 */ + qa1 = 1.0642088205e-01, /* 0x3dd9f331 */ + qa2 = 5.4039794207e-01, /* 0x3f0a5785 */ + qa3 = 7.1828655899e-02, /* 0x3d931ae7 */ + qa4 = 1.2617121637e-01, /* 0x3e013307 */ + qa5 = 1.3637083583e-02, /* 0x3c5f6e13 */ + qa6 = 1.1984500103e-02, /* 0x3c445aa3 */ + /* + * Coefficients for approximation to erfc in [1.25,1/0.35] + */ + ra0 = -9.8649440333e-03, /* 0xbc21a093 */ + ra1 = -6.9385856390e-01, /* 0xbf31a0b7 */ + ra2 = -1.0558626175e+01, /* 0xc128f022 */ + ra3 = -6.2375331879e+01, /* 0xc2798057 */ + ra4 = -1.6239666748e+02, /* 0xc322658c */ + ra5 = -1.8460508728e+02, /* 0xc3389ae7 */ + ra6 = -8.1287437439e+01, /* 0xc2a2932b */ + ra7 = -9.8143291473e+00, /* 0xc11d077e */ + sa1 = 1.9651271820e+01, /* 0x419d35ce */ + sa2 = 1.3765776062e+02, /* 0x4309a863 */ + sa3 = 4.3456588745e+02, /* 0x43d9486f */ + sa4 = 6.4538726807e+02, /* 0x442158c9 */ + sa5 = 4.2900814819e+02, /* 0x43d6810b */ + sa6 = 1.0863500214e+02, /* 0x42d9451f */ + sa7 = 6.5702495575e+00, /* 0x40d23f7c */ + sa8 = -6.0424413532e-02, /* 0xbd777f97 */ + /* + * Coefficients for approximation to erfc in [1/.35,28] + */ + rb0 = -9.8649431020e-03, /* 0xbc21a092 */ + rb1 = -7.9928326607e-01, /* 0xbf4c9dd4 */ + rb2 = -1.7757955551e+01, /* 0xc18e104b */ + rb3 = -1.6063638306e+02, /* 0xc320a2ea */ + rb4 = -6.3756646729e+02, /* 0xc41f6441 */ + rb5 = -1.0250950928e+03, /* 0xc480230b */ + rb6 = -4.8351919556e+02, /* 0xc3f1c275 */ + sb1 = 3.0338060379e+01, /* 0x41f2b459 */ + sb2 = 3.2579251099e+02, /* 0x43a2e571 */ + sb3 = 1.5367296143e+03, /* 0x44c01759 */ + sb4 = 3.1998581543e+03, /* 0x4547fdbb */ + sb5 = 2.5530502930e+03, /* 0x451f90ce */ + sb6 = 4.7452853394e+02, /* 0x43ed43a7 */ + sb7 = -2.2440952301e+01; /* 0xc1b38712 */ + +float +erff(float x) +{ + __int32_t hx, ix, i; + float R, S, P, Q, s, y, z, r; + GET_FLOAT_WORD(hx, x); + ix = hx & 0x7fffffff; + if (!FLT_UWORD_IS_FINITE(ix)) { /* erf(nan)=nan */ + i = ((__uint32_t)hx >> 31) << 1; + return (float)(1 - i) + one / x; /* erf(+-inf)=+-1 */ + } + + if (ix < 0x3f580000) { /* |x|<0.84375 */ + if (ix < 0x31800000) { /* |x|<2**-28 */ + if (ix < 0x04000000) + /*avoid underflow */ + return (float)0.125 * ((float)8.0 * x + efx8 * x); + return x + efx * x; + } + z = x * x; + r = pp0 + z * (pp1 + z * (pp2 + z * (pp3 + z * pp4))); + s = one + z * (qq1 + z * (qq2 + z * (qq3 + z * (qq4 + z * qq5)))); + y = r / s; + return x + x * y; + } + if (ix < 0x3fa00000) { /* 0.84375 <= |x| < 1.25 */ + s = fabsf(x) - one; + P = pa0 + + s * (pa1 + s * (pa2 + s * (pa3 + s * (pa4 + s * (pa5 + s * pa6))))); + Q = one + + s * (qa1 + s * (qa2 + s * (qa3 + s * (qa4 + s * (qa5 + s * qa6))))); + if (hx >= 0) + return erx + P / Q; + else + return -erx - P / Q; + } + if (ix >= 0x40c00000) { /* inf>|x|>=6 */ + if (hx >= 0) + return one - tiny; + else + return tiny - one; + } + x = fabsf(x); + s = one / (x * x); + if (ix < 0x4036DB6E) { /* |x| < 1/0.35 */ + R = ra0 + + s * (ra1 + + s * (ra2 + + s * (ra3 + s * (ra4 + s * (ra5 + s * (ra6 + s * ra7)))))); + S = one + + s * (sa1 + + s * (sa2 + + s * (sa3 + + s * (sa4 + + s * (sa5 + s * (sa6 + s * (sa7 + s * sa8))))))); + } else { /* |x| >= 1/0.35 */ + R = rb0 + + s * (rb1 + s * (rb2 + s * (rb3 + s * (rb4 + s * (rb5 + s * rb6))))); + S = one + + s * (sb1 + + s * (sb2 + + s * (sb3 + s * (sb4 + s * (sb5 + s * (sb6 + s * sb7)))))); + } + GET_FLOAT_WORD(ix, x); + SET_FLOAT_WORD(z, ix & 0xfffff000); + r = expf(-z * z - (float)0.5625) * expf((z - x) * (z + x) + R / S); + if (hx >= 0) + return one - r / x; + else + return r / x - one; +} + +float +erfcf(float x) +{ + __int32_t hx, ix; + float R, S, P, Q, s, y, z, r; + GET_FLOAT_WORD(hx, x); + ix = hx & 0x7fffffff; + if (!FLT_UWORD_IS_FINITE(ix)) { /* erfc(nan)=nan */ + /* erfc(+-inf)=0,2 */ + return (float)(((__uint32_t)hx >> 31) << 1) + one / x; + } + + if (ix < 0x3f580000) { /* |x|<0.84375 */ + if (ix < 0x23800000) /* |x|<2**-56 */ + return one - x; + z = x * x; + r = pp0 + z * (pp1 + z * (pp2 + z * (pp3 + z * pp4))); + s = one + z * (qq1 + z * (qq2 + z * (qq3 + z * (qq4 + z * qq5)))); + y = r / s; + if (hx < 0x3e800000) { /* x<1/4 */ + return one - (x + x * y); + } else { + r = x * y; + r += (x - half); + return half - r; + } + } + if (ix < 0x3fa00000) { /* 0.84375 <= |x| < 1.25 */ + s = fabsf(x) - one; + P = pa0 + + s * (pa1 + s * (pa2 + s * (pa3 + s * (pa4 + s * (pa5 + s * pa6))))); + Q = one + + s * (qa1 + s * (qa2 + s * (qa3 + s * (qa4 + s * (qa5 + s * qa6))))); + if (hx >= 0) { + z = one - erx; + return z - P / Q; + } else { + z = erx + P / Q; + return one + z; + } + } + if (ix < 0x41e00000) { /* |x|<28 */ + x = fabsf(x); + s = one / (x * x); + if (ix < 0x4036DB6D) { /* |x| < 1/.35 ~ 2.857143*/ + R = ra0 + + s * (ra1 + + s * (ra2 + + s * (ra3 + + s * (ra4 + s * (ra5 + s * (ra6 + s * ra7)))))); + S = one + + s * (sa1 + + s * (sa2 + + s * (sa3 + + s * (sa4 + + s * (sa5 + + s * (sa6 + s * (sa7 + s * sa8))))))); + } else { /* |x| >= 1/.35 ~ 2.857143 */ + if (hx < 0 && ix >= 0x40c00000) + return two - tiny; /* x < -6 */ + R = rb0 + + s * (rb1 + + s * (rb2 + s * (rb3 + s * (rb4 + s * (rb5 + s * rb6))))); + S = one + + s * (sb1 + + s * (sb2 + + s * (sb3 + + s * (sb4 + s * (sb5 + s * (sb6 + s * sb7)))))); + } + GET_FLOAT_WORD(ix, x); + SET_FLOAT_WORD(z, ix & 0xfffff000); + r = expf(-z * z - (float)0.5625) * expf((z - x) * (z + x) + R / S); + if (hx > 0) + return r / x; + else + return two - r / x; + } else { + if (hx > 0) + return __math_uflowf(0); + else + return two - tiny; + } +} + +_MATH_ALIAS_f_f(erf) + +_MATH_ALIAS_f_f(erfc) diff --git a/src/audio/ffmpeg_dec/libm/math/sf_exp.c b/src/audio/ffmpeg_dec/libm/math/sf_exp.c new file mode 100644 index 000000000000..a1e63cd2d241 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_exp.c @@ -0,0 +1,103 @@ +/* ef_exp.c -- float version of e_exp.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" +#include "math_config.h" + +#if __OBSOLETE_MATH_FLOAT +#ifdef __v810__ +#define const +#endif + +static const float +one = 1.0, +halF[2] = {0.5,-0.5,}, +huge = 1.0e+30, +twom100 = 7.8886090522e-31, /* 2**-100=0x0d800000 */ +ln2HI[2] ={ 6.9313812256e-01, /* 0x3f317180 */ + -6.9313812256e-01,}, /* 0xbf317180 */ +ln2LO[2] ={ 9.0580006145e-06, /* 0x3717f7d1 */ + -9.0580006145e-06,}, /* 0xb717f7d1 */ +invln2 = 1.4426950216e+00, /* 0x3fb8aa3b */ +P1 = 1.6666667163e-01, /* 0x3e2aaaab */ +P2 = -2.7777778450e-03, /* 0xbb360b61 */ +P3 = 6.6137559770e-05, /* 0x388ab355 */ +P4 = -1.6533901999e-06, /* 0xb5ddea0e */ +P5 = 4.1381369442e-08; /* 0x3331bb4c */ + +float +expf(float x) /* default IEEE double exp */ +{ + float y, hi, lo, c, t; + __int32_t k = 0, xsb, sx; + __uint32_t hx; + + GET_FLOAT_WORD(sx, x); + xsb = (sx >> 31) & 1; /* sign bit of x */ + hx = sx & 0x7fffffff; /* high word of |x| */ + + /* filter out non-finite argument */ + if (FLT_UWORD_IS_NAN(hx)) + return x + x; /* NaN */ + if (FLT_UWORD_IS_INFINITE(hx)) + return (xsb == 0) ? x : 0.0f; /* exp(+-inf)={inf,0} */ + if (sx > FLT_UWORD_LOG_MAX) + return __math_oflowf(0); /* overflow */ + if (sx < 0 && hx > FLT_UWORD_LOG_MIN) + return __math_uflowf(0); /* underflow */ + + /* argument reduction */ + if (hx > 0x3eb17218) { /* if |x| > 0.5 ln2 */ + if (hx < 0x3F851592) { /* and |x| < 1.5 ln2 */ + hi = x - ln2HI[xsb]; + lo = ln2LO[xsb]; + k = 1 - xsb - xsb; + } else { + k = invln2 * x + halF[xsb]; + t = k; + hi = x - t * ln2HI[0]; /* t*ln2HI is exact here */ + lo = t * ln2LO[0]; + } + x = hi - lo; + } else if (hx < 0x34000000) { /* when |x|<2**-23 */ + if (huge + x > one) + return one + x; /* trigger inexact */ + } + + /* x is now in primary range */ + t = x * x; + c = x - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5)))); + if (k == 0) + return one - ((x * c) / (c - 2.0f) - x); + else + y = one - ((lo - (x * c) / (2.0f - c)) - hi); + if (k >= -125) { + __uint32_t hy; + GET_FLOAT_WORD(hy, y); + SET_FLOAT_WORD(y, hy + lsl(k, 23)); /* add k to y's exponent */ + return y; + } else { + __uint32_t hy; + GET_FLOAT_WORD(hy, y); + SET_FLOAT_WORD(y, hy + lsl((k + 100), 23)); /* add k to y's exponent */ + return y * twom100; + } +} + +_MATH_ALIAS_f_f(exp) + +#else +#include "../common/sf_exp.c" +#endif /* __OBSOLETE_MATH_FLOAT */ diff --git a/src/audio/ffmpeg_dec/libm/math/sf_exp2.c b/src/audio/ffmpeg_dec/libm/math/sf_exp2.c new file mode 100644 index 000000000000..d688e6aad4da --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_exp2.c @@ -0,0 +1,35 @@ +/* wf_exp2.c -- float version of w_exp2.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * wrapper exp2f(x) + */ + +#include "fdlibm.h" +#if __OBSOLETE_MATH_FLOAT +#include +#include + +float __no_builtin +exp2f(float x) /* wrapper exp2f */ +{ + return _powf(2.0, x); +} + +_MATH_ALIAS_f_f(exp2) + +#else +#include "../common/sf_exp2.c" +#endif /* __OBSOLETE_MATH_FLOAT */ diff --git a/src/audio/ffmpeg_dec/libm/math/sf_fabs.c b/src/audio/ffmpeg_dec/libm/math/sf_fabs.c new file mode 100644 index 000000000000..5accf8f49a00 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_fabs.c @@ -0,0 +1,31 @@ +/* sf_fabs.c -- float version of s_fabs.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * fabsf(x) returns the absolute value of x. + */ + +#include "fdlibm.h" + +float +fabsf(float x) +{ + __uint32_t ix; + GET_FLOAT_WORD(ix, x); + SET_FLOAT_WORD(x, ix & 0x7fffffff); + return x; +} + +_MATH_ALIAS_f_f(fabs) diff --git a/src/audio/ffmpeg_dec/libm/math/sf_floor.c b/src/audio/ffmpeg_dec/libm/math/sf_floor.c new file mode 100644 index 000000000000..806b6625f7a6 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_floor.c @@ -0,0 +1,60 @@ +/* sf_floor.c -- float version of s_floor.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * floorf(x) + * Return x rounded toward -inf to integral value + * Method: + * Bit twiddling. + * Exception: + * Inexact flag raised if x not equal to floorf(x). + */ + +#include "fdlibm.h" + +float +floorf(float x) +{ + __int32_t i0, j0; + __uint32_t i, ix; + GET_FLOAT_WORD(i0, x); + ix = (i0 & 0x7fffffff); + j0 = (ix >> 23) - 0x7f; + if (j0 < 23) { + if (j0 < 0) { /* raise inexact if x != 0 */ + if (i0 >= 0) { + i0 = 0; + } else if (!FLT_UWORD_IS_ZERO(ix)) { + i0 = 0xbf800000; + } + } else { + i = (0x007fffff) >> j0; + if ((i0 & i) == 0) + return x; /* x is integral */ + if (i0 < 0) + i0 += (0x00800000) >> j0; + i0 &= (~i); + } + } else { + if (!FLT_UWORD_IS_FINITE(ix)) + return x + x; /* inf or NaN */ + else + return x; /* x is integral */ + } + SET_FLOAT_WORD(x, i0); + return x; +} + +_MATH_ALIAS_f_f(floor) diff --git a/src/audio/ffmpeg_dec/libm/math/sf_fmod.c b/src/audio/ffmpeg_dec/libm/math/sf_fmod.c new file mode 100644 index 000000000000..ed2aece45602 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_fmod.c @@ -0,0 +1,123 @@ +/* ef_fmod.c -- float version of e_fmod.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * fmodf(x,y) + * Return x mod y in exact arithmetic + * Method: shift and subtract + */ + +#include "fdlibm.h" + +static const float one = 1.0, Zero[] = { + 0.0, + -0.0, +}; + +float +fmodf(float x, float y) +{ + __int32_t n, hx, hy, hz, ix, iy, sx, i; + + GET_FLOAT_WORD(hx, x); + GET_FLOAT_WORD(hy, y); + sx = hx & 0x80000000; /* sign of x */ + hx ^= sx; /* |x| */ + hy &= 0x7fffffff; /* |y| */ + + /* purge off exception values */ + if (isnan(x) || isnan(y)) /* x or y nan, return nan */ + return x + y; + + if (isinf(x)) /* x == inf, domain error */ + return __math_invalidf(x); + + if (hy == 0) /* y=0, domain error */ + return __math_invalidf(y); + + if (hx < hy) + return x; /* |x|<|y| return x */ + if (hx == hy) + return Zero[(__uint32_t)sx >> 31]; /* |x|=|y| return x*0*/ + + /* Note: y cannot be zero if we reach here. */ + + /* determine ix = ilogb(x) */ + if (FLT_UWORD_IS_SUBNORMAL(hx)) { /* subnormal x */ + for (ix = -126, i = lsl(hx, 8); i > 0; i = lsl(i, 1)) + ix -= 1; + } else + ix = (hx >> 23) - 127; + + /* determine iy = ilogb(y) */ + if (FLT_UWORD_IS_SUBNORMAL(hy)) { /* subnormal y */ + for (iy = -126, i = lsl(hy, 8); i >= 0; i = lsl(i, 1)) + iy -= 1; + } else + iy = (hy >> 23) - 127; + + /* set up {hx,lx}, {hy,ly} and align y to x */ + if (ix >= -126) + hx = 0x00800000 | (0x007fffff & hx); + else { /* subnormal x, shift x to normal */ + n = -126 - ix; + hx = hx << n; + } + if (iy >= -126) + hy = 0x00800000 | (0x007fffff & hy); + else { /* subnormal y, shift y to normal */ + n = -126 - iy; + hy = hy << n; + } + + /* fix point fmod */ + n = ix - iy; + while (n--) { + hz = hx - hy; + if (hz < 0) { + hx = hx + hx; + } else { + if (hz == 0) /* return sign(x)*0 */ + return Zero[(__uint32_t)sx >> 31]; + hx = hz + hz; + } + } + hz = hx - hy; + if (hz >= 0) { + hx = hz; + } + + /* convert back to floating value and restore the sign */ + if (hx == 0) /* return sign(x)*0 */ + return Zero[(__uint32_t)sx >> 31]; + while (hx < 0x00800000) { /* normalize x */ + hx = hx + hx; + iy -= 1; + } + if (iy >= -126) { /* normalize output */ + hx = ((hx - 0x00800000) | ((iy + 127) << 23)); + SET_FLOAT_WORD(x, hx | sx); + } else { /* subnormal output */ + /* If denormals are not supported, this code will generate a + zero representation. */ + n = -126 - iy; + hx >>= n; + SET_FLOAT_WORD(x, hx | sx); + x *= one; /* create necessary signal */ + } + return x; /* exact output */ +} + +_MATH_ALIAS_f_ff(fmod) diff --git a/src/audio/ffmpeg_dec/libm/math/sf_frexp.c b/src/audio/ffmpeg_dec/libm/math/sf_frexp.c new file mode 100644 index 000000000000..e3be2b66a4f2 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_frexp.c @@ -0,0 +1,41 @@ +/* sf_frexp.c -- float version of s_frexp.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" + +static const float two25 = 3.3554432000e+07; /* 0x4c000000 */ + +float +frexpf(float x, int *eptr) +{ + __int32_t hx, ix; + GET_FLOAT_WORD(hx, x); + ix = 0x7fffffff & hx; + *eptr = 0; + if (!FLT_UWORD_IS_FINITE(ix) || FLT_UWORD_IS_ZERO(ix)) + return x + x; /* 0,inf,nan */ + if (FLT_UWORD_IS_SUBNORMAL(ix)) { /* subnormal */ + x *= two25; + GET_FLOAT_WORD(hx, x); + ix = hx & 0x7fffffff; + *eptr = -25; + } + *eptr += (ix >> 23) - 126; + hx = (hx & 0x807fffff) | 0x3f000000; + SET_FLOAT_WORD(x, hx); + return x; +} + +_MATH_ALIAS_f_fI(frexp) diff --git a/src/audio/ffmpeg_dec/libm/math/sf_hypot.c b/src/audio/ffmpeg_dec/libm/math/sf_hypot.c new file mode 100644 index 000000000000..7f7fcb5a4ecf --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_hypot.c @@ -0,0 +1,92 @@ +/* ef_hypot.c -- float version of e_hypot.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" + +float +hypotf(float x, float y) +{ + float a = x, b = y, t1, t2, y1, y2, w; + __int32_t j, k, ha, hb; + + GET_FLOAT_WORD(ha, x); + ha &= 0x7fffffffL; + GET_FLOAT_WORD(hb, y); + hb &= 0x7fffffffL; + if (hb > ha) { + j = ha; + ha = hb; + hb = j; + } + SET_FLOAT_WORD(a, ha); /* a <- |a| */ + SET_FLOAT_WORD(b, hb); /* b <- |b| */ + if ((ha - hb) > 0xf000000L) { + return a + b; + } /* x/y > 2**30 */ + k = 0; + if (ha > 0x58800000L) { /* a>2**50 */ + if (!FLT_UWORD_IS_FINITE(ha)) { /* Inf or NaN */ + w = a + b; /* for sNaN */ + if (FLT_UWORD_IS_INFINITE(ha) && !issignaling(b)) + w = a; + if (FLT_UWORD_IS_INFINITE(hb) && !issignaling(a)) + w = b; + return w; + } + /* scale a and b by 2**-68 */ + ha -= 0x22000000L; + hb -= 0x22000000L; + k += 68; + SET_FLOAT_WORD(a, ha); + SET_FLOAT_WORD(b, hb); + } + if (hb < 0x26800000L) { /* b < 2**-50 */ + if (FLT_UWORD_IS_ZERO(hb)) { + return a; + } else if (FLT_UWORD_IS_SUBNORMAL(hb)) { + SET_FLOAT_WORD(t1, 0x7e800000L); /* t1=2^126 */ + b *= t1; + a *= t1; + k -= 126; + } else { /* scale a and b by 2^68 */ + ha += 0x22000000; /* a *= 2^68 */ + hb += 0x22000000; /* b *= 2^68 */ + k -= 68; + SET_FLOAT_WORD(a, ha); + SET_FLOAT_WORD(b, hb); + } + } + /* medium size a and b */ + w = a - b; + if (w > b) { + SET_FLOAT_WORD(t1, ha & 0xfffff000L); + t2 = a - t1; + w = sqrtf(t1 * t1 - (b * (-b) - t2 * (a + t1))); + } else { + a = a + a; + SET_FLOAT_WORD(y1, hb & 0xfffff000L); + y2 = b - y1; + SET_FLOAT_WORD(t1, (ha + 0x00800000L) & 0xfffff000UL); + t2 = a - t1; + w = sqrtf(t1 * y1 - (w * (-w) - (t1 * y2 + t2 * b))); + } + if (k != 0) { + SET_FLOAT_WORD(t1, 0x3f800000L + lsl(k, 23)); + w *= t1; + } + return check_oflowf(w); +} + +_MATH_ALIAS_f_ff(hypot) diff --git a/src/audio/ffmpeg_dec/libm/math/sf_j0.c b/src/audio/ffmpeg_dec/libm/math/sf_j0.c new file mode 100644 index 000000000000..1456dfcc3393 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_j0.c @@ -0,0 +1,389 @@ +/* ef_j0.c -- float version of e_j0.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" + +static float pzerof(float), qzerof(float); + +static const float huge = 1e30, one = 1.0, + invsqrtpi = 5.6418961287e-01, /* 0x3f106ebb */ + tpi = 6.3661974669e-01, /* 0x3f22f983 */ + /* R0/S0 on [0, 2.00] */ + R02 = 1.5625000000e-02, /* 0x3c800000 */ + R03 = -1.8997929874e-04, /* 0xb947352e */ + R04 = 1.8295404516e-06, /* 0x35f58e88 */ + R05 = -4.6183270541e-09, /* 0xb19eaf3c */ + S01 = 1.5619102865e-02, /* 0x3c7fe744 */ + S02 = 1.1692678527e-04, /* 0x38f53697 */ + S03 = 5.1354652442e-07, /* 0x3509daa6 */ + S04 = 1.1661400734e-09; /* 0x30a045e8 */ + +static const float zero = 0.0; + +float +j0f(float x) +{ + float z, s, c, ss, cc, r, u, v; + __int32_t hx, ix; + + if (isnan(x)) + return x + x; + + if (isinf(x)) + return zero; + + GET_FLOAT_WORD(hx, x); + ix = hx & 0x7fffffff; + x = fabsf(x); + if (ix >= 0x40000000) { /* |x| >= 2.0 */ + s = sinf(x); + c = cosf(x); + ss = s - c; + cc = s + c; + if (ix <= FLT_UWORD_HALF_MAX) { /* make sure x+x not overflow */ + z = -cosf(x + x); + if ((s * c) < zero) + cc = z / ss; + else + ss = z / cc; + } + /* + * j0(x) = 1/sqrt(pi) * (P(0,x)*cc - Q(0,x)*ss) / sqrt(x) + * y0(x) = 1/sqrt(pi) * (P(0,x)*ss + Q(0,x)*cc) / sqrt(x) + */ + if (ix > 0x5c000000) + z = (invsqrtpi * cc) / sqrtf(x); + else { + u = pzerof(x); + v = qzerof(x); + z = invsqrtpi * (u * cc - v * ss) / sqrtf(x); + } + return z; + } + if (ix < 0x39000000) { /* |x| < 2**-13 */ + if (huge + x > one) { /* raise inexact if x != 0 */ + if (ix < 0x32000000) + return one; /* |x|<2**-27 */ + else + return one - (float)0.25 * x * x; + } + } + z = x * x; + r = z * (R02 + z * (R03 + z * (R04 + z * R05))); + s = one + z * (S01 + z * (S02 + z * (S03 + z * S04))); + if (ix < 0x3F800000) { /* |x| < 1.00 */ + return one + z * ((float)-0.25 + (r / s)); + } else { + u = (float)0.5 * x; + return ((one + u) * (one - u) + z * (r / s)); + } +} + +static const float u00 = -7.3804296553e-02, /* 0xbd9726b5 */ + u01 = 1.7666645348e-01, /* 0x3e34e80d */ + u02 = -1.3818567619e-02, /* 0xbc626746 */ + u03 = 3.4745343146e-04, /* 0x39b62a69 */ + u04 = -3.8140706238e-06, /* 0xb67ff53c */ + u05 = 1.9559013964e-08, /* 0x32a802ba */ + u06 = -3.9820518410e-11, /* 0xae2f21eb */ + v01 = 1.2730483897e-02, /* 0x3c509385 */ + v02 = 7.6006865129e-05, /* 0x389f65e0 */ + v03 = 2.5915085189e-07, /* 0x348b216c */ + v04 = 4.4111031494e-10; /* 0x2ff280c2 */ + +float +y0f(float x) +{ + float z, s, c, ss, cc, u, v; + __int32_t hx, ix; + + GET_FLOAT_WORD(hx, x); + ix = 0x7fffffff & hx; + + if (ix == 0) + return __math_divzerof(1); + + if (ix > 0x7f800000) + return x + x; + + if (hx < 0) + return __math_invalidf(x); + + if (ix == 0x7f800000) + return zero; + + if (ix >= 0x40000000) { /* |x| >= 2.0 */ + /* y0(x) = sqrt(2/(pi*x))*(p0(x)*sin(x0)+q0(x)*cos(x0)) + * where x0 = x-pi/4 + * Better formula: + * cos(x0) = cos(x)cos(pi/4)+sin(x)sin(pi/4) + * = 1/sqrt(2) * (sin(x) + cos(x)) + * sin(x0) = sin(x)cos(3pi/4)-cos(x)sin(3pi/4) + * = 1/sqrt(2) * (sin(x) - cos(x)) + * To avoid cancellation, use + * sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x)) + * to compute the worse one. + */ + s = sinf(x); + c = cosf(x); + ss = s - c; + cc = s + c; + /* + * j0(x) = 1/sqrt(pi) * (P(0,x)*cc - Q(0,x)*ss) / sqrt(x) + * y0(x) = 1/sqrt(pi) * (P(0,x)*ss + Q(0,x)*cc) / sqrt(x) + */ + if (ix <= FLT_UWORD_HALF_MAX) { /* make sure x+x not overflow */ + z = -cosf(x + x); + if ((s * c) < zero) + cc = z / ss; + else + ss = z / cc; + } + if (ix > 0x5c000000) + z = (invsqrtpi * ss) / sqrtf(x); + else { + u = pzerof(x); + v = qzerof(x); + z = invsqrtpi * (u * ss + v * cc) / sqrtf(x); + } + return z; + } + if (ix <= 0x39800000) { /* x < 2**-27 */ + return (u00 + tpi * logf(x)); + } + z = x * x; + u = u00 + + z * (u01 + z * (u02 + z * (u03 + z * (u04 + z * (u05 + z * u06))))); + v = one + z * (v01 + z * (v02 + z * (v03 + z * v04))); + return (u / v + tpi * (j0f(x) * logf(x))); +} + +/* The asymptotic expansions of pzero is + * 1 - 9/128 s^2 + 11025/98304 s^4 - ..., where s = 1/x. + * For x >= 2, We approximate pzero by + * pzero(x) = 1 + (R/S) + * where R = pR0 + pR1*s^2 + pR2*s^4 + ... + pR5*s^10 + * S = 1 + pS0*s^2 + ... + pS4*s^10 + * and + * | pzero(x)-1-R/S | <= 2 ** ( -60.26) + */ +static const float pR8[6] = { + /* for x in [inf, 8]=1/[0,0.125] */ + 0.0000000000e+00, /* 0x00000000 */ + -7.0312500000e-02, /* 0xbd900000 */ + -8.0816707611e+00, /* 0xc1014e86 */ + -2.5706311035e+02, /* 0xc3808814 */ + -2.4852163086e+03, /* 0xc51b5376 */ + -5.2530439453e+03, /* 0xc5a4285a */ +}; +static const float pS8[5] = { + 1.1653436279e+02, /* 0x42e91198 */ + 3.8337448730e+03, /* 0x456f9beb */ + 4.0597855469e+04, /* 0x471e95db */ + 1.1675296875e+05, /* 0x47e4087c */ + 4.7627726562e+04, /* 0x473a0bba */ +}; +static const float pR5[6] = { + /* for x in [8,4.5454]=1/[0.125,0.22001] */ + -1.1412546255e-11, /* 0xad48c58a */ + -7.0312492549e-02, /* 0xbd8fffff */ + -4.1596107483e+00, /* 0xc0851b88 */ + -6.7674766541e+01, /* 0xc287597b */ + -3.3123129272e+02, /* 0xc3a59d9b */ + -3.4643338013e+02, /* 0xc3ad3779 */ +}; +static const float pS5[5] = { + 6.0753936768e+01, /* 0x42730408 */ + 1.0512523193e+03, /* 0x44836813 */ + 5.9789707031e+03, /* 0x45bad7c4 */ + 9.6254453125e+03, /* 0x461665c8 */ + 2.4060581055e+03, /* 0x451660ee */ +}; + +static const float pR3[6] = { + /* for x in [4.547,2.8571]=1/[0.2199,0.35001] */ + -2.5470459075e-09, /* 0xb12f081b */ + -7.0311963558e-02, /* 0xbd8fffb8 */ + -2.4090321064e+00, /* 0xc01a2d95 */ + -2.1965976715e+01, /* 0xc1afba52 */ + -5.8079170227e+01, /* 0xc2685112 */ + -3.1447946548e+01, /* 0xc1fb9565 */ +}; +static const float pS3[5] = { + 3.5856033325e+01, /* 0x420f6c94 */ + 3.6151397705e+02, /* 0x43b4c1ca */ + 1.1936077881e+03, /* 0x44953373 */ + 1.1279968262e+03, /* 0x448cffe6 */ + 1.7358093262e+02, /* 0x432d94b8 */ +}; + +static const float pR2[6] = { + /* for x in [2.8570,2]=1/[0.3499,0.5] */ + -8.8753431271e-08, /* 0xb3be98b7 */ + -7.0303097367e-02, /* 0xbd8ffb12 */ + -1.4507384300e+00, /* 0xbfb9b1cc */ + -7.6356959343e+00, /* 0xc0f4579f */ + -1.1193166733e+01, /* 0xc1331736 */ + -3.2336456776e+00, /* 0xc04ef40d */ +}; +static const float pS2[5] = { + 2.2220300674e+01, /* 0x41b1c32d */ + 1.3620678711e+02, /* 0x430834f0 */ + 2.7047027588e+02, /* 0x43873c32 */ + 1.5387539673e+02, /* 0x4319e01a */ + 1.4657617569e+01, /* 0x416a859a */ +}; + +static float +pzerof(float x) +{ + const float *p, *q; + float z, r, s; + __int32_t ix; + GET_FLOAT_WORD(ix, x); + ix &= 0x7fffffff; + if (ix >= 0x41000000) { + p = pR8; + q = pS8; + } else if (ix >= 0x40f71c58) { + p = pR5; + q = pS5; + } else if (ix >= 0x4036db68) { + p = pR3; + q = pS3; + } else { + p = pR2; + q = pS2; + } + z = one / (x * x); + r = p[0] + z * (p[1] + z * (p[2] + z * (p[3] + z * (p[4] + z * p[5])))); + s = one + z * (q[0] + z * (q[1] + z * (q[2] + z * (q[3] + z * q[4])))); + return one + r / s; +} + +/* For x >= 8, the asymptotic expansions of qzero is + * -1/8 s + 75/1024 s^3 - ..., where s = 1/x. + * We approximate qzero by + * qzero(x) = s*(-1.25 + (R/S)) + * where R = qR0 + qR1*s^2 + qR2*s^4 + ... + qR5*s^10 + * S = 1 + qS0*s^2 + ... + qS5*s^12 + * and + * | qzero(x)/s +1.25-R/S | <= 2 ** ( -61.22) + */ +static const float qR8[6] = { + /* for x in [inf, 8]=1/[0,0.125] */ + 0.0000000000e+00, /* 0x00000000 */ + 7.3242187500e-02, /* 0x3d960000 */ + 1.1768206596e+01, /* 0x413c4a93 */ + 5.5767340088e+02, /* 0x440b6b19 */ + 8.8591972656e+03, /* 0x460a6cca */ + 3.7014625000e+04, /* 0x471096a0 */ +}; +static const float qS8[6] = { + 1.6377603149e+02, /* 0x4323c6aa */ + 8.0983447266e+03, /* 0x45fd12c2 */ + 1.4253829688e+05, /* 0x480b3293 */ + 8.0330925000e+05, /* 0x49441ed4 */ + 8.4050156250e+05, /* 0x494d3359 */ + -3.4389928125e+05, /* 0xc8a7eb69 */ +}; + +static const float qR5[6] = { + /* for x in [8,4.5454]=1/[0.125,0.22001] */ + 1.8408595828e-11, /* 0x2da1ec79 */ + 7.3242180049e-02, /* 0x3d95ffff */ + 5.8356351852e+00, /* 0x40babd86 */ + 1.3511157227e+02, /* 0x43071c90 */ + 1.0272437744e+03, /* 0x448067cd */ + 1.9899779053e+03, /* 0x44f8bf4b */ +}; +static const float qS5[6] = { + 8.2776611328e+01, /* 0x42a58da0 */ + 2.0778142090e+03, /* 0x4501dd07 */ + 1.8847289062e+04, /* 0x46933e94 */ + 5.6751113281e+04, /* 0x475daf1d */ + 3.5976753906e+04, /* 0x470c88c1 */ + -5.3543427734e+03, /* 0xc5a752be */ +}; + +static const float qR3[6] = { + /* for x in [4.547,2.8571]=1/[0.2199,0.35001] */ + 4.3774099900e-09, /* 0x3196681b */ + 7.3241114616e-02, /* 0x3d95ff70 */ + 3.3442313671e+00, /* 0x405607e3 */ + 4.2621845245e+01, /* 0x422a7cc5 */ + 1.7080809021e+02, /* 0x432acedf */ + 1.6673394775e+02, /* 0x4326bbe4 */ +}; +static const float qS3[6] = { + 4.8758872986e+01, /* 0x42430916 */ + 7.0968920898e+02, /* 0x44316c1c */ + 3.7041481934e+03, /* 0x4567825f */ + 6.4604252930e+03, /* 0x45c9e367 */ + 2.5163337402e+03, /* 0x451d4557 */ + -1.4924745178e+02, /* 0xc3153f59 */ +}; + +static const float qR2[6] = { + /* for x in [2.8570,2]=1/[0.3499,0.5] */ + 1.5044444979e-07, /* 0x342189db */ + 7.3223426938e-02, /* 0x3d95f62a */ + 1.9981917143e+00, /* 0x3fffc4bf */ + 1.4495602608e+01, /* 0x4167edfd */ + 3.1666231155e+01, /* 0x41fd5471 */ + 1.6252708435e+01, /* 0x4182058c */ +}; +static const float qS2[6] = { + 3.0365585327e+01, /* 0x41f2ecb8 */ + 2.6934811401e+02, /* 0x4386ac8f */ + 8.4478375244e+02, /* 0x44533229 */ + 8.8293585205e+02, /* 0x445cbbe5 */ + 2.1266638184e+02, /* 0x4354aa98 */ + -5.3109550476e+00, /* 0xc0a9f358 */ +}; + +static float +qzerof(float x) +{ + const float *p, *q; + float s, r, z; + __int32_t ix; + GET_FLOAT_WORD(ix, x); + ix &= 0x7fffffff; + if (ix >= 0x41000000) { + p = qR8; + q = qS8; + } else if (ix >= 0x40f71c58) { + p = qR5; + q = qS5; + } else if (ix >= 0x4036db68) { + p = qR3; + q = qS3; + } else { + p = qR2; + q = qS2; + } + z = one / (x * x); + r = p[0] + z * (p[1] + z * (p[2] + z * (p[3] + z * (p[4] + z * p[5])))); + s = one + + z * (q[0] + + z * (q[1] + z * (q[2] + z * (q[3] + z * (q[4] + z * q[5]))))); + return (-(float).125 + r / s) / x; +} + +_MATH_ALIAS_f_f(j0) + +_MATH_ALIAS_f_f(y0) + diff --git a/src/audio/ffmpeg_dec/libm/math/sf_j1.c b/src/audio/ffmpeg_dec/libm/math/sf_j1.c new file mode 100644 index 000000000000..5776b96802fa --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_j1.c @@ -0,0 +1,385 @@ +/* ef_j1.c -- float version of e_j1.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" + +static float ponef(float), qonef(float); + +static const float one = 1.0, + invsqrtpi = 5.6418961287e-01, /* 0x3f106ebb */ + tpi = 6.3661974669e-01, /* 0x3f22f983 */ + /* R0/S0 on [0,2] */ + r00 = -6.2500000000e-02, /* 0xbd800000 */ + r01 = 1.4070566976e-03, /* 0x3ab86cfd */ + r02 = -1.5995563444e-05, /* 0xb7862e36 */ + r03 = 4.9672799207e-08, /* 0x335557d2 */ + s01 = 1.9153760746e-02, /* 0x3c9ce859 */ + s02 = 1.8594678841e-04, /* 0x3942fab6 */ + s03 = 1.1771846857e-06, /* 0x359dffc2 */ + s04 = 5.0463624390e-09, /* 0x31ad6446 */ + s05 = 1.2354227016e-11; /* 0x2d59567e */ + +static const float zero = 0.0; + +float +j1f(float x) +{ + float z, s, c, ss, cc, r, u, v, y; + __int32_t hx, ix; + + if (isnan(x)) + return x + x; + + if (isinf(x)) + return zero; + + GET_FLOAT_WORD(hx, x); + ix = hx & 0x7fffffff; + + y = fabsf(x); + if (ix >= 0x40000000) { /* |x| >= 2.0 */ + s = sinf(y); + c = cosf(y); + ss = -s - c; + cc = s - c; + if (ix <= FLT_UWORD_HALF_MAX) { /* make sure y+y not overflow */ + z = cosf(y + y); + if ((s * c) > zero) + cc = z / ss; + else + ss = z / cc; + } + /* + * j1(x) = 1/sqrt(pi) * (P(1,x)*cc - Q(1,x)*ss) / sqrt(x) + * y1(x) = 1/sqrt(pi) * (P(1,x)*ss + Q(1,x)*cc) / sqrt(x) + */ + if (ix > 0x5c000000) + z = (invsqrtpi * cc) / sqrtf(y); + else { + u = ponef(y); + v = qonef(y); + z = invsqrtpi * (u * cc - v * ss) / sqrtf(y); + } + if (hx < 0) + return -z; + else + return z; + } + if (ix < 0x32000000) { /* |x|<2**-27 */ + if (ix == 0) + return x; + return check_uflowf(0.5f * x); /* inexact if x!=0 necessary */ + } + z = x * x; + r = z * (r00 + z * (r01 + z * (r02 + z * r03))); + s = one + z * (s01 + z * (s02 + z * (s03 + z * (s04 + z * s05)))); + r *= x; + return (x * (float)0.5 + r / s); +} + +static const float U0[5] = { + -1.9605709612e-01, /* 0xbe48c331 */ + 5.0443872809e-02, /* 0x3d4e9e3c */ + -1.9125689287e-03, /* 0xbafaaf2a */ + 2.3525259166e-05, /* 0x37c5581c */ + -9.1909917899e-08, /* 0xb3c56003 */ +}; +static const float V0[5] = { + 1.9916731864e-02, /* 0x3ca3286a */ + 2.0255257550e-04, /* 0x3954644b */ + 1.3560879779e-06, /* 0x35b602d4 */ + 6.2274145840e-09, /* 0x31d5f8eb */ + 1.6655924903e-11, /* 0x2d9281cf */ +}; + +float +y1f(float x) +{ + float z, s, c, ss, cc, u, v; + __int32_t hx, ix; + + GET_FLOAT_WORD(hx, x); + ix = 0x7fffffff & hx; + + if (ix == 0) + return __math_divzerof(1); + + if (ix > 0x7f800000) + return x+x; + + if (hx < 0) + return __math_invalidf(x); + + if (ix == 0x7f800000) + return zero; + + if (ix >= 0x40000000) { /* |x| >= 2.0 */ + s = sinf(x); + c = cosf(x); + ss = -s - c; + cc = s - c; + if (ix <= FLT_UWORD_HALF_MAX) { /* make sure x+x not overflow */ + z = cosf(x + x); + if ((s * c) > zero) + cc = z / ss; + else + ss = z / cc; + } + /* y1(x) = sqrt(2/(pi*x))*(p1(x)*sin(x0)+q1(x)*cos(x0)) + * where x0 = x-3pi/4 + * Better formula: + * cos(x0) = cos(x)cos(3pi/4)+sin(x)sin(3pi/4) + * = 1/sqrt(2) * (sin(x) - cos(x)) + * sin(x0) = sin(x)cos(3pi/4)-cos(x)sin(3pi/4) + * = -1/sqrt(2) * (cos(x) + sin(x)) + * To avoid cancellation, use + * sin(x) +- cos(x) = -cos(2x)/(sin(x) -+ cos(x)) + * to compute the worse one. + */ + if (ix > 0x5c000000) + z = (invsqrtpi * ss) / sqrtf(x); + else { + u = ponef(x); + v = qonef(x); + z = invsqrtpi * (u * ss + v * cc) / sqrtf(x); + } + return z; + } + if (ix <= 0x24800000) { /* x < 2**-54 */ + return check_oflowf(-tpi / x); + } + z = x * x; + u = U0[0] + z * (U0[1] + z * (U0[2] + z * (U0[3] + z * U0[4]))); + v = one + z * (V0[0] + z * (V0[1] + z * (V0[2] + z * (V0[3] + z * V0[4])))); + return (x * (u / v) + tpi * (j1f(x) * logf(x) - one / x)); +} + +/* For x >= 8, the asymptotic expansions of pone is + * 1 + 15/128 s^2 - 4725/2^15 s^4 - ..., where s = 1/x. + * We approximate pone by + * pone(x) = 1 + (R/S) + * where R = pr0 + pr1*s^2 + pr2*s^4 + ... + pr5*s^10 + * S = 1 + ps0*s^2 + ... + ps4*s^10 + * and + * | pone(x)-1-R/S | <= 2 ** ( -60.06) + */ + +static const float pr8[6] = { + /* for x in [inf, 8]=1/[0,0.125] */ + 0.0000000000e+00, /* 0x00000000 */ + 1.1718750000e-01, /* 0x3df00000 */ + 1.3239480972e+01, /* 0x4153d4ea */ + 4.1205184937e+02, /* 0x43ce06a3 */ + 3.8747453613e+03, /* 0x45722bed */ + 7.9144794922e+03, /* 0x45f753d6 */ +}; +static const float ps8[5] = { + 1.1420736694e+02, /* 0x42e46a2c */ + 3.6509309082e+03, /* 0x45642ee5 */ + 3.6956207031e+04, /* 0x47105c35 */ + 9.7602796875e+04, /* 0x47bea166 */ + 3.0804271484e+04, /* 0x46f0a88b */ +}; + +static const float pr5[6] = { + /* for x in [8,4.5454]=1/[0.125,0.22001] */ + 1.3199052094e-11, /* 0x2d68333f */ + 1.1718749255e-01, /* 0x3defffff */ + 6.8027510643e+00, /* 0x40d9b023 */ + 1.0830818176e+02, /* 0x42d89dca */ + 5.1763616943e+02, /* 0x440168b7 */ + 5.2871520996e+02, /* 0x44042dc6 */ +}; +static const float ps5[5] = { + 5.9280597687e+01, /* 0x426d1f55 */ + 9.9140142822e+02, /* 0x4477d9b1 */ + 5.3532670898e+03, /* 0x45a74a23 */ + 7.8446904297e+03, /* 0x45f52586 */ + 1.5040468750e+03, /* 0x44bc0180 */ +}; + +static const float pr3[6] = { + 3.0250391081e-09, /* 0x314fe10d */ + 1.1718686670e-01, /* 0x3defffab */ + 3.9329774380e+00, /* 0x407bb5e7 */ + 3.5119403839e+01, /* 0x420c7a45 */ + 9.1055007935e+01, /* 0x42b61c2a */ + 4.8559066772e+01, /* 0x42423c7c */ +}; +static const float ps3[5] = { + 3.4791309357e+01, /* 0x420b2a4d */ + 3.3676245117e+02, /* 0x43a86198 */ + 1.0468714600e+03, /* 0x4482dbe3 */ + 8.9081134033e+02, /* 0x445eb3ed */ + 1.0378793335e+02, /* 0x42cf936c */ +}; + +static const float pr2[6] = { + /* for x in [2.8570,2]=1/[0.3499,0.5] */ + 1.0771083225e-07, /* 0x33e74ea8 */ + 1.1717621982e-01, /* 0x3deffa16 */ + 2.3685150146e+00, /* 0x401795c0 */ + 1.2242610931e+01, /* 0x4143e1bc */ + 1.7693971634e+01, /* 0x418d8d41 */ + 5.0735230446e+00, /* 0x40a25a4d */ +}; +static const float ps2[5] = { + 2.1436485291e+01, /* 0x41ab7dec */ + 1.2529022980e+02, /* 0x42fa9499 */ + 2.3227647400e+02, /* 0x436846c7 */ + 1.1767937469e+02, /* 0x42eb5bd7 */ + 8.3646392822e+00, /* 0x4105d590 */ +}; + +static float +ponef(float x) +{ + const float *p, *q; + float z, r, s; + __int32_t ix; + GET_FLOAT_WORD(ix, x); + ix &= 0x7fffffff; + if (ix >= 0x41000000) { + p = pr8; + q = ps8; + } else if (ix >= 0x40f71c58) { + p = pr5; + q = ps5; + } else if (ix >= 0x4036db68) { + p = pr3; + q = ps3; + } else { + p = pr2; + q = ps2; + } + z = one / (x * x); + r = p[0] + z * (p[1] + z * (p[2] + z * (p[3] + z * (p[4] + z * p[5])))); + s = one + z * (q[0] + z * (q[1] + z * (q[2] + z * (q[3] + z * q[4])))); + return one + r / s; +} + +/* For x >= 8, the asymptotic expansions of qone is + * 3/8 s - 105/1024 s^3 - ..., where s = 1/x. + * We approximate qone by + * qone(x) = s*(0.375 + (R/S)) + * where R = qr1*s^2 + qr2*s^4 + ... + qr5*s^10 + * S = 1 + qs1*s^2 + ... + qs6*s^12 + * and + * | qone(x)/s -0.375-R/S | <= 2 ** ( -61.13) + */ + +static const float qr8[6] = { + /* for x in [inf, 8]=1/[0,0.125] */ + 0.0000000000e+00, /* 0x00000000 */ + -1.0253906250e-01, /* 0xbdd20000 */ + -1.6271753311e+01, /* 0xc1822c8d */ + -7.5960174561e+02, /* 0xc43de683 */ + -1.1849806641e+04, /* 0xc639273a */ + -4.8438511719e+04, /* 0xc73d3683 */ +}; +static const float qs8[6] = { + 1.6139537048e+02, /* 0x43216537 */ + 7.8253862305e+03, /* 0x45f48b17 */ + 1.3387534375e+05, /* 0x4802bcd6 */ + 7.1965775000e+05, /* 0x492fb29c */ + 6.6660125000e+05, /* 0x4922be94 */ + -2.9449025000e+05, /* 0xc88fcb48 */ +}; + +static const float qr5[6] = { + /* for x in [8,4.5454]=1/[0.125,0.22001] */ + -2.0897993405e-11, /* 0xadb7d219 */ + -1.0253904760e-01, /* 0xbdd1fffe */ + -8.0564479828e+00, /* 0xc100e736 */ + -1.8366960144e+02, /* 0xc337ab6b */ + -1.3731937256e+03, /* 0xc4aba633 */ + -2.6124443359e+03, /* 0xc523471c */ +}; +static const float qs5[6] = { + 8.1276550293e+01, /* 0x42a28d98 */ + 1.9917987061e+03, /* 0x44f8f98f */ + 1.7468484375e+04, /* 0x468878f8 */ + 4.9851425781e+04, /* 0x4742bb6d */ + 2.7948074219e+04, /* 0x46da5826 */ + -4.7191835938e+03, /* 0xc5937978 */ +}; + +static const float qr3[6] = { + -5.0783124372e-09, /* 0xb1ae7d4f */ + -1.0253783315e-01, /* 0xbdd1ff5b */ + -4.6101160049e+00, /* 0xc0938612 */ + -5.7847221375e+01, /* 0xc267638e */ + -2.2824453735e+02, /* 0xc3643e9a */ + -2.1921012878e+02, /* 0xc35b35cb */ +}; +static const float qs3[6] = { + 4.7665153503e+01, /* 0x423ea91e */ + 6.7386511230e+02, /* 0x4428775e */ + 3.3801528320e+03, /* 0x45534272 */ + 5.5477290039e+03, /* 0x45ad5dd5 */ + 1.9031191406e+03, /* 0x44ede3d0 */ + -1.3520118713e+02, /* 0xc3073381 */ +}; + +static const float qr2[6] = { + /* for x in [2.8570,2]=1/[0.3499,0.5] */ + -1.7838172539e-07, /* 0xb43f8932 */ + -1.0251704603e-01, /* 0xbdd1f475 */ + -2.7522056103e+00, /* 0xc0302423 */ + -1.9663616180e+01, /* 0xc19d4f16 */ + -4.2325313568e+01, /* 0xc2294d1f */ + -2.1371921539e+01, /* 0xc1aaf9b2 */ +}; +static const float qs2[6] = { + 2.9533363342e+01, /* 0x41ec4454 */ + 2.5298155212e+02, /* 0x437cfb47 */ + 7.5750280762e+02, /* 0x443d602e */ + 7.3939318848e+02, /* 0x4438d92a */ + 1.5594900513e+02, /* 0x431bf2f2 */ + -4.9594988823e+00, /* 0xc09eb437 */ +}; + +static float +qonef(float x) +{ + const float *p, *q; + float s, r, z; + __int32_t ix; + GET_FLOAT_WORD(ix, x); + ix &= 0x7fffffff; + if (ix >= 0x40200000) { + p = qr8; + q = qs8; + } else if (ix >= 0x40f71c58) { + p = qr5; + q = qs5; + } else if (ix >= 0x4036db68) { + p = qr3; + q = qs3; + } else { + p = qr2; + q = qs2; + } + z = one / (x * x); + r = p[0] + z * (p[1] + z * (p[2] + z * (p[3] + z * (p[4] + z * p[5])))); + s = one + + z * (q[0] + + z * (q[1] + z * (q[2] + z * (q[3] + z * (q[4] + z * q[5]))))); + return ((float).375 + r / s) / x; +} + +_MATH_ALIAS_f_f(j1) + +_MATH_ALIAS_f_f(y1) diff --git a/src/audio/ffmpeg_dec/libm/math/sf_jn.c b/src/audio/ffmpeg_dec/libm/math/sf_jn.c new file mode 100644 index 000000000000..abe1e7be47ff --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_jn.c @@ -0,0 +1,220 @@ +/* ef_jn.c -- float version of e_jn.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" + +static const float two = 2.0000000000e+00, /* 0x40000000 */ + one = 1.0000000000e+00; /* 0x3F800000 */ + +static const float zero = 0.0000000000e+00; + +float +jnf(int n, float x) +{ + __int32_t i, hx, ix, sgn; + float a, b, temp, di; + float z, w; + + /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x) + * Thus, J(-n,x) = J(n,-x) + */ + GET_FLOAT_WORD(hx, x); + ix = 0x7fffffff & hx; + /* if J(n,NaN) is NaN */ + if (FLT_UWORD_IS_NAN(ix)) + return x + x; + if (n < 0) { + n = -n; + x = -x; + hx ^= 0x80000000; + } + if (n == 0) + return (j0f(x)); + if (n == 1) + return (j1f(x)); + sgn = (n & 1) & (hx >> 31); /* even n -- 0, odd n -- sign(x) */ + x = fabsf(x); + if (FLT_UWORD_IS_ZERO(ix) || FLT_UWORD_IS_INFINITE(ix)) + b = zero; + else if ((float)n <= x) { + /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */ + a = j0f(x); + b = j1f(x); + for (i = 1; i < n; i++) { + temp = b; + b = b * ((float)(i + i) / x) - a; /* avoid underflow */ + a = temp; + } + } else { + if (ix < 0x30800000) { /* x < 2**-29 */ + /* x is tiny, return the first Taylor expansion of J(n,x) + * J(n,x) = 1/n!*(x/2)^n - ... + */ + if (n > 33) /* underflow */ + b = zero; + else { + temp = x * (float)0.5; + b = temp; + for (a = one, i = 2; i <= n; i++) { + a *= (float)i; /* a = n! */ + b *= temp; /* b = (x/2)^n */ + } + b = b / a; + } + } else { + /* use backward recurrence */ + /* x x^2 x^2 + * J(n,x)/J(n-1,x) = ---- ------ ------ ..... + * 2n - 2(n+1) - 2(n+2) + * + * 1 1 1 + * (for large x) = ---- ------ ------ ..... + * 2n 2(n+1) 2(n+2) + * -- - ------ - ------ - + * x x x + * + * Let w = 2n/x and h=2/x, then the above quotient + * is equal to the continued fraction: + * 1 + * = ----------------------- + * 1 + * w - ----------------- + * 1 + * w+h - --------- + * w+2h - ... + * + * To determine how many terms needed, let + * Q(0) = w, Q(1) = w(w+h) - 1, + * Q(k) = (w+k*h)*Q(k-1) - Q(k-2), + * When Q(k) > 1e4 good for single + * When Q(k) > 1e9 good for double + * When Q(k) > 1e17 good for quadruple + */ + /* determine k */ + float t, v; + float q0, q1, h, tmp; + __int32_t k, m; + w = (n + n) / (float)x; + h = (float)2.0 / (float)x; + q0 = w; + z = w + h; + q1 = w * z - (float)1.0; + k = 1; + while (q1 < (float)1.0e9) { + k += 1; + z += h; + tmp = z * q1 - q0; + q0 = q1; + q1 = tmp; + } + m = n + n; + for (t = zero, i = 2 * (n + k); i >= m; i -= 2) + t = one / (i / x - t); + a = t; + b = one; + /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n) + * Hence, if n*(log(2n/x)) > ... + * single 8.8722839355e+01 + * double 7.09782712893383973096e+02 + * long double 1.1356523406294143949491931077970765006170e+04 + * then recurrent value may overflow and the result is + * likely underflow to zero + */ + tmp = n; + v = two / x; + tmp = tmp * logf(fabsf(v * tmp)); + if (tmp < (float)8.8721679688e+01) { + for (i = n - 1, di = (float)(i + i); i > 0; i--) { + temp = b; + b *= di; + b = b / x - a; + a = temp; + di -= two; + } + } else { + for (i = n - 1, di = (float)(i + i); i > 0; i--) { + temp = b; + b *= di; + b = b / x - a; + a = temp; + di -= two; + /* scale b to avoid spurious overflow */ + if (b > (float)1e10) { + a /= b; + t /= b; + b = one; + } + } + } + b = (t * j0f(x) / b); + } + } + if (sgn == 1) + return -b; + else + return b; +} + +float +ynf(int n, float x) +{ + __int32_t i, hx, ix, ib; + __int32_t sign; + float a, b, temp; + + GET_FLOAT_WORD(hx, x); + ix = 0x7fffffff & hx; + + if (ix == 0) + return __math_divzerof(1); + + if (ix > 0x7f800000) + return x+x; + + if (hx < 0) + return __math_invalidf(x); + + if (ix == 0x7f800000) + return zero; + + sign = 1; + if (n < 0) { + n = -n; + sign = 1 - ((n & 1) << 1); + } + if (n == 0) + return (y0f(x)); + if (n == 1) + return (sign * y1f(x)); + + a = y0f(x); + b = y1f(x); + /* quit if b is -inf */ + GET_FLOAT_WORD(ib, b); + for (i = 1; i < n && ib != (__int32_t)0xff800000; i++) { + temp = b; + b = ((float)(i + i) / x) * b - a; + GET_FLOAT_WORD(ib, b); + a = temp; + } + if (sign > 0) + return b; + else + return -b; +} + +_MATH_ALIAS_f_if(jn) + +_MATH_ALIAS_f_if(yn) diff --git a/src/audio/ffmpeg_dec/libm/math/sf_lgamma.c b/src/audio/ffmpeg_dec/libm/math/sf_lgamma.c new file mode 100644 index 000000000000..34b94410835b --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_lgamma.c @@ -0,0 +1,43 @@ +/* wf_lgamma.c -- float version of w_lgamma.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + * + */ + +#include "fdlibm.h" +#include + +float +lgammaf(float x) +{ + return lgammaf_r(x, &__signgam); +} + +# ifdef __strong_reference +#ifdef __GNUCLIKE_PRAGMA_DIAGNOSTIC +#pragma GCC diagnostic ignored "-Wpragmas" +#pragma GCC diagnostic ignored "-Wunknown-warning-option" +#pragma GCC diagnostic ignored "-Wattribute-alias=" +#pragma GCC diagnostic ignored "-Wmissing-attributes" +#endif +__strong_reference(lgammaf, gammaf); +#else +float +gammaf(float x) +{ + return lgammaf(x); +} +#endif + +_MATH_ALIAS_f_f(lgamma) +_MATH_ALIAS_f_f(gamma) diff --git a/src/audio/ffmpeg_dec/libm/math/sf_log.c b/src/audio/ffmpeg_dec/libm/math/sf_log.c new file mode 100644 index 000000000000..77cb69a5b6f4 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_log.c @@ -0,0 +1,103 @@ +/* ef_log.c -- float version of e_log.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" + +#if __OBSOLETE_MATH_FLOAT +static const float ln2_hi = 6.9313812256e-01, /* 0x3f317180 */ + ln2_lo = 9.0580006145e-06, /* 0x3717f7d1 */ + two25 = 3.355443200e+07, /* 0x4c000000 */ + Lg1 = 6.6666668653e-01, /* 3F2AAAAB */ + Lg2 = 4.0000000596e-01, /* 3ECCCCCD */ + Lg3 = 2.8571429849e-01, /* 3E924925 */ + Lg4 = 2.2222198546e-01, /* 3E638E29 */ + Lg5 = 1.8183572590e-01, /* 3E3A3325 */ + Lg6 = 1.5313838422e-01, /* 3E1CD04F */ + Lg7 = 1.4798198640e-01; /* 3E178897 */ + +static const float zero = 0.0; + +float +logf(float x) +{ + float hfsq, f, s, z, R, w, t1, t2, dk; + __int32_t k, ix, i, j; + + GET_FLOAT_WORD(ix, x); + + k = 0; + if (FLT_UWORD_IS_ZERO(ix & 0x7fffffff)) + return __math_divzerof(1); /* log(+-0)=-inf */ + if (ix < 0) + return __math_invalidf(x); /* log(-#) = NaN */ + if (!FLT_UWORD_IS_FINITE(ix)) + return x + x; + if (FLT_UWORD_IS_SUBNORMAL(ix)) { + k -= 25; + x *= two25; /* subnormal number, scale up x */ + GET_FLOAT_WORD(ix, x); + } + k += (ix >> 23) - 127; + ix &= 0x007fffff; + i = (ix + (0x95f64 << 3)) & 0x800000; + SET_FLOAT_WORD(x, ix | (i ^ 0x3f800000)); /* normalize x or x/2 */ + k += (i >> 23); + f = x - (float)1.0; + if ((0x007fffff & (15 + ix)) < 16) { /* |f| < 2**-20 */ + if (f == zero) { + if (k == 0) + return zero; + else { + dk = (float)k; + return dk * ln2_hi + dk * ln2_lo; + } + } + R = f * f * ((float)0.5 - (float)0.33333333333333333 * f); + if (k == 0) + return f - R; + else { + dk = (float)k; + return dk * ln2_hi - ((R - dk * ln2_lo) - f); + } + } + s = f / ((float)2.0 + f); + dk = (float)k; + z = s * s; + i = ix - (0x6147a << 3); + w = z * z; + j = (0x6b851 << 3) - ix; + t1 = w * (Lg2 + w * (Lg4 + w * Lg6)); + t2 = z * (Lg1 + w * (Lg3 + w * (Lg5 + w * Lg7))); + i |= j; + R = t2 + t1; + if (i > 0) { + hfsq = (float)0.5 * f * f; + if (k == 0) + return f - (hfsq - s * (hfsq + R)); + else + return dk * ln2_hi - ((hfsq - (s * (hfsq + R) + dk * ln2_lo)) - f); + } else { + if (k == 0) + return f - s * (f - R); + else + return dk * ln2_hi - ((s * (f - R) - dk * ln2_lo) - f); + } +} + +_MATH_ALIAS_f_f(log) + +#else +#include "../common/sf_log.c" +#endif /* __OBSOLETE_MATH_FLOAT */ diff --git a/src/audio/ffmpeg_dec/libm/math/sf_log10.c b/src/audio/ffmpeg_dec/libm/math/sf_log10.c new file mode 100644 index 000000000000..d992cfd09221 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_log10.c @@ -0,0 +1,52 @@ +/* ef_log10.c -- float version of e_log10.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" + +static const float two25 = 3.3554432000e+07, /* 0x4c000000 */ + ivln10 = 4.3429449201e-01, /* 0x3ede5bd9 */ + log10_2hi = 3.0102920532e-01, /* 0x3e9a2080 */ + log10_2lo = 7.9034151668e-07; /* 0x355427db */ + +float +log10f(float x) +{ + float y, z; + __int32_t i, k, hx; + + GET_FLOAT_WORD(hx, x); + + k = 0; + if (FLT_UWORD_IS_ZERO(hx & 0x7fffffff)) + return __math_divzerof(1); /* log(+-0)=-inf */ + if (hx < 0) + return __math_invalidf(x); /* log(-#) = NaN */ + if (!FLT_UWORD_IS_FINITE(hx)) + return x + x; + if (FLT_UWORD_IS_SUBNORMAL(hx)) { + k -= 25; + x *= two25; /* subnormal number, scale up x */ + GET_FLOAT_WORD(hx, x); + } + k += (hx >> 23) - 127; + i = ((__uint32_t)k & 0x80000000) >> 31; + hx = (hx & 0x007fffff) | ((0x7f - i) << 23); + y = (float)(k + i); + SET_FLOAT_WORD(x, hx); + z = y * log10_2lo + ivln10 * logf(x); + return z + y * log10_2hi; +} + +_MATH_ALIAS_f_f(log10) diff --git a/src/audio/ffmpeg_dec/libm/math/sf_log2.c b/src/audio/ffmpeg_dec/libm/math/sf_log2.c new file mode 100644 index 000000000000..60b051579835 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_log2.c @@ -0,0 +1,39 @@ +/* wf_log2.c -- float version of s_log2.c. + * Modification of sf_exp10.c by Yaakov Selkowitz 2009. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +/* + * wrapper log2f(x) + */ + +#include "fdlibm.h" +#if __OBSOLETE_MATH_FLOAT +#include +#include +#undef log2 +#undef log2f + +static const float log2_inv = 1.0f / (float) M_LN2; + +float +log2f(float x) /* wrapper log2f */ +{ + return logf(x) * log2_inv; +} + +_MATH_ALIAS_f_f(log2) + +#else +#include "../common/sf_log2.c" +#endif /* __OBSOLETE_MATH_FLOAT */ diff --git a/src/audio/ffmpeg_dec/libm/math/sf_pow.c b/src/audio/ffmpeg_dec/libm/math/sf_pow.c new file mode 100644 index 000000000000..46d674888554 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_pow.c @@ -0,0 +1,304 @@ +/* ef_pow.c -- float version of e_pow.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" +#include "math_config.h" + +#if __OBSOLETE_MATH_FLOAT + +#ifdef __v810__ +#define const +#endif + +static const float +bp[] = {1.0, 1.5,}, +dp_h[] = { 0.0, 5.84960938e-01,}, /* 0x3f15c000 */ +dp_l[] = { 0.0, 1.56322085e-06,}, /* 0x35d1cfdc */ +zero = 0.0, +one = 1.0, +two = 2.0, +two24 = 16777216.0, /* 0x4b800000 */ + /* poly coefs for (3/2)*(log(x)-2s-2/3*s**3 */ +L1 = 6.0000002384e-01, /* 0x3f19999a */ +L2 = 4.2857143283e-01, /* 0x3edb6db7 */ +L3 = 3.3333334327e-01, /* 0x3eaaaaab */ +L4 = 2.7272811532e-01, /* 0x3e8ba305 */ +L5 = 2.3066075146e-01, /* 0x3e6c3255 */ +L6 = 2.0697501302e-01, /* 0x3e53f142 */ +P1 = 1.6666667163e-01, /* 0x3e2aaaab */ +P2 = -2.7777778450e-03, /* 0xbb360b61 */ +P3 = 6.6137559770e-05, /* 0x388ab355 */ +P4 = -1.6533901999e-06, /* 0xb5ddea0e */ +P5 = 4.1381369442e-08, /* 0x3331bb4c */ +lg2 = 6.9314718246e-01, /* 0x3f317218 */ +lg2_h = 6.93145752e-01, /* 0x3f317200 */ +lg2_l = 1.42860654e-06, /* 0x35bfbe8c */ +ovt = 4.2995665694e-08, /* -(128-log2(ovfl+.5ulp)) */ +cp = 9.6179670095e-01, /* 0x3f76384f =2/(3ln2) */ +cp_h = 9.61914062e-01, /* 0x3f764000 =head of cp */ +cp_l = -1.17368574e-04, /* 0xb8f623c6 =tail of cp */ +ivln2 = 1.4426950216e+00, /* 0x3fb8aa3b =1/ln2 */ +ivln2_h = 1.4426879883e+00, /* 0x3fb8aa00 =16b 1/ln2*/ +ivln2_l = 7.0526075433e-06; /* 0x36eca570 =1/ln2 tail*/ + +float +powf(float x, float y) +{ + float z, ax, z_h, z_l, p_h, p_l; + float y1, t1, t2, r, s, t, u, v, w; + __int32_t i, j, k, yisint, n; + __int32_t hx, hy, ix, iy, is; + + GET_FLOAT_WORD(hx, x); + GET_FLOAT_WORD(hy, y); + ix = hx & 0x7fffffff; + iy = hy & 0x7fffffff; + + /* y==zero: x**0 = 1 */ + if (FLT_UWORD_IS_ZERO(iy)) { + if (issignalingf_inline(x)) + return x + y; + return one; + } + + /* x|y==NaN return NaN unless x==1 then return 1 */ + if (FLT_UWORD_IS_NAN(ix) || FLT_UWORD_IS_NAN(iy)) { + if (hx == 0x3f800000 && !issignalingf_inline(y)) + return one; + else + return x + y; + } + + /* determine if y is an odd int when x < 0 + * yisint = 0 ... y is not an integer + * yisint = 1 ... y is an odd int + * yisint = 2 ... y is an even int + */ + yisint = 0; + if (hx < 0) { + if (iy >= 0x4b800000) + yisint = 2; /* even integer y */ + else if (iy >= 0x3f800000) { + k = (iy >> 23) - 0x7f; /* exponent */ + j = iy >> (23 - k); + if (lsl(j, (23 - k)) == iy) + yisint = 2 - (j & 1); + } + } + + /* special value of y */ + if (FLT_UWORD_IS_INFINITE(iy)) { /* y is +-inf */ + if (ix == 0x3f800000) + return one; /* +-1**+-inf = 1 */ + else if (ix > 0x3f800000) /* (|x|>1)**+-inf = inf,0 */ + return (hy >= 0) ? y : zero; + else /* (|x|<1)**-,+inf = inf,0 */ + return (hy < 0) ? -y : zero; + } + if (iy == 0x3f800000) { /* y is +-1 */ + if (hy < 0) { + if (x == 0) + return __math_divzerof(hx < 0); + return one / x; + } else + return x; + } + if (hy == 0x40000000 && ix < 0x5f800000 && ix > 0x1a000000) + return x * x; /* y is 2 */ + if (hy == 0x3f000000) { /* y is 0.5 */ + if (hx >= 0) /* x >= +0 */ + return sqrtf(x); + } + + ax = fabsf(x); + /* special value of x */ + if (FLT_UWORD_IS_INFINITE(ix) || ix == 0x3f800000) { + z = ax; /*x is +-inf,+-1*/ + if (hy < 0) + z = one / z; /* z = (1/|x|) */ + if (hx < 0) { + if (((ix - 0x3f800000) | yisint) == 0) { + return __math_invalidf(x); /* (-1)**non-int is NaN */ + } else if (yisint == 1) + z = -z; /* (x<0)**odd = -(|x|**odd) */ + } + return z; + } + + if (FLT_UWORD_IS_ZERO(ix)) { + if (hy < 0) + return __math_divzerof(hx < 0 && yisint == 1); + if (yisint != 1) + x = ax; + return x; + } + + /* (x<0)**(non-int) is NaN */ + if (((((__uint32_t)hx >> 31) - 1) | yisint) == 0) + return __math_invalidf(x); + + /* |y| is huge */ + if (iy > 0x4d000000) { /* if |y| > 2**27 */ + /* over/underflow if x is not close to one */ + if (ix < 0x3f7ffff4) + return (hy < 0) ? __math_oflowf(0) : __math_uflowf(0); + if (ix > 0x3f800007) + return (hy > 0) ? __math_oflowf(0) : __math_uflowf(0); + /* now |1-x| is tiny <= 2**-20, suffice to compute + log(x) by x-x^2/2+x^3/3-x^4/4 */ + t = ax - 1; /* t has 20 trailing zeros */ + w = (t * t) * + ((float)0.5 - t * ((float)0.333333333333 - t * (float)0.25)); + u = ivln2_h * t; /* ivln2_h has 16 sig. bits */ + v = t * ivln2_l - w * ivln2; + t1 = u + v; + GET_FLOAT_WORD(is, t1); + SET_FLOAT_WORD(t1, is & 0xfffff000); + t2 = v - (t1 - u); + } else { + float s2, s_h, s_l, t_h, t_l; + n = 0; + /* take care subnormal number */ + if (FLT_UWORD_IS_SUBNORMAL(ix)) { + ax *= two24; + n -= 24; + GET_FLOAT_WORD(ix, ax); + } + n += ((ix) >> 23) - 0x7f; + j = ix & 0x007fffff; + /* determine interval */ + ix = j | 0x3f800000; /* normalize ix */ + if (j <= 0x1cc471) + k = 0; /* |x|> 1) & 0xfffff000U) | 0x20000000; + SET_FLOAT_WORD(t_h, is + 0x00400000 + lsl(k, 21)); + t_l = ax - (t_h - bp[k]); + s_l = v * ((u - s_h * t_h) - s_h * t_l); + /* compute log(ax) */ + s2 = s * s; + r = s2 * s2 * + (L1 + s2 * (L2 + s2 * (L3 + s2 * (L4 + s2 * (L5 + s2 * L6))))); + r += s_l * (s_h + s); + s2 = s_h * s_h; + t_h = (float)3.0 + s2 + r; + GET_FLOAT_WORD(is, t_h); + SET_FLOAT_WORD(t_h, is & 0xfffff000); + t_l = r - ((t_h - (float)3.0) - s2); + /* u+v = s*(1+...) */ + u = s_h * t_h; + v = s_l * t_h + t_l * s; + /* 2/(3log2)*(s+...) */ + p_h = u + v; + GET_FLOAT_WORD(is, p_h); + SET_FLOAT_WORD(p_h, is & 0xfffff000); + p_l = v - (p_h - u); + z_h = cp_h * p_h; /* cp_h+cp_l = 2/(3*log2) */ + z_l = cp_l * p_h + p_l * cp + dp_l[k]; + /* log2(ax) = (s+..)*2/(3*log2) = n + dp_h + z_h + z_l */ + t = (float)n; + t1 = (((z_h + z_l) + dp_h[k]) + t); + GET_FLOAT_WORD(is, t1); + SET_FLOAT_WORD(t1, is & 0xfffff000); + t2 = z_l - (((t1 - t) - dp_h[k]) - z_h); + } + + s = one; /* s (sign of result -ve**odd) = -1 else = 1 */ + if (((((__uint32_t)hx >> 31) - 1) | (yisint - 1)) == 0) + s = -one; /* (-ve)**(odd int) */ + + /* split up y into y1+y2 and compute (y1+y2)*(t1+t2) */ + GET_FLOAT_WORD(is, y); + SET_FLOAT_WORD(y1, is & 0xfffff000); + p_l = (y - y1) * t1 + y * t2; + p_h = y1 * t1; + z = p_l + p_h; + GET_FLOAT_WORD(j, z); + i = j & 0x7fffffff; + if (j > 0) { + if (i > FLT_UWORD_EXP_MAX) + return __math_oflowf(s < 0); /* overflow */ + else if (i == FLT_UWORD_EXP_MAX) + if (p_l + ovt > z - p_h) + return __math_oflowf(s < 0); /* overflow */ + } else { + if (i > FLT_UWORD_EXP_MIN) + return __math_uflowf(s < 0); /* underflow */ + else if (i == FLT_UWORD_EXP_MIN) + if (p_l <= z - p_h) + return __math_uflowf(s < 0); /* underflow */ + } + /* + * compute 2**(p_h+p_l) + */ + k = (i >> 23) - 0x7f; + n = 0; + if (i > 0x3f000000) { /* if |z| > 0.5, set n = [z+0.5] */ + n = j + (0x00800000 >> (k + 1)); + k = ((n & 0x7fffffff) >> 23) - 0x7f; /* new k for n */ + SET_FLOAT_WORD(t, n & ~(0x007fffff >> k)); + n = ((n & 0x007fffff) | 0x00800000) >> (23 - k); + if (j < 0) + n = -n; + p_h -= t; + } + t = p_l + p_h; + GET_FLOAT_WORD(is, t); + SET_FLOAT_WORD(t, is & 0xfffff000); + u = t * lg2_h; + v = (p_l - (t - p_h)) * lg2 + t * lg2_l; + z = u + v; + w = v - (z - u); + t = z * z; + t1 = z - t * (P1 + t * (P2 + t * (P3 + t * (P4 + t * P5)))); + r = (z * t1) / (t1 - two) - (w + z * w); + z = one - (r - z); + GET_FLOAT_WORD(j, z); + j += lsl(n, 23); + if (asr(j, 23) <= 0) + z = scalbnf(z, (int)n); /* subnormal output */ + else + SET_FLOAT_WORD(z, j); + return s * z; +} + +#ifdef __strong_reference +#if defined(__GNUCLIKE_PRAGMA_DIAGNOSTIC) && !defined(__clang__) +#pragma GCC diagnostic ignored "-Wmissing-attributes" +#endif +__strong_reference(powf, _powf); +#endif + +_MATH_ALIAS_f_ff(pow) + +#else +#include "../common/sf_pow.c" +#endif /* __OBSOLETE_MATH_FLOAT */ diff --git a/src/audio/ffmpeg_dec/libm/math/sf_rem_pio2.c b/src/audio/ffmpeg_dec/libm/math/sf_rem_pio2.c new file mode 100644 index 000000000000..d45bb56364d7 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_rem_pio2.c @@ -0,0 +1,185 @@ +/* ef_rem_pio2.c -- float version of e_rem_pio2.c + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + * + */ + +/* __rem_pio2f(x,y) + * + * return the remainder of x rem pi/2 in y[0]+y[1] + * use __kernel_rem_pio2_32() + */ + +#include "fdlibm.h" + +/* + * Table of constants for 2/pi, 396 Hex digits (476 decimal) of 2/pi + */ +static const __int32_t two_over_pi[] = { + 0xA2, 0xF9, 0x83, 0x6E, 0x4E, 0x44, 0x15, 0x29, 0xFC, 0x27, 0x57, 0xD1, + 0xF5, 0x34, 0xDD, 0xC0, 0xDB, 0x62, 0x95, 0x99, 0x3C, 0x43, 0x90, 0x41, + 0xFE, 0x51, 0x63, 0xAB, 0xDE, 0xBB, 0xC5, 0x61, 0xB7, 0x24, 0x6E, 0x3A, + 0x42, 0x4D, 0xD2, 0xE0, 0x06, 0x49, 0x2E, 0xEA, 0x09, 0xD1, 0x92, 0x1C, + 0xFE, 0x1D, 0xEB, 0x1C, 0xB1, 0x29, 0xA7, 0x3E, 0xE8, 0x82, 0x35, 0xF5, + 0x2E, 0xBB, 0x44, 0x84, 0xE9, 0x9C, 0x70, 0x26, 0xB4, 0x5F, 0x7E, 0x41, + 0x39, 0x91, 0xD6, 0x39, 0x83, 0x53, 0x39, 0xF4, 0x9C, 0x84, 0x5F, 0x8B, + 0xBD, 0xF9, 0x28, 0x3B, 0x1F, 0xF8, 0x97, 0xFF, 0xDE, 0x05, 0x98, 0x0F, + 0xEF, 0x2F, 0x11, 0x8B, 0x5A, 0x0A, 0x6D, 0x1F, 0x6D, 0x36, 0x7E, 0xCF, + 0x27, 0xCB, 0x09, 0xB7, 0x4F, 0x46, 0x3F, 0x66, 0x9E, 0x5F, 0xEA, 0x2D, + 0x75, 0x27, 0xBA, 0xC7, 0xEB, 0xE5, 0xF1, 0x7B, 0x3D, 0x07, 0x39, 0xF7, + 0x8A, 0x52, 0x92, 0xEA, 0x6B, 0xFB, 0x5F, 0xB1, 0x1F, 0x8D, 0x5D, 0x08, + 0x56, 0x03, 0x30, 0x46, 0xFC, 0x7B, 0x6B, 0xAB, 0xF0, 0xCF, 0xBC, 0x20, + 0x9A, 0xF4, 0x36, 0x1D, 0xA9, 0xE3, 0x91, 0x61, 0x5E, 0xE6, 0x1B, 0x08, + 0x65, 0x99, 0x85, 0x5F, 0x14, 0xA0, 0x68, 0x40, 0x8D, 0xFF, 0xD8, 0x80, + 0x4D, 0x73, 0x27, 0x31, 0x06, 0x06, 0x15, 0x56, 0xCA, 0x73, 0xA8, 0xC9, + 0x60, 0xE2, 0x7B, 0xC0, 0x8C, 0x6B, +}; + +/* This array is like the one in e_rem_pio2.c, but the numbers are + single precision and the last 8 bits are forced to 0. */ +static const __uint32_t npio2_hw[] = { + 0x3fc90f00, 0x40490f00, 0x4096cb00, 0x40c90f00, 0x40fb5300, 0x4116cb00, + 0x412fed00, 0x41490f00, 0x41623100, 0x417b5300, 0x418a3a00, 0x4196cb00, + 0x41a35c00, 0x41afed00, 0x41bc7e00, 0x41c90f00, 0x41d5a000, 0x41e23100, + 0x41eec200, 0x41fb5300, 0x4203f200, 0x420a3a00, 0x42108300, 0x4216cb00, + 0x421d1400, 0x42235c00, 0x4229a500, 0x422fed00, 0x42363600, 0x423c7e00, + 0x4242c700, 0x42490f00 +}; + +/* + * invpio2: 24 bits of 2/pi + * pio2_1: first 17 bit of pi/2 + * pio2_1t: pi/2 - pio2_1 + * pio2_2: second 17 bit of pi/2 + * pio2_2t: pi/2 - (pio2_1+pio2_2) + * pio2_3: third 17 bit of pi/2 + * pio2_3t: pi/2 - (pio2_1+pio2_2+pio2_3) + */ + +static const float zero = 0.0000000000e+00, /* 0x00000000 */ + half = 5.0000000000e-01, /* 0x3f000000 */ + two8 = 2.5600000000e+02, /* 0x43800000 */ + invpio2 = 6.3661980629e-01, /* 0x3f22f984 */ + pio2_1 = 1.5707855225e+00, /* 0x3fc90f80 */ + pio2_1t = 1.0804334124e-05, /* 0x37354443 */ + pio2_2 = 1.0804273188e-05, /* 0x37354400 */ + pio2_2t = 6.0770999344e-11, /* 0x2e85a308 */ + pio2_3 = 6.0770943833e-11, /* 0x2e85a300 */ + pio2_3t = 6.1232342629e-17; /* 0x248d3132 */ + +__int32_t +__rem_pio2f(float x, float *y) +{ + float z, w, t, r, fn; + float tx[3]; + __int32_t i, j, n, ix, hx; + int e0, nx; + + GET_FLOAT_WORD(hx, x); + ix = hx & 0x7fffffff; + if (ix <= 0x3f490fd8) /* |x| ~<= pi/4 , no need for reduction */ + { + y[0] = x; + y[1] = 0; + return 0; + } + if (ix < 0x4016cbe4) { /* |x| < 3pi/4, special case with n=+-1 */ + if (hx > 0) { + z = x - pio2_1; + if ((ix & 0xfffffff0) != 0x3fc90fd0) { /* 24+24 bit pi OK */ + y[0] = z - pio2_1t; + y[1] = (z - y[0]) - pio2_1t; + } else { /* near pi/2, use 24+24+24 bit pi */ + z -= pio2_2; + y[0] = z - pio2_2t; + y[1] = (z - y[0]) - pio2_2t; + } + return 1; + } else { /* negative x */ + z = x + pio2_1; + if ((ix & 0xfffffff0) != 0x3fc90fd0) { /* 24+24 bit pi OK */ + y[0] = z + pio2_1t; + y[1] = (z - y[0]) + pio2_1t; + } else { /* near pi/2, use 24+24+24 bit pi */ + z += pio2_2; + y[0] = z + pio2_2t; + y[1] = (z - y[0]) + pio2_2t; + } + return -1; + } + } + if (ix <= 0x43490f80) { /* |x| ~<= 2^7*(pi/2), medium size */ + t = fabsf(x); + n = (__int32_t)(t * invpio2 + half); + fn = (float)n; + r = t - fn * pio2_1; + w = fn * pio2_1t; /* 1st round good to 40 bit */ + if (n < 32 && (ix & 0xffffff00) != npio2_hw[n - 1]) { + y[0] = r - w; /* quick check no cancellation */ + } else { + __uint32_t high; + j = ix >> 23; + y[0] = r - w; + GET_FLOAT_WORD(high, y[0]); + i = j - ((high >> 23) & 0xff); + if (i > 8) { /* 2nd iteration needed, good to 57 */ + t = r; + w = fn * pio2_2; + r = t - w; + w = fn * pio2_2t - ((t - r) - w); + y[0] = r - w; + GET_FLOAT_WORD(high, y[0]); + i = j - ((high >> 23) & 0xff); + if (i > 25) { /* 3rd iteration need, 74 bits acc */ + t = r; /* will cover all possible cases */ + w = fn * pio2_3; + r = t - w; + w = fn * pio2_3t - ((t - r) - w); + y[0] = r - w; + } + } + } + y[1] = (r - y[0]) - w; + if (hx < 0) { + y[0] = -y[0]; + y[1] = -y[1]; + return -n; + } else + return n; + } + /* + * all other (large) arguments + */ + if (!FLT_UWORD_IS_FINITE(ix)) { + y[0] = y[1] = x - x; + return 0; + } + /* set z = scalbn(|x|,ilogb(x)-7) */ + e0 = (int)((ix >> 23) - 134); /* e0 = ilogb(z)-7; */ + SET_FLOAT_WORD(z, ix - ((__int32_t)e0 << 23)); + for (i = 0; i < 2; i++) { + tx[i] = (float)((__int32_t)(z)); + z = (z - tx[i]) * two8; + } + tx[2] = z; + nx = 3; + while (tx[nx - 1] == zero) + nx--; /* skip zero term */ + n = __kernel_rem_pio2f(tx, y, e0, nx, 2, two_over_pi); + if (hx < 0) { + y[0] = -y[0]; + y[1] = -y[1]; + return -n; + } + return n; +} diff --git a/src/audio/ffmpeg_dec/libm/math/sf_remainder.c b/src/audio/ffmpeg_dec/libm/math/sf_remainder.c new file mode 100644 index 000000000000..b290b9be6c93 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_remainder.c @@ -0,0 +1,65 @@ +/* ef_remainder.c -- float version of e_remainder.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" + +static const float zero = 0.0; + +float +remainderf(float x, float p) +{ + __int32_t hx, hp; + __uint32_t sx; + float p_half; + + GET_FLOAT_WORD(hx, x); + GET_FLOAT_WORD(hp, p); + sx = hx & 0x80000000; + hp &= 0x7fffffff; + hx &= 0x7fffffff; + + /* purge off exception values */ + if (FLT_UWORD_IS_NAN(hx) || FLT_UWORD_IS_NAN(hp)) + return x + p; + + if (isinf(x) || hp == 0) + return __math_invalidf(x); + + if (hp <= FLT_UWORD_HALF_MAX) + x = fmodf(x, p + p); /* now x < 2p */ + if ((hx - hp) == 0) + return zero * x; + x = fabsf(x); + p = fabsf(p); + if (hp < 0x01000000) { + if (x + x > p) { + x -= p; + if (x + x >= p) + x -= p; + } + } else { + p_half = (float)0.5 * p; + if (x > p_half) { + x -= p; + if (x >= p_half) + x -= p; + } + } + GET_FLOAT_WORD(hx, x); + SET_FLOAT_WORD(x, hx ^ sx); + return x; +} + +_MATH_ALIAS_f_ff(remainder) diff --git a/src/audio/ffmpeg_dec/libm/math/sf_scalb.c b/src/audio/ffmpeg_dec/libm/math/sf_scalb.c new file mode 100644 index 000000000000..75c3ab489a8a --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_scalb.c @@ -0,0 +1,46 @@ +/* ef_scalb.c -- float version of e_scalb.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" +#include + +float +scalbf(float x, float fn) +{ + if (isnan(fn) || isnan(x)) + return x + fn; + + if (isinf(fn)) { + if ((x == 0.0f && fn > 0.0f) || (isinf(x) && fn < 0.0f)) + return __math_invalidf(fn); + if (fn > 0.0f) + return fn*x; + else + return x/(-fn); + } + + if (rintf(fn) != fn) + return __math_invalidf(fn); + + if (fn > 4 * __FLT_MAX_EXP__) + fn = 4 * __FLT_MAX_EXP__; + + if (fn < -4 * __FLT_MAX_EXP__) + fn = -4 * __FLT_MAX_EXP__; + + return scalbnf(x, (int)fn); +} + +_MATH_ALIAS_f_ff(scalb) diff --git a/src/audio/ffmpeg_dec/libm/math/sf_signif.c b/src/audio/ffmpeg_dec/libm/math/sf_signif.c new file mode 100644 index 000000000000..90b27d93e906 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_signif.c @@ -0,0 +1,24 @@ +/* sf_signif.c -- float version of s_signif.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" + +float +significandf(float x) +{ + return scalbnf(x, -ilogbf(x)); +} + +_MATH_ALIAS_f_f(significand) diff --git a/src/audio/ffmpeg_dec/libm/math/sf_sin.c b/src/audio/ffmpeg_dec/libm/math/sf_sin.c new file mode 100644 index 000000000000..d06ee829724a --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_sin.c @@ -0,0 +1,63 @@ +/* sf_sin.c -- float version of s_sin.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" +#if __OBSOLETE_MATH_FLOAT + +float +sinf(float x) +{ + float y[2], z = 0.0; + __int32_t n, ix; + + GET_FLOAT_WORD(ix, x); + + /* |x| ~< pi/4 */ + ix &= 0x7fffffff; + if (ix <= 0x3f490fd8) + return __kernel_sinf(x, z, 0); + + /* sin(Inf or NaN) is NaN */ + else if (!FLT_UWORD_IS_FINITE(ix)) + return __math_invalidf(x); + + /* argument reduction needed */ + else { + n = __rem_pio2f(x, y); + switch (n & 3) { + case 0: + return __kernel_sinf(y[0], y[1], 1); + case 1: + return __kernel_cosf(y[0], y[1]); + case 2: + return -__kernel_sinf(y[0], y[1], 1); + default: + return -__kernel_cosf(y[0], y[1]); + } + } +} + +#ifdef __strong_reference +#if defined(__GNUCLIKE_PRAGMA_DIAGNOSTIC) && !defined(__clang__) +#pragma GCC diagnostic ignored "-Wmissing-attributes" +#endif +__strong_reference(sinf, _sinf); +#endif + +_MATH_ALIAS_f_f(sin) + +#else +#include "../common/sinf.c" +#endif /* __OBSOLETE_MATH_FLOAT */ diff --git a/src/audio/ffmpeg_dec/libm/math/sf_sincos.c b/src/audio/ffmpeg_dec/libm/math/sf_sincos.c new file mode 100644 index 000000000000..5d3fce238684 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_sincos.c @@ -0,0 +1,21 @@ +/* Copyright (c) 2002 Thomas Fitzsimmons */ +/* sincos -- currently no more efficient than two separate calls to + sin and cos. */ +#define _GNU_SOURCE +#include "fdlibm.h" +#if __OBSOLETE_MATH_FLOAT + +#include + +void __no_builtin +sincosf(float x, float *sinx, float *cosx) +{ + *sinx = _sinf(x); + *cosx = _cosf(x); +} + +_MATH_ALIAS_v_fFF(sincos) + +#else +#include "../common/sincosf.c" +#endif /* __OBSOLETE_MATH_FLOAT */ diff --git a/src/audio/ffmpeg_dec/libm/math/sf_sinh.c b/src/audio/ffmpeg_dec/libm/math/sf_sinh.c new file mode 100644 index 000000000000..cd7e01a76229 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_sinh.c @@ -0,0 +1,61 @@ +/* ef_sinh.c -- float version of e_sinh.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" + +static const float one = 1.0; + +float +sinhf(float x) +{ + float t, w, h; + __int32_t ix, jx; + + GET_FLOAT_WORD(jx, x); + ix = jx & 0x7fffffff; + + /* x is INF or NaN */ + if (!FLT_UWORD_IS_FINITE(ix)) + return x + x; + + h = 0.5; + if (jx < 0) + h = -h; + /* |x| in [0,22], return sign(x)*0.5*(E+E/(E+1))) */ + if (ix < 0x41b00000) { /* |x|<22 */ + if (ix < 0x31800000) /* |x|<2**-28 */ + return __math_inexactf(x); /* sinh(tiny) = tiny with inexact */ + t = expm1f(fabsf(x)); + if (ix < 0x3f800000) + return h * ((float)2.0 * t - t * t / (t + one)); + return h * (t + t / (t + one)); + } + + /* |x| in [22, log(maxdouble)] return 0.5*exp(|x|) */ + if (ix <= FLT_UWORD_LOG_MAX) + return h * expf(fabsf(x)); + + /* |x| in [log(maxdouble), overflowthresold] */ + if (ix <= FLT_UWORD_LOG_2MAX) { + w = expf((float)0.5 * fabsf(x)); + t = h * w; + return t * w; + } + + /* |x| > overflowthresold, sinh(x) overflow */ + return __math_oflowf(x < 0); +} + +_MATH_ALIAS_f_f(sinh) diff --git a/src/audio/ffmpeg_dec/libm/math/sf_sqrt.c b/src/audio/ffmpeg_dec/libm/math/sf_sqrt.c new file mode 100644 index 000000000000..bf4ebf722977 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_sqrt.c @@ -0,0 +1,83 @@ +/* ef_sqrtf.c -- float version of e_sqrt.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" + +float +sqrtf(float x) +{ + float z; + __uint32_t r, hx; + __int32_t ix, s, q, m, t, i; + + GET_FLOAT_WORD(ix, x); + hx = ix & 0x7fffffff; + + /* take care of Inf and NaN */ + if (!FLT_UWORD_IS_FINITE(hx)) { + if (ix < 0 && !isnanf(x)) + return __math_invalidf(x); /* sqrt(-inf)=sNaN */ + return x + x; /* sqrt(NaN)=NaN, sqrt(+inf)=+inf */ + } + + /* take care of zero and -ves */ + if (FLT_UWORD_IS_ZERO(hx)) + return x; /* sqrt(+-0) = +-0 */ + if (ix < 0) + return __math_invalidf(x); /* sqrt(-ve) = sNaN */ + + /* normalize x */ + m = (ix >> 23); + if (FLT_UWORD_IS_SUBNORMAL(hx)) { /* subnormal x */ + for (i = 0; (ix & 0x00800000L) == 0; i++) + ix <<= 1; + m -= i - 1; + } + m -= 127; /* unbias exponent */ + ix = (ix & 0x007fffffL) | 0x00800000L; + if (m & 1) /* odd m, double x to make it even */ + ix += ix; + m >>= 1; /* m = [m/2] */ + + /* generate sqrt(x) bit by bit */ + ix += ix; + q = s = 0; /* q = sqrt(x) */ + r = 0x01000000L; /* r = moving bit from right to left */ + + while (r != 0) { + t = s + r; + if (t <= ix) { + s = t + r; + ix -= t; + q += r; + } + ix += ix; + r >>= 1; + } + + if (ix != 0) { + FE_DECL_ROUND(rnd); + if (__is_nearest(rnd)) + q += (q & 1); + else if (__is_upward(rnd)) + q += 2; + } + ix = (q >> 1) + 0x3f000000L; + ix += lsl(m, 23); + SET_FLOAT_WORD(z, ix); + return z; +} + +_MATH_ALIAS_f_f(sqrt) diff --git a/src/audio/ffmpeg_dec/libm/math/sf_tan.c b/src/audio/ffmpeg_dec/libm/math/sf_tan.c new file mode 100644 index 000000000000..3f9cba8bc300 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_tan.c @@ -0,0 +1,43 @@ +/* sf_tan.c -- float version of s_tan.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" + +float +tanf(float x) +{ + float y[2], z = 0.0; + __int32_t n, ix; + + GET_FLOAT_WORD(ix, x); + + /* |x| ~< pi/4 */ + ix &= 0x7fffffff; + if (ix <= 0x3f490fda) + return __kernel_tanf(x, z, 1); + + /* tan(Inf or NaN) is NaN */ + else if (!FLT_UWORD_IS_FINITE(ix)) + return __math_invalidf(x); /* NaN */ + + /* argument reduction needed */ + else { + n = __rem_pio2f(x, y); + return __kernel_tanf(y[0], y[1], 1 - ((n & 1) << 1)); /* 1 -- n even + -1 -- n odd */ + } +} + +_MATH_ALIAS_f_f(tan) diff --git a/src/audio/ffmpeg_dec/libm/math/sf_tanh.c b/src/audio/ffmpeg_dec/libm/math/sf_tanh.c new file mode 100644 index 000000000000..919e38dc6dd9 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_tanh.c @@ -0,0 +1,55 @@ +/* sf_tanh.c -- float version of s_tanh.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + */ + +#include "fdlibm.h" + +static const float one = 1.0, two = 2.0; + +float +tanhf(float x) +{ + float t, z; + __int32_t jx, ix; + + GET_FLOAT_WORD(jx, x); + ix = jx & 0x7fffffff; + + /* x is INF or NaN */ + if (!FLT_UWORD_IS_FINITE(ix)) { + if (jx >= 0) + return one / x + one; /* tanh(+-inf)=+-1 */ + else + return one / x - one; /* tanh(NaN) = NaN */ + } + + /* |x| < 22 */ + if (ix < 0x41b00000) { /* |x|<22 */ + if (ix < 0x24000000) /* |x|<2**-55 */ + return x * (one + x); /* tanh(small) = small */ + if (ix >= 0x3f800000) { /* |x|>=1 */ + t = expm1f(two * fabsf(x)); + z = one - two / (t + two); + } else { + t = expm1f(-two * fabsf(x)); + z = -t / (t + two); + } + /* |x| > 22, return +-1 */ + } else { + z = __math_inexactf(one); + } + return (jx >= 0) ? z : -z; +} + +_MATH_ALIAS_f_f(tanh) diff --git a/src/audio/ffmpeg_dec/libm/math/sf_tgamma.c b/src/audio/ffmpeg_dec/libm/math/sf_tgamma.c new file mode 100644 index 000000000000..cca18e81a42a --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sf_tgamma.c @@ -0,0 +1,42 @@ +/* ef_tgamma.c -- float version of e_tgamma.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + * + */ + +/* tgammaf(x) + * Float version the Gamma function. Returns gamma(x) + * + * Method: See lgammaf + */ + +#include "fdlibm.h" + +float +tgammaf(float x) +{ + int signgam_local; + int divzero = 0; + + if (isless(x, 0.0f) && clang_barrier_float(rintf(x)) == x) + return __math_invalidf(x); + + float y = expf(__math_lgammaf_r(x, &signgam_local, &divzero)); + if (signgam_local < 0) + y = -y; + if (isinff(y) && finitef(x) && !divzero) + return __math_oflowf(signgam_local < 0); + return y; +} + +_MATH_ALIAS_f_f(tgamma) diff --git a/src/audio/ffmpeg_dec/libm/math/sl_hypot.c b/src/audio/ffmpeg_dec/libm/math/sl_hypot.c new file mode 100644 index 000000000000..1547be6d53e6 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sl_hypot.c @@ -0,0 +1,27 @@ +/* Copyright (C) 2015 by Red Hat, Incorporated. All rights reserved. + * + * Permission to use, copy, modify, and distribute this software + * is freely granted, provided that this notice is preserved. + */ + +#include "fdlibm.h" + +#if defined(_NEED_FLOAT_HUGE) && !defined(__HAVE_LONG_DOUBLE_MATH) + +long double +hypotl(long double x, long double y) +{ + if ((isinf(x) && isnan(y) && !issignaling(y)) || + (isinf(y) && isnan(x) && !issignaling(x))) + return (long double)INFINITY; + + /* Keep it simple for now... */ + long double z = sqrtl((x * x) + (y * y)); +#ifdef __MATH_ERRNO + if (!finite(z) && finite(x) && finite(y)) + errno = ERANGE; +#endif + return z; +} + +#endif diff --git a/src/audio/ffmpeg_dec/libm/math/sr_lgamma.c b/src/audio/ffmpeg_dec/libm/math/sr_lgamma.c new file mode 100644 index 000000000000..b356206df308 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/sr_lgamma.c @@ -0,0 +1,363 @@ + +/* @(#)er_lgamma.c 5.1 93/09/24 */ +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + * + */ + +/* lgamma_r(x) + * Reentrant version of the logarithm of the Gamma function + * with signgam for the sign of Gamma(x). + * + * Method: + * 1. Argument Reduction for 0 < x <= 8 + * Since gamma(1+s)=s*gamma(s), for x in [0,8], we may + * reduce x to a number in [1.5,2.5] by + * lgamma(1+s) = log(s) + lgamma(s) + * for example, + * lgamma(7.3) = log(6.3) + lgamma(6.3) + * = log(6.3*5.3) + lgamma(5.3) + * = log(6.3*5.3*4.3*3.3*2.3) + lgamma(2.3) + * 2. Polynomial approximation of lgamma around its + * minimun ymin=1.461632144968362245 to maintain monotonicity. + * On [ymin-0.23, ymin+0.27] (i.e., [1.23164,1.73163]), use + * Let z = x-ymin; + * lgamma(x) = -1.214862905358496078218 + z^2*poly(z) + * where + * poly(z) is a 14 degree polynomial. + * 2. Rational approximation in the primary interval [2,3] + * We use the following approximation: + * s = x-2.0; + * lgamma(x) = 0.5*s + s*P(s)/Q(s) + * with accuracy + * |P/Q - (lgamma(x)-0.5s)| < 2**-61.71 + * Our algorithms are based on the following observation + * + * zeta(2)-1 2 zeta(3)-1 3 + * lgamma(2+s) = s*(1-Euler) + --------- * s - --------- * s + ... + * 2 3 + * + * where Euler = 0.5771... is the Euler constant, which is very + * close to 0.5. + * + * 3. For x>=8, we have + * lgamma(x)~(x-0.5)log(x)-x+0.5*log(2pi)+1/(12x)-1/(360x**3)+.... + * (better formula: + * lgamma(x)~(x-0.5)*(log(x)-1)-.5*(log(2pi)-1) + ...) + * Let z = 1/x, then we approximation + * f(z) = lgamma(x) - (x-0.5)(log(x)-1) + * by + * 3 5 11 + * w = w0 + w1*z + w2*z + w3*z + ... + w6*z + * where + * |w - f(z)| < 2**-58.74 + * + * 4. For negative x, since (G is gamma function) + * -x*G(-x)*G(x) = pi/sin(pi*x), + * we have + * G(x) = pi/(sin(pi*x)*(-x)*G(-x)) + * since G(-x) is positive, sign(G(x)) = sign(sin(pi*x)) for x<0 + * Hence, for x<0, signgam = sign(sin(pi*x)) and + * lgamma(x) = log(|Gamma(x)|) + * = log(pi/(|x*sin(pi*x)|)) - lgamma(-x); + * Note: one should avoid compute pi*(-x) directly in the + * computation of sin(pi*(-x)). + * + * 5. Special Cases + * lgamma(2+s) ~ s*(1-Euler) for tiny s + * lgamma(1)=lgamma(2)=0 + * lgamma(x) ~ -log(x) for tiny x + * lgamma(0) = lgamma(inf) = inf + * lgamma(-integer) = +-inf + * + */ + +#define _ADD_UNDER_R_TO_FUNCS + +#include "fdlibm.h" + +#ifdef _NEED_FLOAT64 + +static const __float64 two52 = + 4.50359962737049600000e+15, /* 0x43300000, 0x00000000 */ + half = 5.00000000000000000000e-01, /* 0x3FE00000, 0x00000000 */ + one = 1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */ + pi = 3.14159265358979311600e+00, /* 0x400921FB, 0x54442D18 */ + a0 = 7.72156649015328655494e-02, /* 0x3FB3C467, 0xE37DB0C8 */ + a1 = 3.22467033424113591611e-01, /* 0x3FD4A34C, 0xC4A60FAD */ + a2 = 6.73523010531292681824e-02, /* 0x3FB13E00, 0x1A5562A7 */ + a3 = 2.05808084325167332806e-02, /* 0x3F951322, 0xAC92547B */ + a4 = 7.38555086081402883957e-03, /* 0x3F7E404F, 0xB68FEFE8 */ + a5 = 2.89051383673415629091e-03, /* 0x3F67ADD8, 0xCCB7926B */ + a6 = 1.19270763183362067845e-03, /* 0x3F538A94, 0x116F3F5D */ + a7 = 5.10069792153511336608e-04, /* 0x3F40B6C6, 0x89B99C00 */ + a8 = 2.20862790713908385557e-04, /* 0x3F2CF2EC, 0xED10E54D */ + a9 = 1.08011567247583939954e-04, /* 0x3F1C5088, 0x987DFB07 */ + a10 = 2.52144565451257326939e-05, /* 0x3EFA7074, 0x428CFA52 */ + a11 = 4.48640949618915160150e-05, /* 0x3F07858E, 0x90A45837 */ + tc = 1.46163214496836224576e+00, /* 0x3FF762D8, 0x6356BE3F */ + tf = -1.21486290535849611461e-01, /* 0xBFBF19B9, 0xBCC38A42 */ + /* tt = -(tail of tf) */ + tt = -3.63867699703950536541e-18, /* 0xBC50C7CA, 0xA48A971F */ + t0 = 4.83836122723810047042e-01, /* 0x3FDEF72B, 0xC8EE38A2 */ + t1 = -1.47587722994593911752e-01, /* 0xBFC2E427, 0x8DC6C509 */ + t2 = 6.46249402391333854778e-02, /* 0x3FB08B42, 0x94D5419B */ + t3 = -3.27885410759859649565e-02, /* 0xBFA0C9A8, 0xDF35B713 */ + t4 = 1.79706750811820387126e-02, /* 0x3F9266E7, 0x970AF9EC */ + t5 = -1.03142241298341437450e-02, /* 0xBF851F9F, 0xBA91EC6A */ + t6 = 6.10053870246291332635e-03, /* 0x3F78FCE0, 0xE370E344 */ + t7 = -3.68452016781138256760e-03, /* 0xBF6E2EFF, 0xB3E914D7 */ + t8 = 2.25964780900612472250e-03, /* 0x3F6282D3, 0x2E15C915 */ + t9 = -1.40346469989232843813e-03, /* 0xBF56FE8E, 0xBF2D1AF1 */ + t10 = 8.81081882437654011382e-04, /* 0x3F4CDF0C, 0xEF61A8E9 */ + t11 = -5.38595305356740546715e-04, /* 0xBF41A610, 0x9C73E0EC */ + t12 = 3.15632070903625950361e-04, /* 0x3F34AF6D, 0x6C0EBBF7 */ + t13 = -3.12754168375120860518e-04, /* 0xBF347F24, 0xECC38C38 */ + t14 = 3.35529192635519073543e-04, /* 0x3F35FD3E, 0xE8C2D3F4 */ + u0 = -7.72156649015328655494e-02, /* 0xBFB3C467, 0xE37DB0C8 */ + u1 = 6.32827064025093366517e-01, /* 0x3FE4401E, 0x8B005DFF */ + u2 = 1.45492250137234768737e+00, /* 0x3FF7475C, 0xD119BD6F */ + u3 = 9.77717527963372745603e-01, /* 0x3FEF4976, 0x44EA8450 */ + u4 = 2.28963728064692451092e-01, /* 0x3FCD4EAE, 0xF6010924 */ + u5 = 1.33810918536787660377e-02, /* 0x3F8B678B, 0xBF2BAB09 */ + v1 = 2.45597793713041134822e+00, /* 0x4003A5D7, 0xC2BD619C */ + v2 = 2.12848976379893395361e+00, /* 0x40010725, 0xA42B18F5 */ + v3 = 7.69285150456672783825e-01, /* 0x3FE89DFB, 0xE45050AF */ + v4 = 1.04222645593369134254e-01, /* 0x3FBAAE55, 0xD6537C88 */ + v5 = 3.21709242282423911810e-03, /* 0x3F6A5ABB, 0x57D0CF61 */ + s0 = -7.72156649015328655494e-02, /* 0xBFB3C467, 0xE37DB0C8 */ + s1 = 2.14982415960608852501e-01, /* 0x3FCB848B, 0x36E20878 */ + s2 = 3.25778796408930981787e-01, /* 0x3FD4D98F, 0x4F139F59 */ + s3 = 1.46350472652464452805e-01, /* 0x3FC2BB9C, 0xBEE5F2F7 */ + s4 = 2.66422703033638609560e-02, /* 0x3F9B481C, 0x7E939961 */ + s5 = 1.84028451407337715652e-03, /* 0x3F5E26B6, 0x7368F239 */ + s6 = 3.19475326584100867617e-05, /* 0x3F00BFEC, 0xDD17E945 */ + r1 = 1.39200533467621045958e+00, /* 0x3FF645A7, 0x62C4AB74 */ + r2 = 7.21935547567138069525e-01, /* 0x3FE71A18, 0x93D3DCDC */ + r3 = 1.71933865632803078993e-01, /* 0x3FC601ED, 0xCCFBDF27 */ + r4 = 1.86459191715652901344e-02, /* 0x3F9317EA, 0x742ED475 */ + r5 = 7.77942496381893596434e-04, /* 0x3F497DDA, 0xCA41A95B */ + r6 = 7.32668430744625636189e-06, /* 0x3EDEBAF7, 0xA5B38140 */ + w0 = 4.18938533204672725052e-01, /* 0x3FDACFE3, 0x90C97D69 */ + w1 = 8.33333333333329678849e-02, /* 0x3FB55555, 0x5555553B */ + w2 = -2.77777777728775536470e-03, /* 0xBF66C16C, 0x16B02E5C */ + w3 = 7.93650558643019558500e-04, /* 0x3F4A019F, 0x98CF38B6 */ + w4 = -5.95187557450339963135e-04, /* 0xBF4380CB, 0x8C0FE741 */ + w5 = 8.36339918996282139126e-04, /* 0x3F4B67BA, 0x4CDAD5D1 */ + w6 = -1.63092934096575273989e-03; /* 0xBF5AB89D, 0x0B9E43E4 */ + +static const __float64 zero = 0.00000000000000000000e+00; + +static __float64 +sin_pi(__float64 x) +{ + __float64 y, z; + __int32_t n, ix; + + GET_HIGH_WORD(ix, x); + ix &= 0x7fffffff; + + if (ix < 0x3fd00000) + return __kernel_sin(pi * x, zero, 0); + y = -x; /* x is assume negative */ + + /* + * argument reduction, make sure inexact flag not raised if input + * is an integer + */ + z = floor64(y); + if (z != y) { /* inexact anyway */ + y *= _F_64(0.5); + y = _F_64(2.0) * (y - floor64(y)); /* y = |x| mod 2.0 */ + n = (__int32_t)(y * _F_64(4.0)); + } else { + if (ix >= 0x43400000) { + y = zero; + n = 0; /* y must be even */ + } else { + if (ix < 0x43300000) + z = y + two52; /* exact */ + GET_LOW_WORD(n, z); + n &= 1; + y = n; + n <<= 2; + } + } + switch (n) { + case 0: + y = __kernel_sin(pi * y, zero, 0); + break; + case 1: + case 2: + y = __kernel_cos(pi * (_F_64(0.5) - y), zero); + break; + case 3: + case 4: + y = __kernel_sin(pi * (one - y), zero, 0); + break; + case 5: + case 6: + y = -__kernel_cos(pi * (y - _F_64(1.5)), zero); + break; + default: + y = __kernel_sin(pi * (y - _F_64(2.0)), zero, 0); + break; + } + return -y; +} + +__float64 +__math_lgamma_r(__float64 x, int *signgamp, int *divzero) +{ + __float64 t, y, z, nadj = 0.0, p, p1, p2, p3, q, r, w; + __int32_t i, hx, lx, ix; + + EXTRACT_WORDS(hx, lx, x); + + /* purge off +-inf, NaN, +-0, and negative arguments */ + *signgamp = 1; + ix = hx & 0x7fffffff; + if (ix >= 0x7ff00000) + return fabs64(x+x); + if ((ix | lx) == 0) { + if (hx < 0) + *signgamp = -1; + *divzero = 1; + return __math_divzero(0); + } + if (ix < 0x3b900000) { /* |x|<2**-70, return -log(|x|) */ + if (hx < 0) { + *signgamp = -1; + return -log64(-x); + } else + return -log64(x); + } + if (hx < 0) { + if (ix >= 0x43300000) { /* |x|>=2**52, must be -integer */ + *divzero = 1; + return __math_divzero(0); + } + t = sin_pi(x); + if (t == zero) { + *divzero = 1; + return __math_divzero(0); + } + nadj = log64(pi / fabs64(t * x)); + if (t < zero) + *signgamp = -1; + x = -x; + } + + /* purge off 1 and 2 */ + if (((((__uint32_t) ix - 0x3ff00000) | lx) == 0) || ((((__uint32_t) ix - 0x40000000) | lx) == 0)) + r = 0; + /* for x < 2.0 */ + else if (ix < 0x40000000) { + if (ix <= 0x3feccccc) { /* lgamma(x) = lgamma(x+1)-log(x) */ + r = -log64(x); + if (ix >= 0x3FE76944) { + y = one - x; + i = 0; + } else if (ix >= 0x3FCDA661) { + y = x - (tc - one); + i = 1; + } else { + y = x; + i = 2; + } + } else { + r = zero; + if (ix >= 0x3FFBB4C3) { + y = 2.0 - x; + i = 0; + } /* [1.7316,2] */ + else if (ix >= 0x3FF3B4C4) { + y = x - tc; + i = 1; + } /* [1.23,1.73] */ + else { + y = x - one; + i = 2; + } + } + switch (i) { + case 0: + z = y * y; + p1 = a0 + z * (a2 + z * (a4 + z * (a6 + z * (a8 + z * a10)))); + p2 = z * (a1 + z * (a3 + z * (a5 + z * (a7 + z * (a9 + z * a11))))); + p = y * p1 + p2; + r += (p - 0.5 * y); + break; + case 1: + z = y * y; + w = z * y; + p1 = t0 + + w * (t3 + w * (t6 + w * (t9 + w * t12))); /* parallel comp */ + p2 = t1 + w * (t4 + w * (t7 + w * (t10 + w * t13))); + p3 = t2 + w * (t5 + w * (t8 + w * (t11 + w * t14))); + p = z * p1 - (tt - w * (p2 + y * p3)); + r += (tf + p); + break; + case 2: + p1 = y * (u0 + y * (u1 + y * (u2 + y * (u3 + y * (u4 + y * u5))))); + p2 = one + y * (v1 + y * (v2 + y * (v3 + y * (v4 + y * v5)))); + r += (-0.5 * y + p1 / p2); + } + } else if (ix < 0x40200000) { /* x < 8.0 */ + i = (__int32_t)x; + y = x - (__float64)i; + p = y * (s0 + + y * (s1 + y * (s2 + y * (s3 + y * (s4 + y * (s5 + y * s6)))))); + q = one + y * (r1 + y * (r2 + y * (r3 + y * (r4 + y * (r5 + y * r6))))); + r = half * y + p / q; + z = one; /* lgamma(1+s) = log(s) + lgamma(s) */ + switch (i) { + case 7: + z *= (y + 6.0); + __fallthrough; + case 6: + z *= (y + 5.0); + __fallthrough; + case 5: + z *= (y + 4.0); + __fallthrough; + case 4: + z *= (y + 3.0); + __fallthrough; + case 3: + z *= (y + 2.0); + r += log64(z); + break; + } + /* 8.0 <= x < 2**58 */ + } else if (ix < 0x43900000) { + t = log(x); + z = one / x; + y = z * z; + w = w0 + z * (w1 + y * (w2 + y * (w3 + y * (w4 + y * (w5 + y * w6))))); + r = (x - half) * (t - one) + w; + } else + /* 2**58 <= x <= inf */ + r = x * (log64(x) - one); + if (hx < 0) + r = nadj - r; + return check_oflow(r); +} + +__float64 +lgamma64_r(__float64 x, int *signgamp) +{ + int divzero = 0; + return __math_lgamma_r(x, signgamp, &divzero); +} + +_MATH_ALIAS_d_dI(lgamma) + +#endif /* _NEED_FLOAT64 */ diff --git a/src/audio/ffmpeg_dec/libm/math/srf_lgamma.c b/src/audio/ffmpeg_dec/libm/math/srf_lgamma.c new file mode 100644 index 000000000000..628c3b67412e --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/math/srf_lgamma.c @@ -0,0 +1,293 @@ +/* erf_lgamma.c -- float version of er_lgamma.c. + * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. + */ + +/* + * ==================================================== + * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. + * + * Developed at SunPro, a Sun Microsystems, Inc. business. + * Permission to use, copy, modify, and distribute this + * software is freely granted, provided that this notice + * is preserved. + * ==================================================== + * + */ + +#define _ADD_UNDER_R_TO_FUNCS + +#include "fdlibm.h" + +static const float two23 = 8.3886080000e+06, /* 0x4b000000 */ + half = 5.0000000000e-01, /* 0x3f000000 */ + one = 1.0000000000e+00, /* 0x3f800000 */ + pi = 3.1415927410e+00, /* 0x40490fdb */ + a0 = 7.7215664089e-02, /* 0x3d9e233f */ + a1 = 3.2246702909e-01, /* 0x3ea51a66 */ + a2 = 6.7352302372e-02, /* 0x3d89f001 */ + a3 = 2.0580807701e-02, /* 0x3ca89915 */ + a4 = 7.3855509982e-03, /* 0x3bf2027e */ + a5 = 2.8905137442e-03, /* 0x3b3d6ec6 */ + a6 = 1.1927076848e-03, /* 0x3a9c54a1 */ + a7 = 5.1006977446e-04, /* 0x3a05b634 */ + a8 = 2.2086278477e-04, /* 0x39679767 */ + a9 = 1.0801156895e-04, /* 0x38e28445 */ + a10 = 2.5214456400e-05, /* 0x37d383a2 */ + a11 = 4.4864096708e-05, /* 0x383c2c75 */ + tc = 1.4616321325e+00, /* 0x3fbb16c3 */ + tf = -1.2148628384e-01, /* 0xbdf8cdcd */ + /* tt = -(tail of tf) */ + tt = 6.6971006518e-09, /* 0x31e61c52 */ + t0 = 4.8383611441e-01, /* 0x3ef7b95e */ + t1 = -1.4758771658e-01, /* 0xbe17213c */ + t2 = 6.4624942839e-02, /* 0x3d845a15 */ + t3 = -3.2788541168e-02, /* 0xbd064d47 */ + t4 = 1.7970675603e-02, /* 0x3c93373d */ + t5 = -1.0314224288e-02, /* 0xbc28fcfe */ + t6 = 6.1005386524e-03, /* 0x3bc7e707 */ + t7 = -3.6845202558e-03, /* 0xbb7177fe */ + t8 = 2.2596477065e-03, /* 0x3b141699 */ + t9 = -1.4034647029e-03, /* 0xbab7f476 */ + t10 = 8.8108185446e-04, /* 0x3a66f867 */ + t11 = -5.3859531181e-04, /* 0xba0d3085 */ + t12 = 3.1563205994e-04, /* 0x39a57b6b */ + t13 = -3.1275415677e-04, /* 0xb9a3f927 */ + t14 = 3.3552918467e-04, /* 0x39afe9f7 */ + u0 = -7.7215664089e-02, /* 0xbd9e233f */ + u1 = 6.3282704353e-01, /* 0x3f2200f4 */ + u2 = 1.4549225569e+00, /* 0x3fba3ae7 */ + u3 = 9.7771751881e-01, /* 0x3f7a4bb2 */ + u4 = 2.2896373272e-01, /* 0x3e6a7578 */ + u5 = 1.3381091878e-02, /* 0x3c5b3c5e */ + v1 = 2.4559779167e+00, /* 0x401d2ebe */ + v2 = 2.1284897327e+00, /* 0x4008392d */ + v3 = 7.6928514242e-01, /* 0x3f44efdf */ + v4 = 1.0422264785e-01, /* 0x3dd572af */ + v5 = 3.2170924824e-03, /* 0x3b52d5db */ + s0 = -7.7215664089e-02, /* 0xbd9e233f */ + s1 = 2.1498242021e-01, /* 0x3e5c245a */ + s2 = 3.2577878237e-01, /* 0x3ea6cc7a */ + s3 = 1.4635047317e-01, /* 0x3e15dce6 */ + s4 = 2.6642270386e-02, /* 0x3cda40e4 */ + s5 = 1.8402845599e-03, /* 0x3af135b4 */ + s6 = 3.1947532989e-05, /* 0x3805ff67 */ + r1 = 1.3920053244e+00, /* 0x3fb22d3b */ + r2 = 7.2193557024e-01, /* 0x3f38d0c5 */ + r3 = 1.7193385959e-01, /* 0x3e300f6e */ + r4 = 1.8645919859e-02, /* 0x3c98bf54 */ + r5 = 7.7794247773e-04, /* 0x3a4beed6 */ + r6 = 7.3266842264e-06, /* 0x36f5d7bd */ + w0 = 4.1893854737e-01, /* 0x3ed67f1d */ + w1 = 8.3333335817e-02, /* 0x3daaaaab */ + w2 = -2.7777778450e-03, /* 0xbb360b61 */ + w3 = 7.9365057172e-04, /* 0x3a500cfd */ + w4 = -5.9518753551e-04, /* 0xba1c065c */ + w5 = 8.3633989561e-04, /* 0x3a5b3dd2 */ + w6 = -1.6309292987e-03; /* 0xbad5c4e8 */ + +static const float zero = 0.0000000000e+00; + +static float +sin_pif(float x) +{ + float y, z; + __int32_t n, ix; + + GET_FLOAT_WORD(ix, x); + ix &= 0x7fffffff; + + if (ix < 0x3e800000) + return __kernel_sinf(pi * x, zero, 0); + y = -x; /* x is assume negative */ + + /* + * argument reduction, make sure inexact flag not raised if input + * is an integer + */ + z = floorf(y); + if (z != y) { /* inexact anyway */ + y *= (float)0.5; + y = (float)2.0 * (y - floorf(y)); /* y = |x| mod 2.0 */ + n = (__int32_t)(y * (float)4.0); + } else { + if (ix >= 0x4b800000) { + y = zero; + n = 0; /* y must be even */ + } else { + if (ix < 0x4b000000) + z = y + two23; /* exact */ + GET_FLOAT_WORD(n, z); + n &= 1; + y = n; + n <<= 2; + } + } + switch (n) { + case 0: + y = __kernel_sinf(pi * y, zero, 0); + break; + case 1: + case 2: + y = __kernel_cosf(pi * ((float)0.5 - y), zero); + break; + case 3: + case 4: + y = __kernel_sinf(pi * (one - y), zero, 0); + break; + case 5: + case 6: + y = -__kernel_cosf(pi * (y - (float)1.5), zero); + break; + default: + y = __kernel_sinf(pi * (y - (float)2.0), zero, 0); + break; + } + return -y; +} + +float +__math_lgammaf_r(float x, int *signgamp, int *divzero) +{ + float t, y, z, nadj = 0.0, p, p1, p2, p3, q, r, w; + __int32_t i, hx, ix; + + GET_FLOAT_WORD(hx, x); + + /* purge off +-inf, NaN, +-0, and negative arguments */ + *signgamp = 1; + ix = hx & 0x7fffffff; + if (ix >= 0x7f800000) + return fabsf(x+x); + if (ix == 0) { + if (hx < 0) + *signgamp = -1; + *divzero = 1; + return __math_divzerof(0); + } + if (ix < 0x1c800000) { /* |x|<2**-70, return -log(|x|) */ + if (hx < 0) { + *signgamp = -1; + return -logf(-x); + } else + return -logf(x); + } + if (hx < 0) { + if (ix >= 0x4b000000) { /* |x|>=2**23, must be -integer */ + *divzero = 1; + return __math_divzerof(0); + } + t = sin_pif(x); + if (t == zero) { + *divzero = 1; + return __math_divzerof(0); + } + nadj = logf(pi / fabsf(t * x)); + if (t < zero) + *signgamp = -1; + x = -x; + } + + /* purge off 1 and 2 */ + if (ix == 0x3f800000 || ix == 0x40000000) + r = 0; + /* for x < 2.0 */ + else if (ix < 0x40000000) { + if (ix <= 0x3f666666) { /* lgamma(x) = lgamma(x+1)-log(x) */ + r = -logf(x); + if (ix >= 0x3f3b4a20) { + y = one - x; + i = 0; + } else if (ix >= 0x3e6d3308) { + y = x - (tc - one); + i = 1; + } else { + y = x; + i = 2; + } + } else { + r = zero; + if (ix >= 0x3fdda618) { + y = (float)2.0 - x; + i = 0; + } /* [1.7316,2] */ + else if (ix >= 0x3F9da620) { + y = x - tc; + i = 1; + } /* [1.23,1.73] */ + else { + y = x - one; + i = 2; + } + } + switch (i) { + case 0: + z = y * y; + p1 = a0 + z * (a2 + z * (a4 + z * (a6 + z * (a8 + z * a10)))); + p2 = z * (a1 + z * (a3 + z * (a5 + z * (a7 + z * (a9 + z * a11))))); + p = y * p1 + p2; + r += (p - (float)0.5 * y); + break; + case 1: + z = y * y; + w = z * y; + p1 = t0 + + w * (t3 + w * (t6 + w * (t9 + w * t12))); /* parallel comp */ + p2 = t1 + w * (t4 + w * (t7 + w * (t10 + w * t13))); + p3 = t2 + w * (t5 + w * (t8 + w * (t11 + w * t14))); + p = z * p1 - (tt - w * (p2 + y * p3)); + r += (tf + p); + break; + case 2: + p1 = y * (u0 + y * (u1 + y * (u2 + y * (u3 + y * (u4 + y * u5))))); + p2 = one + y * (v1 + y * (v2 + y * (v3 + y * (v4 + y * v5)))); + r += (-(float)0.5 * y + p1 / p2); + } + } else if (ix < 0x41000000) { /* x < 8.0 */ + i = (__int32_t)x; + y = x - (float)i; + p = y * (s0 + + y * (s1 + y * (s2 + y * (s3 + y * (s4 + y * (s5 + y * s6)))))); + q = one + y * (r1 + y * (r2 + y * (r3 + y * (r4 + y * (r5 + y * r6))))); + r = half * y + p / q; + z = one; /* lgamma(1+s) = log(s) + lgamma(s) */ + switch (i) { + case 7: + z *= (y + (float)6.0); + __fallthrough; + case 6: + z *= (y + (float)5.0); + __fallthrough; + case 5: + z *= (y + (float)4.0); + __fallthrough; + case 4: + z *= (y + (float)3.0); + __fallthrough; + case 3: + z *= (y + (float)2.0); + r += logf(z); + break; + } + /* 8.0 <= x < 2**58 */ + } else if (ix < 0x5c800000) { + t = logf(x); + z = one / x; + y = z * z; + w = w0 + z * (w1 + y * (w2 + y * (w3 + y * (w4 + y * (w5 + y * w6))))); + r = (x - half) * (t - one) + w; + } else + /* 2**58 <= x <= inf */ + r = x * (logf(x) - one); + if (hx < 0) + r = nadj - r; + return check_oflowf(r); +} + +float +lgammaf_r(float x, int *signgamp) +{ + int divzero = 0; + return __math_lgammaf_r(x, signgamp, &divzero); +} + +_MATH_ALIAS_f_fI(lgamma) diff --git a/src/audio/ffmpeg_dec/libm/newlib_math_sources.cmake b/src/audio/ffmpeg_dec/libm/newlib_math_sources.cmake new file mode 100644 index 000000000000..9938e0c1d4d0 --- /dev/null +++ b/src/audio/ffmpeg_dec/libm/newlib_math_sources.cmake @@ -0,0 +1,205 @@ +# SPDX-License-Identifier: BSD-3-Clause +# Auto-generated: newlib libm sources COMPILED into libnewlib_m.a. +# The 13 common/*.c that are only textually #included by these are present +# in the tree but deliberately excluded here (compiling them standalone +# would multiply-define exp/log/pow/...). See README. Do not edit by hand; +# regenerate with the recipe in README. +set(NEWLIB_MATH_SOURCES + common/copysignl.c + common/exp10l.c + common/exp_data.c + common/fabsl.c + common/frexpl.c + common/log2_data.c + common/log_data.c + common/math_denorm.c + common/math_denormf.c + common/math_denorml.c + common/math_err_check_oflow.c + common/math_err_check_uflow.c + common/math_err_divzero.c + common/math_err_invalid.c + common/math_err_may_uflow.c + common/math_err_oflow.c + common/math_err_uflow.c + common/math_err_with_errno.c + common/math_errf_check_oflowf.c + common/math_errf_check_uflowf.c + common/math_errf_divzerof.c + common/math_errf_invalidf.c + common/math_errf_may_uflowf.c + common/math_errf_oflowf.c + common/math_errf_uflowf.c + common/math_errf_with_errnof.c + common/math_inexact.c + common/math_inexactf.c + common/math_inexactl.c + common/nanl.c + common/nexttoward.c + common/nexttowardf.c + common/pow_log_data.c + common/s_cbrt.c + common/s_copysign.c + common/s_exp10.c + common/s_expm1.c + common/s_fdim.c + common/s_finite.c + common/s_fma.c + common/s_fmax.c + common/s_fmin.c + common/s_fpclassify.c + common/s_getpayload.c + common/s_ilogb.c + common/s_infinity.c + common/s_iseqsig.c + common/s_isinf.c + common/s_isnan.c + common/s_issignaling.c + common/s_llrint.c + common/s_llround.c + common/s_log1p.c + common/s_log2.c + common/s_logb.c + common/s_lrint.c + common/s_lround.c + common/s_modf.c + common/s_nan.c + common/s_nearbyint.c + common/s_nextafter.c + common/s_pow10.c + common/s_remquo.c + common/s_rint.c + common/s_round.c + common/s_scalbln.c + common/s_scalbn.c + common/s_signbit.c + common/s_trunc.c + common/sf_cbrt.c + common/sf_copysign.c + common/sf_exp10.c + common/sf_exp2_data.c + common/sf_expm1.c + common/sf_fdim.c + common/sf_finite.c + common/sf_fma.c + common/sf_fmax.c + common/sf_fmin.c + common/sf_fpclassify.c + common/sf_getpayload.c + common/sf_ilogb.c + common/sf_infinity.c + common/sf_iseqsig.c + common/sf_isinf.c + common/sf_isnan.c + common/sf_issignaling.c + common/sf_llrint.c + common/sf_llround.c + common/sf_log1p.c + common/sf_log2_data.c + common/sf_log_data.c + common/sf_logb.c + common/sf_lrint.c + common/sf_lround.c + common/sf_modf.c + common/sf_nan.c + common/sf_nearbyint.c + common/sf_nextafter.c + common/sf_pow10.c + common/sf_pow_log2_data.c + common/sf_remquo.c + common/sf_rint.c + common/sf_round.c + common/sf_scalbln.c + common/sf_scalbn.c + common/sf_trunc.c + common/signgam.c + common/sincosf_data.c + common/sl_iseqsig.c + common/sl_issignaling.c + math/k_cos.c + math/k_rem_pio2.c + math/k_sin.c + math/k_tan.c + math/kf_cos.c + math/kf_rem_pio2.c + math/kf_sin.c + math/kf_tan.c + math/s_acos.c + math/s_acosh.c + math/s_asin.c + math/s_asinh.c + math/s_atan.c + math/s_atan2.c + math/s_atanh.c + math/s_ceil.c + math/s_cos.c + math/s_cosh.c + math/s_drem.c + math/s_erf.c + math/s_exp.c + math/s_exp2.c + math/s_fabs.c + math/s_floor.c + math/s_fmod.c + math/s_frexp.c + math/s_hypot.c + math/s_j0.c + math/s_j1.c + math/s_jn.c + math/s_lgamma.c + math/s_log.c + math/s_log10.c + math/s_pow.c + math/s_rem_pio2.c + math/s_remainder.c + math/s_scalb.c + math/s_signif.c + math/s_sin.c + math/s_sincos.c + math/s_sinh.c + math/s_sqrt.c + math/s_tan.c + math/s_tanh.c + math/s_tgamma.c + math/sf_acos.c + math/sf_acosh.c + math/sf_asin.c + math/sf_asinh.c + math/sf_atan.c + math/sf_atan2.c + math/sf_atanh.c + math/sf_ceil.c + math/sf_cos.c + math/sf_cosh.c + math/sf_drem.c + math/sf_erf.c + math/sf_exp.c + math/sf_exp2.c + math/sf_fabs.c + math/sf_floor.c + math/sf_fmod.c + math/sf_frexp.c + math/sf_hypot.c + math/sf_j0.c + math/sf_j1.c + math/sf_jn.c + math/sf_lgamma.c + math/sf_log.c + math/sf_log10.c + math/sf_log2.c + math/sf_pow.c + math/sf_rem_pio2.c + math/sf_remainder.c + math/sf_scalb.c + math/sf_signif.c + math/sf_sin.c + math/sf_sincos.c + math/sf_sinh.c + math/sf_sqrt.c + math/sf_tan.c + math/sf_tanh.c + math/sf_tgamma.c + math/sl_hypot.c + math/sr_lgamma.c + math/srf_lgamma.c +) diff --git a/src/audio/ffmpeg_dec/llext/CMakeLists.txt b/src/audio/ffmpeg_dec/llext/CMakeLists.txt new file mode 100644 index 000000000000..df794983f1dc --- /dev/null +++ b/src/audio/ffmpeg_dec/llext/CMakeLists.txt @@ -0,0 +1,73 @@ +# Copyright (c) 2026 Intel Corporation. +# SPDX-License-Identifier: Apache-2.0 + +if(CONFIG_COMP_FFMPEG_DEC_STUB) + # Dependency-free build: SOF glue + passthrough stub backend, no libavcodec. + sof_llext_build("ffmpeg_dec" + SOURCES ../ffmpeg_dec.c + ../ffmpeg_dec-stub.c + LIB openmodules + ) +else() + # Real decoder: cross-build the FFmpeg archive (Kconfig-selected decoders) + # and link it. libc/libm are NOT bundled (non-PIC, and would clash) — the + # module provides the needed libc symbols itself (shims/alloc), resolves the + # rest from the SOF core, and supplies fast single-precision float math + # (fastmathf.c) when a float codec (AAC/Opus) is selected. + include(${CMAKE_CURRENT_LIST_DIR}/../ffmpeg.cmake) + + set(ffmpeg_dec_srcs + ../ffmpeg_dec.c + ../ffmpeg_dec-ffmpeg.c + ../ffmpeg_dec-shims.c + ../ffmpeg_dec-alloc.c) + # fastmathf provides the single-precision libm the codecs need AND sofm_log2f + # used by the shims' log10(); always built for the real (non-stub) backend. + list(APPEND ffmpeg_dec_srcs ../fastmathf.c) + + # avfilter graph backend (PCM effects, e.g. afftdn noise reduction). Pulls + # libavfilter into the link; it must precede the libs it depends on. + set(ffmpeg_dec_libs avcodec swresample avutil) + set(ffmpeg_dec_libpaths "${FFMPEG_INSTALL_DIR}/lib") + if(CONFIG_FFMPEG_BUILD_AVFILTER) + list(APPEND ffmpeg_dec_srcs ../ffmpeg_dec-filter.c) + set(ffmpeg_dec_libs avfilter ${ffmpeg_dec_libs}) + endif() + + # Encode mode: PCM->compressed backend + libshine (linked AFTER libavcodec, + # which references it), from its own cross-build install dir. + if(CONFIG_FFMPEG_DEC_ENCODE_MODE) + list(APPEND ffmpeg_dec_srcs ../ffmpeg_dec-encode.c) + endif() + if(CONFIG_FFMPEG_ENC_MP3) + list(APPEND ffmpeg_dec_libs shine) + list(APPEND ffmpeg_dec_libpaths "${SHINE_INSTALL}/lib") + endif() + + # Cold split: module code must also use -mlongcalls to reach FFmpeg code + # that now lives in the far (DRAM) .cold region. + set(ffmpeg_dec_cflags "") + if(CONFIG_FFMPEG_DEC_COLD_SPLIT) + set(ffmpeg_dec_cflags -mlongcalls) + endif() + + sof_llext_build("ffmpeg_dec" + SOURCES ${ffmpeg_dec_srcs} + INCLUDES "${FFMPEG_INSTALL_DIR}/include" + LIBS_PATH ${ffmpeg_dec_libpaths} + LIBS ${ffmpeg_dec_libs} + CFLAGS ${ffmpeg_dec_cflags} + LIB openmodules + ) + + # The archive must be built before the module compiles/links against it. + add_dependencies(ffmpeg_dec_llext_lib ffmpeg_ext) + add_dependencies(ffmpeg_dec ffmpeg_ext) + + # Cold split: place the (renamed) FFmpeg .cold code so the loader relocates + # it to DRAM. See ffmpeg_cold.ld for why INSERT AFTER .bss. + if(CONFIG_FFMPEG_DEC_COLD_SPLIT) + llext_link_options(ffmpeg_dec + "-Wl,-T,${CMAKE_CURRENT_LIST_DIR}/ffmpeg_cold.ld") + endif() +endif() diff --git a/src/audio/ffmpeg_dec/llext/ffmpeg_cold.ld b/src/audio/ffmpeg_dec/llext/ffmpeg_cold.ld new file mode 100644 index 000000000000..1ddecd4dbfff --- /dev/null +++ b/src/audio/ffmpeg_dec/llext/ffmpeg_cold.ld @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright(c) 2026 Intel Corporation. + * + * LLEXT linker fragment for the ffmpeg_dec hot/cold split + * (CONFIG_FFMPEG_DEC_COLD_SPLIT). Collects the FFmpeg code (renamed to .cold in + * the archives by ffmpeg.cmake) plus any av_cold leftovers into an explicit + * .cold output section so the LLEXT loader (llext_manager) relocates it to DRAM. + * + * Placed with INSERT AFTER .bss so .cold gets the highest link-time address and + * cannot overlap the fixed-LMA .text/.rodata/.hash of the ace "mirrored" -shared + * layout (forcing it before/after .text overran those budgets). Its link address + * is irrelevant at runtime: the loader reads the .cold section and copies it to + * the cold/DRAM region. Cross-region .cold<->.text calls are handled by + * -mlongcalls (FFmpeg and module code). + */ +SECTIONS +{ + .cold : { + *(.cold .cold.*) + *(.literal.unlikely .literal.unlikely.*) + *(.text.unlikely .text.unlikely.*) + } +} INSERT AFTER .bss; diff --git a/src/audio/ffmpeg_dec/llext/llext.toml.h b/src/audio/ffmpeg_dec/llext/llext.toml.h new file mode 100644 index 000000000000..eda3fce26a5c --- /dev/null +++ b/src/audio/ffmpeg_dec/llext/llext.toml.h @@ -0,0 +1,6 @@ +#include +#define LOAD_TYPE "2" +#include "../ffmpeg_dec.toml" + +[module] +count = __COUNTER__ diff --git a/src/audio/smart_amp/smart_amp.c b/src/audio/smart_amp/smart_amp.c index f82e3d2f7360..38577545b36e 100644 --- a/src/audio/smart_amp/smart_amp.c +++ b/src/audio/smart_amp/smart_amp.c @@ -289,7 +289,11 @@ static int smart_amp_get_config(struct processing_module *mod, comp_dbg(dev, "actual blob size = %zu, expected blob size = %zu", bs, sizeof(struct sof_smart_amp_config)); - if (bs == 0 || bs > size) + /* bs is the host-set config.size and is used as the memcpy source + * length from the fixed-size sad->config, so bound it by the struct + * size. memcpy_s() below already bounds it against the destination. + */ + if (bs == 0 || bs > sizeof(struct sof_smart_amp_config)) return -EINVAL; ret = memcpy_s(cdata->data->data, size, &sad->config, bs); @@ -521,8 +525,15 @@ static int smart_amp_ff_process(struct processing_module *mod, return 0; } - if (frames > SMART_AMP_FF_BUF_DB_SZ) { - comp_err(dev, "feed forward frame size overflow: %u", frames); + /* + * The remap functions write frames * ff_mod.channels samples into + * ff_mod.buf, which holds SMART_AMP_FF_BUF_DB_SZ samples. Bound the + * total sample count, not just the frame count, so an unexpected + * channel count cannot overflow the buffer. + */ + if ((uint64_t)frames * sad->ff_mod.channels > SMART_AMP_FF_BUF_DB_SZ) { + comp_err(dev, "feed forward frame size overflow: %u frames, %u ch", + frames, sad->ff_mod.channels); sad->ff_mod.consumed = frames; return -EINVAL; } @@ -556,8 +567,10 @@ static int smart_amp_fb_process(struct processing_module *mod, return 0; } - if (frames > SMART_AMP_FB_BUF_DB_SZ) { - comp_err(dev, "feedback frame size overflow: %u", frames); + /* bound total samples (frames * channels) against the buffer size */ + if ((uint64_t)frames * sad->fb_mod.channels > SMART_AMP_FB_BUF_DB_SZ) { + comp_err(dev, "feedback frame size overflow: %u frames, %u ch", + frames, sad->fb_mod.channels); sad->fb_mod.consumed = frames; return -EINVAL; } diff --git a/src/audio/smart_amp/smart_amp_generic.c b/src/audio/smart_amp/smart_amp_generic.c index 91223eb398bd..72f81d0e5117 100644 --- a/src/audio/smart_amp/smart_amp_generic.c +++ b/src/audio/smart_amp/smart_amp_generic.c @@ -35,7 +35,11 @@ static void remap_s32_to_s32(struct smart_amp_mod_stream *src_mod, uint32_t fram n = MIN(num_samples_remaining, nmax); for (ch = 0; ch < src_mod->channels; ch++) { - if (chan_map[ch] == -1) + /* skip unmapped (-1) and out-of-range source channels; + * the uint8_t cast folds the negative case into the + * single upper-bound check (-1 becomes 255 >= src_ch) + */ + if ((uint8_t)chan_map[ch] >= src_ch) continue; mod_ptr = mod_ptr_base + ch; @@ -103,7 +107,11 @@ static void remap_s16_to_s16(struct smart_amp_mod_stream *src_mod, uint32_t fram n = MIN(num_samples_remaining, nmax); for (ch = 0; ch < src_mod->channels; ch++) { - if (chan_map[ch] == -1) + /* skip unmapped (-1) and out-of-range source channels; + * the uint8_t cast folds the negative case into the + * single upper-bound check (-1 becomes 255 >= src_ch) + */ + if ((uint8_t)chan_map[ch] >= src_ch) continue; mod_ptr = mod_ptr_base + ch; @@ -147,7 +155,11 @@ static void remap_s16_to_b32(struct smart_amp_mod_stream *src_mod, uint32_t fram n = MIN(num_samples_remaining, nmax); for (ch = 0; ch < src_mod->channels; ch++) { - if (chan_map[ch] == -1) + /* skip unmapped (-1) and out-of-range source channels; + * the uint8_t cast folds the negative case into the + * single upper-bound check (-1 becomes 255 >= src_ch) + */ + if ((uint8_t)chan_map[ch] >= src_ch) continue; mod_ptr = mod_ptr_base + ch; diff --git a/src/audio/smart_amp/smart_amp_maxim_dsm.c b/src/audio/smart_amp/smart_amp_maxim_dsm.c index c83a971d8175..4b808dfaaf36 100644 --- a/src/audio/smart_amp/smart_amp_maxim_dsm.c +++ b/src/audio/smart_amp/smart_amp_maxim_dsm.c @@ -321,7 +321,18 @@ static int maxim_dsm_get_param(struct smart_amp_mod_struct_t *hspk, * required size */ if (bs > size) { - comp_err(dev, "[DSM] invalid size %d", bs); + comp_err(dev, "[DSM] invalid size %zu", bs); + return -EINVAL; + } + + /* Host controls msg_index and therefore data_pos; make sure the + * fragment stays inside caldata->data to avoid leaking adjacent + * heap back to the host. + */ + if (caldata->data_pos >= caldata->data_size || + bs > caldata->data_size - caldata->data_pos) { + comp_err(dev, "[DSM] invalid data_pos %u, size %zu, total %u", + caldata->data_pos, bs, caldata->data_size); return -EINVAL; } diff --git a/src/audio/src/src_common.c b/src/audio/src/src_common.c index 3a9c7dac280f..ee2c6820c05e 100644 --- a/src/audio/src/src_common.c +++ b/src/audio/src/src_common.c @@ -532,7 +532,16 @@ int src_params_general(struct processing_module *mod, /* free any existing delay lines. TODO reuse if same size */ mod_free(mod, cd->delay_lines); - cd->delay_lines = mod_alloc(mod, delay_lines_size); + /* + * The base of delay_lines (and hence buffer_start, which is offset + * from it by a multiple of 8 bytes) must itself be 8-byte aligned: + * the HiFi FIR cores issue 64-bit AE_L32X2/AE_S32X2 circular + * accesses into it and fault (EXCCAUSE 9) on a 4-byte-aligned base. + * ALIGN_UP above only rounds the size; request the alignment on the + * allocation explicitly rather than relying on the allocator default + * (mod_alloc()/alignment 0 may return a 4-byte-aligned pointer). + */ + cd->delay_lines = mod_alloc_align(mod, delay_lines_size, sizeof(int32_t) * 2); if (!cd->delay_lines) { comp_err(dev, "failed to alloc cd->delay_lines, delay_lines_size = %zu", delay_lines_size); diff --git a/src/audio/src/src_hifi2ep.c b/src/audio/src/src_hifi2ep.c index 73bf0797c91c..52039f075e6b 100644 --- a/src/audio/src/src_hifi2ep.c +++ b/src/audio/src/src_hifi2ep.c @@ -12,7 +12,13 @@ #include "src_common.h" -#include +#if defined(__has_include) +# if __has_include() +# include +# endif +#else +# include +#endif #include #include #include diff --git a/src/audio/src/src_hifi3.c b/src/audio/src/src_hifi3.c index 06a7b7aa75ba..f9b8ed86bf9c 100644 --- a/src/audio/src/src_hifi3.c +++ b/src/audio/src/src_hifi3.c @@ -12,7 +12,13 @@ #include "src_common.h" -#include +#if defined(__has_include) +# if __has_include() +# include +# endif +#else +# include +#endif #include #include #include diff --git a/src/audio/src/src_hifi4.c b/src/audio/src/src_hifi4.c index ad3b8583113f..d203f9c0b5c1 100644 --- a/src/audio/src/src_hifi4.c +++ b/src/audio/src/src_hifi4.c @@ -14,7 +14,13 @@ #include "src_common.h" #include -#include +#if defined(__has_include) +# if __has_include() +# include +# endif +#else +# include +#endif #include #include #include diff --git a/src/audio/src/src_hifi5.c b/src/audio/src/src_hifi5.c index 0adfc4796d25..7ec403821c1f 100644 --- a/src/audio/src/src_hifi5.c +++ b/src/audio/src/src_hifi5.c @@ -14,7 +14,13 @@ #include "src_common.h" #include -#include +#if defined(__has_include) +# if __has_include() +# include +# endif +#else +# include +#endif #include #include #include diff --git a/src/audio/tdfb/tdfb.c b/src/audio/tdfb/tdfb.c index 1cda385cae84..bf3d1dd8ff5e 100644 --- a/src/audio/tdfb/tdfb.c +++ b/src/audio/tdfb/tdfb.c @@ -302,78 +302,185 @@ static int wrap_180(int a) return a; } -static int tdfb_init_coef(struct processing_module *mod, int source_nch, - int sink_nch) +/* Walk the TDFB blob and verify that every FIR section and trailing array + * fits exactly inside the IPC payload. The walk mirrors tdfb_init_coef() + * but stays bounded so a malformed blob cannot push tdfb_filter_seek() + * past the buffer end at setup time. The channel-vs-stream relationships + * are also checked here so a mismatching blob cannot replace the working + * configuration during streaming. + */ +static int tdfb_validate_config(struct comp_dev *dev, + struct sof_tdfb_config *config, + size_t config_size) { + struct processing_module *mod = comp_mod(dev); struct tdfb_comp_data *cd = module_get_private_data(mod); struct sof_fir_coef_data *coef_data; - struct sof_tdfb_config *config = cd->config; - struct comp_dev *dev = mod->dev; - int16_t *output_channel_mix_beam_off = NULL; - int16_t *coefp; - int size_sum = 0; - int min_delta_idx; /* Index to beam angle with smallest delta vs. target */ - int min_delta; /* Smallest angle difference found in degrees */ - int max_ch; - int num_filters; - int target_az; /* Target azimuth angle in degrees */ - int delta; /* Target minus found angle in degrees absolute value */ - int idx; - int s; + struct sof_tdfb_angle *filter_angles; + int16_t *input_channel_select; + uint8_t *p; + uint8_t *end; + size_t remaining; + size_t step; + size_t expected; + int16_t max_ch; + int total_filters; int i; - /* Sanity checks */ - if (config->size != cd->config_size) { - comp_err(dev, "Incorrect configuration blob size"); + if ((size_t)config->size != config_size) { + comp_err(dev, "blob size %zu / header size %u mismatch", + config_size, config->size); return -EINVAL; } - - if (config->num_output_channels > PLATFORM_MAX_CHANNELS || - !config->num_output_channels) { - comp_err(dev, "invalid num_output_channels %d", + if (!config->num_output_channels || + config->num_output_channels > PLATFORM_MAX_CHANNELS) { + comp_err(dev, "invalid num_output_channels %u", config->num_output_channels); return -EINVAL; } - - if (config->num_output_channels != sink_nch) { - comp_err(dev, "stream output channels count %d does not match configuration %d", - sink_nch, config->num_output_channels); - return -EINVAL; - } - - if (config->num_filters > SOF_TDFB_FIR_MAX_COUNT) { - comp_err(dev, "invalid num_filters %d", - config->num_filters); + if (!config->num_filters || config->num_filters > SOF_TDFB_FIR_MAX_COUNT) { + comp_err(dev, "invalid num_filters %u", config->num_filters); return -EINVAL; } - - /* In SOF v1.6 - 1.8 based beamformer topologies the multiple angles, mic locations, - * and beam on/off switch were not defined. A most basic supported blob has num_angles - * equal to 1. Mic locations data is optional. - */ - if (config->num_angles == 0 || config->num_angles > SOF_TDFB_MAX_ANGLES) { - comp_err(dev, "invalid num_angles %d", - config->num_angles); + if (!config->num_angles || config->num_angles > SOF_TDFB_MAX_ANGLES) { + comp_err(dev, "invalid num_angles %u", config->num_angles); return -EINVAL; } - if (!config->angle_enum_mult) { comp_err(dev, "invalid angle_enum_mult"); return -EINVAL; } - if (config->beam_off_defined > 1) { - comp_err(dev, "invalid beam_off_defined %d", + comp_err(dev, "invalid beam_off_defined %u", config->beam_off_defined); return -EINVAL; } - if (config->num_mic_locations > SOF_TDFB_MAX_MICROPHONES) { - comp_err(dev, "invalid num_mic_locations %d", + comp_err(dev, "invalid num_mic_locations %u", config->num_mic_locations); return -EINVAL; } + total_filters = config->num_filters * (config->num_angles + config->beam_off_defined); + p = (uint8_t *)&config->data[0]; + end = (uint8_t *)config + config_size; + for (i = 0; i < total_filters; i++) { + remaining = end - p; + if (remaining < sizeof(struct sof_fir_coef_data)) { + comp_err(dev, "FIR %d header out of bounds", i); + return -EINVAL; + } + coef_data = (struct sof_fir_coef_data *)p; + if (fir_delay_size(coef_data) <= 0) { + comp_err(dev, "FIR %d invalid length %d", + i, coef_data->length); + return -EINVAL; + } + step = sizeof(struct sof_fir_coef_data) + + (size_t)coef_data->length * sizeof(int16_t); + if (step > remaining) { + comp_err(dev, "FIR %d coefs out of bounds", i); + return -EINVAL; + } + p += step; + } + + /* p now points at input_channel_select[]. The remaining bytes must + * hold exactly: 3 per-filter int16 arrays (input_channel_select, + * output_channel_mix, output_stream_mix), an optional beam-off output + * channel mix, num_angles angle entries and num_mic_locations + * microphone entries. + */ + input_channel_select = (int16_t *)p; + expected = ((size_t)config->num_filters * SOF_TDFB_CONFIG_FILTER_CONTROL_NUM_WORDS + + (size_t)config->beam_off_defined * config->num_filters) * + sizeof(int16_t) + + (size_t)config->num_angles * sizeof(struct sof_tdfb_angle) + + (size_t)config->num_mic_locations * + sizeof(struct sof_tdfb_mic_location); + if ((size_t)(end - p) != expected) { + comp_err(dev, "blob trailer size mismatch: have %zu, expected %zu", + (size_t)(end - p), expected); + return -EINVAL; + } + + /* Each filter_angles[].filter_index points at the first filter of a + * num_filters-wide bank, so it must be in [0, total_filters - + * num_filters]. Validating here ensures the runtime path in + * tdfb_init_coef() cannot be reached with an out-of-range index. + */ + filter_angles = (struct sof_tdfb_angle *) + (p + ((size_t)config->num_filters * SOF_TDFB_CONFIG_FILTER_CONTROL_NUM_WORDS + + (size_t)config->beam_off_defined * config->num_filters) * sizeof(int16_t)); + for (i = 0; i < config->num_angles; i++) { + if (filter_angles[i].filter_index < 0 || + filter_angles[i].filter_index + config->num_filters > total_filters) { + comp_err(dev, "invalid filter_index %d for angle %d", + filter_angles[i].filter_index, i); + return -EINVAL; + } + } + + /* The blob must match the running stream. Skip these checks when no + * stream is bound yet (cached channel counts are zero before prepare). + */ + if (cd->sink_channels && config->num_output_channels != cd->sink_channels) { + comp_err(dev, "blob num_output_channels %u does not match sink %d", + config->num_output_channels, cd->sink_channels); + return -EINVAL; + } + if (cd->source_channels) { + max_ch = 0; + for (i = 0; i < config->num_filters; i++) { + if (input_channel_select[i] < 0) { + comp_err(dev, "invalid channel select for filter %d", i); + return -EINVAL; + } + if (input_channel_select[i] > max_ch) + max_ch = input_channel_select[i]; + } + if (max_ch + 1 > cd->source_channels) { + comp_err(dev, "blob needs %d source channels, stream has %d", + max_ch + 1, cd->source_channels); + return -EINVAL; + } + } + + return 0; +} + +static int tdfb_validator(struct comp_dev *dev, void *new_data, uint32_t new_data_size) +{ + if (new_data_size < sizeof(struct sof_tdfb_config) || + new_data_size > SOF_TDFB_MAX_SIZE) { + comp_err(dev, "invalid configuration blob, size %u", new_data_size); + return -EINVAL; + } + + return tdfb_validate_config(dev, new_data, new_data_size); +} + +static int tdfb_init_coef(struct processing_module *mod) +{ + struct tdfb_comp_data *cd = module_get_private_data(mod); + struct sof_fir_coef_data *coef_data; + struct sof_tdfb_config *config = cd->config; + struct comp_dev *dev = mod->dev; + int16_t *output_channel_mix_beam_off = NULL; + int16_t *coefp; + int size_sum = 0; + int min_delta_idx; /* Index to beam angle with smallest delta vs. target */ + int min_delta; /* Smallest angle difference found in degrees */ + int num_filters; + int target_az; /* Target azimuth angle in degrees */ + int delta; /* Target minus found angle in degrees absolute value */ + int idx; + int i; + + /* Blob layout, bounds, size and stream channel relationships are + * pre-validated by tdfb_validator() / tdfb_validate_config(). + */ + /* Skip filter coefficients */ num_filters = config->num_filters * (config->num_angles + config->beam_off_defined); coefp = tdfb_filter_seek(config, num_filters); @@ -386,8 +493,8 @@ static int tdfb_init_coef(struct processing_module *mod, int source_nch, cd->output_stream_mix = coefp; coefp += config->num_filters; - /* Check if there's beam-off configured, then get pointers to beam angles data - * and microphone locations. Finally check that size matches. + /* Check if there's beam-off configured, then get pointers to beam angles + * data and microphone locations. */ if (config->beam_off_defined) { output_channel_mix_beam_off = coefp; @@ -396,11 +503,6 @@ static int tdfb_init_coef(struct processing_module *mod, int source_nch, cd->filter_angles = (struct sof_tdfb_angle *)coefp; cd->mic_locations = (struct sof_tdfb_mic_location *) (&cd->filter_angles[config->num_angles]); - if ((uint8_t *)&cd->mic_locations[config->num_mic_locations] != - (uint8_t *)config + config->size) { - comp_err(dev, "invalid config size"); - return -EINVAL; - } /* Skip to requested coefficient set */ min_delta = 360; @@ -437,47 +539,16 @@ static int tdfb_init_coef(struct processing_module *mod, int source_nch, /* Seek to proper filter for requested angle or beam off configuration */ coefp = tdfb_filter_seek(config, idx); - /* Initialize filter bank */ + /* Initialize filter bank. FIR header bounds and length validity were + * already checked when the blob entered the component. + */ for (i = 0; i < config->num_filters; i++) { - /* Get delay line size */ coef_data = (struct sof_fir_coef_data *)coefp; - s = fir_delay_size(coef_data); - if (s > 0) { - size_sum += s; - } else { - comp_err(dev, "FIR length %d is invalid", - coef_data->length); - return -EINVAL; - } - - /* Initialize coefficients for FIR filter and find next - * filter. - */ + size_sum += fir_delay_size(coef_data); fir_init_coef(&cd->fir[i], coef_data); coefp = coef_data->coef + coef_data->length; } - /* Find max used input channel */ - max_ch = 0; - for (i = 0; i < config->num_filters; i++) { - if (cd->input_channel_select[i] > max_ch) - max_ch = cd->input_channel_select[i]; - - if (cd->input_channel_select[i] < 0) { - comp_err(dev, "invalid channel select for filter %d", i); - return -EINVAL; - } - } - - /* The stream must contain at least the number of channels that is - * used for filters input. - */ - if (max_ch + 1 > source_nch) { - comp_err(dev, "stream input channels count %d is not sufficient for configuration %d", - source_nch, max_ch + 1); - return -EINVAL; - } - return size_sum; } @@ -513,7 +584,7 @@ static int tdfb_setup(struct processing_module *mod, int source_nch, int sink_nc } /* Set coefficients for each channel from coefficient blob */ - delay_size = tdfb_init_coef(mod, source_nch, sink_nch); + delay_size = tdfb_init_coef(mod); if (delay_size < 0) return delay_size; /* Contains error code */ @@ -586,6 +657,13 @@ static int tdfb_init(struct processing_module *mod) goto err; } + /* Reject malformed blobs at IPC time so a bad run-time update cannot + * replace the working configuration. The channel-count checks inside + * the validator are skipped until tdfb_prepare() caches the stream + * channel counts. + */ + comp_data_blob_set_validator(cd->model_handler, tdfb_validator); + for (i = 0; i < PLATFORM_MAX_CHANNELS; i++) fir_reset(&cd->fir[i]); @@ -655,14 +733,14 @@ static int tdfb_process(struct processing_module *mod, comp_dbg(dev, "entry"); - /* Check for changed configuration */ + /* Check for changed configuration. The IPC-time validator installed + * in tdfb_init() has already structurally validated the blob, so + * only NULL needs to be guarded here. + */ if (comp_is_new_data_blob_available(cd->model_handler)) { cd->config = comp_get_data_blob(cd->model_handler, &cd->config_size, NULL); - if (!cd->config || cd->config_size < sizeof(*cd->config) || - cd->config_size > SOF_TDFB_MAX_SIZE) { - comp_err(dev, "invalid configuration blob, size %zu", cd->config_size); + if (!cd->config) return -EINVAL; - } ret = tdfb_setup(mod, audio_stream_get_channels(source), audio_stream_get_channels(sink), audio_stream_get_frm_fmt(source)); @@ -754,15 +832,31 @@ static int tdfb_prepare(struct processing_module *mod, sink_channels = audio_stream_get_channels(&sinkb->stream); rate = audio_stream_get_rate(&sourceb->stream); + /* Cache stream channel counts for the blob validator before any + * validate_config() call runs. + */ + cd->source_channels = source_channels; + cd->sink_channels = sink_channels; + /* Initialize filter */ cd->config = comp_get_data_blob(cd->model_handler, &cd->config_size, NULL); - if (!cd->config || cd->config_size < sizeof(*cd->config) || - cd->config_size > SOF_TDFB_MAX_SIZE) { - comp_err(dev, "invalid configuration blob, size %zu", cd->config_size); + if (!cd->config) { ret = -EINVAL; goto out; } + /* Re-validate now that the stream channel counts are cached: the + * IPC-time validator skips the channel checks before prepare, so the + * blob must be walked again here before tdfb_setup() uses it. + * Discard a malformed blob so the runtime path (tdfb_process()) + * cannot dereference it. + */ + ret = tdfb_validator(dev, cd->config, (uint32_t)cd->config_size); + if (ret < 0) { + cd->config = NULL; + goto out; + } + ret = tdfb_setup(mod, source_channels, sink_channels, frame_fmt); if (ret < 0) { comp_err(dev, "error: tdfb_setup failed."); @@ -785,18 +879,18 @@ static int tdfb_prepare(struct processing_module *mod, /* Initialize tracking */ ret = tdfb_direction_init(mod, rate, source_channels); - if (!ret) { - comp_info(dev, "max_lag = %d, xcorr_size = %zu", - cd->direction.max_lag, cd->direction.d_size); - comp_info(dev, "line_array = %d, a_step = %d, a_offs = %d", - (int)cd->direction.line_array, cd->config->angle_enum_mult, - cd->config->angle_enum_offs); - } - -out: if (ret < 0) - comp_set_state(dev, COMP_TRIGGER_RESET); + goto out; + comp_info(dev, "max_lag = %d, xcorr_size = %zu", + cd->direction.max_lag, cd->direction.d_size); + comp_info(dev, "line_array = %d, a_step = %d, a_offs = %d", + (int)cd->direction.line_array, cd->config->angle_enum_mult, + cd->config->angle_enum_offs); + + return 0; +out: + comp_set_state(dev, COMP_TRIGGER_RESET); return ret; } @@ -807,6 +901,9 @@ static int tdfb_reset(struct processing_module *mod) comp_dbg(mod->dev, "entry"); + cd->source_channels = 0; + cd->sink_channels = 0; + tdfb_free_delaylines(mod); cd->tdfb_func = NULL; diff --git a/src/audio/tdfb/tdfb.h b/src/audio/tdfb/tdfb.h index c6fd1ff0f97d..1787fef919e6 100644 --- a/src/audio/tdfb/tdfb.h +++ b/src/audio/tdfb/tdfb.h @@ -29,6 +29,13 @@ #define SOF_TDFB_CTRL_INDEX_AZIMUTH_ESTIMATE 1 /* enum */ #define SOF_TDFB_CTRL_INDEX_FILTERBANK 0 /* bytes */ +/* Helper for magic value for number of words for input_channel_select, output_channel_mix, + * and output_stream_mix. It used to validate that the blob size is sufficient for all the + * filters defined for controllable beam angles. The words count is in the check code + * multiplied with num_filters and sizeof(). See the configuration data description below: + */ +#define SOF_TDFB_CONFIG_FILTER_CONTROL_NUM_WORDS 3 + /* * sof_tdfb_config data[] diff --git a/src/audio/tdfb/tdfb_comp.h b/src/audio/tdfb/tdfb_comp.h index 9e0727474f4a..f59ff48bacaf 100644 --- a/src/audio/tdfb/tdfb_comp.h +++ b/src/audio/tdfb/tdfb_comp.h @@ -88,6 +88,8 @@ struct tdfb_comp_data { int16_t *output_stream_mix; /**< for each FIR define stream */ int16_t az_value; /**< beam steer azimuth as in control enum */ int16_t az_value_estimate; /**< beam steer azimuth as in control enum */ + int16_t source_channels; /**< source channel count cached for validator */ + int16_t sink_channels; /**< sink channel count cached for validator */ size_t config_size; /**< size of the configuration blob */ size_t fir_delay_size; /**< allocated size */ unsigned int max_frames; /**< max frames to process */ diff --git a/src/audio/webrtc_aec/CMakeLists.txt b/src/audio/webrtc_aec/CMakeLists.txt new file mode 100644 index 000000000000..e42a8644a0b2 --- /dev/null +++ b/src/audio/webrtc_aec/CMakeLists.txt @@ -0,0 +1,13 @@ +# SPDX-License-Identifier: BSD-3-Clause + +if(CONFIG_COMP_WEBRTC_AEC STREQUAL "m" AND DEFINED CONFIG_LLEXT) + add_subdirectory(llext ${PROJECT_BINARY_DIR}/webrtc_aec_llext) + add_dependencies(app webrtc_aec) +else() + add_local_sources(sof webrtc_aec.c) + if(CONFIG_COMP_WEBRTC_AEC_STUB) + add_local_sources(sof webrtc_aec-stub.c) + else() + add_local_sources(sof webrtc_aec-aecm.c) + endif() +endif() diff --git a/src/audio/webrtc_aec/Kconfig b/src/audio/webrtc_aec/Kconfig new file mode 100644 index 000000000000..7f3602d6cbf3 --- /dev/null +++ b/src/audio/webrtc_aec/Kconfig @@ -0,0 +1,66 @@ +# SPDX-License-Identifier: BSD-3-Clause + +config COMP_WEBRTC_AEC + tristate "WebRTC AECm — fixed-point Acoustic Echo Canceller" + help + Select to include the WebRTC AECm (Mobile) acoustic echo canceller. + AECm is a fixed-point Q15 implementation derived from the WebRTC + audio processing library. It requires no floating-point unit and is + well suited to Xtensa HiFi3/HiFi4 class DSPs. + + The module accepts two input streams (2 input pins): + Pin 0 — microphone capture (same pipeline as the output) + Pin 1 — playback echo reference (from the render pipeline) + and produces one denoised microphone output stream. + + This module is intended as an open-source drop-in replacement for + the google_rtc_audio_processing module that requires a proprietary + library. Both implement the same two-input / one-output topology so + topologies are interchangeable. + + The real backend requires webrtc-audio-processing 0.3.x source + (west module modules/audio/webrtc-apm, same as COMP_WEBRTC_NS). + For CI/testing without that source, select COMP_WEBRTC_AEC_STUB. + +if COMP_WEBRTC_AEC + +config COMP_WEBRTC_AEC_STUB + bool "WebRTC AECm stub backend (no WebRTC source dependency)" + default y if COMP_STUBS + help + Build the webrtc_aec module against a dependency-free stub backend + that passes microphone audio through without modification. Useful + for CI validation, topology testing, and LLEXT packaging without + the cross-built WebRTC AECm archive. + +config WEBRTC_AEC_FILTER_LEN_MS + int "AECm adaptive filter length in ms (32, 64 or 128)" + default 64 + help + Length of the adaptive echo-cancellation filter. Longer filters + can handle larger acoustic echo delays (e.g. in rooms with long + reverb tails) at the cost of increased memory and CPU. + Valid values: 32, 64, 128. + +config WEBRTC_AEC_SUPPRESSION_LEVEL + int "AECm comfort-noise suppression level (0=off, 1=low, 2=medium)" + range 0 2 + default 1 + help + Controls how aggressively residual echo / background noise is + suppressed in the CNG (comfort noise generator) post-filter: + 0 - disabled (only echo suppression, no CNG) + 1 - low suppression (recommended) + 2 - medium suppression + +config WEBRTC_AEC_SAMPLE_RATE_HZ + int "AECm processing sample rate in Hz" + default 16000 + help + AECm operates at 8000 or 16000 Hz. When the pipeline runs at a + higher rate (e.g. 48 kHz), the module downsamples before AEC + processing and upsamples afterward. 16 kHz is recommended for + best echo suppression quality. + Valid values: 8000, 16000. + +endif # COMP_WEBRTC_AEC diff --git a/src/audio/webrtc_aec/README.md b/src/audio/webrtc_aec/README.md new file mode 100644 index 000000000000..4fa1e045b326 --- /dev/null +++ b/src/audio/webrtc_aec/README.md @@ -0,0 +1,104 @@ +# WebRTC Acoustic Echo Cancellation Module (`webrtc_aec`) + +Wraps the fixed-point mobile echo canceller (AECm) from [webrtc-audio-processing](https://gitlab.freedesktop.org/gstreamer/webrtc-audio-processing) behind the SOF `module_interface`. + +--- + +## Features + +- **Dual-Input Processing**: Connects both the microphone capture stream (Pin 0) and the speaker playback reference stream (Pin 1). +- **AECm Algorithm**: Highly optimized Q15 fixed-point echo cancellation; runs in real-time on low-power embedded DSPs without an FPU. +- **Dynamic Routing**: Automatic stream alignment using SOF pipeline-ID mapping. +- **Multichannel Independent Filtering**: Allocates one `AecmCore` handle per channel, supporting independent echo cancellation across multiple microphone paths. +- **Rate Options**: Natively supports 8 kHz and 16 kHz sample rates (requires matching rates for both microphone and speaker reference inputs). +- **Frame Buffer**: Synchronizes inputs and processes audio in 10 ms blocks. + +--- + +## Architecture & Data Flow + +The following Mermaid diagram explains how the dual input pins (Microphone and Speaker Reference) are processed and aligned inside the module: + +```mermaid +graph TD + %% Microphone capture path (Pin 0) + MicIn[Microphone Input Pin 0] -->|S16/S32 PCM| Core[webrtc_aec.c Core] + + %% Speaker playback reference path (Pin 1) + RefIn[Speaker Reference Pin 1] -->|S16 PCM| Core + + %% Buffering & Alignment + Core -->|Accumulate Mic| MicAccum[Mic Frame Buffer 10ms] + Core -->|Accumulate Ref| RefAccum[Ref Frame Buffer 10ms] + + MicAccum -->|Plane S16| Backend[webrtc_aec-aecm.c Backend] + RefAccum -->|Plane S16| Backend + + %% AEC Processing + Backend -->|WebRtcAecm_BufferFarend| AECM[AecmCore Channel Instance] + Backend -->|WebRtcAecm_Process| AECM + + AECM -->|Clean Mic Audio| Backend + Backend -->|Scale & Interleave| Core + Core -->|Echo Cancelled Output| OutPin[Output Pin 0] +``` + +### Components +- `webrtc_aec.c`: Core SOF wrapper handling dual-source mapping, preparing format layouts, and period accumulator synchronization. +- `webrtc_aec-aecm.c`: Real AECm library integration translation unit. +- `webrtc_aec-stub.c`: Dependency-free pass-through stub. +- `webrtc_aec.cmake`: CMake compiler scripting pulling the WebRTC AECm codebase from the `webrtc-apm` dependency. + +--- + +## Build Instructions + +### 1. Stub Mode (CI / Staging) +```ini +CONFIG_COMP_WEBRTC_AEC=y # or =m for LLEXT +CONFIG_COMP_WEBRTC_AEC_STUB=y +``` + +### 2. Real AECm Integration +Integrates the WebRTC AECm engine: +```ini +CONFIG_COMP_WEBRTC_AEC=m +CONFIG_COMP_WEBRTC_AEC_STUB=n +``` +*Note: Make sure to run `west update` to retrieve `modules/audio/webrtc-apm` before compiling.* + +--- + +## Kconfig Parameters + +| Option | Default | Range / Value | Description | +|---|---|---|---| +| `CONFIG_COMP_WEBRTC_AEC` | n | y / m / n | Enable WebRTC AECm module | +| `CONFIG_COMP_WEBRTC_AEC_STUB` | y | y / n | Use pass-through stub | +| `CONFIG_WEBRTC_AEC_CHANNELS_MAX` | 2 | 1 - 8 | Maximum channels supported | +| `CONFIG_WEBRTC_AEC_ROUTING_HEURISTIC` | y | y / n | Use pipeline-ID matching | + +--- + +## Usage & Topology + +### Topology Connections +AEC requires two input pin bindings: +1. **Pin 0 (Capture)**: Fed by the Microphone SSP DAI. +2. **Pin 1 (Reference)**: Fed by the Speaker SSP DAI (cross-pipeline). + +#### Example Topology Code snippet: +``` +Object.Widget.webrtc-aec.1 { + # Pin 0: Microphone Capture + Object.Base.input_pin_binding.1 { + input_pin_binding_name "dai-copier.SSP.NoCodec-0.capture" + } + # Pin 1: Speaker Reference + Object.Base.input_pin_binding.2 { + input_pin_binding_name "dai-copier.SSP.NoCodec-2.capture" + } +} +``` + +Both input streams must be active and running at the same sample rate (8 kHz or 16 kHz) for the echo canceller to align and filter the signals. diff --git a/src/audio/webrtc_aec/llext/CMakeLists.txt b/src/audio/webrtc_aec/llext/CMakeLists.txt new file mode 100644 index 000000000000..0786faec8ca6 --- /dev/null +++ b/src/audio/webrtc_aec/llext/CMakeLists.txt @@ -0,0 +1,24 @@ +# Copyright (c) 2026 Intel Corporation. +# SPDX-License-Identifier: Apache-2.0 + +if(CONFIG_COMP_WEBRTC_AEC_STUB) + sof_llext_build("webrtc_aec" + SOURCES ../webrtc_aec.c + ../webrtc_aec-stub.c + LIB openmodules + ) +else() + include(${CMAKE_CURRENT_LIST_DIR}/../webrtc_aec.cmake) + + sof_llext_build("webrtc_aec" + SOURCES ../webrtc_aec.c + ../webrtc_aec-aecm.c + INCLUDES "${WEBRTC_AECM_INSTALL_DIR}/include" + LIBS_PATH "${WEBRTC_AECM_INSTALL_DIR}/lib" + LIBS webrtc_aecm + LIB openmodules + ) + + add_dependencies(webrtc_aec_llext_lib webrtc_aecm_ext) + add_dependencies(webrtc_aec webrtc_aecm_ext) +endif() diff --git a/src/audio/webrtc_aec/llext/llext.toml.h b/src/audio/webrtc_aec/llext/llext.toml.h new file mode 100644 index 000000000000..154329ba4e89 --- /dev/null +++ b/src/audio/webrtc_aec/llext/llext.toml.h @@ -0,0 +1,6 @@ +#include +#define LOAD_TYPE "2" +#include "../webrtc_aec.toml" + +[module] +count = __COUNTER__ diff --git a/src/audio/webrtc_aec/webrtc_aec-aecm.c b/src/audio/webrtc_aec/webrtc_aec-aecm.c new file mode 100644 index 000000000000..c8b60678746b --- /dev/null +++ b/src/audio/webrtc_aec/webrtc_aec-aecm.c @@ -0,0 +1,178 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Real AECm backend for the webrtc_aec module. +// +// Wraps WebRtcAecm_Create/Init/BufferFarend/Process from the WebRTC AECm +// (Mobile) fixed-point library extracted from webrtc-audio-processing 0.3.x. +// +// API summary: +// WebRtcAecm_Create() — allocate AecmCore handle +// WebRtcAecm_Init() — configure sample rate (8000 or 16000 Hz) +// WebRtcAecm_BufferFarend() — queue 10 ms of echo reference (far-end) +// WebRtcAecm_Process() — cancel echo from near-end mic frame +// WebRtcAecm_Enable() — configure filter length and CNG suppression +// WebRtcAecm_Free() — release handle +// +// All operations are Q15 fixed-point on int16_t. One handle per channel. + +#include +#include +#include "webrtc_aec.h" + +#include /* from cross-built webrtc-audio-processing 0.3.x */ + +LOG_MODULE_DECLARE(webrtc_aec, CONFIG_SOF_LOG_LEVEL); + +struct webrtc_aec_real_data { + void *aecm[WEBRTC_AEC_CHANNELS_MAX]; + int num_channels; + int filter_len_ms; + int suppression; +}; + +static int webrtc_aec_real_init(struct processing_module *mod) +{ + struct webrtc_aec_comp_data *cd = module_get_private_data(mod); + struct webrtc_aec_real_data *rd; + + rd = mod_zalloc(mod, sizeof(*rd)); + if (!rd) + return -ENOMEM; + + cd->backend_data = rd; + comp_info(mod->dev, "webrtc_aec: AECm real backend initialised"); + return 0; +} + +static int webrtc_aec_real_configure(struct processing_module *mod, int sample_rate_hz, + int filter_len_ms, int suppression, int num_channels) +{ + struct webrtc_aec_comp_data *cd = module_get_private_data(mod); + struct webrtc_aec_real_data *rd = cd->backend_data; + AecmConfig config; + int c, ret; + + /* Free previously allocated handles. */ + for (c = 0; c < rd->num_channels; c++) { + if (rd->aecm[c]) { + WebRtcAecm_Free(rd->aecm[c]); + rd->aecm[c] = NULL; + } + } + rd->num_channels = 0; + rd->filter_len_ms = filter_len_ms; + rd->suppression = suppression; + + for (c = 0; c < num_channels; c++) { + rd->aecm[c] = WebRtcAecm_Create(); + if (!rd->aecm[c]) { + comp_err(mod->dev, "webrtc_aec: WebRtcAecm_Create() failed ch%d", c); + goto err; + } + + ret = WebRtcAecm_Init(rd->aecm[c], sample_rate_hz); + if (ret) { + comp_err(mod->dev, + "webrtc_aec: WebRtcAecm_Init(ch%d, %d) failed %d", + c, sample_rate_hz, ret); + goto err; + } + + config.cngMode = suppression; + config.echoMode = (filter_len_ms == 128) ? 4 : + (filter_len_ms == 64) ? 3 : + (filter_len_ms == 32) ? 2 : 3; + ret = WebRtcAecm_set_config(rd->aecm[c], config); + if (ret) { + comp_err(mod->dev, "webrtc_aec: set_config ch%d failed %d", c, ret); + goto err; + } + } + + rd->num_channels = num_channels; + comp_info(mod->dev, "webrtc_aec: AECm rate=%d filter=%dms cng=%d ch=%d", + sample_rate_hz, filter_len_ms, suppression, num_channels); + return 0; + +err: + for (c = 0; c < num_channels; c++) { + if (rd->aecm[c]) { + WebRtcAecm_Free(rd->aecm[c]); + rd->aecm[c] = NULL; + } + } + return -ENOMEM; +} + +static int webrtc_aec_real_process_ch(struct processing_module *mod, + const int16_t *mic, const int16_t *ref, int16_t *out, + int frame_samples, int ch) +{ + struct webrtc_aec_comp_data *cd = module_get_private_data(mod); + struct webrtc_aec_real_data *rd = cd->backend_data; + int ret; + + /* Queue the far-end (reference/playback) frame first. */ + ret = WebRtcAecm_BufferFarend(rd->aecm[ch], ref, frame_samples); + if (ret) { + comp_err(mod->dev, "webrtc_aec: BufferFarend ch%d failed %d", ch, ret); + return ret; + } + + /* Process near-end (mic) and produce echo-cancelled output. + * The third parameter (near_end_noiseless) can be NULL. */ + ret = WebRtcAecm_Process(rd->aecm[ch], mic, NULL, out, frame_samples, 0); + if (ret) { + comp_err(mod->dev, "webrtc_aec: Process ch%d failed %d", ch, ret); + return ret; + } + + return 0; +} + +static int webrtc_aec_real_reset(struct processing_module *mod) +{ + struct webrtc_aec_comp_data *cd = module_get_private_data(mod); + struct webrtc_aec_real_data *rd = cd->backend_data; + int c, ret; + + for (c = 0; c < rd->num_channels; c++) { + if (!rd->aecm[c]) + continue; + ret = WebRtcAecm_Init(rd->aecm[c], cd->proc_rate); + if (ret) + comp_warn(mod->dev, "webrtc_aec: reset ch%d failed %d", c, ret); + } + return 0; +} + +static int webrtc_aec_real_free(struct processing_module *mod) +{ + struct webrtc_aec_comp_data *cd = module_get_private_data(mod); + struct webrtc_aec_real_data *rd = cd->backend_data; + int c; + + if (!rd) + return 0; + + for (c = 0; c < rd->num_channels; c++) { + if (rd->aecm[c]) { + WebRtcAecm_Free(rd->aecm[c]); + rd->aecm[c] = NULL; + } + } + mod_free(mod, rd); + cd->backend_data = NULL; + return 0; +} + +const struct webrtc_aec_backend webrtc_aec_backend = { + .name = "aecm", + .init = webrtc_aec_real_init, + .configure = webrtc_aec_real_configure, + .process_ch = webrtc_aec_real_process_ch, + .reset = webrtc_aec_real_reset, + .free = webrtc_aec_real_free, +}; diff --git a/src/audio/webrtc_aec/webrtc_aec-stub.c b/src/audio/webrtc_aec/webrtc_aec-stub.c new file mode 100644 index 000000000000..dce0b9bd7d2e --- /dev/null +++ b/src/audio/webrtc_aec/webrtc_aec-stub.c @@ -0,0 +1,59 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Stub backend for webrtc_aec: passes mic audio through unchanged. +// Reference audio is consumed and discarded. + +#include +#include +#include +#include "webrtc_aec.h" + +LOG_MODULE_DECLARE(webrtc_aec, CONFIG_SOF_LOG_LEVEL); + +static int webrtc_aec_stub_init(struct processing_module *mod) +{ + comp_info(mod->dev, "webrtc_aec stub: no AECm library linked"); + return 0; +} + +static int webrtc_aec_stub_configure(struct processing_module *mod, int sample_rate_hz, + int filter_len_ms, int suppression, int num_channels) +{ + comp_info(mod->dev, "webrtc_aec stub: rate=%d filter=%dms sup=%d ch=%d (ignored)", + sample_rate_hz, filter_len_ms, suppression, num_channels); + return 0; +} + +static int webrtc_aec_stub_process_ch(struct processing_module *mod, + const int16_t *mic, const int16_t *ref, int16_t *out, + int frame_samples, int ch) +{ + /* Pass mic straight to output; ref is silently discarded. */ + memcpy(out, mic, (size_t)frame_samples * sizeof(int16_t)); + (void)ref; + (void)ch; + return 0; +} + +static int webrtc_aec_stub_reset(struct processing_module *mod) +{ + (void)mod; + return 0; +} + +static int webrtc_aec_stub_free(struct processing_module *mod) +{ + (void)mod; + return 0; +} + +const struct webrtc_aec_backend webrtc_aec_backend = { + .name = "stub", + .init = webrtc_aec_stub_init, + .configure = webrtc_aec_stub_configure, + .process_ch = webrtc_aec_stub_process_ch, + .reset = webrtc_aec_stub_reset, + .free = webrtc_aec_stub_free, +}; diff --git a/src/audio/webrtc_aec/webrtc_aec.c b/src/audio/webrtc_aec/webrtc_aec.c new file mode 100644 index 000000000000..1f7d796fa30a --- /dev/null +++ b/src/audio/webrtc_aec/webrtc_aec.c @@ -0,0 +1,371 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// WebRTC AECm module — SOF module_interface core. +// +// This is the SOF glue layer for the AECm fixed-point echo canceller. +// The dual-source topology and source-routing heuristic are copied +// directly from google_rtc_audio_processing.c which is the canonical +// reference for this pattern in SOF. +// +// Key differences from google_rtc_audio_processing: +// - Uses fixed-point S16 throughout (no float intermediate buffers) +// - AECm runs per-channel independently, not as a multi-channel block +// - Operates at 8 or 16 kHz maximum (pipeline downsampling is out-of-scope +// for this first revision — see CONFIG_WEBRTC_AEC_SAMPLE_RATE_HZ) + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "webrtc_aec.h" + +SOF_DEFINE_REG_UUID(webrtc_aec); +LOG_MODULE_REGISTER(webrtc_aec, CONFIG_SOF_LOG_LEVEL); + +/* ---------------------------------------------------------------- + * Helper: convert between S32/S16 interleaved ↔ per-channel S16 + * (matching the style of google_rtc_audio_processing.c) + * ---------------------------------------------------------------- */ + +/** + * src_to_s16() - Copy frames from a source into per-channel S16 scratch. + * Handles both S16 and S32 pipeline formats. + * @src: SOF source (provides source_get_data / source_release_data) + * @n: number of frames to consume + * @dst: per-channel S16 destination (ch × frame_samples) + * @frame0: offset within dst at which to start writing + * @ch: number of channels + * @is_s32: true if the pipeline format is S32_LE + */ +static void src_to_s16(struct sof_source *src, int n, + int16_t dst[][WEBRTC_AEC_FRAME_SAMPLES_MAX], + int frame0, int ch, bool is_s32) +{ + size_t sample_sz = is_s32 ? sizeof(int32_t) : sizeof(int16_t); + size_t nbytes = (size_t)n * ch * sample_sz; + const char *buf, *bufstart; + size_t bufsz; + int i, c, err; + + err = source_get_data(src, nbytes, (void *)&buf, (void *)&bufstart, &bufsz); + if (err) + return; /* shouldn't happen if caller checked availability */ + + for (i = 0; i < n; i++) { + for (c = 0; c < ch; c++) { + if (is_s32) { + int32_t s = *(const int32_t *)buf; + + dst[c][frame0 + i] = (int16_t)(s >> 16); + buf += sizeof(int32_t); + } else { + dst[c][frame0 + i] = *(const int16_t *)buf; + buf += sizeof(int16_t); + } + } + /* Wrap circular buffer. */ + if (buf >= bufstart + bufsz) + buf = bufstart; + } + + source_release_data(src, nbytes); +} + +/** + * s16_to_sink() - Write per-channel S16 output to a SOF sink. + * Upshifts to S32 if the pipeline format requires it. + * @dst: SOF sink + * @src: per-channel S16 source (ch × frame_samples, from index 0) + * @n: number of frames to write + * @ch: number of channels + * @is_s32: true if the pipeline format is S32_LE + */ +static void s16_to_sink(struct sof_sink *dst, int16_t src[][WEBRTC_AEC_FRAME_SAMPLES_MAX], + int n, int ch, bool is_s32) +{ + size_t sample_sz = is_s32 ? sizeof(int32_t) : sizeof(int16_t); + size_t nbytes = (size_t)n * ch * sample_sz; + char *buf, *bufstart; + size_t bufsz; + int i, c, err; + + err = sink_get_buffer(dst, nbytes, (void *)&buf, (void *)&bufstart, &bufsz); + if (err) + return; + + for (i = 0; i < n; i++) { + for (c = 0; c < ch; c++) { + if (is_s32) { + *(int32_t *)buf = (int32_t)src[c][i] << 16; + buf += sizeof(int32_t); + } else { + *(int16_t *)buf = src[c][i]; + buf += sizeof(int16_t); + } + } + if (buf >= bufstart + bufsz) + buf = bufstart; + } + + sink_commit_buffer(dst, nbytes); +} + +/* ---------------------------------------------------------------- + * module_interface operations + * ---------------------------------------------------------------- */ + +__cold static int webrtc_aec_init(struct processing_module *mod) +{ + struct module_data *md = &mod->priv; + struct comp_dev *dev = mod->dev; + struct webrtc_aec_comp_data *cd; + int ret; + + assert_can_be_cold(); + comp_info(dev, "webrtc_aec: init"); + + cd = mod_zalloc(mod, sizeof(*cd)); + if (!cd) + return -ENOMEM; + + md->private = cd; + cd->backend = &webrtc_aec_backend; + + /* Two input pins: mic + echo reference. */ + mod->max_sources = 2; + + comp_info(dev, "webrtc_aec: backend '%s'", cd->backend->name); + + if (cd->backend->init) { + ret = cd->backend->init(mod); + if (ret) { + comp_err(dev, "webrtc_aec: backend init failed %d", ret); + mod_free(mod, cd); + return ret; + } + } + + return 0; +} + +__cold static int webrtc_aec_prepare(struct processing_module *mod, + struct sof_source **sources, int num_of_sources, + struct sof_sink **sinks, int num_of_sinks) +{ + struct webrtc_aec_comp_data *cd = module_get_private_data(mod); + struct comp_dev *dev = mod->dev; + int mic_fmt, ref_fmt, mic_rate, ref_rate, ret; + + assert_can_be_cold(); + + if (num_of_sources != 2 || num_of_sinks != 1) { + comp_err(dev, "webrtc_aec: need 2 sources and 1 sink (got %d/%d)", + num_of_sources, num_of_sinks); + return -EINVAL; + } + + /* + * Resolve which source is mic and which is echo reference. + * The mic is the one on the same pipeline as the output sink. + * This matches google_rtc_audio_processing.c exactly. + */ + cd->ref_src = (source_get_pipeline_id(sources[0]) == sink_get_pipeline_id(sinks[0])); + cd->mic_src = cd->ref_src ? 0 : 1; + + mic_fmt = source_get_frm_fmt(sources[cd->mic_src]); + ref_fmt = source_get_frm_fmt(sources[cd->ref_src]); + mic_rate = source_get_rate(sources[cd->mic_src]); + ref_rate = source_get_rate(sources[cd->ref_src]); + cd->channels = source_get_channels(sources[cd->mic_src]); + + if (cd->channels > WEBRTC_AEC_CHANNELS_MAX) { + comp_err(dev, "webrtc_aec: too many channels %d (max %d)", + cd->channels, WEBRTC_AEC_CHANNELS_MAX); + return -EINVAL; + } + + if (mic_rate != ref_rate) { + comp_err(dev, "webrtc_aec: mic_rate %d != ref_rate %d", mic_rate, ref_rate); + return -EINVAL; + } + cd->rate = mic_rate; + + /* AECm supports only 8 and 16 kHz natively. */ + cd->proc_rate = CONFIG_WEBRTC_AEC_SAMPLE_RATE_HZ; + if (cd->proc_rate != 8000 && cd->proc_rate != 16000) { + comp_err(dev, "webrtc_aec: invalid proc_rate %d (must be 8000 or 16000)", + cd->proc_rate); + return -EINVAL; + } + + if (cd->rate != cd->proc_rate) { + comp_warn(dev, "webrtc_aec: pipeline rate %d != AECm rate %d; " + "pipeline resampling required upstream", cd->rate, cd->proc_rate); + } + + if ((mic_fmt != SOF_IPC_FRAME_S16_LE && mic_fmt != SOF_IPC_FRAME_S32_LE) || + (ref_fmt != SOF_IPC_FRAME_S16_LE && ref_fmt != SOF_IPC_FRAME_S32_LE)) { + comp_err(dev, "webrtc_aec: unsupported format mic=%d ref=%d", + mic_fmt, ref_fmt); + return -EINVAL; + } + + cd->is_s32 = (mic_fmt == SOF_IPC_FRAME_S32_LE); + cd->mic_frame_bytes = source_get_frame_bytes(sources[cd->mic_src]); + cd->ref_frame_bytes = source_get_frame_bytes(sources[cd->ref_src]); + cd->out_frame_bytes = sink_get_frame_bytes(sinks[0]); + cd->frame_samples = (cd->proc_rate * 10) / 1000; /* 10 ms */ + + if (cd->frame_samples > WEBRTC_AEC_FRAME_SAMPLES_MAX) { + comp_err(dev, "webrtc_aec: frame_samples %d exceeds max %d", + cd->frame_samples, WEBRTC_AEC_FRAME_SAMPLES_MAX); + return -EINVAL; + } + + comp_info(dev, "webrtc_aec: mic_src=%d ref_src=%d rate=%d/%d ch=%d frame=%d %s", + cd->mic_src, cd->ref_src, cd->rate, cd->proc_rate, cd->channels, + cd->frame_samples, cd->is_s32 ? "S32" : "S16"); + +#ifdef CONFIG_IPC_MAJOR_4 + /* Apply reference format override from topology pin descriptor. */ + ipc4_update_source_format(sources[cd->ref_src], + &mod->priv.cfg.input_pins[1].audio_fmt); +#endif + + if (cd->backend->configure) { + ret = cd->backend->configure(mod, cd->proc_rate, + CONFIG_WEBRTC_AEC_FILTER_LEN_MS, + CONFIG_WEBRTC_AEC_SUPPRESSION_LEVEL, + cd->channels); + if (ret) { + comp_err(dev, "webrtc_aec: backend configure failed %d", ret); + return ret; + } + } + + cd->buffered_frames = 0; + cd->last_ref_ok = true; + cd->configured = true; + return 0; +} + +static int webrtc_aec_process(struct processing_module *mod, + struct sof_source **sources, int num_of_sources, + struct sof_sink **sinks, int num_of_sinks) +{ + struct webrtc_aec_comp_data *cd = module_get_private_data(mod); + struct sof_source *mic = sources[cd->mic_src]; + struct sof_source *ref = sources[cd->ref_src]; + struct sof_sink *out = sinks[0]; + int fmic = (int)source_get_data_frames_available(mic); + int fref = (int)source_get_data_frames_available(ref); + int frames = MIN(fmic, fref); + int n, frames_rem; + + for (frames_rem = frames; frames_rem > 0; frames_rem -= n) { + /* Consume at most what fills one complete AECm frame. */ + n = MIN(frames_rem, cd->frame_samples - cd->buffered_frames); + + /* Convert source data → per-channel S16 accumulators. */ + src_to_s16(mic, n, cd->mic_buf, cd->buffered_frames, cd->channels, cd->is_s32); + src_to_s16(ref, n, cd->ref_buf, cd->buffered_frames, cd->channels, cd->is_s32); + + cd->buffered_frames += n; + + /* Once we have a full 10 ms block, run AECm per channel. */ + if (cd->buffered_frames >= cd->frame_samples) { + int fs = cd->frame_samples; + + /* Check output headroom. */ + if (sink_get_free_size(out) < (size_t)(fs * cd->out_frame_bytes)) { + comp_warn(mod->dev, "webrtc_aec: sink backed up!"); + break; + } + + int c, ret; + + for (c = 0; c < cd->channels; c++) { + ret = cd->backend->process_ch(mod, + cd->mic_buf[c], + cd->ref_buf[c], + cd->out_buf[c], + fs, c); + if (ret) { + /* Fall back to mic pass-through for this channel. */ + memcpy(cd->out_buf[c], cd->mic_buf[c], + (size_t)fs * sizeof(int16_t)); + } + } + + /* Write denoised output to sink. */ + s16_to_sink(out, cd->out_buf, fs, cd->channels, cd->is_s32); + cd->buffered_frames = 0; + } + } + + cd->last_ref_ok = true; + return 0; +} + +static int webrtc_aec_reset(struct processing_module *mod) +{ + struct webrtc_aec_comp_data *cd = module_get_private_data(mod); + + comp_dbg(mod->dev, "webrtc_aec: reset"); + cd->buffered_frames = 0; + + if (cd->backend->reset) + return cd->backend->reset(mod); + + return 0; +} + +__cold static int webrtc_aec_free(struct processing_module *mod) +{ + struct webrtc_aec_comp_data *cd = module_get_private_data(mod); + + assert_can_be_cold(); + comp_dbg(mod->dev, "webrtc_aec: free"); + + if (cd->backend->free) + cd->backend->free(mod); + + mod_free(mod, cd); + return 0; +} + +static const struct module_interface webrtc_aec_interface = { + .init = webrtc_aec_init, + .prepare = webrtc_aec_prepare, + .process = webrtc_aec_process, + .reset = webrtc_aec_reset, + .free = webrtc_aec_free, +}; + +#if CONFIG_COMP_WEBRTC_AEC_MODULE + +#include +#include +#include + +static const struct sof_man_module_manifest mod_manifest __section(".module") __used = + SOF_LLEXT_MODULE_MANIFEST("WRTCAEC", &webrtc_aec_interface, 2, + SOF_REG_UUID(webrtc_aec), 40); + +SOF_LLEXT_BUILDINFO; + +#else + +DECLARE_TR_CTX(webrtc_aec_tr, SOF_UUID(webrtc_aec_uuid), LOG_LEVEL_INFO); +DECLARE_MODULE_ADAPTER(webrtc_aec_interface, webrtc_aec_uuid, webrtc_aec_tr); +SOF_MODULE_INIT(webrtc_aec, sys_comp_module_webrtc_aec_interface_init); + +#endif diff --git a/src/audio/webrtc_aec/webrtc_aec.cmake b/src/audio/webrtc_aec/webrtc_aec.cmake new file mode 100644 index 000000000000..2753ab018fc2 --- /dev/null +++ b/src/audio/webrtc_aec/webrtc_aec.cmake @@ -0,0 +1,85 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Cross-build the WebRTC AECm (echo_control_mobile) fixed-point C sources +# for the webrtc_aec LLEXT module. +# +# Source: webrtc-audio-processing 0.3.x (same west module as webrtc_ns). +# The AECm module lives under webrtc/modules/audio_processing/aecm/ and +# depends on webrtc/common_audio/signal_processing/ (same as NS). +# +# Produces: +# ${WEBRTC_AECM_INSTALL_DIR}/lib/libwebrtc_aecm.a +# ${WEBRTC_AECM_INSTALL_DIR}/include/echo_control_mobile.h + +if(NOT DEFINED SOF_WEBRTC_APM_SRC_DIR) + set(SOF_WEBRTC_APM_SRC_DIR "${sof_top_dir}/../modules/audio/webrtc-apm" + CACHE PATH "webrtc-audio-processing 0.3.x source directory (west-pinned)") +endif() +cmake_path(NORMAL_PATH SOF_WEBRTC_APM_SRC_DIR) +if(NOT EXISTS "${SOF_WEBRTC_APM_SRC_DIR}/webrtc/modules/audio_processing/aecm/aecm_core.c") + message(FATAL_ERROR + "webrtc_aec: AECm source not found at '${SOF_WEBRTC_APM_SRC_DIR}'.\n" + "Run 'west update' or pass -DSOF_WEBRTC_APM_SRC_DIR=.") +endif() + +get_filename_component(_tc_dir "${CMAKE_C_COMPILER}" DIRECTORY) +get_filename_component(_tc_name "${CMAKE_C_COMPILER}" NAME) +string(REGEX REPLACE "gcc$" "" _tc_prefix_name "${_tc_name}") +set(_aecm_cross_prefix "${_tc_dir}/${_tc_prefix_name}") + +set(WEBRTC_AECM_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/webrtc-aecm-install" + CACHE INTERNAL "webrtc_aec: AECm library install prefix") + +set(_aecm_src "${SOF_WEBRTC_APM_SRC_DIR}/webrtc") + +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/webrtc-aecm-build.sh" +"#!/bin/sh +set -e +SRC=${_aecm_src} +INST=${WEBRTC_AECM_INSTALL_DIR} +OBJ=${CMAKE_CURRENT_BINARY_DIR}/webrtc-aecm-obj + +mkdir -p \"$OBJ\" \"$INST/lib\" \"$INST/include\" + +CFLAGS=\"-O2 -fPIC -I$SRC -I$SRC/common_audio/signal_processing/include\" + +# AECm core +SRCS=\" + $SRC/modules/audio_processing/aecm/aecm_core.c + $SRC/modules/audio_processing/aecm/echo_control_mobile.c + $SRC/common_audio/signal_processing/complex_bit_reverse.c + $SRC/common_audio/signal_processing/complex_fft.c + $SRC/common_audio/signal_processing/cross_correlation.c + $SRC/common_audio/signal_processing/division_operations.c + $SRC/common_audio/signal_processing/downsample_fast.c + $SRC/common_audio/signal_processing/energy.c + $SRC/common_audio/signal_processing/get_scaling_square.c + $SRC/common_audio/signal_processing/min_max_operations.c + $SRC/common_audio/signal_processing/real_fft.c + $SRC/common_audio/signal_processing/resample_48khz.c + $SRC/common_audio/signal_processing/resample_by_2_internal.c + $SRC/common_audio/signal_processing/resample_fractional.c + $SRC/common_audio/signal_processing/spl_inl.c + $SRC/common_audio/signal_processing/sqrt_of_one_minus_x_squared.c + $SRC/common_audio/signal_processing/vector_scaling_operations.c +\" + +for f in \$SRCS; do + bn=\$(basename \"\$f\" .c) + ${CMAKE_C_COMPILER} \$CFLAGS -c \"\$f\" -o \"$OBJ/\${bn}.o\" +done + +${_aecm_cross_prefix}ar rcs \"$INST/lib/libwebrtc_aecm.a\" \"$OBJ\"/*.o +cp $SRC/modules/audio_processing/aecm/echo_control_mobile.h \"$INST/include/\" +") + +add_custom_command( + OUTPUT "${WEBRTC_AECM_INSTALL_DIR}/lib/libwebrtc_aecm.a" + "${WEBRTC_AECM_INSTALL_DIR}/include/echo_control_mobile.h" + COMMAND sh "${CMAKE_CURRENT_BINARY_DIR}/webrtc-aecm-build.sh" + VERBATIM) + +add_custom_target(webrtc_aecm_ext + DEPENDS "${WEBRTC_AECM_INSTALL_DIR}/lib/libwebrtc_aecm.a") + +message(STATUS "webrtc_aec: cross-building AECm from ${SOF_WEBRTC_APM_SRC_DIR}") diff --git a/src/audio/webrtc_aec/webrtc_aec.h b/src/audio/webrtc_aec/webrtc_aec.h new file mode 100644 index 000000000000..2832f21339a2 --- /dev/null +++ b/src/audio/webrtc_aec/webrtc_aec.h @@ -0,0 +1,122 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright(c) 2026 Intel Corporation. + * + * WebRTC AECm (fixed-point Acoustic Echo Canceller Mobile) for SOF. + * + * Two input pins: + * sources[mic_src] — microphone capture (same pipeline as the sink) + * sources[ref_src] — playback reference (different render pipeline) + * One output pin: + * sinks[0] — echo-cancelled microphone + * + * The mic/ref source indices are resolved at prepare() time using the same + * pipeline-ID heuristic as google_rtc_audio_processing.c. + * + * The actual AEC processing is delegated to a pluggable backend so the + * module can be validated with a stub (webrtc_aec-stub.c) before the + * real AECm backend (webrtc_aec-webrtc.c) is linked in. + */ +#ifndef __SOF_AUDIO_WEBRTC_AEC_H__ +#define __SOF_AUDIO_WEBRTC_AEC_H__ + +#include +#include +#include +#include + +/* AECm operates on 10 ms frames. Max 16 kHz × 10 ms = 160 samples/channel. */ +#define WEBRTC_AEC_FRAME_SAMPLES_MAX 160 + +/* Maximum supported channel count (AECm is mono; multi-ch runs N instances). */ +#define WEBRTC_AEC_CHANNELS_MAX 4 + +/* Maximum DMA buffer alignment. */ +#define WEBRTC_AEC_MEM_ALIGN 64 + +/** + * struct webrtc_aec_backend - AECm backend operations. + */ +struct webrtc_aec_backend { + const char *name; + + /* One-time allocation, called from init(). */ + int (*init)(struct processing_module *mod); + + /** + * Open/configure AECm state. Called from prepare(). + * @sample_rate_hz: 8000 or 16000 + * @filter_len_ms: adaptive filter length (32/64/128) + * @suppression: CNG level 0..2 + * @num_channels: mic and ref channel count (same; AECm runs per-ch) + */ + int (*configure)(struct processing_module *mod, int sample_rate_hz, + int filter_len_ms, int suppression, int num_channels); + + /** + * Process one 10 ms frame (interleaved S16 mic + ref → interleaved S16 out). + * All arrays are mono; caller provides per-channel slices. + * @mic: mic input S16 samples (frame_samples) + * @ref: echo reference S16 samples (frame_samples) + * @out: denoised output S16 samples (frame_samples) + * @ch: channel index (for multi-channel instances) + */ + int (*process_ch)(struct processing_module *mod, + const int16_t *mic, const int16_t *ref, int16_t *out, + int frame_samples, int ch); + + /* Reset adaptive filters (keep configuration). Called from reset(). */ + int (*reset)(struct processing_module *mod); + + /* Tear down all state from configure(). */ + int (*free)(struct processing_module *mod); +}; + +/** + * struct webrtc_aec_comp_data - webrtc_aec module private data. + * + * All buffers are S16 because AECm is a fixed-point Q15 algorithm. + * We do S32→S16 downshift on input and S16→S32 upshift on output when + * the pipeline format is S32. + */ +struct webrtc_aec_comp_data { + const struct webrtc_aec_backend *backend; + void *backend_data; + + /* Source routing (resolved in prepare). */ + int mic_src; /* index into sources[] for microphone */ + int ref_src; /* index into sources[] for echo reference */ + + /* Negotiated format. */ + int rate; /* pipeline sample rate (Hz) */ + int proc_rate; /* AECm processing rate (8000 or 16000 Hz) */ + int channels; + int mic_frame_bytes; /* bytes per pipeline frame on mic input */ + int ref_frame_bytes; /* bytes per pipeline frame on ref input */ + int out_frame_bytes; + bool is_s32; /* true for S32_LE pipeline */ + + /* Processing frame size at proc_rate. */ + int frame_samples; /* proc_rate * 10 / 1000 */ + + /* Accumulation state: we collect frames until a full 10 ms block. */ + int buffered_frames; + + /* Per-channel S16 scratch buffers. */ + int16_t mic_buf[WEBRTC_AEC_CHANNELS_MAX][WEBRTC_AEC_FRAME_SAMPLES_MAX]; + int16_t ref_buf[WEBRTC_AEC_CHANNELS_MAX][WEBRTC_AEC_FRAME_SAMPLES_MAX]; + int16_t out_buf[WEBRTC_AEC_CHANNELS_MAX][WEBRTC_AEC_FRAME_SAMPLES_MAX]; + + /* Ref stream liveness (IPC4: always active). */ + bool last_ref_ok; + + /* IPC4 tuning (echo path delay, etc.) — reserved for set_config. */ + struct sof_ipc4_aec_config config; + + bool configured; +}; + +/* Backend instance exported from the selected translation unit. */ +extern const struct webrtc_aec_backend webrtc_aec_backend; + +#endif /* __SOF_AUDIO_WEBRTC_AEC_H__ */ diff --git a/src/audio/webrtc_aec/webrtc_aec.toml b/src/audio/webrtc_aec/webrtc_aec.toml new file mode 100644 index 000000000000..fde0f102e0ce --- /dev/null +++ b/src/audio/webrtc_aec/webrtc_aec.toml @@ -0,0 +1,24 @@ +#ifndef LOAD_TYPE +#define LOAD_TYPE "0" +#endif + + REM # WebRTC AECm acoustic echo canceller module config. + REM # Two input pins: pin 0 = mic capture, pin 1 = echo reference. + [[module.entry]] + name = "WRTCAEC" + uuid = UUIDREG_STR_WEBRTC_AEC + affinity_mask = "0x1" + instance_count = "40" + domain_types = "0" + load_type = LOAD_TYPE + module_type = "9" + auto_start = "0" + sched_caps = [1, 0x00008000] + REM # pin = [dir, type, sample rate, size, container, channel-cfg] + REM # 2 input pins (mic + echo ref), 1 output pin (processed mic). + pin = [0, 0, 0xfeef, 0xf, 0xf, 0x45ff, 0, 0, 0xfeef, 0xf, 0xf, 0x45ff, 1, 0, 0xfeef, 0xf, 0xf, 0x45ff] + REM # mod_cfg [PAR_0 PAR_1 PAR_2 PAR_3 IS_BYTES CPS IBS OBS MOD_FLAGS CPC OBLS] + REM # AECm is fixed-point, modest CPU; 2×10ms buffers for mic+ref at 16kHz S32. + mod_cfg = [0, 0, 0, 0, 0, 2000000, 16384, 8192, 0, 2000, 0] + + index = __COUNTER__ diff --git a/src/audio/webrtc_ns/CMakeLists.txt b/src/audio/webrtc_ns/CMakeLists.txt new file mode 100644 index 000000000000..b1f24ed5d925 --- /dev/null +++ b/src/audio/webrtc_ns/CMakeLists.txt @@ -0,0 +1,13 @@ +# SPDX-License-Identifier: BSD-3-Clause + +if(CONFIG_COMP_WEBRTC_NS STREQUAL "m" AND DEFINED CONFIG_LLEXT) + add_subdirectory(llext ${PROJECT_BINARY_DIR}/webrtc_ns_llext) + add_dependencies(app webrtc_ns) +else() + add_local_sources(sof webrtc_ns.c) + if(CONFIG_COMP_WEBRTC_NS_STUB) + add_local_sources(sof webrtc_ns-stub.c) + else() + add_local_sources(sof webrtc_ns-webrtc.c) + endif() +endif() diff --git a/src/audio/webrtc_ns/Kconfig b/src/audio/webrtc_ns/Kconfig new file mode 100644 index 000000000000..062c6cd8b248 --- /dev/null +++ b/src/audio/webrtc_ns/Kconfig @@ -0,0 +1,55 @@ +# SPDX-License-Identifier: BSD-3-Clause + +config COMP_WEBRTC_NS + tristate "WebRTC Noise Suppression (classic spectral Wiener filter)" + help + Select to include the WebRTC Noise Suppression module. It wraps + the classic WebRTC NS algorithm — a frequency-domain spectral + subtraction / Wiener filter — as a single-input, single-output + PCM effect module. + + The NS module reduces stationary and slowly-varying background + noise (HVAC hum, fan noise, electrical hiss). It is independent + of the ffmpeg afftdn filter already available through ffmpeg_dec, + providing users a build-time choice between the two. + + The real backend uses the pure-C noise_suppression module + extracted from webrtc-audio-processing 0.3.x (no abseil, no C++, + BSD-3-Clause). Source is fetched via west (west update). + + Without the source, or for CI/testing, select COMP_WEBRTC_NS_STUB. + +if COMP_WEBRTC_NS + +config COMP_WEBRTC_NS_STUB + bool "WebRTC NS stub backend (no WebRTC source dependency)" + default y if COMP_STUBS + help + Build the webrtc_ns module against a dependency-free stub backend + that passes audio through unmodified. Useful for CI validation of + the SOF module glue, LLEXT packaging, and topology wiring without + needing the cross-built libwebrtc_ns archive. + +config WEBRTC_NS_LEVEL + int "Noise suppression level (0=mild, 1=moderate, 2=aggressive, 3=very_aggressive)" + range 0 3 + default 1 + help + Controls the aggressiveness of the noise suppressor: + 0 - Mild (minimal suppression, lowest distortion) + 1 - Moderate (recommended for most use cases) + 2 - Aggressive + 3 - Very aggressive (maximum suppression, most distortion) + +config WEBRTC_NS_SAMPLE_RATE_HZ + int "NS processing sample rate in Hz" + default 16000 + help + The internal processing sample rate. The WebRTC NS algorithm works + best at 16000 Hz. When the pipeline runs at 48000 Hz the module + can downsample to 16 kHz for NS processing and upsample back to + 48 kHz, saving significant CPU. Set to 48000 to process at full + rate (higher quality, higher CPU cost). + Valid values: 8000, 16000, 32000, 48000. + +endif # COMP_WEBRTC_NS diff --git a/src/audio/webrtc_ns/README.md b/src/audio/webrtc_ns/README.md new file mode 100644 index 000000000000..8da440d612ba --- /dev/null +++ b/src/audio/webrtc_ns/README.md @@ -0,0 +1,89 @@ +# WebRTC Noise Suppression Module (`webrtc_ns`) + +Wraps the classic fixed-point WebRTC Noise Suppression (NS) algorithm (spectral Wiener filter) from [webrtc-audio-processing](https://gitlab.freedesktop.org/gstreamer/webrtc-audio-processing) behind the SOF `module_interface`. + +--- + +## Features + +- **Wiener Filter Suppression**: Fixed-point spectral subtraction to isolate stationary noise. +- **Low Complexity**: Highly optimized for low-power embedded DSPs; requires no FPU. +- **Multiple Modes**: Supports four levels of suppression (Mild, Medium, Aggressive, Very Aggressive). +- **Per-Channel Processing**: Allocates one NS instance per audio channel, processing them independently. +- **8 kHz and 16 kHz Support**: Natively supports 8 kHz and 16 kHz sample rates (requires resampling upstream if running at higher rates). +- **Frame Size**: Operates on 10 ms audio frames (80 samples at 8 kHz, 160 samples at 16 kHz). + +--- + +## Architecture & Data Flow + +The following Mermaid diagram outlines the internal architecture of the `webrtc_ns` module: + +```mermaid +graph TD + %% Data Flow + InBuf[Input Audio Buffer] -->|S16/S32 Interleaved| Core[webrtc_ns.c Core] + Core -->|1. Demux & Normalise| ScaleIn[Format Conversion] + ScaleIn -->|10 ms Frame per Channel| Backend[webrtc_ns-webrtc.c Backend] + + Backend -->|WebRTC NS Core Filter| NSInstance[NsCore Instance per Channel] + NSInstance -->|Filtered Audio| Backend + Backend -->|Scale & Interleave| ScaleOut[Format Conversion] + + ScaleOut -->|S16/S32 Interleaved| Core + Core -->|Denoised PCM| OutBuf[Output Audio Buffer] + + %% Control Flow + IPC[IPC Control / Set Config] -.->|Suppression Level| Core + Core -.->|WebRtcNs_set_policy| NSInstance +``` + +### Components +- `webrtc_ns.c`: Core SOF wrapper logic, handling format parsing, period buffering, and interleaved-to-planar copying. +- `webrtc_ns-webrtc.c`: Integration backend interfacing directly with the WebRTC NS codebase. +- `webrtc_ns-stub.c`: Standard pass-through stub for testing. +- `webrtc_ns-shims.c`: Platform-specific macros and memory mapping layers. +- `webrtc_ns.cmake`: Downloads and extracts the `webrtc-audio-processing` 0.3.1 source and compiles the necessary C files. + +--- + +## Build Instructions + +### 1. Stub Mode (CI / Staging) +```ini +CONFIG_COMP_WEBRTC_NS=y # or =m for LLEXT +CONFIG_COMP_WEBRTC_NS_STUB=y +``` + +### 2. Real NS Integration +Pulls the classic WebRTC codebase via West and builds the fixed-point Wiener filter: +```ini +CONFIG_COMP_WEBRTC_NS=m +CONFIG_COMP_WEBRTC_NS_STUB=n +``` +*Note: Make sure to run `west update` to retrieve `modules/audio/webrtc-apm` before compiling.* + +--- + +## Kconfig Parameters + +| Option | Default | Range / Value | Description | +|---|---|---|---| +| `CONFIG_COMP_WEBRTC_NS` | n | y / m / n | Enable WebRTC Noise Suppressor | +| `CONFIG_COMP_WEBRTC_NS_STUB` | y | y / n | Use pass-through stub | +| `CONFIG_WEBRTC_NS_POLICY` | 2 | 0 - 3 | Severity (0: Mild, 3: Very Aggressive) | +| `CONFIG_WEBRTC_NS_CHANNELS_MAX` | 2 | 1 - 8 | Maximum channels supported | + +--- + +## Usage & Topology + +### Pipeline Integration +The module is integrated as a normal 1-in / 1-out effect widget. It should be run in a pipeline configured for either **8000 Hz** or **16000 Hz**. + +#### Example Topology Pipeline +``` +DAI Copier (SSP RX) ---> [ webrtc-ns ] ---> Host Copier (PCM) +``` + +For applications requiring higher rates (e.g. 48 kHz mic capture), place an ASRC component upstream to downsample to 16 kHz before running the `webrtc-ns` widget. diff --git a/src/audio/webrtc_ns/llext/CMakeLists.txt b/src/audio/webrtc_ns/llext/CMakeLists.txt new file mode 100644 index 000000000000..8810c457281c --- /dev/null +++ b/src/audio/webrtc_ns/llext/CMakeLists.txt @@ -0,0 +1,27 @@ +# Copyright (c) 2026 Intel Corporation. +# SPDX-License-Identifier: Apache-2.0 + +if(CONFIG_COMP_WEBRTC_NS_STUB) + # Dependency-free build: SOF glue + pass-through stub, no WebRTC library. + sof_llext_build("webrtc_ns" + SOURCES ../webrtc_ns.c + ../webrtc_ns-stub.c + LIB openmodules + ) +else() + # Real NS: cross-build webrtc-apm NS subset and link it. + include(${CMAKE_CURRENT_LIST_DIR}/../webrtc_ns.cmake) + + sof_llext_build("webrtc_ns" + SOURCES ../webrtc_ns.c + ../webrtc_ns-webrtc.c + ../webrtc_ns-shims.c + INCLUDES "${WEBRTC_NS_INSTALL_DIR}/include" + LIBS_PATH "${WEBRTC_NS_INSTALL_DIR}/lib" + LIBS webrtc_ns + LIB openmodules + ) + + add_dependencies(webrtc_ns_llext_lib webrtc_ns_ext) + add_dependencies(webrtc_ns webrtc_ns_ext) +endif() diff --git a/src/audio/webrtc_ns/llext/llext.toml.h b/src/audio/webrtc_ns/llext/llext.toml.h new file mode 100644 index 000000000000..1e75499f0efd --- /dev/null +++ b/src/audio/webrtc_ns/llext/llext.toml.h @@ -0,0 +1,6 @@ +#include +#define LOAD_TYPE "2" +#include "../webrtc_ns.toml" + +[module] +count = __COUNTER__ diff --git a/src/audio/webrtc_ns/webrtc_ns-shims.c b/src/audio/webrtc_ns/webrtc_ns-shims.c new file mode 100644 index 000000000000..16a6c028874e --- /dev/null +++ b/src/audio/webrtc_ns/webrtc_ns-shims.c @@ -0,0 +1,88 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// libc shims for the webrtc_ns real backend in LLEXT context. +// +// The WebRTC NS C library references malloc/free and a small set of math +// symbols. These are not exported by SOF core to LLEXT modules, so we +// provide thin wrappers backed by SOF's rballoc heap and sofm_ math. + +#include +#include +#include + +/* ============================ Memory ============================ */ + +void *malloc(size_t size) +{ + return rballoc(SOF_MEM_FLAG_USER, size); +} + +void free(void *ptr) +{ + rfree(ptr); +} + +void *calloc(size_t nmemb, size_t size) +{ + void *p = rballoc(SOF_MEM_FLAG_USER, nmemb * size); + + if (p) + memset(p, 0, nmemb * size); + return p; +} + +/* ============================ String helpers ===================== */ + +void *memset(void *s, int c, size_t n) +{ + uint8_t *p = s; + + while (n--) + *p++ = (uint8_t)c; + return s; +} + +void *memcpy(void *dest, const void *src, size_t n) +{ + const uint8_t *s = src; + uint8_t *d = dest; + + while (n--) + *d++ = *s++; + return dest; +} + +void *memmove(void *dest, const void *src, size_t n) +{ + uint8_t *d = dest; + const uint8_t *s = src; + + if (d < s || d >= s + n) { + while (n--) + *d++ = *s++; + } else { + d += n; + s += n; + while (n--) + *--d = *--s; + } + return dest; +} + +/* ============================ Math ============================== */ +/* + * The NS spectral processing uses log(), exp(), sqrt(), pow(), fabsf(). + * Route to sofm_ equivalents where available; fall back to soft-float + * versions provided by the Zephyr minimal libc for others. + */ +#include + +float sqrtf(float x) { return (float)sofm_sqrt_int32((int32_t)(x * 65536.0f)) / 256.0f; } + +/* log/exp/pow are used for spectral gain computation — leave as weak + * references satisfied by the toolchain's soft-float libm linked into + * the SOF binary. LLEXT resolves them at load time from the core symbol + * table. These shims are intentionally empty for now; add implementations + * if the real WebRTC NS backend needs them unsatisfied. */ diff --git a/src/audio/webrtc_ns/webrtc_ns-stub.c b/src/audio/webrtc_ns/webrtc_ns-stub.c new file mode 100644 index 000000000000..6bf58eaba9b9 --- /dev/null +++ b/src/audio/webrtc_ns/webrtc_ns-stub.c @@ -0,0 +1,64 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Dependency-free stub backend for the webrtc_ns module. +// +// Always passes audio through unmodified (identity transform). Used in CI +// and for topology/LLEXT packaging validation without the WebRTC NS library. + +#include +#include +#include +#include "webrtc_ns.h" + +LOG_MODULE_DECLARE(webrtc_ns, CONFIG_SOF_LOG_LEVEL); + +static int webrtc_ns_stub_init(struct processing_module *mod) +{ + comp_info(mod->dev, "webrtc_ns stub backend: no WebRTC NS linked"); + return 0; +} + +static int webrtc_ns_stub_configure(struct processing_module *mod, + int sample_rate_hz, int level, int num_channels) +{ + comp_info(mod->dev, "webrtc_ns stub: rate=%d level=%d ch=%d (ignored)", + sample_rate_hz, level, num_channels); + return 0; +} + +/* Stub: copy input to output (pass-through). */ +static int webrtc_ns_stub_process(struct processing_module *mod, + const float *const *in, float *const *out, + int frame_samples) +{ + struct webrtc_ns_comp_data *cd = module_get_private_data(mod); + int c; + + for (c = 0; c < cd->channels; c++) + memcpy(out[c], in[c], (size_t)frame_samples * sizeof(float)); + + return 0; +} + +static int webrtc_ns_stub_reset(struct processing_module *mod) +{ + (void)mod; + return 0; +} + +static int webrtc_ns_stub_free(struct processing_module *mod) +{ + (void)mod; + return 0; +} + +const struct webrtc_ns_backend webrtc_ns_backend = { + .name = "stub", + .init = webrtc_ns_stub_init, + .configure = webrtc_ns_stub_configure, + .process = webrtc_ns_stub_process, + .reset = webrtc_ns_stub_reset, + .free = webrtc_ns_stub_free, +}; diff --git a/src/audio/webrtc_ns/webrtc_ns-webrtc.c b/src/audio/webrtc_ns/webrtc_ns-webrtc.c new file mode 100644 index 000000000000..8bb27c1b7f0e --- /dev/null +++ b/src/audio/webrtc_ns/webrtc_ns-webrtc.c @@ -0,0 +1,174 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// WebRTC NS real backend for the webrtc_ns module. +// +// Wraps the classic WebRTC noise_suppression library extracted from +// webrtc-audio-processing 0.3.x. This version is pure C, has no C++ +// or abseil dependency, and uses a simple stateless API: +// +// WebRtcNs_Create() — allocate NsHandle +// WebRtcNs_Init() — configure sample rate +// WebRtcNs_set_policy() — aggressiveness 0..3 +// WebRtcNs_Analyze() — feed 10ms frame for noise model update +// WebRtcNs_Process() — run suppression +// WebRtcNs_Free() — release NsHandle +// +// The library works at a single fixed sample rate; if the pipeline runs at +// a different rate, the caller (webrtc_ns.c) is responsible for downsampling +// before and upsampling after (not yet implemented in this backend — for now, +// the backend uses CONFIG_WEBRTC_NS_SAMPLE_RATE_HZ directly and requires the +// pipeline to run at that rate, or to be configured identically). +// +// Requires cross-built libwebrtc_ns.a (west module modules/audio/webrtc-apm). +// See webrtc_ns.cmake for the cross-build recipe. + +#include +#include +#include +#include "webrtc_ns.h" + +#include /* from cross-built webrtc-audio-processing 0.3.x */ + +LOG_MODULE_DECLARE(webrtc_ns, CONFIG_SOF_LOG_LEVEL); + +/* Per-channel NS state handles. */ +struct webrtc_ns_real_data { + NsHandle *ns[WEBRTC_NS_CHANNELS_MAX]; + int num_channels; +}; + +static int webrtc_ns_real_init(struct processing_module *mod) +{ + struct webrtc_ns_comp_data *cd = module_get_private_data(mod); + struct webrtc_ns_real_data *rd; + + rd = mod_zalloc(mod, sizeof(*rd)); + if (!rd) + return -ENOMEM; + + cd->backend_data = rd; + comp_info(mod->dev, "webrtc_ns: WebRTC NS real backend initialised"); + return 0; +} + +static int webrtc_ns_real_configure(struct processing_module *mod, + int sample_rate_hz, int level, int num_channels) +{ + struct webrtc_ns_comp_data *cd = module_get_private_data(mod); + struct webrtc_ns_real_data *rd = cd->backend_data; + int c, ret; + + /* Free any previously allocated handles from a re-prepare. */ + for (c = 0; c < rd->num_channels; c++) { + if (rd->ns[c]) { + WebRtcNs_Free(rd->ns[c]); + rd->ns[c] = NULL; + } + } + + for (c = 0; c < num_channels; c++) { + rd->ns[c] = WebRtcNs_Create(); + if (!rd->ns[c]) { + comp_err(mod->dev, "webrtc_ns: WebRtcNs_Create() failed ch%d", c); + goto err; + } + + ret = WebRtcNs_Init(rd->ns[c], (uint32_t)sample_rate_hz); + if (ret) { + comp_err(mod->dev, "webrtc_ns: WebRtcNs_Init(ch%d, %d) failed %d", + c, sample_rate_hz, ret); + goto err; + } + + ret = WebRtcNs_set_policy(rd->ns[c], level); + if (ret) { + comp_err(mod->dev, "webrtc_ns: set_policy(%d) failed %d", level, ret); + goto err; + } + } + + rd->num_channels = num_channels; + comp_info(mod->dev, "webrtc_ns: WebRTC NS rate=%d level=%d ch=%d", + sample_rate_hz, level, num_channels); + return 0; + +err: + for (c = 0; c < num_channels; c++) { + if (rd->ns[c]) { + WebRtcNs_Free(rd->ns[c]); + rd->ns[c] = NULL; + } + } + return -ENOMEM; +} + +static int webrtc_ns_real_process(struct processing_module *mod, + const float *const *in, float *const *out, + int frame_samples) +{ + struct webrtc_ns_comp_data *cd = module_get_private_data(mod); + struct webrtc_ns_real_data *rd = cd->backend_data; + int c; + + for (c = 0; c < rd->num_channels; c++) { + /* Analyze updates the noise model (non-destructive). */ + WebRtcNs_Analyze(rd->ns[c], in[c]); + + /* Process suppresses noise: in → out. */ + const float *in_ptrs[1] = { in[c] }; + float *out_ptrs[1] = { out[c] }; + + WebRtcNs_Process(rd->ns[c], in_ptrs, 1, out_ptrs); + } + + return 0; +} + +static int webrtc_ns_real_reset(struct processing_module *mod) +{ + struct webrtc_ns_comp_data *cd = module_get_private_data(mod); + struct webrtc_ns_real_data *rd = cd->backend_data; + int c, ret; + + /* Re-initialise each channel to reset internal state. */ + for (c = 0; c < rd->num_channels; c++) { + if (!rd->ns[c]) + continue; + ret = WebRtcNs_Init(rd->ns[c], (uint32_t)cd->proc_rate); + if (ret) + comp_warn(mod->dev, "webrtc_ns: reset ch%d failed %d", c, ret); + } + return 0; +} + +static int webrtc_ns_real_free(struct processing_module *mod) +{ + struct webrtc_ns_comp_data *cd = module_get_private_data(mod); + struct webrtc_ns_real_data *rd = cd->backend_data; + int c; + + if (!rd) + return 0; + + for (c = 0; c < rd->num_channels; c++) { + if (rd->ns[c]) { + WebRtcNs_Free(rd->ns[c]); + rd->ns[c] = NULL; + } + } + + mod_free(mod, rd); + cd->backend_data = NULL; + return 0; +} + +const struct webrtc_ns_backend webrtc_ns_backend = { + .name = "webrtc-ns", + .init = webrtc_ns_real_init, + .configure = webrtc_ns_real_configure, + .process = webrtc_ns_real_process, + .reset = webrtc_ns_real_reset, + .free = webrtc_ns_real_free, +}; diff --git a/src/audio/webrtc_ns/webrtc_ns.c b/src/audio/webrtc_ns/webrtc_ns.c new file mode 100644 index 000000000000..13bca7606be7 --- /dev/null +++ b/src/audio/webrtc_ns/webrtc_ns.c @@ -0,0 +1,373 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// WebRTC Noise Suppression module — SOF module_interface core. +// +// Single-input, single-output PCM effect (mic → denoised mic). The module +// converts interleaved S16/S32 → planar float, accumulates a full 10 ms frame, +// runs the backend NS for each channel, and converts float → S16/S32 back to +// the sink. Latency is at most 10 ms (one NS frame period). + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "webrtc_ns.h" + +/* UUID registered in uuid-registry.txt. */ +SOF_DEFINE_REG_UUID(webrtc_ns); + +LOG_MODULE_REGISTER(webrtc_ns, CONFIG_SOF_LOG_LEVEL); + +/* S32 normalisation scale factor (2^31). */ +#define WEBRTC_NS_S32_SCALE 2147483648.0f + +/* S16 normalisation scale factor (2^15). */ +#define WEBRTC_NS_S16_SCALE 32768.0f + +/** + * webrtc_ns_init() - Allocate private data and initialise the backend. + */ +__cold static int webrtc_ns_init(struct processing_module *mod) +{ + struct module_data *md = &mod->priv; + struct comp_dev *dev = mod->dev; + struct webrtc_ns_comp_data *cd; + int ret, i; + + assert_can_be_cold(); + comp_info(dev, "webrtc_ns: init"); + + cd = mod_zalloc(mod, sizeof(*cd)); + if (!cd) + return -ENOMEM; + + md->private = cd; + cd->backend = &webrtc_ns_backend; + + /* Wire up permanent planar buffer pointer arrays. */ + for (i = 0; i < WEBRTC_NS_CHANNELS_MAX; i++) { + cd->in_ptrs[i] = cd->in_buf[i]; + cd->out_ptrs[i] = cd->out_buf[i]; + } + + comp_info(dev, "webrtc_ns: backend '%s'", cd->backend->name); + + if (cd->backend->init) { + ret = cd->backend->init(mod); + if (ret) { + comp_err(dev, "webrtc_ns: backend init failed %d", ret); + mod_free(mod, cd); + return ret; + } + } + + return 0; +} + +/** + * webrtc_ns_prepare() - Configure NS for the negotiated pipeline format. + */ +__cold static int webrtc_ns_prepare(struct processing_module *mod, + struct sof_source **sources, int num_of_sources, + struct sof_sink **sinks, int num_of_sinks) +{ + struct webrtc_ns_comp_data *cd = module_get_private_data(mod); + struct comp_dev *dev = mod->dev; + int rate, fmt, ret; + + assert_can_be_cold(); + + if (num_of_sources != 1 || num_of_sinks != 1) { + comp_err(dev, "webrtc_ns: need exactly 1 source and 1 sink"); + return -EINVAL; + } + + rate = source_get_rate(sources[0]); + fmt = source_get_frm_fmt(sources[0]); + cd->channels = source_get_channels(sources[0]); + + if (cd->channels > WEBRTC_NS_CHANNELS_MAX) { + comp_err(dev, "webrtc_ns: too many channels %d (max %d)", + cd->channels, WEBRTC_NS_CHANNELS_MAX); + return -EINVAL; + } + + /* NS only accepts 8/16/32/48 kHz. */ + if (rate != 8000 && rate != 16000 && rate != 32000 && rate != 48000) { + comp_err(dev, "webrtc_ns: unsupported rate %d", rate); + return -EINVAL; + } + + switch (fmt) { + case SOF_IPC_FRAME_S16_LE: + case SOF_IPC_FRAME_S32_LE: + break; + default: + comp_err(dev, "webrtc_ns: unsupported frame format %d", fmt); + return -EINVAL; + } + + cd->in_rate = rate; + cd->proc_rate = CONFIG_WEBRTC_NS_SAMPLE_RATE_HZ; + if (cd->proc_rate != rate) { + /* Proc rate must be <= in_rate for simple downsampling. */ + if (cd->proc_rate > rate || rate % cd->proc_rate != 0) { + comp_err(dev, "webrtc_ns: proc_rate %d incompatible with in_rate %d", + cd->proc_rate, rate); + return -EINVAL; + } + } + + cd->in_frame_bytes = source_get_frame_bytes(sources[0]); + cd->proc_frame_samples = (cd->proc_rate * 10) / 1000; /* 10 ms */ + cd->in_frame_samples = (cd->in_rate * 10) / 1000; + + if (cd->proc_frame_samples > WEBRTC_NS_FRAME_SAMPLES_MAX) { + comp_err(dev, "webrtc_ns: proc_frame_samples %d exceeds max %d", + cd->proc_frame_samples, WEBRTC_NS_FRAME_SAMPLES_MAX); + return -EINVAL; + } + + comp_info(dev, "webrtc_ns: in_rate=%d proc_rate=%d ch=%d proc_frame=%d", + cd->in_rate, cd->proc_rate, cd->channels, cd->proc_frame_samples); + + if (cd->backend->configure) { + ret = cd->backend->configure(mod, cd->proc_rate, + CONFIG_WEBRTC_NS_LEVEL, + cd->channels); + if (ret) { + comp_err(dev, "webrtc_ns: backend configure failed %d", ret); + return ret; + } + } + + cd->buffered_frames = 0; + cd->configured = true; + return 0; +} + +/** + * s32_to_float() - Convert interleaved S32 samples to planar float. + * @cd: Module private data (channels, proc_frame_samples). + * @src: Source pointer (interleaved S32, raw[ch]). + * @n_frames: Number of frames (each frame has cd->channels samples). + * @frame0: Starting offset in the planar float buffer (for accumulation). + */ +static void s32_to_float(struct webrtc_ns_comp_data *cd, + const int32_t *src, int n_frames, int frame0) +{ + int i, c; + + for (i = 0; i < n_frames; i++) + for (c = 0; c < cd->channels; c++) + cd->in_buf[c][frame0 + i] = + (float)src[i * cd->channels + c] / WEBRTC_NS_S32_SCALE; +} + +/** + * s16_to_float() - Convert interleaved S16 samples to planar float. + */ +static void s16_to_float(struct webrtc_ns_comp_data *cd, + const int16_t *src, int n_frames, int frame0) +{ + int i, c; + + for (i = 0; i < n_frames; i++) + for (c = 0; c < cd->channels; c++) + cd->in_buf[c][frame0 + i] = + (float)src[i * cd->channels + c] / WEBRTC_NS_S16_SCALE; +} + +/** + * float_to_s32() - Write planar float NS output back as interleaved S32. + */ +static void float_to_s32(struct webrtc_ns_comp_data *cd, + int32_t *dst, int n_frames) +{ + int i, c; + + for (i = 0; i < n_frames; i++) { + for (c = 0; c < cd->channels; c++) { + float f = cd->out_buf[c][i] * WEBRTC_NS_S32_SCALE; + + f = f > 2147483647.0f ? 2147483647.0f : + f < -2147483648.0f ? -2147483648.0f : f; + dst[i * cd->channels + c] = (int32_t)f; + } + } +} + +/** + * float_to_s16() - Write planar float NS output back as interleaved S16. + */ +static void float_to_s16(struct webrtc_ns_comp_data *cd, + int16_t *dst, int n_frames) +{ + int i, c; + + for (i = 0; i < n_frames; i++) { + for (c = 0; c < cd->channels; c++) { + float f = cd->out_buf[c][i] * WEBRTC_NS_S16_SCALE; + + f = f > 32767.0f ? 32767.0f : + f < -32768.0f ? -32768.0f : f; + dst[i * cd->channels + c] = (int16_t)f; + } + } +} + +/** + * webrtc_ns_process() - Run NS on accumulated 10 ms frames. + * + * The pipeline period may be shorter than 10 ms, so we accumulate interleaved + * PCM → planar float until we have a full NS frame, then process and drain. + */ +static int webrtc_ns_process(struct processing_module *mod, + struct sof_source **sources, int num_of_sources, + struct sof_sink **sinks, int num_of_sinks) +{ + struct webrtc_ns_comp_data *cd = module_get_private_data(mod); + struct sof_source *src = sources[0]; + struct sof_sink *snk = sinks[0]; + enum sof_ipc_frame fmt = source_get_frm_fmt(src); + int avail = (int)source_get_data_frames_available(src); + int free = (int)sink_get_free_frames(snk); + int n = MIN(avail, free); + size_t nbytes, buf_size; + void const *rd_ptr, *buf_start; + void *wr_ptr, *wr_buf_start; + int ret; + + if (n <= 0) + return 0; + + nbytes = (size_t)n * source_get_frame_bytes(src); + + ret = source_get_data(src, nbytes, &rd_ptr, &buf_start, &buf_size); + if (ret) + return ret; + + ret = sink_get_buffer(snk, nbytes, &wr_ptr, &wr_buf_start, &buf_size); + if (ret) { + source_release_data(src, 0); + return ret; + } + + /* Convert input to planar float, accumulating into in_buf[]. */ + if (fmt == SOF_IPC_FRAME_S32_LE) + s32_to_float(cd, rd_ptr, n, cd->buffered_frames); + else + s16_to_float(cd, rd_ptr, n, cd->buffered_frames); + + source_release_data(src, nbytes); + + cd->buffered_frames += n; + + /* + * Process complete NS frames. Each call consumes proc_frame_samples. + * Any partial frame remains in in_buf[] for the next cycle. + */ + while (cd->buffered_frames >= cd->proc_frame_samples) { + int fs = cd->proc_frame_samples; + + ret = cd->backend->process(mod, + (const float *const *)cd->in_ptrs, + cd->out_ptrs, fs); + if (ret) { + comp_err(mod->dev, "webrtc_ns: backend process error %d", ret); + /* On error: fall back to pass-through for this frame. */ + int c; + + for (c = 0; c < cd->channels; c++) + memcpy(cd->out_buf[c], cd->in_buf[c], + (size_t)fs * sizeof(float)); + } + + /* Write processed frame to sink. */ + if (fmt == SOF_IPC_FRAME_S32_LE) + float_to_s32(cd, wr_ptr, fs); + else + float_to_s16(cd, wr_ptr, fs); + + /* Slide remaining samples to front of in_buf[]. */ + cd->buffered_frames -= fs; + if (cd->buffered_frames > 0) { + int c; + + for (c = 0; c < cd->channels; c++) + memmove(cd->in_buf[c], cd->in_buf[c] + fs, + (size_t)cd->buffered_frames * sizeof(float)); + } + } + + sink_commit_buffer(snk, nbytes); + return 0; +} + +/** + * webrtc_ns_reset() - Flush accumulator and reset backend state. + */ +static int webrtc_ns_reset(struct processing_module *mod) +{ + struct webrtc_ns_comp_data *cd = module_get_private_data(mod); + + comp_dbg(mod->dev, "webrtc_ns: reset"); + cd->buffered_frames = 0; + + if (cd->backend->reset) + return cd->backend->reset(mod); + + return 0; +} + +/** + * webrtc_ns_free() - Free all resources. + */ +__cold static int webrtc_ns_free(struct processing_module *mod) +{ + struct webrtc_ns_comp_data *cd = module_get_private_data(mod); + + assert_can_be_cold(); + comp_dbg(mod->dev, "webrtc_ns: free"); + + if (cd->backend->free) + cd->backend->free(mod); + + mod_free(mod, cd); + return 0; +} + +static const struct module_interface webrtc_ns_interface = { + .init = webrtc_ns_init, + .prepare = webrtc_ns_prepare, + .process = webrtc_ns_process, + .reset = webrtc_ns_reset, + .free = webrtc_ns_free, +}; + +#if CONFIG_COMP_WEBRTC_NS_MODULE + +#include +#include +#include + +static const struct sof_man_module_manifest mod_manifest __section(".module") __used = + SOF_LLEXT_MODULE_MANIFEST("WRTCNS", &webrtc_ns_interface, 1, + SOF_REG_UUID(webrtc_ns), 40); + +SOF_LLEXT_BUILDINFO; + +#else + +DECLARE_TR_CTX(webrtc_ns_tr, SOF_UUID(webrtc_ns_uuid), LOG_LEVEL_INFO); +DECLARE_MODULE_ADAPTER(webrtc_ns_interface, webrtc_ns_uuid, webrtc_ns_tr); +SOF_MODULE_INIT(webrtc_ns, sys_comp_module_webrtc_ns_interface_init); + +#endif diff --git a/src/audio/webrtc_ns/webrtc_ns.cmake b/src/audio/webrtc_ns/webrtc_ns.cmake new file mode 100644 index 000000000000..7ca121417c5f --- /dev/null +++ b/src/audio/webrtc_ns/webrtc_ns.cmake @@ -0,0 +1,80 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Cross-build the WebRTC Noise Suppression (classic 0.3.x) C sources into +# a static library for the webrtc_ns LLEXT module. +# +# webrtc-audio-processing 0.3.x uses a pure-C noise suppressor with no C++ +# and no abseil dependency. We compile the relevant source files directly +# using the Xtensa cross-toolchain (same approach as libfvad in webrtc_vad). +# +# Source is pulled via west from the webrtc-apm west module (which contains +# the webrtc-audio-processing 0.3.1 tarball extraction). See west.yml. +# +# Produces: +# ${WEBRTC_NS_INSTALL_DIR}/lib/libwebrtc_ns.a +# ${WEBRTC_NS_INSTALL_DIR}/include/noise_suppression.h + +if(NOT DEFINED SOF_WEBRTC_APM_SRC_DIR) + set(SOF_WEBRTC_APM_SRC_DIR "${sof_top_dir}/../modules/audio/webrtc-apm" + CACHE PATH "webrtc-audio-processing 0.3.x source directory (west-pinned)") +endif() +cmake_path(NORMAL_PATH SOF_WEBRTC_APM_SRC_DIR) +if(NOT EXISTS "${SOF_WEBRTC_APM_SRC_DIR}/webrtc/modules/audio_processing/ns/ns_core.c") + message(FATAL_ERROR + "webrtc_ns: WebRTC APM NS source not found at '${SOF_WEBRTC_APM_SRC_DIR}'.\n" + "Run 'west update' (webrtc-apm is pinned in west.yml) or pass\n" + "-DSOF_WEBRTC_APM_SRC_DIR=.") +endif() + +# Derive cross-toolchain prefix from the Zephyr compiler path. +get_filename_component(_tc_dir "${CMAKE_C_COMPILER}" DIRECTORY) +get_filename_component(_tc_name "${CMAKE_C_COMPILER}" NAME) +string(REGEX REPLACE "gcc$" "" _tc_prefix_name "${_tc_name}") +set(_ns_cross_prefix "${_tc_dir}/${_tc_prefix_name}") + +set(WEBRTC_NS_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/webrtc-ns-install" + CACHE INTERNAL "webrtc_ns: NS library install prefix") + +set(_ns_src "${SOF_WEBRTC_APM_SRC_DIR}/webrtc") + +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/webrtc-ns-build.sh" +"#!/bin/sh +set -e +SRC=${_ns_src} +INST=${WEBRTC_NS_INSTALL_DIR} +OBJ=${CMAKE_CURRENT_BINARY_DIR}/webrtc-ns-obj + +mkdir -p \"$OBJ\" \"$INST/lib\" \"$INST/include\" + +CFLAGS=\"-O2 -fPIC -I$SRC -I$SRC/common_audio/signal_processing/include\" + +SRCS=\" + $SRC/modules/audio_processing/ns/ns_core.c + $SRC/modules/audio_processing/ns/ns_fft.c + $SRC/common_audio/signal_processing/complex_fft.c + $SRC/common_audio/signal_processing/division_operations.c + $SRC/common_audio/signal_processing/energy.c + $SRC/common_audio/signal_processing/get_scaling_square.c + $SRC/common_audio/signal_processing/real_fft.c + $SRC/common_audio/signal_processing/spl_inl.c +\" + +for f in \$SRCS; do + bn=\$(basename \"\$f\" .c) + ${CMAKE_C_COMPILER} \$CFLAGS -c \"\$f\" -o \"$OBJ/\${bn}.o\" +done + +${_ns_cross_prefix}ar rcs \"$INST/lib/libwebrtc_ns.a\" \"$OBJ\"/*.o +cp $SRC/modules/audio_processing/ns/include/noise_suppression.h \"$INST/include/\" +") + +add_custom_command( + OUTPUT "${WEBRTC_NS_INSTALL_DIR}/lib/libwebrtc_ns.a" + "${WEBRTC_NS_INSTALL_DIR}/include/noise_suppression.h" + COMMAND sh "${CMAKE_CURRENT_BINARY_DIR}/webrtc-ns-build.sh" + VERBATIM) + +add_custom_target(webrtc_ns_ext + DEPENDS "${WEBRTC_NS_INSTALL_DIR}/lib/libwebrtc_ns.a") + +message(STATUS "webrtc_ns: cross-building WebRTC NS from ${SOF_WEBRTC_APM_SRC_DIR}") diff --git a/src/audio/webrtc_ns/webrtc_ns.h b/src/audio/webrtc_ns/webrtc_ns.h new file mode 100644 index 000000000000..2e215418166f --- /dev/null +++ b/src/audio/webrtc_ns/webrtc_ns.h @@ -0,0 +1,104 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright(c) 2026 Intel Corporation. + * + * WebRTC Noise Suppression module for SOF. + * + * Wraps the classic WebRTC NS algorithm (spectral subtraction / Wiener filter + * from webrtc-audio-processing 0.3.x) behind the SOF module_interface as a + * single-input, single-output PCM effect. + * + * The actual suppression is delegated to a pluggable backend so the SOF glue + * can be validated with a stub (webrtc_ns-stub.c) before the real WebRTC NS + * backend (webrtc_ns-webrtc.c) is linked in. + */ +#ifndef __SOF_AUDIO_WEBRTC_NS_H__ +#define __SOF_AUDIO_WEBRTC_NS_H__ + +#include +#include +#include + +/* + * WebRTC NS requires exactly 10 ms frames at the processing sample rate. + * Maximum: 10 ms at 48 kHz = 480 samples/channel. + */ +#define WEBRTC_NS_FRAME_SAMPLES_MAX 480 + +/* Maximum channel count supported. */ +#define WEBRTC_NS_CHANNELS_MAX 8 + +/** + * struct webrtc_ns_backend - NS backend operations. + * + * A backend owns the WebRTC NS state and the per-frame suppression call. + * All ops return 0 on success or a negative errno. + */ +struct webrtc_ns_backend { + const char *name; + + /* One-time init, called from module init(). */ + int (*init)(struct processing_module *mod); + + /** + * Configure the NS for rate/level. Called from prepare(). + * @sample_rate_hz: processing rate (8000/16000/32000/48000) + * @level: NS aggressiveness 0..3 + * @num_channels: number of channels to suppress independently + */ + int (*configure)(struct processing_module *mod, + int sample_rate_hz, int level, int num_channels); + + /** + * Process one 10 ms frame of float PCM (per channel, planar). + * @in: array of num_channels pointers, each of frame_samples floats + * @out: array of num_channels pointers, each of frame_samples floats + * @frame_samples: samples per channel (rate * 10 / 1000) + */ + int (*process)(struct processing_module *mod, + const float *const *in, float *const *out, + int frame_samples); + + /* Reset NS state (keep configuration). Called from reset(). */ + int (*reset)(struct processing_module *mod); + + /* Tear down all state from init()/configure(). */ + int (*free)(struct processing_module *mod); +}; + +/** + * struct webrtc_ns_comp_data - webrtc_ns module private data. + * @backend: Selected backend operations. + * @backend_data: Backend-private state handle. + * @in_rate: Input sample rate from pipeline (Hz). + * @proc_rate: Processing rate used by NS (may be < in_rate). + * @channels: Channel count. + * @in_frame_bytes: Bytes per frame at pipeline rate. + * @proc_frame_samples: Samples per 10 ms at proc_rate (per channel). + * @in_frame_samples: Samples per 10 ms at in_rate (per channel). + * @buffered_frames: Samples currently in accumulator. + * @in_buf: Float planar input accumulator [ch][frame_samples]. + * @out_buf: Float planar output buffer [ch][frame_samples]. + * @configured: True once backend has been opened. + */ +struct webrtc_ns_comp_data { + const struct webrtc_ns_backend *backend; + void *backend_data; + int in_rate; + int proc_rate; + int channels; + int in_frame_bytes; + int proc_frame_samples; + int in_frame_samples; + int buffered_frames; + float in_buf[WEBRTC_NS_CHANNELS_MAX][WEBRTC_NS_FRAME_SAMPLES_MAX]; + float out_buf[WEBRTC_NS_CHANNELS_MAX][WEBRTC_NS_FRAME_SAMPLES_MAX]; + const float *in_ptrs[WEBRTC_NS_CHANNELS_MAX]; + float *out_ptrs[WEBRTC_NS_CHANNELS_MAX]; + bool configured; +}; + +/* Backend instance provided by the selected translation unit. */ +extern const struct webrtc_ns_backend webrtc_ns_backend; + +#endif /* __SOF_AUDIO_WEBRTC_NS_H__ */ diff --git a/src/audio/webrtc_ns/webrtc_ns.toml b/src/audio/webrtc_ns/webrtc_ns.toml new file mode 100644 index 000000000000..293fd9dcd66f --- /dev/null +++ b/src/audio/webrtc_ns/webrtc_ns.toml @@ -0,0 +1,24 @@ +#ifndef LOAD_TYPE +#define LOAD_TYPE "0" +#endif + + REM # WebRTC Noise Suppression module config + [[module.entry]] + REM # Module name limited to 8 chars; must match SOF_LLEXT_MODULE_MANIFEST() in webrtc_ns.c. + name = "WRTCNS" + uuid = UUIDREG_STR_WEBRTC_NS + affinity_mask = "0x1" + instance_count = "40" + domain_types = "0" + load_type = LOAD_TYPE + module_type = "9" + auto_start = "0" + sched_caps = [1, 0x00008000] + REM # pin = [dir, type, sample rate, size, container, channel-cfg] + REM # Single input pin (PCM), single output pin (denoised PCM). Pass-through topology. + pin = [0, 0, 0xfeef, 0xf, 0xf, 0x45ff, 1, 0, 0xfeef, 0xf, 0xf, 0x45ff] + REM # mod_cfg [PAR_0 PAR_1 PAR_2 PAR_3 IS_BYTES CPS IBS OBS MOD_FLAGS CPC OBLS] + REM # NS adds ~10ms buffering latency; IBS/OBS sized for 2×10ms at 48kHz stereo S32. + mod_cfg = [0, 0, 0, 0, 0, 3000000, 16384, 16384, 0, 3000, 0] + + index = __COUNTER__ diff --git a/src/audio/webrtc_ns2/CMakeLists.txt b/src/audio/webrtc_ns2/CMakeLists.txt new file mode 100644 index 000000000000..fbca7ab5205d --- /dev/null +++ b/src/audio/webrtc_ns2/CMakeLists.txt @@ -0,0 +1,13 @@ +# SPDX-License-Identifier: BSD-3-Clause + +if(CONFIG_COMP_WEBRTC_NS2 STREQUAL "m" AND DEFINED CONFIG_LLEXT) + add_subdirectory(llext ${PROJECT_BINARY_DIR}/webrtc_ns2_llext) + add_dependencies(app webrtc_ns2) +else() + add_local_sources(sof webrtc_ns2.c) + if(CONFIG_COMP_WEBRTC_NS2_STUB) + add_local_sources(sof webrtc_ns2-stub.c) + else() + add_local_sources(sof webrtc_ns2-rnn.c) + endif() +endif() diff --git a/src/audio/webrtc_ns2/Kconfig b/src/audio/webrtc_ns2/Kconfig new file mode 100644 index 000000000000..09af8b84d9d2 --- /dev/null +++ b/src/audio/webrtc_ns2/Kconfig @@ -0,0 +1,69 @@ +# SPDX-License-Identifier: BSD-3-Clause + +config COMP_WEBRTC_NS2 + tristate "RNNoise deep-learning noise suppressor (floating-point)" + help + Select to include the RNNoise module — a recurrent neural network + noise suppressor originally developed by Jean-Marc Valin (Mozilla / + Opus). RNNoise operates at 48 kHz on 10 ms (480-sample) mono frames + and achieves significantly better suppression of non-stationary noise + (music, babble, keyboard) than the classic spectral Wiener filter + (COMP_WEBRTC_NS). + + The module is single-input, single-output and internally emits a + per-frame VAD probability (0.0–1.0) via NOTIFIER_ID_VAD, making it + a combined denoiser and activity detector. + + Requirements: + - Floating-point unit (FPU=y on ACE30/PTL; soft-float also works + but at significant CPU cost) + - ~30 KB of state per channel (DenoiseState + RNN weights) + - Pipeline sample rate exactly 48000 Hz + - Pipeline period must be a multiple of 480 samples (10 ms) + + The real backend builds RNNoise from source (west module rnnoise, + pinned at commit 70f1d256). For CI/stub builds select + COMP_WEBRTC_NS2_STUB. + + License: BSD-3-Clause (xiph/rnnoise). + +if COMP_WEBRTC_NS2 + +config COMP_WEBRTC_NS2_STUB + bool "RNNoise stub backend (no RNNoise source dependency)" + default y if COMP_STUBS + help + Build webrtc_ns2 against a dependency-free pass-through stub instead + of the real RNNoise library. The stub passes audio through unmodified + and always reports VAD probability = 1.0. Used for CI and LLEXT + packaging validation without the RNNoise source. + +config WEBRTC_NS2_CHANNELS_MAX + int "Maximum channel count (each channel gets its own RNNoise instance)" + range 1 8 + default 2 + help + RNNoise is strictly mono; the module runs one DenoiseState per + channel. Increasing this raises SRAM usage by ~30 KB per extra + channel. Set to 1 for pure mono capture pipelines. + +config WEBRTC_NS2_VAD_NOTIFY + bool "Emit NOTIFIER_ID_VAD event from RNNoise VAD probability" + default y + help + RNNoise returns a per-frame speech probability in [0.0, 1.0]. When + this option is enabled the module broadcasts a NOTIFIER_ID_VAD event + (speech=1 / silence=0) using a configurable threshold, providing a + combined denoiser + VAD without running a separate webrtc_vad module. + +config WEBRTC_NS2_VAD_THRESHOLD_PCT + int "VAD threshold as percentage of max probability (0-100)" + range 0 100 + default 50 + depends on WEBRTC_NS2_VAD_NOTIFY + help + Speech is declared when rnnoise_process_frame() returns a probability + >= this value / 100.0. 50 is a good default; lower values increase + sensitivity (more false positives), higher values reduce it. + +endif # COMP_WEBRTC_NS2 diff --git a/src/audio/webrtc_ns2/README.md b/src/audio/webrtc_ns2/README.md new file mode 100644 index 000000000000..b5276e3db5a0 --- /dev/null +++ b/src/audio/webrtc_ns2/README.md @@ -0,0 +1,97 @@ +# WebRTC Noise Suppression 2 Module (`webrtc_ns2` / RNNoise) + +Wraps Xiph's [RNNoise](https://github.com/xiph/rnnoise) — a recurrent neural network (RNN/GRU) based noise suppression algorithm — behind the SOF `module_interface`. + +--- + +## Features + +- **Deep Learning Suppression**: Uses a hybrid DSP + deep recurrent neural network structure (GRU) for suppressing non-stationary noise (such as keyboard clicks, speech babble, or music). +- **VAD Output**: Calculates speech probability per frame and broadcasts binary voice activity events via `NOTIFIER_ID_VAD`. +- **Locked Rate (48 kHz)**: Runs exclusively at **48000 Hz** (requires an ASRC upstream if the DAI runs at another rate). +- **10 ms Process Window**: Accumulates pipeline periods to process complete 480-sample mono frames. +- **Float Scaled**: normalizes standard SOF float pipelines to the internal full-scale float representation ($\pm 32768.0$) used during training. +- **Low Memory / Fast Math**: Employs a 201-entry lookup table for sigmoid and tanh functions; eliminates hot-path `expf` calls. + +--- + +## Architecture & Data Flow + +The following Mermaid diagram outlines the data normalization, period accumulator, and deep-learning inference flow inside the module: + +```mermaid +graph TD + %% Input Path + InBuf[Input Audio Buffer] -->|S16 or S32 PCM| Core[webrtc_ns2.c Core] + + %% Accumulation & Scaling + Core -->|Accumulate 480 Samples| Acc{Frame Ready?} + Acc -->|No| Core + Acc -->|Yes: Normalize| FloatScale[Scale to ±32768.0] + + %% RNNoise Processing + FloatScale -->|Full-Scale Float Frame| Backend[webrtc_ns2-rnn.c Backend] + Backend -->|rnnoise_process_frame| RNNEngine[RNNoise GRU inference] + + %% Outputs + RNNEngine -->|Denoised Frame| Backend + RNNEngine -->|Speech Probability 0.0 - 1.0| VAD{Prob >= Threshold?} + + %% Scale down and copy out + Backend -->|Scale back to ±1.0| Core + Core -->|Interleaved output| OutBuf[Output Audio Buffer] + + VAD -->|Yes: 1 / No: 0| Notifier[NOTIFIER_ID_VAD Event] +``` + +### Components +- `webrtc_ns2.c`: Handles format conversions, period packing, and the VAD event notifier. +- `webrtc_ns2-rnn.c`: Real RNNoise library integration backend. +- `webrtc_ns2-stub.c`: Pass-through stub that always outputs `1.0` VAD probability. +- `webrtc_ns2.cmake`: Controls downloading the `rnnoise` Git repository and building its files. + +--- + +## Build Instructions + +### 1. Stub Mode (CI / Staging) +```ini +CONFIG_COMP_WEBRTC_NS2=y # or =m for LLEXT +CONFIG_COMP_WEBRTC_NS2_STUB=y +``` + +### 2. Real RNNoise Integration +Cross-compiles the neural networks: +```ini +CONFIG_COMP_WEBRTC_NS2=m +CONFIG_COMP_WEBRTC_NS2_STUB=n +``` +Requires `west update` to retrieve `modules/audio/rnnoise` (SHA `70f1d256`). + +*Important: The target DSP board must support floating-point compilation. Although a hardware FPU is not strictly mandatory, software-float emulation will impose a severe CPU bottleneck.* + +--- + +## Kconfig Parameters + +| Option | Default | Range / Value | Description | +|---|---|---|---| +| `CONFIG_COMP_WEBRTC_NS2` | n | y / m / n | Enable RNNoise module | +| `CONFIG_COMP_WEBRTC_NS2_STUB` | y | y / n | Use pass-through stub | +| `CONFIG_WEBRTC_NS2_CHANNELS_MAX` | 2 | 1 - 8 | Maximum channels supported | +| `CONFIG_WEBRTC_NS2_VAD_NOTIFY` | y | y / n | Emit NOTIFIER_ID_VAD events | +| `CONFIG_WEBRTC_NS2_VAD_THRESHOLD_PCT` | 50 | 0 - 100 | VAD speech threshold percentage | + +--- + +## Usage & Topology + +### Pipeline Integration +The module is integrated as a normal 1-in / 1-out effect widget. **The sample rate must be exactly 48000 Hz**. + +#### Example Topology Route +``` +DAI Copier (SSP RX @ 48kHz) ---> [ webrtc-ns2 ] ---> Host Copier (PCM @ 48kHz) +``` + +If the capture microphone or stream runs at 16 kHz, you must insert an ASRC widget upstream of `webrtc-ns2` to upsample the stream to 48 kHz before processing. diff --git a/src/audio/webrtc_ns2/llext/CMakeLists.txt b/src/audio/webrtc_ns2/llext/CMakeLists.txt new file mode 100644 index 000000000000..51393a0c41bf --- /dev/null +++ b/src/audio/webrtc_ns2/llext/CMakeLists.txt @@ -0,0 +1,24 @@ +# Copyright (c) 2026 Intel Corporation. +# SPDX-License-Identifier: Apache-2.0 + +if(CONFIG_COMP_WEBRTC_NS2_STUB) + sof_llext_build("webrtc_ns2" + SOURCES ../webrtc_ns2.c + ../webrtc_ns2-stub.c + LIB openmodules + ) +else() + include(${CMAKE_CURRENT_LIST_DIR}/../webrtc_ns2.cmake) + + sof_llext_build("webrtc_ns2" + SOURCES ../webrtc_ns2.c + ../webrtc_ns2-rnn.c + INCLUDES "${WEBRTC_NS2_INSTALL_DIR}/include" + LIBS_PATH "${WEBRTC_NS2_INSTALL_DIR}/lib" + LIBS rnnoise + LIB openmodules + ) + + add_dependencies(webrtc_ns2_llext_lib rnnoise_ext) + add_dependencies(webrtc_ns2 rnnoise_ext) +endif() diff --git a/src/audio/webrtc_ns2/llext/llext.toml.h b/src/audio/webrtc_ns2/llext/llext.toml.h new file mode 100644 index 000000000000..ac60a66b75c0 --- /dev/null +++ b/src/audio/webrtc_ns2/llext/llext.toml.h @@ -0,0 +1,6 @@ +#include +#define LOAD_TYPE "2" +#include "../webrtc_ns2.toml" + +[module] +count = __COUNTER__ diff --git a/src/audio/webrtc_ns2/webrtc_ns2-rnn.c b/src/audio/webrtc_ns2/webrtc_ns2-rnn.c new file mode 100644 index 000000000000..c0914df0e4ac --- /dev/null +++ b/src/audio/webrtc_ns2/webrtc_ns2-rnn.c @@ -0,0 +1,173 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Real RNNoise backend for webrtc_ns2. +// +// RNNoise public API (rnnoise.h / denoise.c): +// +// int rnnoise_get_size(void); +// Returns sizeof(DenoiseState). Use this instead of sizeof() so the +// wrapper doesn't need to know the internal layout. +// +// int rnnoise_init(DenoiseState *st, const RNNModel *model); +// Initialise state in-place. model=NULL uses the built-in weights +// (rnnoise_model_orig compiled into rnnoise_tables.c). +// +// DenoiseState *rnnoise_create(const RNNModel *model); +// Allocate + init. Calls malloc() internally. +// +// void rnnoise_destroy(DenoiseState *st); +// Free. Calls free() internally. +// +// float rnnoise_process_frame(DenoiseState *st, float *out, const float *in); +// Process one 480-sample (10 ms at 48 kHz) mono frame. +// Samples are in full-scale float (not normalised to [-1,1]). +// Returns speech probability in [0, 1]. +// +// Memory: ~30 KB per DenoiseState (including RNN state). +// Dependencies: libm (expf, tanhf, sinf, cosf, sqrtf). + +#include +#include +#include "webrtc_ns2.h" + +#include /* from cross-built rnnoise library */ + +LOG_MODULE_DECLARE(webrtc_ns2, CONFIG_SOF_LOG_LEVEL); + +/* RNNoise uses full-scale float (raw amplitude, not normalised). */ +#define RNN_FULL_SCALE 32768.0f + +struct webrtc_ns2_rnn_data { + DenoiseState *st[WEBRTC_NS2_CHANNELS_MAX]; + int num_channels; +}; + +/* Per-frame scratch buffer at full scale. */ +static float rnn_in[WEBRTC_NS2_FRAME_SAMPLES]; +static float rnn_out[WEBRTC_NS2_FRAME_SAMPLES]; + +static int webrtc_ns2_rnn_init(struct processing_module *mod) +{ + struct webrtc_ns2_comp_data *cd = module_get_private_data(mod); + struct webrtc_ns2_rnn_data *rd; + + rd = mod_zalloc(mod, sizeof(*rd)); + if (!rd) + return -ENOMEM; + + cd->backend_data = rd; + comp_info(mod->dev, "webrtc_ns2: RNNoise real backend, frame=%d rate=%d", + WEBRTC_NS2_FRAME_SAMPLES, WEBRTC_NS2_SAMPLE_RATE); + return 0; +} + +static int webrtc_ns2_rnn_configure(struct processing_module *mod, int num_channels) +{ + struct webrtc_ns2_comp_data *cd = module_get_private_data(mod); + struct webrtc_ns2_rnn_data *rd = cd->backend_data; + int c; + + /* Free any previous instances from a re-prepare. */ + for (c = 0; c < rd->num_channels; c++) { + if (rd->st[c]) { + rnnoise_destroy(rd->st[c]); + rd->st[c] = NULL; + } + } + rd->num_channels = 0; + + for (c = 0; c < num_channels; c++) { + /* Pass NULL to use the built-in model weights. */ + rd->st[c] = rnnoise_create(NULL); + if (!rd->st[c]) { + comp_err(mod->dev, + "webrtc_ns2: rnnoise_create() failed ch%d", c); + goto err; + } + } + + rd->num_channels = num_channels; + comp_info(mod->dev, "webrtc_ns2: %d RNNoise instance(s) created", num_channels); + return 0; + +err: + for (c = 0; c < num_channels; c++) { + if (rd->st[c]) { + rnnoise_destroy(rd->st[c]); + rd->st[c] = NULL; + } + } + return -ENOMEM; +} + +static float webrtc_ns2_rnn_process_ch(struct processing_module *mod, + const float *in, float *out, int ch) +{ + struct webrtc_ns2_comp_data *cd = module_get_private_data(mod); + struct webrtc_ns2_rnn_data *rd = cd->backend_data; + float vad_prob; + int i; + + /* + * RNNoise expects full-scale float (raw PCM amplitude), but the SOF + * glue normalises to [-1, +1]. Scale up before processing and back + * down afterward. + */ + for (i = 0; i < WEBRTC_NS2_FRAME_SAMPLES; i++) + rnn_in[i] = in[i] * RNN_FULL_SCALE; + + vad_prob = rnnoise_process_frame(rd->st[ch], rnn_out, rnn_in); + + for (i = 0; i < WEBRTC_NS2_FRAME_SAMPLES; i++) + out[i] = rnn_out[i] / RNN_FULL_SCALE; + + return vad_prob; +} + +static int webrtc_ns2_rnn_reset(struct processing_module *mod) +{ + struct webrtc_ns2_comp_data *cd = module_get_private_data(mod); + struct webrtc_ns2_rnn_data *rd = cd->backend_data; + int c; + + /* + * RNNoise has no reset API; re-initialise each instance in-place + * using rnnoise_init() to clear the GRU hidden state. + */ + for (c = 0; c < rd->num_channels; c++) { + if (rd->st[c]) + rnnoise_init(rd->st[c], NULL); + } + return 0; +} + +static int webrtc_ns2_rnn_free(struct processing_module *mod) +{ + struct webrtc_ns2_comp_data *cd = module_get_private_data(mod); + struct webrtc_ns2_rnn_data *rd = cd->backend_data; + int c; + + if (!rd) + return 0; + + for (c = 0; c < rd->num_channels; c++) { + if (rd->st[c]) { + rnnoise_destroy(rd->st[c]); + rd->st[c] = NULL; + } + } + mod_free(mod, rd); + cd->backend_data = NULL; + return 0; +} + +const struct webrtc_ns2_backend webrtc_ns2_backend = { + .name = "rnnoise", + .init = webrtc_ns2_rnn_init, + .configure = webrtc_ns2_rnn_configure, + .process_ch = webrtc_ns2_rnn_process_ch, + .reset = webrtc_ns2_rnn_reset, + .free = webrtc_ns2_rnn_free, +}; diff --git a/src/audio/webrtc_ns2/webrtc_ns2-stub.c b/src/audio/webrtc_ns2/webrtc_ns2-stub.c new file mode 100644 index 000000000000..0cc943319c19 --- /dev/null +++ b/src/audio/webrtc_ns2/webrtc_ns2-stub.c @@ -0,0 +1,53 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Stub backend for webrtc_ns2. Passes audio through unchanged. +// Returns VAD probability = 1.0 (always speech). + +#include +#include +#include "webrtc_ns2.h" + +LOG_MODULE_DECLARE(webrtc_ns2, CONFIG_SOF_LOG_LEVEL); + +static int webrtc_ns2_stub_init(struct processing_module *mod) +{ + comp_info(mod->dev, "webrtc_ns2 stub: no RNNoise library linked"); + return 0; +} + +static int webrtc_ns2_stub_configure(struct processing_module *mod, int num_channels) +{ + comp_info(mod->dev, "webrtc_ns2 stub: ch=%d (ignored)", num_channels); + return 0; +} + +static float webrtc_ns2_stub_process_ch(struct processing_module *mod, + const float *in, float *out, int ch) +{ + memcpy(out, in, WEBRTC_NS2_FRAME_SAMPLES * sizeof(float)); + (void)ch; + return 1.0f; /* always report speech */ +} + +static int webrtc_ns2_stub_reset(struct processing_module *mod) +{ + (void)mod; + return 0; +} + +static int webrtc_ns2_stub_free(struct processing_module *mod) +{ + (void)mod; + return 0; +} + +const struct webrtc_ns2_backend webrtc_ns2_backend = { + .name = "stub", + .init = webrtc_ns2_stub_init, + .configure = webrtc_ns2_stub_configure, + .process_ch = webrtc_ns2_stub_process_ch, + .reset = webrtc_ns2_stub_reset, + .free = webrtc_ns2_stub_free, +}; diff --git a/src/audio/webrtc_ns2/webrtc_ns2.c b/src/audio/webrtc_ns2/webrtc_ns2.c new file mode 100644 index 000000000000..8642dd3b3039 --- /dev/null +++ b/src/audio/webrtc_ns2/webrtc_ns2.c @@ -0,0 +1,335 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// webrtc_ns2 — SOF module_interface glue for the RNNoise deep-learning +// noise suppressor. +// +// Pass-through PCM effect: audio flows source → sink after per-channel +// RNNoise denoising. Each 480-sample (10 ms at 48 kHz) frame is +// processed independently per channel. Partial pipeline periods are +// accumulated until a full frame is ready. +// +// As a bonus, the per-frame VAD probability returned by RNNoise is +// compared against a configurable threshold and a NOTIFIER_ID_VAD event +// is fired, making this a drop-in combined denoiser + voice detector. + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "webrtc_ns2.h" + +SOF_DEFINE_REG_UUID(webrtc_ns2); +LOG_MODULE_REGISTER(webrtc_ns2, CONFIG_SOF_LOG_LEVEL); + +/* Normalisation constants for S16 ↔ float and S32 ↔ float. */ +#define NS2_S16_SCALE 32768.0f +#define NS2_S32_SCALE 2147483648.0f + +/* ---------------------------------------------------------------- + * Format conversion helpers (interleaved PCM ↔ per-channel float) + * ---------------------------------------------------------------- */ + +static inline void src_to_float(struct webrtc_ns2_comp_data *cd, + const void *raw, int n_frames, int frame0) +{ + int i, c, ch = cd->channels; + + if (cd->is_s32) { + const int32_t *p = raw; + + for (i = 0; i < n_frames; i++) + for (c = 0; c < ch; c++) + cd->in_buf[c][frame0 + i] = + (float)p[i * ch + c] / NS2_S32_SCALE; + } else { + const int16_t *p = raw; + + for (i = 0; i < n_frames; i++) + for (c = 0; c < ch; c++) + cd->in_buf[c][frame0 + i] = + (float)p[i * ch + c] / NS2_S16_SCALE; + } +} + +static inline void float_to_sink(struct webrtc_ns2_comp_data *cd, + void *raw, int n_frames) +{ + int i, c, ch = cd->channels; + + if (cd->is_s32) { + int32_t *p = raw; + + for (i = 0; i < n_frames; i++) { + for (c = 0; c < ch; c++) { + float f = cd->out_buf[c][i] * NS2_S32_SCALE; + + f = f > 2147483647.0f ? 2147483647.0f : + f < -2147483648.0f ? -2147483648.0f : f; + p[i * ch + c] = (int32_t)f; + } + } + } else { + int16_t *p = raw; + + for (i = 0; i < n_frames; i++) { + for (c = 0; c < ch; c++) { + float f = cd->out_buf[c][i] * NS2_S16_SCALE; + + f = f > 32767.0f ? 32767.0f : + f < -32768.0f ? -32768.0f : f; + p[i * ch + c] = (int16_t)f; + } + } + } +} + +/* ---------------------------------------------------------------- + * module_interface operations + * ---------------------------------------------------------------- */ + +__cold static int webrtc_ns2_init(struct processing_module *mod) +{ + struct module_data *md = &mod->priv; + struct comp_dev *dev = mod->dev; + struct webrtc_ns2_comp_data *cd; + int ret; + + assert_can_be_cold(); + comp_info(dev, "webrtc_ns2: init"); + + cd = mod_zalloc(mod, sizeof(*cd)); + if (!cd) + return -ENOMEM; + + md->private = cd; + cd->backend = &webrtc_ns2_backend; + cd->vad_threshold = (float)CONFIG_WEBRTC_NS2_VAD_THRESHOLD_PCT / 100.0f; + + comp_info(dev, "webrtc_ns2: backend '%s' vad_threshold=%.2f", + cd->backend->name, (double)cd->vad_threshold); + + if (cd->backend->init) { + ret = cd->backend->init(mod); + if (ret) { + comp_err(dev, "webrtc_ns2: backend init failed %d", ret); + mod_free(mod, cd); + return ret; + } + } + + return 0; +} + +__cold static int webrtc_ns2_prepare(struct processing_module *mod, + struct sof_source **sources, int num_of_sources, + struct sof_sink **sinks, int num_of_sinks) +{ + struct webrtc_ns2_comp_data *cd = module_get_private_data(mod); + struct comp_dev *dev = mod->dev; + int rate, fmt, ret; + + assert_can_be_cold(); + + if (num_of_sources != 1 || num_of_sinks != 1) { + comp_err(dev, "webrtc_ns2: need exactly 1 source and 1 sink"); + return -EINVAL; + } + + rate = source_get_rate(sources[0]); + fmt = source_get_frm_fmt(sources[0]); + cd->channels = source_get_channels(sources[0]); + + /* RNNoise is hard-wired to 48 kHz. */ + if (rate != WEBRTC_NS2_SAMPLE_RATE) { + comp_err(dev, "webrtc_ns2: rate %d != required %d Hz; " + "add an ASRC upstream", rate, WEBRTC_NS2_SAMPLE_RATE); + return -EINVAL; + } + + if (cd->channels > WEBRTC_NS2_CHANNELS_MAX) { + comp_err(dev, "webrtc_ns2: %d channels exceeds max %d", + cd->channels, WEBRTC_NS2_CHANNELS_MAX); + return -EINVAL; + } + + switch (fmt) { + case SOF_IPC_FRAME_S16_LE: + cd->is_s32 = false; + break; + case SOF_IPC_FRAME_S32_LE: + cd->is_s32 = true; + break; + default: + comp_err(dev, "webrtc_ns2: unsupported format %d", fmt); + return -EINVAL; + } + + comp_info(dev, "webrtc_ns2: rate=%d ch=%d %s frame=%d", + rate, cd->channels, cd->is_s32 ? "S32" : "S16", + WEBRTC_NS2_FRAME_SAMPLES); + + if (cd->backend->configure) { + ret = cd->backend->configure(mod, cd->channels); + if (ret) { + comp_err(dev, "webrtc_ns2: backend configure failed %d", ret); + return ret; + } + } + + cd->buffered_frames = 0; + cd->last_vad = -1; /* unknown */ + cd->configured = true; + return 0; +} + +static int webrtc_ns2_process(struct processing_module *mod, + struct sof_source **sources, int num_of_sources, + struct sof_sink **sinks, int num_of_sinks) +{ + struct webrtc_ns2_comp_data *cd = module_get_private_data(mod); + struct sof_source *src = sources[0]; + struct sof_sink *snk = sinks[0]; + int frame_bytes = source_get_frame_bytes(src); + int avail = (int)source_get_data_frames_available(src); + int free_f = (int)sink_get_free_frames(snk); + int n = MIN(avail, free_f); + size_t nbytes; + const void *rd_ptr, *rd_start; + void *wr_ptr, *wr_start; + size_t buf_sz; + int ret, frames_rem, chunk; + + if (n <= 0) + return 0; + + nbytes = (size_t)n * frame_bytes; + + ret = source_get_data(src, nbytes, &rd_ptr, &rd_start, &buf_sz); + if (ret) + return ret; + + ret = sink_get_buffer(snk, nbytes, &wr_ptr, &wr_start, &buf_sz); + if (ret) { + source_release_data(src, 0); + return ret; + } + + /* + * Accumulate into in_buf[], process full 480-sample frames, + * drain denoised samples from out_buf[] into the sink write pointer. + * Both rd_ptr and wr_ptr advance by frame_bytes * n total. + */ + const char *rp = rd_ptr; + char *wp = wr_ptr; + + for (frames_rem = n; frames_rem > 0; frames_rem -= chunk) { + chunk = MIN(frames_rem, + WEBRTC_NS2_FRAME_SAMPLES - cd->buffered_frames); + + src_to_float(cd, rp, chunk, cd->buffered_frames); + rp += chunk * frame_bytes; + cd->buffered_frames += chunk; + + if (cd->buffered_frames >= WEBRTC_NS2_FRAME_SAMPLES) { + float vad_prob = 0.0f, ch_prob; + int c; + + for (c = 0; c < cd->channels; c++) { + ch_prob = cd->backend->process_ch( + mod, + cd->in_buf[c], + cd->out_buf[c], c); + /* Average VAD probability across channels. */ + if (ch_prob >= 0.0f) + vad_prob += ch_prob; + } + vad_prob /= (float)cd->channels; + + /* Write denoised samples to sink. */ + float_to_sink(cd, wp, WEBRTC_NS2_FRAME_SAMPLES); + wp += WEBRTC_NS2_FRAME_SAMPLES * frame_bytes; + + cd->buffered_frames = 0; + +#if CONFIG_WEBRTC_NS2_VAD_NOTIFY + { + int vad = (vad_prob >= cd->vad_threshold) ? 1 : 0; + + if (vad != cd->last_vad) { + notifier_event(mod->dev, NOTIFIER_ID_VAD, + NOTIFIER_TARGET_CORE_LOCAL, + &vad, sizeof(vad)); + cd->last_vad = vad; + } + } +#endif + } + } + + source_release_data(src, nbytes); + sink_commit_buffer(snk, nbytes); + return 0; +} + +static int webrtc_ns2_reset(struct processing_module *mod) +{ + struct webrtc_ns2_comp_data *cd = module_get_private_data(mod); + + comp_dbg(mod->dev, "webrtc_ns2: reset"); + cd->buffered_frames = 0; + cd->last_vad = -1; + + if (cd->backend->reset) + return cd->backend->reset(mod); + + return 0; +} + +__cold static int webrtc_ns2_free(struct processing_module *mod) +{ + struct webrtc_ns2_comp_data *cd = module_get_private_data(mod); + + assert_can_be_cold(); + comp_dbg(mod->dev, "webrtc_ns2: free"); + + if (cd->backend->free) + cd->backend->free(mod); + + mod_free(mod, cd); + return 0; +} + +static const struct module_interface webrtc_ns2_interface = { + .init = webrtc_ns2_init, + .prepare = webrtc_ns2_prepare, + .process = webrtc_ns2_process, + .reset = webrtc_ns2_reset, + .free = webrtc_ns2_free, +}; + +#if CONFIG_COMP_WEBRTC_NS2_MODULE + +#include +#include +#include + +static const struct sof_man_module_manifest mod_manifest __section(".module") __used = + SOF_LLEXT_MODULE_MANIFEST("WRTCNS2", &webrtc_ns2_interface, 1, + SOF_REG_UUID(webrtc_ns2), 40); + +SOF_LLEXT_BUILDINFO; + +#else + +DECLARE_TR_CTX(webrtc_ns2_tr, SOF_UUID(webrtc_ns2_uuid), LOG_LEVEL_INFO); +DECLARE_MODULE_ADAPTER(webrtc_ns2_interface, webrtc_ns2_uuid, webrtc_ns2_tr); +SOF_MODULE_INIT(webrtc_ns2, sys_comp_module_webrtc_ns2_interface_init); + +#endif diff --git a/src/audio/webrtc_ns2/webrtc_ns2.cmake b/src/audio/webrtc_ns2/webrtc_ns2.cmake new file mode 100644 index 000000000000..f3f4604050e3 --- /dev/null +++ b/src/audio/webrtc_ns2/webrtc_ns2.cmake @@ -0,0 +1,76 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Cross-build RNNoise from the xiph/rnnoise repository for the webrtc_ns2 +# LLEXT module. RNNoise is pure C, requires libm (expf, tanhf, sinf, cosf, +# sqrtf), and carries built-in model weights in rnnoise_tables.c. +# +# Pinned SHA: 70f1d256acd4b34a572f999a05c87bf00b67730d (Feb 2025) +# +# Produces: +# ${WEBRTC_NS2_INSTALL_DIR}/lib/librnnoise.a +# ${WEBRTC_NS2_INSTALL_DIR}/include/rnnoise.h + +if(NOT DEFINED SOF_RNNOISE_SRC_DIR) + set(SOF_RNNOISE_SRC_DIR "${sof_top_dir}/../modules/audio/rnnoise" + CACHE PATH "xiph/rnnoise source directory (west-pinned)") +endif() +cmake_path(NORMAL_PATH SOF_RNNOISE_SRC_DIR) +if(NOT EXISTS "${SOF_RNNOISE_SRC_DIR}/src/denoise.c") + message(FATAL_ERROR + "webrtc_ns2: RNNoise source not found at '${SOF_RNNOISE_SRC_DIR}'.\n" + "Run 'west update' (rnnoise is pinned in west.yml) or pass\n" + "-DSOF_RNNOISE_SRC_DIR=.") +endif() + +get_filename_component(_tc_dir "${CMAKE_C_COMPILER}" DIRECTORY) +get_filename_component(_tc_name "${CMAKE_C_COMPILER}" NAME) +string(REGEX REPLACE "gcc$" "" _tc_prefix_name "${_tc_name}") +set(_rnn_cross_prefix "${_tc_dir}/${_tc_prefix_name}") + +set(WEBRTC_NS2_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/rnnoise-install" + CACHE INTERNAL "webrtc_ns2: RNNoise library install prefix") + +set(_rnn_src "${SOF_RNNOISE_SRC_DIR}/src") +set(_rnn_inc "${SOF_RNNOISE_SRC_DIR}/include") + +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/rnnoise-build.sh" +"#!/bin/sh +set -e +SRC=${_rnn_src} +INC=${_rnn_inc} +INST=${WEBRTC_NS2_INSTALL_DIR} +OBJ=${CMAKE_CURRENT_BINARY_DIR}/rnnoise-obj + +mkdir -p \"$OBJ\" \"$INST/lib\" \"$INST/include\" + +CFLAGS=\"-O2 -fPIC -I$SRC -I$INC -DOUTSIDE_SPEEX -DRANDOM_PREFIX=rnn\" + +# Core RNNoise sources (excludes dump_features.c and train-only files). +SRCS=\" + $SRC/denoise.c + $SRC/rnn.c + $SRC/rnn_data.c + $SRC/pitch.c + $SRC/celt_lpc.c + $SRC/kiss_fft.c +\" + +for f in \$SRCS; do + bn=\$(basename \"\$f\" .c) + ${CMAKE_C_COMPILER} \$CFLAGS -c \"\$f\" -o \"$OBJ/\${bn}.o\" +done + +${_rnn_cross_prefix}ar rcs \"$INST/lib/librnnoise.a\" \"$OBJ\"/*.o +cp \"$INC/rnnoise.h\" \"$INST/include/\" +") + +add_custom_command( + OUTPUT "${WEBRTC_NS2_INSTALL_DIR}/lib/librnnoise.a" + "${WEBRTC_NS2_INSTALL_DIR}/include/rnnoise.h" + COMMAND sh "${CMAKE_CURRENT_BINARY_DIR}/rnnoise-build.sh" + VERBATIM) + +add_custom_target(rnnoise_ext + DEPENDS "${WEBRTC_NS2_INSTALL_DIR}/lib/librnnoise.a") + +message(STATUS "webrtc_ns2: cross-building RNNoise from ${SOF_RNNOISE_SRC_DIR}") diff --git a/src/audio/webrtc_ns2/webrtc_ns2.h b/src/audio/webrtc_ns2/webrtc_ns2.h new file mode 100644 index 000000000000..51c74ffd9da6 --- /dev/null +++ b/src/audio/webrtc_ns2/webrtc_ns2.h @@ -0,0 +1,84 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright(c) 2026 Intel Corporation. + * + * webrtc_ns2 — RNNoise deep-learning noise suppressor for SOF. + * + * Single-input, single-output PCM effect. Processes interleaved S16/S32 + * at 48 kHz in 480-sample (10 ms) mono frames. One RNNoise instance is + * allocated per channel and processes channel audio independently. + * + * In addition to denoising, the module broadcasts a NOTIFIER_ID_VAD event + * from the per-frame speech probability returned by rnnoise_process_frame(). + */ +#ifndef __SOF_AUDIO_WEBRTC_NS2_H__ +#define __SOF_AUDIO_WEBRTC_NS2_H__ + +#include +#include +#include + +/* RNNoise is strictly 48 kHz, 10 ms frames. */ +#define WEBRTC_NS2_SAMPLE_RATE 48000 +#define WEBRTC_NS2_FRAME_SAMPLES 480 /* 10 ms at 48 kHz */ +#define WEBRTC_NS2_CHANNELS_MAX CONFIG_WEBRTC_NS2_CHANNELS_MAX + +/** + * struct webrtc_ns2_backend - NS2 backend operations. + */ +struct webrtc_ns2_backend { + const char *name; + + /* One-time init called from module init(). */ + int (*init)(struct processing_module *mod); + + /** + * Open and configure one RNNoise instance per channel. + * Called from prepare(). + * @num_channels: number of channels to process + */ + int (*configure)(struct processing_module *mod, int num_channels); + + /** + * Process one 480-sample mono frame for a single channel. + * @in: 480 floats in [-1.0, +1.0] (pipeline samples normalised) + * @out: 480 floats out (denoised) + * @ch: channel index + * Returns speech probability in [0.0, 1.0], or < 0 on error. + */ + float (*process_ch)(struct processing_module *mod, + const float *in, float *out, int ch); + + /* Reset per-channel state (history buffers). Called from reset(). */ + int (*reset)(struct processing_module *mod); + + /* Deallocate all state. Called from free(). */ + int (*free)(struct processing_module *mod); +}; + +/** + * struct webrtc_ns2_comp_data - webrtc_ns2 module private data. + */ +struct webrtc_ns2_comp_data { + const struct webrtc_ns2_backend *backend; + void *backend_data; + + int channels; + bool is_s32; /* true when pipeline format is S32_LE */ + int buffered_frames; /* frames accumulated toward 480 */ + + /* Per-channel float scratch buffers (480 samples each). */ + float in_buf[WEBRTC_NS2_CHANNELS_MAX][WEBRTC_NS2_FRAME_SAMPLES]; + float out_buf[WEBRTC_NS2_CHANNELS_MAX][WEBRTC_NS2_FRAME_SAMPLES]; + + /* VAD: last decision and threshold. */ + int last_vad; + float vad_threshold; + + bool configured; +}; + +/* Backend instance provided by the selected translation unit. */ +extern const struct webrtc_ns2_backend webrtc_ns2_backend; + +#endif /* __SOF_AUDIO_WEBRTC_NS2_H__ */ diff --git a/src/audio/webrtc_ns2/webrtc_ns2.toml b/src/audio/webrtc_ns2/webrtc_ns2.toml new file mode 100644 index 000000000000..8a35dfb1c38e --- /dev/null +++ b/src/audio/webrtc_ns2/webrtc_ns2.toml @@ -0,0 +1,24 @@ +#ifndef LOAD_TYPE +#define LOAD_TYPE "0" +#endif + + REM # RNNoise deep-learning noise suppressor module config. + REM # Single input, single output. 48 kHz only. Float-only. + [[module.entry]] + name = "WRTCNS2" + uuid = UUIDREG_STR_WEBRTC_NS2 + affinity_mask = "0x1" + instance_count = "40" + domain_types = "0" + load_type = LOAD_TYPE + module_type = "9" + auto_start = "0" + sched_caps = [1, 0x00008000] + REM # pin = [dir, type, sample rate, size, container, channel-cfg] + REM # Single input (48 kHz PCM), single output. + pin = [0, 0, 0xfeef, 0xf, 0xf, 0x45ff, 1, 0, 0xfeef, 0xf, 0xf, 0x45ff] + REM # mod_cfg: RNNoise ~30KB state, ~10ms latency, 48kHz stereo S32 + REM # [PAR_0 PAR_1 PAR_2 PAR_3 IS_BYTES CPS IBS OBS MOD_FLAGS CPC OBLS] + mod_cfg = [0, 0, 0, 0, 0, 5000000, 16384, 16384, 0, 5000, 0] + + index = __COUNTER__ diff --git a/src/audio/webrtc_vad/CMakeLists.txt b/src/audio/webrtc_vad/CMakeLists.txt new file mode 100644 index 000000000000..e0fc7b39ffaf --- /dev/null +++ b/src/audio/webrtc_vad/CMakeLists.txt @@ -0,0 +1,17 @@ +# SPDX-License-Identifier: BSD-3-Clause + +if(CONFIG_COMP_WEBRTC_VAD STREQUAL "m" AND DEFINED CONFIG_LLEXT) + add_subdirectory(llext ${PROJECT_BINARY_DIR}/webrtc_vad_llext) + add_dependencies(app webrtc_vad) +else() + add_local_sources(sof webrtc_vad.c) + + if(CONFIG_COMP_WEBRTC_VAD_STUB) + add_local_sources(sof webrtc_vad-stub.c) + else() + add_local_sources(sof webrtc_vad-fvad.c) + # NOTE: the libc symbols needed by libfvad (malloc/free/memset/...) are + # already provided by the SOF core in built-in and testbench builds. + # For LLEXT the shims are in llext/CMakeLists.txt (webrtc_vad-shims.c). + endif() +endif() diff --git a/src/audio/webrtc_vad/Kconfig b/src/audio/webrtc_vad/Kconfig new file mode 100644 index 000000000000..d90a6e6a75ec --- /dev/null +++ b/src/audio/webrtc_vad/Kconfig @@ -0,0 +1,56 @@ +# SPDX-License-Identifier: BSD-3-Clause + +config COMP_WEBRTC_VAD + tristate "WebRTC Voice Activity Detection (libfvad)" + help + Select to include the WebRTC Voice Activity Detection module. It + wraps the libfvad library (a standalone pure-C extraction of the + WebRTC GMM VAD) behind the SOF module interface. The module + inspects each 10/20/30 ms PCM frame and annotates it as speech or + non-speech via a NOTIFIER_ID_VAD notifier event, leaving the audio + stream unmodified (pass-through). + + libfvad supports 8, 16, 32 and 48 kHz; it resamples to 8 kHz + internally. The GMM classifier operates in Q15 fixed-point and + requires no FPU. + + The real backend requires libfvad cross-built for the target + (west module: modules/audio/libfvad). Without the source, or for + CI/stub builds, select COMP_WEBRTC_VAD_STUB. + +if COMP_WEBRTC_VAD + +config COMP_WEBRTC_VAD_STUB + bool "WebRTC VAD stub backend (no libfvad dependency)" + default y if COMP_STUBS + help + Build the webrtc_vad module against a dependency-free stub backend + instead of libfvad. The stub always reports speech (VAD = 1) so + the audio data flow and LLEXT packaging can be validated without + the libfvad source or a cross-built archive. + +config WEBRTC_VAD_MODE + int "VAD aggressiveness mode (0=quality ... 3=aggressive)" + range 0 3 + default 2 + help + Sets the libfvad operating mode which controls the trade-off + between false-positive rate and missed-speech rate: + 0 - highest quality (fewest false alarms, may miss quiet speech) + 1 - moderate quality + 2 - moderate aggressiveness (default) + 3 - most aggressive (detects most speech, more false alarms) + This option is also used by the stub backend (mode value is ignored + by the stub but must be a valid integer for compilation). + +config WEBRTC_VAD_FRAME_MS + int "VAD frame length in milliseconds (10, 20 or 30)" + default 10 + help + libfvad processes audio in fixed-size frames. Valid values are + 10, 20 and 30 ms. Smaller frames give lower latency; 10 ms is + the recommended default and matches the WebRTC pipeline convention. + The SOF pipeline period must be a multiple of this value (the + module accumulates sub-frames internally if needed). + +endif # COMP_WEBRTC_VAD diff --git a/src/audio/webrtc_vad/README.md b/src/audio/webrtc_vad/README.md new file mode 100644 index 000000000000..f81a69b406fa --- /dev/null +++ b/src/audio/webrtc_vad/README.md @@ -0,0 +1,88 @@ +# WebRTC Voice Activity Detection Module (`webrtc_vad`) + +Wraps [libfvad](https://github.com/dpirch/libfvad) — a standalone, pure-C, BSD-3 licensed extraction of the WebRTC GMM Voice Activity Detection algorithm — behind the SOF `module_interface`. + +--- + +## Features + +- **GMM Classifier**: Uses Gaussian Mixture Models (GMM) for robust speech vs. noise classification. +- **Pass-through Data Flow**: Does not modify PCM audio; passes it through untouched. +- **Notifier Integration**: Broadcasts binary speech/silence decisions (`1` or `0`) via SOF's `NOTIFIER_ID_VAD` event channel. +- **Low Power & Fixed-Point**: Operates entirely in Q15 fixed-point; requires no floating-point hardware (FPU). +- **Flexible Frame Sizes**: Supports 10 ms, 20 ms, and 30 ms classification frame windows. +- **Multichannel Support**: Classifies channel-0 and copies remaining channels. +- **LLEXT Packaging**: Full support for loading as a shared library module. + +--- + +## Architecture & Data Flow + +The following Mermaid diagram outlines how audio data flows through the pipeline and how the classification events are broadcast to other modules: + +```mermaid +graph TD + %% Audio Data Flow + InBuf[Input Audio Buffer] -->|S16 or S32 PCM| Core[webrtc_vad.c Core] + Core -->|Pass-through| OutBuf[Output Audio Buffer] + + %% Accumulation & Classification + Core -->|Extract Channel 0 S16| RingBuf[Ring Buffer Accumulator] + RingBuf -->|10/20/30 ms Block| Backend[webrtc_vad-fvad.c Backend] + Backend -->|libfvad GMM Classify| Decision{Speech Detected?} + + %% Notifications + Decision -->|Yes: 1 / No: 0| Notifier[NOTIFIER_ID_VAD Event] + Notifier -->|.subscribe| SubscribedModules[e.g., webrtc_ns2, Host, KWD] +``` + +### Components +- `webrtc_vad.c`: SOF module interface glue (handles pipeline buffers and state transitions). +- `webrtc_vad-fvad.c`: Standard libfvad execution backend. +- `webrtc_vad-stub.c`: Pass-through stub that always reports `1` (speech) to satisfy CI validation. +- `webrtc_vad-shims.c`: Cross-compilation memory wrapper mappings. +- `webrtc_vad.cmake`: External build driving the download and build of the `libfvad` library. + +--- + +## Build Instructions + +### 1. Stub Mode (CI / Staging) +Building without external repository dependencies: +```ini +CONFIG_COMP_WEBRTC_VAD=y # or =m for LLEXT +CONFIG_COMP_WEBRTC_VAD_STUB=y +``` + +### 2. Real VAD Integration +Pulls `libfvad` automatically via West and compiles the full GMM detector: +```ini +CONFIG_COMP_WEBRTC_VAD=m +CONFIG_COMP_WEBRTC_VAD_STUB=n +``` +Ensure you run `west update` after updating `west.yml` to retrieve `modules/audio/libfvad`. + +--- + +## Kconfig Parameters + +| Option | Default | Range / Value | Description | +|---|---|---|---| +| `CONFIG_COMP_WEBRTC_VAD` | n | y / m / n | Enable WebRTC GMM VAD module | +| `CONFIG_COMP_WEBRTC_VAD_STUB` | y | y / n | Use pass-through stub | +| `CONFIG_WEBRTC_VAD_MODE` | 2 | 0 - 3 | Aggressiveness (3 = most restrictive) | +| `CONFIG_WEBRTC_VAD_FRAME_MS` | 10 | 10, 20, 30 | Window size for classification (ms) | + +--- + +## Usage & Topology + +### Pipeline Integration +The module functions as a simple 1-in / 1-out effect widget. Connect it directly in your capture stream path. + +#### Example Topology Route +``` +DAI Copier (Mic) ---> [ webrtc-vad ] ---> [ webrtc-ns ] ---> Host Copier +``` + +In this routing model, `webrtc-vad` runs first. Any downstream module or host application subscribing to the `NOTIFIER_ID_VAD` event will receive real-time classification changes instantly without querying the module adapter. diff --git a/src/audio/webrtc_vad/llext/CMakeLists.txt b/src/audio/webrtc_vad/llext/CMakeLists.txt new file mode 100644 index 000000000000..7d12eb109978 --- /dev/null +++ b/src/audio/webrtc_vad/llext/CMakeLists.txt @@ -0,0 +1,28 @@ +# Copyright (c) 2026 Intel Corporation. +# SPDX-License-Identifier: Apache-2.0 + +if(CONFIG_COMP_WEBRTC_VAD_STUB) + # Dependency-free build: SOF glue + passthrough stub backend, no libfvad. + sof_llext_build("webrtc_vad" + SOURCES ../webrtc_vad.c + ../webrtc_vad-stub.c + LIB openmodules + ) +else() + # Real VAD: cross-build libfvad and link it. + include(${CMAKE_CURRENT_LIST_DIR}/../webrtc_vad.cmake) + + sof_llext_build("webrtc_vad" + SOURCES ../webrtc_vad.c + ../webrtc_vad-fvad.c + ../webrtc_vad-shims.c + INCLUDES "${LIBFVAD_INSTALL_DIR}/include" + LIBS_PATH "${LIBFVAD_INSTALL_DIR}/lib" + LIBS fvad + LIB openmodules + ) + + # The archive must be built before the module compiles/links against it. + add_dependencies(webrtc_vad_llext_lib fvad_ext) + add_dependencies(webrtc_vad fvad_ext) +endif() diff --git a/src/audio/webrtc_vad/llext/llext.toml.h b/src/audio/webrtc_vad/llext/llext.toml.h new file mode 100644 index 000000000000..4975162cab08 --- /dev/null +++ b/src/audio/webrtc_vad/llext/llext.toml.h @@ -0,0 +1,6 @@ +#include +#define LOAD_TYPE "2" +#include "../webrtc_vad.toml" + +[module] +count = __COUNTER__ diff --git a/src/audio/webrtc_vad/webrtc_vad-fvad.c b/src/audio/webrtc_vad/webrtc_vad-fvad.c new file mode 100644 index 000000000000..ef1988db8cd7 --- /dev/null +++ b/src/audio/webrtc_vad/webrtc_vad-fvad.c @@ -0,0 +1,127 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// libfvad backend for the webrtc_vad module. +// +// libfvad is a standalone pure-C BSD-3 extraction of the WebRTC GMM Voice +// Activity Detection algorithm. It operates in Q15 fixed-point and requires +// no FPU, making it well suited to Xtensa DSPs and Cortex-M class targets. +// +// The backend wraps the following minimal libfvad API: +// +// fvad_new() — allocate a FvadState +// fvad_set_mode() — aggressiveness 0..3 +// fvad_set_sample_rate() — 8000/16000/32000/48000 Hz +// fvad_process() — classify num_samples of int16 mono audio +// fvad_free() — release FvadState +// +// Requires cross-compiled libfvad static library and headers (west module +// modules/audio/libfvad; see webrtc_vad.cmake for the cross-build recipe). +// Only compiled when CONFIG_COMP_WEBRTC_VAD_STUB is not selected. + +#include +#include +#include +#include "webrtc_vad.h" + +#include /* from cross-built libfvad install */ + +LOG_MODULE_DECLARE(webrtc_vad, CONFIG_SOF_LOG_LEVEL); + +static int webrtc_vad_fvad_init(struct processing_module *mod) +{ + struct webrtc_vad_comp_data *cd = module_get_private_data(mod); + Fvad *fvad; + + fvad = fvad_new(); + if (!fvad) { + comp_err(mod->dev, "webrtc_vad: fvad_new() failed"); + return -ENOMEM; + } + + cd->backend_data = fvad; + comp_info(mod->dev, "webrtc_vad: libfvad backend initialised"); + return 0; +} + +static int webrtc_vad_fvad_configure(struct processing_module *mod, + int sample_rate_hz, int mode) +{ + struct webrtc_vad_comp_data *cd = module_get_private_data(mod); + Fvad *fvad = cd->backend_data; + int ret; + + ret = fvad_set_sample_rate(fvad, sample_rate_hz); + if (ret < 0) { + comp_err(mod->dev, "webrtc_vad: fvad_set_sample_rate(%d) failed %d", + sample_rate_hz, ret); + return -EINVAL; + } + + ret = fvad_set_mode(fvad, mode); + if (ret < 0) { + comp_err(mod->dev, "webrtc_vad: fvad_set_mode(%d) failed %d", + mode, ret); + return -EINVAL; + } + + comp_info(mod->dev, "webrtc_vad: libfvad rate=%d mode=%d", + sample_rate_hz, mode); + return 0; +} + +static int webrtc_vad_fvad_classify(struct processing_module *mod, + const int16_t *samples, int num_samples) +{ + struct webrtc_vad_comp_data *cd = module_get_private_data(mod); + Fvad *fvad = cd->backend_data; + int decision; + + /* fvad_process returns 1 (speech), 0 (non-speech), or <0 on error. */ + decision = fvad_process(fvad, samples, num_samples); + if (decision < 0) { + comp_err(mod->dev, "webrtc_vad: fvad_process error %d", decision); + return -EIO; + } + + return decision; +} + +static int webrtc_vad_fvad_reset(struct processing_module *mod) +{ + struct webrtc_vad_comp_data *cd = module_get_private_data(mod); + Fvad *fvad = cd->backend_data; + + /* libfvad has no explicit reset API; re-init the state via free+new. */ + int rate = cd->sample_rate; + int mode = CONFIG_WEBRTC_VAD_MODE; + + fvad_free(fvad); + fvad = fvad_new(); + if (!fvad) + return -ENOMEM; + + cd->backend_data = fvad; + return webrtc_vad_fvad_configure(mod, rate, mode); +} + +static int webrtc_vad_fvad_free(struct processing_module *mod) +{ + struct webrtc_vad_comp_data *cd = module_get_private_data(mod); + + if (cd->backend_data) { + fvad_free(cd->backend_data); + cd->backend_data = NULL; + } + return 0; +} + +const struct webrtc_vad_backend webrtc_vad_backend = { + .name = "fvad", + .init = webrtc_vad_fvad_init, + .configure = webrtc_vad_fvad_configure, + .classify = webrtc_vad_fvad_classify, + .reset = webrtc_vad_fvad_reset, + .free = webrtc_vad_fvad_free, +}; diff --git a/src/audio/webrtc_vad/webrtc_vad-shims.c b/src/audio/webrtc_vad/webrtc_vad-shims.c new file mode 100644 index 000000000000..b2b35b6fbf8d --- /dev/null +++ b/src/audio/webrtc_vad/webrtc_vad-shims.c @@ -0,0 +1,89 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Local libc surface for the webrtc_vad libfvad backend in LLEXT context. +// +// libfvad references a small set of libc symbols that the SOF core does not +// export to LLEXT modules. This file defines them inside the module so it +// resolves at load time. Unlike the ffmpeg_dec shims, libfvad's requirements +// are minimal — it only needs memory allocation and a handful of string/math +// helpers. +// +// NOTE: this file intentionally includes NO libc headers to avoid clashing +// with newlib prototypes; only the symbol names matter to the linker. + +#include +#include +#include + +/* ============================ Memory ============================ */ + +void *malloc(size_t size) +{ + return rballoc(SOF_MEM_FLAG_USER, size); +} + +void free(void *ptr) +{ + rfree(ptr); +} + +void *calloc(size_t nmemb, size_t size) +{ + void *p = rballoc(SOF_MEM_FLAG_USER, nmemb * size); + + if (p) + memset(p, 0, nmemb * size); + return p; +} + +/* ============================ String helpers ===================== */ + +void *memset(void *s, int c, size_t n) +{ + uint8_t *p = s; + + while (n--) + *p++ = (uint8_t)c; + return s; +} + +void *memcpy(void *dest, const void *src, size_t n) +{ + const uint8_t *s = src; + uint8_t *d = dest; + + while (n--) + *d++ = *s++; + return dest; +} + +void *memmove(void *dest, const void *src, size_t n) +{ + uint8_t *d = dest; + const uint8_t *s = src; + + if (d < s || d >= s + n) { + while (n--) + *d++ = *s++; + } else { + d += n; + s += n; + while (n--) + *--d = *--s; + } + return dest; +} + +int memcmp(const void *s1, const void *s2, size_t n) +{ + const uint8_t *a = s1, *b = s2; + + while (n--) { + if (*a != *b) + return *a - *b; + a++; b++; + } + return 0; +} diff --git a/src/audio/webrtc_vad/webrtc_vad-stub.c b/src/audio/webrtc_vad/webrtc_vad-stub.c new file mode 100644 index 000000000000..278fb3545a6a --- /dev/null +++ b/src/audio/webrtc_vad/webrtc_vad-stub.c @@ -0,0 +1,60 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// Dependency-free stub backend for the webrtc_vad module. +// +// This lets the SOF module glue (init/prepare/process/reset/free, the LLEXT +// manifest and topology wiring) be built and exercised in CI without pulling +// in libfvad. The stub always reports SPEECH (decision = 1) so audio flow +// and notifier events can be validated end-to-end. The real libfvad backend +// lives in webrtc_vad-fvad.c and provides the same webrtc_vad_backend symbol. + +#include +#include +#include "webrtc_vad.h" + +LOG_MODULE_DECLARE(webrtc_vad, CONFIG_SOF_LOG_LEVEL); + +static int webrtc_vad_stub_init(struct processing_module *mod) +{ + comp_info(mod->dev, "webrtc_vad stub backend: no libfvad linked"); + return 0; +} + +static int webrtc_vad_stub_configure(struct processing_module *mod, + int sample_rate_hz, int mode) +{ + comp_info(mod->dev, "webrtc_vad stub: rate=%d mode=%d (ignored)", + sample_rate_hz, mode); + return 0; +} + +/* Stub classifier: always reports speech. */ +static int webrtc_vad_stub_classify(struct processing_module *mod, + const int16_t *samples, int num_samples) +{ + (void)mod; (void)samples; (void)num_samples; + return WEBRTC_VAD_SPEECH; +} + +static int webrtc_vad_stub_reset(struct processing_module *mod) +{ + (void)mod; + return 0; +} + +static int webrtc_vad_stub_free(struct processing_module *mod) +{ + (void)mod; + return 0; +} + +const struct webrtc_vad_backend webrtc_vad_backend = { + .name = "stub", + .init = webrtc_vad_stub_init, + .configure = webrtc_vad_stub_configure, + .classify = webrtc_vad_stub_classify, + .reset = webrtc_vad_stub_reset, + .free = webrtc_vad_stub_free, +}; diff --git a/src/audio/webrtc_vad/webrtc_vad.c b/src/audio/webrtc_vad/webrtc_vad.c new file mode 100644 index 000000000000..30b230f9b90a --- /dev/null +++ b/src/audio/webrtc_vad/webrtc_vad.c @@ -0,0 +1,352 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. +// +// WebRTC Voice Activity Detection module — SOF module_interface core. +// +// This translation unit contains the SOF module_interface glue only; the +// actual VAD classification is delegated to the backend selected at build +// time (webrtc_vad-stub.c or webrtc_vad-fvad.c). +// +// The module is a pass-through PCM effect: audio flows from source to sink +// unmodified. On each complete VAD frame (10/20/30 ms at the configured +// rate), the module classifies channel-0 audio and broadcasts the binary +// speech/non-speech decision via NOTIFIER_ID_VAD so that downstream +// consumers (keyword detection gating, host-side VAD) can react. +// +// Because the WebRTC VAD requires a fixed 10 ms frame size which may differ +// from the SOF pipeline period, the module accumulates incoming S16 samples +// in a small ring buffer, running the classifier once per full frame. + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "webrtc_vad.h" + +/* UUID identifies the component. Registered in uuid-registry.txt. */ +SOF_DEFINE_REG_UUID(webrtc_vad); + +/* Logging context. */ +LOG_MODULE_REGISTER(webrtc_vad, CONFIG_SOF_LOG_LEVEL); + +/** + * webrtc_vad_init() - Allocate private data and initialise the backend. + * @mod: Pointer to the processing module. + * + * Return: 0 on success, negative errno on failure. + */ +__cold static int webrtc_vad_init(struct processing_module *mod) +{ + struct module_data *md = &mod->priv; + struct comp_dev *dev = mod->dev; + struct webrtc_vad_comp_data *cd; + int ret; + + assert_can_be_cold(); + comp_info(dev, "webrtc_vad: init"); + + cd = mod_zalloc(mod, sizeof(*cd)); + if (!cd) + return -ENOMEM; + + md->private = cd; + cd->backend = &webrtc_vad_backend; + + comp_info(dev, "webrtc_vad: backend '%s'", cd->backend->name); + + if (cd->backend->init) { + ret = cd->backend->init(mod); + if (ret) { + comp_err(dev, "webrtc_vad: backend init failed %d", ret); + mod_free(mod, cd); + return ret; + } + } + + return 0; +} + +/** + * webrtc_vad_prepare() - Configure the VAD for the negotiated audio format. + * @mod: Pointer to the processing module. + * @sources: Array of input audio sources (exactly 1 expected). + * @num_of_sources: Must be 1. + * @sinks: Array of output sinks (exactly 1 expected). + * @num_of_sinks: Must be 1. + * + * Reads the sample rate and format from the source, configures the backend, + * and calculates the frame size for the configured frame_ms. + * + * Return: 0 on success, negative errno on failure. + */ +__cold static int webrtc_vad_prepare(struct processing_module *mod, + struct sof_source **sources, int num_of_sources, + struct sof_sink **sinks, int num_of_sinks) +{ + struct webrtc_vad_comp_data *cd = module_get_private_data(mod); + struct comp_dev *dev = mod->dev; + int rate, fmt, ret; + int frame_ms = CONFIG_WEBRTC_VAD_FRAME_MS; + + assert_can_be_cold(); + + if (num_of_sources != 1 || num_of_sinks != 1) { + comp_err(dev, "webrtc_vad: need exactly 1 source and 1 sink"); + return -EINVAL; + } + + rate = source_get_rate(sources[0]); + fmt = source_get_frm_fmt(sources[0]); + cd->channels = source_get_channels(sources[0]); + cd->sample_rate = rate; + + /* libfvad accepts 8/16/32/48 kHz. */ + if (rate != 8000 && rate != 16000 && rate != 32000 && rate != 48000) { + comp_err(dev, "webrtc_vad: unsupported rate %d", rate); + return -EINVAL; + } + + switch (fmt) { + case SOF_IPC_FRAME_S16_LE: + cd->sample_bytes = sizeof(int16_t); + break; + case SOF_IPC_FRAME_S32_LE: + cd->sample_bytes = sizeof(int32_t); + break; + default: + comp_err(dev, "webrtc_vad: unsupported frame format %d", fmt); + return -EINVAL; + } + + /* Number of samples per VAD frame (mono). */ + cd->frame_samples = (rate * frame_ms) / 1000; + if (cd->frame_samples > WEBRTC_VAD_MAX_FRAME_SAMPLES) { + comp_err(dev, "webrtc_vad: frame too large (%d samples)", cd->frame_samples); + return -EINVAL; + } + + comp_info(dev, "webrtc_vad: rate=%d ch=%d frame_ms=%d frame_samples=%d", + rate, cd->channels, frame_ms, cd->frame_samples); + + if (cd->backend->configure) { + ret = cd->backend->configure(mod, rate, CONFIG_WEBRTC_VAD_MODE); + if (ret) { + comp_err(dev, "webrtc_vad: backend configure failed %d", ret); + return ret; + } + } + + cd->accum_samples = 0; + cd->last_decision = WEBRTC_VAD_SILENCE; + cd->configured = true; + return 0; +} + +/** + * webrtc_vad_accumulate() - Extract channel-0 samples into the VAD ring buffer. + * @cd: Module private data. + * @raw: Pointer to interleaved raw audio (S16 or S32). + * @n_frames: Number of frames in @raw. + * + * Downmixes to mono by picking channel 0. For S32, shifts right 16 bits to + * produce S16 for libfvad (which always classifies at 16-bit depth). + */ +static void webrtc_vad_accumulate(struct webrtc_vad_comp_data *cd, + const void *raw, int n_frames) +{ + int ch = cd->channels; + int i; + + for (i = 0; i < n_frames && cd->accum_samples < WEBRTC_VAD_MAX_FRAME_SAMPLES; i++) { + int16_t s; + + if (cd->sample_bytes == sizeof(int16_t)) { + const int16_t *p = raw; + + s = p[i * ch]; /* channel 0 */ + } else { + const int32_t *p = raw; + + s = (int16_t)(p[i * ch] >> 16); + } + cd->accumulator[cd->accum_samples++] = s; + } +} + +/** + * webrtc_vad_classify_pending() - Drain the accumulator, classifying full frames. + * @mod: Pointer to the processing module. + * + * For each complete VAD frame accumulated, calls the backend and fires a + * NOTIFIER_ID_VAD event with the binary decision. + */ +static void webrtc_vad_classify_pending(struct processing_module *mod) +{ + struct webrtc_vad_comp_data *cd = module_get_private_data(mod); + struct comp_dev *dev = mod->dev; + int decision, remaining; + + while (cd->accum_samples >= cd->frame_samples) { + decision = cd->backend->classify(mod, cd->accumulator, + cd->frame_samples); + if (decision < 0) { + comp_warn(dev, "webrtc_vad: classify error %d", decision); + decision = cd->last_decision; + } else { + cd->last_decision = decision; + } + + /* Shift consumed samples out of the accumulator. */ + remaining = cd->accum_samples - cd->frame_samples; + if (remaining > 0) + memmove(cd->accumulator, + cd->accumulator + cd->frame_samples, + (size_t)remaining * sizeof(int16_t)); + cd->accum_samples = remaining; + + /* Broadcast VAD decision to registered listeners. */ + notifier_event(dev, NOTIFIER_ID_VAD, + NOTIFIER_TARGET_CORE_LOCAL, &decision, + sizeof(decision)); + } +} + +/** + * webrtc_vad_process() - Pass audio through and classify accumulated frames. + * @mod: Pointer to the processing module. + * @sources: Input sources (1 element). + * @num_of_sources: 1. + * @sinks: Output sinks (1 element). + * @num_of_sinks: 1. + * + * Audio is passed from source to sink byte-for-byte. Simultaneously, channel-0 + * samples are extracted from the source data buffer (before it is released) + * and appended to the accumulator for VAD classification. + * + * Return: 0 on success, negative errno on error. + */ +static int webrtc_vad_process(struct processing_module *mod, + struct sof_source **sources, int num_of_sources, + struct sof_sink **sinks, int num_of_sinks) +{ + struct webrtc_vad_comp_data *cd = module_get_private_data(mod); + struct sof_source *src = sources[0]; + struct sof_sink *snk = sinks[0]; + size_t frame_bytes = source_get_frame_bytes(src); + int avail = (int)source_get_data_frames_available(src); + int free = (int)sink_get_free_frames(snk); + int n = MIN(avail, free); + size_t nbytes; + void const *rd_ptr, *buf_start; + void *wr_ptr, *wr_buf_start; + size_t buf_size; + int ret; + + if (n <= 0) + return 0; + + nbytes = (size_t)n * frame_bytes; + + /* ---- Obtain source data pointer (read-only, zero-copy) ---- */ + ret = source_get_data(src, nbytes, &rd_ptr, &buf_start, &buf_size); + if (ret) + return ret; + + /* ---- Obtain sink write pointer ---- */ + ret = sink_get_buffer(snk, nbytes, &wr_ptr, &wr_buf_start, &buf_size); + if (ret) { + source_release_data(src, 0); /* release without consuming */ + return ret; + } + + /* ---- Pass-through copy ---- */ + memcpy_s(wr_ptr, nbytes, rd_ptr, nbytes); + + /* ---- Accumulate channel-0 samples for VAD (from source read ptr) ---- */ + webrtc_vad_accumulate(cd, rd_ptr, n); + + /* ---- Commit / release ---- */ + source_release_data(src, nbytes); + sink_commit_buffer(snk, nbytes); + + /* ---- Classify any complete frames ---- */ + webrtc_vad_classify_pending(mod); + + return 0; +} + +/** + * webrtc_vad_reset() - Flush accumulator and reset backend state. + * @mod: Pointer to the processing module. + * + * Return: 0 on success. + */ +static int webrtc_vad_reset(struct processing_module *mod) +{ + struct webrtc_vad_comp_data *cd = module_get_private_data(mod); + + comp_dbg(mod->dev, "webrtc_vad: reset"); + cd->accum_samples = 0; + cd->last_decision = WEBRTC_VAD_SILENCE; + + if (cd->backend->reset) + return cd->backend->reset(mod); + + return 0; +} + +/** + * webrtc_vad_free() - Free all resources. + * @mod: Pointer to the processing module. + * + * Return: 0 on success. + */ +__cold static int webrtc_vad_free(struct processing_module *mod) +{ + struct webrtc_vad_comp_data *cd = module_get_private_data(mod); + + assert_can_be_cold(); + comp_dbg(mod->dev, "webrtc_vad: free"); + + if (cd->backend->free) + cd->backend->free(mod); + + mod_free(mod, cd); + return 0; +} + +/* Module operations vtable. */ +static const struct module_interface webrtc_vad_interface = { + .init = webrtc_vad_init, + .prepare = webrtc_vad_prepare, + .process = webrtc_vad_process, + .reset = webrtc_vad_reset, + .free = webrtc_vad_free, +}; + +/* If COMP_WEBRTC_VAD is =m in Kconfig this is built as a loadable LLEXT. */ +#if CONFIG_COMP_WEBRTC_VAD_MODULE + +#include +#include +#include + +static const struct sof_man_module_manifest mod_manifest __section(".module") __used = + SOF_LLEXT_MODULE_MANIFEST("WRTCVAD", &webrtc_vad_interface, 1, + SOF_REG_UUID(webrtc_vad), 40); + +SOF_LLEXT_BUILDINFO; + +#else + +DECLARE_TR_CTX(webrtc_vad_tr, SOF_UUID(webrtc_vad_uuid), LOG_LEVEL_INFO); +DECLARE_MODULE_ADAPTER(webrtc_vad_interface, webrtc_vad_uuid, webrtc_vad_tr); +SOF_MODULE_INIT(webrtc_vad, sys_comp_module_webrtc_vad_interface_init); + +#endif diff --git a/src/audio/webrtc_vad/webrtc_vad.cmake b/src/audio/webrtc_vad/webrtc_vad.cmake new file mode 100644 index 000000000000..0e3177b0a2ee --- /dev/null +++ b/src/audio/webrtc_vad/webrtc_vad.cmake @@ -0,0 +1,90 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Cross-build libfvad static library for the webrtc_vad module. +# +# libfvad is a standalone pure-C extraction of the WebRTC GMM VAD. It has +# no build system of its own beyond a trivial set of C source files, so we +# compile them directly with ExternalProject using the Zephyr cross-toolchain. +# +# Source is pulled via west from github.com/dpirch/libfvad (see west.yml). +# Produces ${LIBFVAD_INSTALL_DIR}/lib/libfvad.a and +# ${LIBFVAD_INSTALL_DIR}/include/fvad.h for the LLEXT to link. + +include(ExternalProject) + +# --- 1. Locate libfvad source (west module) --- +if(NOT DEFINED SOF_LIBFVAD_SRC_DIR) + set(SOF_LIBFVAD_SRC_DIR "${sof_top_dir}/../modules/audio/libfvad" + CACHE PATH "libfvad source tree (west-pinned)") +endif() +cmake_path(NORMAL_PATH SOF_LIBFVAD_SRC_DIR) +if(NOT EXISTS "${SOF_LIBFVAD_SRC_DIR}/src/fvad.c") + message(FATAL_ERROR + "webrtc_vad: libfvad source not found at '${SOF_LIBFVAD_SRC_DIR}'.\n" + "Run 'west update' (libfvad is pinned in west.yml) or pass " + "-DSOF_LIBFVAD_SRC_DIR=.") +endif() + +# --- 2. Derive cross toolchain prefix from Zephyr target compiler --- +# e.g. .../bin/xtensa-intel_ace30_ptl_zephyr-elf-gcc +# -> prefix .../bin/xtensa-intel_ace30_ptl_zephyr-elf- +get_filename_component(_tc_dir "${CMAKE_C_COMPILER}" DIRECTORY) +get_filename_component(_tc_name "${CMAKE_C_COMPILER}" NAME) +string(REGEX REPLACE "gcc$" "" _tc_prefix_name "${_tc_name}") +set(_fvad_cross_prefix "${_tc_dir}/${_tc_prefix_name}") + +# --- 3. Output directories --- +set(LIBFVAD_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/libfvad-install" + CACHE INTERNAL "webrtc_vad: libfvad install prefix") + +# libfvad has exactly two source files (fvad.c and the internal vad/ sources). +# Rather than invoking its CMakeLists (which would need a CMake toolchain file), +# we compile the objects directly and archive them. This mirrors the libshine +# approach in ffmpeg.cmake. +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/fvad-build.sh" +"#!/bin/sh +set -e +export PATH=${_tc_dir}:$ENV{PATH} +SRC=${SOF_LIBFVAD_SRC_DIR} +INST=${LIBFVAD_INSTALL_DIR} +OBJ=${CMAKE_CURRENT_BINARY_DIR}/fvad-obj + +mkdir -p \"$OBJ\" \"$INST/lib\" \"$INST/include\" + +SRCS=\" + $SRC/src/fvad.c + $SRC/src/signal_processing/division_operations.c + $SRC/src/signal_processing/energy.c + $SRC/src/signal_processing/get_scaling_square.c + $SRC/src/signal_processing/resample_48khz.c + $SRC/src/signal_processing/resample_by_2_internal.c + $SRC/src/signal_processing/resample_fractional.c + $SRC/src/signal_processing/spl_inl.c + $SRC/src/vad/vad_core.c + $SRC/src/vad/vad_filterbank.c + $SRC/src/vad/vad_gmm.c + $SRC/src/vad/vad_sp.c +\" + +for f in \$SRCS; do + bn=\$(basename \"\$f\" .c) + ${CMAKE_C_COMPILER} -O2 -fPIC \ + -I$SRC/include \ + -I$SRC/src \ + -c \"\$f\" -o \"$OBJ/\${bn}.o\" +done + +${_fvad_cross_prefix}ar rcs \"$INST/lib/libfvad.a\" \"$OBJ\"/*.o +cp $SRC/include/fvad.h \"$INST/include/fvad.h\" +") + +add_custom_command( + OUTPUT "${LIBFVAD_INSTALL_DIR}/lib/libfvad.a" + "${LIBFVAD_INSTALL_DIR}/include/fvad.h" + COMMAND sh "${CMAKE_CURRENT_BINARY_DIR}/fvad-build.sh" + VERBATIM) + +add_custom_target(fvad_ext + DEPENDS "${LIBFVAD_INSTALL_DIR}/lib/libfvad.a") + +message(STATUS "webrtc_vad: cross-building libfvad from ${SOF_LIBFVAD_SRC_DIR}") diff --git a/src/audio/webrtc_vad/webrtc_vad.h b/src/audio/webrtc_vad/webrtc_vad.h new file mode 100644 index 000000000000..d6fb31ae4791 --- /dev/null +++ b/src/audio/webrtc_vad/webrtc_vad.h @@ -0,0 +1,92 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright(c) 2026 Intel Corporation. + * + * WebRTC Voice Activity Detection (VAD) module for SOF. + * + * Wraps libfvad — a standalone pure-C BSD-3 port of the WebRTC GMM VAD — + * behind the SOF module_interface. The module inspects each incoming PCM + * frame and emits a binary speech/non-speech decision as a SOF notifier + * event, leaving the audio data stream unmodified (pass-through). + * + * The actual VAD work is delegated to a pluggable backend so the SOF glue + * can be validated with a dependency-free stub (webrtc_vad-stub.c) before + * the real libfvad backend (webrtc_vad-fvad.c) is linked in. + */ +#ifndef __SOF_AUDIO_WEBRTC_VAD_H__ +#define __SOF_AUDIO_WEBRTC_VAD_H__ + +#include +#include +#include + +/* Maximum accumulation buffer: 30 ms at 48 kHz, one channel of int16. */ +#define WEBRTC_VAD_MAX_FRAME_SAMPLES (30 * 48) + +/* VAD decision values emitted via notifier. */ +#define WEBRTC_VAD_SILENCE 0 +#define WEBRTC_VAD_SPEECH 1 + +struct webrtc_vad_comp_data; + +/** + * struct webrtc_vad_backend - VAD backend operations. + * + * A backend owns the real VAD state and the frame-level classification. + * All ops return 0 on success or a negative errno; process() additionally + * returns the VAD decision (0 = silence, 1 = speech) via *decision. + */ +struct webrtc_vad_backend { + const char *name; + + /* One-time backend init, called from module init(). */ + int (*init)(struct processing_module *mod); + + /* Configure the VAD for rate/mode, called from prepare(). */ + int (*configure)(struct processing_module *mod, + int sample_rate_hz, int mode); + + /** + * Classify one complete VAD frame (frame_ms worth of int16 samples, + * mono). Returns 1 for speech, 0 for non-speech, negative on error. + */ + int (*classify)(struct processing_module *mod, + const int16_t *samples, int num_samples); + + /* Reset VAD state, keep configuration. Called from reset(). */ + int (*reset)(struct processing_module *mod); + + /* Tear down any state allocated by init()/configure(). */ + int (*free)(struct processing_module *mod); +}; + +/** + * struct webrtc_vad_comp_data - webrtc_vad module private data. + * @backend: Selected VAD backend ops. + * @backend_data: Backend-private state (e.g. FvadState*). + * @sample_rate: Input sample rate (Hz). + * @channels: Input channel count (VAD runs on channel 0 only). + * @frame_samples: Samples per VAD frame (frame_ms * sample_rate / 1000). + * @sample_bytes: Bytes per sample (2 for S16, 4 for S32). + * @accumulator: S16 mono ring buffer collecting sub-frame periods. + * @accum_samples: Number of S16 samples currently in @accumulator. + * @last_decision: Most recent VAD classification (0/1). + * @configured: True once the backend has been opened. + */ +struct webrtc_vad_comp_data { + const struct webrtc_vad_backend *backend; + void *backend_data; + int sample_rate; + int channels; + int frame_samples; + int sample_bytes; + int16_t accumulator[WEBRTC_VAD_MAX_FRAME_SAMPLES]; + int accum_samples; + int last_decision; + bool configured; +}; + +/* Backend instance provided by the selected translation unit. */ +extern const struct webrtc_vad_backend webrtc_vad_backend; + +#endif /* __SOF_AUDIO_WEBRTC_VAD_H__ */ diff --git a/src/audio/webrtc_vad/webrtc_vad.toml b/src/audio/webrtc_vad/webrtc_vad.toml new file mode 100644 index 000000000000..6081d82bd278 --- /dev/null +++ b/src/audio/webrtc_vad/webrtc_vad.toml @@ -0,0 +1,25 @@ +#ifndef LOAD_TYPE +#define LOAD_TYPE "0" +#endif + + REM # WebRTC VAD (libfvad) module config + [[module.entry]] + REM # NOTE: module name is limited to 8 chars (manifest name field); + REM # it must match the SOF_LLEXT_MODULE_MANIFEST() name in webrtc_vad.c. + name = "WRTCVAD" + uuid = UUIDREG_STR_WEBRTC_VAD + affinity_mask = "0x1" + instance_count = "40" + domain_types = "0" + load_type = LOAD_TYPE + module_type = "9" + auto_start = "0" + sched_caps = [1, 0x00008000] + REM # pin = [dir, type, sample rate, size, container, channel-cfg] + REM # Single input pin (source), single output pin (sink). PCM pass-through. + pin = [0, 0, 0xfeef, 0xf, 0xf, 0x45ff, 1, 0, 0xfeef, 0xf, 0xf, 0x45ff] + REM # mod_cfg [PAR_0 PAR_1 PAR_2 PAR_3 IS_BYTES CPS IBS OBS MOD_FLAGS CPC OBLS] + REM # VAD is pure pass-through: IBS == OBS, modest CPU (fixed-point GMM). + mod_cfg = [0, 0, 0, 0, 0, 500000, 8192, 8192, 0, 500, 0] + + index = __COUNTER__ diff --git a/src/drivers/amd/rembrandt/acp_hs_dai.c b/src/drivers/amd/rembrandt/acp_hs_dai.c index bc1579d3f4d8..e7c70507f72d 100644 --- a/src/drivers/amd/rembrandt/acp_hs_dai.c +++ b/src/drivers/amd/rembrandt/acp_hs_dai.c @@ -37,7 +37,7 @@ static inline int hsdai_set_config(struct dai *dai, struct ipc_config_dai *commo acp_i2stdm_mstrclkgen_t i2stdm_mstrclkgen; acpdata->config = *config; - acpdata->params = config->acphs; + acpdata->params = config->acptdm; i2stdm_mstrclkgen.u32all = io_reg_read(PU_REGISTER_BASE + ACP_I2STDM2_MSTRCLKGEN); i2stdm_mstrclkgen.bits.i2stdm_master_mode = 1; switch (config->format & SOF_DAI_FMT_FORMAT_MASK) { diff --git a/src/idc/idc.c b/src/idc/idc.c index 145a22abfa1c..1d1fcd52ec4d 100644 --- a/src/idc/idc.c +++ b/src/idc/idc.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -120,6 +121,8 @@ static int idc_get_attribute(uint32_t comp_id) struct ipc_comp_dev *ipc_dev; struct idc_payload *idc_payload; struct get_attribute_remote_payload *get_attr_payload; + struct ipc4_base_module_cfg local_cfg; + int ret; ipc_dev = ipc_get_comp_by_id(ipc_get(), comp_id); if (!ipc_dev) @@ -128,7 +131,21 @@ static int idc_get_attribute(uint32_t comp_id) idc_payload = idc_payload_get(*idc_get(), cpu_get_id()); get_attr_payload = (struct get_attribute_remote_payload *)idc_payload; - return comp_get_attribute(ipc_dev->cd, get_attr_payload->type, get_attr_payload->value); + /* + * Let the module fill a local (cacheable) copy, then publish it to the + * caller's coherent buffer with aligned 32-bit stores. Passing the + * coherent buffer straight to the module makes it memcpy() a whole + * struct into the uncached alias, which faults (EXCCAUSE 9) on Xtensa. + * Only COMP_ATTR_BASE_CONFIG is supported remotely (enforced by the + * sender), so local_cfg is always the right size. + */ + ret = comp_get_attribute(ipc_dev->cd, get_attr_payload->type, &local_cfg); + if (ret < 0) + return ret; + + ipc4_coherent_copy32(get_attr_payload->value, &local_cfg, sizeof(local_cfg)); + + return ret; } /** diff --git a/src/include/sof/audio/component.h b/src/include/sof/audio/component.h index db8a69e63646..11b8023c16c0 100644 --- a/src/include/sof/audio/component.h +++ b/src/include/sof/audio/component.h @@ -954,6 +954,7 @@ void sys_comp_module_drc_interface_init(void); void sys_comp_module_dts_interface_init(void); void sys_comp_module_eq_fir_interface_init(void); void sys_comp_module_eq_iir_interface_init(void); +void sys_comp_module_ffmpeg_dec_interface_init(void); void sys_comp_module_gain_interface_init(void); void sys_comp_module_google_rtc_audio_processing_interface_init(void); void sys_comp_module_google_ctc_audio_processing_interface_init(void); diff --git a/src/include/sof/audio/component_ext.h b/src/include/sof/audio/component_ext.h index 70324175fea8..c506d98eafa0 100644 --- a/src/include/sof/audio/component_ext.h +++ b/src/include/sof/audio/component_ext.h @@ -190,7 +190,21 @@ static inline int comp_prepare_remote(struct comp_dev *dev) struct idc_msg msg = { IDC_MSG_PREPARE, IDC_MSG_PREPARE_EXT(dev->ipc_config.id), dev->ipc_config.core, }; - return idc_send_msg(&msg, IDC_BLOCKING); + /* + * Non-blocking: a remote component's prepare may be expensive (e.g. a DP + * decoder's one-time codec open building large trig tables -- measured + * ~580 ms for mp3 on a secondary core). Waiting for it here stalls the + * initiator core's IPC/EDF thread for that whole time, and with the + * default CONFIG_IDC_TIMEOUT_US the k_p4wq_wait() would instead time out + * mid-prepare (-EAGAIN) and race the pipeline teardown into a crash. + * + * Ordering is preserved without the wait: each core drains its IDC work + * through a single in-order p4wq worker, so this prepare completes before + * the subsequent cross-core trigger (START) that is queued behind it. For + * a compress stream the START IPC also arrives only after the app's first + * write, giving the remote prepare time to finish off the critical path. + */ + return idc_send_msg(&msg, IDC_NON_BLOCKING); } /** See comp_ops::prepare */ @@ -211,6 +225,29 @@ struct get_attribute_remote_payload { void *value; }; +/* + * Copy to/from a coherent (uncached) buffer using aligned 32-bit word accesses. + * + * A whole-struct memcpy() straight into (or out of) the uncached alias faults + * on Xtensa with EXCCAUSE 9: the compiler lowers the struct copy to wide/HiFi + * accesses (e.g. AE_L32X2 / 64-bit loads) that the uncached window does not + * support. The IDC status word (a single aligned uint32_t) already round-trips + * through this same coherent region without faulting, so restricting the copy + * to volatile 32-bit word transactions is safe. @bytes must be a multiple of 4 + * and both pointers 32-bit aligned (guaranteed here: ipc4_base_module_cfg is + * aligned(4) and rzalloc() returns >=4-byte-aligned memory). + */ +static inline void ipc4_coherent_copy32(void *dst, const void *src, size_t bytes) +{ + volatile uint32_t *d = dst; + const volatile uint32_t *s = src; + size_t words = bytes / sizeof(uint32_t); + size_t i; + + for (i = 0; i < words; i++) + d[i] = s[i]; +} + static inline int comp_ipc4_get_attribute_remote(struct comp_dev *dev, uint32_t type, void *value) { @@ -234,9 +271,12 @@ static inline int comp_ipc4_get_attribute_remote(struct comp_dev *dev, uint32_t ret = idc_send_msg(&msg, IDC_BLOCKING); + /* base_cfg is coherent (uncached) -- read it back with 32-bit word + * accesses, not a struct memcpy that would fault on the uncached alias. + */ if (ret == 0) - memcpy_s(value, sizeof(struct ipc4_base_module_cfg), - base_cfg, sizeof(struct ipc4_base_module_cfg)); + ipc4_coherent_copy32(value, base_cfg, + sizeof(struct ipc4_base_module_cfg)); rfree(base_cfg); return ret; diff --git a/src/include/sof/audio/format.h b/src/include/sof/audio/format.h index a468278531f3..6440419a5ab0 100644 --- a/src/include/sof/audio/format.h +++ b/src/include/sof/audio/format.h @@ -10,7 +10,13 @@ #ifndef __SOF_AUDIO_FORMAT_H__ #define __SOF_AUDIO_FORMAT_H__ -#if defined __XCC__ +/* __XCC_CLANG__ is defined when the Zephyr LLVM toolchain wraps clang with + * -D__XCC__ for compatibility. The HiFi3 intrinsic path in format_hifi3.h + * relies on XCC-specific AE built-ins that clang does not support the same + * way; LLVM selects HiFi3 instructions automatically from the target feature + * set and may emit them in FLIX-encoded form even on targets (e.g. TGL) + * without a FLIX execution unit. Use the generic C path for clang. */ +#if defined __XCC__ && !defined __XCC_CLANG__ #include #if XCHAL_HAVE_HIFI3 == 1 #define __AUDIO_FORMAT_GENERIC__ 0 @@ -21,10 +27,10 @@ #define __AUDIO_FORMAT_HIFI3__ 0 #endif /* !XCHAL_HAVE_HIFI3 */ #else -/* GCC */ +/* GCC or clang (__XCC_CLANG__) */ #define __AUDIO_FORMAT_GENERIC__ 1 #define __AUDIO_FORMAT_HIFI3__ 0 -#endif /* !__XCC__ */ +#endif /* !__XCC__ || __XCC_CLANG__ */ #include #include diff --git a/src/include/sof/audio/format_hifi3.h b/src/include/sof/audio/format_hifi3.h index 762d57ab41ff..2c510b42d4d4 100644 --- a/src/include/sof/audio/format_hifi3.h +++ b/src/include/sof/audio/format_hifi3.h @@ -9,7 +9,13 @@ #define __SOF_AUDIO_FORMAT_HIFI3_H__ #include -#include +#if defined(__has_include) +# if __has_include() +# include +# endif +#else +# include +#endif #include /* Saturation inline functions */ diff --git a/src/include/sof/lib/notifier.h b/src/include/sof/lib/notifier.h index 87ca2cd40265..0ac19a8687b9 100644 --- a/src/include/sof/lib/notifier.h +++ b/src/include/sof/lib/notifier.h @@ -32,6 +32,7 @@ enum notify_id { NOTIFIER_ID_DMA_IRQ, /* struct dma_chan_data * */ NOTIFIER_ID_DAI_TRIGGER, /* struct dai_group * */ NOTIFIER_ID_MIC_PRIVACY_STATE_CHANGE, /* struct mic_privacy_settings * */ + NOTIFIER_ID_VAD, /* int * (WEBRTC_VAD_SPEECH / WEBRTC_VAD_SILENCE) */ NOTIFIER_ID_COUNT }; diff --git a/src/include/sof/lib/uuid.h b/src/include/sof/lib/uuid.h index d45f821613d7..13fa9610b2f2 100644 --- a/src/include/sof/lib/uuid.h +++ b/src/include/sof/lib/uuid.h @@ -87,11 +87,19 @@ struct sof_uuid_entry { */ #define _UUID(uuid_name) (&_##uuid_name) #define _RT_UUID(uuid_name) (&uuid_name) +#ifdef __clang__ +#define _DEF_UUID(entity_name, uuid_name, initializer) \ + const STRUCT_SECTION_ITERABLE(sof_uuid_entry, _##uuid_name) = \ + { .id = initializer, .name = entity_name }; \ + extern const struct sof_uuid uuid_name; \ + const struct sof_uuid uuid_name = initializer +#else #define _DEF_UUID(entity_name, uuid_name, initializer) \ const STRUCT_SECTION_ITERABLE(sof_uuid_entry, _##uuid_name) = \ { .id = initializer, .name = entity_name }; \ extern const struct sof_uuid \ __attribute__((alias("_" #uuid_name))) uuid_name +#endif #else /* XTOS SOF emits two definitions, one into the runtime (which may not diff --git a/src/include/sof/math/fir_hifi2ep.h b/src/include/sof/math/fir_hifi2ep.h index f3625104b3dc..4e78b14ea8c6 100644 --- a/src/include/sof/math/fir_hifi2ep.h +++ b/src/include/sof/math/fir_hifi2ep.h @@ -16,7 +16,13 @@ #include #include #include -#include +#if defined(__has_include) +# if __has_include() +# include +# endif +#else +# include +#endif #include #include diff --git a/src/include/sof/math/fir_hifi3.h b/src/include/sof/math/fir_hifi3.h index abcf46df5f6f..150cf31728f3 100644 --- a/src/include/sof/math/fir_hifi3.h +++ b/src/include/sof/math/fir_hifi3.h @@ -16,7 +16,13 @@ #include #include #include -#include +#if defined(__has_include) +# if __has_include() +# include +# endif +#else +# include +#endif #include struct sof_eq_fir_coef_data; diff --git a/src/include/sof/trace/trace.h b/src/include/sof/trace/trace.h index fc3aa324f847..0d1268c18dea 100644 --- a/src/include/sof/trace/trace.h +++ b/src/include/sof/trace/trace.h @@ -31,7 +31,7 @@ #endif #include -#if CONFIG_ZEPHYR_LOG || CONFIG_LIBRARY || CONFIG_ARCH_POSIX_LIBFUZZER +#if defined(__ZEPHYR__) || CONFIG_ZEPHYR_LOG || CONFIG_LIBRARY || CONFIG_ARCH_POSIX_LIBFUZZER #include #endif #include diff --git a/src/ipc/ipc3/dai.c b/src/ipc/ipc3/dai.c index af06e21955cd..f7826c71db31 100644 --- a/src/ipc/ipc3/dai.c +++ b/src/ipc/ipc3/dai.c @@ -97,9 +97,11 @@ int dai_config_dma_channel(struct dai_data *dd, struct comp_dev *dev, const void case SOF_DAI_AMD_HS_VIRTUAL: case SOF_DAI_AMD_TDM: case SOF_DAI_AMD_SDW: { +#ifdef CONFIG_ZEPHYR_NATIVE_DRIVERS struct dai_config *params = (struct dai_config *)dd->dai->dev->config; params->dai_index = dd->dai->index; +#endif channel = dai_get_handshake(dd->dai, dai->direction, dd->stream_id); #if defined(CONFIG_SOC_ACP_7_0) @@ -361,7 +363,7 @@ void dai_dma_release(struct dai_data *dd, struct comp_dev *dev) return; } -#if defined(CONFIG_AMD) +#if defined(CONFIG_AMD) && !defined(CONFIG_SOC_ACP_6_0) /* Free DAI-specific data allocated in ipc_dai_data_config() */ if (dd->dma && dd->dma->z_dev && dd->dma->z_dev->data) { struct acp_dma_dev_data *dev_data = dd->dma->z_dev->data; diff --git a/src/library_manager/llext_manager.c b/src/library_manager/llext_manager.c index 4c1e4f02d5b5..91fa6b2f4279 100644 --- a/src/library_manager/llext_manager.c +++ b/src/library_manager/llext_manager.c @@ -389,7 +389,11 @@ static int llext_manager_link(const char *name, */ struct llext_load_param ldr_parm = { .relocate_local = !*llext, +#if defined(CONFIG_SOC_ACE30) + .pre_located = false, +#else .pre_located = true, +#endif .section_detached = llext_manager_section_detached, .keep_section_info = true, }; diff --git a/src/library_manager/llext_manager_dram.c b/src/library_manager/llext_manager_dram.c index 2f6cff2b3501..d3401952f443 100644 --- a/src/library_manager/llext_manager_dram.c +++ b/src/library_manager/llext_manager_dram.c @@ -35,6 +35,15 @@ __imrdata static struct lib_manager_dram_storage lib_manager_dram; /* Store LLEXT manager context in DRAM to be restored during the next boot. */ int llext_manager_store_to_dram(void) { + /* + * If IMR context save is disabled, HP-SRAM and L3 heap memory are lost + * on suspend/resume, but IMR remains persistent. Skip saving to avoid + * restoring stale pointers. + */ + if (!IS_ENABLED(CONFIG_ADSP_IMR_CONTEXT_SAVE)) { + return 0; + } + struct ext_library *_ext_lib = ext_lib_get(); unsigned int i, j, k, l, n_lib, n_mod, n_llext, n_sect, n_sym; size_t buf_size; @@ -161,6 +170,15 @@ int llext_manager_store_to_dram(void) int llext_manager_restore_from_dram(void) { + /* + * If IMR context save is disabled, the L3 heap and HP-SRAM memory were lost. + * The saved context will contain stale pointers, so cleanly re-initialize instead. + */ + if (!IS_ENABLED(CONFIG_ADSP_IMR_CONTEXT_SAVE)) { + lib_manager_init(); + return 0; + } + lib_manager_init(); struct ext_library *_ext_lib = ext_lib_get(); diff --git a/src/math/fir_hifi2ep.c b/src/math/fir_hifi2ep.c index 1dd03d7c1485..1e98f295b62f 100644 --- a/src/math/fir_hifi2ep.c +++ b/src/math/fir_hifi2ep.c @@ -12,7 +12,13 @@ #include #include #include -#include +#if defined(__has_include) +# if __has_include() +# include +# endif +#else +# include +#endif #include #include #include diff --git a/src/math/fir_hifi3.c b/src/math/fir_hifi3.c index dd3e0edf8bde..3ace7767fccd 100644 --- a/src/math/fir_hifi3.c +++ b/src/math/fir_hifi3.c @@ -12,7 +12,13 @@ #include #include #include -#include +#if defined(__has_include) +# if __has_include() +# include +# endif +#else +# include +#endif #include #include #include diff --git a/src/math/fir_hifi5.c b/src/math/fir_hifi5.c index d4b699ceabb2..5a784dcdc849 100644 --- a/src/math/fir_hifi5.c +++ b/src/math/fir_hifi5.c @@ -12,7 +12,13 @@ #include #include #include -#include +#if defined(__has_include) +# if __has_include() +# include +# endif +#else +# include +#endif #include #include #include diff --git a/src/platform/ace30/include/platform/lib/memory.h b/src/platform/ace30/include/platform/lib/memory.h index 70888798e22a..27fabb9d0093 100644 --- a/src/platform/ace30/include/platform/lib/memory.h +++ b/src/platform/ace30/include/platform/lib/memory.h @@ -41,8 +41,12 @@ #define SRAM_STREAM_BASE (SRAM_EXCEPT_BASE + SRAM_EXCEPT_SIZE) #define SRAM_STREAM_SIZE 0x1000 -/* Stack configuration */ -#define SOF_STACK_SIZE 0x1000 +/* Stack configuration. + * On ace30 this feeds only the per-core IDC p4wq worker threads (zephyr_idc.c), + * which run cross-core module .init/.set_config synchronously. libavcodec + * (ffmpeg_dec) init needs far more than the historical 4 KiB, so size it up. + */ +#define SOF_STACK_SIZE 0x8000 #define PLATFORM_HEAP_SYSTEM CONFIG_CORE_COUNT /* one per core */ #define PLATFORM_HEAP_SYSTEM_RUNTIME CONFIG_CORE_COUNT /* one per core */ diff --git a/src/platform/amd/acp_7_0/include/arch/xtensa/config/defs.h b/src/platform/amd/acp_7_0/include/arch/xtensa/config/defs.h index bc058b9b906d..91c73e60042a 100644 --- a/src/platform/amd/acp_7_0/include/arch/xtensa/config/defs.h +++ b/src/platform/amd/acp_7_0/include/arch/xtensa/config/defs.h @@ -30,9 +30,11 @@ #ifdef __XTENSA__ +#if defined(__has_include) && __has_include() #include #include #include +#endif #endif /* __XTENSA__ */ #endif /* !_XTENSA_BASE_HEADER */ diff --git a/src/platform/amd/acp_7_x/include/arch/xtensa/config/defs.h b/src/platform/amd/acp_7_x/include/arch/xtensa/config/defs.h index cbcb6b5c39de..87ef561858e8 100644 --- a/src/platform/amd/acp_7_x/include/arch/xtensa/config/defs.h +++ b/src/platform/amd/acp_7_x/include/arch/xtensa/config/defs.h @@ -30,9 +30,11 @@ #ifdef __XTENSA__ +#if defined(__has_include) && __has_include() #include #include #include +#endif #endif /* __XTENSA__ */ #endif /* !_XTENSA_BASE_HEADER */ diff --git a/src/platform/amd/rembrandt/include/arch/xtensa/config/defs.h b/src/platform/amd/rembrandt/include/arch/xtensa/config/defs.h index 0da4586ce0d3..a5db4db5a4e6 100644 --- a/src/platform/amd/rembrandt/include/arch/xtensa/config/defs.h +++ b/src/platform/amd/rembrandt/include/arch/xtensa/config/defs.h @@ -30,9 +30,11 @@ #ifdef __XTENSA__ +#if defined(__has_include) && __has_include() #include #include #include +#endif #endif /* __XTENSA__ */ #endif /* !_XTENSA_BASE_HEADER */ diff --git a/src/platform/imx8m/imx8m.x.in b/src/platform/imx8m/imx8m.x.in index 8c90dd919b02..e74e0b6a204b 100644 --- a/src/platform/imx8m/imx8m.x.in +++ b/src/platform/imx8m/imx8m.x.in @@ -90,13 +90,13 @@ MEMORY sof_sdram1 : org = SDRAM1_BASE, len = SDRAM1_SIZE - static_uuid_entries_seg (!ari) : + static_uuid_entries_seg : org = UUID_ENTRY_ELF_BASE, len = UUID_ENTRY_ELF_SIZE - static_log_entries_seg (!ari) : + static_log_entries_seg : org = LOG_ENTRY_ELF_BASE, len = LOG_ENTRY_ELF_SIZE - fw_metadata_seg (!ari) : + fw_metadata_seg : org = EXT_MANIFEST_ELF_BASE, len = EXT_MANIFEST_ELF_SIZE } diff --git a/src/platform/posix/ipc.c b/src/platform/posix/ipc.c index c8012a697847..8ef28ec654e4 100644 --- a/src/platform/posix/ipc.c +++ b/src/platform/posix/ipc.c @@ -17,6 +17,7 @@ SOF_DEFINE_REG_UUID(ipc_task_posix); static struct ipc *global_ipc; +#ifdef CONFIG_ARCH_POSIX_LIBFUZZER // Not an ISR, called from the native_posix fuzz interrupt. Left // alone for general hygiene. This is how a IPC interrupt would look // if we had one. @@ -169,6 +170,7 @@ static void fuzz_isr(const void *arg) posix_ipc_isr(NULL); } +#endif // This API is... confounded by its history. With IPC3, the job of // this function is to get a newly-received IPC message header (!) @@ -237,12 +239,14 @@ int ipc_platform_compact_read_msg(struct ipc_cmd_hdr *hdr, int words) // Re-raise the interrupt if there's still fuzz data to process void ipc_platform_complete_cmd(struct ipc *ipc) { +#ifdef CONFIG_ARCH_POSIX_LIBFUZZER extern void posix_sw_set_pending_IRQ(unsigned int IRQn); if (fuzz_in_sz > 0) { posix_fuzz_sz = 0; posix_sw_set_pending_IRQ(CONFIG_ZEPHYR_POSIX_FUZZ_IRQ); } +#endif } int ipc_platform_send_msg(const struct ipc_msg *msg) @@ -265,8 +269,10 @@ void ipc_platform_send_msg_direct(const struct ipc_msg *msg) int platform_ipc_init(struct ipc *ipc) { +#ifdef CONFIG_ARCH_POSIX_LIBFUZZER IRQ_CONNECT(CONFIG_ZEPHYR_POSIX_FUZZ_IRQ, 0, fuzz_isr, NULL, 0); irq_enable(CONFIG_ZEPHYR_POSIX_FUZZ_IRQ); +#endif global_ipc = ipc; schedule_task_init_edf(&ipc->ipc_task, SOF_UUID(ipc_task_posix_uuid), diff --git a/src/samples/audio/smart_amp_test_ipc3.c b/src/samples/audio/smart_amp_test_ipc3.c index 5b2abf1ae03b..1172fb3c0716 100644 --- a/src/samples/audio/smart_amp_test_ipc3.c +++ b/src/samples/audio/smart_amp_test_ipc3.c @@ -518,6 +518,16 @@ static int smart_amp_prepare(struct comp_dev *dev) sad->in_channels = audio_stream_get_channels(&sad->source_buf->stream); + /* out_channels bounds the processing loop that indexes the + * PLATFORM_MAX_CHANNELS-sized channel map, so reject a larger count + */ + if (sad->out_channels > PLATFORM_MAX_CHANNELS || + sad->in_channels > PLATFORM_MAX_CHANNELS) { + comp_err(dev, "invalid channel count, in %u out %u", + sad->in_channels, sad->out_channels); + return -EINVAL; + } + if (sad->feedback_buf) { audio_stream_set_channels(&sad->feedback_buf->stream, sad->config.feedback_channels); diff --git a/tools/rimage/config/lnl.toml.h b/tools/rimage/config/lnl.toml.h index faefbb8acadb..ab45dd1b9902 100644 --- a/tools/rimage/config/lnl.toml.h +++ b/tools/rimage/config/lnl.toml.h @@ -101,6 +101,9 @@ #if defined(CONFIG_COMP_FIR) || defined(LLEXT_FORCE_ALL_MODULAR) #include