1 /********************************************************************** 2 Copyright(c) 2011-2016 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 #include "endian_helper.h" 38 39 // Use sys/time.h functions for time 40 #if defined (__unix__) || (__APPLE__) || (__MINGW32__) 41 # include <sys/time.h> 42 #endif 43 44 #ifdef _MSC_VER 45 # define inline __inline 46 # include <time.h> 47 # include <Windows.h> 48 #endif 49 50 #include <stdio.h> 51 #include <stdint.h> 52 53 struct perf{ 54 struct timeval tv; 55 }; 56 57 58 #if defined (__unix__) || (__APPLE__) || (__MINGW32__) 59 static inline int perf_start(struct perf *p) 60 { 61 return gettimeofday(&(p->tv), 0); 62 } 63 static inline int perf_stop(struct perf *p) 64 { 65 return gettimeofday(&(p->tv), 0); 66 } 67 68 static inline void perf_print(struct perf stop, struct perf start, long long dsize) 69 { 70 long long secs = stop.tv.tv_sec - start.tv.tv_sec; 71 long long usecs = secs * 1000000 + stop.tv.tv_usec - start.tv.tv_usec; 72 73 printf("runtime = %10lld usecs", usecs); 74 if (dsize != 0) { 75 #if 1 // not bug in printf for 32-bit 76 printf(", bandwidth %lld MB in %.4f sec = %.2f MB/s\n", dsize/(1024*1024), 77 ((double) usecs)/1000000, ((double) dsize) / (double)usecs); 78 #else 79 printf(", bandwidth %lld MB ", dsize/(1024*1024)); 80 printf("in %.4f sec ",(double)usecs/1000000); 81 printf("= %.2f MB/s\n", (double)dsize/usecs); 82 #endif 83 } 84 else 85 printf("\n"); 86 } 87 #endif 88 89 static inline uint64_t get_filesize(FILE *fp) 90 { 91 uint64_t file_size; 92 fpos_t pos, pos_curr; 93 94 fgetpos(fp, &pos_curr); /* Save current position */ 95 #if defined(_WIN32) || defined(_WIN64) 96 _fseeki64(fp, 0, SEEK_END); 97 #else 98 fseeko(fp, 0, SEEK_END); 99 #endif 100 fgetpos(fp, &pos); 101 file_size = *(uint64_t *)&pos; 102 fsetpos(fp, &pos_curr); /* Restore position */ 103 104 return file_size; 105 } 106 107 #ifdef __cplusplus 108 } 109 #endif 110 111 #endif // _TEST_H 112