Coranac

It would seem these two aren't finished with each other yet.

 

A while ago, I wrote an article about NDS caching , how it can interfere with DMA transfers and what you can do about them. A little later I got a pingback from ant512, who had tried the “safe” DMA routines I made and found they weren't nearly as safe as I'd hoped. I'm still not sure what the actual problem was, but this incident did make me think about one possible reason, namely the one that will be discussed in this post: problematic cache invalidation.

1 Test base

But first things first. Let's start with some simple test code, see below. We have a simple struct definition, two arrays using this struct, and some default data for both arrays that we'll use later.

// A random struct, 32-bits in size.
struct Foo
{
    u8  type;
    u8  id;
    u16 data;
} ALIGN(4);

// Define some globals. We only use 4 of each.
Foo g_src[16] ALIGN(32);
Foo g_dst[16] ALIGN(32);

const Foo c_fooIn[2][4]=
{
    {   // Initial source data.
        { 0x55, 0, 0x5111 },
        { 0x55, 1, 0x5111 },
        { 0x55, 2, 0x5111 },
        { 0x55, 3, 0x5111 }
    },
    {   // Initial destination data.
        { 0xDD, 0, 0xD111 },
        { 0xDD, 1, 0xD111 },
        { 0xDD, 2, 0xD111 },
        { 0xDD, 3, 0xD111 }
    },
};

And now we're going to do some simple things with these arrays that we always do: some reads, some writes, and a struct copy. And for the copying, I'm going to use DMA, because DMA-transfers are fast, amirite(1)? The specific actions I will do are the following:

Initialization
  • Zero out g_src and g_dst.
  • Initialize the arrays with some data, in this case from c_fooIn.
  • Cache-Flush both arrays to ensure they're uncached.
Testing
  • Modify element in g_src, namely g_src[0].
  • Modify an element in g_dst, namely g_dst[3].
  • DMA-copy g_src[0] to g_dst[3].

In other words, this:

void test_init()
{
    // Zero out everything
    memset(g_src, 0, sizeof(g_src));
    memset(g_dst, 0, sizeof(g_dst));

    // Fill 4 of each.
    for(int i=0; i<4; i++)
    {
        g_src[i]= c_fooIn[0][i];
        g_dst[i]= c_fooIn[1][i];
    }

    // Flush data to be sure.
    DC_FlushRange(g_src, sizeof(g_src));
    DC_FlushRange(g_dst, sizeof(g_dst));
}

void test_dmaCopy()
{
    test_init();

    // Change g_src[0] and g_dst[3]
    g_src[0].id += 0x10;
    g_src[0].data= 0x5222;

    g_dst[3].id += 0x10;
    g_dst[3].data= 0xD333;

    // DMA src[0] into dst[0];
    dmaCopy(&g_src[0], &g_dst[0], sizeof(Foo));
}

Note that there is nothing spectacularly interesting going on here. There's just your average struct definition, run of the mill array definitions, and boring old accesses without even any pointer magic that might hint at something tricky going on. Yes, alignment is forced, but that just makes the test more reliable. Also, the fact that I'm incrementing Foo.id rather than just reading from it is only because ARM9 cache is read-allocate, and I need to have these things end up in cache. The main point is that the actions in test_dmaCopy() are very ordinary.

2 Results

It should be obvious what the outcome of the test should be. However, when you run the test (on hardware! not emulator), the result seems to be something different.

// Just dmaCopy.

    // Result           // Expected:
    // Source (hex)
    55, 10, 5222        // 55, 10, 5222
    55, 01, 5111        // 55, 01, 5111
    55, 02, 5111        // 55, 02, 5111
    55, 03, 5111        // 55, 03, 5111
                                 
    // Destination (hex)
    DD, 00, D111        // 55, 10, 5222 (bad!)
    DD, 01, D111        // DD, 01, D111
    DD, 02, D111        // DD, 02, D111
    DD, 13, D333        // DD, 13, D333

Notice that the changed values of g_src[0] never end up in g_dst[0]. Not only that, not even the original values g_src[0] have been copied. It's as if the transfer never happened at all.

The reason for this was covered in detail in the original article. Basically, it's because cache is invisible to DMA. Once a part of memory is cached, the CPU only looks to the contents of the cache and not the actual addresses, meaning that DMA not only reads out-of-date (stale) source data, but also puts it where the CPU won't look. Two actions allow you to remedy this. The first is the cache flush, which write the cache-lines back to RAM and frees the cache-line. Then there's cache invalidate, which just frees the cache-line. Note that in both cases, the cache is dissociated from memory.

With this information, it should be obvious what to do. When DMA-ing from RAM, you need to flush the cache before the transfer to update the source's memory. When DMA-ing to RAM, you need to invalidate after the transfer because now it's actually the cache's data that's stale. When you think about it a little this makes perfect sense, and it's easy enough to implement:

// New DMA-code:
    DC_FlushRange(&g_src[0], sizeof(Foo));          // Flush source.
    dmaCopy(&g_src[0], &g_dst[0], sizeof(Foo));     // Transfer.
    DC_InvalidateRange(&g_dst[0], sizeof(Foo));     // Invalidate destination.

Unfortunately, this doesn't work right either. And if you think about it a lot instead of merely a little, you'll see why. Maybe showing the results will make you see what I mean. The transfer seems to work now, but the earlier changes to g_dst[3] have been erased. How come?

    // Result:          // Expected:
    // Source (hex)
    55, 10, 5222        // 55, 10, 5222
    55, 01, 5111        // 55, 01, 5111
    55, 02, 5111        // 55, 02, 5111
    55, 03, 5111        // 55, 03, 5111
                                 
    // Destination (hex)
    55, 10, D222        // 55, 10, 5222
    DD, 01, D111        // DD, 01, D111
    DD, 02, D111        // DD, 02, D111
    DD, 13, D111        // DD, 13, D333 (wut?)

The problem is that a cache-invalidate invalidates entire cache-lines, not just the range you supply. If the start or end of the data you want invalidate does not align to a cache-line, the adjacent data contained in that line is also thrown away. I hope you can see that this is bad.

This is exactly what's happening here. The ARM9's cache-lines are 32 bytes in size. Because of the alignment I gave the arrays, elements 0 through 3 lie on the same cache-line. The changes to g_dst[3] occur in cache (but only because I read from it through +=). The invalidate of g_dst[0] also invalidates g_dst[3], which throws out the perfectly legit data and you're left in a bummed state. And again, I've done nothing spectacularly interesting here; all I did was modify something and then invalidated data that just happened to be adjacent to it. But that was enough. Very, very bad.

Just to be sure, this is not due to a bad implementation of DC_InvalidateRange(). The function does exactly what it's supposed to do. The problem is inherent in the hardware. If your data does not align correctly to cache-lines, an invalidate will apply to the adjacent data as well. If you do not want that to happen, do not invalidate.

