1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2002-2012, Industrial Light & Magic, a division of Lucas
4 // Digital Ltd. LLC
5 //
6 // All rights reserved.
7 //
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
10 // met:
11 // *       Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // *       Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
16 // distribution.
17 // *       Neither the name of Industrial Light & Magic nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
33 ///////////////////////////////////////////////////////////////////////////
34 
35 
36 #include "ImathFun.h"
37 
38 IMATH_INTERNAL_NAMESPACE_SOURCE_ENTER
39 
40 
41 float
succf(float f)42 succf (float f)
43 {
44     union {float f; int i;} u;
45     u.f = f;
46 
47     if ((u.i & 0x7f800000) == 0x7f800000)
48     {
49         // Nan or infinity; don't change value.
50     }
51     else if (u.i == 0x00000000 || u.i == 0x80000000)
52     {
53         // Plus or minus zero.
54 
55         u.i = 0x00000001;
56     }
57     else if (u.i > 0)
58     {
59         // Positive float, normalized or denormalized.
60         // Incrementing the largest positive float
61         // produces +infinity.
62 
63         ++u.i;
64     }
65     else
66     {
67         // Negative normalized or denormalized float.
68 
69         --u.i;
70     }
71 
72     return u.f;
73 }
74 
75 
76 float
predf(float f)77 predf (float f)
78 {
79     union {float f; int i;} u;
80     u.f = f;
81 
82     if ((u.i & 0x7f800000) == 0x7f800000)
83     {
84         // Nan or infinity; don't change value.
85     }
86     else if (u.i == 0x00000000 || u.i == 0x80000000)
87     {
88         // Plus or minus zero.
89 
90         u.i = 0x80000001;
91     }
92     else if (u.i > 0)
93     {
94         // Positive float, normalized or denormalized.
95 
96         --u.i;
97     }
98     else
99     {
100         // Negative normalized or denormalized float.
101         // Decrementing the largest negative float
102         // produces -infinity.
103 
104         ++u.i;
105     }
106 
107     return u.f;
108 }
109 
110 
111 double
succd(double d)112 succd (double d)
113 {
114     union {double d; Int64 i;} u;
115     u.d = d;
116 
117     if ((u.i & 0x7ff0000000000000LL) == 0x7ff0000000000000LL)
118     {
119         // Nan or infinity; don't change value.
120     }
121     else if (u.i == 0x0000000000000000LL || u.i == 0x8000000000000000LL)
122     {
123         // Plus or minus zero.
124 
125         u.i = 0x0000000000000001LL;
126     }
127     else if (u.d > 0)
128     {
129         // Positive double, normalized or denormalized.
130         // Incrementing the largest positive double
131         // produces +infinity.
132 
133         ++u.i;
134     }
135     else
136     {
137         // Negative normalized or denormalized double.
138 
139         --u.i;
140     }
141 
142     return u.d;
143 }
144 
145 
146 double
predd(double d)147 predd (double d)
148 {
149     union {double d; Int64 i;} u;
150     u.d = d;
151 
152     if ((u.i & 0x7ff0000000000000LL) == 0x7ff0000000000000LL)
153     {
154         // Nan or infinity; don't change value.
155     }
156     else if (u.i == 0x0000000000000000LL || u.i == 0x8000000000000000LL)
157     {
158         // Plus or minus zero.
159 
160         u.i = 0x8000000000000001LL;
161     }
162     else if (u.d > 0)
163     {
164         // Positive double, normalized or denormalized.
165 
166         --u.i;
167     }
168     else
169     {
170         // Negative normalized or denormalized double.
171         // Decrementing the largest negative double
172         // produces -infinity.
173 
174         ++u.i;
175     }
176 
177     return u.d;
178 }
179 
180 
181 IMATH_INTERNAL_NAMESPACE_SOURCE_EXIT
182