Commit 09bd58d68 for imagemagick.org
commit 09bd58d68718a00a82bc4ed7998fc363bdd1e4aa
Author: Cristy <urban-warrior@imagemagick.org>
Date: Wed Dec 2 20:09:48 2026 -0500
HalfToSinglePrecision() no longer relies on an unsigned exponent wrapping
diff --git a/MagickCore/quantum-private.h b/MagickCore/quantum-private.h
index 662ef82ab..3d0f8ab73 100644
--- a/MagickCore/quantum-private.h
+++ b/MagickCore/quantum-private.h
@@ -188,18 +188,20 @@ static inline float HalfToSinglePrecision(const unsigned short half)
value=sign_bit << SignBitShift;
else
{
+ int
+ shifted_exponent;
+
/*
- Subnormal half: normalize. Unsigned wrap-around of exponent is
- intentional and is corrected by the exponent++ / bias below.
+ Subnormal half: normalize.
*/
+ shifted_exponent=1;
while ((significand & SignificandMask) == 0)
{
significand<<=1;
- exponent--;
+ shifted_exponent--;
}
- exponent++;
significand&=(~SignificandMask);
- exponent+=ExponentBias;
+ exponent=(unsigned int) (shifted_exponent+ExponentBias);
value=(sign_bit << SignBitShift) | (exponent << ExponentShift) |
(significand << SignificandShift);
}
@@ -394,9 +396,11 @@ static inline Quantum ScaleAnyToQuantum(const QuantumAny quantum,
if (quantum > range)
return(QuantumRange);
#if !defined(MAGICKCORE_HDRI_SUPPORT)
- return((Quantum) ((double) QuantumRange*((double) quantum*MagickSafeReciprocal((double) range))+0.5));
+ return((Quantum) ((double) QuantumRange*((double) quantum*
+ MagickSafeReciprocal((double) range))+0.5));
#else
- return((Quantum) ((double) QuantumRange*((double) quantum*MagickSafeReciprocal((double) range))));
+ return((Quantum) ((double) QuantumRange*((double) quantum*
+ MagickSafeReciprocal((double) range))));
#endif
}
@@ -412,10 +416,7 @@ static inline QuantumAny ScaleQuantumToAny(const Quantum quantum,
#endif
if (quantum >= (Quantum) QuantumRange)
return(range);
- value=(double) range*(double) quantum/(double) QuantumRange;
-#if defined(MAGICKCORE_HDRI_SUPPORT)
- value+=0.5;
-#endif
+ value=(double) range*(double) quantum/(double) QuantumRange+0.5;
if (value >= (double) range)
return(range);
return((QuantumAny) value);