xref: /isa-l/include/test.h (revision 4f59eeda903272a4daf93cbe5352ba7917b7a85d)
1 /**********************************************************************
2   Copyright(c) 2011-2015 Intel Corporation All rights reserved.
3 
4   Redistribution and use in source and binary forms, with or without
5   modification, are permitted provided that the following conditions
6   are met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above copyright
10       notice, this list of conditions and the following disclaimer in
11       the documentation and/or other materials provided with the
12       distribution.
13     * Neither the name of Intel Corporation nor the names of its
14       contributors may be used to endorse or promote products derived
15       from this software without specific prior written permission.
16 
17   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
29 
30 #ifndef _TEST_H
31 #define _TEST_H
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 // Use sys/time.h functions for time
38 #if defined (__unix__) || (__APPLE__) || (__MINGW32__)
39 # include <sys/time.h>
40 #endif
41 
42 #ifdef _MSC_VER
43 # define inline __inline
44 # include <time.h>
45 # include <Windows.h>
46 #endif
47 
48 #include <stdio.h>
49 #include <stdint.h>
50 
51 struct perf{
52 	struct timeval tv;
53 };
54 
55 
56 #if defined (__unix__) || (__APPLE__) || (__MINGW32__)
57 static inline int perf_start(struct perf *p)
58 {
59 	return gettimeofday(&(p->tv), 0);
60 }
61 static inline int perf_stop(struct perf *p)
62 {
63 	return gettimeofday(&(p->tv), 0);
64 }
65 
66 static inline void perf_print(struct perf stop, struct perf start, long long dsize)
67 {
68 	long long secs = stop.tv.tv_sec - start.tv.tv_sec;
69 	long long usecs = secs * 1000000 + stop.tv.tv_usec - start.tv.tv_usec;
70 
71 	printf("runtime = %10lld usecs", usecs);
72 	if (dsize != 0) {
73 #if 1 // not bug in printf for 32-bit
74 		printf(", bandwidth %lld MB in %.4f sec = %.2f MB/s\n", dsize/(1024*1024),
75 			((double) usecs)/1000000, ((double) dsize) / (double)usecs);
76 #else
77 		printf(", bandwidth %lld MB ", dsize/(1024*1024));
78 		printf("in %.4f sec ",(double)usecs/1000000);
79 		printf("= %.2f MB/s\n", (double)dsize/usecs);
80 #endif
81 	}
82 	else
83 		printf("\n");
84 }
85 #endif
86 
87 static inline uint64_t get_filesize(FILE *fp)
88 {
89 	uint64_t file_size;
90 	fpos_t pos, pos_curr;
91 
92 	fgetpos(fp, &pos_curr);  /* Save current position */
93 #if defined(_WIN32) || defined(_WIN64)
94 	_fseeki64(fp, 0, SEEK_END);
95 #else
96 	fseeko(fp, 0, SEEK_END);
97 #endif
98 	fgetpos(fp, &pos);
99 	file_size = *(uint64_t *)&pos;
100 	fsetpos(fp, &pos_curr);  /* Restore position */
101 
102 	return file_size;
103 }
104 
105 #ifdef __cplusplus
106 }
107 #endif
108 
109 #endif // _TEST_H
110