1 /*
2   (C) Copyright 2001,2006,
3   International Business Machines Corporation,
4   Sony Computer Entertainment, Incorporated,
5   Toshiba Corporation,
6 
7   All rights reserved.
8 
9   Redistribution and use in source and binary forms, with or without
10   modification, are permitted provided that the following conditions are met:
11 
12     * Redistributions of source code must retain the above copyright notice,
13   this list of conditions and the following disclaimer.
14     * Redistributions in binary form must reproduce the above copyright
15   notice, this list of conditions and the following disclaimer in the
16   documentation and/or other materials provided with the distribution.
17     * Neither the names of the copyright holders nor the names of their
18   contributors may be used to endorse or promote products derived from this
19   software without specific prior written permission.
20 
21   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22   IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23   TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24   PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 #ifndef _FLOORF_H_
34 #define _FLOORF_H_	1
35 
36 #include <spu_intrinsics.h>
37 #include "headers/vec_literal.h"
38 
39 /*
40  * FUNCTION
41  *	float _floorf(float value)
42  *
43  * DESCRIPTION
44  *	The _floorf routine round the input value "value" downwards to the
45  *	nearest integer returning the result as a float. Two forms of the
46  *	floor function are provided - full range and limited (integer)
47  *	range.
48  *
49  *	The full range form (default) provides floor computation on
50  *	all IEEE floating point values. The floor of NANs remain NANs.
51  *	The floor of denorms results in zero.
52  *
53  *	The limited range form (selected by defining FLOOR_INTEGER_RANGE)
54  *	compute ths floor of all floating-point values in the 32-bit
55  *	signed integer range. Values outside this range get clamped.
56  */
57 
_floorf(float value)58 static __inline float _floorf(float value)
59 {
60 #ifdef FLOOR_INTEGER_RANGE
61   /* 32-BIT INTEGER DYNAMIC RANGE
62    */
63   union {
64     float f;
65     signed int i;
66     unsigned int ui;
67   } bias;
68 
69   bias.f = value;
70 
71   /* If positive, bias the input value to truncate towards
72    * positive infinity, instead of zero.
73    */
74   bias.ui = (unsigned int)(bias.i >> 31) & 0x3F7FFFFF;
75   value -= bias.f;
76 
77   /* Remove fraction bits by casting to an integer and back
78    * to a floating-point value.
79    */
80   return ((float)((int)value));
81 
82 #else /* !FLOOR_INTEGER_RANGE */
83   /* FULL FLOATING-POINT RANGE
84    */
85   vec_int4 exp, shift;
86   vec_uint4 mask, frac_mask, addend, insert, pos;
87   vec_float4 in, out;
88 
89   in = spu_promote(value, 0);
90 
91   /* This function generates the following component
92    * based upon the inputs.
93    *
94    *   mask = bits of the input that need to be replaced.
95    *   insert = value of the bits that need to be replaced
96    *   addend = value to be added to perform function.
97    *
98    * These are applied as follows:.
99    *
100    *   out = ((in & mask) | insert) + addend
101    */
102   pos = spu_cmpgt((vec_int4)in, -1);
103   exp = spu_and(spu_rlmask((vec_int4)in, -23), 0xFF);
104 
105   shift = spu_sub(127, exp);
106 
107   frac_mask = spu_and(spu_rlmask(VEC_SPLAT_U32(0x7FFFFF), shift),
108                       spu_cmpgt((vec_int4)shift, -31));
109 
110   mask = spu_orc(frac_mask, spu_cmpgt(exp, 126));
111 
112   addend = spu_andc(spu_andc(spu_add(mask, 1), pos),
113                     spu_cmpeq(spu_and((vec_uint4)in, mask), 0));
114 
115   insert = spu_andc(spu_andc(VEC_SPLAT_U32(0xBF800000), pos),
116                     spu_cmpgt((vec_uint4)spu_add(exp, -1), 126));
117 
118   out = (vec_float4)spu_add(spu_sel((vec_uint4)in, insert, mask), addend);
119 
120   return (spu_extract(out, 0));
121 #endif /* FLOOR_INTEGER_RANGE */
122 }
123 #endif /* _FLOORF_H_ */
124