@@ -744,12 +744,14 @@ function is_top_bit_set(x::Union{Int8,UInt8})
744744 eq_int (lshr_int (x, 7 ), trunc_int (typeof (x), 1 ))
745745end
746746
747- # TODO delete this function (but see #48097):
747+ # n.b. This function exists for CUDA to overload to configure error behavior ( see #48097)
748748throw_inexacterror (args... ) = throw (InexactError (args... ))
749749
750- function check_top_bit (:: Type{To} , x) where {To}
750+ function check_sign_bit (:: Type{To} , x) where {To}
751751 @inline
752- is_top_bit_set (x) && throw_inexacterror (:check_top_bit , To, x)
752+ # the top bit is the sign bit of x but "sign bit" sounds better in stacktraces
753+ # n.b. if x is signed, then sizeof(x) === sizeof(To), otherwise sizeof(x) >= sizeof(To)
754+ is_top_bit_set (x) && throw_inexacterror (sizeof (x) === sizeof (To) ? :convert : :trunc , To, x)
753755 x
754756end
755757
@@ -774,22 +776,22 @@ toInt8(x::Int16) = checked_trunc_sint(Int8, x)
774776toInt8 (x:: Int32 ) = checked_trunc_sint (Int8, x)
775777toInt8 (x:: Int64 ) = checked_trunc_sint (Int8, x)
776778toInt8 (x:: Int128 ) = checked_trunc_sint (Int8, x)
777- toInt8 (x:: UInt8 ) = bitcast (Int8, check_top_bit (Int8, x))
778- toInt8 (x:: UInt16 ) = checked_trunc_sint (Int8, check_top_bit (Int8, x))
779- toInt8 (x:: UInt32 ) = checked_trunc_sint (Int8, check_top_bit (Int8, x))
780- toInt8 (x:: UInt64 ) = checked_trunc_sint (Int8, check_top_bit (Int8, x))
781- toInt8 (x:: UInt128 ) = checked_trunc_sint (Int8, check_top_bit (Int8, x))
779+ toInt8 (x:: UInt8 ) = bitcast (Int8, check_sign_bit (Int8, x))
780+ toInt8 (x:: UInt16 ) = checked_trunc_sint (Int8, check_sign_bit (Int8, x))
781+ toInt8 (x:: UInt32 ) = checked_trunc_sint (Int8, check_sign_bit (Int8, x))
782+ toInt8 (x:: UInt64 ) = checked_trunc_sint (Int8, check_sign_bit (Int8, x))
783+ toInt8 (x:: UInt128 ) = checked_trunc_sint (Int8, check_sign_bit (Int8, x))
782784toInt8 (x:: Bool ) = and_int (bitcast (Int8, x), Int8 (1 ))
783785toInt16 (x:: Int8 ) = sext_int (Int16, x)
784786toInt16 (x:: Int16 ) = x
785787toInt16 (x:: Int32 ) = checked_trunc_sint (Int16, x)
786788toInt16 (x:: Int64 ) = checked_trunc_sint (Int16, x)
787789toInt16 (x:: Int128 ) = checked_trunc_sint (Int16, x)
788790toInt16 (x:: UInt8 ) = zext_int (Int16, x)
789- toInt16 (x:: UInt16 ) = bitcast (Int16, check_top_bit (Int16, x))
790- toInt16 (x:: UInt32 ) = checked_trunc_sint (Int16, check_top_bit (Int16, x))
791- toInt16 (x:: UInt64 ) = checked_trunc_sint (Int16, check_top_bit (Int16, x))
792- toInt16 (x:: UInt128 ) = checked_trunc_sint (Int16, check_top_bit (Int16, x))
791+ toInt16 (x:: UInt16 ) = bitcast (Int16, check_sign_bit (Int16, x))
792+ toInt16 (x:: UInt32 ) = checked_trunc_sint (Int16, check_sign_bit (Int16, x))
793+ toInt16 (x:: UInt64 ) = checked_trunc_sint (Int16, check_sign_bit (Int16, x))
794+ toInt16 (x:: UInt128 ) = checked_trunc_sint (Int16, check_sign_bit (Int16, x))
793795toInt16 (x:: Bool ) = and_int (zext_int (Int16, x), Int16 (1 ))
794796toInt32 (x:: Int8 ) = sext_int (Int32, x)
795797toInt32 (x:: Int16 ) = sext_int (Int32, x)
@@ -798,9 +800,9 @@ toInt32(x::Int64) = checked_trunc_sint(Int32, x)
798800toInt32 (x:: Int128 ) = checked_trunc_sint (Int32, x)
799801toInt32 (x:: UInt8 ) = zext_int (Int32, x)
800802toInt32 (x:: UInt16 ) = zext_int (Int32, x)
801- toInt32 (x:: UInt32 ) = bitcast (Int32, check_top_bit (Int32, x))
802- toInt32 (x:: UInt64 ) = checked_trunc_sint (Int32, check_top_bit (Int32, x))
803- toInt32 (x:: UInt128 ) = checked_trunc_sint (Int32, check_top_bit (Int32, x))
803+ toInt32 (x:: UInt32 ) = bitcast (Int32, check_sign_bit (Int32, x))
804+ toInt32 (x:: UInt64 ) = checked_trunc_sint (Int32, check_sign_bit (Int32, x))
805+ toInt32 (x:: UInt128 ) = checked_trunc_sint (Int32, check_sign_bit (Int32, x))
804806toInt32 (x:: Bool ) = and_int (zext_int (Int32, x), Int32 (1 ))
805807toInt64 (x:: Int8 ) = sext_int (Int64, x)
806808toInt64 (x:: Int16 ) = sext_int (Int64, x)
@@ -810,8 +812,8 @@ toInt64(x::Int128) = checked_trunc_sint(Int64, x)
810812toInt64 (x:: UInt8 ) = zext_int (Int64, x)
811813toInt64 (x:: UInt16 ) = zext_int (Int64, x)
812814toInt64 (x:: UInt32 ) = zext_int (Int64, x)
813- toInt64 (x:: UInt64 ) = bitcast (Int64, check_top_bit (Int64, x))
814- toInt64 (x:: UInt128 ) = checked_trunc_sint (Int64, check_top_bit (Int64, x))
815+ toInt64 (x:: UInt64 ) = bitcast (Int64, check_sign_bit (Int64, x))
816+ toInt64 (x:: UInt128 ) = checked_trunc_sint (Int64, check_sign_bit (Int64, x))
815817toInt64 (x:: Bool ) = and_int (zext_int (Int64, x), Int64 (1 ))
816818toInt128 (x:: Int8 ) = sext_int (Int128, x)
817819toInt128 (x:: Int16 ) = sext_int (Int128, x)
@@ -822,9 +824,9 @@ toInt128(x::UInt8) = zext_int(Int128, x)
822824toInt128 (x:: UInt16 ) = zext_int (Int128, x)
823825toInt128 (x:: UInt32 ) = zext_int (Int128, x)
824826toInt128 (x:: UInt64 ) = zext_int (Int128, x)
825- toInt128 (x:: UInt128 ) = bitcast (Int128, check_top_bit (Int128, x))
827+ toInt128 (x:: UInt128 ) = bitcast (Int128, check_sign_bit (Int128, x))
826828toInt128 (x:: Bool ) = and_int (zext_int (Int128, x), Int128 (1 ))
827- toUInt8 (x:: Int8 ) = bitcast (UInt8, check_top_bit (UInt8, x))
829+ toUInt8 (x:: Int8 ) = bitcast (UInt8, check_sign_bit (UInt8, x))
828830toUInt8 (x:: Int16 ) = checked_trunc_uint (UInt8, x)
829831toUInt8 (x:: Int32 ) = checked_trunc_uint (UInt8, x)
830832toUInt8 (x:: Int64 ) = checked_trunc_uint (UInt8, x)
@@ -835,8 +837,8 @@ toUInt8(x::UInt32) = checked_trunc_uint(UInt8, x)
835837toUInt8 (x:: UInt64 ) = checked_trunc_uint (UInt8, x)
836838toUInt8 (x:: UInt128 ) = checked_trunc_uint (UInt8, x)
837839toUInt8 (x:: Bool ) = and_int (bitcast (UInt8, x), UInt8 (1 ))
838- toUInt16 (x:: Int8 ) = sext_int (UInt16, check_top_bit (UInt16, x))
839- toUInt16 (x:: Int16 ) = bitcast (UInt16, check_top_bit (UInt16, x))
840+ toUInt16 (x:: Int8 ) = sext_int (UInt16, check_sign_bit (UInt16, x))
841+ toUInt16 (x:: Int16 ) = bitcast (UInt16, check_sign_bit (UInt16, x))
840842toUInt16 (x:: Int32 ) = checked_trunc_uint (UInt16, x)
841843toUInt16 (x:: Int64 ) = checked_trunc_uint (UInt16, x)
842844toUInt16 (x:: Int128 ) = checked_trunc_uint (UInt16, x)
@@ -846,9 +848,9 @@ toUInt16(x::UInt32) = checked_trunc_uint(UInt16, x)
846848toUInt16 (x:: UInt64 ) = checked_trunc_uint (UInt16, x)
847849toUInt16 (x:: UInt128 ) = checked_trunc_uint (UInt16, x)
848850toUInt16 (x:: Bool ) = and_int (zext_int (UInt16, x), UInt16 (1 ))
849- toUInt32 (x:: Int8 ) = sext_int (UInt32, check_top_bit (UInt32, x))
850- toUInt32 (x:: Int16 ) = sext_int (UInt32, check_top_bit (UInt32, x))
851- toUInt32 (x:: Int32 ) = bitcast (UInt32, check_top_bit (UInt32, x))
851+ toUInt32 (x:: Int8 ) = sext_int (UInt32, check_sign_bit (UInt32, x))
852+ toUInt32 (x:: Int16 ) = sext_int (UInt32, check_sign_bit (UInt32, x))
853+ toUInt32 (x:: Int32 ) = bitcast (UInt32, check_sign_bit (UInt32, x))
852854toUInt32 (x:: Int64 ) = checked_trunc_uint (UInt32, x)
853855toUInt32 (x:: Int128 ) = checked_trunc_uint (UInt32, x)
854856toUInt32 (x:: UInt8 ) = zext_int (UInt32, x)
@@ -857,22 +859,22 @@ toUInt32(x::UInt32) = x
857859toUInt32 (x:: UInt64 ) = checked_trunc_uint (UInt32, x)
858860toUInt32 (x:: UInt128 ) = checked_trunc_uint (UInt32, x)
859861toUInt32 (x:: Bool ) = and_int (zext_int (UInt32, x), UInt32 (1 ))
860- toUInt64 (x:: Int8 ) = sext_int (UInt64, check_top_bit (UInt64, x))
861- toUInt64 (x:: Int16 ) = sext_int (UInt64, check_top_bit (UInt64, x))
862- toUInt64 (x:: Int32 ) = sext_int (UInt64, check_top_bit (UInt64, x))
863- toUInt64 (x:: Int64 ) = bitcast (UInt64, check_top_bit (UInt64, x))
862+ toUInt64 (x:: Int8 ) = sext_int (UInt64, check_sign_bit (UInt64, x))
863+ toUInt64 (x:: Int16 ) = sext_int (UInt64, check_sign_bit (UInt64, x))
864+ toUInt64 (x:: Int32 ) = sext_int (UInt64, check_sign_bit (UInt64, x))
865+ toUInt64 (x:: Int64 ) = bitcast (UInt64, check_sign_bit (UInt64, x))
864866toUInt64 (x:: Int128 ) = checked_trunc_uint (UInt64, x)
865867toUInt64 (x:: UInt8 ) = zext_int (UInt64, x)
866868toUInt64 (x:: UInt16 ) = zext_int (UInt64, x)
867869toUInt64 (x:: UInt32 ) = zext_int (UInt64, x)
868870toUInt64 (x:: UInt64 ) = x
869871toUInt64 (x:: UInt128 ) = checked_trunc_uint (UInt64, x)
870872toUInt64 (x:: Bool ) = and_int (zext_int (UInt64, x), UInt64 (1 ))
871- toUInt128 (x:: Int8 ) = sext_int (UInt128, check_top_bit (UInt128, x))
872- toUInt128 (x:: Int16 ) = sext_int (UInt128, check_top_bit (UInt128, x))
873- toUInt128 (x:: Int32 ) = sext_int (UInt128, check_top_bit (UInt128, x))
874- toUInt128 (x:: Int64 ) = sext_int (UInt128, check_top_bit (UInt128, x))
875- toUInt128 (x:: Int128 ) = bitcast (UInt128, check_top_bit (UInt128, x))
873+ toUInt128 (x:: Int8 ) = sext_int (UInt128, check_sign_bit (UInt128, x))
874+ toUInt128 (x:: Int16 ) = sext_int (UInt128, check_sign_bit (UInt128, x))
875+ toUInt128 (x:: Int32 ) = sext_int (UInt128, check_sign_bit (UInt128, x))
876+ toUInt128 (x:: Int64 ) = sext_int (UInt128, check_sign_bit (UInt128, x))
877+ toUInt128 (x:: Int128 ) = bitcast (UInt128, check_sign_bit (UInt128, x))
876878toUInt128 (x:: UInt8 ) = zext_int (UInt128, x)
877879toUInt128 (x:: UInt16 ) = zext_int (UInt128, x)
878880toUInt128 (x:: UInt32 ) = zext_int (UInt128, x)
0 commit comments