]> git.unchartedbackwaters.co.uk Git - francis/winuae.git/commitdiff
pcem: avoid dynamic offsetof in Voodoo codegen
authorStefan Reinauer <stefan.reinauer@coreboot.org>
Mon, 1 Jun 2026 21:15:51 +0000 (14:15 -0700)
committerStefan Reinauer <stefan.reinauer@coreboot.org>
Mon, 1 Jun 2026 21:15:51 +0000 (14:15 -0700)
The Voodoo x86 code generators compute offsets into array members
with offsetof(type, field[index]). Some compilers reject that
expression form.

Add helpers that derive the offset from field[0] plus an element
stride. That preserves the generated offsets without relying on a
runtime-indexed offsetof expression. This matches the same cleanup
merged upstream in 86Box.

pcem/vid_voodoo_codegen_x86-64.h
pcem/vid_voodoo_codegen_x86.h

index 581dfa652710e5d8691859c0dbd413149805e486..e582f2c3b9d9e4dbd234529ecbd468628497372b 100644 (file)
@@ -73,6 +73,14 @@ static int next_block_to_write[4] = {0, 0};
                         fatal("Over!\n");                       \
         } while (0)
 
+#define VOODOO_OFFSETOF_ARRAY(type, field, index)               \
+        (offsetof(type, field[0]) +                             \
+        ((index) * sizeof(((type *)0)->field[0])))
+
+#define VOODOO_OFFSETOF_ARRAY_MEMBER(type, field, index, member) \
+        (offsetof(type, field[0].member) +                      \
+        ((index) * sizeof(((type *)0)->field[0])))
+
 
 static __m128i xmm_01_w;// = 0x0001000100010001ull;
 static __m128i xmm_ff_w;// = 0x00ff00ff00ff00ffull;
@@ -173,21 +181,21 @@ static inline int codegen_texture_fetch(uint8_t *code_block, voodoo_t *voodoo, v
                 addbyte(0xd0);
                 addbyte(0x03); /*ADD EAX, state->lod*/
                 addbyte(0x87);
-                addlong(offsetof(voodoo_state_t, tmu[tmu].lod));
+                addlong(VOODOO_OFFSETOF_ARRAY_MEMBER(voodoo_state_t, tmu, tmu, lod));
                 addbyte(0x3b); /*CMP EAX, state->lod_min*/
                 addbyte(0x87);
-                addlong(offsetof(voodoo_state_t, lod_min[tmu]));
+                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_state_t, lod_min, tmu));
                 addbyte(0x0f); /*CMOVL EAX, state->lod_min*/
                 addbyte(0x4c);
                 addbyte(0x87);
-                addlong(offsetof(voodoo_state_t, lod_min[tmu]));
+                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_state_t, lod_min, tmu));
                 addbyte(0x3b); /*CMP EAX, state->lod_max*/
                 addbyte(0x87);
-                addlong(offsetof(voodoo_state_t, lod_max[tmu]));
+                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_state_t, lod_max, tmu));
                 addbyte(0x0f); /*CMOVNL EAX, state->lod_max*/
                 addbyte(0x4d);
                 addbyte(0x87);
-                addlong(offsetof(voodoo_state_t, lod_max[tmu]));
+                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_state_t, lod_max, tmu));
                 addbyte(0xc1); /*SHR EAX, 8*/
                 addbyte(0xe8);
                 addbyte(8);        
@@ -211,7 +219,7 @@ static inline int codegen_texture_fetch(uint8_t *code_block, voodoo_t *voodoo, v
                 addbyte(28);
                 addbyte(0x8b); /*MOV EBX, state->lod_min*/
                 addbyte(0x9f);
-                addlong(offsetof(voodoo_state_t, lod_min[tmu]));
+                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_state_t, lod_min, tmu));
                 addbyte(0x48); /*SHR RCX, 28*/
                 addbyte(0xc1);
                 addbyte(0xe9);