3 Solutions

So what to do? Well, there is one thing, but I'm not sure how foolproof this is, but instead of invalidating the destination afterwards, you can also flush it before the transfer. This frees up the cache-lines without loss of data, and then it should be safe to DMA-copy to it.

    DC_FlushRange(&g_src[0], sizeof(Foo));          // Flush source.
    DC_FlushRange(&g_dst[0], sizeof(Foo));          // Flush destination.
    dmaCopy(&g_src[0], &g_dst[0], sizeof(Foo));     // Transfer.
    // Result:          // Expected:
    // Source (hex)
    55, 10, 5222        // 55, 10, 5222
    55, 01, 5111        // 55, 01, 5111
    55, 02, 5111        // 55, 02, 5111
    55, 03, 5111        // 55, 03, 5111
                                 
    // Destination (hex)
    55, 10, D222        // 55, 10, 5222
    DD, 01, D111        // DD, 01, D111
    DD, 02, D111        // DD, 02, D111
    DD, 13, D333        // DD, 13, D333
   
    // Yay \o/

Alternatively, you can also disable the underlying reason behind the problem with invalidation: the write-buffer. The ARM9 cache allows two modes for writing: write-through, which also updates the memory related to the cache-line; and write-back, which doesn't. Obviously, the write-back is faster, so that's how libnds sets things up. I know that putting the cache in write-through mode fixes this problem, because in libnds 1.4.0 the write-buffer had been accidentally disabled and my test cases didn't fail. This is probably not the route you want to take, though.

4 Conclusions

So what have we learned?

  • Cache - DMA interactions suck and can cause really subtle bugs. Ones that will only show up on hardware too.
  • Cache-flushes and invalidates cover the cache-lines of the requested ranges, which exceed the range you actually wanted.
  • To safely DMA from cachable memory, flush the source range first.
  • Contrary to what I wrote earlier, to DMA to cachable memory, do not cache-invalidate – at least not when the range is not properly aligned to cache-lines. Instead, flush the destination range before the transfer (at which time invalidation should be unnecessary). That said, invalidate should still be safe if the write-buffer is disabled.

Link to test code.

 

Notes:
  1. No I'm not. For NDS WRAM-WRAM copies, DMA is actually slow as hell and outperformed by every other method. But hopefully more on that later. For now, though, I need the DMA for testing purposes.

Some new notes on NDS code size

2009-11-24 – 17:31 | .

When I discussed the memory footprints of several C/C++ elements, I apparently missed a very important item: operator new and related functions. I assumed new shouldn't increase the binary that much, but boy was I wrong.

The short story is that officially new should throw an exception when it can't allocate new memory. Exceptions come with about 60 kb worth of baggage. Yes, this is more or less the same stuff that goes into vector and string.

The long story, including a detailed look at a minimal binary, a binary that uses new and a solution to the exception overhead (in this particular case anyway) can be read below the fold.

(more...)

Signs from Hell

2009-08-03 – 21:42 | .

The integer datatypes in C can be either signed or unsigned. Sometimes, it's obvious which should be used; for negative values you clearly should use signed types, for example. In many cases there is no obvious choice – in that case it usually doesn't matter which you use. Usually, but not always. Sometimes, picking the wrong kind can introduce subtle bugs in your program that, unless you know what to look out for, can catch you off-guard and have you searching for the problem for hours.

I've mentioned a few of these occasions in Tonc here and there, but I think it's worth going over them again in a little more detail. First, I'll explain how signed integers work and what the difference between signed and unsigned and where potential problems can come from. Then I'll discuss some common pitfalls so you know what to expect.

1 Basics

The signedness of a variable refers to whether it can be used to represent negative values or not. Unsigned variables can only have positive values; signed values can be both positive or negative.

In the computer world, signedness is mostly a matter of interpretation. Say you have a variable that is N bits long. This is enough room for 2N distinct numbers, but it says nothing about which range of numbers you should be using them for. Interpreted as unsigned integers, its range would be [0, 2N−1]. Under a signed interpretation, you'd use some bit-patterns for negative numbers. There are actually several ways of doing this, but the most commonly used is known as two's complement which leads to a [−2N−1, 2N−1−1] range: half positive and half negative.

1.1 Two's complement theory

Two's complement is sometimes seen as an awkward system, but it actually follows quite naturally when you only have a fixed number of digits to write down numbers with. Consider the whole line of positive and negative integers. As you move away from zero, the numbers will grow larger and larger. Now suppose you have an counting device composed of a limited number of digits, each of which can only display numbers 0 through 10−1. With N digits, you only have room for 10N different numbers, and once those are used up (at 10N−1), the counter returns to 0 and counting effectively resets. In essence, the number on the counter works in modulo 10N.

The key is that this works in both positive and negative directions. As far as the counter is concerned, 0 and 10N are the same thing. This being the case, you can argue that −1 (that is, the number before zero) is equivalent to 10N−1; and −2 ≡ 10N−2, and so on. Note that this works regardless of what 10 actually is; it can be ten (decimal), two (binary) or sixteen (hexadecimal).

The 10N possible numbers form a window over the number line, but where the window starts is up to the user. For signed numbers, you can move the window so that the upper half of the 10N range is interpreted as negative numbers.

 

Fig 1 shows how this works for 8-bit numbers (written in hex for convenience). The black numbers represent the entire number line, where numbers can have as many digits as you need. With only two nybbles, the counter repeats every 100h = 256 values. FFh, 1FFh, but also −1 all reduce to the same symbol, namely FFh. In Fig 2 you can see how the available symbols are mapped to either signed or unsigned values. In the unsigned case, numbers simply count from 0 to FFh; for signed, the top half of the symbol range is put on the left side of zero and are used for negative numbers.


Fig 1. Representing numbers with limited bits in hexadecimal. With only 8 bits, only the last 2 nybbles are shown. The cycle 00h-FFh repeats every 256 values.

Fig 2. The 00h-FFh range interpreted as unsigned or signed numbers. Note that, say, symbol “FF“ is used for both values FFh (=255) and −1.
 

The mathematical reason behind all this like this. Assume for convenience that N = 1, so that 0 is equivalent to 10 and in fact every multiple of 10. By definition, subtracting a value from itself gives 0. Because subtraction is merely addition by its negative value, you get the following:

(1) \begin{eqnarray} x &-& x &=& 0& \\ x &+& (-x) &=& 0 & \\ x &+& (-x) &\equiv& 10 & \\ & & (-x) &=& 10 &- x \end{eqnarray}

The term −x in the last step should be seen as a unit, call it C. Numerically, C is the number that, when added to x, gives 10. In decimal, if x = 1, then C = 9. C is called the 10's complement of x, because it's what's needed to complete the 10. It's called the two's complement in binary, because then 10 equals two.

