dotimings.c
This file
Path
stb_image_resize_test/dotimings.c
224 lines · 6.1 KiB · C
Last changed by
Merge branch 'master' into workingSean Barrettbefore adoption6e9f34d
History of this fileTake it
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>45#ifdef _MSC_VER67#define stop() __debugbreak()8#include <windows.h>9#define int64 __int6410#pragma warning(disable:4127)1112#define get_milliseconds GetTickCount1314#else1516#define stop() __builtin_trap()17#define int64 long long1819typedef unsigned int U32;20typedef unsigned long long U64;2122#include <time.h>23static int get_milliseconds()24{25 struct timespec ts;26 clock_gettime( CLOCK_MONOTONIC, &ts );27 return (U32) ( ( ((U64)(U32)ts.tv_sec) * 1000LL ) + (U64)(((U32)ts.tv_nsec+500000)/1000000) );28}2930#endif3132#if defined(TIME_SIMD)33 // default for most platforms34#elif defined(TIME_SCALAR)35 #define STBIR_NO_SIMD36#else37 #error You must define TIME_SIMD or TIME_SCALAR when compiling this file.38#endif3940#define STBIR_PROFILE41#define STB_IMAGE_RESIZE_IMPLEMENTATION42#define STBIR__V_FIRST_INFO_BUFFER v_info43#include "stb_image_resize2.h" // new one!4445#if defined(TIME_SIMD) && !defined(STBIR_SIMD)46#error Timing SIMD, but scalar was ON!47#endif4849#if defined(TIME_SCALAR) && defined(STBIR_SIMD)50#error Timing scalar, but SIMD was ON!51#endif5253#define HEADER 32545556static int file_write( const char *filename, void * buffer, size_t size ) 57{58 FILE * f = fopen( filename, "wb" );59 if ( f == 0 ) return 0;60 if ( fwrite( buffer, 1, size, f) != size ) return 0;61 fclose(f);62 return 1;63}6465int64 nresize( void * o, int ox, int oy, int op, void * i, int ix, int iy, int ip, int buf, int type, int edg, int flt )66{67 STBIR_RESIZE resize;68 int t;69 int64 b;7071 stbir_resize_init( &resize, i, ix, iy, ip, o, ox, oy, op, buf, type );72 stbir_set_edgemodes( &resize, edg, edg );73 stbir_set_filters( &resize, flt, flt );74 75 stbir_build_samplers_with_splits( &resize, 1 );7677 b = 0x7fffffffffffffffULL;78 for( t = 0 ; t < 16 ; t++ )79 {80 STBIR_PROFILE_INFO profile;81 int64 v;82 if(!stbir_resize_extended( &resize ) )83 stop();84 stbir_resize_extended_profile_info( &profile, &resize );85 v = profile.clocks[1]+profile.clocks[2];86 if ( v < b )87 {88 b = v;89 t = 0;90 }91 }9293 stbir_free_samplers( &resize );9495 return b;96}979899#define INSIZES 5100#define TYPESCOUNT 5101#define NUM 64102103static const int sizes[INSIZES]={63,126,252,520,772};104static const int types[TYPESCOUNT]={STBIR_1CHANNEL,STBIR_2CHANNEL,STBIR_RGB,STBIR_4CHANNEL,STBIR_RGBA};105static const int effective[TYPESCOUNT]={1,2,3,4,7};106107int main( int argc, char ** argv )108{109 unsigned char * input;110 unsigned char * output;111 int dimensionx, dimensiony;112 int scalex, scaley;113 int totalms;114 int timing_count;115 int ir;116 int * file;117 int * ts;118 int64 totalcycles;119120 if ( argc != 6 )121 {122 printf("command: dotimings x_samps y_samps x_scale y_scale outfilename\n");123 exit(1);124 }125126 input = malloc( 4*1200*1200 );127 memset( input, 0x80, 4*1200*1200 );128 output = malloc( 4*10000*10000ULL );129 130 dimensionx = atoi( argv[1] );131 dimensiony = atoi( argv[2] );132 scalex = atoi( argv[3] );133 scaley = atoi( argv[4] );134135 timing_count = dimensionx * dimensiony * INSIZES * TYPESCOUNT;136137 file = malloc( sizeof(int) * ( 2 * timing_count + HEADER ) );138 ts = file + HEADER;139140 totalms = get_milliseconds(); 141 totalcycles = STBIR_PROFILE_FUNC();142 for( ir = 0 ; ir < INSIZES ; ir++ )143 {144 int ix, iy, ty;145 ix = iy = sizes[ir];146147 for( ty = 0 ; ty < TYPESCOUNT ; ty++ )148 {149 int h, hh;150151 h = 1;152 for( hh = 0 ; hh < dimensiony; hh++ )153 {154 int ww, w = 1;155 for( ww = 0 ; ww < dimensionx; ww++ )156 {157 int64 VF, HF;158 int good;159 160 v_info.control_v_first = 2; // vertical first161 VF = nresize( output, w, h, (w*4*1)&~3, input, ix, iy, ix*4*1, types[ty], STBIR_TYPE_UINT8, STBIR_EDGE_CLAMP, STBIR_FILTER_MITCHELL );162 v_info.control_v_first = 1; // horizonal first163 HF = nresize( output, w, h, (w*4*1)&~3, input, ix, iy, ix*4*1, types[ty], STBIR_TYPE_UINT8, STBIR_EDGE_CLAMP, STBIR_FILTER_MITCHELL );164165 good = ( ((HF<=VF) && (!v_info.v_first)) || ((VF<=HF) && (v_info.v_first)));166167// printf("\r%d,%d, %d,%d, %d, %I64d,%I64d, // Good: %c(%c-%d) CompEst: %.1f %.1f\n", ix, iy, w, h, ty, VF, HF, good?'y':'n', v_info.v_first?'v':'h', v_info.v_resize_classification, v_info.v_cost,v_info.h_cost );168 ts[0] = (int)VF;169 ts[1] = (int)HF;170171 ts += 2;172173 w += scalex;174 }175 printf(".");176 h += scaley; 177 }178 }179 }180 totalms = get_milliseconds() - totalms; 181 totalcycles = STBIR_PROFILE_FUNC() - totalcycles;182183 printf("\n");184185 file[0] = 'VFT1';186187 #if defined(_x86_64) || defined( __x86_64__ ) || defined( _M_X64 ) || defined(__x86_64) || defined(__SSE2__) || defined( _M_IX86_FP ) || defined(__i386) || defined( __i386__ ) || defined( _M_IX86 ) || defined( _X86_ )188 file[1] = 1; // x64189 #elif defined( _M_AMD64 ) || defined( __aarch64__ ) || defined( __arm64__ ) || defined(__ARM_NEON__) || defined(__ARM_NEON) || defined(__arm__) || defined( _M_ARM )190 file[1] = 2; // arm191 #else192 file[1] = 99; // who knows???193 #endif194 195 #ifdef STBIR_SIMD8196 file[2] = 2; // simd-8197 #elif defined( STBIR_SIMD )198 file[2] = 1; // simd-4199 #else200 file[2] = 0; // nosimd201 #endif202 203 file[3] = dimensionx; // dimx204 file[4] = dimensiony; // dimy205 file[5] = TYPESCOUNT; // channel types206 file[ 6] = types[0]; file[7] = types[1]; file[8] = types[2]; file[9] = types[3]; file[10] = types[4]; // buffer_type 207 file[11] = effective[0]; file[12] = effective[1]; file[13] = effective[2]; file[14] = effective[3]; file[15] = effective[4]; // effective channels 208 file[16] = INSIZES; // resizes209 file[17] = sizes[0]; file[18] = sizes[0]; // input sizes (w x h)210 file[19] = sizes[1]; file[20] = sizes[1];211 file[21] = sizes[2]; file[22] = sizes[2];212 file[23] = sizes[3]; file[24] = sizes[3];213 file[25] = sizes[4]; file[26] = sizes[4];214 file[27] = scalex; file[28] = scaley; // scale the dimx and dimy amount ( for(i=0;i<dimx) outputx = 1 + i*scalex; )215 file[29] = totalms;216 ((int64*)(file+30))[0] = totalcycles;217218 if ( !file_write( argv[5], file, sizeof(int) * ( 2 * timing_count + HEADER ) ) )219 printf( "Error writing file: %s\n", argv[5] );220 else221 printf( "Successfully wrote timing file: %s\n", argv[5] );222223 return 0;224}