Archive for the ‘Crazy snippets’ Category
Tuesday, February 9th, 2010
Little challenge posted by a workmate. Good for those 5 minute breaks at work when you need to force your brain to think about something else than your current task.
Recode the following piece of code so that it doesn't use branches/multiplies:
[code lang="C++"]
unsigned char* oldStart = 0;
if (m_start + size ...
Posted in Crazy snippets, Gamedev, General programming | 13 Comments »
Tuesday, October 27th, 2009
Duff's Device is one of the most brilliant exploits of C syntax. It's used to unroll loops and save some cycles spent on loop 'maintenance'. Let's take a look at typical fill_n function:
[code lang="C++"]
template RDE_FORCEINLINE
void fill_n(T* first, size_t n, const T& val)
{
for (size_t i = 0; ...
Posted in Crazy snippets, Gamedev, General programming | 5 Comments »
Wednesday, October 21st, 2009
Everyone who's done at least some vertex/pixel shader/HLSL programming has probably encountered mechanism called "swizzling". It's an operation where we create new vector using arbitrarily selected components of another vector (also a little bit similiar to SSE shuffling). Code snippet is worth 100 words, so some examples:
[code lang="C++"]
a = b.zyzx; ...
Posted in Crazy snippets, Gamedev, General programming | 4 Comments »
Sunday, March 1st, 2009
When developing stack-based containers for RDESTL I've encountered the following problem - how to get block of uninitialized memory that's aligned properly for type T. Consider fixed_vector class: [code lang="C++"]template
class fixed_array
{
...
char m_data[N * sizeof(T)];[/code]Size is OK (we need N elements of type T), sadly alignment is invalid here. It may ...
Posted in Crazy snippets, General programming | 3 Comments »
Saturday, April 19th, 2008
OK, so how does this snippet work? There's a function that takes array of N elements as an argument. It returns an array of N chars. Assuming sizeof(char) == 1, size of the return type for this function is N. There's no function body, because it's not needed, function is ...
Posted in Crazy snippets, General programming | No Comments »
Saturday, April 12th, 2008
(or one more reason to love C++).
Problem: how to find out number of elements in a C++ array? The most popular form is probably: [code lang="C++"]#define RDE_COUNT_OF(arr) (sizeof(arr)/sizeof(arr[0]))
...
Foo myTab[10];
size_t numElems = RDE_COUNT_OF(myTab);
assert(numElems == 10); [/code]
It's being widely used, however most people do not realize the potential danger with this small ...
Posted in Crazy snippets, General programming | 3 Comments »
Wednesday, February 13th, 2008
Consider the following code snippet:
[code lang="C++"]struct Foo
{
void* operator new(size_t bytes);
void operator delete(void* ptr);
};
struct Bar : public Foo
{
void* operator new(size_t bytes);
void operator delete(void* ptr);
};
[...]
Foo* b = new Bar();
delete b;
[/code]
Can you see a problem here?
Posted in Crazy snippets, General programming | 1 Comment »
Wednesday, January 9th, 2008
I've found an interesting bug in a very old code today. Consider the following code snippet:
[code lang='c++']struct foo
{
void* operator new(size_t t);
void operator delete(void* p);
int i;
};
[...]
{
foo* f = new foo();
::delete(f);
}[/code]
Can you ...
Posted in Crazy snippets, General programming | No Comments »
Tuesday, January 8th, 2008
Something we’ve been wondering at work one day (just for kicks, but being resident template freak I couldnt resist to give it a try). Challenge: given STL collection, find a way to determine “compatible” iterator type for it. Example of intended usage:
[code lang='c++']std::vector v;
for (ITER_TYPE(int, v)::iterator it = v.begin(); it ...
Posted in Crazy snippets | 1 Comment »