In binary, there is an alternative to calculate the twos complement of a number. Subtracting a number from 2N is equivalent to inverting all its bits, so you get:

(2) \begin{eqnarray} (-x) &=& 2^N - x \\ &=& 2^N -1 - x + 1 \\ (-x) &=& \sim x + 1 \\ \end{eqnarray}

Using two's complement(1) for negative numbers has some interesting properties. First, subtraction and addition are basically the same thing. This is nice for arithmetic implementers for two reasons: the same hardware can be used for both operations, and it can be used for both positive and negative numbers.

Second, because the top half is now used for negative numbers, the most significant bit can be seen as a sign bit. Note: a sign bit, not the sign bit. There is a subtle linguistic difference here. When talking about the sign bit, one may thing of it as a single bit that indicates the sign. For example, 8-bit +1 and −1 could be `0000 0001' and `1000 0001', respectively. In two's complement, however, +1 and −1 are actually `0000 0001' and `11111111' (the sum of which is `1,00000000' ≡ 0, as it should be).

1.2 Declaring signed or unsigned

In the end, whether a particular group of bits is signed or unsigned is a matter of interpretation. For example, the 8-bit group `1111 1111' can be either 255 or −1, depending on how you want to look at it. You can't determine the signedness from just the bits themselves.

Also, when you've decided you're going to use a signed interpretation, whether the group forms negative number or not depends on the size of the group. for example, consider the two bytes `01 FF'. As separate bytes, these would form +1 and −1, respectively. However, if you view them as a single 16-bit integer (‘short”), it forms 0x01FF, which is a positive number.

 

In C, you specify signedness when you declare a variable. The general rule is that an integer is signed unless the keyword `unsigned' is used. The exception to the rule is `char', whose default signedness is platform and compiler-dependent! Be careful with this particular datatype.

int ia;                 // Signed integer.
unsigned int ib;        // Unsigned integer.

short sa;               // Signed 16-bit integer.
unsigned short sb;      // Signed 16-bit integer.

char ca;                // ??-signed 8-bit integer.
signed char cb;         // signed 8-bit integer.
unsigned char cc;       // unsigned 8-bit integer.

Because they're shorter and more descriptive, the following typedefs are often used for variable declarations. Basically, it's ‘s’ or ‘u’ for signed or unsigned, respectively, followed by the size of the type in bits. Unsigned variants are also sometimes indicated by ‘u”+typename.

Table 1: common short (un)signed typedefs.
Base type Signed Unsigned
char s8 u8 uchar
short s16 u16 ushort
int/long s32 u32 uint
long long s64 u64  
 

In assembly, you can't declare the signedness of variables, because there's no such thing as variables. There's only labels and how you use those labels determines what the related data are. Technically, there is only one datatype: the 32-bit word, corresponding to C's int or long. The other datatypes are essentially emulated, or defined by how which memory instructions you use: LDRB/LDRSB/STRB for bytes and LDRH/LDRSH/STRH for halfwords. For most data operations, signedness is irrelevant and as such mostly ignored. Only in a few cases does the sign actually matter and as these are essentially the topic of the rest of the article, we'll get to those eventually.

2 Potential problems

The following sections are cases where signedness may become problematic. I say “may”, because often it just works out. But that's just the thing: it can work most of the time and then things can go horribly wrong all of a sudden. The root of the problem comes down to one thing: negative numbers; usually, negative numbers becoming large positive numbers when interpreted as unsigned values.

For example, 32-bit signed −1 = 0xFFFFFFFF = unsigned 4294967295 (= 232−1). If nothing else, remember that part.

2.1 Sign extension, casting and shifting

When you go from a small datatype to a larger one, you're essentially adding a new set of bits at the top, and these bits have to be initialized in a meaningful way. The addition of these bits should have no effect on the value itself. For example, +1 should remain +1 and −1 should remain −1. What this boils down to for two's complement is that the new bits need to be filled with the sign-bit of the old value. This is called sign extension, because the top-bit (the sign-bit) is extended into all the higher bits. There is also zero-extension, which is when the higher bits are zeroed out. These two forms effectively correspond to signed and unsigned casting. (2).


Fig 3. Sign- and zero-extension for bytes when casting. A raw “F0” becomes 240 unsigned or −1 signed.

Conversions of this kind actually happen all the time, without any kind of direct intervention from the programmer. Data operations are always done in CPU words and any time you use a smaller datatype, there is the need to sign- or zero-extend. This also brings forth the question of which type of extension will be used: sign- or zero-extension. As the following bit of code shows, it depends on the signedness of the variable you're converting from. 8-bit variables sc and uc are both initialized by 0xFF, which is either −1 or 255 (you can use either of those too, by the way). After that, these are used to initialize signed or unsigned words.

As you can see from the output, the value in the words correspond to the signedness of the bytes, not the words. Also note that printing sc (the signed byte) gives 0xFFFFFFFF and not the 0xFF you initialized it with, and which are in fact its actual contents since 0xFFFFFFFF is too large to fit into a byte. However, when using it with anything, it's automatically extended to word-size. This becomes great fun when you later compare it to 0xFF again.

// Testing implicit conversions.
void test_conversion()
{
    s8 sc= 0xFF;        // 8-bit -1 (and 255)                  
    u8 uc= 0xFF;        // 8-bit 255 (and -1)

    s32 sisc= sc, siuc= uc;
    u32 uisc= sc, uiuc= uc;

    printf("  sc: %4d=%08X ;   uc:%4d=%08X\n", sc, sc, uc, uc);
    printf("sisc: %4d=%08X ; siuc:%4d=%08X\n", sisc, sisc, siuc, siuc);
    printf("uisc: %4d=%08X ; uiuc:%4d=%08X\n", uisc, uisc, uiuc, uiuc);
    printf("sc==0xFF : %s\n", (sc==0xFF ? "true" : "false") );

    /* Output:
          sc:   -1=FFFFFFFF ;   uc: 255=000000FF
        sisc:   -1=FFFFFFFF ; siuc: 255=000000FF
        uisc:   -1=FFFFFFFF ; uiuc: 255=000000FF
        sc==0xFF : false

        Warnings issued (for sc=0xFF):
        - warning C4305: 'initializing' : truncation from 'const int' to 'signed char'
        - warning C4309: 'initializing' : truncation of constant value
    */

}
 

Sign- and zero-extension also play a role in right-shifts. When using shifts for arithmetic (shift-right is short-hand for a division by power of two), you want the sign preserved. For example, when dividing −16 = 0xFFFF:FFF0 by 16 (shift-right by 4), you want the result to be −1 (=0xFFFF:FFFF), and not 268435455 (=0x0FFF:FFFF). The right-shift that preserves the sign is the arithmetic right-shift, and is used for signed numbers. For unsigned numbers, or if the variable is considered a set of bits instead of a single number, a logical right-shift is appropriate, since that uses zero-extension.

