⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gbench.c

📁 Demo for Free type 2.2.1
💻 C
📖 第 1 页 / 共 2 页
字号:
            chits++;

          g = grades + ALGO_GRADE_INDEX(a)*3;

          dst[0] = g[0];
          dst[1] = g[1];
          dst[2] = g[2];
        }

        src += 1;
        dst += 3;
      }
      while (--w > 0);

      src_line += blitter->src_incr;
      dst_line += blitter->dst_incr;
    }
    while (--h > 0);
  }



  int
  gblitter_init_rgb24( GBlitter   blitter,
                       GBitmap    src,
                       int        dst_x,
                       int        dst_y,
                       int        dst_width,
                       int        dst_height,
                       void*      dst_buffer,
                       int        dst_pitch )
  {
    int  width  = src->width;
    int  height = src->height;
    int  delta;
    int  src_x  = 0;
    int  src_y  = 0;

    if ( dst_x < 0 )
    {
      width += dst_x;
      src_x  = -dst_x;
      dst_x  = 0;
    }

    delta = dst_x + width - dst_width;
    if ( delta > 0 )
      width -= delta;

    if ( dst_y < 0 )
    {
      height += dst_y;
      src_y   = -dst_y;
      dst_y   = 0;
    }

    delta = dst_y + height - dst_height;
    if ( delta > 0 )
      height -= delta;

    if ( width <= 0 || height <= 0 )
    {
      blitter->width  = 0;
      blitter->height = 0;
      blitter->blit   = NULL;

      return 1;
    }

    blitter->width    = width;
    blitter->height   = height;

    blitter->src_x    = src_x;
    blitter->src_line = src->buffer + src_y*src->pitch;
    blitter->src_incr = src->pitch;

    blitter->dst_x    = dst_x;
    blitter->dst_line = (unsigned char*)dst_buffer + dst_y*dst_pitch;
    blitter->dst_incr = dst_pitch;

    return 0;
  }



#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

#ifdef UNIX
#include <sys/time.h>
#endif

typedef int  (*bench_t)( int  arg );

#define BENCH_TIME 3.0f

double
get_time(void)
{
#ifdef UNIX
  struct timeval tv;

  gettimeofday(&tv, NULL);
  return (double)tv.tv_sec + (double)tv.tv_usec / 1E6;
#else
  /* clock() has an awful precision (~10ms) under Linux 2.4 + glibc 2.2 */
  return (double)clock() / (double)CLOCKS_PER_SEC;
#endif
}


double  bench_time = BENCH_TIME;

static void
bench( bench_t      bench_func,
       int          bench_arg,
       const char*  title,
       int          max)
{
  int      i, n, done;
  double   t0, delta;

  printf("%-30s : ", title);
  fflush(stdout);

  n = 0;
  done = 0;
  t0 = get_time();
  do
  {
    if (!(*bench_func)( bench_arg ) )
      done++;
    n++;
    delta = get_time() - t0;
  }
  while ((!max || n < max) && delta < bench_time);

  printf("%5.3f us/op\n", delta * 1E6 / (double)done);
}



/* this is un-hinted "W" in Times New Roman
 * this glyph is very fuzzy
 */
static const unsigned char  glyph_data[18*14] =
{
   0x4a, 0x91, 0x94, 0x93, 0x5a, 0x00, 0x4c, 0x92, 0x94, 0x94, 0x67, 0x0b, 0x00,
 0x00, 0x61, 0x92, 0x93, 0x50,
   0x00, 0x65, 0xff, 0xbb, 0x00, 0x00, 0x00, 0x65, 0xff, 0xd5, 0x00, 0x00, 0x00,
 0x00, 0x00, 0xa4, 0x7f, 0x00,
   0x00, 0x08, 0xf0, 0xe6, 0x01, 0x00, 0x00, 0x02, 0xe4, 0xf1, 0x05, 0x00, 0x00,
 0x00, 0x00, 0xc0, 0x10, 0x00,
   0x00, 0x00, 0x9c, 0xff, 0x3f, 0x00, 0x00, 0x00, 0x8e, 0xff, 0x4b, 0x00, 0x00,
 0x00, 0x1d, 0xae, 0x00, 0x00,
   0x00, 0x00, 0x41, 0xff, 0x99, 0x00, 0x00, 0x00, 0x82, 0xff, 0xa6, 0x00, 0x00,
 0x00, 0x74, 0x57, 0x00, 0x00,
   0x00, 0x00, 0x01, 0xe3, 0xed, 0x05, 0x00, 0x00, 0xcd, 0xd9, 0xf4, 0x0b, 0x00,
 0x00, 0xc1, 0x0a, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x89, 0xff, 0x4d, 0x00, 0x33, 0xa1, 0x7a, 0xff, 0x5b, 0x00,
 0x25, 0xa6, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x2e, 0xff, 0xa7, 0x00, 0x8c, 0x46, 0x20, 0xfe, 0xb6, 0x00,
 0x7d, 0x4e, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0xd2, 0xf5, 0x0d, 0xca, 0x03, 0x00, 0xc4, 0xfb, 0x15,
 0xc4, 0x06, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x77, 0xff, 0x98, 0x90, 0x00, 0x00, 0x69, 0xff, 0x98,
 0x9d, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x1d, 0xfe, 0xff, 0x35, 0x00, 0x00, 0x13, 0xfa, 0xff,
 0x45, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0xbf, 0xd9, 0x00, 0x00, 0x00, 0x00, 0xb2, 0xe9,
 0x03, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x7e, 0x00, 0x00, 0x00, 0x00, 0x57, 0x94,
 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x13, 0x00, 0x00, 0x00, 0x00, 0x08, 0x19,
 0x00, 0x00, 0x00, 0x00, 0x00
};

