Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7d3a511

Browse files
committed
Cray J90 fixes for long ints.
This was a convenient excuse to create the pyport.h file recently discussed! Please use new Py_ARITHMETIC_RIGHT_SHIFT when right-shifting a signed int and you *need* sign-extension. This is #define'd in pyport.h, keying off new config symbol SIGNED_RIGHT_SHIFT_ZERO_FILLS. If you're running on a platform that needs that symbol #define'd, the std tests never would have worked for you (in particular, at least test_long would have failed). The autoconfig stuff got added to Python after my Unix days, so I don't know how that works. Would someone please look into doing & testing an auto-config of the SIGNED_RIGHT_SHIFT_ZERO_FILLS symbol? It needs to be defined if & only if, e.g., (-1) >> 3 is not -1.
1 parent 5639ba4 commit 7d3a511

6 files changed

Lines changed: 76 additions & 12 deletions

File tree

Include/Python.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
3131

3232
#include "patchlevel.h"
3333
#include "config.h"
34+
#include "pyport.h"
3435

3536
/* config.h may or may not define DL_IMPORT */
3637
#ifndef DL_IMPORT /* declarations for DLL import/export */

Include/longintrepr.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
1818

1919
/* Parameters of the long integer representation.
2020
These shouldn't have to be changed as C should guarantee that a short
21-
contains at least 16 bits, but it's made changeable any way.
21+
contains at least 16 bits, but it's made changeable anyway.
2222
Note: 'digit' should be able to hold 2*MASK+1, and 'twodigits'
2323
should be able to hold the intermediate results in 'mul'
2424
(at most MASK << SHIFT).
@@ -28,8 +28,9 @@ redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
2828

2929
typedef unsigned short digit;
3030
typedef unsigned int wdigit; /* digit widened to parameter size */
31-
typedef unsigned long twodigits;
32-
typedef long stwodigits; /* signed variant of twodigits */
31+
#define BASE_TWODIGITS_TYPE long
32+
typedef unsigned BASE_TWODIGITS_TYPE twodigits;
33+
typedef BASE_TWODIGITS_TYPE stwodigits; /* signed variant of twodigits */
3334

3435
#define SHIFT 15
3536
#define BASE ((digit)1 << SHIFT)

Include/pyport.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/***********************************************************
2+
Copyright (c) 2000, BeOpen.com.
3+
All rights reserved.
4+
5+
See the file "Misc/COPYRIGHT" for information on usage and
6+
redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
7+
******************************************************************/
8+
9+
#ifndef Py_PYPORT_H
10+
#define Py_PYPORT_H 1
11+
12+
/**************************************************************************
13+
Symbols and macros to supply platform-independent interfaces to basic
14+
C-language operations whose spellings vary across platforms.
15+
16+
Please try to make documentation here as clear as possible: by definition,
17+
the stuff here is trying to illuminate C's darkest corners.
18+
19+
Config #defines referenced here:
20+
21+
SIGNED_RIGHT_SHIFT_ZERO_FILLS
22+
Meaning: To be defined iff i>>j does not extend the sign bit when i is a
23+
signed integral type and i < 0.
24+
Used in: Py_ARITHMETIC_RIGHT_SHIFT
25+
**************************************************************************/
26+
27+
28+
#ifdef __cplusplus
29+
extern "C" {
30+
#endif
31+
32+
/* Py_ARITHMETIC_RIGHT_SHIFT
33+
* C doesn't define whether a right-shift of a signed integer sign-extends
34+
* or zero-fills. Here a macro to force sign extension:
35+
* Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J)
36+
* Return I >> J, forcing sign extension.
37+
* Requirements:
38+
* I is of basic signed type TYPE (char, short, int, long, or long long).
39+
* TYPE is one of char, short, int, long, or long long, although long long
40+
* must not be used except on platforms that support it.
41+
* J is an integer >= 0 and strictly less than the number of bits in TYPE
42+
* (because C doesn't define what happens for J outside that range either).
43+
* Caution:
44+
* I may be evaluated more than once.
45+
*/
46+
#ifdef SIGNED_RIGHT_SHIFT_ZERO_FILLS
47+
#define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) \
48+
((I) < 0 ? ~((~(unsigned TYPE)(I)) >> (J)) : (I) >> (J))
49+
#else
50+
#define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) ((I) >> (J))
51+
#endif
52+
53+
#ifdef __cplusplus
54+
}
55+
#endif
56+
57+
#endif /* Py_PYPORT_H */

Objects/intobject.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -721,10 +721,7 @@ int_rshift(v, w)
721721
a = 0;
722722
}
723723
else {
724-
if (a < 0)
725-
a = ~( ~(unsigned long)a >> b );
726-
else
727-
a = (unsigned long)a >> b;
724+
a = Py_ARITHMETIC_RIGHT_SHIFT(long, a, b);
728725
}
729726
return PyInt_FromLong(a);
730727
}

Objects/longobject.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,8 @@ long_format(PyObject *aa, int base, int addL)
571571
int last = abs(a->ob_size);
572572
int basebits = 1;
573573
i = base;
574-
while ((i >>= 1) > 1) ++basebits;
574+
while ((i >>= 1) > 1)
575+
++basebits;
575576

576577
i = 0;
577578
for (;;) {
@@ -853,7 +854,9 @@ x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
853854
carry += v->ob_digit[i+k] - z
854855
+ ((twodigits)zz << SHIFT);
855856
v->ob_digit[i+k] = carry & MASK;
856-
carry = (carry >> SHIFT) - zz;
857+
carry = Py_ARITHMETIC_RIGHT_SHIFT(BASE_TWODIGITS_TYPE,
858+
carry, SHIFT);
859+
carry -= zz;
857860
}
858861

859862
if (i+k < size_v) {
@@ -870,7 +873,9 @@ x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
870873
for (i = 0; i < size_w && i+k < size_v; ++i) {
871874
carry += v->ob_digit[i+k] + w->ob_digit[i];
872875
v->ob_digit[i+k] = carry & MASK;
873-
carry >>= SHIFT;
876+
carry = Py_ARITHMETIC_RIGHT_SHIFT(
877+
BASE_TWODIGITS_TYPE,
878+
carry, SHIFT);
874879
}
875880
}
876881
} /* for j, k */
@@ -988,8 +993,6 @@ x_add(PyLongObject *a, PyLongObject *b)
988993
for (i = 0; i < size_b; ++i) {
989994
carry += a->ob_digit[i] + b->ob_digit[i];
990995
z->ob_digit[i] = carry & MASK;
991-
/* The following assumes unsigned shifts don't
992-
propagate the sign bit. */
993996
carry >>= SHIFT;
994997
}
995998
for (; i < size_a; ++i) {

acconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,5 +169,10 @@
169169
/* Defined when any dynamic module loading is enabled */
170170
#undef HAVE_DYNAMIC_LOADING
171171

172+
/* Define if i>>j for signed int i does not extend the sign bit
173+
when i < 0
174+
*/
175+
#undef SIGNED_RIGHT_SHIFT_ZERO_FILLS
176+
172177

173178
/* Leave that blank line there-- autoheader needs it! */

0 commit comments

Comments
 (0)