@@ -795,118 +795,217 @@ llvm::Value* EmitF8e4m3b11fnuzToF16(llvm::Value* f8_value,
795
795
796
796
absl::StatusOr<llvm::Value*> EmitF16ToF4e2m1fn (llvm::Value* f16_value,
797
797
llvm::IRBuilder<>* b) {
798
+ auto i8_const = [&](int val) {
799
+ return llvm::ConstantInt::get (b->getInt8Ty (), val);
800
+ };
801
+ auto i16_const = [&](int val) {
802
+ return llvm::ConstantInt::get (b->getInt16Ty (), val);
803
+ };
804
+ constexpr int mantissa_diff = 9 ; // 10 for F16, 1 for F4
805
+ constexpr int bias_diff = 14 ; // 15 for F16, 1 for F4
806
+
807
+ // Cast the input value to an integer for bitwise manipulation.
808
+ // Get the absolute value of the input (discard the sign).
809
+ // f16_bits = bitcast(f16_value, int)
810
+ // f16_abs_bits = f16_bits & 0x7FFF
811
+ llvm::Value* f16_bits = b->CreateBitCast (f16_value, b->getInt16Ty ());
812
+ llvm::Value* f16_abs_bits = b->CreateAnd (f16_bits, i16_const (0x7FFF ));
813
+
814
+ // If the input absolute value is >= 7.0 or an infinity, the result saturates
815
+ // to max value (6.0). If (0.75 <= input < 1), the result is rounded to 1.0.
816
+ // If (0 <= input <= 0.25), the result is rounded to 0.0.
817
+ // If the input is NaN, the result is undefined (implemented as minus zero).
818
+ // The rest of the cases are handled by the "happy path".
819
+ // is_overflow = f16_abs_bits >= 0x1.Cp2
820
+ // is_one = f16_abs_bits >= 0x1.8p-1 (used only if exponent underflows)
821
+ // is_zero = f16_abs_bits <= 0x1p-2 (used only if exponent underflows)
822
+ // is_nan = f16_abs_bits > 0x7C00 (F16 NaN threshold)
823
+ llvm::Value* is_overflow =
824
+ b->CreateICmpUGE (f16_abs_bits, i16_const (0x4700 )); // 7.0
825
+ llvm::Value* is_one =
826
+ b->CreateICmpUGE (f16_abs_bits, i16_const (0x3A00 )); // 0.75
827
+ llvm::Value* is_zero =
828
+ b->CreateICmpULE (f16_abs_bits, i16_const (0x3400 )); // 0.25
829
+ llvm::Value* is_nan =
830
+ b->CreateICmpUGT (f16_abs_bits, i16_const (0x7C00 )); // inf
831
+
832
+ // Truncate the mantissa to 1 bit and the exponent to 3 bits (not 2 bits, as
833
+ // the type doesn't have Inf/NaN and can represent unbiased exponent 2).
834
+ // This case, as well as the denormal, is handled below.
798
835
TF_ASSIGN_OR_RETURN (
799
836
llvm::Value * reduced_precision,
800
837
EmitReducePrecisionIR (
801
838
/* src_ty=*/ F16, f16_value,
802
839
/* dest_exponent_bits=*/ primitive_util::ExponentWidth (F4E2M1FN) + 1 ,
803
840
/* dest_mantissa_bits=*/ primitive_util::SignificandWidth (F4E2M1FN) - 1 ,
804
841
/* quiet_nans=*/ false , b));
842
+
843
+ // Cast the reduced precision value to an integer for bitwise manipulation.
844
+ // Discard the least significant (9) mantissa bits leaving 1 bit.
845
+ // Truncate to
846
+ // as_int16 = bitcast(reduced_precision, int)
847
+ // as_int8 = as_int16 >> (f16_mantissa - f4_mantissa)
805
848
llvm::Value* as_int16 = b->CreateBitCast (reduced_precision, b->getInt16Ty ());
806
849
llvm::Value* as_int8 =
807
- b->CreateTrunc (b->CreateLShr (as_int16, 9 ), b->getInt8Ty ());
850
+ b->CreateTrunc (b->CreateLShr (as_int16, mantissa_diff ), b->getInt8Ty ());
808
851
809
- // Extract sign, exponent and mantissa from reduced precision value.
810
- auto i8_const = [&](int val) {
811
- return llvm::ConstantInt::get (b->getInt8Ty (), val);
812
- };
852
+ // Get the sign (0 or 1).
853
+ // f4_sign = as_int8 >> 6
813
854
llvm::Value* f4_sign = b->CreateLShr (as_int8, 6 );
855
+
856
+ // Get exponent and mantissa bits without the sign.
857
+ // Important: the mask is 0x3F (not 0x7F), discard bit #6.
858
+ // f4_bits = as_int8 & 0x3F
814
859
llvm::Value* f4_bits = b->CreateAnd (as_int8, i8_const (0x3F ));
815
- llvm::Value* f4_normal = b->CreateSub (f4_bits, i8_const (28 ));
816
860
817
- // Special case for exponent overflow.
818
- auto i16_const = [&](int val) {
819
- return llvm::ConstantInt::get (b->getInt16Ty (), val);
820
- };
821
- llvm::Value* f16_bits = b->CreateAnd (
822
- b->CreateBitCast (f16_value, b->getInt16Ty ()), i16_const (0x7FFF ));
823
- llvm::Value* is_overflow =
824
- b->CreateICmpUGE (f16_bits, i16_const (0x4700 )); // 7.0
825
- llvm::Value* is_nan = b->CreateICmpUGT (f16_bits, i16_const (0x7C00 )); // inf
826
- llvm::Value* max_or_nan =
827
- b->CreateSelect (is_nan, i8_const (0x8 ), i8_const (0x7 ));
828
- llvm::Value* f4_normal_or_overflow =
829
- b->CreateSelect (is_overflow, max_or_nan, f4_normal);
830
-
831
- // Special case for exponent underflow.
861
+ // Convert F16 exponent to F4 exponent by readjusting the exponent bias.
862
+ // This produces the "normal" result, i.e. not Inf or NaN or denormal.
863
+ // f4_normal = f4_bits - ((f16_bias - f4_bias) << f4_mantissa)
864
+ constexpr int f4_exponent_offset = bias_diff << 1 ;
865
+ llvm::Value* f4_normal = b->CreateSub (f4_bits, i8_const (f4_exponent_offset));
866
+
867
+ // If the rounding resulted in zero exponent, the value is incorrect.
868
+ // This happens when the input is < 1.0
869
+ // is_underflow = f4_normal <= 1
832
870
llvm::Value* is_underflow = b->CreateICmpSLE (f4_normal, i8_const (1 ));
833
- llvm::Value* is_one = b->CreateICmpUGE (f16_bits, i16_const (0x3A00 )); // 0.75
834
- llvm::Value* is_zero = b->CreateICmpULE (f16_bits, i16_const (0x3400 )); // 0.25
835
- llvm::Value* denorm_or_zero =
836
- b->CreateSelect (is_zero, i8_const (0x0 ), i8_const (0x1 ));
837
- llvm::Value* f4_small =
838
- b->CreateSelect (is_one, i8_const (0x2 ), denorm_or_zero);
839
- llvm::Value* f4_result =
840
- b->CreateSelect (is_underflow, f4_small, f4_normal_or_overflow);
871
+
872
+ // Chain of selects that handles the special cases.
873
+ // f4_result =
874
+ // is_underflow ? (is_one ? 1.0 : (is_zero ? 0.0 : 0.5)) :
875
+ // is_overflow ? (is_nan ? -0.0 : 6.0) :
876
+ // f4_normal
877
+ llvm::Value* f4_result = b->CreateSelect (
878
+ is_underflow,
879
+ // If underflow, the input is < 1.0; the result is either 0.0, 0.5 or 1.0
880
+ b->CreateSelect (is_one, i8_const (0x2 ),
881
+ b->CreateSelect (is_zero, i8_const (0x0 ), i8_const (0x1 ))),
882
+ // If overflow, the input is >= 7.0 or infinity or NaN.
883
+ b->CreateSelect (is_overflow,
884
+ b->CreateSelect (is_nan, i8_const (0x8 ), i8_const (0x7 )),
885
+ f4_normal));
841
886
842
887
// Add sign to the resulting value.
888
+ // f4_signed_result = (f4_sign << 3) | f4_result
843
889
return b->CreateOr (f4_result, b->CreateShl (f4_sign, 3 ));
844
890
}
845
891
846
892
llvm::Value* EmitF4e2m1fnToF16 (llvm::Value* f8_value, llvm::IRBuilder<>* b) {
847
- llvm::Value* as_int16 = b->CreateZExt (f8_value, b->getInt16Ty ());
848
-
849
- // Extract sign, exponent and mantissa from reduced precision value.
850
893
auto i16_const = [&](int val) {
851
894
return llvm::ConstantInt::get (b->getInt16Ty (), val);
852
895
};
853
- llvm::Value* sign = b->CreateLShr (as_int16, 3 );
854
- llvm::Value* sign_shifted = b->CreateShl (sign, 15 );
855
- llvm::Value* bits = b->CreateAnd (as_int16, i16_const (0x7 ));
856
- llvm::Value* bits_shifted = b->CreateShl (bits, 9 );
857
-
858
- // Re-bias the exponent and handle denormals.
859
- llvm::Value* f16_normal = b->CreateAdd (bits_shifted, i16_const (14 << 10 ));
860
- llvm::Value* is_denorm_or_zero = b->CreateICmpULE (bits, i16_const (1 ));
861
- llvm::Value* is_zero = b->CreateICmpEQ (bits, i16_const (0 ));
862
- llvm::Value* denorm_or_zero =
863
- b->CreateSelect (is_zero, i16_const (0x0000 ), i16_const (0x3800 ));
864
- llvm::Value* f16_result =
865
- b->CreateSelect (is_denorm_or_zero, denorm_or_zero, f16_normal);
896
+ constexpr int mantissa_diff = 9 ; // 10 for F16, 1 for F4
897
+ constexpr int bias_diff = 14 ; // 15 for F16, 1 for F4
898
+
899
+ // The input value is a 8-bit integer, extend it to 16-bit integer.
900
+ // as_int16 = bitcast(f8_value, int)
901
+ llvm::Value* as_int16 = b->CreateZExt (f8_value, b->getInt16Ty ());
902
+
903
+ // Get the sign and shift it to F16 position.
904
+ // f4_sign = as_int16 >> 3
905
+ // f16_sign_bit = f4_sign << 15
906
+ llvm::Value* f4_sign = b->CreateLShr (as_int16, 3 );
907
+ llvm::Value* f16_sign_bit = b->CreateShl (f4_sign, 15 );
908
+
909
+ // Get exponent and mantissa bits without the sign.
910
+ // f4_bits = as_int16 & 0x7
911
+ // f16_bits = f4_bits << (f16_mantissa - f4_mantissa)
912
+ llvm::Value* f4_bits = b->CreateAnd (as_int16, i16_const (0x7 ));
913
+ llvm::Value* f16_bits = b->CreateShl (f4_bits, mantissa_diff);
914
+
915
+ // Convert F16 exponent to F4 exponent by readjusting the exponent bias.
916
+ // f4_normal = f4_bits - ((f16_bias - f4_bias) << f4_mantissa)
917
+ constexpr int f16_exponent_offset = bias_diff << 10 ;
918
+ llvm::Value* f16_normal =
919
+ b->CreateAdd (f16_bits, i16_const (f16_exponent_offset));
920
+
921
+ // For denormal and zero, the exponent is different. Handle these cases
922
+ // separately below.
923
+ // is_denorm_or_zero = f4_bits <= 1
924
+ // is_zero = f4_bits == 0
925
+ llvm::Value* is_denorm_or_zero = b->CreateICmpULE (f4_bits, i16_const (1 ));
926
+ llvm::Value* is_zero = b->CreateICmpEQ (f4_bits, i16_const (0 ));
927
+
928
+ // Chain of selects that handles the special cases.
929
+ // f16_result = is_denorm_or_zero ? (is_zero ? 0.0 : 0.5) : f16_normal
930
+ llvm::Value* f16_result = b->CreateSelect (
931
+ is_denorm_or_zero,
932
+ b->CreateSelect (is_zero, i16_const (0x0000 ), i16_const (0x3800 )),
933
+ f16_normal);
866
934
867
935
// Add sign to the resulting value.
868
- llvm::Value* f16_signed = b->CreateOr (f16_result, sign_shifted);
869
- return b->CreateBitCast (f16_signed, b->getHalfTy ());
936
+ // f16_signed_result = f16_sign_bit | f16_result
937
+ llvm::Value* f16_signed_result = b->CreateOr (f16_result, f16_sign_bit);
938
+ return b->CreateBitCast (f16_signed_result, b->getHalfTy ());
870
939
}
871
940
872
941
llvm::Value* EmitF32ToF8e8m0fnu (llvm::Value* f32_value, llvm::IRBuilder<>* b) {
873
- llvm::Value* as_int32 = b->CreateBitCast (f32_value, b->getInt32Ty ());
874
-
875
- // Result is NaN if input is zero, negative, infinity or NaN.
876
942
auto i32_const = [&](int val) {
877
943
return llvm::ConstantInt::get (b->getInt32Ty (), val);
878
944
};
879
- llvm::Value* is_denorm = b->CreateICmpULE (as_int32, i32_const (0x400000 ));
880
- llvm::Value* is_nan =
881
- b->CreateOr (b->CreateICmpSLE (as_int32, i32_const (0 )),
882
- b->CreateICmpSGE (as_int32, i32_const (0x7F400000 )));
883
945
884
- // Round the value and extract exponent.
885
- llvm::Value* rounded = b->CreateAdd (as_int32, i32_const (0x400000 ));
886
- llvm::Value* shifted = b->CreateAShr (rounded, 23 );
887
- llvm::Value* finite = b->CreateSelect (is_denorm, i32_const (0x00 ), shifted);
888
- llvm::Value* f32_result = b->CreateSelect (is_nan, i32_const (0xFF ), finite);
946
+ // Cast the input value to an integer for bitwise manipulation.
947
+ // as_int32 = bitcast(f32_value, int)
948
+ llvm::Value* as_int32 = b->CreateBitCast (f32_value, b->getInt32Ty ());
949
+
950
+ // Check if the input is zero, negative, overflow, infinity or NaN.
951
+ // All of these cases cannot be represented in the E8M0 format.
952
+ // is_zero_or_negative = as_int32 <= 0
953
+ // is_overflow_or_nan = as_int32 >= 0x1.8p127
954
+ // is_nan = is_zero_or_negative | is_overflow_or_nan
955
+ llvm::Value* is_zero_or_negative = b->CreateICmpSLE (as_int32, i32_const (0 ));
956
+ llvm::Value* is_overflow_or_nan =
957
+ b->CreateICmpSGE (as_int32, i32_const (0x7F400000 )); // 1.5 * 2^127
958
+ llvm::Value* is_nan = b->CreateOr (is_zero_or_negative, is_overflow_or_nan);
959
+
960
+ // Check if the input is a denormal which should round to the minimum value
961
+ // (2^-127), as there is no zero value.
962
+ // is_denorm = as_int32 <= 0x1p-127
963
+ llvm::Value* is_denorm =
964
+ b->CreateICmpULE (as_int32, i32_const (0x400000 )); // 1.0 * 2^-127
965
+
966
+ // Round the value (always up) and discard the mantissa.
967
+ // rounded = as_int32 + 0x1p-127
968
+ // f8_normal = as_int32 >> f32_mantissa
969
+ llvm::Value* rounded =
970
+ b->CreateAdd (as_int32, i32_const (0x400000 )); // 1.0 * 2^-127
971
+ llvm::Value* f8_normal = b->CreateAShr (rounded, 23 );
972
+
973
+ // Chain of selects that handles the special cases.
974
+ // f8_result = is_nan ? 0xFF : (is_denorm ? 0x00 : f8_normal)
975
+ llvm::Value* f8_result =
976
+ b->CreateSelect (is_nan, i32_const (0xFF ),
977
+ b->CreateSelect (is_denorm, i32_const (0x00 ), f8_normal));
889
978
890
979
// Truncate to the result type.
891
- return b->CreateTrunc (f32_result , b->getInt8Ty ());
980
+ return b->CreateTrunc (f8_result , b->getInt8Ty ());
892
981
}
893
982
894
983
llvm::Value* EmitF8e8m0fnuToF32 (llvm::Value* f8_value, llvm::IRBuilder<>* b) {
895
- // Shift exponent to the left for the normal case.
896
- llvm::Value* as_int32 = b->CreateZExt (f8_value, b->getInt32Ty ());
897
- llvm::Value* shifted = b->CreateShl (as_int32, 23 );
898
-
899
- // Special values for 0x00 (denorm) and 0xFF (NaN).
900
984
auto i32_const = [&](int val) {
901
985
return llvm::ConstantInt::get (b->getInt32Ty (), val);
902
986
};
987
+
988
+ // The input value is a 8-bit integer, extend it to 32-bit integer.
989
+ // as_int32 = bitcast(f8_value, int)
990
+ llvm::Value* as_int32 = b->CreateZExt (f8_value, b->getInt32Ty ());
991
+
992
+ // Check if the input is a denormal or NaN.
993
+ // is_zero = as_int32 == 0x00
994
+ // is_nan = as_int32 == 0xFF
903
995
llvm::Value* is_zero = b->CreateICmpEQ (as_int32, i32_const (0 ));
904
996
llvm::Value* is_nan = b->CreateICmpEQ (as_int32, i32_const (0xFF ));
905
- llvm::Value* denorm_or_shifted =
906
- b->CreateSelect (is_zero, i32_const (0x00400000 ), shifted);
907
- llvm::Value* f32_result =
908
- b->CreateSelect (is_nan, i32_const (0x7FC00000 ), denorm_or_shifted);
909
997
998
+ // Shift exponent to the left for the normal case.
999
+ // f32_normal = as_int32 << mantissa_diff
1000
+ llvm::Value* f32_normal = b->CreateShl (as_int32, 23 );
1001
+
1002
+ // Chain of selects that handles the special cases.
1003
+ // f32_result = is_nan ? 0x7FC00000 : (is_zero ? 0x1p-127 : f32_normal)
1004
+ llvm::Value* f32_result = b->CreateSelect (
1005
+ is_nan, i32_const (0x7FC00000 ),
1006
+ b->CreateSelect (is_zero, i32_const (0x400000 ), f32_normal));
1007
+
1008
+ // Bitcast integer bits to the result type.
910
1009
return b->CreateBitCast (f32_result, b->getFloatTy ());
911
1010
}
912
1011
0 commit comments