cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 1 | /* |
cristy | fe676ee | 2013-11-18 13:03:38 +0000 | [diff] [blame] | 2 | Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 3 | dedicated to making software imaging solutions freely available. |
| 4 | |
| 5 | You may not use this file except in compliance with the License. |
| 6 | obtain a copy of the License at |
| 7 | |
| 8 | http://www.imagemagick.org/script/license.php |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | |
| 16 | MagickCore quantum inline methods. |
| 17 | */ |
| 18 | #ifndef _MAGICKCORE_QUANTUM_PRIVATE_H |
| 19 | #define _MAGICKCORE_QUANTUM_PRIVATE_H |
| 20 | |
cristy | 6398ec7 | 2013-11-28 02:00:27 +0000 | [diff] [blame^] | 21 | #include "MagickCore/cache.h" |
| 22 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 23 | #if defined(__cplusplus) || defined(c_plusplus) |
| 24 | extern "C" { |
| 25 | #endif |
| 26 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 27 | typedef struct _QuantumState |
| 28 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 29 | double |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 30 | inverse_scale; |
| 31 | |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 32 | unsigned int |
cristy | 6b825f9 | 2010-06-04 02:01:10 +0000 | [diff] [blame] | 33 | pixel; |
| 34 | |
| 35 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 36 | bits; |
| 37 | |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 38 | const unsigned int |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 39 | *mask; |
| 40 | } QuantumState; |
| 41 | |
| 42 | struct _QuantumInfo |
| 43 | { |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 44 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 45 | depth, |
| 46 | quantum; |
| 47 | |
| 48 | QuantumFormatType |
| 49 | format; |
| 50 | |
| 51 | double |
| 52 | minimum, |
| 53 | maximum, |
| 54 | scale; |
| 55 | |
| 56 | size_t |
| 57 | pad; |
| 58 | |
| 59 | MagickBooleanType |
| 60 | min_is_white, |
| 61 | pack; |
| 62 | |
| 63 | QuantumAlphaType |
| 64 | alpha_type; |
| 65 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 66 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 67 | number_threads; |
| 68 | |
| 69 | unsigned char |
| 70 | **pixels; |
| 71 | |
| 72 | size_t |
| 73 | extent; |
| 74 | |
cristy | 32c6843 | 2012-01-12 15:06:28 +0000 | [diff] [blame] | 75 | EndianType |
| 76 | endian; |
| 77 | |
| 78 | QuantumState |
| 79 | state; |
| 80 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 81 | SemaphoreInfo |
| 82 | *semaphore; |
| 83 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 84 | size_t |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 85 | signature; |
| 86 | }; |
| 87 | |
cristy | 7963022 | 2012-01-14 01:38:37 +0000 | [diff] [blame] | 88 | extern MagickPrivate void |
| 89 | ResetQuantumState(QuantumInfo *); |
| 90 | |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 91 | static inline MagickSizeType GetQuantumRange(const size_t depth) |
cristy | 2a4d01c | 2010-01-10 21:14:51 +0000 | [diff] [blame] | 92 | { |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 93 | MagickSizeType |
| 94 | one; |
| 95 | |
| 96 | one=1; |
| 97 | return((MagickSizeType) ((one << (depth-1))+((one << (depth-1))-1))); |
cristy | 2a4d01c | 2010-01-10 21:14:51 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static inline float HalfToSinglePrecision(const unsigned short half) |
cristy | 5ab5fc8 | 2010-01-05 20:52:48 +0000 | [diff] [blame] | 101 | { |
cristy | 8a6a980 | 2010-04-14 13:22:18 +0000 | [diff] [blame] | 102 | #define ExponentBias (127-15) |
cristy | 24fb7dc | 2010-01-10 20:15:51 +0000 | [diff] [blame] | 103 | #define ExponentMask 0x7c00 |
| 104 | #define ExponentShift 23 |
cristy | bccb64f | 2010-01-10 21:16:36 +0000 | [diff] [blame] | 105 | #define SignBitShift 31 |
cristy | 24fb7dc | 2010-01-10 20:15:51 +0000 | [diff] [blame] | 106 | #define SignificandShift 13 |
| 107 | #define SignificandMask 0x00000400 |
cristy | 5ab5fc8 | 2010-01-05 20:52:48 +0000 | [diff] [blame] | 108 | |
cristy | 24fb7dc | 2010-01-10 20:15:51 +0000 | [diff] [blame] | 109 | typedef union _SinglePrecision |
| 110 | { |
| 111 | unsigned int |
| 112 | fixed_point; |
| 113 | |
| 114 | float |
| 115 | single_precision; |
| 116 | } SinglePrecision; |
| 117 | |
| 118 | register unsigned int |
| 119 | exponent, |
| 120 | significand, |
| 121 | sign_bit; |
| 122 | |
| 123 | SinglePrecision |
| 124 | map; |
| 125 | |
| 126 | unsigned int |
| 127 | value; |
| 128 | |
| 129 | /* |
cristy | bccb64f | 2010-01-10 21:16:36 +0000 | [diff] [blame] | 130 | The IEEE 754 standard specifies half precision as having: |
cristy | 24fb7dc | 2010-01-10 20:15:51 +0000 | [diff] [blame] | 131 | |
| 132 | Sign bit: 1 bit |
| 133 | Exponent width: 5 bits |
| 134 | Significand precision: 11 (10 explicitly stored) |
| 135 | */ |
cristy | 2a4d01c | 2010-01-10 21:14:51 +0000 | [diff] [blame] | 136 | sign_bit=(unsigned int) ((half >> 15) & 0x00000001); |
| 137 | exponent=(unsigned int) ((half >> 10) & 0x0000001f); |
| 138 | significand=(unsigned int) (half & 0x000003ff); |
cristy | 24fb7dc | 2010-01-10 20:15:51 +0000 | [diff] [blame] | 139 | if (exponent == 0) |
| 140 | { |
cristy | 92caafa | 2010-05-10 12:56:29 +0000 | [diff] [blame] | 141 | if (significand == 0) |
cristy | 24fb7dc | 2010-01-10 20:15:51 +0000 | [diff] [blame] | 142 | value=sign_bit << SignBitShift; |
cristy | 92caafa | 2010-05-10 12:56:29 +0000 | [diff] [blame] | 143 | else |
| 144 | { |
cristy | 24fb7dc | 2010-01-10 20:15:51 +0000 | [diff] [blame] | 145 | while ((significand & SignificandMask) == 0) |
| 146 | { |
| 147 | significand<<=1; |
| 148 | exponent--; |
| 149 | } |
cristy | 92caafa | 2010-05-10 12:56:29 +0000 | [diff] [blame] | 150 | exponent++; |
cristy | 24fb7dc | 2010-01-10 20:15:51 +0000 | [diff] [blame] | 151 | significand&=(~SignificandMask); |
| 152 | exponent+=ExponentBias; |
| 153 | value=(sign_bit << SignBitShift) | (exponent << ExponentShift) | |
| 154 | (significand << SignificandShift); |
cristy | 92caafa | 2010-05-10 12:56:29 +0000 | [diff] [blame] | 155 | } |
cristy | 24fb7dc | 2010-01-10 20:15:51 +0000 | [diff] [blame] | 156 | } |
| 157 | else |
| 158 | if (exponent == SignBitShift) |
| 159 | { |
| 160 | value=(sign_bit << SignBitShift) | 0x7f800000; |
| 161 | if (significand != 0) |
| 162 | value|=(significand << SignificandShift); |
| 163 | } |
| 164 | else |
| 165 | { |
| 166 | exponent+=ExponentBias; |
| 167 | significand<<=SignificandShift; |
| 168 | value=(sign_bit << SignBitShift) | (exponent << ExponentShift) | |
| 169 | significand; |
| 170 | } |
| 171 | map.fixed_point=value; |
| 172 | return(map.single_precision); |
| 173 | } |
cristy | 5ab5fc8 | 2010-01-05 20:52:48 +0000 | [diff] [blame] | 174 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 175 | static inline unsigned char *PopCharPixel(const unsigned char pixel, |
| 176 | unsigned char *pixels) |
| 177 | { |
| 178 | *pixels++=pixel; |
| 179 | return(pixels); |
| 180 | } |
| 181 | |
| 182 | static inline unsigned char *PopLongPixel(const EndianType endian, |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 183 | const unsigned int pixel,unsigned char *pixels) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 184 | { |
| 185 | register unsigned int |
| 186 | quantum; |
| 187 | |
| 188 | quantum=(unsigned int) pixel; |
cristy | 1715f79 | 2012-11-22 16:56:12 +0000 | [diff] [blame] | 189 | if (endian == LSBEndian) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 190 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 191 | *pixels++=(unsigned char) (quantum); |
cristy | 1715f79 | 2012-11-22 16:56:12 +0000 | [diff] [blame] | 192 | *pixels++=(unsigned char) (quantum >> 8); |
| 193 | *pixels++=(unsigned char) (quantum >> 16); |
| 194 | *pixels++=(unsigned char) (quantum >> 24); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 195 | return(pixels); |
| 196 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 197 | *pixels++=(unsigned char) (quantum >> 24); |
cristy | 1715f79 | 2012-11-22 16:56:12 +0000 | [diff] [blame] | 198 | *pixels++=(unsigned char) (quantum >> 16); |
| 199 | *pixels++=(unsigned char) (quantum >> 8); |
| 200 | *pixels++=(unsigned char) (quantum); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 201 | return(pixels); |
| 202 | } |
| 203 | |
| 204 | static inline unsigned char *PopShortPixel(const EndianType endian, |
| 205 | const unsigned short pixel,unsigned char *pixels) |
| 206 | { |
| 207 | register unsigned int |
| 208 | quantum; |
| 209 | |
| 210 | quantum=pixel; |
cristy | 1715f79 | 2012-11-22 16:56:12 +0000 | [diff] [blame] | 211 | if (endian == LSBEndian) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 212 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 213 | *pixels++=(unsigned char) (quantum); |
cristy | 1715f79 | 2012-11-22 16:56:12 +0000 | [diff] [blame] | 214 | *pixels++=(unsigned char) (quantum >> 8); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 215 | return(pixels); |
| 216 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 217 | *pixels++=(unsigned char) (quantum >> 8); |
cristy | 1715f79 | 2012-11-22 16:56:12 +0000 | [diff] [blame] | 218 | *pixels++=(unsigned char) (quantum); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 219 | return(pixels); |
| 220 | } |
| 221 | |
| 222 | static inline const unsigned char *PushCharPixel(const unsigned char *pixels, |
| 223 | unsigned char *pixel) |
| 224 | { |
| 225 | *pixel=(*pixels++); |
| 226 | return(pixels); |
| 227 | } |
| 228 | |
| 229 | static inline const unsigned char *PushLongPixel(const EndianType endian, |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 230 | const unsigned char *pixels,unsigned int *pixel) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 231 | { |
| 232 | register unsigned int |
| 233 | quantum; |
| 234 | |
cristy | 1715f79 | 2012-11-22 16:56:12 +0000 | [diff] [blame] | 235 | if (endian == LSBEndian) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 236 | { |
cristy | 1715f79 | 2012-11-22 16:56:12 +0000 | [diff] [blame] | 237 | quantum=(unsigned int) (*pixels++); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 238 | quantum|=(unsigned int) (*pixels++ << 8); |
cristy | 1715f79 | 2012-11-22 16:56:12 +0000 | [diff] [blame] | 239 | quantum|=(unsigned int) (*pixels++ << 16); |
| 240 | quantum|=(unsigned int) (*pixels++ << 24); |
cristy | 70d7eae | 2012-11-17 19:40:21 +0000 | [diff] [blame] | 241 | *pixel=(unsigned int) (quantum & 0xffffffff); |
| 242 | return(pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 243 | } |
cristy | 1715f79 | 2012-11-22 16:56:12 +0000 | [diff] [blame] | 244 | quantum=(unsigned int) (*pixels++ << 24); |
cristy | 70d7eae | 2012-11-17 19:40:21 +0000 | [diff] [blame] | 245 | quantum|=(unsigned int) (*pixels++ << 16); |
cristy | 1715f79 | 2012-11-22 16:56:12 +0000 | [diff] [blame] | 246 | quantum|=(unsigned int) (*pixels++ << 8); |
| 247 | quantum|=(unsigned int) (*pixels++); |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 248 | *pixel=(unsigned int) (quantum & 0xffffffff); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 249 | return(pixels); |
| 250 | } |
| 251 | |
| 252 | static inline const unsigned char *PushShortPixel(const EndianType endian, |
| 253 | const unsigned char *pixels,unsigned short *pixel) |
| 254 | { |
| 255 | register unsigned int |
| 256 | quantum; |
| 257 | |
cristy | 1715f79 | 2012-11-22 16:56:12 +0000 | [diff] [blame] | 258 | if (endian == LSBEndian) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 259 | { |
cristy | 1715f79 | 2012-11-22 16:56:12 +0000 | [diff] [blame] | 260 | quantum=(unsigned int) *pixels++; |
| 261 | quantum|=(unsigned int) (*pixels++ << 8); |
cristy | 70d7eae | 2012-11-17 19:40:21 +0000 | [diff] [blame] | 262 | *pixel=(unsigned short) (quantum & 0xffff); |
| 263 | return(pixels); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 264 | } |
cristy | 1715f79 | 2012-11-22 16:56:12 +0000 | [diff] [blame] | 265 | quantum=(unsigned int) (*pixels++ << 8); |
| 266 | quantum|=(unsigned int) *pixels++; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 267 | *pixel=(unsigned short) (quantum & 0xffff); |
| 268 | return(pixels); |
| 269 | } |
| 270 | |
| 271 | static inline Quantum ScaleAnyToQuantum(const QuantumAny quantum, |
| 272 | const QuantumAny range) |
| 273 | { |
cristy | b3963e6 | 2010-05-09 21:36:40 +0000 | [diff] [blame] | 274 | #if !defined(MAGICKCORE_HDRI_SUPPORT) |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 275 | return((Quantum) (((double) QuantumRange*quantum)/range+0.5)); |
cristy | b3963e6 | 2010-05-09 21:36:40 +0000 | [diff] [blame] | 276 | #else |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 277 | return((Quantum) (((double) QuantumRange*quantum)/range)); |
cristy | b3963e6 | 2010-05-09 21:36:40 +0000 | [diff] [blame] | 278 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 279 | } |
| 280 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 281 | static inline QuantumAny ScaleQuantumToAny(const Quantum quantum, |
| 282 | const QuantumAny range) |
| 283 | { |
cristy | a19f1d7 | 2012-08-07 18:24:38 +0000 | [diff] [blame] | 284 | return((QuantumAny) (((double) range*quantum)/QuantumRange+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | #if (MAGICKCORE_QUANTUM_DEPTH == 8) |
| 288 | static inline Quantum ScaleCharToQuantum(const unsigned char value) |
| 289 | { |
| 290 | return((Quantum) value); |
| 291 | } |
| 292 | |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 293 | static inline Quantum ScaleLongToQuantum(const unsigned int value) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 294 | { |
| 295 | #if !defined(MAGICKCORE_HDRI_SUPPORT) |
| 296 | return((Quantum) ((value+8421504UL)/16843009UL)); |
| 297 | #else |
| 298 | return((Quantum) (value/16843009.0)); |
| 299 | #endif |
| 300 | } |
| 301 | |
cristy | b13e12a | 2012-01-06 21:48:27 +0000 | [diff] [blame] | 302 | static inline Quantum ScaleLongLongToQuantum(const MagickSizeType value) |
| 303 | { |
| 304 | #if !defined(MAGICKCORE_HDRI_SUPPORT) |
| 305 | return((Quantum) ((value+MagickULLConstant(551911719039))/ |
| 306 | MagickULLConstant(1103823438079))); |
| 307 | #else |
| 308 | return((Quantum) (value/1103823438079.0)); |
| 309 | #endif |
| 310 | } |
| 311 | |
cristy | bce4f4a | 2012-10-14 14:57:47 +0000 | [diff] [blame] | 312 | static inline Quantum ScaleMapToQuantum(const MagickRealType value) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 313 | { |
cristy | a45da3b | 2010-03-31 02:06:45 +0000 | [diff] [blame] | 314 | if (value <= 0.0) |
| 315 | return((Quantum) 0); |
cristy | a45da3b | 2010-03-31 02:06:45 +0000 | [diff] [blame] | 316 | if (value >= MaxMap) |
cristy | 6e963d8 | 2012-06-19 15:23:24 +0000 | [diff] [blame] | 317 | return(QuantumRange); |
cristy | dd2fd9f | 2010-05-10 01:43:37 +0000 | [diff] [blame] | 318 | #if !defined(MAGICKCORE_HDRI_SUPPORT) |
| 319 | return((Quantum) (value+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 320 | #else |
cristy | dd2fd9f | 2010-05-10 01:43:37 +0000 | [diff] [blame] | 321 | return((Quantum) value); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 322 | #endif |
| 323 | } |
| 324 | |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 325 | static inline unsigned int ScaleQuantumToLong(const Quantum quantum) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 326 | { |
| 327 | #if !defined(MAGICKCORE_HDRI_SUPPORT) |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 328 | return((unsigned int) (16843009UL*quantum)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 329 | #else |
| 330 | if (quantum <= 0.0) |
| 331 | return(0UL); |
| 332 | if ((16843009.0*quantum) >= 4294967295.0) |
| 333 | return(4294967295UL); |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 334 | return((unsigned int) (16843009.0*quantum+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 335 | #endif |
| 336 | } |
| 337 | |
cristy | b13e12a | 2012-01-06 21:48:27 +0000 | [diff] [blame] | 338 | static inline MagickSizeType ScaleQuantumToLongLong(const Quantum quantum) |
| 339 | { |
| 340 | #if !defined(MAGICKCORE_HDRI_SUPPORT) |
| 341 | return((MagickSizeType) (MagickULLConstant(551911719039)*quantum)); |
| 342 | #else |
| 343 | if (quantum <= 0.0) |
| 344 | return(0UL); |
| 345 | if ((551911719039.0*quantum) >= 18446744073709551615.0) |
| 346 | return(MagickULLConstant(18446744073709551615)); |
| 347 | return((MagickSizeType) (1103823438079.0*quantum+0.5)); |
| 348 | #endif |
| 349 | } |
| 350 | |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 351 | static inline unsigned int ScaleQuantumToMap(const Quantum quantum) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 352 | { |
| 353 | if (quantum >= (Quantum) MaxMap) |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 354 | return((unsigned int) MaxMap); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 355 | #if !defined(MAGICKCORE_HDRI_SUPPORT) |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 356 | return((unsigned int) quantum); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 357 | #else |
cristy | dd2fd9f | 2010-05-10 01:43:37 +0000 | [diff] [blame] | 358 | if (quantum < 0.0) |
| 359 | return(0UL); |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 360 | return((unsigned int) (quantum+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 361 | #endif |
| 362 | } |
| 363 | |
| 364 | static inline unsigned short ScaleQuantumToShort(const Quantum quantum) |
| 365 | { |
| 366 | #if !defined(MAGICKCORE_HDRI_SUPPORT) |
| 367 | return((unsigned short) (257UL*quantum)); |
| 368 | #else |
| 369 | if (quantum <= 0.0) |
| 370 | return(0); |
cristy | cd817db | 2010-05-09 03:05:41 +0000 | [diff] [blame] | 371 | if ((257.0*quantum) >= 65535.0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 372 | return(65535); |
cristy | cd817db | 2010-05-09 03:05:41 +0000 | [diff] [blame] | 373 | return((unsigned short) (257.0*quantum+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 374 | #endif |
| 375 | } |
| 376 | |
| 377 | static inline Quantum ScaleShortToQuantum(const unsigned short value) |
| 378 | { |
| 379 | #if !defined(MAGICKCORE_HDRI_SUPPORT) |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 380 | return((Quantum) ((value+128U)/257U)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 381 | #else |
cristy | dd2fd9f | 2010-05-10 01:43:37 +0000 | [diff] [blame] | 382 | return((Quantum) (value/257.0)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 383 | #endif |
| 384 | } |
| 385 | #elif (MAGICKCORE_QUANTUM_DEPTH == 16) |
| 386 | static inline Quantum ScaleCharToQuantum(const unsigned char value) |
| 387 | { |
| 388 | #if !defined(MAGICKCORE_HDRI_SUPPORT) |
cristy | e90d740 | 2010-03-14 18:21:29 +0000 | [diff] [blame] | 389 | return((Quantum) (257U*value)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 390 | #else |
cristy | dd2fd9f | 2010-05-10 01:43:37 +0000 | [diff] [blame] | 391 | return((Quantum) (257.0*value)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 392 | #endif |
| 393 | } |
| 394 | |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 395 | static inline Quantum ScaleLongToQuantum(const unsigned int value) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 396 | { |
| 397 | #if !defined(MAGICKCORE_HDRI_SUPPORT) |
| 398 | return((Quantum) ((value+MagickULLConstant(32768))/ |
| 399 | MagickULLConstant(65537))); |
| 400 | #else |
cristy | dd2fd9f | 2010-05-10 01:43:37 +0000 | [diff] [blame] | 401 | return((Quantum) (value/65537.0)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 402 | #endif |
| 403 | } |
| 404 | |
cristy | b13e12a | 2012-01-06 21:48:27 +0000 | [diff] [blame] | 405 | static inline Quantum ScaleLongLongToQuantum(const MagickSizeType value) |
| 406 | { |
| 407 | #if !defined(MAGICKCORE_HDRI_SUPPORT) |
| 408 | return((Quantum) ((value+MagickULLConstant(8421376))/ |
| 409 | MagickULLConstant(16842752))); |
| 410 | #else |
| 411 | return((Quantum) (value/16842752.0)); |
| 412 | #endif |
| 413 | } |
| 414 | |
cristy | bce4f4a | 2012-10-14 14:57:47 +0000 | [diff] [blame] | 415 | static inline Quantum ScaleMapToQuantum(const MagickRealType value) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 416 | { |
cristy | a45da3b | 2010-03-31 02:06:45 +0000 | [diff] [blame] | 417 | if (value <= 0.0) |
| 418 | return((Quantum) 0); |
cristy | a45da3b | 2010-03-31 02:06:45 +0000 | [diff] [blame] | 419 | if (value >= MaxMap) |
cristy | 6e963d8 | 2012-06-19 15:23:24 +0000 | [diff] [blame] | 420 | return(QuantumRange); |
cristy | dd2fd9f | 2010-05-10 01:43:37 +0000 | [diff] [blame] | 421 | #if !defined(MAGICKCORE_HDRI_SUPPORT) |
| 422 | return((Quantum) (value+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 423 | #else |
cristy | dd2fd9f | 2010-05-10 01:43:37 +0000 | [diff] [blame] | 424 | return((Quantum) value); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 425 | #endif |
| 426 | } |
| 427 | |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 428 | static inline unsigned int ScaleQuantumToLong(const Quantum quantum) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 429 | { |
| 430 | #if !defined(MAGICKCORE_HDRI_SUPPORT) |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 431 | return((unsigned int) (65537UL*quantum)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 432 | #else |
| 433 | if (quantum <= 0.0) |
| 434 | return(0UL); |
cristy | cd817db | 2010-05-09 03:05:41 +0000 | [diff] [blame] | 435 | if ((65537.0*quantum) >= 4294967295.0) |
cristy | 9f48ca6 | 2010-11-25 03:06:31 +0000 | [diff] [blame] | 436 | return(4294967295U); |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 437 | return((unsigned int) (65537.0*quantum+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 438 | #endif |
| 439 | } |
| 440 | |
cristy | b13e12a | 2012-01-06 21:48:27 +0000 | [diff] [blame] | 441 | static inline MagickSizeType ScaleQuantumToLongLong(const Quantum quantum) |
| 442 | { |
| 443 | #if !defined(MAGICKCORE_HDRI_SUPPORT) |
| 444 | return((MagickSizeType) (MagickULLConstant(16842752)*quantum)); |
| 445 | #else |
| 446 | if (quantum <= 0.0) |
| 447 | return(0UL); |
| 448 | if ((65537.0*quantum) >= 18446744073709551615.0) |
| 449 | return(MagickULLConstant(18446744073709551615)); |
| 450 | return((MagickSizeType) (16842752.0*quantum+0.5)); |
| 451 | #endif |
| 452 | } |
| 453 | |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 454 | static inline unsigned int ScaleQuantumToMap(const Quantum quantum) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 455 | { |
| 456 | if (quantum >= (Quantum) MaxMap) |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 457 | return((unsigned int) MaxMap); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 458 | #if !defined(MAGICKCORE_HDRI_SUPPORT) |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 459 | return((unsigned int) quantum); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 460 | #else |
cristy | a830757 | 2010-03-31 14:24:19 +0000 | [diff] [blame] | 461 | if (quantum < 0.0) |
| 462 | return(0UL); |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 463 | return((unsigned int) (quantum+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 464 | #endif |
| 465 | } |
| 466 | |
| 467 | static inline unsigned short ScaleQuantumToShort(const Quantum quantum) |
| 468 | { |
| 469 | #if !defined(MAGICKCORE_HDRI_SUPPORT) |
| 470 | return((unsigned short) quantum); |
| 471 | #else |
| 472 | if (quantum <= 0.0) |
| 473 | return(0); |
| 474 | if (quantum >= 65535.0) |
| 475 | return(65535); |
| 476 | return((unsigned short) (quantum+0.5)); |
| 477 | #endif |
| 478 | } |
| 479 | |
| 480 | static inline Quantum ScaleShortToQuantum(const unsigned short value) |
| 481 | { |
| 482 | return((Quantum) value); |
| 483 | } |
| 484 | #elif (MAGICKCORE_QUANTUM_DEPTH == 32) |
| 485 | static inline Quantum ScaleCharToQuantum(const unsigned char value) |
| 486 | { |
| 487 | #if !defined(MAGICKCORE_HDRI_SUPPORT) |
| 488 | return((Quantum) (16843009UL*value)); |
| 489 | #else |
cristy | dd2fd9f | 2010-05-10 01:43:37 +0000 | [diff] [blame] | 490 | return((Quantum) (16843009.0*value)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 491 | #endif |
| 492 | } |
| 493 | |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 494 | static inline Quantum ScaleLongToQuantum(const unsigned int value) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 495 | { |
| 496 | return((Quantum) value); |
| 497 | } |
| 498 | |
cristy | b13e12a | 2012-01-06 21:48:27 +0000 | [diff] [blame] | 499 | static inline Quantum ScaleLongLongToQuantum(const MagickSizeType value) |
| 500 | { |
| 501 | return((Quantum) value); |
| 502 | } |
| 503 | |
cristy | bce4f4a | 2012-10-14 14:57:47 +0000 | [diff] [blame] | 504 | static inline Quantum ScaleMapToQuantum(const MagickRealType value) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 505 | { |
cristy | a45da3b | 2010-03-31 02:06:45 +0000 | [diff] [blame] | 506 | if (value <= 0.0) |
| 507 | return((Quantum) 0); |
cristy | dd2fd9f | 2010-05-10 01:43:37 +0000 | [diff] [blame] | 508 | if (value >= (Quantum) MaxMap) |
cristy | a45da3b | 2010-03-31 02:06:45 +0000 | [diff] [blame] | 509 | return(QuantumRange); |
cristy | dd2fd9f | 2010-05-10 01:43:37 +0000 | [diff] [blame] | 510 | #if !defined(MAGICKCORE_HDRI_SUPPORT) |
| 511 | return((Quantum) (65537.0*value+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 512 | #else |
cristy | dd2fd9f | 2010-05-10 01:43:37 +0000 | [diff] [blame] | 513 | return((Quantum) (65537.0*value)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 514 | #endif |
| 515 | } |
| 516 | |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 517 | static inline unsigned int ScaleQuantumToLong(const Quantum quantum) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 518 | { |
cristy | 0402ad3 | 2010-05-11 13:57:55 +0000 | [diff] [blame] | 519 | #if !defined(MAGICKCORE_HDRI_SUPPORT) |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 520 | return((unsigned int) quantum); |
cristy | 0402ad3 | 2010-05-11 13:57:55 +0000 | [diff] [blame] | 521 | #else |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 522 | return((unsigned int) (quantum+0.5)); |
cristy | 0402ad3 | 2010-05-11 13:57:55 +0000 | [diff] [blame] | 523 | #endif |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 524 | } |
| 525 | |
cristy | b13e12a | 2012-01-06 21:48:27 +0000 | [diff] [blame] | 526 | static inline MagickSizeType ScaleQuantumToLongLong(const Quantum quantum) |
| 527 | { |
| 528 | #if !defined(MAGICKCORE_HDRI_SUPPORT) |
| 529 | return((MagickSizeType) quantum); |
| 530 | #else |
| 531 | return((MagickSizeType) (quantum+0.5)); |
| 532 | #endif |
| 533 | } |
| 534 | |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 535 | static inline unsigned int ScaleQuantumToMap(const Quantum quantum) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 536 | { |
cristy | a45da3b | 2010-03-31 02:06:45 +0000 | [diff] [blame] | 537 | if (quantum < 0.0) |
| 538 | return(0UL); |
cristy | dd2fd9f | 2010-05-10 01:43:37 +0000 | [diff] [blame] | 539 | if ((quantum/65537) >= (Quantum) MaxMap) |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 540 | return((unsigned int) MaxMap); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 541 | #if !defined(MAGICKCORE_HDRI_SUPPORT) |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 542 | return((unsigned int) ((quantum+MagickULLConstant(32768))/ |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 543 | MagickULLConstant(65537))); |
| 544 | #else |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 545 | return((unsigned int) (quantum/65537.0+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 546 | #endif |
| 547 | } |
| 548 | |
| 549 | static inline unsigned short ScaleQuantumToShort(const Quantum quantum) |
| 550 | { |
| 551 | #if !defined(MAGICKCORE_HDRI_SUPPORT) |
| 552 | return((unsigned short) ((quantum+MagickULLConstant(32768))/ |
| 553 | MagickULLConstant(65537))); |
| 554 | #else |
| 555 | if (quantum <= 0.0) |
| 556 | return(0); |
cristy | cd817db | 2010-05-09 03:05:41 +0000 | [diff] [blame] | 557 | if ((quantum/65537.0) >= 65535.0) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 558 | return(65535); |
cristy | cd817db | 2010-05-09 03:05:41 +0000 | [diff] [blame] | 559 | return((unsigned short) (quantum/65537.0+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 560 | #endif |
| 561 | } |
| 562 | |
| 563 | static inline Quantum ScaleShortToQuantum(const unsigned short value) |
| 564 | { |
| 565 | #if !defined(MAGICKCORE_HDRI_SUPPORT) |
| 566 | return((Quantum) (65537UL*value)); |
| 567 | #else |
cristy | 41f82a6 | 2010-05-10 13:05:18 +0000 | [diff] [blame] | 568 | return((Quantum) (65537.0*value)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 569 | #endif |
| 570 | } |
| 571 | #elif (MAGICKCORE_QUANTUM_DEPTH == 64) |
| 572 | static inline Quantum ScaleCharToQuantum(const unsigned char value) |
| 573 | { |
cristy | dd2fd9f | 2010-05-10 01:43:37 +0000 | [diff] [blame] | 574 | return((Quantum) (72340172838076673.0*value)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 575 | } |
| 576 | |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 577 | static inline Quantum ScaleLongToQuantum(const unsigned int value) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 578 | { |
cristy | ff2c025 | 2010-05-11 14:00:48 +0000 | [diff] [blame] | 579 | return((Quantum) (4294967297.0*value)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 580 | } |
| 581 | |
cristy | b13e12a | 2012-01-06 21:48:27 +0000 | [diff] [blame] | 582 | static inline Quantum ScaleLongLongToQuantum(const MagickSizeType value) |
| 583 | { |
| 584 | return((Quantum) (18446744073709551615.0*value)); |
| 585 | } |
| 586 | |
cristy | bce4f4a | 2012-10-14 14:57:47 +0000 | [diff] [blame] | 587 | static inline Quantum ScaleMapToQuantum(const MagickRealType value) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 588 | { |
cristy | a45da3b | 2010-03-31 02:06:45 +0000 | [diff] [blame] | 589 | if (value <= 0.0) |
| 590 | return((Quantum) 0); |
cristy | a45da3b | 2010-03-31 02:06:45 +0000 | [diff] [blame] | 591 | if (value >= MaxMap) |
| 592 | return(QuantumRange); |
cristy | dd2fd9f | 2010-05-10 01:43:37 +0000 | [diff] [blame] | 593 | return((Quantum) (281479271743489.0*value)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 594 | } |
| 595 | |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 596 | static inline unsigned int ScaleQuantumToLong(const Quantum quantum) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 597 | { |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 598 | return((unsigned int) (quantum/4294967297.0+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 599 | } |
| 600 | |
cristy | b13e12a | 2012-01-06 21:48:27 +0000 | [diff] [blame] | 601 | static inline MagickSizeType ScaleQuantumToLongLong(const Quantum quantum) |
| 602 | { |
| 603 | return((MagickSizeType) (quantum/18446744073709551615.0+0.5)); |
| 604 | } |
| 605 | |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 606 | static inline unsigned int ScaleQuantumToMap(const Quantum quantum) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 607 | { |
cristy | 41f82a6 | 2010-05-10 13:05:18 +0000 | [diff] [blame] | 608 | if (quantum <= 0.0) |
cristy | dd2fd9f | 2010-05-10 01:43:37 +0000 | [diff] [blame] | 609 | return(0UL); |
cristy | 4351352 | 2010-05-11 13:46:36 +0000 | [diff] [blame] | 610 | if ((quantum/281479271743489.0) >= MaxMap) |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 611 | return((unsigned int) MaxMap); |
| 612 | return((unsigned int) (quantum/281479271743489.0+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | static inline unsigned short ScaleQuantumToShort(const Quantum quantum) |
| 616 | { |
cristy | 5f54f2b | 2010-05-11 14:29:17 +0000 | [diff] [blame] | 617 | if (quantum <= 0.0) |
| 618 | return(0); |
cristy | f4ad8d9 | 2010-05-12 17:58:41 +0000 | [diff] [blame] | 619 | if ((quantum/281479271743489.0) >= 65535.0) |
cristy | 5f54f2b | 2010-05-11 14:29:17 +0000 | [diff] [blame] | 620 | return(65535); |
cristy | 4351352 | 2010-05-11 13:46:36 +0000 | [diff] [blame] | 621 | return((unsigned short) (quantum/281479271743489.0+0.5)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | static inline Quantum ScaleShortToQuantum(const unsigned short value) |
| 625 | { |
cristy | b3963e6 | 2010-05-09 21:36:40 +0000 | [diff] [blame] | 626 | return((Quantum) (281479271743489.0*value)); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 627 | } |
| 628 | #endif |
| 629 | |
cristy | 2a4d01c | 2010-01-10 21:14:51 +0000 | [diff] [blame] | 630 | static inline unsigned short SinglePrecisionToHalf(const float value) |
cristy | 24fb7dc | 2010-01-10 20:15:51 +0000 | [diff] [blame] | 631 | { |
| 632 | typedef union _SinglePrecision |
| 633 | { |
| 634 | unsigned int |
| 635 | fixed_point; |
| 636 | |
| 637 | float |
| 638 | single_precision; |
| 639 | } SinglePrecision; |
| 640 | |
| 641 | register int |
| 642 | exponent; |
| 643 | |
| 644 | register unsigned int |
| 645 | significand, |
| 646 | sign_bit; |
| 647 | |
| 648 | SinglePrecision |
| 649 | map; |
| 650 | |
| 651 | unsigned short |
cristy | 2a4d01c | 2010-01-10 21:14:51 +0000 | [diff] [blame] | 652 | half; |
cristy | 24fb7dc | 2010-01-10 20:15:51 +0000 | [diff] [blame] | 653 | |
| 654 | /* |
cristy | bccb64f | 2010-01-10 21:16:36 +0000 | [diff] [blame] | 655 | The IEEE 754 standard specifies half precision as having: |
cristy | 24fb7dc | 2010-01-10 20:15:51 +0000 | [diff] [blame] | 656 | |
| 657 | Sign bit: 1 bit |
| 658 | Exponent width: 5 bits |
| 659 | Significand precision: 11 (10 explicitly stored) |
| 660 | */ |
| 661 | map.single_precision=value; |
| 662 | sign_bit=(map.fixed_point >> 16) & 0x00008000; |
| 663 | exponent=(int) ((map.fixed_point >> ExponentShift) & 0x000000ff)-ExponentBias; |
| 664 | significand=map.fixed_point & 0x007fffff; |
| 665 | if (exponent <= 0) |
| 666 | { |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 667 | int |
cristy | 24fb7dc | 2010-01-10 20:15:51 +0000 | [diff] [blame] | 668 | shift; |
| 669 | |
| 670 | if (exponent < -10) |
| 671 | return((unsigned short) sign_bit); |
| 672 | significand=significand | 0x00800000; |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 673 | shift=(int) (14-exponent); |
cristy | 24fb7dc | 2010-01-10 20:15:51 +0000 | [diff] [blame] | 674 | significand=(unsigned int) ((significand+((1 << (shift-1))-1)+ |
| 675 | ((significand >> shift) & 0x01)) >> shift); |
| 676 | return((unsigned short) (sign_bit | significand)); |
| 677 | } |
| 678 | else |
| 679 | if (exponent == (0xff-ExponentBias)) |
| 680 | { |
| 681 | if (significand == 0) |
| 682 | return((unsigned short) (sign_bit | ExponentMask)); |
| 683 | else |
| 684 | { |
| 685 | significand>>=SignificandShift; |
cristy | 2a4d01c | 2010-01-10 21:14:51 +0000 | [diff] [blame] | 686 | half=(unsigned short) (sign_bit | significand | |
cristy | 24fb7dc | 2010-01-10 20:15:51 +0000 | [diff] [blame] | 687 | (significand == 0) | ExponentMask); |
cristy | 2a4d01c | 2010-01-10 21:14:51 +0000 | [diff] [blame] | 688 | return(half); |
cristy | 24fb7dc | 2010-01-10 20:15:51 +0000 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | significand=significand+((significand >> SignificandShift) & 0x01)+0x00000fff; |
| 692 | if ((significand & 0x00800000) != 0) |
| 693 | { |
| 694 | significand=0; |
| 695 | exponent++; |
| 696 | } |
| 697 | if (exponent > 30) |
| 698 | { |
| 699 | float |
| 700 | alpha; |
| 701 | |
cristy | 4cb162a | 2010-05-30 03:04:47 +0000 | [diff] [blame] | 702 | register int |
cristy | 24fb7dc | 2010-01-10 20:15:51 +0000 | [diff] [blame] | 703 | i; |
| 704 | |
| 705 | /* |
| 706 | Float overflow. |
| 707 | */ |
| 708 | alpha=1.0e10; |
| 709 | for (i=0; i < 10; i++) |
| 710 | alpha*=alpha; |
| 711 | return((unsigned short) (sign_bit | ExponentMask)); |
| 712 | } |
cristy | 2a4d01c | 2010-01-10 21:14:51 +0000 | [diff] [blame] | 713 | half=(unsigned short) (sign_bit | (exponent << 10) | |
cristy | 24fb7dc | 2010-01-10 20:15:51 +0000 | [diff] [blame] | 714 | (significand >> SignificandShift)); |
cristy | 2a4d01c | 2010-01-10 21:14:51 +0000 | [diff] [blame] | 715 | return(half); |
cristy | 24fb7dc | 2010-01-10 20:15:51 +0000 | [diff] [blame] | 716 | } |
| 717 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 718 | #if defined(__cplusplus) || defined(c_plusplus) |
| 719 | } |
| 720 | #endif |
| 721 | |
| 722 | #endif |