| 1 | /* $Id: mmath.c,v 1.1 2000-02-29 00:50:07 sandervl Exp $ */
|
|---|
| 2 |
|
|---|
| 3 | /*
|
|---|
| 4 | * Mesa 3-D graphics library
|
|---|
| 5 | * Version: 3.1
|
|---|
| 6 | *
|
|---|
| 7 | * Copyright (C) 1999 Brian Paul All Rights Reserved.
|
|---|
| 8 | *
|
|---|
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a
|
|---|
| 10 | * copy of this software and associated documentation files (the "Software"),
|
|---|
| 11 | * to deal in the Software without restriction, including without limitation
|
|---|
| 12 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|---|
| 13 | * and/or sell copies of the Software, and to permit persons to whom the
|
|---|
| 14 | * Software is furnished to do so, subject to the following conditions:
|
|---|
| 15 | *
|
|---|
| 16 | * The above copyright notice and this permission notice shall be included
|
|---|
| 17 | * in all copies or substantial portions of the Software.
|
|---|
| 18 | *
|
|---|
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|---|
| 20 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|---|
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|---|
| 22 | * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
|---|
| 23 | * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|---|
| 24 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|---|
| 25 | */
|
|---|
| 26 | /* $XFree86: xc/lib/GL/mesa/src/mmath.c,v 1.2 1999/04/04 00:20:28 dawes Exp $ */
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 | #ifdef PC_HEADER
|
|---|
| 33 | #include "all.h"
|
|---|
| 34 | #else
|
|---|
| 35 | #ifndef XFree86Server
|
|---|
| 36 | #include <math.h>
|
|---|
| 37 | #else
|
|---|
| 38 | #include "GL/xf86glx.h"
|
|---|
| 39 | #endif
|
|---|
| 40 | #include "gl.h"
|
|---|
| 41 | #include "mmath.h"
|
|---|
| 42 | #endif
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 | static int in_fast_math;
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 | /*
|
|---|
| 49 | * A High Speed, Low Precision Square Root
|
|---|
| 50 | * by Paul Lalonde and Robert Dawson
|
|---|
| 51 | * from "Graphics Gems", Academic Press, 1990
|
|---|
| 52 | */
|
|---|
| 53 |
|
|---|
| 54 | /*
|
|---|
| 55 | * SPARC implementation of a fast square root by table
|
|---|
| 56 | * lookup.
|
|---|
| 57 | * SPARC floating point format is as follows:
|
|---|
| 58 | *
|
|---|
| 59 | * BIT 31 30 23 22 0
|
|---|
| 60 | * sign exponent mantissa
|
|---|
| 61 | */
|
|---|
| 62 | static short sqrttab[0x100]; /* declare table of square roots */
|
|---|
| 63 |
|
|---|
| 64 | static void init_sqrt(void)
|
|---|
| 65 | {
|
|---|
| 66 | #ifdef FAST_MATH
|
|---|
| 67 | unsigned short i;
|
|---|
| 68 | float f;
|
|---|
| 69 | unsigned int *fi = (unsigned int *)&f;
|
|---|
| 70 | /* to access the bits of a float in */
|
|---|
| 71 | /* C quickly we must misuse pointers */
|
|---|
| 72 |
|
|---|
| 73 | for(i=0; i<= 0x7f; i++) {
|
|---|
| 74 | *fi = 0;
|
|---|
| 75 |
|
|---|
| 76 | /*
|
|---|
| 77 | * Build a float with the bit pattern i as mantissa
|
|---|
| 78 | * and an exponent of 0, stored as 127
|
|---|
| 79 | */
|
|---|
| 80 |
|
|---|
| 81 | *fi = (i << 16) | (127 << 23);
|
|---|
| 82 | f = sqrt(f);
|
|---|
| 83 |
|
|---|
| 84 | /*
|
|---|
| 85 | * Take the square root then strip the first 7 bits of
|
|---|
| 86 | * the mantissa into the table
|
|---|
| 87 | */
|
|---|
| 88 |
|
|---|
| 89 | sqrttab[i] = (*fi & 0x7fffff) >> 16;
|
|---|
| 90 |
|
|---|
| 91 | /*
|
|---|
| 92 | * Repeat the process, this time with an exponent of
|
|---|
| 93 | * 1, stored as 128
|
|---|
| 94 | */
|
|---|
| 95 |
|
|---|
| 96 | *fi = 0;
|
|---|
| 97 | *fi = (i << 16) | (128 << 23);
|
|---|
| 98 | f = sqrt(f);
|
|---|
| 99 | sqrttab[i+0x80] = (*fi & 0x7fffff) >> 16;
|
|---|
| 100 | }
|
|---|
| 101 | #else
|
|---|
| 102 | (void) sqrttab; /* silence compiler warning - unused var */
|
|---|
| 103 | #endif
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 |
|
|---|
| 107 | float gl_sqrt( float x )
|
|---|
| 108 | {
|
|---|
| 109 | #ifdef FAST_MATH
|
|---|
| 110 | unsigned int *num = (unsigned int *)&x;
|
|---|
| 111 | /* to access the bits of a float in C
|
|---|
| 112 | * we must misuse pointers */
|
|---|
| 113 |
|
|---|
| 114 | short e; /* the exponent */
|
|---|
| 115 | if (x == 0.0F) return 0.0F; /* check for square root of 0 */
|
|---|
| 116 | e = (*num >> 23) - 127; /* get the exponent - on a SPARC the */
|
|---|
| 117 | /* exponent is stored with 127 added */
|
|---|
| 118 | *num &= 0x7fffff; /* leave only the mantissa */
|
|---|
| 119 | if (e & 0x01) *num |= 0x800000;
|
|---|
| 120 | /* the exponent is odd so we have to */
|
|---|
| 121 | /* look it up in the second half of */
|
|---|
| 122 | /* the lookup table, so we set the */
|
|---|
| 123 | /* high bit */
|
|---|
| 124 | e >>= 1; /* divide the exponent by two */
|
|---|
| 125 | /* note that in C the shift */
|
|---|
| 126 | /* operators are sign preserving */
|
|---|
| 127 | /* for signed operands */
|
|---|
| 128 | /* Do the table lookup, based on the quaternary mantissa,
|
|---|
| 129 | * then reconstruct the result back into a float
|
|---|
| 130 | */
|
|---|
| 131 | *num = ((sqrttab[*num >> 16]) << 16) | ((e + 127) << 23);
|
|---|
| 132 | return x;
|
|---|
| 133 | #else
|
|---|
| 134 | return sqrt(x);
|
|---|
| 135 | #endif
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | float gl_ubyte_to_float_color_tab[256];
|
|---|
| 139 | float gl_ubyte_to_float_255_color_tab[256];
|
|---|
| 140 |
|
|---|
| 141 | static void
|
|---|
| 142 | init_ubyte_color_tab(void)
|
|---|
| 143 | {
|
|---|
| 144 | int i;
|
|---|
| 145 | for (i = 0 ; i < 256 ; i++) {
|
|---|
| 146 | gl_ubyte_to_float_color_tab[i] = (float) i * (1.0/255.0);
|
|---|
| 147 | gl_ubyte_to_float_255_color_tab[i] = (float) i;
|
|---|
| 148 | }
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | /*
|
|---|
| 152 | * Initialize tables, etc for fast math functions.
|
|---|
| 153 | */
|
|---|
| 154 | void gl_init_math(void)
|
|---|
| 155 | {
|
|---|
| 156 | static GLboolean initialized = GL_FALSE;
|
|---|
| 157 |
|
|---|
| 158 | if (!initialized) {
|
|---|
| 159 | init_sqrt();
|
|---|
| 160 | init_ubyte_color_tab();
|
|---|
| 161 |
|
|---|
| 162 | initialized = GL_TRUE;
|
|---|
| 163 | in_fast_math = 0;
|
|---|
| 164 | }
|
|---|
| 165 | }
|
|---|