@@ -326,7 +334,7 @@ static inline int codegen_texture_fetch(uint8_t *code_block, voodoo_t *voodoo, v
                         addbyte(0x8b);
                         addbyte(0xac);
                         addbyte(0xcf);
-                        addlong(offsetof(voodoo_state_t, tex[tmu]));
+                        addlong(VOODOO_OFFSETOF_ARRAY(voodoo_state_t, tex, tmu));
                         addbyte(0x88); /*MOV CL, DL*/
                         addbyte(0xd1);
                         addbyte(0x89); /*MOV EDX, EBX*/
@@ -335,7 +343,7 @@ static inline int codegen_texture_fetch(uint8_t *code_block, voodoo_t *voodoo, v
                         {
                                 addbyte(0x23); /*AND EAX, params->tex_w_mask[ESI]*/
                                 addbyte(0x86);
-                                addlong(offsetof(voodoo_params_t, tex_w_mask[tmu]));
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_w_mask, tmu));
                         }
                         addbyte(0x83); /*ADD EDX, 1*/
                         addbyte(0xc2);
@@ -348,11 +356,11 @@ static inline int codegen_texture_fetch(uint8_t *code_block, voodoo_t *voodoo, v
                                 addbyte(0x12);
                                 addbyte(0x3b); /*CMP EDX, params->tex_h_mask[ESI]*/
                                 addbyte(0x96);
-                                addlong(offsetof(voodoo_params_t, tex_h_mask[tmu]));
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_h_mask, tmu));
                                 addbyte(0x0f); /*CMOVA EDX, params->tex_h_mask[ESI]*/
                                 addbyte(0x47);
                                 addbyte(0x96);
-                                addlong(offsetof(voodoo_params_t, tex_h_mask[tmu]));
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_h_mask, tmu));
                                 addbyte(0x85); /*TEST EBX,EBX*/
                                 addbyte(0xdb);
                                 addbyte(0x41); /*CMOVS EBX, R10(alookup[0](zero))*/
@@ -361,20 +369,20 @@ static inline int codegen_texture_fetch(uint8_t *code_block, voodoo_t *voodoo, v
                                 addbyte(0x1a);
                                 addbyte(0x3b); /*CMP EBX, params->tex_h_mask[ESI]*/
                                 addbyte(0x9e);
-                                addlong(offsetof(voodoo_params_t, tex_h_mask[tmu]));
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_h_mask, tmu));
                                 addbyte(0x0f); /*CMOVA EBX, params->tex_h_mask[ESI]*/
                                 addbyte(0x47);
                                 addbyte(0x9e);
-                                addlong(offsetof(voodoo_params_t, tex_h_mask[tmu]));
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_h_mask, tmu));
                         }
                         else
                         {
                                 addbyte(0x23); /*AND EDX, params->tex_h_mask[ESI]*/
                                 addbyte(0x96);
-                                addlong(offsetof(voodoo_params_t, tex_h_mask[tmu]));
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_h_mask, tmu));
                                 addbyte(0x23); /*AND EBX, params->tex_h_mask[ESI]*/
                                 addbyte(0x9e);
-                                addlong(offsetof(voodoo_params_t, tex_h_mask[tmu]));
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_h_mask, tmu));
                         }
                         /*EAX = S, EBX = T0, EDX = T1*/
                         addbyte(0xd3); /*SHL EBX, CL*/
@@ -395,7 +403,7 @@ static inline int codegen_texture_fetch(uint8_t *code_block, voodoo_t *voodoo, v
                         {
                                 addbyte(0x8b); /*MOV EBP, params->tex_w_mask[ESI]*/
                                 addbyte(0xae);
-                                addlong(offsetof(voodoo_params_t, tex_w_mask[tmu]));
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_w_mask, tmu));
                                 addbyte(0x85); /*TEST EAX, EAX*/
                                 addbyte(0xc0);
                                 addbyte(0x8b); /*MOV ebp_store2, RSI*/
@@ -419,7 +427,7 @@ static inline int codegen_texture_fetch(uint8_t *code_block, voodoo_t *voodoo, v
                         {
                                 addbyte(0x3b); /*CMP EAX, params->tex_w_mask[ESI] - is S at texture edge (ie will wrap/clamp)?*/
                                 addbyte(0x86);
-                                addlong(offsetof(voodoo_params_t, tex_w_mask[tmu]));
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_w_mask, tmu));
                                 addbyte(0x8b); /*MOV ebp_store2, ESI*/
                                 addbyte(0xb7);
                                 addlong(offsetof(voodoo_state_t, ebp_store));
