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 _CEILF_H_
34 #define _CEILF_H_	1
35 
36 #include <spu_intrinsics.h>
37 #include "headers/vec_literal.h"
38 
39 /*
40  * FUNCTION
41  *	float _ceilf(float value)
42  *
43  * DESCRIPTION
44  *	The _ceilf routine round the input value "value" upwards to the
45  *	nearest integer returning the result as a float. Two forms of the
46  *	ceiling function are provided - full range and limited (integer)
47  *	range.
48  *
49  *	The full range form (default) provides ceiling computation on
50  *	all IEEE floating point values. The ceiling of NANs remain NANs.
51  *	The ceiling of denorms results in zero.
52  *
53  *	The limited range form (selected by defining CEIL_INTEGER_RANGE)
54  *	compute ths ceiling of all floating-point values in the 32-bit
55  *	signed integer range. Values outside this range get clamped.
56  */
57 
_ceilf(float value)58 static __inline float _ceilf(float value)
59 {
60 #ifdef CEIL_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 /* !CEIL_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   vec_float4 one = VEC_SPLAT_F32(1.0f);
89 
90   in = spu_promote(value, 0);
91 
92   /* This function generates the following component
93    * based upon the inputs.
94    *
95    *   mask = bits of the input that need to be replaced.
96    *   insert = value of the bits that need to be replaced
97    *   addend = value to be added to perform function.
98    *
99    * These are applied as follows:.
100    *
101    *   out = ((in & mask) | insert) + addend
102    */
103   pos = spu_cmpgt((vec_int4)in, -1);
104   exp = spu_and(spu_rlmask((vec_int4)in, -23), 0xFF);
105 
106   shift = spu_sub(127, exp);
107 
108   frac_mask = spu_and(spu_rlmask(VEC_SPLAT_U32(0x7FFFFF), shift),
109                       spu_cmpgt((vec_int4)shift, -31));
110 
111   mask = spu_orc(frac_mask, spu_cmpgt(exp, 126));
112 
113   addend = spu_andc(spu_and(spu_add(mask, 1), pos), spu_cmpeq(spu_and((vec_uint4)in, mask), 0));
114 
115   insert = spu_andc(spu_and(pos, (vec_uint4)one),
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 /* CEIL_INTEGER_RANGE */
122 }
123 #endif /* _CEILF_H_ */
124