Commit f517c3e0 authored by multrus's avatar multrus
Browse files

add mechanism to enable a trap on underflows - tested only on gcc so far

parent 91d6421f
Loading
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -56,6 +56,9 @@
#include "debug.h"
#endif
#include "wmc_auto.h"
#ifdef DENORMAL_TRAP
#include "flp_debug.h"
#endif


#define WMC_TOOL_SKIP
@@ -229,6 +232,9 @@ int main(
    reset_wmops();
    reset_mem( USE_BYTES );
#endif
#ifdef DENORMAL_TRAP
    enable_denorm_trap();
#endif

    hHrtfBinary.hHrtfTD = NULL;         /* just to avoid compilation warning */
    hHrtfBinary.hHrtfStatistics = NULL; /* just to avoid compilation warning */
+6 −1
Original line number Diff line number Diff line
@@ -15,7 +15,6 @@
   the software. This notice grants no license of any kind, including but not limited to patent
   license, nor is any license granted by implication, estoppel or otherwise.

   Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
   Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
   contributions.

@@ -43,6 +42,9 @@
#include "debug.h"
#endif
#include "wmc_auto.h"
#ifdef DENORMAL_TRAP
#include "flp_debug.h"
#endif


#define WMC_TOOL_SKIP
@@ -206,6 +208,9 @@ int main(
    reset_wmops();
    reset_mem( USE_BYTES );
#endif
#ifdef DENORMAL_TRAP
    enable_denorm_trap();
#endif

    /*------------------------------------------------------------------------------------------*
     * Parse command-line arguments
+6 −0
Original line number Diff line number Diff line
@@ -53,6 +53,9 @@
#include "debug.h"
#endif
#include "wmc_auto.h"
#ifdef DENORMAL_TRAP
#include "flp_debug.h"
#endif


#define WMC_TOOL_SKIP
@@ -723,6 +726,9 @@ int main(
    reset_wmops();
    reset_mem( USE_BYTES );
#endif
#ifdef DENORMAL_TRAP
    enable_denorm_trap();
#endif

    for ( i = 0; i < RENDERER_MAX_MASA_INPUTS; ++i )
    {
+1 −0
Original line number Diff line number Diff line
@@ -55,6 +55,7 @@
/*#define WMOPS_DETAIL*/                        /* Output detailed complexity printout for every function. Increases runtime overhead */
/*#define WMOPS_WC_FRAME_ANALYSIS*/             /* Output detailed complexity analysis for the worst-case frame */
/*#define MEM_COUNT_DETAILS*/                   /* Output detailed memory analysis for the worst-case frame (writes to the file "mem_analysis.csv") */
/*#define DENORMAL_TRAP*/                       /* Enable trap for denormals */

#ifdef DEBUGGING
/*#define DBG_BITSTREAM_ANALYSIS*/              /* Write bitstream with annotations to a text file */

lib_debug/flp_debug.h

0 → 100755
+102 −0
Original line number Diff line number Diff line
/******************************************************************************************************

   (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
   Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
   Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
   Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
   contributors to this repository. All Rights Reserved.

   This software is protected by copyright law and by international treaties.
   The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
   Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
   Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
   Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
   contributors to this repository retain full ownership rights in their respective contributions in
   the software. This notice grants no license of any kind, including but not limited to patent
   license, nor is any license granted by implication, estoppel or otherwise.

   Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
   contributions.

   This software is provided "AS IS", without any express or implied warranties. The software is in the
   development stage. It is intended exclusively for experts who have experience with such software and
   solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
   and fitness for a particular purpose are hereby disclaimed and excluded.

   Any dispute, controversy or claim arising under or in relation to providing this software shall be
   submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
   accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
   the United Nations Convention on Contracts on the International Sales of Goods.

*******************************************************************************************************/

#include <stdio.h>

#if defined(_MSC_VER)
    // MSVC, x87
    #include <float.h>
#elif defined(__GNUC__) && (defined(__SSE__)||defined(__SSE2__)||defined(__AVX__))
    // GCC/Clang, x86 SSE/AVX
    #include <xmmintrin.h>
#endif

/* 
   detect underflow execption, which results in a denormal; 
   this will not detect each and every denormal - otherwise, 
   all FLP values would have to be tested for denormals using 
   e.g. fpclassify()/fpstatus or bitmasks
*/


static inline void enable_denorm_trap(void) {
#if defined(_MSC_VER)
    // MSVC, x87
    unsigned int cw = _controlfp(0,0);
    cw &= ~_EM_UNDERFLOW;
    _controlfpEM_DENORMAL(cw, _MCW_EM);

#elif defined(__GNUC__) && (defined(__SSE__)||defined(__SSE2__)||defined(__AVX__))
    // GCC/Clang, x86 SSE/AVX
    unsigned int mx = _mm_getcsr();
    mx &= ~_MM_MASK_UNDERFLOW;  // unmaks underflows
    _mm_setcsr(mx);

#elif defined(__aarch64__)
    // AArch64 (Apple Silicon)
    uint64_t fpcr;
    __asm__ volatile("mrs %0, fpcr" : "=r"(fpcr));
    // disable sits 24(FZ) & 25(DN) --> allow denormals to happen
    fpcr &= ~((1ull<<24)|(1ull<<25));
    // set bit 3 (UFE) to unmask underflow exceptions
    fpcr |=  (1ull<<3);
    __asm__ volatile("msr fpcr, %0" :: "r"(fpcr));

#else
    fprintf(stderr, "enable_denorm_trap() not supported on platform!\n");
#endif
}

static inline void disable_denorm_trap(void) {
#if defined(_MSC_VER)
    unsigned int cw = _controlfp(0,0);
    cw |= _EM_UNDERFLOW;
    _controlfp(cw, _MCW_EM);

#elif defined(__GNUC__) && (defined(__SSE__)||defined(__SSE2__)||defined(__AVX__))
    unsigned int mx = _mm_getcsr();
    mx |= _MM_MASK_UNDERFLOW;       // mask underflows
    _mm_setcsr(mx);

#elif defined(__aarch64__)
    // AArch64 (Apple Silicon)
    uint64_t fpcr;
    __asm__ volatile("mrs %0, fpcr" : "=r"(fpcr));
    // delete bit 3 (UFE), set bits 24/25 (FZ/DN) again
    fpcr &= ~(1ull<<3);
    fpcr |=  (1ull<<24)|(1ull<<25);
    __asm__ volatile("msr fpcr, %0" :: "r"(fpcr));

#else
    fprintf(stderr, "disable_denorm_trap() not supported on platform!\n");
#endif
}