@@ -576,7 +584,7 @@ static inline int codegen_texture_fetch(uint8_t *code_block, voodoo_t *voodoo, v
                         addbyte(0x8b);
                         addbyte(0xac);
                         addbyte(0xcf);
-                        addlong(offsetof(voodoo_state_t, tex[tmu]));
+                        addlong(VOODOO_OFFSETOF_ARRAY(voodoo_state_t, tex, tmu));
                         addbyte(0x28); /*SUB DL, CL*/
                         addbyte(0xca);
                         addbyte(0x80); /*ADD CL, 4*/
@@ -622,12 +630,12 @@ static inline int codegen_texture_fetch(uint8_t *code_block, voodoo_t *voodoo, v
                                 addbyte(0x3b); /*CMP EAX, params->tex_w_mask[ESI+ECX*4]*/
                                 addbyte(0x84);
                                 addbyte(0x8e);
-                                addlong(offsetof(voodoo_params_t, tex_w_mask[tmu]) - 0x10);
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_w_mask, tmu) - 0x10);
                                 addbyte(0x0f); /*CMOVAE EAX, params->tex_w_mask[ESI+ECX*4]*/
                                 addbyte(0x43);
                                 addbyte(0x84);
                                 addbyte(0x8e);
-                                addlong(offsetof(voodoo_params_t, tex_w_mask[tmu]) - 0x10);
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_w_mask, tmu) - 0x10);
 
                         }
                         else
@@ -635,7 +643,7 @@ static inline int codegen_texture_fetch(uint8_t *code_block, voodoo_t *voodoo, v
                                 addbyte(0x23); /*AND EAX, params->tex_w_mask-0x10[ESI+ECX*4]*/
                                 addbyte(0x84);
                                 addbyte(0x8e);
-                                addlong(offsetof(voodoo_params_t, tex_w_mask[tmu]) - 0x10);
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_w_mask, tmu) - 0x10);
                         }
                         if (state->clamp_t[tmu])
                         {
@@ -648,19 +656,19 @@ static inline int codegen_texture_fetch(uint8_t *code_block, voodoo_t *voodoo, v
                                 addbyte(0x3b); /*CMP EBX, params->tex_h_mask[ESI+ECX*4]*/
                                 addbyte(0x9c);
                                 addbyte(0x8e);
-                                addlong(offsetof(voodoo_params_t, tex_h_mask[tmu]) - 0x10);
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_h_mask, tmu) - 0x10);
                                 addbyte(0x0f); /*CMOVAE EBX, params->tex_h_mask[ESI+ECX*4]*/
                                 addbyte(0x43);
                                 addbyte(0x9c);
                                 addbyte(0x8e);
-                                addlong(offsetof(voodoo_params_t, tex_h_mask[tmu]) - 0x10);
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_h_mask, tmu) - 0x10);
                         }
                         else
                         {
                                 addbyte(0x23); /*AND EBX, params->tex_h_mask-0x10[ESI+ECX*4]*/
                                 addbyte(0x9c);
                                 addbyte(0x8e);
-                                addlong(offsetof(voodoo_params_t, tex_h_mask[tmu]) - 0x10);
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_h_mask, tmu) - 0x10);
                         }
                         addbyte(0x88); /*MOV CL, DL*/
                         addbyte(0xd1);
@@ -3464,4 +3472,3 @@ void voodoo_codegen_close(voodoo_t *voodoo)
         munmap(voodoo->codegen_data, sizeof(voodoo_x86_data_t) * BLOCK_NUM*4);
 #endif
 }
-
index 05c4f5e9144f6a7fc8165a99bcd54bfe7e133de4..da0c5e85fc739c5f1b4da060b26ad0863a816925 100644 (file)
@@ -71,6 +71,14 @@ static int next_block_to_write[4] = {0, 0};
                         fatal("Over!\n");                       \
         } while (0)
 
+#define VOODOO_OFFSETOF_ARRAY(type, field, index)               \
+        (offsetof(type, field[0]) +                             \
+        ((index) * sizeof(((type *)0)->field[0])))
+
+#define VOODOO_OFFSETOF_ARRAY_MEMBER(type, field, index, member) \
+        (offsetof(type, field[0].member) +                      \
+        ((index) * sizeof(((type *)0)->field[0])))
+
 
 static __m128i xmm_01_w;// = 0x0001000100010001ull;
 static __m128i xmm_ff_w;// = 0x00ff00ff00ff00ffull;
