Commit e427a56
Release Manager
gh-41171: Speedup and proper fix for gap conversion from sage/python integer
follow up to #41057
My machine has `sizeof(mp_limb_t) == sizeof(UInt)` true, but I've tested
both branches by changing the `if ...` to `if False` and see that the
tests pass.
the expression `num_gmp_limbs * sizeof(mp_limb_t)` cannot overflow
(because there's a buffer in memory with that size), so as long as
`sizeof(mp_limb_t) > 1` (which must be the case, see below),
`-num_gmp_limbs` cannot overflow `UInt`.
## note on word size
From `gmp-h.in`:
```
#ifdef __GMP_SHORT_LIMB
typedef unsigned int mp_limb_t;
typedef int mp_limb_signed_t;
#else
#ifdef _LONG_LONG_LIMB
typedef unsigned long long int mp_limb_t;
typedef long long int mp_limb_signed_t;
#else
typedef unsigned long int mp_limb_t;
typedef long int mp_limb_signed_t;
#endif
#endif
typedef unsigned long int mp_bitcnt_t;
/* For reference, note that the name __mpz_struct gets into C++ mangled
function names, which means although the "__" suggests an internal,
we
must leave this name for binary compatibility. */
typedef struct
{
int _mp_alloc; /* Number of *limbs* allocated and
pointed
to by the _mp_d field. */
int _mp_size; /* abs(_mp_size) is the number of limbs
the
last field points to. If _mp_size is
negative this is a negative number.
*/
mp_limb_t *_mp_d; /* Pointer to the limbs. */
} __mpz_struct;
```
From `gap/src/common.h`:
```
typedef intptr_t Int;
typedef uintptr_t UInt;
```
so their sizes need not always be equal.
## note on gap internal implementation of integer
That said, `gap/src/integer.c` contains this
```
GAP_STATIC_ASSERT( sizeof(mp_limb_t) == sizeof(UInt), "gmp limb size
incompatible with GAP word size");
```
maybe we're safe...? On the other hand it's entirely possible that gap
and sage are compared with different settings of gmp, they're distinct
shared libraries.
also there's this
```
/***********************************************************************
*****
**
** 'ADDR_INT' returns a pointer to the limbs of the large integer
'obj'.
** 'CONST_ADDR_INT' does the same, but returns a const pointer.
*/
EXPORT_INLINE UInt * ADDR_INT(Obj obj)
{
GAP_ASSERT(IS_LARGEINT(obj));
return (UInt *)ADDR_OBJ(obj);
}
static Obj GMPorINTOBJ_MPZ( mpz_t v )
{
return MakeObjInt((const UInt *)v->_mp_d, v->_mp_size);
}
```
the former is actually exported. If we're fine with using more
internals, we could call `NewBag` directly with the correct size, but I
think that's unnecessary.
### 📝 Checklist
<!-- Put an `x` in all the boxes that apply. -->
- [ ] The title is concise and informative.
- [ ] The description explains in detail what this PR is about.
- [ ] I have linked a relevant issue or discussion.
- [ ] I have created tests covering the changes.
- [ ] I have updated the documentation and checked the documentation
preview.
### ⌛ Dependencies
<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - #12345: short description why this is a dependency -->
<!-- - #34567: ... -->
URL: #41171
Reported by: user202729
Reviewer(s): Chenxin Zhong, Tobias Diez, user202729
3 files changed
+75
-117
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
| 18 | + | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| 28 | + | |
28 | 29 | | |
29 | 30 | | |
30 | 31 | | |
| |||
213 | 214 | | |
214 | 215 | | |
215 | 216 | | |
216 | | - | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
217 | 261 | | |
218 | | - | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
219 | 268 | | |
220 | 269 | | |
221 | 270 | | |
222 | | - | |
| 271 | + | |
223 | 272 | | |
224 | 273 | | |
225 | 274 | | |
226 | | - | |
| 275 | + | |
227 | 276 | | |
228 | | - | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
229 | 280 | | |
230 | | - | |
231 | | - | |
232 | | - | |
233 | | - | |
234 | | - | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | | - | |
239 | | - | |
240 | | - | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | | - | |
246 | | - | |
247 | | - | |
248 | | - | |
249 | | - | |
250 | | - | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | | - | |
256 | | - | |
257 | | - | |
258 | | - | |
259 | | - | |
260 | | - | |
261 | | - | |
262 | | - | |
263 | | - | |
264 | | - | |
265 | | - | |
| 281 | + | |
266 | 282 | | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
| 283 | + | |
271 | 284 | | |
272 | 285 | | |
273 | | - | |
274 | | - | |
275 | | - | |
276 | | - | |
277 | | - | |
278 | | - | |
279 | | - | |
280 | | - | |
281 | 286 | | |
282 | 287 | | |
283 | | - | |
284 | | - | |
285 | | - | |
286 | | - | |
287 | | - | |
288 | | - | |
289 | | - | |
290 | | - | |
291 | | - | |
292 | | - | |
293 | | - | |
294 | | - | |
295 | | - | |
296 | | - | |
297 | | - | |
298 | | - | |
299 | | - | |
300 | | - | |
301 | | - | |
302 | | - | |
303 | | - | |
304 | | - | |
305 | | - | |
306 | | - | |
307 | | - | |
308 | | - | |
309 | | - | |
310 | | - | |
311 | | - | |
312 | | - | |
313 | | - | |
314 | | - | |
315 | | - | |
316 | | - | |
317 | | - | |
318 | | - | |
319 | | - | |
320 | | - | |
321 | | - | |
322 | | - | |
323 | | - | |
324 | | - | |
325 | | - | |
326 | | - | |
327 | | - | |
328 | | - | |
329 | | - | |
330 | | - | |
331 | | - | |
332 | | - | |
333 | | - | |
334 | | - | |
335 | | - | |
336 | | - | |
337 | | - | |
338 | | - | |
339 | | - | |
340 | | - | |
341 | | - | |
342 | | - | |
343 | | - | |
344 | | - | |
| 288 | + | |
| 289 | + | |
345 | 290 | | |
346 | 291 | | |
347 | 292 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6365 | 6365 | | |
6366 | 6366 | | |
6367 | 6367 | | |
| 6368 | + | |
| 6369 | + | |
| 6370 | + | |
| 6371 | + | |
| 6372 | + | |
| 6373 | + | |
| 6374 | + | |
| 6375 | + | |
| 6376 | + | |
| 6377 | + | |
| 6378 | + | |
| 6379 | + | |
| 6380 | + | |
6368 | 6381 | | |
6369 | 6382 | | |
6370 | 6383 | | |
| |||
0 commit comments