Skip to content

Commit 248d069

Browse files
mardyDacoTaco
authored andcommitted
gx: make GX_SetTevIndTile() more readable
The old code suffered from some obfuscation due to how it was reverse engineered: - The 0x43300000 magix number is the binary representation of the upper 32 bits of a double floating point number havint the exponent set to 1075, which makes it so that the least significant bit of the lower 32 bit word maps exactly to the unit 1. - 4503599627370496.0F is 1 << 52, which in floating point representation is 0x43300000 00000000. - 0.00097656250F is "1.0 / (1 << 10)" In other words: multiplying by 0.00097656250F is equivalent to dividing by 2^10, and this is the actual operation that the old code was fulfilling. The subtraction between the double floating point numbers is just an optimisation to convert the integer parameter into a float, so we can safely ignore it and leave it all to the compiler.
1 parent 8aecd24 commit 248d069

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

libogc/gx.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4213,9 +4213,6 @@ void GX_SetTevIndTile(u8 tevstage,u8 indtexid,u16 tilesize_x,u16 tilesize_y,u16
42134213
{
42144214
s32 wrap_s,wrap_t;
42154215
f32 offset_mtx[2][3];
4216-
f64 fdspace_x,fdspace_y;
4217-
u32 fbuf_x[2] = { 0x43300000,tilespacing_x };
4218-
u32 fbuf_y[2] = { 0x43300000,tilespacing_y };
42194216

42204217
wrap_s = GX_ITW_OFF;
42214218
if(tilesize_x==16) wrap_s = GX_ITW_16;
@@ -4231,14 +4228,15 @@ void GX_SetTevIndTile(u8 tevstage,u8 indtexid,u16 tilesize_x,u16 tilesize_y,u16
42314228
else if(tilesize_y==128) wrap_t = GX_ITW_128;
42324229
else if(tilesize_y==256) wrap_t = GX_ITW_256;
42334230

4234-
fdspace_x = *(f64*)((void*)fbuf_x);
4235-
fdspace_y = *(f64*)((void*)fbuf_y);
4236-
4237-
offset_mtx[0][0] = (f32)((fdspace_x - 4503599627370496.0F)*0.00097656250F);
4231+
/* Dividing the tile spacing by 2^10 and passing 10 as the exponent to
4232+
* GX_SetIndTexMatrix() does not alter the end result, but allows us to
4233+
* workaround the numeric limitations described in GX_SetIndTexMatrix()
4234+
* documentation. */
4235+
offset_mtx[0][0] = ((f32)tilespacing_x)/(1 << 10);
42384236
offset_mtx[0][1] = 0.0F;
42394237
offset_mtx[0][2] = 0.0F;
42404238
offset_mtx[1][0] = 0.0F;
4241-
offset_mtx[1][1] = (f32)((fdspace_y - 4503599627370496.0F)*0.00097656250F);
4239+
offset_mtx[1][1] = ((f32)tilespacing_x)/(1 << 10);
42424240
offset_mtx[1][2] = 0.0F;
42434241

42444242
GX_SetIndTexMatrix(indtexmtx,offset_mtx,10);

0 commit comments

Comments
 (0)