@@ -143,21 +151,21 @@ static inline int codegen_texture_fetch(uint8_t *code_block, voodoo_t *voodoo, v
                 addbyte(0xd8);
                 addbyte(0x03); /*ADD EAX, state->lod*/
                 addbyte(0x87);
-                addlong(offsetof(voodoo_state_t, tmu[tmu].lod));
+                addlong(VOODOO_OFFSETOF_ARRAY_MEMBER(voodoo_state_t, tmu, tmu, lod));
                 addbyte(0x3b); /*CMP EAX, state->lod_min*/
                 addbyte(0x87);
-                addlong(offsetof(voodoo_state_t, lod_min[tmu]));
+                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_state_t, lod_min, tmu));
                 addbyte(0x0f); /*CMOVL EAX, state->lod_min*/
                 addbyte(0x4c);
                 addbyte(0x87);
-                addlong(offsetof(voodoo_state_t, lod_min[tmu]));
+                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_state_t, lod_min, tmu));
                 addbyte(0x3b); /*CMP EAX, state->lod_max*/
                 addbyte(0x87);
-                addlong(offsetof(voodoo_state_t, lod_max[tmu]));
+                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_state_t, lod_max, tmu));
                 addbyte(0x0f); /*CMOVNL EAX, state->lod_max*/
                 addbyte(0x4d);
                 addbyte(0x87);
-                addlong(offsetof(voodoo_state_t, lod_max[tmu]));
+                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_state_t, lod_max, tmu));
                 addbyte(0x0f); /*MOVZX EBX, AL*/
                 addbyte(0xb6);
                 addbyte(0xd8);
@@ -166,7 +174,7 @@ static inline int codegen_texture_fetch(uint8_t *code_block, voodoo_t *voodoo, v
                 addbyte(8);
                 addbyte(0x89); /*MOV state->lod_frac[tmu], EBX*/
                 addbyte(0x9f);
-                addlong(offsetof(voodoo_state_t, lod_frac[tmu]));
+                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_state_t, lod_frac, tmu));
                 addbyte(0x89); /*MOV state->lod, EAX*/
                 addbyte(0x87);
                 addlong(offsetof(voodoo_state_t, lod));
@@ -185,11 +193,11 @@ static inline int codegen_texture_fetch(uint8_t *code_block, voodoo_t *voodoo, v
                 addlong(tmu ? offsetof(voodoo_state_t, tmu1_t) : offsetof(voodoo_state_t, tmu0_t));
                 addbyte(0xc7); /*MOV state->lod[tmu], 0*/
                 addbyte(0x87);
-                addlong(offsetof(voodoo_state_t, lod_frac[tmu]));
+                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_state_t, lod_frac, tmu));
                 addlong(0);
                 addbyte(0x8b); /*MOV EAX, state->lod_min*/
                 addbyte(0x87);
-                addlong(offsetof(voodoo_state_t, lod_min[tmu]));
+                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_state_t, lod_min, tmu));
                 addbyte(0x66); /*SHRQ XMM4, 28*/
                 addbyte(0x0f);
                 addbyte(0x73);
@@ -218,7 +226,7 @@ static inline int codegen_texture_fetch(uint8_t *code_block, voodoo_t *voodoo, v
                 addlong(offsetof(voodoo_state_t, tex_t));
                 addbyte(0x89); /*MOV state->lod_frac[tmu], EBX*/
                 addbyte(0x9f);
-                addlong(offsetof(voodoo_state_t, lod_frac[tmu]));
+                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_state_t, lod_frac, tmu));
                 addbyte(0x89); /*MOV state->lod, EAX*/
                 addbyte(0x87);
                 addlong(offsetof(voodoo_state_t, lod));
