Eliminate some unreachable cases from the cwrs code and fixup the
[opus.git] / silk / silk_debug.c
1 /***********************************************************************
2 Copyright (c) 2006-2011, Skype Limited. All rights reserved.
3 Redistribution and use in source and binary forms, with or without
4 modification, (subject to the limitations in the disclaimer below)
5 are permitted provided that the following conditions are met:
6 - Redistributions of source code must retain the above copyright notice,
7 this list of conditions and the following disclaimer.
8 - Redistributions in binary form must reproduce the above copyright
9 notice, this list of conditions and the following disclaimer in the
10 documentation and/or other materials provided with the distribution.
11 - Neither the name of Skype Limited, nor the names of specific
12 contributors, may be used to endorse or promote products derived from
13 this software without specific prior written permission.
14 NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED
15 BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
16 CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
17 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
22 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 ***********************************************************************/
27
28 #include "silk_debug.h"
29 #include "silk_SigProc_FIX.h"
30
31 #if SILK_TIC_TOC
32
33 #ifdef _WIN32
34
35 #if (defined(_WIN32) || defined(_WINCE))
36 #include <windows.h>    /* timer */
37 #else   // Linux or Mac
38 #include <sys/time.h>
39 #endif
40
41 unsigned long silk_GetHighResolutionTime(void) /* O: time in usec*/
42 {
43     /* Returns a time counter in microsec   */
44     /* the resolution is platform dependent */
45     /* but is typically 1.62 us resolution  */
46     LARGE_INTEGER lpPerformanceCount;
47     LARGE_INTEGER lpFrequency;
48     QueryPerformanceCounter(&lpPerformanceCount);
49     QueryPerformanceFrequency(&lpFrequency);
50     return (unsigned long)((1000000*(lpPerformanceCount.QuadPart)) / lpFrequency.QuadPart);
51 }
52 #else   // Linux or Mac
53 unsigned long GetHighResolutionTime(void) /* O: time in usec*/
54 {
55     struct timeval tv;
56     gettimeofday(&tv, 0);
57     return((tv.tv_sec*1000000)+(tv.tv_usec));
58 }
59 #endif
60
61 int           silk_Timer_nTimers = 0;
62 int           silk_Timer_depth_ctr = 0;
63 char          silk_Timer_tags[SKP_NUM_TIMERS_MAX][SKP_NUM_TIMERS_MAX_TAG_LEN];
64 #ifdef WIN32
65 LARGE_INTEGER silk_Timer_start[SKP_NUM_TIMERS_MAX];
66 #else
67 unsigned long silk_Timer_start[SKP_NUM_TIMERS_MAX];
68 #endif
69 unsigned int  silk_Timer_cnt[SKP_NUM_TIMERS_MAX];
70 opus_int64     silk_Timer_min[SKP_NUM_TIMERS_MAX];
71 opus_int64     silk_Timer_sum[SKP_NUM_TIMERS_MAX];
72 opus_int64     silk_Timer_max[SKP_NUM_TIMERS_MAX];
73 opus_int64     silk_Timer_depth[SKP_NUM_TIMERS_MAX];
74
75 #ifdef WIN32
76 void silk_TimerSave(char *file_name)
77 {
78     if( silk_Timer_nTimers > 0 )
79     {
80         int k;
81         FILE *fp;
82         LARGE_INTEGER lpFrequency;
83         LARGE_INTEGER lpPerformanceCount1, lpPerformanceCount2;
84         int del = 0x7FFFFFFF;
85         double avg, sum_avg;
86         /* estimate overhead of calling performance counters */
87         for( k = 0; k < 1000; k++ ) {
88             QueryPerformanceCounter(&lpPerformanceCount1);
89             QueryPerformanceCounter(&lpPerformanceCount2);
90             lpPerformanceCount2.QuadPart -= lpPerformanceCount1.QuadPart;
91             if( (int)lpPerformanceCount2.LowPart < del )
92                 del = lpPerformanceCount2.LowPart;
93         }
94         QueryPerformanceFrequency(&lpFrequency);
95         /* print results to file */
96         sum_avg = 0.0f;
97         for( k = 0; k < silk_Timer_nTimers; k++ ) {
98             if (silk_Timer_depth[k] == 0) {
99                 sum_avg += (1e6 * silk_Timer_sum[k] / silk_Timer_cnt[k] - del) / lpFrequency.QuadPart * silk_Timer_cnt[k];
100             }
101         }
102         fp = fopen(file_name, "w");
103         fprintf(fp, "                                min         avg     %%         max      count\n");
104         for( k = 0; k < silk_Timer_nTimers; k++ ) {
105             if (silk_Timer_depth[k] == 0) {
106                 fprintf(fp, "%-28s", silk_Timer_tags[k]);
107             } else if (silk_Timer_depth[k] == 1) {
108                 fprintf(fp, " %-27s", silk_Timer_tags[k]);
109             } else if (silk_Timer_depth[k] == 2) {
110                 fprintf(fp, "  %-26s", silk_Timer_tags[k]);
111             } else if (silk_Timer_depth[k] == 3) {
112                 fprintf(fp, "   %-25s", silk_Timer_tags[k]);
113             } else {
114                 fprintf(fp, "    %-24s", silk_Timer_tags[k]);
115             }
116             avg = (1e6 * silk_Timer_sum[k] / silk_Timer_cnt[k] - del) / lpFrequency.QuadPart;
117             fprintf(fp, "%8.2f", (1e6 * (SKP_max_64(silk_Timer_min[k] - del, 0))) / lpFrequency.QuadPart);
118             fprintf(fp, "%12.2f %6.2f", avg, 100.0 * avg / sum_avg * silk_Timer_cnt[k]);
119             fprintf(fp, "%12.2f", (1e6 * (SKP_max_64(silk_Timer_max[k] - del, 0))) / lpFrequency.QuadPart);
120             fprintf(fp, "%10d\n", silk_Timer_cnt[k]);
121         }
122         fprintf(fp, "                                microseconds\n");
123         fclose(fp);
124     }
125 }
126 #else
127 void silk_TimerSave(char *file_name)
128 {
129     if( silk_Timer_nTimers > 0 )
130     {
131         int k;
132         FILE *fp;
133         /* print results to file */
134         fp = fopen(file_name, "w");
135         fprintf(fp, "                                min         avg         max      count\n");
136         for( k = 0; k < silk_Timer_nTimers; k++ )
137         {
138             if (silk_Timer_depth[k] == 0) {
139                 fprintf(fp, "%-28s", silk_Timer_tags[k]);
140             } else if (silk_Timer_depth[k] == 1) {
141                 fprintf(fp, " %-27s", silk_Timer_tags[k]);
142             } else if (silk_Timer_depth[k] == 2) {
143                 fprintf(fp, "  %-26s", silk_Timer_tags[k]);
144             } else if (silk_Timer_depth[k] == 3) {
145                 fprintf(fp, "   %-25s", silk_Timer_tags[k]);
146             } else {
147                 fprintf(fp, "    %-24s", silk_Timer_tags[k]);
148             }
149             fprintf(fp, "%d ", silk_Timer_min[k]);
150             fprintf(fp, "%f ", (double)silk_Timer_sum[k] / (double)silk_Timer_cnt[k]);
151             fprintf(fp, "%d ", silk_Timer_max[k]);
152             fprintf(fp, "%10d\n", silk_Timer_cnt[k]);
153         }
154         fprintf(fp, "                                microseconds\n");
155         fclose(fp);
156     }
157 }
158 #endif
159
160 #endif /* SILK_TIC_TOC */
161
162 #if SILK_DEBUG
163 FILE *silk_debug_store_fp[ SKP_NUM_STORES_MAX ];
164 int silk_debug_store_count = 0;
165 #endif /* SILK_DEBUG */
166