src/video/math_private.h
author Sam Lantinga <slouken@libsdl.org>
Mon, 21 Sep 2009 08:58:51 +0000
branchSDL-1.2
changeset 4214 4250beeb5ad1
parent 1424 7a610f25c12f
child 1662 782fd950bd46
permissions -rw-r--r--
Oh yeah, we have GLX support too.
slouken@1330
     1
/*
slouken@1330
     2
 * ====================================================
slouken@1330
     3
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
slouken@1330
     4
 *
slouken@1330
     5
 * Developed at SunPro, a Sun Microsystems, Inc. business.
slouken@1330
     6
 * Permission to use, copy, modify, and distribute this
slouken@1330
     7
 * software is freely granted, provided that this notice
slouken@1330
     8
 * is preserved.
slouken@1330
     9
 * ====================================================
slouken@1330
    10
 */
slouken@1330
    11
slouken@1330
    12
/*
slouken@1330
    13
 * from: @(#)fdlibm.h 5.1 93/09/24
slouken@1330
    14
 * $Id$
slouken@1330
    15
 */
slouken@1330
    16
slouken@1330
    17
#ifndef _MATH_PRIVATE_H_
slouken@1330
    18
#define _MATH_PRIVATE_H_
slouken@1330
    19
slouken@1424
    20
#include "SDL_name.h"
slouken@1330
    21
#include "SDL_endian.h"
slouken@1330
    22
slouken@1344
    23
#define huge		really_big /* huge is a reserved keyword in VC++ 6.0 */
slouken@1361
    24
#define u_int32_t	uint32_t
slouken@1330
    25
slouken@1330
    26
/* The original fdlibm code used statements like:
slouken@1330
    27
	n0 = ((*(int*)&one)>>29)^1;		* index of high word *
slouken@1330
    28
	ix0 = *(n0+(int*)&x);			* high word of x *
slouken@1330
    29
	ix1 = *((1-n0)+(int*)&x);		* low word of x *
slouken@1330
    30
   to dig two 32 bit words out of the 64 bit IEEE floating point
slouken@1330
    31
   value.  That is non-ANSI, and, moreover, the gcc instruction
slouken@1330
    32
   scheduler gets it wrong.  We instead use the following macros.
slouken@1330
    33
   Unlike the original code, we determine the endianness at compile
slouken@1330
    34
   time, not at run time; I don't see much benefit to selecting
slouken@1330
    35
   endianness at run time.  */
slouken@1330
    36
slouken@1330
    37
/* A union which permits us to convert between a double and two 32 bit
slouken@1330
    38
   ints.  */
slouken@1330
    39
slouken@1330
    40
/*
slouken@1330
    41
 * Math on arm is special:
slouken@1330
    42
 * For FPA, float words are always big-endian.
slouken@1330
    43
 * For VFP, floats words follow the memory system mode.
slouken@1330
    44
 */
slouken@1330
    45
slouken@1330
    46
#if (SDL_BYTEORDER == SDL_BIG_ENDIAN) || \
slouken@1330
    47
    (!defined(__VFP_FP__) && (defined(__arm__) || defined(__thumb__)))
slouken@1330
    48
slouken@1330
    49
typedef union
slouken@1330
    50
{
slouken@1330
    51
  double value;
slouken@1330
    52
  struct
slouken@1330
    53
  {
slouken@1330
    54
    u_int32_t msw;
slouken@1330
    55
    u_int32_t lsw;
slouken@1330
    56
  } parts;
slouken@1330
    57
} ieee_double_shape_type;
slouken@1330
    58
slouken@1330
    59
#else
slouken@1330
    60
slouken@1330
    61
typedef union
slouken@1330
    62
{
slouken@1330
    63
  double value;
slouken@1330
    64
  struct
slouken@1330
    65
  {
slouken@1330
    66
    u_int32_t lsw;
slouken@1330
    67
    u_int32_t msw;
slouken@1330
    68
  } parts;
slouken@1330
    69
} ieee_double_shape_type;
slouken@1330
    70
slouken@1330
    71
#endif
slouken@1330
    72
slouken@1330
    73
/* Get two 32 bit ints from a double.  */
slouken@1330
    74
slouken@1330
    75
#define EXTRACT_WORDS(ix0,ix1,d)				\
slouken@1330
    76
do {								\
slouken@1330
    77
  ieee_double_shape_type ew_u;					\
slouken@1330
    78
  ew_u.value = (d);						\
slouken@1330
    79
  (ix0) = ew_u.parts.msw;					\
slouken@1330
    80
  (ix1) = ew_u.parts.lsw;					\
slouken@1330
    81
} while (0)
slouken@1330
    82
slouken@1330
    83
/* Get the more significant 32 bit int from a double.  */
slouken@1330
    84
slouken@1330
    85
