#ifndef _MGL_INTERNAL_UTIL_H_ #define _MGL_INTERNAL_UTIL_H_ /* Fast conversion of 0.0-1.0 -> 0-255 */ extern inline uint32 fast_normalized_to_u8(float32 x) { union { float32 f; uint32 i; } u; u.f = 32768.0f + x * (255.0f / 256.0f); return (uint8)u.i; } /* Fast conversion of 0.0-1.0 -> 0-65535 */ extern inline uint32 fast_normalized_to_u16(float32 x) { union { float32 f; uint32 i; } u; u.f = 16777216.0f + x * (65535.0f / 65536.0f); return (uint16)u.i; } static inline int32 fast_log2(int32 value) { register int32 result; __asm__ __volatile__ ( "cntlzw %0, %1" : "=r"(result) : "r"(value) ); return 31L-result; } // Moved from sysinc #ifdef MGL_TABOR extern inline float fast_fabs(float x) { return x>=0.0f ? x : -x; } extern inline float fast_sqrt(float x) { return (float)sqrt((double)x); } extern inline float fast_reciprocal_sqrt(float x) { return (float)(1.0 / sqrt((double)x)); } #else extern inline float fast_fabs(float x) { float abs_x; __asm volatile ("fabs %0,%1" : "=f" (abs_x) : "f" (x)); return abs_x; } #if MGL_USE_EXACT_SQRT extern inline float fast_sqrt(float x) { return (float)sqrt((double)x); } extern inline float fast_reciprocal_sqrt(float x) { return (float)(1.0 / sqrt((double)x)); } #else extern inline float fast_sqrt(float number) //InvSqrt(x) code { float x = 0.5f * number; float y; __asm volatile ("frsqrte %0,%1" : "=f" (y) : "f" (number)); y = y * (1.5f - (x * y * y)); return y * number; } extern inline float fast_reciprocal_sqrt(float number) { float y; __asm volatile ("frsqrte %0,%1" : "=f" (y) : "f" (number)); y = y + 0.5f * y * (1.0f - (number * y * y)); return y; } #endif #endif // MGL_TABOR #ifndef MAX #define MAX(a,b) ( (a) > (b) ? (a) : (b) ) #endif #ifndef MIN #define MIN(a,b) ( (a) < (b) ? (a) : (b) ) #endif #define DL_CHECK(SAVE) \ if (context->TargetDisplayListStart) { \ dl_save_##SAVE; \ if (context->DisplayListCompileMode != GL_COMPILE_AND_EXECUTE) { \ return; \ } \ } #define SWAP_FRONT_FACE \ if (context->polygon.FrontFace == GL_CCW) { \ context->polygon.FrontFace = GL_CW; \ } \ else { \ context->polygon.FrontFace = GL_CCW; \ } #ifdef AUTOMATIC_LOCKING_ENABLE #define ENTER_LOCK \ if (context->LockMode == MGL_LOCK_SMART) { \ if (smartlock_beginDraw(context->smartLock) == GL_FALSE) { \ dprintf("Error during W3D_LockHardware()\n"); \ return; \ } \ } \ else if (context->LockMode == MGL_LOCK_AUTOMATIC) { \ if (W3D_SUCCESS != IWarp3D->W3D_LockHardware(context->w3dContext)) { \ dprintf("Error during W3D_LockHardware()\n"); \ return; \ } \ else { \ context->w3dLocked = GL_TRUE; \ } \ } #define EXIT_LOCK \ if (context->LockMode == MGL_LOCK_SMART) { \ smartlock_endDraw(context->smartLock); \ } \ else if (context->LockMode == MGL_LOCK_AUTOMATIC) { \ IWarp3D->W3D_UnLockHardware(context->w3dContext); \ context->w3dLocked = GL_FALSE; \ } #else #define ENTER_LOCK #define EXIT_LOCK #endif #define IS_COMPRESSED_FORMAT(x) ((x) >= W3D_COMPRESSED_R5G6B5 && (x) <= W3D_COMPRESSED_A8R5G6B5) #define THIS_VERTEX context->VertexBuffer[context->VertexBufferPointer] /* AllocVec / FreeVec replacements */ void *AllocVecInternal(ULONG byteSize, ULONG attributes); void FreeVecInternal(void *memoryBlock); void FreeVecInternalCheckAndClear(void **memoryBlock); /* Create / DeleteIORequest replacements */ struct IORequest *CreateIORequestInternal(struct MsgPort *ioReplyPort, ULONG size); void DeleteIORequestInternal(struct IORequest *ioReq); /* Create / Delete MsgPort replacements */ struct MsgPort* CreateMsgPortInternal(void); void DeleteMsgPortInternal(struct MsgPort *port); #endif