@@ -230,7 +238,7 @@ static inline int codegen_texture_fetch(uint8_t *code_block, voodoo_t *voodoo, v
                 {
                         addbyte(0x8b); /*MOV ECX, state->tex_lod[tmu]*/
                         addbyte(0x8f);
-                        addlong(offsetof(voodoo_state_t, tex_lod[tmu]));
+                        addlong(VOODOO_OFFSETOF_ARRAY(voodoo_state_t, tex_lod, tmu));
                         addbyte(0xb2); /*MOV DL, 8*/
                         addbyte(8);
                         addbyte(0x8b); /*MOV ECX, [ECX+EAX*4]*/
@@ -312,7 +320,7 @@ static inline int codegen_texture_fetch(uint8_t *code_block, voodoo_t *voodoo, v
                         addbyte(0x8b); /*MOV EBP, state->tex[EDI+ECX*4]*/
                         addbyte(0xac);
                         addbyte(0x8f);
-                        addlong(offsetof(voodoo_state_t, tex[tmu]));
+                        addlong(VOODOO_OFFSETOF_ARRAY(voodoo_state_t, tex, tmu));
                         addbyte(0x88); /*MOV CL, DL*/
                         addbyte(0xd1);
                         addbyte(0x89); /*MOV EDX, EBX*/
@@ -321,7 +329,7 @@ static inline int codegen_texture_fetch(uint8_t *code_block, voodoo_t *voodoo, v
                         {
                                 addbyte(0x23); /*AND EAX, params->tex_w_mask[ESI]*/
                                 addbyte(0x86);
-                                addlong(offsetof(voodoo_params_t, tex_w_mask[tmu]));
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_w_mask, tmu));
                         }
                         addbyte(0x83); /*ADD EDX, 1*/
                         addbyte(0xc2);
@@ -334,11 +342,11 @@ static inline int codegen_texture_fetch(uint8_t *code_block, voodoo_t *voodoo, v
                                 addlong((uint32_t)&zero);
                                 addbyte(0x3b); /*CMP EDX, params->tex_h_mask[ESI]*/
                                 addbyte(0x96);
-                                addlong(offsetof(voodoo_params_t, tex_h_mask[tmu]));
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_h_mask, tmu));
                                 addbyte(0x0f); /*CMOVA EDX, params->tex_h_mask[ESI]*/
                                 addbyte(0x47);
                                 addbyte(0x96);
-                                addlong(offsetof(voodoo_params_t, tex_h_mask[tmu]));
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_h_mask, tmu));
                                 addbyte(0x85); /*TEST EBX,EBX*/
                                 addbyte(0xdb);
                                 addbyte(0x0f); /*CMOVS EBX, zero*/
@@ -347,20 +355,20 @@ static inline int codegen_texture_fetch(uint8_t *code_block, voodoo_t *voodoo, v
                                 addlong((uint32_t)&zero);
                                 addbyte(0x3b); /*CMP EBX, params->tex_h_mask[ESI]*/
                                 addbyte(0x9e);
-                                addlong(offsetof(voodoo_params_t, tex_h_mask[tmu]));
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_h_mask, tmu));
                                 addbyte(0x0f); /*CMOVA EBX, params->tex_h_mask[ESI]*/
                                 addbyte(0x47);
                                 addbyte(0x9e);
-                                addlong(offsetof(voodoo_params_t, tex_h_mask[tmu]));
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_h_mask, tmu));
                         }
                         else
                         {
                                 addbyte(0x23); /*AND EDX, params->tex_h_mask[ESI]*/
                                 addbyte(0x96);
-                                addlong(offsetof(voodoo_params_t, tex_h_mask[tmu]));
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_h_mask, tmu));
                                 addbyte(0x23); /*AND EBX, params->tex_h_mask[ESI]*/
                                 addbyte(0x9e);
-                                addlong(offsetof(voodoo_params_t, tex_h_mask[tmu]));
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_h_mask, tmu));
                         }
                         /*EAX = S, EBX = T0, EDX = T1*/
                         addbyte(0xd3); /*SHL EBX, CL*/
@@ -379,7 +387,7 @@ static inline int codegen_texture_fetch(uint8_t *code_block, voodoo_t *voodoo, v
                         {
                                 addbyte(0x8b); /*MOV EBP, params->tex_w_mask[ESI]*/
                                 addbyte(0xae);
-                                addlong(offsetof(voodoo_params_t, tex_w_mask[tmu]));
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_w_mask, tmu));
                                 addbyte(0x85); /*TEST EAX, EAX*/
                                 addbyte(0xc0);
                                 addbyte(0x8b); /*MOV ESI, ebp_store*/
@@ -403,7 +411,7 @@ static inline int codegen_texture_fetch(uint8_t *code_block, voodoo_t *voodoo, v
                         {
                                 addbyte(0x3b); /*CMP EAX, params->tex_w_mask[ESI] - is S at texture edge (ie will wrap/clamp)?*/
                                 addbyte(0x86);
-                                addlong(offsetof(voodoo_params_t, tex_w_mask[tmu]));
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_w_mask, tmu));
                                 addbyte(0x8b); /*MOV ESI, ebp_store*/
                                 addbyte(0xb7);
                                 addlong(offsetof(voodoo_state_t, ebp_store));
