MagickCore 7.1.2-37
Convert, Edit, Or Compose Bitmap Images
Loading...
Searching...
No Matches
quantum-private.h
1/*
2 Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization
3 dedicated to making software imaging solutions freely available.
4
5 You may not use this file except in compliance with the License. You may
6 obtain a copy of the License at
7
8 https://imagemagick.org/license/
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
21#include <stddef.h>
22#include "MagickCore/memory_.h"
23#include "MagickCore/cache.h"
24#include "MagickCore/image-private.h"
25#include "MagickCore/pixel-accessor.h"
26#include "MagickCore/statistic-private.h"
27
28#if defined(__cplusplus) || defined(c_plusplus)
29extern "C" {
30#endif
31
32#define MagickMax(x,y) (((x) > (y)) ? (x) : (y))
33#define MagickMin(x,y) (((x) < (y)) ? (x) : (y))
34
35typedef struct _QuantumState
36{
37 double
38 inverse_scale;
39
40 unsigned int
41 pixel;
42
43 size_t
44 bits;
45
46 const unsigned int
47 *mask;
48} QuantumState;
49
51{
52 size_t
53 depth,
54 quantum;
55
56 QuantumFormatType
57 format;
58
59 double
60 minimum,
61 maximum,
62 scale;
63
64 size_t
65 pad;
66
67 MagickBooleanType
68 min_is_white,
69 pack;
70
71 QuantumAlphaType
72 alpha_type;
73
74 size_t
75 number_threads;
76
77 MemoryInfo
78 **pixels;
79
80 size_t
81 extent;
82
83 EndianType
84 endian;
85
86 QuantumState
87 state;
88
90 *semaphore;
91
92 size_t
93 signature;
94
95 size_t
96 meta_channel;
97};
98
99extern MagickExport MagickBooleanType
100 SetQuantumExtent(const Image *,QuantumInfo *);
101
102extern MagickPrivate void
103 ResetQuantumState(QuantumInfo *);
104
105static inline MagickSizeType GetQuantumRange(const size_t depth)
106{
107 MagickSizeType
108 one;
109
110 size_t
111 max_depth;
112
113 if (depth == 0)
114 return(0);
115 one=1;
116 max_depth=8*sizeof(MagickSizeType);
117 return((MagickSizeType) ((one << (MagickMin(depth,max_depth)-1))+
118 ((one << (MagickMin(depth,max_depth)-1))-1)));
119}
120
121static inline EndianType GetHostEndian(void)
122{
123 unsigned long
124 lsb_first;
125
126 lsb_first=1;
127 return((*(char *) &lsb_first) == 1 ? LSBEndian : MSBEndian);
128}
129
130static inline float HalfToSinglePrecision(const unsigned short half)
131{
132#define ExponentBias (127-15)
133#define ExponentMask (0x7c00U)
134#define ExponentShift 23
135#define SignBitShift 31
136#define SignificandShift 13
137#define SignificandMask (0x00000400U)
138
139 typedef union _SinglePrecision
140 {
141 unsigned int
142 fixed_point;
143
144 float
145 single_precision;
146 } SinglePrecision;
147
148 SinglePrecision
149 map;
150
151 unsigned int
152 exponent,
153 significand,
154 sign_bit,
155 value;
156
157 /*
158 The IEEE 754 standard specifies half precision as having:
159
160 Sign bit: 1 bit
161 Exponent width: 5 bits
162 Significand precision: 11 (10 explicitly stored)
163 */
164 sign_bit=(unsigned int) ((half >> 15) & 0x00000001);
165 exponent=(unsigned int) ((half >> 10) & 0x0000001f);
166 significand=(unsigned int) (half & 0x000003ff);
167 if (exponent == 0)
168 {
169 if (significand == 0)
170 value=sign_bit << SignBitShift;
171 else
172 {
173 while ((significand & SignificandMask) == 0)
174 {
175 significand<<=1;
176 exponent--;
177 }
178 exponent++;
179 significand&=(~SignificandMask);
180 exponent+=ExponentBias;
181 value=(sign_bit << SignBitShift) | (exponent << ExponentShift) |
182 (significand << SignificandShift);
183 }
184 }
185 else
186 if (exponent == SignBitShift)
187 {
188 value=(sign_bit << SignBitShift) | 0x7f800000;
189 if (significand != 0)
190 value|=(significand << SignificandShift);
191 }
192 else
193 {
194 exponent+=ExponentBias;
195 significand<<=SignificandShift;
196 value=(sign_bit << SignBitShift) | (exponent << ExponentShift) |
197 significand;
198 }
199 map.fixed_point=value;
200 return(map.single_precision);
201}
202
203static inline unsigned char *PopCharPixel(const unsigned char pixel,
204 unsigned char *magick_restrict pixels)
205{
206 *pixels++=pixel;
207 return(pixels);
208}
209
210static inline unsigned char *PopLongPixel(const EndianType endian,
211 const unsigned int pixel,unsigned char *magick_restrict pixels)
212{
213 unsigned int
214 quantum;
215
216 quantum=(unsigned int) pixel;
217 if (endian == LSBEndian)
218 {
219 *pixels++=(unsigned char) (quantum);
220 *pixels++=(unsigned char) (quantum >> 8);
221 *pixels++=(unsigned char) (quantum >> 16);
222 *pixels++=(unsigned char) (quantum >> 24);
223 return(pixels);
224 }
225 *pixels++=(unsigned char) (quantum >> 24);
226 *pixels++=(unsigned char) (quantum >> 16);
227 *pixels++=(unsigned char) (quantum >> 8);
228 *pixels++=(unsigned char) (quantum);
229 return(pixels);
230}
231
232static inline unsigned char *PopLongLongPixel(const EndianType endian,
233 const MagickSizeType pixel,unsigned char *magick_restrict pixels)
234{
235 if (endian == LSBEndian)
236 {
237 pixels=PopLongPixel(endian,(unsigned int) pixel,pixels);
238 return(PopLongPixel(endian,(unsigned int) (pixel >> 32),pixels));
239 }
240 pixels=PopLongPixel(endian,(unsigned int) (pixel >> 32),pixels);
241 return(PopLongPixel(endian,(unsigned int) pixel,pixels));
242}
243
244static inline unsigned char *PopShortPixel(const EndianType endian,
245 const unsigned short pixel,unsigned char *magick_restrict pixels)
246{
247 unsigned int
248 quantum;
249
250 quantum=pixel;
251 if (endian == LSBEndian)
252 {
253 *pixels++=(unsigned char) (quantum);
254 *pixels++=(unsigned char) (quantum >> 8);
255 return(pixels);
256 }
257 *pixels++=(unsigned char) (quantum >> 8);
258 *pixels++=(unsigned char) (quantum);
259 return(pixels);
260}
261
262static inline const unsigned char *PushCharPixel(
263 const unsigned char *magick_restrict pixels,
264 unsigned char *magick_restrict pixel)
265{
266 *pixel=(*pixels++);
267 return(pixels);
268}
269
270static inline const unsigned char *PushLongPixel(const EndianType endian,
271 const unsigned char *magick_restrict pixels,
272 unsigned int *magick_restrict pixel)
273{
274 unsigned int
275 quantum;
276
277 if (endian == LSBEndian)
278 {
279 quantum=((unsigned int) *pixels++);
280 quantum|=((unsigned int) *pixels++ << 8);
281 quantum|=((unsigned int) *pixels++ << 16);
282 quantum|=((unsigned int) *pixels++ << 24);
283 *pixel=quantum;
284 return(pixels);
285 }
286 quantum=((unsigned int) *pixels++ << 24);
287 quantum|=((unsigned int) *pixels++ << 16);
288 quantum|=((unsigned int) *pixels++ << 8);
289 quantum|=((unsigned int) *pixels++);
290 *pixel=quantum;
291 return(pixels);
292}
293
294static inline const unsigned char *PushLongLongPixel(const EndianType endian,
295 const unsigned char *magick_restrict pixels,
296 MagickSizeType *magick_restrict pixel)
297{
298 unsigned int
299 high,
300 low;
301
302 if (endian == LSBEndian)
303 {
304 pixels=PushLongPixel(endian,pixels,&low);
305 pixels=PushLongPixel(endian,pixels,&high);
306 }
307 else
308 {
309 pixels=PushLongPixel(endian,pixels,&high);
310 pixels=PushLongPixel(endian,pixels,&low);
311 }
312 *pixel=((MagickSizeType) high << 32) | (MagickSizeType) low;
313 return(pixels);
314}
315
316static inline const unsigned char *PushShortPixel(const EndianType endian,
317 const unsigned char *magick_restrict pixels,
318 unsigned short *magick_restrict pixel)
319{
320 unsigned int
321 quantum;
322
323 if (endian == LSBEndian)
324 {
325 quantum=(unsigned int) *pixels++;
326 quantum|=(unsigned int) (*pixels++ << 8);
327 *pixel=(unsigned short) (quantum & 0xffff);
328 return(pixels);
329 }
330 quantum=(unsigned int) (*pixels++ << 8);
331 quantum|=(unsigned int) *pixels++;
332 *pixel=(unsigned short) (quantum & 0xffff);
333 return(pixels);
334}
335
336static inline const unsigned char *PushFloatPixel(const EndianType endian,
337 const unsigned char *magick_restrict pixels,
338 MagickFloatType *magick_restrict pixel)
339{
340 union
341 {
342 unsigned int
343 unsigned_value;
344
345 MagickFloatType
346 float_value;
347 } quantum;
348
349 if (endian == LSBEndian)
350 {
351 quantum.unsigned_value=((unsigned int) *pixels++);
352 quantum.unsigned_value|=((unsigned int) *pixels++ << 8);
353 quantum.unsigned_value|=((unsigned int) *pixels++ << 16);
354 quantum.unsigned_value|=((unsigned int) *pixels++ << 24);
355 *pixel=quantum.float_value;
356 return(pixels);
357 }
358 quantum.unsigned_value=((unsigned int) *pixels++ << 24);
359 quantum.unsigned_value|=((unsigned int) *pixels++ << 16);
360 quantum.unsigned_value|=((unsigned int) *pixels++ << 8);
361 quantum.unsigned_value|=((unsigned int) *pixels++);
362 *pixel=quantum.float_value;
363 return(pixels);
364}
365
366static inline Quantum ScaleAnyToQuantum(const QuantumAny quantum,
367 const QuantumAny range)
368{
369 if (quantum > range)
370 return(QuantumRange);
371#if !defined(MAGICKCORE_HDRI_SUPPORT)
372 return((Quantum) ((double) QuantumRange*(quantum*
373 MagickSafeReciprocal((double) range))+0.5));
374#else
375 return((Quantum) ((double) QuantumRange*((double) quantum*
376 MagickSafeReciprocal((double) range))));
377#endif
378}
379
380static inline QuantumAny ScaleQuantumToAny(const Quantum quantum,
381 const QuantumAny range)
382{
383 double
384 value;
385
386#if defined(MAGICKCORE_HDRI_SUPPORT)
387 if ((IsNaN(quantum) != 0) || (quantum <= 0.0f))
388 return((QuantumAny) 0UL);
389#endif
390 if (quantum >= (Quantum) QuantumRange)
391 return(range);
392 value=(double) range*(double) quantum/(double) QuantumRange;
393#if defined(MAGICKCORE_HDRI_SUPPORT)
394 value+=0.5;
395#endif
396 if (value >= (double) range)
397 return(range);
398 return((QuantumAny) value);
399}
400
401#if (MAGICKCORE_QUANTUM_DEPTH == 8)
402static inline Quantum ScaleCharToQuantum(const unsigned char value)
403{
404 return((Quantum) value);
405}
406
407static inline Quantum ScaleLongToQuantum(const unsigned int value)
408{
409#if !defined(MAGICKCORE_HDRI_SUPPORT)
410 return((Quantum) ((value)/16843009UL));
411#else
412 return((Quantum) (value/16843009.0));
413#endif
414}
415
416static inline Quantum ScaleLongLongToQuantum(const MagickSizeType value)
417{
418#if !defined(MAGICKCORE_HDRI_SUPPORT)
419 return((Quantum) (value/MagickULLConstant(72340172838076673)));
420#else
421 return((Quantum) (value/72340172838076673.0));
422#endif
423}
424
425static inline Quantum ScaleMapToQuantum(const MagickRealType value)
426{
427 if (value <= 0.0)
428 return((Quantum) 0);
429 if (value >= MaxMap)
430 return(QuantumRange);
431#if !defined(MAGICKCORE_HDRI_SUPPORT)
432 return((Quantum) (value+0.5));
433#else
434 return((Quantum) value);
435#endif
436}
437
438static inline unsigned int ScaleQuantumToLong(const Quantum quantum)
439{
440#if !defined(MAGICKCORE_HDRI_SUPPORT)
441 return((unsigned int) (16843009UL*quantum));
442#else
443 if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
444 return(0U);
445 if ((16843009.0*quantum) >= 4294967295.0)
446 return(4294967295UL);
447 return((unsigned int) (16843009.0*quantum+0.5));
448#endif
449}
450
451static inline MagickSizeType ScaleQuantumToLongLong(const Quantum quantum)
452{
453#if !defined(MAGICKCORE_HDRI_SUPPORT)
454 return((MagickSizeType) (MagickULLConstant(72340172838076673)*quantum));
455#else
456 if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
457 return(0UL);
458 if ((72340172838076673.0*quantum) >= 18446744073709551615.0)
459 return(MagickULLConstant(18446744073709551615));
460 return((MagickSizeType) (72340172838076673.0*quantum+0.5));
461#endif
462}
463
464static inline unsigned int ScaleQuantumToMap(const Quantum quantum)
465{
466 if (quantum >= (Quantum) MaxMap)
467 return((unsigned int) MaxMap);
468#if !defined(MAGICKCORE_HDRI_SUPPORT)
469 return((unsigned int) quantum);
470#else
471 if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
472 return(0U);
473 return((unsigned int) (quantum+0.5));
474#endif
475}
476
477static inline unsigned short ScaleQuantumToShort(const Quantum quantum)
478{
479#if !defined(MAGICKCORE_HDRI_SUPPORT)
480 return((unsigned short) (257UL*quantum));
481#else
482 if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
483 return(0);
484 if ((257.0*quantum) >= 65535.0)
485 return(65535);
486 return((unsigned short) (257.0*quantum+0.5));
487#endif
488}
489
490static inline Quantum ScaleShortToQuantum(const unsigned short value)
491{
492#if !defined(MAGICKCORE_HDRI_SUPPORT)
493 return((Quantum) ((value+128U)/257U));
494#else
495 return((Quantum) (value/257.0));
496#endif
497}
498#elif (MAGICKCORE_QUANTUM_DEPTH == 16)
499static inline Quantum ScaleCharToQuantum(const unsigned char value)
500{
501#if !defined(MAGICKCORE_HDRI_SUPPORT)
502 return((Quantum) (257U*value));
503#else
504 return((Quantum) (257.0*value));
505#endif
506}
507
508static inline Quantum ScaleLongToQuantum(const unsigned int value)
509{
510#if !defined(MAGICKCORE_HDRI_SUPPORT)
511 return((Quantum) ((value)/MagickULLConstant(65537)));
512#else
513 return((Quantum) (value/65537.0));
514#endif
515}
516
517static inline Quantum ScaleLongLongToQuantum(const MagickSizeType value)
518{
519#if !defined(MAGICKCORE_HDRI_SUPPORT)
520 return((Quantum) ((value)/MagickULLConstant(281479271743489)));
521#else
522 return((Quantum) ((double) value/281479271743489.0));
523#endif
524}
525
526static inline Quantum ScaleMapToQuantum(const MagickRealType value)
527{
528 if (value <= 0.0)
529 return((Quantum) 0);
530 if (value >= MaxMap)
531 return(QuantumRange);
532#if !defined(MAGICKCORE_HDRI_SUPPORT)
533 return((Quantum) (value+0.5));
534#else
535 return((Quantum) value);
536#endif
537}
538
539static inline unsigned int ScaleQuantumToLong(const Quantum quantum)
540{
541#if !defined(MAGICKCORE_HDRI_SUPPORT)
542 return((unsigned int) (65537UL*quantum));
543#else
544 if ((IsNaN(quantum) != 0) || (quantum <= 0.0f))
545 return(0U);
546 if ((65537.0*(double) quantum) >= 4294967295.0)
547 return(4294967295U);
548 return((unsigned int) (65537.0*(double) quantum+0.5));
549#endif
550}
551
552static inline MagickSizeType ScaleQuantumToLongLong(const Quantum quantum)
553{
554#if !defined(MAGICKCORE_HDRI_SUPPORT)
555 return((MagickSizeType) (MagickULLConstant(281479271743489)*quantum));
556#else
557 if ((IsNaN(quantum) != 0) || (quantum <= 0.0f))
558 return(0UL);
559 if ((281479271743489.0*(double) quantum) >= 18446744073709551615.0)
560 return(MagickULLConstant(18446744073709551615));
561 return((MagickSizeType) (281479271743489.0*(double) quantum+0.5));
562#endif
563}
564
565static inline unsigned int ScaleQuantumToMap(const Quantum quantum)
566{
567 if (quantum >= (Quantum) MaxMap)
568 return((unsigned int) MaxMap);
569#if !defined(MAGICKCORE_HDRI_SUPPORT)
570 return((unsigned int) quantum);
571#else
572 if ((IsNaN(quantum) != 0) || (quantum <= 0.0f))
573 return(0U);
574 return((unsigned int) (quantum+0.5f));
575#endif
576}
577
578static inline unsigned short ScaleQuantumToShort(const Quantum quantum)
579{
580#if !defined(MAGICKCORE_HDRI_SUPPORT)
581 return((unsigned short) quantum);
582#else
583 if ((IsNaN(quantum) != 0) || (quantum <= 0.0f))
584 return(0);
585 if (quantum >= 65535.0f)
586 return(65535);
587 return((unsigned short) (quantum+0.5f));
588#endif
589}
590
591static inline Quantum ScaleShortToQuantum(const unsigned short value)
592{
593 return((Quantum) value);
594}
595#elif (MAGICKCORE_QUANTUM_DEPTH == 32)
596static inline Quantum ScaleCharToQuantum(const unsigned char value)
597{
598#if !defined(MAGICKCORE_HDRI_SUPPORT)
599 return((Quantum) (16843009UL*value));
600#else
601 return((Quantum) (16843009.0*value));
602#endif
603}
604
605static inline Quantum ScaleLongToQuantum(const unsigned int value)
606{
607 return((Quantum) value);
608}
609
610static inline Quantum ScaleLongLongToQuantum(const MagickSizeType value)
611{
612#if !defined(MAGICKCORE_HDRI_SUPPORT)
613 return((Quantum) ((value)/MagickULLConstant(4294967297)));
614#else
615 return((Quantum) (value/4294967297.0));
616#endif
617}
618
619static inline Quantum ScaleMapToQuantum(const MagickRealType value)
620{
621 if (value <= 0.0)
622 return((Quantum) 0);
623 if (value >= (Quantum) MaxMap)
624 return(QuantumRange);
625#if !defined(MAGICKCORE_HDRI_SUPPORT)
626 return((Quantum) (65537.0*value+0.5));
627#else
628 return((Quantum) (65537.0*value));
629#endif
630}
631
632static inline unsigned int ScaleQuantumToLong(const Quantum quantum)
633{
634#if !defined(MAGICKCORE_HDRI_SUPPORT)
635 return((unsigned int) quantum);
636#else
637 if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
638 return(0U);
639 if ((quantum) >= 4294967295.0)
640 return(4294967295);
641 return((unsigned int) (quantum+0.5));
642#endif
643}
644
645static inline MagickSizeType ScaleQuantumToLongLong(const Quantum quantum)
646{
647#if !defined(MAGICKCORE_HDRI_SUPPORT)
648 return((MagickSizeType) (MagickULLConstant(4294967297)*quantum));
649#else
650 if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
651 return(0UL);
652 if ((4294967297.0*quantum) >= 18446744073709551615.0)
653 return(MagickULLConstant(18446744073709551615));
654 return((MagickSizeType) (4294967297.0*quantum+0.5));
655#endif
656}
657
658static inline unsigned int ScaleQuantumToMap(const Quantum quantum)
659{
660 if ((quantum/65537) >= (Quantum) MaxMap)
661 return((unsigned int) MaxMap);
662#if !defined(MAGICKCORE_HDRI_SUPPORT)
663 return((unsigned int) ((quantum+MagickULLConstant(32768))/
664 MagickULLConstant(65537)));
665#else
666 if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
667 return(0U);
668 return((unsigned int) (quantum/65537.0+0.5));
669#endif
670}
671
672static inline unsigned short ScaleQuantumToShort(const Quantum quantum)
673{
674#if !defined(MAGICKCORE_HDRI_SUPPORT)
675 return((unsigned short) ((quantum+MagickULLConstant(32768))/
676 MagickULLConstant(65537)));
677#else
678 if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
679 return(0);
680 if ((quantum/65537.0) >= 65535.0)
681 return(65535);
682 return((unsigned short) (quantum/65537.0+0.5));
683#endif
684}
685
686static inline Quantum ScaleShortToQuantum(const unsigned short value)
687{
688#if !defined(MAGICKCORE_HDRI_SUPPORT)
689 return((Quantum) (65537UL*value));
690#else
691 return((Quantum) (65537.0*value));
692#endif
693}
694#elif (MAGICKCORE_QUANTUM_DEPTH == 64)
695static inline Quantum ScaleCharToQuantum(const unsigned char value)
696{
697 return((Quantum) (72340172838076673.0*value));
698}
699
700static inline Quantum ScaleLongToQuantum(const unsigned int value)
701{
702 return((Quantum) (4294967297.0*value));
703}
704
705static inline Quantum ScaleLongLongToQuantum(const MagickSizeType value)
706{
707 return((Quantum) (value));
708}
709
710static inline Quantum ScaleMapToQuantum(const MagickRealType value)
711{
712 if (value <= 0.0)
713 return((Quantum) 0);
714 if (value >= MaxMap)
715 return(QuantumRange);
716 return((Quantum) (281479271743489.0*value));
717}
718
719static inline unsigned int ScaleQuantumToLong(const Quantum quantum)
720{
721 return((unsigned int) (quantum/4294967297.0+0.5));
722}
723
724static inline MagickSizeType ScaleQuantumToLongLong(const Quantum quantum)
725{
726#if !defined(MAGICKCORE_HDRI_SUPPORT)
727 return((MagickSizeType) quantum);
728#else
729 if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
730 return(0UL);
731 if (quantum >= 18446744073709551615.0)
732 return(MagickULLConstant(18446744073709551615));
733 return((MagickSizeType) (quantum+0.5));
734#endif
735}
736
737static inline unsigned int ScaleQuantumToMap(const Quantum quantum)
738{
739 if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
740 return(0U);
741 if ((quantum/281479271743489.0) >= MaxMap)
742 return((unsigned int) MaxMap);
743 return((unsigned int) (quantum/281479271743489.0+0.5));
744}
745
746static inline unsigned short ScaleQuantumToShort(const Quantum quantum)
747{
748 if ((IsNaN(quantum) != 0) || (quantum <= 0.0))
749 return(0);
750 if ((quantum/281479271743489.0) >= 65535.0)
751 return(65535);
752 return((unsigned short) (quantum/281479271743489.0+0.5));
753}
754
755static inline Quantum ScaleShortToQuantum(const unsigned short value)
756{
757 return((Quantum) (281479271743489.0*value));
758}
759#endif
760
761static inline unsigned short SinglePrecisionToHalf(const double value)
762{
763 typedef union _SinglePrecision
764 {
765 unsigned int
766 fixed_point;
767
768 float
769 single_precision;
770 } SinglePrecision;
771
772 int
773 exponent;
774
775 SinglePrecision
776 map;
777
778 unsigned int
779 significand,
780 sign_bit;
781
782 unsigned short
783 half;
784
785 /*
786 The IEEE 754 standard specifies half precision as having:
787
788 Sign bit: 1 bit
789 Exponent width: 5 bits
790 Significand precision: 11 (10 explicitly stored)
791 */
792 map.single_precision=(float) value;
793 sign_bit=(map.fixed_point >> 16) & 0x00008000;
794 exponent=(int) ((map.fixed_point >> ExponentShift) & 0x000000ff)-ExponentBias;
795 significand=map.fixed_point & 0x007fffff;
796 if (exponent <= 0)
797 {
798 int
799 shift;
800
801 if (exponent < -10)
802 return((unsigned short) sign_bit);
803 significand=significand | 0x00800000;
804 shift=(int) (14-exponent);
805 significand=(unsigned int) ((significand+((1U << (shift-1))-1)+
806 ((significand >> shift) & 0x01)) >> shift);
807 return((unsigned short) (sign_bit | significand));
808 }
809 else
810 if (exponent == (0xff-ExponentBias))
811 {
812 if (significand == 0)
813 return((unsigned short) (sign_bit | ExponentMask));
814 else
815 {
816 significand>>=SignificandShift;
817 half=(unsigned short) (sign_bit | significand |
818 (significand == 0) | ExponentMask);
819 return(half);
820 }
821 }
822 significand=significand+((significand >> SignificandShift) & 0x01)+0x00000fff;
823 if ((significand & 0x00800000) != 0)
824 {
825 significand=0;
826 exponent++;
827 }
828 if (exponent > 30)
829 {
830 float
831 alpha;
832
833 int
834 i;
835
836 /*
837 Float overflow.
838 */
839 alpha=1.0e10;
840 for (i=0; i < 10; i++)
841 alpha*=alpha;
842 return((unsigned short) (sign_bit | ExponentMask));
843 }
844 half=(unsigned short) (sign_bit | ((unsigned int) exponent << 10) |
845 (significand >> SignificandShift));
846 return(half);
847}
848
849#if defined(__cplusplus) || defined(c_plusplus)
850}
851#endif
852
853#endif