HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UT_CommonMMXFuncs.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * Authored by:
7  * Mark Alexander
8  * Side Effects Software Inc.
9  *
10  * NAME: UT_CommonMMXFuncs.h (Utility Library, C++)
11  *
12  * COMMENTS:
13  * Common MMX functions. They do not check for whether MMX/SSE are
14  * actually present - it is up to the programmer to check beforehand.
15  */
16 
17 #ifndef __UT_CommonMMXFuncs_h__
18 #define __UT_CommonMMXFuncs_h__
19 
20 #include "UT_API.h"
21 #include <SYS/SYS_Types.h>
22 #include <SYS/SYS_Floor.h>
23 
24 #ifdef LINUX
25 #if !defined(IA64) && !defined(AMD64) && !defined(PPC)
26 #define UT_USE_ASM_ACCEL
27 #endif
28 #endif
29 
30 // You must ensure that the processor supports SSE before calling these
31 // functions by calling UTgetProcessor() and checking for UT_PROCFLAG_SSE or
32 // UT_PROCFLAG_3DNOW. This is not done in these functions as it would be
33 // pretty inefficent.
34 
35 // The FP -> int versions are much faster because clamping does not have
36 // any branches (about 10x). The int->FP versions are slightly faster (1.5x).
37 // Performance tested on Linux RH7, P4.
38 
39 // Float to byte conversions; (0-1) -> (0-255) and (0,1) -> (b,w)
40 UT_API void UTconvertFPto8(unsigned char *dest,
41  const fpreal32 *source,
42  int length,
43  int useprocext = 0);
44 
45 UT_API void UTconvertFPto8BW(unsigned char *dest,
46  const fpreal32 *source,
47  int length,
48  unsigned int black,
49  unsigned int white,
50  int useprocext = 0);
51 
52 // byte to fpreal32 conversions; (0-65535) -> (0-1) and (b,w) -> (0,1)
53 UT_API void UTconvert8toFP(fpreal32 *dest,
54  const unsigned char *source,
55  int length,
56  bool usesse = true);
57 
59  const unsigned char*source,
60  int length,
61  unsigned int black,
62  unsigned int white,
63  bool usesse = true);
64 
65 // Float to short conversions; (0-1) -> (0-65535) and (0,1) -> (b,w)
66 UT_API void UTconvertFPto16(unsigned short *dest,
67  const fpreal32 *source,
68  int length,
69  int procext = 0);
70 
71 UT_API void UTconvertFPto16BW(unsigned short *dest,
72  const fpreal32 *source,
73  int length,
74  unsigned int black,
75  unsigned int white,
76  bool usesse = true);
77 
78 // short to fpreal32 conversions; (0-65535) -> (0-1) and (b,w) -> (0,1)
80  const unsigned short*source,
81  int length,
82  bool usesse = true);
83 
85  const unsigned short *source,
86  int length,
87  unsigned int black,
88  unsigned int white,
89  bool usesse = true);
90 
91 // The next 2 functions convert FP numbers to 8 bit and 16 bit values.
92 UT_API inline unsigned char UTconvertFPto8(fpreal32 fval)
93 {
94  unsigned char val;
95 
96 #ifdef UT_USE_ASM_ACCEL
97  const fpreal32 f255[1] = { 255.0f };
98  const fpreal32 *fptr = &fval;
99  int ival = 0;
100 
101  __asm__ __volatile (
102  "movss (%%edi), %%xmm0 \n"
103  "movss (%%ebx), %%xmm1 \n"
104  "mov $255, %%ecx \n" // ecx = 255
105  "mulss %%xmm1, %%xmm0 \n"
106  "mov $0, %%ebx \n" // ebx = 0
107  "cvtss2si %%xmm0, %%eax \n"
108  "cmp %%eax, %%ecx \n" // compare to 255
109  "cmovl %%ecx, %%eax \n"
110  "cmp %%eax, %%ebx \n" // compare to 0
111  "cmovg %%ebx, %%eax \n"
112  : "=a" (ival)
113  : "D" (fptr),
114  "b" (f255) : "%ecx");
115 
116  val = (unsigned char) ival;
117 #else
118  int t = (int) SYSfloorIL(fval * 255.0f +0.5F);
119 
120  if(t < 0)
121  val = 0;
122  else if(t > 255)
123  val = 255;
124  else
125  val = (unsigned char) t;
126 #endif
127  return val;
128 }
129 
130 UT_API inline unsigned short UTconvertFPto16(fpreal32 fval)
131 {
132  unsigned short val;
133 
134 #ifdef UT_USE_ASM_ACCEL
135  const fpreal32 f65535[1] = { 65535.0f };
136  const fpreal32 *fptr = &fval;
137  int ival = 0;
138 
139  __asm__ __volatile (
140  "movss (%%edi), %%xmm0 \n"
141  "movss (%%ebx), %%xmm1 \n"
142  "mov $65535, %%ecx \n" // ecx = 65535
143  "mulss %%xmm1, %%xmm0 \n"
144  "mov $0, %%ebx \n" // ebx = 0
145  "cvtss2si %%xmm0, %%eax \n"
146  "cmp %%eax, %%ecx \n" // compare to 65535
147  "cmovl %%ecx, %%eax \n"
148  "cmp %%eax, %%ebx \n" // compare to 0
149  "cmovg %%ebx, %%eax \n"
150  : "=a" (ival)
151  : "D" (fptr),
152  "b" (f65535) : "%ecx");
153 
154  val = (unsigned short) ival;
155 #else
156  int t = (int) SYSfloorIL(fval * 65535.0f +0.5F);
157  if(t < 0)
158  val = 0;
159  else if(t > 65535)
160  val = 65535;
161  else
162  val = (unsigned short) t;
163 #endif
164  return val;
165 }
166 
167 #endif
UT_API void UTconvertFPto8(unsigned char *dest, const fpreal32 *source, int length, int useprocext=0)
#define UT_API
Definition: UT_API.h:12
GLfloat f
Definition: glcorearb.h:1925
UT_API void UTconvert8BWtoFP(fpreal32 *dest, const unsigned char *source, int length, unsigned int black, unsigned int white, bool usesse=true)
UT_API void UTconvert8toFP(fpreal32 *dest, const unsigned char *source, int length, bool usesse=true)
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:802
UT_API void UTconvertFPto16(unsigned short *dest, const fpreal32 *source, int length, int procext=0)
UT_API void UTconvertFPto16BW(unsigned short *dest, const fpreal32 *source, int length, unsigned int black, unsigned int white, bool usesse=true)
UT_API void UTconvert16toFP(fpreal32 *dest, const unsigned short *source, int length, bool usesse=true)
typedef int
Definition: png.h:1175
GLuint GLfloat * val
Definition: glcorearb.h:1607
fpreal32 SYSfloorIL(fpreal32 val)
Definition: SYS_Floor.h:59
float fpreal32
Definition: SYS_Types.h:184
UT_API void UTconvert16BWtoFP(fpreal32 *dest, const unsigned short *source, int length, unsigned int black, unsigned int white, bool usesse=true)
UT_API void UTconvertFPto8BW(unsigned char *dest, const fpreal32 *source, int length, unsigned int black, unsigned int white, int useprocext=0)
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:794