Coverage Report

Created: 2025-07-11 07:51

/src/opus/celt/float_cast.h
Line
Count
Source
1
/* Copyright (C) 2001 Erik de Castro Lopo <erikd AT mega-nerd DOT com> */
2
/*
3
   Redistribution and use in source and binary forms, with or without
4
   modification, are permitted provided that the following conditions
5
   are met:
6
7
   - Redistributions of source code must retain the above copyright
8
   notice, this list of conditions and the following disclaimer.
9
10
   - Redistributions in binary form must reproduce the above copyright
11
   notice, this list of conditions and the following disclaimer in the
12
   documentation and/or other materials provided with the distribution.
13
14
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17
   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
18
   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
*/
26
27
/* Version 1.1 */
28
29
#ifndef FLOAT_CAST_H
30
#define FLOAT_CAST_H
31
32
33
#include "arch.h"
34
35
/*============================================================================
36
**      On Intel Pentium processors (especially PIII and probably P4), converting
37
**      from float to int is very slow. To meet the C specs, the code produced by
38
**      most C compilers targeting Pentium needs to change the FPU rounding mode
39
**      before the float to int conversion is performed.
40
**
41
**      Changing the FPU rounding mode causes the FPU pipeline to be flushed. It
42
**      is this flushing of the pipeline which is so slow.
43
**
44
**      Fortunately the ISO C99 specifications define the functions lrint, lrintf,
45
**      llrint and llrintf which fix this problem as a side effect.
46
**
47
**      On Unix-like systems, the configure process should have detected the
48
**      presence of these functions. If they weren't found we have to replace them
49
**      here with a standard C cast.
50
*/
51
52
/*
53
**      The C99 prototypes for lrint and lrintf are as follows:
54
**
55
**              long int lrintf (float x) ;
56
**              long int lrint  (double x) ;
57
*/
58
59
/*      The presence of the required functions are detected during the configure
60
**      process and the values HAVE_LRINT and HAVE_LRINTF are set accordingly in
61
**      the config.h file.
62
*/
63
64
/* With GCC, when SSE is available, the fastest conversion is cvtss2si. */
65
#if defined(__GNUC__) && defined(__SSE__)
66
67
#include <xmmintrin.h>
68
25.2G
static OPUS_INLINE opus_int32 float2int(float x) {return _mm_cvt_ss2si(_mm_set_ss(x));}
Unexecuted instantiation: celt.c:float2int
Unexecuted instantiation: opus_decoder.c:float2int
Unexecuted instantiation: celt_decoder.c:float2int
Unexecuted instantiation: mathops.c:float2int
mapping_matrix.c:float2int
Line
Count
Source
68
11.5M
static OPUS_INLINE opus_int32 float2int(float x) {return _mm_cvt_ss2si(_mm_set_ss(x));}
Unexecuted instantiation: opus_encoder.c:float2int
Unexecuted instantiation: opus_multistream_encoder.c:float2int
analysis.c:float2int
Line
Count
Source
68
383M
static OPUS_INLINE opus_int32 float2int(float x) {return _mm_cvt_ss2si(_mm_set_ss(x));}
Unexecuted instantiation: celt_encoder.c:float2int
Unexecuted instantiation: opus_multistream.c:float2int
enc_API.c:float2int
Line
Count
Source
68
11.8G
static OPUS_INLINE opus_int32 float2int(float x) {return _mm_cvt_ss2si(_mm_set_ss(x));}
Unexecuted instantiation: HP_variable_cutoff.c:float2int
Unexecuted instantiation: init_encoder.c:float2int
control_codec.c:float2int
Line
Count
Source
68
76.1k
static OPUS_INLINE opus_int32 float2int(float x) {return _mm_cvt_ss2si(_mm_set_ss(x));}
Unexecuted instantiation: encode_frame_FLP.c:float2int
Unexecuted instantiation: find_pitch_lags_FLP.c:float2int
Unexecuted instantiation: find_pred_coefs_FLP.c:float2int
Unexecuted instantiation: LPC_analysis_filter_FLP.c:float2int
Unexecuted instantiation: LTP_analysis_filter_FLP.c:float2int
Unexecuted instantiation: LTP_scale_ctrl_FLP.c:float2int
Unexecuted instantiation: noise_shape_analysis_FLP.c:float2int
Unexecuted instantiation: process_gains_FLP.c:float2int
Unexecuted instantiation: residual_energy_FLP.c:float2int
Unexecuted instantiation: warped_autocorrelation_FLP.c:float2int
wrappers_FLP.c:float2int
Line
Count
Source
68
11.8G
static OPUS_INLINE opus_int32 float2int(float x) {return _mm_cvt_ss2si(_mm_set_ss(x));}
Unexecuted instantiation: autocorrelation_FLP.c:float2int
Unexecuted instantiation: bwexpander_FLP.c:float2int
Unexecuted instantiation: energy_FLP.c:float2int
Unexecuted instantiation: k2a_FLP.c:float2int
pitch_analysis_core_FLP.c:float2int
Line
Count
Source
68
1.06G
static OPUS_INLINE opus_int32 float2int(float x) {return _mm_cvt_ss2si(_mm_set_ss(x));}
Unexecuted instantiation: scale_copy_vector_FLP.c:float2int
Unexecuted instantiation: schur_FLP.c:float2int
Unexecuted instantiation: sort_FLP.c:float2int
Unexecuted instantiation: x86_silk_map.c:float2int
Unexecuted instantiation: apply_sine_window_FLP.c:float2int
Unexecuted instantiation: find_LPC_FLP.c:float2int
Unexecuted instantiation: find_LTP_FLP.c:float2int
Unexecuted instantiation: burg_modified_FLP.c:float2int
Unexecuted instantiation: inner_product_FLP.c:float2int
Unexecuted instantiation: scale_vector_FLP.c:float2int
Unexecuted instantiation: inner_product_FLP_avx2.c:float2int
Unexecuted instantiation: corrMatrix_FLP.c:float2int
opus_multistream_decoder.c:float2int
Line
Count
Source
68
47.1M
static OPUS_INLINE opus_int32 float2int(float x) {return _mm_cvt_ss2si(_mm_set_ss(x));}
69
70
#elif (defined(_MSC_VER) && _MSC_VER >= 1400) && (defined(_M_X64) || (defined(_M_IX86_FP) && _M_IX86_FP >= 1))
71
72
        #include <xmmintrin.h>