In assembly, arithmetic and logical right-shift are called ASR and LSR, respectively. In Java and other languages where the keyword unsigned does not exist the difference is indicated by >> (sign-extend) and >>> (zero-extend). In C, however, both types use the same symbol: >>. As such, you cannot tell which type of extension is used from just the expression; you'd have to look at the signedness of the operands (including temporaries) to see if it's a logical or arithmetic right-shift.

Table 2: Right-shifts for different languages.
Language Signed Unsigned
ARM asm asr lsr
C >> >>
Java(script) >> >>>

Fig 4. Sign- and zero-extension when right-shifting by four. Unsigned F0h=240 or −16. Unsigned 240>>4 = 15; Signed −16>>4 = −1.

This ambivalence of shift symbols in C can be a major source of pain in fixed-point calculations. Since unsigned has precedence over signed, if you have an unsigned variable at any point of the calculation, all subsequent calculations are unsigned too and you can kiss negative numbers goodbye. If everything starts going wrong as soon as you move in another direction or if rotations aren't calculated properly, this will be the cause.

 

The code below illustrates the problem in a very common situation. You have a position p, and a directional vector for movement, u. Since you want sub-pixel control of these, you use fixed-point notation for both (I'm assuming non-FPU system here). The u vector is a unit vector (say, cos(α), sin(α)); to get to the full velocity vector, we have to multiply u by some speed. The procedure comes down to something like this:

pnew = pold + speed·u

In the example, I'm only considering the x-component for convenience. Now, because position and direction can have negative components, those would be signed. The speed, however, is a length and therefore always positive, so it makes sense to make it unsigned, right? Well, yes and no. As you can see from the result, mostly no.

With speed = +1 and ux = −1, the end result should be +1*−1 = −1, which would be 0xFFFFFF00 in Q8 fixed-point notation. However, it isn't, thanks to the unsignedness of speed, which makes subsequent arithmetic unsigned so the right-shift does not sign-extend. So instead of the small step you intended, you get a giant leap into no man's land.

void test_right_shift()
{
    // Assume movement for 2 directions, with Q8 for everything.
    // a = look direction.  
    // p = (px, py) = position.
    // u = (ux, uy) = ( cos(a), sin(a) )

    int  px= 0;                 // Starting position.
    int  ux= -1<<8;             // Moving backwards.
    uint speed= 1<<8;           // Unsigned as speed's always >= 0, right?

    px = px + (speed*ux>>8);    // Fixed point motion. Result should be -1<<8.

    printf("px : %d=%08X\n", px, px);

    /* Result:
        px: px : 16776960=00FFFF00

        In other words: NOT the -1<<8 = 0xFFFFFF00 you were after.
    */

}

This mistake is depressingly easy to make, even for those who generally think about which datatype to use. Especially those people, as they're prone to optimize prematurely and automatically pick unsigned for a variable that will never be negative. The danger is that unsigned arithmetic has precedence, which can screw up at later right-shifts.

Bottom line: variables used in fixed-point calculations should be signed. Always.

2.2 Division

This isn't really a signed-vs-unsigned item per se, but integer division behaves in a peculiar way for negative numbers. It becomes one, however when you throw right-shift in the fray, which doesn't quite work like a division equivalent anymore for negative numbers. To discriminate between integer and normal division, I will use ‘\ ’ for integer division in this section. Note also the modulo operation is intimately tied to division, so this section applies to that as well.

What integer division comes down to is taking a normal division and throwing away the remaining fraction. For example, 7 / 4 = 1¾. The integer division is just 1. This is also true for negative numbers: −7 / 4 = −1¾, so 7 \ 4 = −1. In short, integer division rounds towards zero. With bit-shifting, however, you get something slightly different. Theoretically, x>>n is equivalent to x \ 2n. For positive numbers, this is true: 7>>2 in binary is 00000111>>2 = 00000001. But with −7>>2 you get 11111001>>2 = 11111110 = −2. Division-by-right-shift always rounds to negative infinity.

The upshot of this difference is that for negative numbers, the results of x \ 2n and x>>n will be out of sync, as Table 3 illustrates. They still give identical results for positive numbers though.

Table 3: integer and by-shift division by four.
x (dec) x \ 4 x>>2 (dec)   x (bin) x>>2 (bin)
-9 -2 -3 11110111 11111101
-8 -2 -2 11111000 11111110
-7 -1 -2 11111001 11111110
-6 -1 -2 11111010 11111110
-5 -1 -2 11111011 11111110
-4 -1 -1 11111100 11111111
-3 0 -1 11111101 11111111
-2 0 -1 11111110 11111111
-1 0 -1 11111111 11111111
0 0 0 00000000 00000000
1 0 0 00000001 00000000
2 0 0 00000010 00000000
3 0 0 00000011 00000000
4 1 1 00000100 00000001
5 1 1 00000101 00000001
6 1 1 00000110 00000001
7 1 1 00000111 00000001
8 2 2 00001000 00000010
9 2 2 00001001 00000010

There are some other consequences besides the obvious difference in results. First, there's how compilers deal with it. Compilers are very well aware that a bit-shift is faster than division and one of the optimizations they perform is replacing divisions by shifts where appropriate(3). For unsigned numerals the division will be replaced by a single shift. However, for signed variables some extra instructions have to added to correct the difference in rounding.

Second, note that the standard integer division does not give an equal distribution of results: there are more results in the zero-bin. Shift-division spreads the results around evenly. In some cases, you will want to use the shift version for that reason. One clear example of this would be tiling: using the ‘proper’ integer division would give you odd-looking results.

Negative number division / right-shift equivalents

Table 3 shows that for negative numbers, integer division and right-shift don't give the same results. If you do want the same results, the following equations can be used. Given x < 0 and N = 2n, then

\begin{eqnarray} x \backslash N &=& (x + (N-1)) >> n \\ \\ x>>n &=& (x - (N-1)) \backslash N \end{eqnarray}

GCC will use the x\N equivalence to produce signed integer division if possible.

2.3 Comparisons

The last area where signedness can be a factor is comparisons. The next bit of code is from my implementation of a filled circle renderer with boundary clipping. The circle is centered on (x0y0). Variables x and y are local variables that keep track of where we are on the circle, because these can be negative, they must be signed. Variables dstW and dstH are the destination image's width and height. Since width and height are unsigned by definition, it'd make sense to make these unsigned, right? Right?

//# Part of a clipped filled circle renderer that didn't quite work.

    int dstP= srf->pitch/2;                     // used in arithmetic, so signed.
    uint dstW= srf->width, dstH= srf->height;   // Unsigned by definition.
    u16 *dstD= ((u16*)srf->data)+(y0*dstP);
   
    int x=0, y= rad, d= 1-rad, left, right;

    ...
   
        // Side octants
        left= x0-y;
        right= x0+y;
        \<b\>if(right>=0 && left<=dstW)\</b\>       // Fully out of bounds
        {
            if(left<0)      left= 0;            // Clip left
            if(right>=dstW) right= dstW-1;      // Clip right

            // Render at scanlines y0-x and y0+x
            if(inRange(y0-x, 0, dstH))
                armset16(color, &dstD[-x*dstP+left], 2*(right-left+1));
            if(inRange(y0+x, 0, dstH))
                armset16(color, &dstD[+x*dstP+left], 2*(right-left+1));
        }
    ...

Well, apparently not. When I tested this, right and bottom edge clipping went fine, but when the circle went over the top or left edge, it disappeared completely.

The problem lies with the line in bold, which does the trivial rejection test. Variables left and right are the left and right-most edges of the scanline of the circle. If this is completely to the left of the screen (right < 0) or to the right of the screen (left ≥ dstW) then there's nothing to do. Technically, the tests on that line are correct, so the code should work. The reason it doesn't actually occurs a few lines earlier: the definition of dstW as an unsigned variable. Because of this, the second condition is an unsigned comparison. Now think of what happens when left moves over the left of the screen. left becomes becomes a (small) negative number, which is converted to postive number for the comparison. A large positive number for that matter – one that's quite a bit larger than the width of the image and as a result the routine thinks the circle is out of bounds.

So again, a routine went all wonky because I assumed that, since a width is always positive, using an unsigned variable would be a good idea.

 

The worst part of this particular bit, however, is that I should have known this. The compiler actually issues a warning for this type of thing:

warning: comparison between signed and unsigned integer expressions

Or at least it would have if I hadn't disabled the warning because the message was cropping up everywhere in my normal and sign-safe for-loops. Let this be a lesson: disable warnings at your own risk and for Offler's sake do not ignore them.

2.4 Well, duh

The problems covered above are the subtle ones, where you have to be aware of some of the details that go into the C language itself. There are also a few issues where the programmer really should have known they were going to be a problem from the start.

 

The first example is, again, one that can occur when optimizing prematurely. You may have heard that loops work better when you count down instead of count up, because in machine code a subtraction is an automatic comparison to zero. So, a clever programmer may turn this:

uint i;         // Unsigned, since it's always positive.
for(i=0; i<size; i++)
{
    // Do whatever
}

into this:

uint i;         // Unsigned, since it's always positive. Right?
for(i=size-1; i>=0; i--)
{
    // Do whatever
}

There are two problems with this code. First, the change probably will not matter with modern compilers because they are aware of the equivalence and can do this conversion themselves(4), so there's nothing to gain from this.

The real problem, however, is the terminating condition: `i>=0'. Since i is unsigned, it can never be negative, and therefore the condition is always true.

 

The second example involves bitfields. As it happens, bitfields can be signed or unsigned as well. For the most part, handling this is like handling normal signedness, but there is one situation where you have to be careful.

void test_bitfield()
{
    struct Foo {
        int     s7 : 7;     // 7-bit signed
        uint    u7 : 7;     // 7-bit unsigned
        int     s1 : 1;     // 1-bit signed
        uint    u1 : 1;     // 1-bit unsigned
    };

    Foo f= { -1, -1, 1, 1 };

    printf("s7: %3d\nu7: %3d\ns1: %3d\nu1: %3d\n\n", f.s7, f.u7, f.s1, f.u1);
   
    /*  Results:
        s7:  -1     // Inited to -1
        u7: 127     // Inited to -1
        \<b\>s1:  -1     // Inited to  1\</b\>
        u1:   1     // Inited to  1
    */

}

In the code above I've created a bif-fielded struct with both signed and unsigned members. There are two 7-bit fields and two 1-bit fields, and these are initialized to −1 and +1, respectively. The values are then printed.

The 7-bit fields work as you might expect. f.s7 is −1, as it's signed, and f.u7 is 127, which is the 7-bit equivalent of −1. The interesting case is for f.s1. This is initialized to 1, but comes out as −1, because for a single signed bit the possibilities are 0 and −1, and not 0 and +1! Without this knowledge, a later test like `f.s1==1' might give unexpected results.

3 Summary

So, summarizing:

  • Unsigned variables only represents positive numbers; signed ones can have positive or negative values. Negative numbers are usually represented via two's complement, which is based on the cyclical nature of counters when you have a limited number of digits.
  • In C, integers are signed unless specified otherwise, except for char, whose signedness is compiler dependent.
  • Careless use of signed and unsigned types can result in subtle runtime bugs with not-so-subtle results. Usually, what happens is that a negative number is reinterpreted as a very large positive number and everything goes banana-shaped.
  • Unsigned has a higher operator precedence than signed. If one of the operands is unsigned, the operation will use unsigned arithmetic. This can cause problems for divisions, modulos, right-shifts and comparisons.
  • For negative numbers, division/modulo by 2n is not quite the same as right-shifts/ANDs. Analyse which is best for your situation, then act accordingly.
  • Ignore compiler warnings at your own peril.
  • The place where a bug manifests is not always the place where it originates. The declaration of variables matters! Do not forget this when debugging or when asking for assistance.
 

There isn't really a hard rule on when to use which signedness, but here are a few guidelines nonetheless.

  • If a variable can, in principle, have negative values, make it signed. If it represents a physical quantity (position, velocity, mass, etc), make it signed.
  • A variable that represents logical values (bools, pixels, colors, raw data) should probably be unsigned.
  • And now the big one: just because a variable will always be positive doesn't mean it should be unsigned. Yes, you may waste half the range, but using signed variables is usually safer. If you must have the larger range (for the smaller datatypes, for example), consider defining the storage variables unsigned, but convert them to local signed ints when you're really going to use them.
  • If mathematical symbols were gods, the minus sign would be Loki. Be extra careful when you encounter them. If there are minus signs anywhere in the algorithm, or even the potential for negative numbers, everything should be done with signed numbers.

Notes:
  1. Or any 10's complement, really.
  2. One could say that zero-extension is just a form of sign-extension; it's just that the sign for an unsigned number is always positive.
  3. And please let the compiler do its job in this regard: the low operator-precedence of shifts makes their use awkward and error-prone. If you mean division, then use division.
  4. Although they may well do it incorrectly: turning the decrementing loop into an incrementing one. Point is, the compiler may not follow exactly what you're doing anyway.

Gaddammit!

 

So here I am, looking forward to a nice quiet weekend; hang back, watch some telly and maybe read a bit – but NNnnneeeEEEEEUUUuuuuuuuu!! Someone had to write an interesting article about sine approximation. With a challenge at the end. And using an inefficient kind of approximation. And so now, instead of just relaxing, I have to spend my entire weekend and most of the week figuring out a better way of doing it. I hate it when this happens >_<.

 

Okay, maybe not.

 

Sarcasm aside, it is an interesting read. While the standard way of calculating a sine – via a look-up table – works and works well, there's just something unsatisfying about it. The LUT-based approach is just … dull. Uninspired. Cowardly. Inelegant. In contrast, finding a suitable algorithm for it requires effort and a modicum of creativity, so something like that always piques my interest.

In this case it's sine approximation. I'd been wondering about that when I did my arctan article, but figured it would require too many terms to really be worth the effort. But looking at Mr Schraut's post (whose site you should be visiting from time to time too; there's good stuff there) it seems you can get a decent version quite rapidly. The article centers around the work found at devmaster thread 5784, which derived the following two equations:

