Skip to content

Commit 03757c9

Browse files
mardyExtrems
authored andcommitted
gx: make GX_SetTevIndTile() more readable
The old code suffered from some obfuscation due to how it was reverse engineered: - The 0x43300000 magic number is the binary representation of the upper 32 bits of a double floating point number having 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 cdbdcdc commit 03757c9

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
@@ -4341,9 +4341,6 @@ void GX_SetTevIndTile(u8 tevstage,u8 indtexid,u16 tilesize_x,u16 tilesize_y,u16
43414341
{
43424342
s32 wrap_s,wrap_t;
43434343
f32 offset_mtx[2][3];
4344-
f64 fdspace_x,fdspace_y;
4345-
u32 fbuf_x[2] = { 0x43300000,tilespacing_x };
4346-
u32 fbuf_y[2] = { 0x43300000,tilespacing_y };
43474344

43484345
wrap_s = GX_ITW_OFF;
43494346
if(tilesize_x==16) wrap_s = GX_ITW_16;
@@ -4359,14 +4356,15 @@ void GX_SetTevIndTile(u8 tevstage,u8 indtexid,u16 tilesize_x,u16 tilesize_y,u16
43594356
else if(tilesize_y==128) wrap_t = GX_ITW_128;
43604357
else if(tilesize_y==256) wrap_t = GX_ITW_256;
43614358

4362-
fdspace_x = *(f64*)((void*)fbuf_x);
4363-
fdspace_y = *(f64*)((void*)fbuf_y);
4364-
4365-
offset_mtx[0][0] = (f32)((fdspace_x - 4503599627370496.0F)*0.00097656250F);
4359+
/* Dividing the tile spacing by 2^10 and passing 10 as the exponent to
4360+
* GX_SetIndTexMatrix() does not alter the end result, but allows us to
4361+
* workaround the numeric limitations described in GX_SetIndTexMatrix()
4362+
* documentation. */
4363+
offset_mtx[0][0] = (f32)tilespacing_x/1024.0F;
43664364
offset_mtx[0][1] = 0.0F;
43674365
offset_mtx[0][2] = 0.0F;
43684366
offset_mtx[1][0] = 0.0F;
4369-
offset_mtx[1][1] = (f32)((fdspace_y - 4503599627370496.0F)*0.00097656250F);
4367+
offset_mtx[1][1] = (f32)tilespacing_y/1024.0F;
43704368
offset_mtx[1][2] = 0.0F;
43714369

43724370
GX_SetIndTexMatrix(indtexmtx,offset_mtx,10);

0 commit comments

Comments
 (0)