This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.

 

  Fast Data Copy Of Compile-Time-Known Size
  Submitted by



The fastest way to copy any data of compile-time-known size is to trick the compiler to moving it for you. The secret here is that the compiler can do cool things. If you want to move 6 bytes, the compiler will generate a DWORD store and a WORD store, etc.

This simple template does all the work for you.

//========================================================================================

template <int size,typename T1,typename T2
void static_copy_bytes(T1 *to,const T2 *fm)
{
	struct char_array
	{
		char data[size];
	};

*(reinterpret_cast< char_array *(to)) = *(reinterpret_cast<const char_array *(fm)); }

template <int count,typename T void static_copy_entries(T *to,const T *fm) { struct entry_array { T data[count]; };

*(reinterpret_cast< entry_array *(to)) = *(reinterpret_cast<const entry_array *(fm)); }

void static_memcpy_test() { int fm[32] = { 4, 7 , 0 }; int to[32]; int to2[32];

static_copy_bytes<32*4(to,fm);

static_copy_entries<32(to2,fm);

printf("%d %d\n",to[0],to[1]); }

//========================================================================================



The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.

 

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.