aboutsummaryrefslogtreecommitdiff
blob: e37328155760692dc2ccf8f1ef643d35fa936c00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
https://bugs.gentoo.org/show_bug.cgi?id=266703
http://bugs.python.org/issue1762561

--- Objects/floatobject.c
+++ Objects/floatobject.c
@@ -1691,9 +1691,18 @@
 /* this is for the benefit of the pack/unpack routines below */
 
 typedef enum {
-    unknown_format, ieee_big_endian_format, ieee_little_endian_format
+    unknown_format,
+    ieee_big_endian_format,
+    ieee_little_endian_format,
+    ieee_arm_mixed_endian_format
 } float_format_type;
 
+/* byte order of a C double for each of the recognised IEEE formats */
+
+static const unsigned char BIG_ENDIAN_BYTEORDER[8] = {7,6,5,4,3,2,1,0};
+static const unsigned char LITTLE_ENDIAN_BYTEORDER[8] = {0,1,2,3,4,5,6,7};
+static const unsigned char ARM_MIXED_ENDIAN_BYTEORDER[8] = {4,5,6,7,0,1,2,3};
+
 static float_format_type double_format, float_format;
 static float_format_type detected_double_format, detected_float_format;
 
@@ -1730,6 +1739,8 @@
         return PyString_FromString("IEEE, little-endian");
     case ieee_big_endian_format:
         return PyString_FromString("IEEE, big-endian");
+    case ieee_arm_mixed_endian_format:
+        return PyString_FromString("IEEE, ARM mixed-endian");
     default:
         Py_FatalError("insane float_format or double_format");
         return NULL;
@@ -1743,8 +1754,9 @@
 "used in Python's test suite.\n"
 "\n"
 "typestr must be 'double' or 'float'.  This function returns whichever of\n"
-"'unknown', 'IEEE, big-endian' or 'IEEE, little-endian' best describes the\n"
-"format of floating point numbers used by the C type named by typestr.");
+"'unknown', 'IEEE, big-endian', 'IEEE, little-endian' or\n"
+"'IEEE, ARM mixed-endian' best describes the format of floating-point\n"
+"numbers used by the C type named by typestr.");
 
 static PyObject *
 float_setformat(PyTypeObject *v, PyObject* args)
@@ -1782,11 +1794,15 @@
     else if (strcmp(format, "IEEE, big-endian") == 0) {
         f = ieee_big_endian_format;
     }
+    else if (strcmp(format, "IEEE, ARM mixed-endian") == 0 &&
+             p == &double_format) {
+        f = ieee_arm_mixed_endian_format;
+    }
     else {
         PyErr_SetString(PyExc_ValueError,
                         "__setformat__() argument 2 must be "
-                        "'unknown', 'IEEE, little-endian' or "
-                        "'IEEE, big-endian'");
+                        "'unknown', 'IEEE, little-endian', "
+                        "'IEEE, big-endian' or 'IEEE, ARM mixed-endian'");
         return NULL;
 
     }
@@ -1809,8 +1825,10 @@
 "used in Python's test suite.\n"
 "\n"
 "typestr must be 'double' or 'float'.  fmt must be one of 'unknown',\n"
-"'IEEE, big-endian' or 'IEEE, little-endian', and in addition can only be\n"
-"one of the latter two if it appears to match the underlying C reality.\n"
+"'IEEE, big-endian', 'IEEE, little-endian' or 'IEEE, ARM mixed-endian'\n"
+"and in addition can only be one of the last three if it appears to\n"
+"match the underlying C reality.  Note that the ARM mixed-endian\n"
+"format can only be set for the 'double' type, not for 'float'.\n"
 "\n"
 "Overrides the automatic determination of C-level floating point type.\n"
 "This affects how floats are converted to and from binary strings.");
@@ -2005,7 +2023,11 @@
        Note that if we're on some whacked-out platform which uses
        IEEE formats but isn't strictly little-endian or big-
        endian, we will fall back to the portable shifts & masks
-       method. */
+       method.
+
+       Addendum: We also attempt to detect the mixed-endian IEEE format
+       used by the ARM old ABI (OABI) and also used by the FPA
+       floating-point unit on some older ARM processors. */
 
 #if SIZEOF_DOUBLE == 8
     {
@@ -2014,6 +2036,8 @@
             detected_double_format = ieee_big_endian_format;
         else if (memcmp(&x, "\x05\x04\x03\x02\x01\xff\x3f\x43", 8) == 0)
             detected_double_format = ieee_little_endian_format;
+        else if (memcmp(&x, "\x01\xff\x3f\x43\x05\x04\x03\x02", 8) == 0)
+            detected_double_format = ieee_arm_mixed_endian_format;
         else
             detected_double_format = unknown_format;
     }
@@ -2353,17 +2377,31 @@
     }
     else {
         const char *s = (char*)&x;
-        int i, incr = 1;
+        int i;
+        const unsigned char *byteorder;
 
-        if ((double_format == ieee_little_endian_format && !le)
-            || (double_format == ieee_big_endian_format && le)) {
-            p += 7;
-            incr = -1;
+        switch (double_format) {
+        case ieee_little_endian_format:
+            byteorder = LITTLE_ENDIAN_BYTEORDER;
+            break;
+        case ieee_big_endian_format:
+            byteorder = BIG_ENDIAN_BYTEORDER;
+            break;
+        case ieee_arm_mixed_endian_format:
+            byteorder = ARM_MIXED_ENDIAN_BYTEORDER;
+            break;
+        default:
+            Py_FatalError("insane float_format or double_format");
+            return -1;
         }
 
-        for (i = 0; i < 8; i++) {
-            *p = *s++;
-            p += incr;
+        if (le) {
+            for (i = 0; i < 8; i++)
+                p[byteorder[i]] = *s++;
+        }
+        else {
+            for (i = 0; i < 8; i++)
+                p[7-byteorder[i]] = *s++;
         }
         return 0;
     }
@@ -2522,22 +2560,33 @@
     }
     else {
         double x;
+        char *s = (char*)&x;
+        const unsigned char *byteorder;
+        int i;
+
+        switch (double_format) {
+        case ieee_little_endian_format:
+            byteorder = LITTLE_ENDIAN_BYTEORDER;
+            break;
+        case ieee_big_endian_format:
+            byteorder = BIG_ENDIAN_BYTEORDER;
+            break;
+        case ieee_arm_mixed_endian_format:
+            byteorder = ARM_MIXED_ENDIAN_BYTEORDER;
+            break;
+        default:
+            Py_FatalError("insane float_format or double_format");
+            return -1.0;
+        }
 
-        if ((double_format == ieee_little_endian_format && !le)
-            || (double_format == ieee_big_endian_format && le)) {
-            char buf[8];
-            char *d = &buf[7];
-            int i;
-
-            for (i = 0; i < 8; i++) {
-                *d-- = *p++;
-            }
-            memcpy(&x, buf, 8);
+        if (le) {
+            for (i=0; i<8; i++)
+                *s++ = p[byteorder[i]];
         }
         else {
-            memcpy(&x, p, 8);
+            for (i=0; i<8; i++)
+                *s++ = p[7-byteorder[i]];
         }
-
         return x;
     }
 }