@@ -550,7 +558,7 @@ static inline int codegen_texture_fetch(uint8_t *code_block, voodoo_t *voodoo, v
                 {
                         addbyte(0x8b); /*MOV ECX, state->tex_lod[tmu]*/
                         addbyte(0x8f);
-                        addlong(offsetof(voodoo_state_t, tex_lod[tmu]));
+                        addlong(VOODOO_OFFSETOF_ARRAY(voodoo_state_t, tex_lod, tmu));
                         addbyte(0xb2); /*MOV DL, 8*/
                         addbyte(8);
                         addbyte(0x8b); /*MOV ECX, [ECX+EAX*4]*/
@@ -559,7 +567,7 @@ static inline int codegen_texture_fetch(uint8_t *code_block, voodoo_t *voodoo, v
                         addbyte(0x8b); /*MOV EBP, state->tex[EDI+ECX*4]*/
                         addbyte(0xac);
                         addbyte(0x8f);
-                        addlong(offsetof(voodoo_state_t, tex[tmu]));
+                        addlong(VOODOO_OFFSETOF_ARRAY(voodoo_state_t, tex, tmu));
                         addbyte(0x28); /*SUB DL, CL*/
                         addbyte(0xca);
                         addbyte(0x80); /*ADD CL, 4*/
@@ -605,12 +613,12 @@ static inline int codegen_texture_fetch(uint8_t *code_block, voodoo_t *voodoo, v
                                 addbyte(0x3b); /*CMP EAX, params->tex_w_mask[ESI+ECX*4]*/
                                 addbyte(0x84);
                                 addbyte(0x8e);
-                                addlong(offsetof(voodoo_params_t, tex_w_mask[tmu]) - 0x10);
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_w_mask, tmu) - 0x10);
                                 addbyte(0x0f); /*CMOVAE EAX, params->tex_w_mask[ESI+ECX*4]*/
                                 addbyte(0x43);
                                 addbyte(0x84);
                                 addbyte(0x8e);
-                                addlong(offsetof(voodoo_params_t, tex_w_mask[tmu]) - 0x10);
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_w_mask, tmu) - 0x10);
 
                         }
                         else
@@ -618,7 +626,7 @@ static inline int codegen_texture_fetch(uint8_t *code_block, voodoo_t *voodoo, v
                                 addbyte(0x23); /*AND EAX, params->tex_w_mask-0x10[ESI+ECX*4]*/
                                 addbyte(0x84);
                                 addbyte(0x8e);
-                                addlong(offsetof(voodoo_params_t, tex_w_mask[tmu]) - 0x10);
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_w_mask, tmu) - 0x10);
                         }
                         if (state->clamp_t[tmu])
                         {
@@ -631,19 +639,19 @@ static inline int codegen_texture_fetch(uint8_t *code_block, voodoo_t *voodoo, v
                                 addbyte(0x3b); /*CMP EBX, params->tex_h_mask[ESI+ECX*4]*/
                                 addbyte(0x9c);
                                 addbyte(0x8e);
-                                addlong(offsetof(voodoo_params_t, tex_h_mask[tmu]) - 0x10);
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_h_mask, tmu) - 0x10);
                                 addbyte(0x0f); /*CMOVAE EBX, params->tex_h_mask[ESI+ECX*4]*/
                                 addbyte(0x43);
                                 addbyte(0x9c);
                                 addbyte(0x8e);
-                                addlong(offsetof(voodoo_params_t, tex_h_mask[tmu]) - 0x10);
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_h_mask, tmu) - 0x10);
                         }
                         else
                         {
                                 addbyte(0x23); /*AND EBX, params->tex_h_mask-0x10[ESI+ECX*4]*/
                                 addbyte(0x9c);
                                 addbyte(0x8e);
-                                addlong(offsetof(voodoo_params_t, tex_h_mask[tmu]) - 0x10);
+                                addlong(VOODOO_OFFSETOF_ARRAY(voodoo_params_t, tex_h_mask, tmu) - 0x10);
                         }
                         addbyte(0x88); /*MOV CL, DL*/
                         addbyte(0xd1);