(1) \begin{eqnarray} S_2(x) &=& \frac4\pi x - \frac4{\pi^2} x^2 \\ \\ S_{4d}(x) &=& (1-P)S_2(x) + P S_2^2(x) \end{eqnarray}

These approximations work quite well, but I feel that it actually uses the wrong starting point. There are alternative approximations that give more accurate results at nearly no extra cost in complexity. In this post, I'll derive higher-order alternatives for both. In passing, I'll also talk about a few of the tools that can help analyse functions and, of course, provide some source code and do some comparisons.

(more...)

DMA vs ARM9 - fight!

2009-05-28 – 23:07 | .

DMA, or Direct Memory Access, is a hardware method for transferring data. As it's hardware-driven, it's pretty damn fast(1). As such, it's pretty much the standard method for copying on the NDS. Unfortunately, as many people have noticed, it doesn't always work.

There are two principle reasons for this: cache and TCM. These are two memory regions of the ARM9 that DMA is unaware of, which can lead to incorrect transfers. In this post, I'll discuss the cache, TCM and their interactions (or lack thereof) with DMA.

The majority of the post is actually about cache. Cache basically determines the speed of your app, so it's worth looking into in more detail. Why it and DMA don't like each other much will become clear along the way. I'll also present a number of test cases that show the conflicting areas, and some functions to deal with these problems.

(more...)
Notes:
  1. Well, quite fast anyway. In some circumstances CPU-based transfers are faster, but that's a story for another day.

mode 7 addendum

2009-04-19 – 18:32 | .

Okay. Apparently, I am an idiot who can't do math.

 

One of the longer chapters in Tonc is Mode 7 part 2, which covers pretty much all the hairy details of producing mode 7 effects on the GBA. The money shot for in terms of code is the following functions, which calculates the affine parameters of the background for each scanline in section 21.7.3.

IWRAM_CODE void m7_prep_affines(M7_LEVEL *level)
{
    if(level->horizon >= SCREEN_HEIGHT)
        return;

    int ii, ii0= (level->horizon>=0 ? level->horizon : 0);

    M7_CAM *cam= level->camera;
    FIXED xc= cam->pos.x, yc= cam->pos.y, zc=cam->pos.z;

    BG_AFFINE *bga= &level->bgaff[ii0];

    FIXED yb, zb;           // b' = Rx(theta) *  (L, ys, -D)
    FIXED cf, sf, ct, st;   // sines and cosines
    FIXED lam, lcf, lsf;    // scale and scaled (co)sine(phi)
    cf= cam->u.x;      sf= cam->u.z;
    ct= cam->v.y;      st= cam->w.y;
    for(ii= ii0; ii<SCREEN_HEIGHT; ii++)
    {
        yb= (ii-M7_TOP)*ct + M7_D*st;
        lam= DivSafe( yc<<12,  yb);     // .12f    <- OI!!!

        lcf= lam*cf>>8;                 // .12f
        lsf= lam*sf>>8;                 // .12f

        bga->pa= lcf>>4;                // .8f
        bga->pc= lsf>>4;                // .8f

        // lambda·Rx·b
        zb= (ii-M7_TOP)*st - M7_D*ct;   // .8f
        bga->dx= xc + (lcf>>4)*M7_LEFT - (lsf*zb>>12);  // .8f
        bga->dy= zc + (lsf>>4)*M7_LEFT + (lcf*zb>>12);  // .8f

        // hack that I need for fog. pb and pd are unused anyway
        bga->pb= lam;
        bga++;
    }
    level->bgaff[SCREEN_HEIGHT]= level->bgaff[0];
}

For details on what all the terms mean, go the page in question. For now, just note that call to DivSafe() to calculate the scaling factor λ and recall that division on the GBA is pretty slow. In Mode 7 part 1, I used a LUT, but here I figured that since the yb term can be anything thanks to the pitch you can't do that. After helping Ruben with his mode 7 demo, it turns out that you can.

 

Fig 1. Sideview of the camera and floor. The camera is tilted slightly down by angle θ.

Fig 1 shows the situation. There is a camera (the black triangle) that is tilted down by pitch angle θ. I've put the origin at the back of the camera because it makes things easier to read. The front of the camera is the projection plane, which is essentially the screen. A ray is cast from the back of the camera on to the floor and this ray intersects the projection plane. The coordinates of this point are xp = (yp, D) in projection plane space, which corresponds to point (yb, zb) in world space. This is simply rotating point xp by θ. The scaling factor is the ratio between the y or z coordinates of the points on the floor and on the projection plane, so that's:

\lambda = y_c / y_b,

and for yb the rotation gives us:

y_b = y_p cos \theta + D sin \theta,

where yc is the camera height, yp is a scanline offset (measured from the center of the screen) and D is the focus length.

Now, the point is that while yb is variable and non-integral when θ ≠ 0, it is still bounded! What's more, you can easily calculate its maximum value, since it's simply the maximum length of xp. Calling this factor R, we get:

R = \sqrt{max(y_p)^2 + D^2}

This factor R, rounded up, is the size of the required LUT. In my particular case, I've used yp= scanline−80 and D = 256, which gives R = sqrt((160−80)² + 256²) = 268.2. In other words, I need a division LUT with 269 entries. Using .16 fixed point numbers for this LUT, the replacement code is essentially:

// The new division LUT. For 1/0 and 1/1, 0xFFFF is used.
u16 m7_div_lut[270]=
{
    0xFFFF, 0xFFFF, 0x8000, 0x5556, ...
};


// Inside the function
    for(ii= ii0; ii<SCREEN_HEIGHT; ii++)
    {
        yb= (ii-M7_TOP)*ct + M7_D*st;           // .8
        lam= (yc*m7_div_lut[yb>>8])>>12;        // .8*.16/.12 = .12
       
        ... // business as usual
    }

At this point, several questions may arise.

  • What about negative yb? The beauty here is that while yb may be negative in principle, such values would correspond to lines above the horizon and we don't calculate those anyway.
  • Won't non-integral yb cause inaccurate look-ups? True, yb will have a fractional part that is simply cut off during a simple look-up and some sort of interpolation would be better. However, in testing there were no noticeable differences between direct look-up, lerped look-up or using Div(), so the simplest method suffices.
  • Are .16 fixed point numbers enough?. Yes, apparently so.
  • ZOMG OVERFLOW! Are .16 fixed point numbers too high? Technically, yes, there is a risk of overflow when the camera height gets too high. However, at high altitudes the map is going to look like crap anyway due to the low resolution of the screen. Furthermore, the hardware only uses 8.8 fixeds, so scales above 256.0 wouldn't work anyway.

And finally:

  • What do I win? With Div() m7_prep_affines() takes about 51k cycles. With the direct look-up this reduces to about 13k: a speed increase by a factor of 4.
 

So yeah, this is what I should have figured out years ago, but somehow kept overlooking it. I'm not sure if I'll add this whole thing to Tonc's text and code, but I'll at least put up a link to here. Thanks Ruben, for showing me how to do this properly.

To C or not to C

2008-09-03 – 1:14 | .

Tonclib is coded mostly in C. The reason for this was twofold. First, I still have it in my head that C is lower level than C++, and that the former would compile to faster code; and faster is good. Second, it's easier for C++ to call C than the other way around so, for maximum compatibility, it made sense to code it in C. But these arguments always felt a little weak and now that I'm trying to port tonclib's functions to the DS, the question pops up again.

 

On many occasions, I just hated not going for C++. Not so much for its higher-level functionality like classes, inheritance and other OOPy goodness (or badness, some might say), but more because I would really, really like to make use of things like function overloading, default parameters and perhaps templates too.

 

For example, say you have a blit routine. You can implement this in multiple ways: with full parameters (srcX/Y, dstX/Y, width/height), using Point and Rect structs (srcRect, dstPoint) or perhaps just a destination point, using the full source-bitmap. In other words:

void blit(Surface *dst, int dstX, int dstY, int srcW, int srcH, Surface *src, int srcX, int srcY);
void blit(Surface *dst, Point *dstPoint, Surface *src, Rect *srcRect);
void blit(Surface *dst, Point *dstPoint, Surface *src);

In C++, this would be no problem. You just declare and define the functions and the compiler mangles the names internally to avoid naming conflicts. You can even make some of the functions inline facades that morphs the arguments for the One True Implementation. In C, however, this won't work. You have to do the name mangling yourself, like blit, blit2, blit3, or blitEx or blitRect, and so on and so forth. Eeghh, that is just ugly.

 

Speaking of points and rectangles, that's another thing. Structs for points and rects are quite useful, so you make one using int members (you should always start with ints). But sometimes it's better to have smaller versions, like shorts. Or maybe unsigned variations. And so you end up with:

struct point8_t   { s8  x, y; };   // Point as signed char
struct point16_t  { s16 x, y; };   // Point as signed short
struct point32_t  { s32 x, y; };   // Point as signed int

struct upoint8_t  { u8  x, y; };   // Point as unsigned char
struct upoint16_t { u16 x, y; };   // Point as unsigned short
struct upoint32_t { u32 x, y; };   // Point as unsigned int

And then that for rects too. And perhaps 3D vectors. And maybe add floats to the mix as well. This all requires that you make structs which are identical except for the primary datatype. That just sounds kinda dumb to me.

But wait, it gets even better! You might like to have some functions to go with these structs, so now you have to create different sets (yes, sets) of functions that differ only by their parameter types too! AAAARGGGGHHHHH, for the love of IPU, NOOOOOOOOOOOOOO!!! Neen, neen; driewerf neen! >_<

That's how it would be in C. In C++, you can just use a template like so:

template<class T>
struct point_t  { T x, y; };    // Point via templates

typedef point_t<u8> point8_t;   // And if you really want, you can
                                // typedef for specific types.

and be done with it. And then you can make a single template function (or was it function template, I always forget) that works for all the datatypes and let the compiler work it out. Letting the computer do the work for you, hah! What will they think of next.

 

Oh, and there's namespaces too! Yes! In C, you always have to worry about if some other library has something with the same name as you're thinking of using. This is where all those silly prefixes come from (oh hai, FreeImage!). With C++, there's a clean way out of that: you can encapsulate them in a namespace and when a conflict arises you can use mynamespace::foo to get out of it. And if there's no conflicts, use using namespace mynamespace; and type just plain foo. None of that FreeImage_foo causing you to have more prefix than genuine function identifier.

 

And [i]then[/i] there's C++ benefits like classes and everything that goes with it. Yes, classes can become fiendishly difficult if pushed too far(1), but inheritance and polymorphism are nice when you have any kind of hierarchy in your program. All Actors have positions, velocities and states. But a PlayerActor also needs input; and an NpcActor has AI. And each kind of NPC has different methods for behaviour and capabilities, and different Items have different effects and so on. It's possible to do this in just C (hint: unioned-structs and function-tables and of course state engines), but whether you'd want to is another matter. And there's constructors for easier memory management, STL and references. And, yes, streams, exceptions and RTTI too if you want to kill your poor CPU (regarding GBA/DS I mean), but nobody's forcing you to use those.


So why the hell am I staying with C again? Oh right, performance!

Performance, really? I think I heard this was a valid point a long time ago, but is it still true now? To test this, I turned all tonclib's C files into C++ files, compiled again and compared the two. This is the result:


Difference in function size between C++ and C in bytes.

That graph shows the difference in the compiled function size. Positive means C++ uses more instructions. In nearly 300 functions, the only differences are minor variations in irq_set(), some of the rendering routines and TTE parsers, and neither language is the clear winner. Overall, C++ seems to do a little bit better, but the difference is marginal.

I've also run a diff between the generated assembly. There are a handful of functions where the order of instructions are different, or different registers are used, or a value is placed in a register instead of on the stack. That's about it. In other words, there is no significant difference between pure C code and its C++ equivalent. Things will probably be a little different when OOP features and exceptions enter the fray, but that's to be expected. But if you stay close to C-like C++, the only place you'll notice anything is in the name-mangling. Which you as a programmer won't notice anyway because it all happens behind the scenes.

 

So that strikes performance off my list, leaving only wider compatibility. I suppose that has still some merit, but considering you can turn C-code into valid C++ by changing the extension(2), this is sound more and more like an excuse instead of a reason.


Notes:
  1. As the saying goes: C++ makes it harder to shoot yourself in the foot, but when you do, you blow off your whole leg.
  2. and clean up the type issues that C allows but C++ doesn't, like void* arithmetic and implicit pointer casts from void*.

Fiddle to the bittle

2008-05-30 – 19:34 | .

I've added two new routines to the bit-trick page: 1→4 bit-unpack with reverse and bit reversals. This last one is elegant … except for one bit of C tomfoolery that is required to get GCC to produce the right ARM code. I hope to discuss this in more detail later.

