micropython/py
Damien George 0a59938574 py/mpz: Fix overflow of borrow in mpn_div.
For certain operands to mpn_div, the existing code path for
`DIG_SIZE == MPZ_DBL_DIG_SIZE / 2` had a bug in it where borrow could still
overflow in the `(x >= *n || *n - x <= borrow)` branch, ie
`borrow + x - (mpz_dbl_dig_t)*n` overflows the borrow variable.  In such
cases the subsequent right-shift of borrow would not bring in the overflow
bit, leading to an error in the result.  An example division that had
overflow when MPZ_DIG_SIZE = 16 is `(2 ** 48 - 1) ** 2 // (2 ** 48 - 1)`.

This is fixed in this commit by simplifying the code and handling the low
digits of borrow first, and then the upper bits (to shift down) separately.
There is no longer a distinction between `DIG_SIZE < MPZ_DBL_DIG_SIZE / 2`
and `DIG_SIZE == MPZ_DBL_DIG_SIZE / 2`.

This commit also simplifies the second part of the calculation so that
borrow does not need to be negated (instead the code just works knowing
that borrow is negative and using + instead of - in calculations involving
borrow).

Fixes #6777.

Signed-off-by: Damien George <damien@micropython.org>
2021-02-08 11:50:05 +11:00
..
argcheck.c
asmarm.c
asmarm.h
asmbase.c
asmbase.h
asmthumb.c py/emitnative: Support binary ops on ARMv6M without use of ite instr. 2021-01-29 23:57:10 +11:00
asmthumb.h py/emitnative: Support binary ops on ARMv6M without use of ite instr. 2021-01-29 23:57:10 +11:00
asmx64.c
asmx64.h
asmx86.c
asmx86.h
asmxtensa.c
asmxtensa.h
bc.c
bc.h py/showbc: Pass in an mp_print_t struct to all bytecode-print functions. 2020-09-11 17:22:28 +10:00
bc0.h
binary.c all: Rename BYTES_PER_WORD to MP_BYTES_PER_OBJ_WORD. 2021-02-04 22:46:42 +11:00
binary.h
builtin.h
builtinevex.c
builtinhelp.c
builtinimport.c
compile.c
compile.h
dynruntime.h py/dynruntime.h: Add mp_import_* and mp_load/store_*. 2020-09-18 18:34:02 +10:00
dynruntime.mk
emit.h
emitbc.c all: Rename BYTES_PER_WORD to MP_BYTES_PER_OBJ_WORD. 2021-02-04 22:46:42 +11:00
emitcommon.c
emitglue.c py/showbc: Pass in an mp_print_t struct to all bytecode-print functions. 2020-09-11 17:22:28 +10:00
emitglue.h
emitinlinethumb.c py/emitinlinethumb: Exclude code using #if when ARMV7M disabled. 2021-01-29 23:57:10 +11:00
emitinlinextensa.c py: Use unsigned comparison of chars. 2020-10-22 11:47:36 +02:00
emitnarm.c
emitnative.c py/emitnative: Support binary ops on ARMv6M without use of ite instr. 2021-01-29 23:57:10 +11:00
emitnthumb.c
emitnx64.c
emitnx86.c
emitnxtensa.c
emitnxtensawin.c
formatfloat.c
formatfloat.h
frozenmod.c
frozenmod.h
gc.c all: Rename BYTES_PER_WORD to MP_BYTES_PER_OBJ_WORD. 2021-02-04 22:46:42 +11:00
gc.h py/gc: Change include of stdint.h to stddef.h. 2021-02-05 15:46:56 +11:00
grammar.h
lexer.c py, extmod: Introduce and use MP_FALLTHROUGH macro. 2020-10-22 11:53:16 +02:00
lexer.h
makecompresseddata.py
makemoduledefs.py all: Update Python code to conform to latest black formatting. 2020-08-29 15:18:01 +10:00
makeqstrdata.py
makeqstrdefs.py tools/makeqstrdefs.py: Run qstr preprocessing in parallel. 2020-11-12 15:04:53 +11:00
makeversionhdr.py py/makeversionhdr: Honor SOURCE_DATE_EPOCH if present. 2021-01-31 17:48:59 +01:00
malloc.c
map.c py/objdict: Add mp_const_empty_dict_obj, use it for mp_const_empty_map. 2020-10-10 00:16:26 +11:00
misc.h extmod/modbluetooth: Add API for L2CAP channels. 2020-11-24 01:07:17 +11:00
mkenv.mk
mkrules.mk py/mkrules.mk: Remove stray vpath and unused -Itmp, add $(Q) for $(AR). 2020-12-14 13:57:15 +11:00
modarray.c
modbuiltins.c
modcmath.c
modcollections.c
modgc.c
modio.c
modmath.c py/modmath: Simplify handling of positional args to reduce code size. 2020-12-14 13:30:56 +11:00
modmicropython.c
modstruct.c
modsys.c
modthread.c
moduerrno.c
mpconfig.h py: Rename WORD_MSBIT_HIGH to MP_OBJ_WORD_MSBIT_HIGH. 2021-02-04 22:46:42 +11:00
mperrno.h
mphal.h all: Rename absolute time-based functions to include "epoch". 2020-09-18 17:20:34 +10:00
mpprint.c py/mpprint: Fix length calculation for strings with precision-modifier. 2020-12-07 23:32:06 +11:00
mpprint.h
mpstate.c
mpstate.h
mpthread.h
mpz.c py/mpz: Fix overflow of borrow in mpn_div. 2021-02-08 11:50:05 +11:00
mpz.h
nativeglue.c
nativeglue.h
nlr.c
nlr.h
nlrpowerpc.c
nlrsetjmp.c
nlrthumb.c
nlrx64.c
nlrx86.c
nlrxtensa.c
obj.c
obj.h py/objdict: Add mp_const_empty_dict_obj, use it for mp_const_empty_map. 2020-10-10 00:16:26 +11:00
objarray.c
objarray.h py/objarray.h: Add mp_obj_memoryview_init() helper function. 2020-09-25 12:23:11 +10:00
objattrtuple.c
objbool.c
objboundmeth.c
objcell.c
objclosure.c
objcomplex.c
objdeque.c
objdict.c py/objdict: Add mp_const_empty_dict_obj, use it for mp_const_empty_map. 2020-10-10 00:16:26 +11:00
objenumerate.c
objexcept.c py/objexcept: Compare mp_emergency_exception_buf_size signed. 2020-10-22 11:47:36 +02:00
objexcept.h
objfilter.c
objfloat.c py: Rename BITS_PER_BYTE to MP_BITS_PER_BYTE. 2021-02-04 22:46:42 +11:00
objfun.c py/objfun: Support fun.__globals__ attribute. 2021-01-29 23:57:10 +11:00
objfun.h
objgenerator.c
objgenerator.h
objgetitemiter.c
objint.c py: Rename BITS_PER_BYTE to MP_BITS_PER_BYTE. 2021-02-04 22:46:42 +11:00
objint.h
objint_longlong.c
objint_mpz.c py/mpz: Do sign extension in mpz_as_bytes for negative values. 2020-11-11 22:18:24 +11:00
objlist.c
objlist.h
objmap.c
objmodule.c all: Rename "sys" module to "usys". 2020-09-04 00:10:24 +10:00
objmodule.h
objnamedtuple.c
objnamedtuple.h
objnone.c
objobject.c
objpolyiter.c
objproperty.c
objrange.c
objreversed.c
objset.c py, extmod: Introduce and use MP_FALLTHROUGH macro. 2020-10-22 11:53:16 +02:00
objsingleton.c
objslice.c
objstr.c py/mpprint: Fix length calculation for strings with precision-modifier. 2020-12-07 23:32:06 +11:00
objstr.h
objstringio.c
objstringio.h
objstrunicode.c py/mpprint: Fix length calculation for strings with precision-modifier. 2020-12-07 23:32:06 +11:00
objtuple.c
objtuple.h
objtype.c py/objtype: Handle __dict__ attribute when type has no locals. 2020-10-10 00:16:32 +11:00
objtype.h
objzip.c
opmethods.c
pairheap.c
pairheap.h
parse.c py/parse: Expose rule-name printing as MICROPY_DEBUG_PARSE_RULE_NAME. 2020-10-01 15:26:43 +10:00
parse.h py/parse: Pass in an mp_print_t to mp_parse_node_print. 2020-09-11 23:00:03 +10:00
parsenum.c
parsenum.h
parsenumbase.c
parsenumbase.h
persistentcode.c all: Rename BYTES_PER_WORD to MP_BYTES_PER_OBJ_WORD. 2021-02-04 22:46:42 +11:00
persistentcode.h
profile.c
profile.h
py.mk py/py.mk: Support C++ code for user C modules. 2020-10-29 15:29:20 +11:00
pystack.c
pystack.h
qstr.c
qstr.h py/qstr.h: Remove QSTR_FROM_STR_STATIC macro. 2021-01-30 13:40:48 +11:00
qstrdefs.h
reader.c
reader.h
repl.c
repl.h
ringbuf.c
ringbuf.h
runtime.c py: Rename BITS_PER_BYTE to MP_BITS_PER_BYTE. 2021-02-04 22:46:42 +11:00
runtime.h
runtime0.h
runtime_utils.c
scheduler.c
scope.c py/scope: Name and use id_kind_type_t. 2020-10-22 11:40:56 +02:00
scope.h py/scope: Name and use id_kind_type_t. 2020-10-22 11:40:56 +02:00
sequence.c
showbc.c py/showbc: Pass in an mp_print_t struct to all bytecode-print functions. 2020-09-11 17:22:28 +10:00
smallint.c
smallint.h py: Rename WORD_MSBIT_HIGH to MP_OBJ_WORD_MSBIT_HIGH. 2021-02-04 22:46:42 +11:00
stackctrl.c
stackctrl.h
stream.c
stream.h
unicode.c
unicode.h
vm.c py/showbc: Pass in an mp_print_t struct to all bytecode-print functions. 2020-09-11 17:22:28 +10:00
vmentrytable.h py/vmentrytable: Ignore GCC -Woverride-init. 2020-10-22 11:47:36 +02:00
vstr.c
warning.c