Re: [cc65] Scalable UI without float?

From: Ullrich von Bassewitz <uz1musoftware.de>
Date: 2010-04-22 19:55:50
Good evening!

On Tue, Apr 20, 2010 at 11:01:55PM -0500, Payton Byrd wrote:
> I told you I was tired. :)  Multiply first is the answer I was looking for.

Let me use this opportunity to introduce a new library feature of the next
cc65 version:

As others have already pointed out, changing the order of operations is the
solution to the problem. But this opens a pitfall: The first operation may
overflow. Which is not true in your case but may become true for graphics
operations, where coordinates are in the range of 2^8 or larger.

As an example, assume that we want to calculate the intersection point of a
line with the right screen border (X coordinate 319 on the C64). If the line
is determined by x1/y1 and x2/y2, the intersection is aprox.


        x = 319
        y = y1 + (x - x1) * (y2 - y1) / (x2 - x1)

As in your case, we need to do the multiplication before the division. While
the final result fits nicely in 16 bits, we may get an intermediate result
that is larger than 16 bits, because we're multiplying X and Y coordinates and
want to allow screen coordinates that are slightly outside of the visible
area. One workaround is to introduce casts to long, so multiplication and
division are done with 32 bit precision. Unfortunately, this is much slower.

To help with this problem, I've added several additional multiplication and
division routines:

long __fastcall__ cc65_imul16x16r32 (int lhs, int rhs);
/* Multiplicate two signed 16 bit to yield a signed 32 bit result */

long __fastcall__ cc65_idiv32by16r16 (long rhs, int lhs);
/* Divide a 32 bit signed value by a 16 bit signed value yielding a 16
 * bit result and a 16 bit remainder. The former is returned in the lower 16
 * bit of the result, the latter in the upper. If you don't need the
 * remainder, just assign (or cast) to an int.
 */

(for more see cc65.h but beware, not all of them are currently implemented)

So it is possible to have just the intermediate result in the necessary
precision, increase the speed by a factor of 2-4 (depending on the operands
and the operation).

Regards


        Uz


-- 
Ullrich von Bassewitz                                  uz@musoftware.de
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Thu Apr 22 19:55:56 2010

This archive was generated by hypermail 2.1.8 : 2010-04-22 19:55:58 CEST