I've also added a new document about dealing with bitfields. It explains what to do with them, gives a few useful functions to get and set bitfields, and demonstrates how to use the C construct for bitfields. It also touches briefly on a nasty detail in the way GCC implements bitfield that can cause them to fail in certain GBA/NDS memory sections. If you're using bitfields to map VRAM or OAM, please read.

Surface drawing routines.

2008-05-14 – 18:19 | .

I've been building a basic interface for dealing with graphic surfaces lately. I already had most of the routines for 16bpp and 8bpp bitmaps in older Toncs, but but their use was still somewhat awkward because you had to provide some details of the destination manually; most notably a base pointer and the pitch. This got more than a little annoying, especially when trying to make blitters as well. So I made some changes.


typedef struct TSurface
{
    u8  *data;      //!< Surface data pointer.
    u32 pitch;      //!< Scanline pitch in bytes (PONDER: alignment?).
    u16 width;      //!< Image width in pixels.
    u16 height;     //!< Image width in pixels.
    u8  bpp;        //!< Bits per pixel.
    u8  type;       //!< Surface type (not used that much).
    u16 palSize;    //!< Number of colors.
    u16 *palData;   //!< Pointer to palette.
} TSurface;

I've rebuilt the routines around a surface description struct called TSurface (see above). This way, I can just initialize the surface somewhere and just pass the pointer to that surface around. There are a number of different kinds of surfaces. The most important ones are these three:

  • bmp16. 16bpp bitmap surfaces.
  • bmp8. 8bpp bitmap surfaces.
  • chr4c. 4bpp tiled surfaces, in column-major order (i.e., tile 1 is under tile 0 instead of to the right). Column-major order may seem strange, but it actually simplifies the code considerably. There is also a chr4r mode for normal, row-major tiling, but that's unfinished and will probably remain so.
surface.gba movie
Demonstrating surface routines for 4bpp tiles.

For each of these three, I have the most important rendering functions: plotting pixels, lines, rectangles and blits. Yes, blits too. Even for chr4c-mode. There are routines for frames (empty rectangles) and floodfill as well. The functions have a uniform interface with respect to surface-type, so switching between them should be easy were it necessary. There are also tables with function pointers to these routines, so by using those you need not really care about the details of the surface after its creation. I'll probably add a pointer to such a table in TSurface in the future.


Linkies


The image on the right is the result of the following routine. Turret pic semi-knowingly provided by Kawa.

void test_surface_procs(const TSurface *src, TSurface *dst,
    const TSurfaceProcTab *procs, u16 colors[])
{
    // Init object text
    tte_init_obj(&oam_mem[127], ATTR0_TALL, ATTR1_SIZE_8, 512,
        CLR_YELLOW, 0, &vwf_default, NULL);
    tte_init_con();
    tte_set_margins(8, 140, 160, 152);

    // And go!
    tte_printf("#{es;P}%s surface primitives#{w:60}", procs->name);

    tte_printf("#{es;P}Rect#{w:20}");
    procs->rect(dst, 20, 20, 100, 100, colors[0]);

    tte_printf("#{w:30;es;P}Frame#{w:20}");
    procs->frame(dst, 21, 21, 99, 99, colors[1]);

    tte_printf("#{w:30;es;P}Hlines#{w:20}");

    procs->hline(dst, 23, 23, 96, colors[2]);
    procs->hline(dst, 23, 96, 96, colors[2]);

    tte_printf("#{w:30;es;P}Vlines#{w:20}");
    procs->vline(dst, 23, 25, 94, colors[3]);
    procs->vline(dst, 96, 25, 94, colors[3]);

    tte_printf("#{w:30;es;P}Lines#{w:20}");
    procs->line(dst, 25, 25, 94, 40, colors[4]);
    procs->line(dst, 94, 25, 79, 94, colors[4]);
    procs->line(dst, 94, 94, 25, 79, colors[4]);
    procs->line(dst, 25, 94, 40, 25, colors[4]);

    tte_printf("#{w:30;es;P}Full blit#{w:20}");
    procs->blit(dst, 120, 16, src->width, src->height, src, 0, 0);

    tte_printf("#{w:30;es;P}Partial blit#{w:20}");
    procs->blit(dst, 40, 40, 40, 40, src, 12, 8);

    tte_printf("#{w:30;es;P}Floodfill#{w:20}");
    procs->flood(dst, 40, 32, colors[5]);
    tte_printf("#{w:30;es;P}Again !#{w:20}");
    procs->flood(dst, 40, 32, colors[6]);

    tte_printf("#{w:30;es;P;w:30}Ta-dah!!!#{w:20}");

    key_wait_till_hit(KEY_ANY);
}

// Test 4bpp tiled, column-major surfaces
void test_chr4c_procs()
{
    TSurface turret, dst;

    // Init turret for blitting.
    srf_init(&turret, SRF_CHR4C, turretChr4cTiles, 128, 128, 4, NULL);

    // Init destination surface
    srf_init(&dst, SRF_CHR4C, tile_mem[0], 240, 160, 4, pal_bg_mem);
    schr4c_prep_map(&dst, se_mem[31], 0);
    GRIT_CPY(pal_bg_mem, turretChr4cPal);

    // Set video stuff
    REG_DISPCNT= DCNT_MODE0 | DCNT_BG2 | DCNT_OBJ | DCNT_OBJ_1D;
    REG_BG2CNT= BG_CBB(0)|BG_SBB(31);

    u16 colors[8]= { 6, 13, 1, 14, 15, 0, 14, 0 };

    // Run internal tester
    test_surface_procs(&turret, &dst, &chr4c_tab, colors);
}

Via coding horror, I stumbled upon a simply wonderful talk by Herb Sutter about various performance issues like how much operations cost. It also discusses how memory, latency and machine architecture can affect that cost how this has changed over the years. You can find the slides and a video of the presentation at http://nwcpp.org/Meetings/2007/09.html.

Be prepared for a total geek-out. This is highly technical (and awesome, but that's bordering on a tautology) stuff and probably not for the faint of heart. Slides 6 and 7, for example, around the 23m mark) show the value of cache compared to getting something from RAM, and just how bad retrieval from disk is. Later (slides 13 and on; around 55m in the video), when it comes to threads and how a compiler or even hardware may screw you over not do what you want to do, or even what you tell it to do, people how still have them are allowed to run to their moms for safety. By Patina, that is just nasty.

Near the end Sutter discusses the differences between using vectors, lists and sets and what the penalties for the latter are for something as simple add adding all the values in them. This starts at around slide 22, or 1h40m. Even if the rest is gobblyjook, this part is easy to understand. Basically, low footprint and sequential accesses are Good Things, even if you have cache and stuff. Especially when you have cache and stuff.

Next Page »

Powered by WordPress