static const GBitmapRec   glyph =
{
  18,
  14,
  18,
  (unsigned char*) glyph_data,
  GBITMAP_FORMAT_GRAY
};

#define  SIZE_X  640
#define  SIZE_Y  480


static unsigned char   buffer[ SIZE_X*3*SIZE_Y ];

unsigned long  seed = 0;

unsigned long  my_rand( void )
{
  seed = seed * 1103515245 + 12345;
  return ((seed >>16) & 32767);
}


#define  RAND(n)  ((unsigned int)my_rand() % (n))

static int
do_glyph( int  arg )
{
  GBlitterRec  blit;
  int          dst_x = RAND(SIZE_X);
  int          dst_y = RAND(SIZE_Y);
  int          color = 0xFFFFFF;  /* draw white exclusively */

  if ( gblitter_init_rgb24( &blit,
                             (GBitmap)&glyph,
                             dst_x,
                             dst_y,
                             SIZE_X,
                             SIZE_Y,
                             buffer,
                             SIZE_X*3 ) )
    return 1;

  if ( arg )
    gblitter_blitrgb24_gray_cache( &blit, color );
  else
    gblitter_blitrgb24_gray_direct( &blit, color );


  return 0;
}



static int
do_glyph_color( int  arg )
{
  GBlitterRec  blit;
  int          dst_x = RAND(SIZE_X);
  int          dst_y = RAND(SIZE_Y);
  int          r     = RAND(256);
  int          g     = RAND(256);
  int          b     = RAND(256);
  int          color = (r << 16) | (g << 8) | b;  /* draw in colors */

  if ( gblitter_init_rgb24( &blit,
                             (GBitmap)&glyph,
                             dst_x,
                             dst_y,
                             SIZE_X,
                             SIZE_Y,
                             buffer,
                             SIZE_X*3 ) )
    return 1;

  if ( arg )
    gblitter_blitrgb24_gray_cache( &blit, color );
  else
    gblitter_blitrgb24_gray_direct( &blit, color );

  return 0;
}


static void
dump_cache_stats( void )
{
  printf( "hits = %ld, miss1 = %ld, miss2 = %ld, hitrate=%.2f%%, miss2rate=%.2f%%\n",
          chits, cmiss1, cmiss2, (double)chits*100.0 / (chits+cmiss1), (double)cmiss2*100.0 / (double)cmiss1 );
}


void usage(void)
{
  fprintf( stderr,
    "gbench: graphics glyph blending benchmark\n"
    "-----------------------------------------\n\n"
    "Usage: gbench [options]\n\n"
    "options:\n" );
  fprintf( stderr,
  "   -t : max time per bench in seconds (default is %.0f)\n", BENCH_TIME );
  fprintf( stderr,
  "   -s seed  : specify random seed\n" );
  fprintf( stderr,
  "   -g gamma : specify gamma\n" );
  exit( 1 );
}

#define TEST(x) (!tests || strchr(tests, x))

int
main(int argc,
     char** argv)
{
  char* tests = NULL;
  int size;
  double gamma = 1.0;

  while (argc > 1 && argv[1][0] == '-')
  {
    switch (argv[1][1])
    {
    case 't':
      argc--;
      argv++;
      if (argc < 1 ||
          sscanf(argv[1], "%lf", &bench_time) != 1)
        usage();
      break;

    case 'g':
      argc--;
      argv++;
      if (argc < 1 ||
          sscanf(argv[1], "%lf", &gamma) != 1)
        usage();
      break;


    case 's':
      if ( argc < 1 )
        usage();

      seed = (unsigned long)atol( argv[1] );
      argc -= 2;
      argv += 2;
      break;

#if 0
    case 'b':
      argc--;
      argv++;
      if (argc < 2)
        usage();
      tests = argv[1];
      break;
#endif

    default:
      fprintf(stderr, "Unknown argument `%s'\n\n", argv[1]);
      usage();
      break;
    }

    argc--;
    argv++;
  }

  if ( argc != 1 )
    usage();

  ggamma_set( gamma );

  memset( buffer, 0, sizeof(buffer) );
  if (TEST('a')) bench( do_glyph, 0, "direct white glyph", 0 );

  chits = cmiss1 = cmiss2 = 0;
  memset( buffer, 0, sizeof(buffer) );
  if (TEST('b')) bench( do_glyph, 1, "cache white glyph", 0 );
  dump_cache_stats();

  memset( buffer, 0, sizeof(buffer) );
  if (TEST('c')) bench( do_glyph_color, 0, "direct color glyph", 0 );

  chits = cmiss1 = cmiss2 = 0;
  memset( buffer, 0, sizeof(buffer) );
  if (TEST('d')) bench( do_glyph_color, 1, "cache color glyph", 0 );
  dump_cache_stats();

  return 0;
}


/* End */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -