Lecture 05-1 · Quantization Iالمحاضرة 05-1 · التكميم I

IEEE 754 FP32: normal, subnormal, special values

IEEE 754 FP32: القيم الطبيعية، ودون الطبيعية (subnormal)، والقيم الخاصة

Slides 7–15الشرائح 7–15 P1 MUST

FP32 spends 1 bit on sign, 8 on exponent and 23 on fraction. The 8-bit exponent field decides which of three formulas applies: subnormal (field 0), normal (1 to 254), or infinity and NaN (255).

يخصص FP32 بتاً واحداً للإشارة و8 للأس (exponent) و23 للكسر (fraction). حقل الأس ذو 8 بتات يحدد أي صيغة من ثلاث تُطبَّق: subnormal (الحقل 0)، أو normal (من 1 إلى 254)، أو اللانهاية وNaN (255).

The layout and the normal formula

التخطيط وصيغة العدد الطبيعي

Slide 7 lays out the 32 bits as sign | 8-bit exponent | 23-bit fraction (the fraction is also called significand or mantissa). For a normal number the stored exponent E is biased, and a hidden leading 1 is added in front of the fraction:

ترتّب الشريحة 7 البتات الـ 32 هكذا: sign | 8-bit exponent | 23-bit fraction (ويسمى الكسر أيضاً significand أو mantissa). في العدد الطبيعي (normal) يكون الأس المخزَّن E مُزاحاً (biased)، ويضاف 1 مخفي قبل الكسر:

value = (−1)sign × (1 + fraction) × 2E − 127    bias = 127 = 28−1 − 1

The bias lets the exponent field be stored as an unsigned number while representing negative powers. The fraction bits are weights 2−1, 2−2, ..., 2−23, so (1 + fraction) lies in [1, 2).

الإزاحة (bias) تسمح بتخزين حقل الأس كعدد بلا إشارة مع تمثيل القوى السالبة. بتات الكسر أوزانها 2^−1, 2^−2, ..., 2^−23، لذلك تقع (1 + fraction) في المجال [1, 2).

0.265625 IN FP32 (SLIDE 7) 0 0 1 1 1 1 1 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 sign exponent (8) fraction (23) + E = 125 2^−4 = 0.0625 (+1) × (1 + 0.0625) × 2^(125 − 127) = 1.0625 × 0.25 = 0.265625
Sign in red, exponent in blue, fraction in green. The hidden 1 is never stored.الإشارة بالأحمر، والأس بالأزرق، والكسر بالأخضر. الـ 1 المخفي لا يُخزَّن أبداً.
Worked exampleمثال محلول

Encode 0.265625 (slide 7), then decode a word the other way.

رمّز 0.265625 (الشريحة 7)، ثم فك رموز كلمة في الاتجاه المعاكس.

Encode 0.265625
  0.265625 = 0.25 + 0.015625 = 2^−2 + 2^−6 = 0.010001₂
  normalise: 0.010001₂ = 1.0001₂ × 2^−2
  sign      = 0
  exponent  E = e + 127 = −2 + 127 = 125 = 0111 1101₂
  fraction  = .0001 → 0001 0000 0000 0000 0000 000  (= 0.0625)
  word      = 0 | 0111 1101 | 0001 000...0

Decode 1 | 1000 0001 | 0100 000...0
  sign      = 1 → negative
  E         = 129 → e = 129 − 127 = 2
  fraction  = 2^−2 = 0.25
  value     = −(1 + 0.25) × 2^2 = −5.0

Zero needs a second formula: subnormals

الصفر يحتاج صيغة ثانية: الأعداد دون الطبيعية (subnormals)

Slide 8 asks how to represent 0. With the hidden 1, the normal formula can never produce zero: (1 + fraction) ≥ 1. IEEE 754 therefore reserves E = 0 for subnormal numbers, which drop the hidden 1. Slide 9 shows the subtle part: the exponent "should have been" 0 − 127, but it is forced to 1 − 127 = −126.

تسأل الشريحة 8 كيف نمثّل الصفر. مع الـ 1 المخفي لا تستطيع الصيغة الطبيعية إنتاج صفر أبداً: (1 + fraction) ≥ 1. لذلك يحجز IEEE 754 القيمة E = 0 للأعداد subnormal التي تُسقط الـ 1 المخفي. وتبيّن الشريحة 9 الجزء الدقيق: الأس "كان يجب" أن يكون 0 − 127، لكنه يُفرض ليكون 1 − 127 = −126.

subnormal (E = 0): value = (−1)sign × fraction × 21 − 127

Why 1 − 127? The smallest normal number is 1.0 × 2−126. Using the same 2−126 for subnormals makes them continue that grid downward with the same spacing 2−23 × 2−126 = 2−149, so there is no gap between the largest subnormal and the smallest normal. With an all-zero word, fraction = 0 gives 0 × 2−126 = 0, and the sign bit gives +0 and −0.