73
        static OPUS_INLINE opus_int32 float2int(float value)
74
        {
75
                /* _mm_load_ss will generate same code as _mm_set_ss
76
                ** in _MSC_VER >= 1914 /02 so keep __mm_load__ss
77
                ** for backward compatibility.
78
                */
79
                return _mm_cvtss_si32(_mm_load_ss(&value));
80
        }
81
82
#elif (defined(_MSC_VER) && _MSC_VER >= 1400) && defined (_M_IX86)
83
84
        #include <math.h>
85
86
        /*      Win32 doesn't seem to have these functions.
87
        **      Therefore implement OPUS_INLINE versions of these functions here.
88
        */
89
90
        static OPUS_INLINE opus_int32
91
        float2int (float flt)
92
        {       int intgr;
93
94
                _asm
95
                {       fld flt
96
                        fistp intgr
97
                } ;
98
99
                return intgr ;
100
        }
101
#elif defined(__aarch64__)
102
103
        #include <arm_neon.h>
104
        static OPUS_INLINE opus_int32 float2int(float flt)
105
        {
106
                return vcvtns_s32_f32(flt);
107
        }
108
109
#elif defined(HAVE_LRINTF) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
110
111
/*      These defines enable functionality introduced with the 1999 ISO C
112
**      standard. They must be defined before the inclusion of math.h to
113
**      engage them. If optimisation is enabled, these functions will be
114
**      inlined. With optimisation switched off, you have to link in the
115
**      maths library using -lm.
116
*/
117
118
#define _ISOC9X_SOURCE 1
119
#define _ISOC99_SOURCE 1
120
121
#define __USE_ISOC9X 1
122
#define __USE_ISOC99 1
123
124
#include <math.h>
125
#define float2int(x) lrintf(x)
126
127
#elif defined(HAVE_LRINT) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
128
129
#define _ISOC9X_SOURCE 1
130
#define _ISOC99_SOURCE 1
131
132
#define __USE_ISOC9X 1
133
#define __USE_ISOC99 1
134
135
#include <math.h>
136
#define float2int(x) lrint(x)
137
138
#else
139
140
#if (defined(__GNUC__) && defined(__STDC__) && __STDC__ && __STDC_VERSION__ >= 199901L)
141
        /* supported by gcc in C99 mode, but not by all other compilers */
142
        #warning "Don't have the functions lrint() and lrintf ()."
143
        #warning "Replacing these functions with a standard C cast."
144
#endif /* __STDC_VERSION__ >= 199901L */
145
        #include <math.h>
146
        #define float2int(flt) ((int)(floor(.5+flt)))
