@@ -817,8 +817,9 @@ bu_short(_structmodulestate *state, const char *p, const formatdef *f)
817
817
do {
818
818
x = (x <<8 ) | * bytes ++ ;
819
819
} while (-- i > 0 );
820
- /* Extend sign. */
821
- return PyLong_FromLong (x & 0x8000U ? (long )x - 0x10000 : (long )x );
820
+ /* Extend sign, avoiding implementation-defined or undefined behaviour. */
821
+ x = (x ^ 0x8000U ) - 0x8000U ;
822
+ return PyLong_FromLong (x & 0x8000U ? -1 - (long )(~x ): (long )x );
822
823
}
823
824
824
825
static PyObject *
@@ -834,8 +835,8 @@ bu_int(_structmodulestate *state, const char *p, const formatdef *f)
834
835
x = (x <<8 ) | * bytes ++ ;
835
836
} while (-- i > 0 );
836
837
/* Extend sign, avoiding implementation-defined or undefined behaviour. */
837
- return PyLong_FromLong (x & 0x80000000U ?
838
- -1 - (long )(0xFFFFFFFFU - x ) : (long )x );
838
+ x = (x ^ 0x80000000U ) - 0x80000000U ;
839
+ return PyLong_FromLong ( x & 0x80000000U ? -1 - (long )(~ x ) : (long )x );
839
840
}
840
841
841
842
static PyObject *
@@ -863,8 +864,9 @@ bu_longlong(_structmodulestate *state, const char *p, const formatdef *f)
863
864
x = (x <<8 ) | * bytes ++ ;
864
865
} while (-- i > 0 );
865
866
/* Extend sign, avoiding implementation-defined or undefined behaviour. */
866
- return PyLong_FromLongLong (x & 0x8000000000000000U ?
867
- -1 - (long long )(0xFFFFFFFFFFFFFFFFU - x ) : (long long )x );
867
+ x = (x ^ 0x8000000000000000U ) - 0x8000000000000000U ;
868
+ return PyLong_FromLongLong (
869
+ x & 0x8000000000000000U ? -1 - (long long )(~x ): (long long )x );
868
870
}
869
871
870
872
static PyObject *
@@ -1058,8 +1060,9 @@ lu_short(_structmodulestate *state, const char *p, const formatdef *f)
1058
1060
do {
1059
1061
x = (x <<8 ) | bytes [-- i ];
1060
1062
} while (i > 0 );
1061
- /* Extend sign. */
1062
- return PyLong_FromLong (x & 0x8000U ? (long )x - 0x10000 : (long )x );
1063
+ /* Extend sign, avoiding implementation-defined or undefined behaviour. */
1064
+ x = (x ^ 0x8000U ) - 0x8000U ;
1065
+ return PyLong_FromLong (x & 0x8000U ? -1 - (long )(~x ): (long )x );
1063
1066
}
1064
1067
1065
1068
static PyObject *
@@ -1075,8 +1078,8 @@ lu_int(_structmodulestate *state, const char *p, const formatdef *f)
1075
1078
x = (x <<8 ) | bytes [-- i ];
1076
1079
} while (i > 0 );
1077
1080
/* Extend sign, avoiding implementation-defined or undefined behaviour. */
1078
- return PyLong_FromLong (x & 0x80000000U ?
1079
- -1 - (long )(0xFFFFFFFFU - x ) : (long )x );
1081
+ x = (x ^ 0x80000000U ) - 0x80000000U ;
1082
+ return PyLong_FromLong ( x & 0x80000000U ? -1 - (long )(~ x ) : (long )x );
1080
1083
}
1081
1084
1082
1085
static PyObject *
@@ -1104,8 +1107,9 @@ lu_longlong(_structmodulestate *state, const char *p, const formatdef *f)
1104
1107
x = (x <<8 ) | bytes [-- i ];
1105
1108
} while (i > 0 );
1106
1109
/* Extend sign, avoiding implementation-defined or undefined behaviour. */
1107
- return PyLong_FromLongLong (x & 0x8000000000000000U ?
1108
- -1 - (long long )(0xFFFFFFFFFFFFFFFFU - x ) : (long long )x );
1110
+ x = (x ^ 0x8000000000000000U ) - 0x8000000000000000U ;
1111
+ return PyLong_FromLongLong (
1112
+ x & 0x8000000000000000U ? -1 - (long long )(~x ): (long long )x );
1109
1113
}
1110
1114
1111
1115
static PyObject *
0 commit comments