لماذا 1 − 127؟ أصغر عدد طبيعي هو 1.0 × 2^−126. استخدام نفس 2^−126 للأعداد subnormal يجعلها تكمل الشبكة نفسها نزولاً بنفس التباعد 2^−23 × 2^−126 = 2^−149، فلا توجد فجوة بين أكبر subnormal وأصغر normal. ومع كلمة كلها أصفار يعطي fraction = 0 القيمة 0 × 2^−126 = 0، وبت الإشارة يعطي +0 و−0.

The four boundary values (slides 10 to 13)

القيم الحدية الأربع (الشرائح 10 إلى 13)

ValueالقيمةBits (sign | exp | fraction)البتات (sign | exp | fraction)Formulaالصيغة≈ Decimal≈ بالعشري
Smallest positive subnormalأصغر عدد subnormal موجب0 | 0000 0000 | 000...0012−23 × 2−126 = 2−1491.4 × 10−45
Largest positive subnormalأكبر عدد subnormal موجب0 | 0000 0000 | 111...111(1 − 2−23) × 2−126 = 2−126 − 2−1491.18 × 10−38
Smallest positive normalأصغر عدد normal موجب0 | 0000 0001 | 000...000(1 + 0) × 21 − 127 = 2−1261.18 × 10−38
Largest positive normalأكبر عدد normal موجب0 | 1111 1110 | 111...111(1 + 1 − 2−23) × 2254 − 127 = (2 − 2−23) × 21273.4 × 1038

يلخص الجدول القيم الحدّية الأربع. أصغر subnormal موجب هو 2^−149 (بت 1 في آخر الكسر فقط). أكبر subnormal هو (1 − 2^−23) × 2^−126 لأن 2^−1 + 2^−2 + ... + 2^−23 = 1 − 2^−23. أصغر normal هو 2^−126 (حقل الأس 1)، وأكبر normal هو (2 − 2^−23) × 2^127 لأن الحقل 254 هو أكبر أس طبيعي.

Notice that the largest normal uses exponent field 254, not 255. Field 255 is reserved, which is the next section.

لاحظ أن أكبر عدد طبيعي يستخدم حقل الأس 254 وليس 255. الحقل 255 محجوز، وهذا موضوع القسم التالي.

Exponent all ones: infinity and NaN

الأس كله آحاد: اللانهاية وNaN

Slide 14: when E = 255 (1111 1111), a zero fraction means ±∞ (sign picks which), and any nonzero fraction means NaN (Not a Number). That is 2 × (223 − 1) bit patterns spent on NaN, which the slide calls "much waste, revisit in fp8": with only 8 bits total, FP8 E4M3 cannot afford it and keeps just one NaN pattern per sign.

الشريحة 14: عندما يكون E = 255 (1111 1111)، الكسر الصفري يعني ±∞ (والإشارة تحدد أيهما)، وأي كسر غير صفري يعني NaN (Not a Number). هذا يعني 2 × (2^23 − 1) نمطاً من البتات مصروفة على NaN، وتسميه الشريحة "much waste, revisit in fp8": مع 8 بتات فقط إجمالاً لا يستطيع FP8 E4M3 تحمّل ذلك، فيحتفظ بنمط NaN واحد لكل إشارة.

Exponent fieldحقل الأسFraction = 0Fraction = 0Fraction ≠ 0Fraction ≠ 0Equationالمعادلة
00H = 0±0subnormalsubnormal(−1)s × fraction × 21 − 127
01H ... FEH = 1 ... 254normalnormal(−1)s × (1 + fraction) × 2E − 127
FFH = 255±INFNaN

هذا هو جدول الشريحة 15 الذي يجب حفظه: الحقل 0 يعطي ±0 أو subnormal، والحقول من 1 إلى 254 طبيعية، والحقل 255 يعطي ±INF أو NaN.

subnormal (E = 0) normal (E = 1 ... 254) ±0 2^−149 (1 − 2^−23)·2^−126 2^−126 (2 − 2^−23)·2^127 E = 255 sits beyond: ±∞ and NaN
Slide 15's number line. Subnormals fill the gap between 0 and the smallest normal with uniform steps of 2−149.خط الأعداد في الشريحة 15. الأعداد subnormal تملأ الفجوة بين 0 وأصغر عدد normal بخطوات منتظمة مقدارها 2−149.
Memory trickحيلة للحفظ

"0 is special, 255 is special, everything between is normal". Exponent field 0: no hidden 1, exponent fixed at −126. Field 255: ∞ if fraction is 0, NaN otherwise.

"0 خاص، 255 خاص، وكل ما بينهما طبيعي". الحقل 0: لا يوجد 1 مخفي، والأس ثابت عند −126. الحقل 255: ∞ إذا كان الكسر صفراً، وNaN غير ذلك.

