HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 #if defined(LINUX) && !defined(AMD64) && !defined(ARM64)
25 #define UT_USE_ASM_ACCEL
26 #endif
27 
28 // You must ensure that the processor supports SSE before calling these
29 // functions by calling UTgetProcessor() and checking for UT_PROCFLAG_SSE or
30 // UT_PROCFLAG_3DNOW. This is not done in these functions as it would be
31 // pretty inefficent.
32 
33 // The FP -> int versions are much faster because clamping does not have
34 // any branches (about 10x). The int->FP versions are slightly faster (1.5x).
35 // Performance tested on Linux RH7, P4.
36 
37 // Float to byte conversions; (0-1) -> (0-255) and (0,1) -> (b,w)
38 UT_API void UTconvertFPto8(unsigned char *dest,
39  const fpreal32 *source,
40  int length,
41  int useprocext = 0);
42 
43 UT_API void UTconvertFPto8BW(unsigned char *dest,
44  const fpreal32 *source,
45  int length,
46  unsigned int black,
47  unsigned int white,
48  int useprocext = 0);
49 
50 // byte to fpreal32 conversions; (0-65535) -> (0-1) and (b,w) -> (0,1)
51 UT_API void UTconvert8toFP(fpreal32 *dest,
52  const unsigned char *source,
53  int length,
54  bool usesse = true);
55 
57  const unsigned char*source,
58  int length,
59  unsigned int black,
60  unsigned int white,
61  bool usesse = true);
62 
63 // Float to short conversions; (0-1) -> (0-65535) and (0,1) -> (b,w)
64 UT_API void UTconvertFPto16(unsigned short *dest,
65  const fpreal32 *source,
66  int length,
67  int procext = 0);
68 
69 UT_API void UTconvertFPto16BW(unsigned short *dest,
70  const fpreal32 *source,
71  int length,
72  unsigned int black,
73  unsigned int white,
74  bool usesse = true);
75 
76 // short to fpreal32 conversions; (0-65535) -> (0-1) and (b,w) -> (0,1)
78  const unsigned short*source,
79  int length,
80  bool usesse = true);
81 
83  const unsigned short *source,
84  int length,
85  unsigned int black,
86  unsigned int white,
87  bool usesse = true);
88 
89 // The next 2 functions convert FP numbers to 8 bit and 16 bit values.
90 UT_API inline unsigned char UTconvertFPto8(fpreal32 fval)
91 {
92  unsigned char val;
93 
94 #ifdef UT_USE_ASM_ACCEL
95  const fpreal32 f255[1] = { 255.0f };
96  const fpreal32 *fptr = &fval;
97  int ival = 0;
98 
99  __asm__ __volatile (
100  "movss (%%edi), %%xmm0 \n"
101  "movss (%%ebx), %%xmm1 \n"
102  "mov $255, %%ecx \n" // ecx = 255
103  "mulss %%xmm1, %%xmm0 \n"
104  "mov $0, %%ebx \n" // ebx = 0
105  "cvtss2si %%xmm0, %%eax \n"
106  "cmp %%eax, %%ecx \n" // compare to 255
107  "cmovl %%ecx, %%eax \n"
108  "cmp %%eax, %%ebx \n" // compare to 0
109  "cmovg %%ebx, %%eax \n"
110  : "=a" (ival)
111  : "D" (fptr),
112  "b" (f255) : "%ecx");
113 
114  val = (unsigned char) ival;
115 #else
116  int t = (int) SYSfloorIL(fval * 255.0f +0.5F);
117 
118  if(t < 0)
119  val = 0;
120  else if(t > 255)
121  val = 255;
122  else
123  val = (unsigned char) t;
124 #endif
125  return val;
126 }
127 
128 UT_API inline unsigned short UTconvertFPto16(fpreal32 fval)
129 {
130  unsigned short val;
131 
132 #ifdef UT_USE_ASM_ACCEL
133  const fpreal32 f65535[1] = { 65535.0f };
134  const fpreal32 *fptr = &fval;
135  int ival = 0;
136 
137  __asm__ __volatile (
138  "movss (%%edi), %%xmm0 \n"
139  "movss (%%ebx), %%xmm1 \n"
140  "mov $65535, %%ecx \n" // ecx = 65535
141  "mulss %%xmm1, %%xmm0 \n"
142  "mov $0, %%ebx \n" // ebx = 0
143  "cvtss2si %%xmm0, %%eax \n"
144  "cmp %%eax, %%ecx \n" // compare to 65535
145  "cmovl %%ecx, %%eax \n"
146  "cmp %%eax, %%ebx \n" // compare to 0
147  "cmovg %%ebx, %%eax \n"
148  : "=a" (ival)
149  : "D" (fptr),
150  "b" (f65535) : "%ecx");
151 
152  val = (unsigned short) ival;
153 #else
154  int t = (int) SYSfloorIL(fval * 65535.0f +0.5F);
155  if(t < 0)
156  val = 0;
157  else if(t > 65535)
158  val = 65535;
159  else
160  val = (unsigned short) t;
161 #endif
162  return val;
163 }
164 
165 #endif
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
UT_API void UTconvertFPto8(unsigned char *dest, const fpreal32 *source, int length, int useprocext=0)
GLuint GLsizei GLsizei * length
Definition: glcorearb.h:795
#define UT_API
Definition: UT_API.h:14
float fpreal32
Definition: SYS_Types.h:200
GLfloat f
Definition: glcorearb.h:1926
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:803
UT_API void UTconvertFPto16(unsigned short *dest, const fpreal32 *source, int length, int procext=0)
GLdouble t
Definition: glad.h:2397
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)
GLuint GLfloat * val
Definition: glcorearb.h:1608
fpreal32 SYSfloorIL(fpreal32 val)
Definition: SYS_Floor.h:59
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)