#define GET_HIGH_WORD(i,d)					\
slouken@1330
    86
do {								\
slouken@1330
    87
  ieee_double_shape_type gh_u;					\
slouken@1330
    88
  gh_u.value = (d);						\
slouken@1330
    89
  (i) = gh_u.parts.msw;						\
slouken@1330
    90
} while (0)
slouken@1330
    91
slouken@1330
    92
/* Get the less significant 32 bit int from a double.  */
slouken@1330
    93
slouken@1330
    94
#define GET_LOW_WORD(i,d)					\
slouken@1330
    95
do {								\
slouken@1330
    96
  ieee_double_shape_type gl_u;					\
slouken@1330
    97
  gl_u.value = (d);						\
slouken@1330
    98
  (i) = gl_u.parts.lsw;						\
slouken@1330
    99
} while (0)
slouken@1330
   100
slouken@1330
   101
/* Set a double from two 32 bit ints.  */
slouken@1330
   102
slouken@1330
   103
#define INSERT_WORDS(d,ix0,ix1)					\
slouken@1330
   104
do {								\
slouken@1330
   105
  ieee_double_shape_type iw_u;					\
slouken@1330
   106
  iw_u.parts.msw = (ix0);					\
slouken@1330
   107
  iw_u.parts.lsw = (ix1);					\
slouken@1330
   108
  (d) = iw_u.value;						\
slouken@1330
   109
} while (0)
slouken@1330
   110
slouken@1330
   111
/* Set the more significant 32 bits of a double from an int.  */
slouken@1330
   112
slouken@1330
   113
#define SET_HIGH_WORD(d,v)					\
slouken@1330
   114
do {								\
slouken@1330
   115
  ieee_double_shape_type sh_u;					\
slouken@1330
   116
  sh_u.value = (d);						\
slouken@1330
   117
  sh_u.parts.msw = (v);						\
slouken@1330
   118
  (d) = sh_u.value;						\
slouken@1330
   119
} while (0)
slouken@1330
   120
slouken@1330
   121
/* Set the less significant 32 bits of a double from an int.  */
slouken@1330
   122
slouken@1330
   123
#define SET_LOW_WORD(d,v)					\
slouken@1330
   124
do {								\
slouken@1330
   125
  ieee_double_shape_type sl_u;					\
slouken@1330
   126
  sl_u.value = (d);						\
slouken@1330
   127
  sl_u.parts.lsw = (v);						\
slouken@1330
   128
  (d) = sl_u.value;						\
slouken@1330
   129
} while (0)
slouken@1330
   130
slouken@1330
   131
/* A union which permits us to convert between a float and a 32 bit
slouken@1330
   132
   int.  */
slouken@1330
   133
slouken@1330
   134
typedef union
slouken@1330
   135
{
slouken@1330
   136
  float value;
slouken@1330
   137
  u_int32_t word;
slouken@1330
   138
} ieee_float_shape_type;
slouken@1330
   139
slouken@1330
   140
/* Get a 32 bit int from a float.  */
slouken@1330
   141
slouken@1330
   142
#define GET_FLOAT_WORD(i,d)					\
slouken@1330
   143
do {								\
slouken@1330
   144
  ieee_float_shape_type gf_u;					\
slouken@1330
   145
  gf_u.value = (d);						\
slouken@1330
   146
  (i) = gf_u.word;						\
slouken@1330
   147
} while (0)
slouken@1330
   148
slouken@1330
   149
/* Set a float from a 32 bit int.  */
slouken@1330
   150
slouken@1330
   151
#define SET_FLOAT_WORD(d,i)					\
slouken@1330
   152
do {								\
slouken@1330
   153
  ieee_float_shape_type sf_u;					\
slouken@1330
   154
  sf_u.word = (i);						\
slouken@1330
   155
  (d) = sf_u.value;						\
slouken@1330
   156
} while (0)
slouken@1330
   157
slouken@1330
   158
slouken@1330
   159
#ifdef __STDC__
slouken@1330
   160
static const double
slouken@1330
   161
#else
slouken@1330
   162
static double
slouken@1330
   163
#endif
slouken@1330
   164
zero    =  0.0,
slouken@1330
   165
one	=  1.0,
slouken@1330
   166
two	=  2.0,
slouken@1330
   167
two53	=  9007199254740992.0,	/* 0x43400000, 0x00000000 */
slouken@1330
   168
two54   =  1.80143985094819840000e+16, /* 0x43500000, 0x00000000 */
slouken@1330
   169
twom54  =  5.55111512312578270212e-17, /* 0x3C900000, 0x00000000 */
slouken@1330
   170
huge   = 1.0e+300,
slouken@1330
   171
tiny   = 1.0e-300;
slouken@1330
   172
slouken@1330
   173
#endif /* _MATH_PRIVATE_H_ */