Exam trapفخ في الاختبار

The subnormal exponent is 1 − 127 = −126, not 0 − 127 = −127. And there is no implicit 1: the multiplier is the fraction alone. Writing (1 + fraction) × 2−127 for E = 0 is wrong twice.

أس الـ subnormal هو 1 − 127 = −126، وليس 0 − 127 = −127. ولا يوجد 1 ضمني: المضروب هو الكسر وحده. كتابة (1 + fraction) × 2^−127 عند E = 0 خطأ مرتين.

Bias is 2k−1 − 1 for a k-bit exponent. For FP32, k = 8 gives 127. Do not reuse 127 for other formats: FP16 has k = 5, bias 15.

الإزاحة هي 2^(k−1) − 1 لأس من k بتات. في FP32 يعطي k = 8 القيمة 127. لا تستخدم 127 لأشكال أخرى: FP16 لديه k = 5 وإزاحة 15.

Smallest subnormal is 2−149, not 2−126 and not 2−127. It is the last fraction bit (2−23) times 2−126.

أصغر subnormal هو 2^−149، وليس 2^−126 ولا 2^−127. إنه آخر بت في الكسر (2^−23) مضروباً في 2^−126.

Check yourself

اختبر نفسك

When 0.265625 is stored in FP32, what is the 8-bit exponent field?

عند تخزين 0.265625 في FP32، ما قيمة حقل الأس ذي 8 بتات؟

C. 0.265625 = 1.00012 × 2−2, so E = −2 + 127 = 125. D adds 2 instead of −2. B stores the unbiased exponent's magnitude. A has nothing to do with this value.

الجواب C. 0.265625 = 1.00012 × 2−2، إذن E = −2 + 127 = 125. والخيار D يضيف 2 بدلاً من −2. وB يخزّن مقدار الأس قبل الإزاحة. وA لا علاقة له بهذه القيمة.

What is the smallest positive subnormal FP32 value?

ما أصغر قيمة subnormal موجبة في FP32؟

A. Fraction = 2−23 (only the last bit set), times 21 − 127 = 2−126, gives 2−149. B is the smallest normal. C and D come from wrongly using 20 − 127 as the subnormal exponent.

الجواب A. Fraction = 2−23 (البت الأخير فقط يساوي 1)، مضروباً في 21 − 127 = 2−126، يعطي 2−149. والخيار B هو أصغر عدد normal. وC وD ينتجان من استخدام 20 − 127 خطأً كأس للأعداد subnormal.

What does the FP32 word 1 | 1000 0001 | 0100 0000 0000 0000 0000 000 represent?

ماذا تمثل كلمة FP32 التالية: 1 | 1000 0001 | 0100 0000 0000 0000 0000 000؟

D. Sign 1 is negative. E = 129, so e = 2. Fraction 01 = 0.25. Value = −(1.25) × 4 = −5.0. A uses e = 1. B ignores the sign bit. C forgets the hidden 1 and the exponent.

الجواب D. الإشارة 1 تعني سالباً. E = 129، إذن e = 2. الكسر 01 يساوي 0.25. القيمة = −(1.25) × 4 = −5.0. والخيار A يستخدم e = 1. وB يتجاهل بت الإشارة. وC ينسى الـ 1 المخفي والأس.

An FP32 word has exponent field 1111 1111 and a nonzero fraction. What is it?

كلمة FP32 حقل الأس فيها 1111 1111 والكسر غير صفري. ماذا تمثل؟

B. E = 255 with fraction ≠ 0 is NaN. A needs fraction = 0. C uses exponent field 254. D needs exponent field 0.

الجواب B. E = 255 مع fraction ≠ 0 هو NaN. والخيار A يحتاج fraction = 0. وC يستخدم حقل أس يساوي 254. وD يحتاج حقل أس يساوي 0.

Why does IEEE 754 use 21 − 127 instead of 20 − 127 for subnormal numbers?

لماذا يستخدم IEEE 754 القيمة 2^(1 − 127) بدل 2^(0 − 127) للأعداد subnormal؟

C. With 2−126, the largest subnormal (1 − 2−23) × 2−126 sits exactly one step 2−149 below the smallest normal 2−126. With 2−127 there would be a gap. A is backwards (subnormals drop the hidden 1). B is unrelated. D is false: NaN uses field 255.

الجواب C. مع 2−126 يقع أكبر عدد subnormal وهو (1 − 2−23) × 2−126 تحت أصغر عدد normal وهو 2−126 بخطوة واحدة تماماً مقدارها 2−149. ومع 2−127 ستظهر فجوة. والخيار A معكوس (الأعداد subnormal تُسقط الـ 1 المخفي). وB لا علاقة له. وD خاطئ: NaN يستخدم الحقل 255.