147
#endif
148
149
#ifndef DISABLE_FLOAT_API
150
static OPUS_INLINE opus_int16 FLOAT2INT16(float x)
151
11.8G
{
152
11.8G
   x = x*CELT_SIG_SCALE;
153
11.8G
   x = MAX32(x, -32768);
154
11.8G
   x = MIN32(x, 32767);
155
11.8G
   return (opus_int16)float2int(x);
156
11.8G
}
Unexecuted instantiation: celt.c:FLOAT2INT16
Unexecuted instantiation: opus_decoder.c:FLOAT2INT16
Unexecuted instantiation: celt_decoder.c:FLOAT2INT16
Unexecuted instantiation: mathops.c:FLOAT2INT16
Unexecuted instantiation: opus_encoder.c:FLOAT2INT16
Unexecuted instantiation: opus_multistream_encoder.c:FLOAT2INT16
Unexecuted instantiation: analysis.c:FLOAT2INT16
Unexecuted instantiation: celt_encoder.c:FLOAT2INT16
Unexecuted instantiation: opus_multistream.c:FLOAT2INT16
enc_API.c:FLOAT2INT16
Line
Count
Source
151
11.8G
{
152
11.8G
   x = x*CELT_SIG_SCALE;
153
11.8G
   x = MAX32(x, -32768);
154
11.8G
   x = MIN32(x, 32767);
155
11.8G
   return (opus_int16)float2int(x);
156
11.8G
}
Unexecuted instantiation: HP_variable_cutoff.c:FLOAT2INT16
Unexecuted instantiation: init_encoder.c:FLOAT2INT16
Unexecuted instantiation: control_codec.c:FLOAT2INT16
Unexecuted instantiation: encode_frame_FLP.c:FLOAT2INT16
Unexecuted instantiation: find_pitch_lags_FLP.c:FLOAT2INT16
Unexecuted instantiation: find_pred_coefs_FLP.c:FLOAT2INT16
Unexecuted instantiation: LPC_analysis_filter_FLP.c:FLOAT2INT16
Unexecuted instantiation: LTP_analysis_filter_FLP.c:FLOAT2INT16
Unexecuted instantiation: LTP_scale_ctrl_FLP.c:FLOAT2INT16
Unexecuted instantiation: noise_shape_analysis_FLP.c:FLOAT2INT16
Unexecuted instantiation: process_gains_FLP.c:FLOAT2INT16
Unexecuted instantiation: residual_energy_FLP.c:FLOAT2INT16
Unexecuted instantiation: warped_autocorrelation_FLP.c:FLOAT2INT16
Unexecuted instantiation: wrappers_FLP.c:FLOAT2INT16
Unexecuted instantiation: autocorrelation_FLP.c:FLOAT2INT16
Unexecuted instantiation: bwexpander_FLP.c:FLOAT2INT16
Unexecuted instantiation: energy_FLP.c:FLOAT2INT16
Unexecuted instantiation: k2a_FLP.c:FLOAT2INT16
Unexecuted instantiation: pitch_analysis_core_FLP.c:FLOAT2INT16
Unexecuted instantiation: scale_copy_vector_FLP.c:FLOAT2INT16
Unexecuted instantiation: schur_FLP.c:FLOAT2INT16
Unexecuted instantiation: sort_FLP.c:FLOAT2INT16
Unexecuted instantiation: x86_silk_map.c:FLOAT2INT16
Unexecuted instantiation: apply_sine_window_FLP.c:FLOAT2INT16
Unexecuted instantiation: find_LPC_FLP.c:FLOAT2INT16
Unexecuted instantiation: find_LTP_FLP.c:FLOAT2INT16
Unexecuted instantiation: burg_modified_FLP.c:FLOAT2INT16
Unexecuted instantiation: inner_product_FLP.c:FLOAT2INT16
Unexecuted instantiation: scale_vector_FLP.c:FLOAT2INT16
Unexecuted instantiation: inner_product_FLP_avx2.c:FLOAT2INT16
Unexecuted instantiation: corrMatrix_FLP.c:FLOAT2INT16
mapping_matrix.c:FLOAT2INT16
Line
Count
Source
151
11.5M
{
152
11.5M
   x = x*CELT_SIG_SCALE;
153
11.5M
   x = MAX32(x, -32768);
154
11.5M
   x = MIN32(x, 32767);
155
11.5M
   return (opus_int16)float2int(x);
156
11.5M
}
opus_multistream_decoder.c:FLOAT2INT16
Line
Count
Source
151
47.1M
{
152
47.1M
   x = x*CELT_SIG_SCALE;
153
47.1M
   x = MAX32(x, -32768);
154
47.1M
   x = MIN32(x, 32767);
155
47.1M
   return (opus_int16)float2int(x);
156
47.1M
}
Unexecuted instantiation: opus_encoder.c:FLOAT2INT16
Unexecuted instantiation: opus_multistream_encoder.c:FLOAT2INT16
157
#endif /* DISABLE_FLOAT_API */
158
159
#endif /* FLOAT_CAST_H */