8d8f83bc05
F-strings, new machine.I2S class, ESP32-C3 support and LEGO_HUB_NO6 board This release of MicroPython adds support for f-strings (PEP-498), with a few limitations compared to normal Python. F-strings are essentially syntactic sugar for "".format() and make formatting strings a lot more convenient. Other improvements to the core runtime include pretty printing OSError when it has two arguments (an errno code and a string), scheduling of KeyboardInterrupt on the main thread, and support for a single argument to the optimised form of StopIteration. In the machine module a new I2S class has been added, with support for esp32 and stm32 ports. This provides a consistent API for transmit and receive of audio data in blocking, non-blocking and asyncio-based operation. Also, the json module has support for the "separators" argument in the dump and dumps functions, and framebuf now includes a way to blit between frame buffers of different formats using a palette. A new, portable machine.bitstream function is also added which can output a stream of bits with configurable timing, and is used as the basis for driving WS2812 LEDs in a common way across ports. There has been some restructuring of the repository directory layout, with all third-party code now in the lib/ directory. And a new top-level directory shared/ has been added with first-party code that was previously in lib/ moved there. The docs have seen further improvement with enhancements and additions to the rp2 parts, as well as a new quick reference for the zephyr port. The terms master/slave have been replaced with controller/peripheral, mainly relating to I2C and SPI usage. And u-module references have been replaced with just the module name without the u-prefix to help clear up the intended usage of modules in MicroPython. For the esp8266 and esp32 ports, hidden networks are now included in WLAN scan results. On the esp32 the RMT class is enhanced with idle_level and write_pulses modes. There is initial support for ESP32-C3 chips with GENERIC_C3 and GENERIC_C3_USB boards. The javascript port has had its Makefile and garbage collector implementation reworked so it compiles and runs with latest the Emscripten using asyncify. The mimxrt port sees the addition of hardware I2C and SPI support, as well as some additional methods to the machine module. There is also support for Hyperflash chips. The nrf port now has full VFS storage support, enables source-line on traceback, and has .mpy features consistent with other ports. For the rp2 port there is now more configurability for boards, and more boards added. The stm32 port has a new LEGO_HUB_NO6 board definition with detailed information how to get this LEGO Hub running stock MicroPython. There is also now support to change the CPU frequency on STM32WB MCUs. And USBD_xxx descriptor options have been renamed to MICROPY_HW_USB_xxx. Thanks to everyone who contributed to this release: Amir Gonnen, Andrew Scheller, Bryan Tong Minh, Chris Wilson, Damien George, Daniel Mizyrycki, David Lechner, David P, Fernando, finefoot, Frank Pilhofer, Glenn Ruben Bakke, iabdalkader, Jeff Epler, Jim Mussared, Jonathan Hogg, Josh Klar, Josh Lloyd, Julia Hathaway, Krzysztof Adamski, Matúš Olekšák, Michael Weiss, Michel Bouwmans, Mike Causer, Mike Teachman, Ned Konz, NitiKaur, oclyke, Patrick Van Oosterwijck, Peter Hinch, Peter Züger, Philipp Ebensberger, robert-hh, Roberto Colistete Jr, Sashkoiv, Seon Rozenblum, Tobias Thyrrestrup, Tom McDermott, Will Sowerbutts, Yonatan Goldschmidt. What follows is a detailed list of changes, generated from the git commit history, and organised into sections. Main components =============== all: - fix signed shifts and NULL access errors from -fsanitize=undefined - update to point to files in new shared/ directory py core: - mpstate: make exceptions thread-local - mpstate: schedule KeyboardInterrupt on main thread - mperrno: add MP_ECANCELED error code - makeqstrdefs.py: don't include .h files explicitly in preprocessing - mark unused arguments from bytecode decoding macros - objexcept: pretty print OSError also when it has 2 arguments - makeversionhdr: add --tags arg to git describe - vm: simplify handling of MP_OBJ_STOP_ITERATION in yield-from opcode - objexcept: make mp_obj_exception_get_value support subclassed excs - support single argument to optimised MP_OBJ_STOP_ITERATION - introduce and use mp_raise_type_arg helper - modsys: optimise sys.exit for code size by using exception helpers - objexcept: make mp_obj_new_exception_arg1 inline - obj: fix formatting of comment for mp_obj_is_integer - emitnative: reuse need_reg_all func in need_stack_settled - emitnative: ensure stack settling is safe mid-branch - runtime: fix bool unary op for subclasses of native types - builtinimport: fix condition for including do_execute_raw_code() - mkrules: automatically build mpy-cross if it doesn't exist - implement partial PEP-498 (f-string) support - lexer: clear fstring_args vstr on lexer free - mkrules.mk: do submodule sync in "make submodules" extmod: - btstack: add missing call to mp_bluetooth_hci_uart_deinit - btstack: check that BLE is active before performing operations - uasyncio: get addr and bind server socket before creating task - axtls-include: add axtls_os_port.h to customise axTLS - update for move of crypto-algorithms, re1.5, uzlib to lib - moduselect: conditionally compile select() - nimble: fix leak in l2cap_send if send-while-stalled - btstack/btstack.mk: use -Wno-implicit-fallthrough, not =0 - utime: always invoke mp_hal_delay_ms when >= to 0ms - modbluetooth: clamp MTU values to 32->UINT16_MAX - nimble: allow modbluetooth binding to hook "sent HCI packet" - nimble: add "memory stalling" mechanism for l2cap_send - uasyncio: in open_connection use address info in socket creation - modujson: add support for dump/dumps separators keyword-argument - modlwip: fix close and clean up of UDP and raw sockets - modbluetooth: add send_update arg to gatts_write - add machine.bitstream - modframebuf: enable blit between different formats via a palette lib: - tinyusb: update to version 0.10.1 - pico-sdk: update to version 1.2.0 - utils/stdout_helpers: make mp_hal_stdout_tx_strn_cooked efficient - axtls: switch to repo at micropython/axtls - axtls: update to latest axtls 2.1.5 wih additional commits - re1.5: move re1.5 code from extmod to lib - uzlib: move uzlib code from extmod to lib - crypto-algorithms: move crypto-algorithms code from extmod to lib - update README's based on contents of these dirs drivers: - neopixel: add common machine.bitstream-based neopixel module - neopixel: optimize fill() for speed - neopixel: reduce code size of driver - cyw43: fix cyw43_deinit so it can be called many times in a row - cyw43: make wifi join fail if interface is not active mpy-cross: - disable stack check when building with Emscripten Support components ================== docs: - library: document new esp32.RMT features and fix wait_done - library: warn that ustruct doesn't handle spaces in format strings - esp8266/tutorial: change flash mode from dio to dout - replace master/slave with controller/peripheral in I2C and SPI - rp2: enhance quickref and change image to Pico pinout - rp2: update general section to give a brief technical overview - library/utime.rst: clarify behaviour and precision of sleep ms/us - library/uasyncio.rst: document stream readexactly() method - library/machine.I2S.rst: fix use of sd pin in examples - zephyr: add quick reference for the Zephyr port - library/zephyr: add libraries specific to the Zephyr port - templates: add unix and zephyr quickref links to top-index - rename ufoo.rst to foo.rst - replace ufoo with foo in all docs - library/index.rst: clarify module naming and purpose - library/builtins.rst: add module title - library/network.rst: simplify socket import - add docs for machine.bitstream and neopixel module - library: fix usage of :term: for frozen module reference - esp8266: use monospace for software tools - reference: mention that slicing a memoryview causes allocation examples: no changes specific to this component/port tests: - extmod: make uasyncio_heaplock test more deterministic - cpydiff/modules_struct_whitespace_in_format: run black - extmod/ujson: add tests for dump/dumps separators argument - run-multitests.py: add broadcast and wait facility - multi_bluetooth/ble_subscribe.py: add test for subscription - extmod/vfs_fat_finaliser.py: ensure alloc at never-used GC blocks - basics: split f-string debug printing to separate file with .exp - pybnative: make while.py test run on boards without pyb.delay tools: - autobuild: add scripts to build release firmware - remove obsolete build-stm-latest.sh script - ci.sh: run apt-get update in ci_powerpc_setup - makemanifest.py: allow passing flags to mpy-tool.py - autobuild: add mimxrt port to build scripts for nightly builds - pyboard.py: add cmd-line option to make soft reset configurable - mpremote: swap order of PID and VID in connect-list output - ci.sh: build unix dev variant as part of macOS CI - ci.sh: build GENERIC_C3 board as part of esp32 CI - autobuild: use separate IDF version to build newer esp32 SoCs - autobuild: add FeatherS2 and TinyS2 to esp32 auto builds - mpremote: add seek whence for mounted files - mpremote: raise OSError on unsupported RemoteFile.seek - autobuild: add the MIMXRT1050_EVKB board to the daily builds - ci.sh: add mpy-cross build to nrf port - codeformat.py: include ports/nrf/modules/nrf in code formatting - gen-cpydiff.py: don't rename foo to ufoo in diff output - autobuild: add auto build for Silicognition wESP32 - mpremote: fix connect-list in case VID/PID are None - mpremote: add "devs" shortcut for "connect list" - mpremote: remove support for pyb.USB_VCP in/out specialisation - autobuild: don't use "-B" for make, it's already a fresh build - pyboard.py: move --no-exclusive/--soft-reset out of mutex group - pyboard.py: make --no-follow use same variable as --follow - pyboard.py: add --exclusive to match --no-exclusive - pyboard.py: make --no-soft-reset consistent with other args - uncrustify: force 1 newline at end of file - mpremote: bump version to 0.0.6 CI: - workflows: add workflow to build and test javascript port - workflows: switch from Coveralls to Codecov - workflows: switch from lcov to gcov - workflows: add workflow to build and test unix dev variant The ports ========= all ports: - use common mp_hal_stdout_tx_strn_cooked instead of custom one - update for move of crypto-algorithms, uzlib to lib - rename USBD_VID/PID config macros to MICROPY_HW_USB_VID/PID bare-arm port: no changes specific to this component/port cc3200 port: no changes specific to this component/port esp8266 port: - add __len__ to NeoPixel driver to support iterating - Makefile: add more libm files to build - include hidden networks in WLAN.scan results - replace esp.neopixel with machine.bitstream - remove dead code for end_ticks in machine_bitstream esp32 port: - boards/sdkconfig.base: disable MEMPROT_FEATURE to alloc from IRAM - add __len__ to NeoPixel driver to support iterating - main: allow MICROPY_DIR to be overridden - esp32_rmt: fix RMT looping in newer IDF versions - esp32_rmt: enhance RMT with idle_level and write_pulses modes - add new machine.I2S class for I2S protocol support - machine_spi: calculate actual attained baudrate - machine_hw_spi: use a 2 item SPI queue for long transfers - machine_dac: add MICROPY_PY_MACHINE_DAC option, enable by default - machine_i2s: add MICROPY_PY_MACHINE_I2S option, enable by default - fix use of mp_int_t, size_t and uintptr_t - add initial support for ESP32C3 SoCs - boards/GENERIC_C3: add generic C3-based board - modmachine: release the GIL in machine.idle() - mphalport: always yield at least once in delay_ms - machine_uart: add flow kw-arg to enable hardware flow control - boards: add Silicognition wESP32 board configuration - mpconfigport.h: enable reverse and inplace special methods - include hidden networks in WLAN.scan results - makeimg.py: get bootloader and partition offset from sdkconfig - enable MICROPY_PY_FSTRINGS by default - machine_hw_spi: release GIL during transfers - machine_pin: make check for non-output pins respect chip variant - replace esp.neopixel with machine.bitstream - remove dead code for end_ticks in machine_bitstream - boards: add GENERIC_C3_USB board with USB serial/JTAG support javascript port: - rework Makefile and GC so it works with latest Emscripten - Makefile: suppress compiler errors from array bounds - Makefile: change variable to EXPORTED_RUNTIME_METHODS mimxrt port: - move calc_weekday helper function to timeutils - machine_spi: add the SPI class to the machine module - moduos: seed the PRNG on boot using the TRNG - boards: set vfs partition start to 1 MBbyte - main: skip running main.py if boot.py failed - main: extend the information returned by help() - mimxrt_flash: remove commented-out code - modmachine: add a few minor methods to the machine module - machine_led: use mp_raise_msg_varg helper - machine_i2c: add hardware-based machine.I2C to machine module - add support for Hyperflash chips - boards: add support for the MIMXRT1050_EVKB board - machine_pin: implement ioctl for Pin minimal port: - Makefile: add support for building with user C modules nrf port: - modules: replace master/slave with controller/peripheral in SPI - boards/common.ld: calculate unused flash region - modules/nrf: add new nrf module with flash block device - drivers: add support for using flash block device with SoftDevice - mpconfigport.h: expose nrf module when MICROPY_PY_NRF is set - README: update README.md to reflect internal file systems - mpconfigport.h: tune FAT FS configuration - Makefile: add _fs_size linker script override from make - modules/uos: allow a board to configure MICROPY_VFS_FAT/LFS1/LFS2 - mpconfigport.h: enable MICROPY_PY_IO_FILEIO when an FS is enabled - qstrdefsport.h: add entries for in-built FS mount points - main: add auto mount and auto format hook for internal flash FS - boards: enable needed features for FAT/LFS1/LFS2 - facilitate use of freeze manifest - boards: set FROZEN_MANIFEST blank when SD present on nrf51 targets - modules/scripts: add file system formatting script - Makefile: set default manifest file for all targets - mphalport: add dummy function for mp_hal_time_ns() - boards: enable MICROPY_VFS_LFS2 for all target boards - modules/uos: add ilistdir to uos module - modules/nrf: add function to enable/disable DCDC - enable source line on tracebacks - set .mpy features consistent with documentation and other ports pic16bit port: no changes specific to this component/port powerpc port: no changes specific to this component/port qemu-arm port: no changes specific to this component/port rp2 port: - use 0=Monday datetime convention in RTC - machine_rtc: in RTC.datetime, compute weekday automatically - CMakeLists.txt: suppress compiler errors for pico-sdk and tinyusb - tusb_config.h: set CFG_TUD_CDC_EP_BUFSIZE to 256 - machine_uart: add hardware flow control support - machine_uart: allow overriding default machine UART pins - machine_i2c: allow boards to configure I2C pins using new macros - machine_spi: allow boards to configure SPI pins using new macros - machine_uart: fix poll ioctl to also check hardware FIFO - machine_uart: fix read when FIFO has chars but ringbuf doesn't - tusb_port: allow boards to configure USB VID and PID - boards/ADAFRUIT_FEATHER_RP2040: configure custom VID/PID - boards/ADAFRUIT_FEATHER_RP2040: configure I2C/SPI default pins - boards/SPARKFUN_PROMICRO: configure UART/I2C/SPI default pins - boards/SPARKFUN_THINGPLUS: configure I2C/SPI default pins - boards: add Adafruit ItsyBitsy RP2040 - boards: add Adafruit QT Py RP2040 - boards: add Pimoroni Pico LiPo 4MB - boards: add Pimoroni Pico LiPo 16MB - boards: add Pimoroni Tiny 2040 - CMakeLists.txt: allow a board's cmake to set the manifest path - enable MICROPY_PY_FSTRINGS by default - Makefile: add "submodules" target, to match other ports - rp2_flash: disable IRQs while calling flash_erase/program - CMakeLists.txt: add option to enable double tap reset to bootrom - mpconfigport.h: allow boards to add root pointers samd port: - add support for building with user C modules stm32 port: - softtimer: add soft_timer_reinsert() helper function - mpbthciport: change from systick to soft-timer for BT scheduling - provide a custom BTstack runloop that integrates with soft timer - usb: make irq's default trigger enable all events - boardctrl: skip running main.py if boot.py had an error - sdio: fix undefined reference to DMA stream on H7 - dma: add DMAMUX configuration for H7 to fix dma_nohal_init - main: call mp_deinit() at end of main - adc: allow using ADC12 and ADC3 for H7 - adc: define the ADC instance used for internal channels - adc: simplify and generalise how pin_adcX table is defined - add new machine.I2S class for I2S protocol support - boards/NUCLEO_F446RE: fix I2C1 pin assignment to match datasheet - replace master/slave with controller/peripheral in I2C and SPI - systick: always POLL_HOOK when delaying for milliseconds - sdram: make SDRAM test cache aware, and optional failure with msg - boards/NUCLEO_F446RE: enable CAN bus support - boards: add support for SparkFun STM32 MicroMod Processor board - uart: fix LPUART1 baudrate set/get - uart: support low baudrates on LPUART1 - boards/STM32F429DISC: set correct UART2 pins and add UART3/6 - boards/NUCLEO_F439ZI: add board definition for NUCLEO_F439ZI - boards/LEGO_HUB_NO6: add board definition for LEGO_HUB_NO6 - Makefile: update to only pull in used Bluetooth library - README.md: update supported MCUs, and submodule and mboot use - usbd_desc: rename USBD_xxx descriptor opts to MICROPY_HW_USB_xxx - usbd_cdc_interface: rename USBD_CDC_xx opts to MICROPY_HW_USB_xx - powerctrl: support changing frequency on WB MCUs - boards/NUCLEO_H743ZI2: add modified version of NUCLEO_H743ZI - mbedtls: fix compile warning about uninitialized val - enable MICROPY_PY_FSTRINGS by default - add implementation of machine.bitstream - Makefile: allow GIT_SUBMODULES and LIBS to be extended - stm32_it: support TIM17 IRQs on WB MCUs - disable computed goto on constrained boards - storage: make extended-block-device more configurable - boards/LEGO_HUB_NO6: change SPI flash storage to use hardware SPI - boards/LEGO_HUB_NO6: skip first 1MiB of SPI flash for storage - boards/LEGO_HUB_NO6: add make commands to backup/restore firmware teensy port: no changes specific to this component/port unix port: - modffi: add option to lock GC in callback, and cfun access - Makefile: add back LIB_SRC_C to list of object files - variants: enable help and help("modules") on standard and dev - Makefile: disable error compression on arm-linux-gnueabi-gcc windows port: - Makefile: add .exe extension to executables name - appveyor: update to VS 2017 and use Python 3.8 for build/test zephyr port: - machine_spi: add support for hardware SPI
1748 lines
47 KiB
C
1748 lines
47 KiB
C
/*
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
*
|
|
* The MIT License (MIT)
|
|
*
|
|
* SPDX-FileCopyrightText: Copyright (c) 2013, 2014 Damien P. George
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in
|
|
* all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
* THE SOFTWARE.
|
|
*/
|
|
|
|
#include <string.h>
|
|
#include <assert.h>
|
|
|
|
#include "py/mpz.h"
|
|
|
|
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
|
|
|
|
#define DIG_SIZE (MPZ_DIG_SIZE)
|
|
#define DIG_MASK ((MPZ_LONG_1 << DIG_SIZE) - 1)
|
|
#define DIG_MSB (MPZ_LONG_1 << (DIG_SIZE - 1))
|
|
#define DIG_BASE (MPZ_LONG_1 << DIG_SIZE)
|
|
|
|
/*
|
|
mpz is an arbitrary precision integer type with a public API.
|
|
|
|
mpn functions act on non-negative integers represented by an array of generalised
|
|
digits (eg a word per digit). You also need to specify separately the length of the
|
|
array. There is no public API for mpn. Rather, the functions are used by mpz to
|
|
implement its features.
|
|
|
|
Integer values are stored little endian (first digit is first in memory).
|
|
|
|
Definition of normalise: ?
|
|
*/
|
|
|
|
STATIC size_t mpn_remove_trailing_zeros(mpz_dig_t *oidig, mpz_dig_t *idig) {
|
|
for (--idig; idig >= oidig && *idig == 0; --idig) {
|
|
}
|
|
return idig + 1 - oidig;
|
|
}
|
|
|
|
/* compares i with j
|
|
returns sign(i - j)
|
|
assumes i, j are normalised
|
|
*/
|
|
STATIC int mpn_cmp(const mpz_dig_t *idig, size_t ilen, const mpz_dig_t *jdig, size_t jlen) {
|
|
if (ilen < jlen) {
|
|
return -1;
|
|
}
|
|
if (ilen > jlen) {
|
|
return 1;
|
|
}
|
|
|
|
for (idig += ilen, jdig += ilen; ilen > 0; --ilen) {
|
|
mpz_dbl_dig_signed_t cmp = (mpz_dbl_dig_t)*(--idig) - (mpz_dbl_dig_t)*(--jdig);
|
|
if (cmp < 0) {
|
|
return -1;
|
|
}
|
|
if (cmp > 0) {
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/* computes i = j << n
|
|
returns number of digits in i
|
|
assumes enough memory in i; assumes normalised j; assumes n > 0
|
|
can have i, j pointing to same memory
|
|
*/
|
|
STATIC size_t mpn_shl(mpz_dig_t *idig, mpz_dig_t *jdig, size_t jlen, mp_uint_t n) {
|
|
mp_uint_t n_whole = (n + DIG_SIZE - 1) / DIG_SIZE;
|
|
mp_uint_t n_part = n % DIG_SIZE;
|
|
if (n_part == 0) {
|
|
n_part = DIG_SIZE;
|
|
}
|
|
|
|
// start from the high end of the digit arrays
|
|
idig += jlen + n_whole - 1;
|
|
jdig += jlen - 1;
|
|
|
|
// shift the digits
|
|
mpz_dbl_dig_t d = 0;
|
|
for (size_t i = jlen; i > 0; i--, idig--, jdig--) {
|
|
d |= *jdig;
|
|
*idig = (d >> (DIG_SIZE - n_part)) & DIG_MASK;
|
|
d <<= DIG_SIZE;
|
|
}
|
|
|
|
// store remaining bits
|
|
*idig = (d >> (DIG_SIZE - n_part)) & DIG_MASK;
|
|
idig -= n_whole - 1;
|
|
memset(idig, 0, (n_whole - 1) * sizeof(mpz_dig_t));
|
|
|
|
// work out length of result
|
|
jlen += n_whole;
|
|
while (jlen != 0 && idig[jlen - 1] == 0) {
|
|
jlen--;
|
|
}
|
|
|
|
// return length of result
|
|
return jlen;
|
|
}
|
|
|
|
/* computes i = j >> n
|
|
returns number of digits in i
|
|
assumes enough memory in i; assumes normalised j; assumes n > 0
|
|
can have i, j pointing to same memory
|
|
*/
|
|
STATIC size_t mpn_shr(mpz_dig_t *idig, mpz_dig_t *jdig, size_t jlen, mp_uint_t n) {
|
|
mp_uint_t n_whole = n / DIG_SIZE;
|
|
mp_uint_t n_part = n % DIG_SIZE;
|
|
|
|
if (n_whole >= jlen) {
|
|
return 0;
|
|
}
|
|
|
|
jdig += n_whole;
|
|
jlen -= n_whole;
|
|
|
|
for (size_t i = jlen; i > 0; i--, idig++, jdig++) {
|
|
mpz_dbl_dig_t d = *jdig;
|
|
if (i > 1) {
|
|
d |= (mpz_dbl_dig_t)jdig[1] << DIG_SIZE;
|
|
}
|
|
d >>= n_part;
|
|
*idig = d & DIG_MASK;
|
|
}
|
|
|
|
if (idig[-1] == 0) {
|
|
jlen--;
|
|
}
|
|
|
|
return jlen;
|
|
}
|
|
|
|
/* computes i = j + k
|
|
returns number of digits in i
|
|
assumes enough memory in i; assumes normalised j, k; assumes jlen >= klen
|
|
can have i, j, k pointing to same memory
|
|
*/
|
|
STATIC size_t mpn_add(mpz_dig_t *idig, const mpz_dig_t *jdig, size_t jlen, const mpz_dig_t *kdig, size_t klen) {
|
|
mpz_dig_t *oidig = idig;
|
|
mpz_dbl_dig_t carry = 0;
|
|
|
|
jlen -= klen;
|
|
|
|
for (; klen > 0; --klen, ++idig, ++jdig, ++kdig) {
|
|
carry += (mpz_dbl_dig_t)*jdig + (mpz_dbl_dig_t)*kdig;
|
|
*idig = carry & DIG_MASK;
|
|
carry >>= DIG_SIZE;
|
|
}
|
|
|
|
for (; jlen > 0; --jlen, ++idig, ++jdig) {
|
|
carry += *jdig;
|
|
*idig = carry & DIG_MASK;
|
|
carry >>= DIG_SIZE;
|
|
}
|
|
|
|
if (carry != 0) {
|
|
*idig++ = carry;
|
|
}
|
|
|
|
return idig - oidig;
|
|
}
|
|
|
|
/* computes i = j - k
|
|
returns number of digits in i
|
|
assumes enough memory in i; assumes normalised j, k; assumes j >= k
|
|
can have i, j, k pointing to same memory
|
|
*/
|
|
STATIC size_t mpn_sub(mpz_dig_t *idig, const mpz_dig_t *jdig, size_t jlen, const mpz_dig_t *kdig, size_t klen) {
|
|
mpz_dig_t *oidig = idig;
|
|
mpz_dbl_dig_signed_t borrow = 0;
|
|
|
|
jlen -= klen;
|
|
|
|
for (; klen > 0; --klen, ++idig, ++jdig, ++kdig) {
|
|
borrow += (mpz_dbl_dig_t)*jdig - (mpz_dbl_dig_t)*kdig;
|
|
*idig = borrow & DIG_MASK;
|
|
borrow >>= DIG_SIZE;
|
|
}
|
|
|
|
for (; jlen > 0; --jlen, ++idig, ++jdig) {
|
|
borrow += *jdig;
|
|
*idig = borrow & DIG_MASK;
|
|
borrow >>= DIG_SIZE;
|
|
}
|
|
|
|
return mpn_remove_trailing_zeros(oidig, idig);
|
|
}
|
|
|
|
#if MICROPY_OPT_MPZ_BITWISE
|
|
|
|
/* computes i = j & k
|
|
returns number of digits in i
|
|
assumes enough memory in i; assumes normalised j, k; assumes jlen >= klen (jlen argument not needed)
|
|
can have i, j, k pointing to same memory
|
|
*/
|
|
STATIC size_t mpn_and(mpz_dig_t *idig, const mpz_dig_t *jdig, const mpz_dig_t *kdig, size_t klen) {
|
|
mpz_dig_t *oidig = idig;
|
|
|
|
for (; klen > 0; --klen, ++idig, ++jdig, ++kdig) {
|
|
*idig = *jdig & *kdig;
|
|
}
|
|
|
|
return mpn_remove_trailing_zeros(oidig, idig);
|
|
}
|
|
|
|
#endif
|
|
|
|
/* i = -((-j) & (-k)) = ~((~j + 1) & (~k + 1)) + 1
|
|
i = (j & (-k)) = (j & (~k + 1)) = ( j & (~k + 1))
|
|
i = ((-j) & k) = ((~j + 1) & k) = ((~j + 1) & k )
|
|
computes general form:
|
|
i = (im ^ (((j ^ jm) + jc) & ((k ^ km) + kc))) + ic where Xm = Xc == 0 ? 0 : DIG_MASK
|
|
returns number of digits in i
|
|
assumes enough memory in i; assumes normalised j, k; assumes length j >= length k
|
|
can have i, j, k pointing to same memory
|
|
*/
|
|
STATIC size_t mpn_and_neg(mpz_dig_t *idig, const mpz_dig_t *jdig, size_t jlen, const mpz_dig_t *kdig, size_t klen,
|
|
mpz_dbl_dig_t carryi, mpz_dbl_dig_t carryj, mpz_dbl_dig_t carryk) {
|
|
mpz_dig_t *oidig = idig;
|
|
mpz_dig_t imask = (0 == carryi) ? 0 : DIG_MASK;
|
|
mpz_dig_t jmask = (0 == carryj) ? 0 : DIG_MASK;
|
|
mpz_dig_t kmask = (0 == carryk) ? 0 : DIG_MASK;
|
|
|
|
for (; jlen > 0; ++idig, ++jdig) {
|
|
carryj += *jdig ^ jmask;
|
|
carryk += (--klen <= --jlen) ? (*kdig++ ^ kmask) : kmask;
|
|
carryi += ((carryj & carryk) ^ imask) & DIG_MASK;
|
|
*idig = carryi & DIG_MASK;
|
|
carryk >>= DIG_SIZE;
|
|
carryj >>= DIG_SIZE;
|
|
carryi >>= DIG_SIZE;
|
|
}
|
|
|
|
if (0 != carryi) {
|
|
*idig++ = carryi;
|
|
}
|
|
|
|
return mpn_remove_trailing_zeros(oidig, idig);
|
|
}
|
|
|
|
#if MICROPY_OPT_MPZ_BITWISE
|
|
|
|
/* computes i = j | k
|
|
returns number of digits in i
|
|
assumes enough memory in i; assumes normalised j, k; assumes jlen >= klen
|
|
can have i, j, k pointing to same memory
|
|
*/
|
|
STATIC size_t mpn_or(mpz_dig_t *idig, const mpz_dig_t *jdig, size_t jlen, const mpz_dig_t *kdig, size_t klen) {
|
|
mpz_dig_t *oidig = idig;
|
|
|
|
jlen -= klen;
|
|
|
|
for (; klen > 0; --klen, ++idig, ++jdig, ++kdig) {
|
|
*idig = *jdig | *kdig;
|
|
}
|
|
|
|
for (; jlen > 0; --jlen, ++idig, ++jdig) {
|
|
*idig = *jdig;
|
|
}
|
|
|
|
return idig - oidig;
|
|
}
|
|
|
|
#endif
|
|
|
|
/* i = -((-j) | (-k)) = ~((~j + 1) | (~k + 1)) + 1
|
|
i = -(j | (-k)) = -(j | (~k + 1)) = ~( j | (~k + 1)) + 1
|
|
i = -((-j) | k) = -((~j + 1) | k) = ~((~j + 1) | k ) + 1
|
|
computes general form:
|
|
i = ~(((j ^ jm) + jc) | ((k ^ km) + kc)) + 1 where Xm = Xc == 0 ? 0 : DIG_MASK
|
|
returns number of digits in i
|
|
assumes enough memory in i; assumes normalised j, k; assumes length j >= length k
|
|
can have i, j, k pointing to same memory
|
|
*/
|
|
|
|
#if MICROPY_OPT_MPZ_BITWISE
|
|
|
|
STATIC size_t mpn_or_neg(mpz_dig_t *idig, const mpz_dig_t *jdig, size_t jlen, const mpz_dig_t *kdig, size_t klen,
|
|
mpz_dbl_dig_t carryj, mpz_dbl_dig_t carryk) {
|
|
mpz_dig_t *oidig = idig;
|
|
mpz_dbl_dig_t carryi = 1;
|
|
mpz_dig_t jmask = (0 == carryj) ? 0 : DIG_MASK;
|
|
mpz_dig_t kmask = (0 == carryk) ? 0 : DIG_MASK;
|
|
|
|
for (; jlen > 0; ++idig, ++jdig) {
|
|
carryj += *jdig ^ jmask;
|
|
carryk += (--klen <= --jlen) ? (*kdig++ ^ kmask) : kmask;
|
|
carryi += ((carryj | carryk) ^ DIG_MASK) & DIG_MASK;
|
|
*idig = carryi & DIG_MASK;
|
|
carryk >>= DIG_SIZE;
|
|
carryj >>= DIG_SIZE;
|
|
carryi >>= DIG_SIZE;
|
|
}
|
|
|
|
// At least one of j,k must be negative so the above for-loop runs at least
|
|
// once. For carryi to be non-zero here it must be equal to 1 at the end of
|
|
// each iteration of the loop. So the accumulation of carryi must overflow
|
|
// each time, ie carryi += 0xff..ff. So carryj|carryk must be 0 in the
|
|
// DIG_MASK bits on each iteration. But considering all cases of signs of
|
|
// j,k one sees that this is not possible.
|
|
assert(carryi == 0);
|
|
|
|
return mpn_remove_trailing_zeros(oidig, idig);
|
|
}
|
|
|
|
#else
|
|
|
|
STATIC size_t mpn_or_neg(mpz_dig_t *idig, const mpz_dig_t *jdig, size_t jlen, const mpz_dig_t *kdig, size_t klen,
|
|
mpz_dbl_dig_t carryi, mpz_dbl_dig_t carryj, mpz_dbl_dig_t carryk) {
|
|
mpz_dig_t *oidig = idig;
|
|
mpz_dig_t imask = (0 == carryi) ? 0 : DIG_MASK;
|
|
mpz_dig_t jmask = (0 == carryj) ? 0 : DIG_MASK;
|
|
mpz_dig_t kmask = (0 == carryk) ? 0 : DIG_MASK;
|
|
|
|
for (; jlen > 0; ++idig, ++jdig) {
|
|
carryj += *jdig ^ jmask;
|
|
carryk += (--klen <= --jlen) ? (*kdig++ ^ kmask) : kmask;
|
|
carryi += ((carryj | carryk) ^ imask) & DIG_MASK;
|
|
*idig = carryi & DIG_MASK;
|
|
carryk >>= DIG_SIZE;
|
|
carryj >>= DIG_SIZE;
|
|
carryi >>= DIG_SIZE;
|
|
}
|
|
|
|
// See comment in above mpn_or_neg for why carryi must be 0.
|
|
assert(carryi == 0);
|
|
|
|
return mpn_remove_trailing_zeros(oidig, idig);
|
|
}
|
|
|
|
#endif
|
|
|
|
#if MICROPY_OPT_MPZ_BITWISE
|
|
|
|
/* computes i = j ^ k
|
|
returns number of digits in i
|
|
assumes enough memory in i; assumes normalised j, k; assumes jlen >= klen
|
|
can have i, j, k pointing to same memory
|
|
*/
|
|
STATIC size_t mpn_xor(mpz_dig_t *idig, const mpz_dig_t *jdig, size_t jlen, const mpz_dig_t *kdig, size_t klen) {
|
|
mpz_dig_t *oidig = idig;
|
|
|
|
jlen -= klen;
|
|
|
|
for (; klen > 0; --klen, ++idig, ++jdig, ++kdig) {
|
|
*idig = *jdig ^ *kdig;
|
|
}
|
|
|
|
for (; jlen > 0; --jlen, ++idig, ++jdig) {
|
|
*idig = *jdig;
|
|
}
|
|
|
|
return mpn_remove_trailing_zeros(oidig, idig);
|
|
}
|
|
|
|
#endif
|
|
|
|
/* i = (-j) ^ (-k) = ~(j - 1) ^ ~(k - 1) = (j - 1) ^ (k - 1)
|
|
i = -(j ^ (-k)) = -(j ^ ~(k - 1)) = ~(j ^ ~(k - 1)) + 1 = (j ^ (k - 1)) + 1
|
|
i = -((-j) ^ k) = -(~(j - 1) ^ k) = ~(~(j - 1) ^ k) + 1 = ((j - 1) ^ k) + 1
|
|
computes general form:
|
|
i = ((j - 1 + jc) ^ (k - 1 + kc)) + ic
|
|
returns number of digits in i
|
|
assumes enough memory in i; assumes normalised j, k; assumes length j >= length k
|
|
can have i, j, k pointing to same memory
|
|
*/
|
|
STATIC size_t mpn_xor_neg(mpz_dig_t *idig, const mpz_dig_t *jdig, size_t jlen, const mpz_dig_t *kdig, size_t klen,
|
|
mpz_dbl_dig_t carryi, mpz_dbl_dig_t carryj, mpz_dbl_dig_t carryk) {
|
|
mpz_dig_t *oidig = idig;
|
|
|
|
for (; jlen > 0; ++idig, ++jdig) {
|
|
carryj += *jdig + DIG_MASK;
|
|
carryk += (--klen <= --jlen) ? (*kdig++ + DIG_MASK) : DIG_MASK;
|
|
carryi += (carryj ^ carryk) & DIG_MASK;
|
|
*idig = carryi & DIG_MASK;
|
|
carryk >>= DIG_SIZE;
|
|
carryj >>= DIG_SIZE;
|
|
carryi >>= DIG_SIZE;
|
|
}
|
|
|
|
if (0 != carryi) {
|
|
*idig++ = carryi;
|
|
}
|
|
|
|
return mpn_remove_trailing_zeros(oidig, idig);
|
|
}
|
|
|
|
/* computes i = i * d1 + d2
|
|
returns number of digits in i
|
|
assumes enough memory in i; assumes normalised i; assumes dmul != 0
|
|
*/
|
|
STATIC size_t mpn_mul_dig_add_dig(mpz_dig_t *idig, size_t ilen, mpz_dig_t dmul, mpz_dig_t dadd) {
|
|
mpz_dig_t *oidig = idig;
|
|
mpz_dbl_dig_t carry = dadd;
|
|
|
|
for (; ilen > 0; --ilen, ++idig) {
|
|
carry += (mpz_dbl_dig_t)*idig * (mpz_dbl_dig_t)dmul; // will never overflow so long as DIG_SIZE <= 8*sizeof(mpz_dbl_dig_t)/2
|
|
*idig = carry & DIG_MASK;
|
|
carry >>= DIG_SIZE;
|
|
}
|
|
|
|
if (carry != 0) {
|
|
*idig++ = carry;
|
|
}
|
|
|
|
return idig - oidig;
|
|
}
|
|
|
|
/* computes i = j * k
|
|
returns number of digits in i
|
|
assumes enough memory in i; assumes i is zeroed; assumes normalised j, k
|
|
can have j, k point to same memory
|
|
*/
|
|
STATIC size_t mpn_mul(mpz_dig_t *idig, mpz_dig_t *jdig, size_t jlen, mpz_dig_t *kdig, size_t klen) {
|
|
mpz_dig_t *oidig = idig;
|
|
size_t ilen = 0;
|
|
|
|
for (; klen > 0; --klen, ++idig, ++kdig) {
|
|
mpz_dig_t *id = idig;
|
|
mpz_dbl_dig_t carry = 0;
|
|
|
|
size_t jl = jlen;
|
|
for (mpz_dig_t *jd = jdig; jl > 0; --jl, ++jd, ++id) {
|
|
carry += (mpz_dbl_dig_t)*id + (mpz_dbl_dig_t)*jd * (mpz_dbl_dig_t)*kdig; // will never overflow so long as DIG_SIZE <= 8*sizeof(mpz_dbl_dig_t)/2
|
|
*id = carry & DIG_MASK;
|
|
carry >>= DIG_SIZE;
|
|
}
|
|
|
|
if (carry != 0) {
|
|
*id++ = carry;
|
|
}
|
|
|
|
ilen = id - oidig;
|
|
// check to prevent usb starvation
|
|
#ifdef RUN_BACKGROUND_TASKS
|
|
RUN_BACKGROUND_TASKS;
|
|
#endif
|
|
}
|
|
|
|
return ilen;
|
|
}
|
|
|
|
/* natural_div - quo * den + new_num = old_num (ie num is replaced with rem)
|
|
assumes den != 0
|
|
assumes num_dig has enough memory to be extended by 1 digit
|
|
assumes quo_dig has enough memory (as many digits as num)
|
|
assumes quo_dig is filled with zeros
|
|
*/
|
|
STATIC void mpn_div(mpz_dig_t *num_dig, size_t *num_len, const mpz_dig_t *den_dig, size_t den_len, mpz_dig_t *quo_dig, size_t *quo_len) {
|
|
mpz_dig_t *orig_num_dig = num_dig;
|
|
mpz_dig_t *orig_quo_dig = quo_dig;
|
|
mpz_dig_t norm_shift = 0;
|
|
mpz_dbl_dig_t lead_den_digit;
|
|
|
|
// handle simple cases
|
|
{
|
|
int cmp = mpn_cmp(num_dig, *num_len, den_dig, den_len);
|
|
if (cmp == 0) {
|
|
*num_len = 0;
|
|
quo_dig[0] = 1;
|
|
*quo_len = 1;
|
|
return;
|
|
} else if (cmp < 0) {
|
|
// numerator remains the same
|
|
*quo_len = 0;
|
|
return;
|
|
}
|
|
}
|
|
|
|
// We need to normalise the denominator (leading bit of leading digit is 1)
|
|
// so that the division routine works. Since the denominator memory is
|
|
// read-only we do the normalisation on the fly, each time a digit of the
|
|
// denominator is needed. We need to know is how many bits to shift by.
|
|
|
|
// count number of leading zeros in leading digit of denominator
|
|
{
|
|
mpz_dig_t d = den_dig[den_len - 1];
|
|
while ((d & DIG_MSB) == 0) {
|
|
d <<= 1;
|
|
++norm_shift;
|
|
}
|
|
}
|
|
|
|
// now need to shift numerator by same amount as denominator
|
|
// first, increase length of numerator in case we need more room to shift
|
|
num_dig[*num_len] = 0;
|
|
++(*num_len);
|
|
for (mpz_dig_t *num = num_dig, carry = 0; num < num_dig + *num_len; ++num) {
|
|
mpz_dig_t n = *num;
|
|
*num = ((n << norm_shift) | carry) & DIG_MASK;
|
|
carry = (mpz_dbl_dig_t)n >> (DIG_SIZE - norm_shift);
|
|
}
|
|
|
|
// cache the leading digit of the denominator
|
|
lead_den_digit = (mpz_dbl_dig_t)den_dig[den_len - 1] << norm_shift;
|
|
if (den_len >= 2) {
|
|
lead_den_digit |= (mpz_dbl_dig_t)den_dig[den_len - 2] >> (DIG_SIZE - norm_shift);
|
|
}
|
|
|
|
// point num_dig to last digit in numerator
|
|
num_dig += *num_len - 1;
|
|
|
|
// calculate number of digits in quotient
|
|
*quo_len = *num_len - den_len;
|
|
|
|
// point to last digit to store for quotient
|
|
quo_dig += *quo_len - 1;
|
|
|
|
// keep going while we have enough digits to divide
|
|
while (*num_len > den_len) {
|
|
mpz_dbl_dig_t quo = ((mpz_dbl_dig_t)*num_dig << DIG_SIZE) | num_dig[-1];
|
|
|
|
// get approximate quotient
|
|
quo /= lead_den_digit;
|
|
|
|
// Multiply quo by den and subtract from num to get remainder.
|
|
// Must be careful with overflow of the borrow variable. Both
|
|
// borrow and low_digs are signed values and need signed right-shift,
|
|
// but x is unsigned and may take a full-range value.
|
|
const mpz_dig_t *d = den_dig;
|
|
mpz_dbl_dig_t d_norm = 0;
|
|
mpz_dbl_dig_signed_t borrow = 0;
|
|
for (mpz_dig_t *n = num_dig - den_len; n < num_dig; ++n, ++d) {
|
|
// Get the next digit in (den).
|
|
d_norm = ((mpz_dbl_dig_t)*d << norm_shift) | (d_norm >> DIG_SIZE);
|
|
// Multiply the next digit in (quo * den).
|
|
mpz_dbl_dig_t x = (mpz_dbl_dig_t)quo * (d_norm & DIG_MASK);
|
|
// Compute the low DIG_MASK bits of the next digit in (num - quo * den)
|
|
mpz_dbl_dig_signed_t low_digs = (borrow & DIG_MASK) + *n - (x & DIG_MASK);
|
|
// Store the digit result for (num).
|
|
*n = low_digs & DIG_MASK;
|
|
// Compute the borrow, shifted right before summing to avoid overflow.
|
|
borrow = (borrow >> DIG_SIZE) - (x >> DIG_SIZE) + (low_digs >> DIG_SIZE);
|
|
}
|
|
|
|
// At this point we have either:
|
|
//
|
|
// 1. quo was the correct value and the most-sig-digit of num is exactly
|
|
// cancelled by borrow (borrow + *num_dig == 0). In this case there is
|
|
// nothing more to do.
|
|
//
|
|
// 2. quo was too large, we subtracted too many den from num, and the
|
|
// most-sig-digit of num is less than needed (borrow + *num_dig < 0).
|
|
// In this case we must reduce quo and add back den to num until the
|
|
// carry from this operation cancels out the borrow.
|
|
//
|
|
borrow += *num_dig;
|
|
for (; borrow != 0; --quo) {
|
|
d = den_dig;
|
|
d_norm = 0;
|
|
mpz_dbl_dig_t carry = 0;
|
|
for (mpz_dig_t *n = num_dig - den_len; n < num_dig; ++n, ++d) {
|
|
d_norm = ((mpz_dbl_dig_t)*d << norm_shift) | (d_norm >> DIG_SIZE);
|
|
carry += (mpz_dbl_dig_t)*n + (d_norm & DIG_MASK);
|
|
*n = carry & DIG_MASK;
|
|
carry >>= DIG_SIZE;
|
|
}
|
|
borrow += carry;
|
|
}
|
|
|
|
// store this digit of the quotient
|
|
*quo_dig = quo & DIG_MASK;
|
|
--quo_dig;
|
|
|
|
// move down to next digit of numerator
|
|
--num_dig;
|
|
--(*num_len);
|
|
}
|
|
|
|
// unnormalise numerator (remainder now)
|
|
for (mpz_dig_t *num = orig_num_dig + *num_len - 1, carry = 0; num >= orig_num_dig; --num) {
|
|
mpz_dig_t n = *num;
|
|
*num = ((n >> norm_shift) | carry) & DIG_MASK;
|
|
carry = (mpz_dbl_dig_t)n << (DIG_SIZE - norm_shift);
|
|
}
|
|
|
|
// strip trailing zeros
|
|
|
|
while (*quo_len > 0 && orig_quo_dig[*quo_len - 1] == 0) {
|
|
--(*quo_len);
|
|
}
|
|
|
|
while (*num_len > 0 && orig_num_dig[*num_len - 1] == 0) {
|
|
--(*num_len);
|
|
}
|
|
}
|
|
|
|
#define MIN_ALLOC (2)
|
|
|
|
void mpz_init_zero(mpz_t *z) {
|
|
z->neg = 0;
|
|
z->fixed_dig = 0;
|
|
z->alloc = 0;
|
|
z->len = 0;
|
|
z->dig = NULL;
|
|
}
|
|
|
|
void mpz_init_from_int(mpz_t *z, mp_int_t val) {
|
|
mpz_init_zero(z);
|
|
mpz_set_from_int(z, val);
|
|
}
|
|
|
|
void mpz_init_fixed_from_int(mpz_t *z, mpz_dig_t *dig, size_t alloc, mp_int_t val) {
|
|
z->neg = 0;
|
|
z->fixed_dig = 1;
|
|
z->alloc = alloc;
|
|
z->len = 0;
|
|
z->dig = dig;
|
|
mpz_set_from_int(z, val);
|
|
}
|
|
|
|
void mpz_deinit(mpz_t *z) {
|
|
if (z != NULL && !z->fixed_dig) {
|
|
m_del(mpz_dig_t, z->dig, z->alloc);
|
|
}
|
|
}
|
|
|
|
#if 0
|
|
these functions are unused
|
|
|
|
mpz_t *mpz_zero(void) {
|
|
mpz_t *z = m_new_obj(mpz_t);
|
|
mpz_init_zero(z);
|
|
return z;
|
|
}
|
|
|
|
mpz_t *mpz_from_int(mp_int_t val) {
|
|
mpz_t *z = mpz_zero();
|
|
mpz_set_from_int(z, val);
|
|
return z;
|
|
}
|
|
|
|
mpz_t *mpz_from_ll(long long val, bool is_signed) {
|
|
mpz_t *z = mpz_zero();
|
|
mpz_set_from_ll(z, val, is_signed);
|
|
return z;
|
|
}
|
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
|
mpz_t *mpz_from_float(mp_float_t val) {
|
|
mpz_t *z = mpz_zero();
|
|
mpz_set_from_float(z, val);
|
|
return z;
|
|
}
|
|
#endif
|
|
|
|
mpz_t *mpz_from_str(const char *str, size_t len, bool neg, unsigned int base) {
|
|
mpz_t *z = mpz_zero();
|
|
mpz_set_from_str(z, str, len, neg, base);
|
|
return z;
|
|
}
|
|
#endif
|
|
|
|
STATIC void mpz_free(mpz_t *z) {
|
|
if (z != NULL) {
|
|
m_del(mpz_dig_t, z->dig, z->alloc);
|
|
m_del_obj(mpz_t, z);
|
|
}
|
|
}
|
|
|
|
STATIC void mpz_need_dig(mpz_t *z, size_t need) {
|
|
if (need < MIN_ALLOC) {
|
|
need = MIN_ALLOC;
|
|
}
|
|
|
|
if (z->dig == NULL || z->alloc < need) {
|
|
// if z has fixed digit buffer there's not much we can do as the caller will
|
|
// be expecting a buffer with at least "need" bytes (but it shouldn't happen)
|
|
assert(!z->fixed_dig);
|
|
z->dig = m_renew(mpz_dig_t, z->dig, z->alloc, need);
|
|
z->alloc = need;
|
|
}
|
|
}
|
|
|
|
STATIC mpz_t *mpz_clone(const mpz_t *src) {
|
|
assert(src->alloc != 0);
|
|
mpz_t *z = m_new_obj(mpz_t);
|
|
z->neg = src->neg;
|
|
z->fixed_dig = 0;
|
|
z->alloc = src->alloc;
|
|
z->len = src->len;
|
|
z->dig = m_new(mpz_dig_t, z->alloc);
|
|
memcpy(z->dig, src->dig, src->alloc * sizeof(mpz_dig_t));
|
|
return z;
|
|
}
|
|
|
|
/* sets dest = src
|
|
can have dest, src the same
|
|
*/
|
|
void mpz_set(mpz_t *dest, const mpz_t *src) {
|
|
mpz_need_dig(dest, src->len);
|
|
dest->neg = src->neg;
|
|
dest->len = src->len;
|
|
memcpy(dest->dig, src->dig, src->len * sizeof(mpz_dig_t));
|
|
}
|
|
|
|
void mpz_set_from_int(mpz_t *z, mp_int_t val) {
|
|
if (val == 0) {
|
|
z->len = 0;
|
|
return;
|
|
}
|
|
|
|
mpz_need_dig(z, MPZ_NUM_DIG_FOR_INT);
|
|
|
|
mp_uint_t uval;
|
|
if (val < 0) {
|
|
z->neg = 1;
|
|
uval = -val;
|
|
} else {
|
|
z->neg = 0;
|
|
uval = val;
|
|
}
|
|
|
|
z->len = 0;
|
|
while (uval > 0) {
|
|
z->dig[z->len++] = uval & DIG_MASK;
|
|
uval >>= DIG_SIZE;
|
|
}
|
|
}
|
|
|
|
void mpz_set_from_ll(mpz_t *z, long long val, bool is_signed) {
|
|
mpz_need_dig(z, MPZ_NUM_DIG_FOR_LL);
|
|
|
|
unsigned long long uval;
|
|
if (is_signed && val < 0) {
|
|
z->neg = 1;
|
|
uval = -(unsigned long long)val;
|
|
} else {
|
|
z->neg = 0;
|
|
uval = val;
|
|
}
|
|
|
|
z->len = 0;
|
|
while (uval > 0) {
|
|
z->dig[z->len++] = uval & DIG_MASK;
|
|
uval >>= DIG_SIZE;
|
|
}
|
|
}
|
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
|
void mpz_set_from_float(mpz_t *z, mp_float_t src) {
|
|
mp_float_union_t u = {src};
|
|
z->neg = u.p.sgn;
|
|
if (u.p.exp == 0) {
|
|
// value == 0 || value < 1
|
|
mpz_set_from_int(z, 0);
|
|
} else if (u.p.exp == ((1 << MP_FLOAT_EXP_BITS) - 1)) {
|
|
// u.p.frc == 0 indicates inf, else NaN
|
|
// should be handled by caller
|
|
mpz_set_from_int(z, 0);
|
|
} else {
|
|
const int adj_exp = (int)u.p.exp - MP_FLOAT_EXP_BIAS;
|
|
if (adj_exp < 0) {
|
|
// value < 1 , truncates to 0
|
|
mpz_set_from_int(z, 0);
|
|
} else if (adj_exp == 0) {
|
|
// 1 <= value < 2 , so truncates to 1
|
|
mpz_set_from_int(z, 1);
|
|
} else {
|
|
// 2 <= value
|
|
const int dig_cnt = (adj_exp + 1 + (DIG_SIZE - 1)) / DIG_SIZE;
|
|
const unsigned int rem = adj_exp % DIG_SIZE;
|
|
int dig_ind, shft;
|
|
mp_float_uint_t frc = u.p.frc | ((mp_float_uint_t)1 << MP_FLOAT_FRAC_BITS);
|
|
|
|
if (adj_exp < MP_FLOAT_FRAC_BITS) {
|
|
shft = 0;
|
|
dig_ind = 0;
|
|
frc >>= MP_FLOAT_FRAC_BITS - adj_exp;
|
|
} else {
|
|
shft = (rem - MP_FLOAT_FRAC_BITS) % DIG_SIZE;
|
|
dig_ind = (adj_exp - MP_FLOAT_FRAC_BITS) / DIG_SIZE;
|
|
}
|
|
mpz_need_dig(z, dig_cnt);
|
|
z->len = dig_cnt;
|
|
if (dig_ind != 0) {
|
|
memset(z->dig, 0, dig_ind * sizeof(mpz_dig_t));
|
|
}
|
|
if (shft != 0) {
|
|
z->dig[dig_ind++] = (frc << shft) & DIG_MASK;
|
|
frc >>= DIG_SIZE - shft;
|
|
}
|
|
#if DIG_SIZE < (MP_FLOAT_FRAC_BITS + 1)
|
|
while (dig_ind != dig_cnt) {
|
|
z->dig[dig_ind++] = frc & DIG_MASK;
|
|
frc >>= DIG_SIZE;
|
|
}
|
|
#else
|
|
if (dig_ind != dig_cnt) {
|
|
z->dig[dig_ind] = frc;
|
|
}
|
|
#endif
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
|
|
// returns number of bytes from str that were processed
|
|
size_t mpz_set_from_str(mpz_t *z, const char *str, size_t len, bool neg, unsigned int base) {
|
|
assert(base <= 36);
|
|
|
|
const char *cur = str;
|
|
const char *top = str + len;
|
|
|
|
mpz_need_dig(z, len * 8 / DIG_SIZE + 1);
|
|
|
|
if (neg) {
|
|
z->neg = 1;
|
|
} else {
|
|
z->neg = 0;
|
|
}
|
|
|
|
z->len = 0;
|
|
for (; cur < top; ++cur) { // XXX UTF8 next char
|
|
// mp_uint_t v = char_to_numeric(cur#); // XXX UTF8 get char
|
|
mp_uint_t v = *cur;
|
|
if ('0' <= v && v <= '9') {
|
|
v -= '0';
|
|
} else if ('A' <= v && v <= 'Z') {
|
|
v -= 'A' - 10;
|
|
} else if ('a' <= v && v <= 'z') {
|
|
v -= 'a' - 10;
|
|
} else {
|
|
break;
|
|
}
|
|
if (v >= base) {
|
|
break;
|
|
}
|
|
z->len = mpn_mul_dig_add_dig(z->dig, z->len, base, v);
|
|
}
|
|
|
|
return cur - str;
|
|
}
|
|
|
|
void mpz_set_from_bytes(mpz_t *z, bool big_endian, size_t len, const byte *buf) {
|
|
int delta = 1;
|
|
if (big_endian) {
|
|
buf += len - 1;
|
|
delta = -1;
|
|
}
|
|
|
|
mpz_need_dig(z, (len * 8 + DIG_SIZE - 1) / DIG_SIZE);
|
|
|
|
mpz_dig_t d = 0;
|
|
int num_bits = 0;
|
|
z->neg = 0;
|
|
z->len = 0;
|
|
while (len) {
|
|
while (len && num_bits < DIG_SIZE) {
|
|
d |= *buf << num_bits;
|
|
num_bits += 8;
|
|
buf += delta;
|
|
len--;
|
|
}
|
|
z->dig[z->len++] = d & DIG_MASK;
|
|
// Need this #if because it's C undefined behavior to do: uint32_t >> 32
|
|
#if DIG_SIZE != 8 && DIG_SIZE != 16 && DIG_SIZE != 32
|
|
d >>= DIG_SIZE;
|
|
#else
|
|
d = 0;
|
|
#endif
|
|
num_bits -= DIG_SIZE;
|
|
}
|
|
|
|
z->len = mpn_remove_trailing_zeros(z->dig, z->dig + z->len);
|
|
}
|
|
|
|
#if 0
|
|
these functions are unused
|
|
|
|
bool mpz_is_pos(const mpz_t *z) {
|
|
return z->len > 0 && z->neg == 0;
|
|
}
|
|
|
|
bool mpz_is_odd(const mpz_t *z) {
|
|
return z->len > 0 && (z->dig[0] & 1) != 0;
|
|
}
|
|
|
|
bool mpz_is_even(const mpz_t *z) {
|
|
return z->len == 0 || (z->dig[0] & 1) == 0;
|
|
}
|
|
#endif
|
|
|
|
int mpz_cmp(const mpz_t *z1, const mpz_t *z2) {
|
|
// to catch comparison of -0 with +0
|
|
if (z1->len == 0 && z2->len == 0) {
|
|
return 0;
|
|
}
|
|
int cmp = (int)z2->neg - (int)z1->neg;
|
|
if (cmp != 0) {
|
|
return cmp;
|
|
}
|
|
cmp = mpn_cmp(z1->dig, z1->len, z2->dig, z2->len);
|
|
if (z1->neg != 0) {
|
|
cmp = -cmp;
|
|
}
|
|
return cmp;
|
|
}
|
|
|
|
#if 0
|
|
// obsolete
|
|
// compares mpz with an integer that fits within DIG_SIZE bits
|
|
mp_int_t mpz_cmp_sml_int(const mpz_t *z, mp_int_t sml_int) {
|
|
mp_int_t cmp;
|
|
if (z->neg == 0) {
|
|
if (sml_int < 0) {
|
|
return 1;
|
|
}
|
|
if (sml_int == 0) {
|
|
if (z->len == 0) {
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
if (z->len == 0) {
|
|
return -1;
|
|
}
|
|
assert(sml_int < (1 << DIG_SIZE));
|
|
if (z->len != 1) {
|
|
return 1;
|
|
}
|
|
cmp = z->dig[0] - sml_int;
|
|
} else {
|
|
if (sml_int > 0) {
|
|
return -1;
|
|
}
|
|
if (sml_int == 0) {
|
|
if (z->len == 0) {
|
|
return 0;
|
|
}
|
|
return -1;
|
|
}
|
|
if (z->len == 0) {
|
|
return 1;
|
|
}
|
|
assert(sml_int > -(1 << DIG_SIZE));
|
|
if (z->len != 1) {
|
|
return -1;
|
|
}
|
|
cmp = -z->dig[0] - sml_int;
|
|
}
|
|
if (cmp < 0) {
|
|
return -1;
|
|
}
|
|
if (cmp > 0) {
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
#endif
|
|
|
|
#if 0
|
|
these functions are unused
|
|
|
|
/* returns abs(z)
|
|
*/
|
|
mpz_t *mpz_abs(const mpz_t *z) {
|
|
// TODO: handle case of z->alloc=0
|
|
mpz_t *z2 = mpz_clone(z);
|
|
z2->neg = 0;
|
|
return z2;
|
|
}
|
|
|
|
/* returns -z
|
|
*/
|
|
mpz_t *mpz_neg(const mpz_t *z) {
|
|
// TODO: handle case of z->alloc=0
|
|
mpz_t *z2 = mpz_clone(z);
|
|
z2->neg = 1 - z2->neg;
|
|
return z2;
|
|
}
|
|
|
|
/* returns lhs + rhs
|
|
can have lhs, rhs the same
|
|
*/
|
|
mpz_t *mpz_add(const mpz_t *lhs, const mpz_t *rhs) {
|
|
mpz_t *z = mpz_zero();
|
|
mpz_add_inpl(z, lhs, rhs);
|
|
return z;
|
|
}
|
|
|
|
/* returns lhs - rhs
|
|
can have lhs, rhs the same
|
|
*/
|
|
mpz_t *mpz_sub(const mpz_t *lhs, const mpz_t *rhs) {
|
|
mpz_t *z = mpz_zero();
|
|
mpz_sub_inpl(z, lhs, rhs);
|
|
return z;
|
|
}
|
|
|
|
/* returns lhs * rhs
|
|
can have lhs, rhs the same
|
|
*/
|
|
mpz_t *mpz_mul(const mpz_t *lhs, const mpz_t *rhs) {
|
|
mpz_t *z = mpz_zero();
|
|
mpz_mul_inpl(z, lhs, rhs);
|
|
return z;
|
|
}
|
|
|
|
/* returns lhs ** rhs
|
|
can have lhs, rhs the same
|
|
*/
|
|
mpz_t *mpz_pow(const mpz_t *lhs, const mpz_t *rhs) {
|
|
mpz_t *z = mpz_zero();
|
|
mpz_pow_inpl(z, lhs, rhs);
|
|
return z;
|
|
}
|
|
|
|
/* computes new integers in quo and rem such that:
|
|
quo * rhs + rem = lhs
|
|
0 <= rem < rhs
|
|
can have lhs, rhs the same
|
|
*/
|
|
void mpz_divmod(const mpz_t *lhs, const mpz_t *rhs, mpz_t **quo, mpz_t **rem) {
|
|
*quo = mpz_zero();
|
|
*rem = mpz_zero();
|
|
mpz_divmod_inpl(*quo, *rem, lhs, rhs);
|
|
}
|
|
#endif
|
|
|
|
/* computes dest = abs(z)
|
|
can have dest, z the same
|
|
*/
|
|
void mpz_abs_inpl(mpz_t *dest, const mpz_t *z) {
|
|
if (dest != z) {
|
|
mpz_set(dest, z);
|
|
}
|
|
dest->neg = 0;
|
|
}
|
|
|
|
/* computes dest = -z
|
|
can have dest, z the same
|
|
*/
|
|
void mpz_neg_inpl(mpz_t *dest, const mpz_t *z) {
|
|
if (dest != z) {
|
|
mpz_set(dest, z);
|
|
}
|
|
dest->neg = 1 - dest->neg;
|
|
}
|
|
|
|
/* computes dest = ~z (= -z - 1)
|
|
can have dest, z the same
|
|
*/
|
|
void mpz_not_inpl(mpz_t *dest, const mpz_t *z) {
|
|
if (dest != z) {
|
|
mpz_set(dest, z);
|
|
}
|
|
if (dest->len == 0) {
|
|
mpz_need_dig(dest, 1);
|
|
dest->dig[0] = 1;
|
|
dest->len = 1;
|
|
dest->neg = 1;
|
|
} else if (dest->neg) {
|
|
dest->neg = 0;
|
|
mpz_dig_t k = 1;
|
|
dest->len = mpn_sub(dest->dig, dest->dig, dest->len, &k, 1);
|
|
} else {
|
|
mpz_need_dig(dest, dest->len + 1);
|
|
mpz_dig_t k = 1;
|
|
dest->len = mpn_add(dest->dig, dest->dig, dest->len, &k, 1);
|
|
dest->neg = 1;
|
|
}
|
|
}
|
|
|
|
/* computes dest = lhs << rhs
|
|
can have dest, lhs the same
|
|
*/
|
|
void mpz_shl_inpl(mpz_t *dest, const mpz_t *lhs, mp_uint_t rhs) {
|
|
if (lhs->len == 0 || rhs == 0) {
|
|
mpz_set(dest, lhs);
|
|
} else {
|
|
mpz_need_dig(dest, lhs->len + (rhs + DIG_SIZE - 1) / DIG_SIZE);
|
|
dest->len = mpn_shl(dest->dig, lhs->dig, lhs->len, rhs);
|
|
dest->neg = lhs->neg;
|
|
}
|
|
}
|
|
|
|
/* computes dest = lhs >> rhs
|
|
can have dest, lhs the same
|
|
*/
|
|
void mpz_shr_inpl(mpz_t *dest, const mpz_t *lhs, mp_uint_t rhs) {
|
|
if (lhs->len == 0 || rhs == 0) {
|
|
mpz_set(dest, lhs);
|
|
} else {
|
|
mpz_need_dig(dest, lhs->len);
|
|
dest->len = mpn_shr(dest->dig, lhs->dig, lhs->len, rhs);
|
|
dest->neg = lhs->neg;
|
|
if (dest->neg) {
|
|
// arithmetic shift right, rounding to negative infinity
|
|
mp_uint_t n_whole = rhs / DIG_SIZE;
|
|
mp_uint_t n_part = rhs % DIG_SIZE;
|
|
mpz_dig_t round_up = 0;
|
|
for (size_t i = 0; i < lhs->len && i < n_whole; i++) {
|
|
if (lhs->dig[i] != 0) {
|
|
round_up = 1;
|
|
break;
|
|
}
|
|
}
|
|
if (n_whole < lhs->len && (lhs->dig[n_whole] & ((1 << n_part) - 1)) != 0) {
|
|
round_up = 1;
|
|
}
|
|
if (round_up) {
|
|
if (dest->len == 0) {
|
|
// dest == 0, so need to add 1 by hand (answer will be -1)
|
|
dest->dig[0] = 1;
|
|
dest->len = 1;
|
|
} else {
|
|
// dest > 0, so can use mpn_add to add 1
|
|
dest->len = mpn_add(dest->dig, dest->dig, dest->len, &round_up, 1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/* computes dest = lhs + rhs
|
|
can have dest, lhs, rhs the same
|
|
*/
|
|
void mpz_add_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs) {
|
|
if (mpn_cmp(lhs->dig, lhs->len, rhs->dig, rhs->len) < 0) {
|
|
const mpz_t *temp = lhs;
|
|
lhs = rhs;
|
|
rhs = temp;
|
|
}
|
|
|
|
if (lhs->neg == rhs->neg) {
|
|
mpz_need_dig(dest, lhs->len + 1);
|
|
dest->len = mpn_add(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len);
|
|
} else {
|
|
mpz_need_dig(dest, lhs->len);
|
|
dest->len = mpn_sub(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len);
|
|
}
|
|
|
|
dest->neg = lhs->neg;
|
|
}
|
|
|
|
/* computes dest = lhs - rhs
|
|
can have dest, lhs, rhs the same
|
|
*/
|
|
void mpz_sub_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs) {
|
|
bool neg = false;
|
|
|
|
if (mpn_cmp(lhs->dig, lhs->len, rhs->dig, rhs->len) < 0) {
|
|
const mpz_t *temp = lhs;
|
|
lhs = rhs;
|
|
rhs = temp;
|
|
neg = true;
|
|
}
|
|
|
|
if (lhs->neg != rhs->neg) {
|
|
mpz_need_dig(dest, lhs->len + 1);
|
|
dest->len = mpn_add(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len);
|
|
} else {
|
|
mpz_need_dig(dest, lhs->len);
|
|
dest->len = mpn_sub(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len);
|
|
}
|
|
|
|
if (neg) {
|
|
dest->neg = 1 - lhs->neg;
|
|
} else {
|
|
dest->neg = lhs->neg;
|
|
}
|
|
}
|
|
|
|
/* computes dest = lhs & rhs
|
|
can have dest, lhs, rhs the same
|
|
*/
|
|
void mpz_and_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs) {
|
|
// make sure lhs has the most digits
|
|
if (lhs->len < rhs->len) {
|
|
const mpz_t *temp = lhs;
|
|
lhs = rhs;
|
|
rhs = temp;
|
|
}
|
|
|
|
#if MICROPY_OPT_MPZ_BITWISE
|
|
|
|
if ((0 == lhs->neg) && (0 == rhs->neg)) {
|
|
mpz_need_dig(dest, lhs->len);
|
|
dest->len = mpn_and(dest->dig, lhs->dig, rhs->dig, rhs->len);
|
|
dest->neg = 0;
|
|
} else {
|
|
mpz_need_dig(dest, lhs->len + 1);
|
|
dest->len = mpn_and_neg(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len,
|
|
lhs->neg == rhs->neg, 0 != lhs->neg, 0 != rhs->neg);
|
|
dest->neg = lhs->neg & rhs->neg;
|
|
}
|
|
|
|
#else
|
|
|
|
mpz_need_dig(dest, lhs->len + (lhs->neg || rhs->neg));
|
|
dest->len = mpn_and_neg(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len,
|
|
(lhs->neg == rhs->neg) ? lhs->neg : 0, lhs->neg, rhs->neg);
|
|
dest->neg = lhs->neg & rhs->neg;
|
|
|
|
#endif
|
|
}
|
|
|
|
/* computes dest = lhs | rhs
|
|
can have dest, lhs, rhs the same
|
|
*/
|
|
void mpz_or_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs) {
|
|
// make sure lhs has the most digits
|
|
if (lhs->len < rhs->len) {
|
|
const mpz_t *temp = lhs;
|
|
lhs = rhs;
|
|
rhs = temp;
|
|
}
|
|
|
|
#if MICROPY_OPT_MPZ_BITWISE
|
|
|
|
if ((0 == lhs->neg) && (0 == rhs->neg)) {
|
|
mpz_need_dig(dest, lhs->len);
|
|
dest->len = mpn_or(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len);
|
|
dest->neg = 0;
|
|
} else {
|
|
mpz_need_dig(dest, lhs->len + 1);
|
|
dest->len = mpn_or_neg(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len,
|
|
0 != lhs->neg, 0 != rhs->neg);
|
|
dest->neg = 1;
|
|
}
|
|
|
|
#else
|
|
|
|
mpz_need_dig(dest, lhs->len + (lhs->neg || rhs->neg));
|
|
dest->len = mpn_or_neg(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len,
|
|
(lhs->neg || rhs->neg), lhs->neg, rhs->neg);
|
|
dest->neg = lhs->neg | rhs->neg;
|
|
|
|
#endif
|
|
}
|
|
|
|
/* computes dest = lhs ^ rhs
|
|
can have dest, lhs, rhs the same
|
|
*/
|
|
void mpz_xor_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs) {
|
|
// make sure lhs has the most digits
|
|
if (lhs->len < rhs->len) {
|
|
const mpz_t *temp = lhs;
|
|
lhs = rhs;
|
|
rhs = temp;
|
|
}
|
|
|
|
#if MICROPY_OPT_MPZ_BITWISE
|
|
|
|
if (lhs->neg == rhs->neg) {
|
|
mpz_need_dig(dest, lhs->len);
|
|
if (lhs->neg == 0) {
|
|
dest->len = mpn_xor(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len);
|
|
} else {
|
|
dest->len = mpn_xor_neg(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len, 0, 0, 0);
|
|
}
|
|
dest->neg = 0;
|
|
} else {
|
|
mpz_need_dig(dest, lhs->len + 1);
|
|
dest->len = mpn_xor_neg(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len, 1,
|
|
0 == lhs->neg, 0 == rhs->neg);
|
|
dest->neg = 1;
|
|
}
|
|
|
|
#else
|
|
|
|
mpz_need_dig(dest, lhs->len + (lhs->neg || rhs->neg));
|
|
dest->len = mpn_xor_neg(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len,
|
|
(lhs->neg != rhs->neg), 0 == lhs->neg, 0 == rhs->neg);
|
|
dest->neg = lhs->neg ^ rhs->neg;
|
|
|
|
#endif
|
|
}
|
|
|
|
/* computes dest = lhs * rhs
|
|
can have dest, lhs, rhs the same
|
|
*/
|
|
void mpz_mul_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs) {
|
|
if (lhs->len == 0 || rhs->len == 0) {
|
|
mpz_set_from_int(dest, 0);
|
|
return;
|
|
}
|
|
|
|
mpz_t *temp = NULL;
|
|
if (lhs == dest) {
|
|
lhs = temp = mpz_clone(lhs);
|
|
if (rhs == dest) {
|
|
rhs = lhs;
|
|
}
|
|
} else if (rhs == dest) {
|
|
rhs = temp = mpz_clone(rhs);
|
|
}
|
|
|
|
mpz_need_dig(dest, lhs->len + rhs->len); // min mem l+r-1, max mem l+r
|
|
memset(dest->dig, 0, dest->alloc * sizeof(mpz_dig_t));
|
|
dest->len = mpn_mul(dest->dig, lhs->dig, lhs->len, rhs->dig, rhs->len);
|
|
|
|
if (lhs->neg == rhs->neg) {
|
|
dest->neg = 0;
|
|
} else {
|
|
dest->neg = 1;
|
|
}
|
|
|
|
mpz_free(temp);
|
|
}
|
|
|
|
/* computes dest = lhs ** rhs
|
|
can have dest, lhs, rhs the same
|
|
*/
|
|
void mpz_pow_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs) {
|
|
if (lhs->len == 0 || rhs->neg != 0) {
|
|
mpz_set_from_int(dest, 0);
|
|
return;
|
|
}
|
|
|
|
if (rhs->len == 0) {
|
|
mpz_set_from_int(dest, 1);
|
|
return;
|
|
}
|
|
|
|
mpz_t *x = mpz_clone(lhs);
|
|
mpz_t *n = mpz_clone(rhs);
|
|
|
|
mpz_set_from_int(dest, 1);
|
|
|
|
while (n->len > 0) {
|
|
if ((n->dig[0] & 1) != 0) {
|
|
mpz_mul_inpl(dest, dest, x);
|
|
}
|
|
n->len = mpn_shr(n->dig, n->dig, n->len, 1);
|
|
if (n->len == 0) {
|
|
break;
|
|
}
|
|
mpz_mul_inpl(x, x, x);
|
|
}
|
|
|
|
mpz_free(x);
|
|
mpz_free(n);
|
|
}
|
|
|
|
/* computes dest = (lhs ** rhs) % mod
|
|
can have dest, lhs, rhs the same; mod can't be the same as dest
|
|
*/
|
|
void mpz_pow3_inpl(mpz_t *dest, const mpz_t *lhs, const mpz_t *rhs, const mpz_t *mod) {
|
|
if (lhs->len == 0 || rhs->neg != 0 || (mod->len == 1 && mod->dig[0] == 1)) {
|
|
mpz_set_from_int(dest, 0);
|
|
return;
|
|
}
|
|
|
|
mpz_set_from_int(dest, 1);
|
|
|
|
if (rhs->len == 0) {
|
|
return;
|
|
}
|
|
|
|
mpz_t *x = mpz_clone(lhs);
|
|
mpz_t *n = mpz_clone(rhs);
|
|
mpz_t quo;
|
|
mpz_init_zero(&quo);
|
|
|
|
while (n->len > 0) {
|
|
if ((n->dig[0] & 1) != 0) {
|
|
mpz_mul_inpl(dest, dest, x);
|
|
mpz_divmod_inpl(&quo, dest, dest, mod);
|
|
}
|
|
n->len = mpn_shr(n->dig, n->dig, n->len, 1);
|
|
if (n->len == 0) {
|
|
break;
|
|
}
|
|
mpz_mul_inpl(x, x, x);
|
|
mpz_divmod_inpl(&quo, x, x, mod);
|
|
}
|
|
|
|
mpz_deinit(&quo);
|
|
mpz_free(x);
|
|
mpz_free(n);
|
|
}
|
|
|
|
#if 0
|
|
these functions are unused
|
|
|
|
/* computes gcd(z1, z2)
|
|
based on Knuth's modified gcd algorithm (I think?)
|
|
gcd(z1, z2) >= 0
|
|
gcd(0, 0) = 0
|
|
gcd(z, 0) = abs(z)
|
|
*/
|
|
mpz_t *mpz_gcd(const mpz_t *z1, const mpz_t *z2) {
|
|
if (z1->len == 0) {
|
|
// TODO: handle case of z2->alloc=0
|
|
mpz_t *a = mpz_clone(z2);
|
|
a->neg = 0;
|
|
return a;
|
|
} else if (z2->len == 0) {
|
|
mpz_t *a = mpz_clone(z1);
|
|
a->neg = 0;
|
|
return a;
|
|
}
|
|
|
|
mpz_t *a = mpz_clone(z1);
|
|
mpz_t *b = mpz_clone(z2);
|
|
mpz_t c;
|
|
mpz_init_zero(&c);
|
|
a->neg = 0;
|
|
b->neg = 0;
|
|
|
|
for (;;) {
|
|
if (mpz_cmp(a, b) < 0) {
|
|
if (a->len == 0) {
|
|
mpz_free(a);
|
|
mpz_deinit(&c);
|
|
return b;
|
|
}
|
|
mpz_t *t = a;
|
|
a = b;
|
|
b = t;
|
|
}
|
|
if (!(b->len >= 2 || (b->len == 1 && b->dig[0] > 1))) { // compute b > 0; could be mpz_cmp_small_int(b, 1) > 0
|
|
break;
|
|
}
|
|
mpz_set(&c, b);
|
|
do {
|
|
mpz_add_inpl(&c, &c, &c);
|
|
} while (mpz_cmp(&c, a) <= 0);
|
|
c.len = mpn_shr(c.dig, c.dig, c.len, 1);
|
|
mpz_sub_inpl(a, a, &c);
|
|
}
|
|
|
|
mpz_deinit(&c);
|
|
|
|
if (b->len == 1 && b->dig[0] == 1) { // compute b == 1; could be mpz_cmp_small_int(b, 1) == 0
|
|
mpz_free(a);
|
|
return b;
|
|
} else {
|
|
mpz_free(b);
|
|
return a;
|
|
}
|
|
}
|
|
|
|
/* computes lcm(z1, z2)
|
|
= abs(z1) / gcd(z1, z2) * abs(z2)
|
|
lcm(z1, z1) >= 0
|
|
lcm(0, 0) = 0
|
|
lcm(z, 0) = 0
|
|
*/
|
|
mpz_t *mpz_lcm(const mpz_t *z1, const mpz_t *z2) {
|
|
if (z1->len == 0 || z2->len == 0) {
|
|
return mpz_zero();
|
|
}
|
|
|
|
mpz_t *gcd = mpz_gcd(z1, z2);
|
|
mpz_t *quo = mpz_zero();
|
|
mpz_t *rem = mpz_zero();
|
|
mpz_divmod_inpl(quo, rem, z1, gcd);
|
|
mpz_mul_inpl(rem, quo, z2);
|
|
mpz_free(gcd);
|
|
mpz_free(quo);
|
|
rem->neg = 0;
|
|
return rem;
|
|
}
|
|
#endif
|
|
|
|
/* computes new integers in quo and rem such that:
|
|
quo * rhs + rem = lhs
|
|
0 <= rem < rhs
|
|
can have lhs, rhs the same
|
|
assumes rhs != 0 (undefined behaviour if it is)
|
|
*/
|
|
void mpz_divmod_inpl(mpz_t *dest_quo, mpz_t *dest_rem, const mpz_t *lhs, const mpz_t *rhs) {
|
|
assert(!mpz_is_zero(rhs));
|
|
|
|
mpz_need_dig(dest_quo, lhs->len + 1); // +1 necessary?
|
|
memset(dest_quo->dig, 0, (lhs->len + 1) * sizeof(mpz_dig_t));
|
|
dest_quo->len = 0;
|
|
mpz_need_dig(dest_rem, lhs->len + 1); // +1 necessary?
|
|
mpz_set(dest_rem, lhs);
|
|
mpn_div(dest_rem->dig, &dest_rem->len, rhs->dig, rhs->len, dest_quo->dig, &dest_quo->len);
|
|
|
|
// check signs and do Python style modulo
|
|
if (lhs->neg != rhs->neg) {
|
|
dest_quo->neg = 1;
|
|
if (!mpz_is_zero(dest_rem)) {
|
|
mpz_t mpzone;
|
|
mpz_init_from_int(&mpzone, -1);
|
|
mpz_add_inpl(dest_quo, dest_quo, &mpzone);
|
|
mpz_add_inpl(dest_rem, dest_rem, rhs);
|
|
}
|
|
}
|
|
}
|
|
|
|
#if 0
|
|
these functions are unused
|
|
|
|
/* computes floor(lhs / rhs)
|
|
can have lhs, rhs the same
|
|
*/
|
|
mpz_t *mpz_div(const mpz_t *lhs, const mpz_t *rhs) {
|
|
mpz_t *quo = mpz_zero();
|
|
mpz_t rem;
|
|
mpz_init_zero(&rem);
|
|
mpz_divmod_inpl(quo, &rem, lhs, rhs);
|
|
mpz_deinit(&rem);
|
|
return quo;
|
|
}
|
|
|
|
/* computes lhs % rhs ( >= 0)
|
|
can have lhs, rhs the same
|
|
*/
|
|
mpz_t *mpz_mod(const mpz_t *lhs, const mpz_t *rhs) {
|
|
mpz_t quo;
|
|
mpz_init_zero(&quo);
|
|
mpz_t *rem = mpz_zero();
|
|
mpz_divmod_inpl(&quo, rem, lhs, rhs);
|
|
mpz_deinit(&quo);
|
|
return rem;
|
|
}
|
|
#endif
|
|
|
|
// must return actual int value if it fits in mp_int_t
|
|
mp_int_t mpz_hash(const mpz_t *z) {
|
|
mp_uint_t val = 0;
|
|
mpz_dig_t *d = z->dig + z->len;
|
|
|
|
while (d-- > z->dig) {
|
|
val = (val << DIG_SIZE) | *d;
|
|
}
|
|
|
|
if (z->neg != 0) {
|
|
val = -val;
|
|
}
|
|
|
|
return val;
|
|
}
|
|
|
|
bool mpz_as_int_checked(const mpz_t *i, mp_int_t *value) {
|
|
mp_uint_t val = 0;
|
|
mpz_dig_t *d = i->dig + i->len;
|
|
|
|
while (d-- > i->dig) {
|
|
if (val > (~(MP_OBJ_WORD_MSBIT_HIGH) >> DIG_SIZE)) {
|
|
// will overflow
|
|
return false;
|
|
}
|
|
val = (val << DIG_SIZE) | *d;
|
|
}
|
|
|
|
if (i->neg != 0) {
|
|
val = -val;
|
|
}
|
|
|
|
*value = val;
|
|
return true;
|
|
}
|
|
|
|
bool mpz_as_uint_checked(const mpz_t *i, mp_uint_t *value) {
|
|
if (i->neg != 0) {
|
|
// can't represent signed values
|
|
return false;
|
|
}
|
|
|
|
mp_uint_t val = 0;
|
|
mpz_dig_t *d = i->dig + i->len;
|
|
|
|
while (d-- > i->dig) {
|
|
if (val > (~(MP_OBJ_WORD_MSBIT_HIGH) >> (DIG_SIZE - 1))) {
|
|
// will overflow
|
|
return false;
|
|
}
|
|
val = (val << DIG_SIZE) | *d;
|
|
}
|
|
|
|
*value = val;
|
|
return true;
|
|
}
|
|
|
|
void mpz_as_bytes(const mpz_t *z, bool big_endian, size_t len, byte *buf) {
|
|
byte *b = buf;
|
|
if (big_endian) {
|
|
b += len;
|
|
}
|
|
mpz_dig_t *zdig = z->dig;
|
|
int bits = 0;
|
|
mpz_dbl_dig_t d = 0;
|
|
mpz_dbl_dig_t carry = 1;
|
|
for (size_t zlen = z->len; zlen > 0; --zlen) {
|
|
bits += DIG_SIZE;
|
|
d = (d << DIG_SIZE) | *zdig++;
|
|
for (; bits >= 8; bits -= 8, d >>= 8) {
|
|
mpz_dig_t val = d;
|
|
if (z->neg) {
|
|
val = (~val & 0xff) + carry;
|
|
carry = val >> 8;
|
|
}
|
|
if (big_endian) {
|
|
*--b = val;
|
|
if (b == buf) {
|
|
return;
|
|
}
|
|
} else {
|
|
*b++ = val;
|
|
if (b == buf + len) {
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// fill remainder of buf with zero/sign extension of the integer
|
|
if (big_endian) {
|
|
len = b - buf;
|
|
} else {
|
|
len = buf + len - b;
|
|
buf = b;
|
|
}
|
|
memset(buf, z->neg ? 0xff : 0x00, len);
|
|
}
|
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
|
mp_float_t mpz_as_float(const mpz_t *i) {
|
|
mp_float_t val = 0;
|
|
mpz_dig_t *d = i->dig + i->len;
|
|
|
|
while (d-- > i->dig) {
|
|
val = val * DIG_BASE + *d;
|
|
}
|
|
|
|
if (i->neg != 0) {
|
|
val = -val;
|
|
}
|
|
|
|
return val;
|
|
}
|
|
#endif
|
|
|
|
#if 0
|
|
this function is unused
|
|
char *mpz_as_str(const mpz_t *i, unsigned int base) {
|
|
char *s = m_new(char, mp_int_format_size(mpz_max_num_bits(i), base, NULL, '\0'));
|
|
mpz_as_str_inpl(i, base, NULL, 'a', '\0', s);
|
|
return s;
|
|
}
|
|
#endif
|
|
|
|
// assumes enough space in str as calculated by mp_int_format_size
|
|
// base must be between 2 and 32 inclusive
|
|
// returns length of string, not including null byte
|
|
size_t mpz_as_str_inpl(const mpz_t *i, unsigned int base, const char *prefix, char base_char, char comma, char *str) {
|
|
assert(str != NULL);
|
|
assert(2 <= base && base <= 32);
|
|
|
|
size_t ilen = i->len;
|
|
|
|
char *s = str;
|
|
if (ilen == 0) {
|
|
if (prefix) {
|
|
while (*prefix) {
|
|
*s++ = *prefix++;
|
|
}
|
|
}
|
|
*s++ = '0';
|
|
*s = '\0';
|
|
return s - str;
|
|
}
|
|
|
|
// make a copy of mpz digits, so we can do the div/mod calculation
|
|
mpz_dig_t *dig = m_new(mpz_dig_t, ilen);
|
|
memcpy(dig, i->dig, ilen * sizeof(mpz_dig_t));
|
|
|
|
// convert
|
|
char *last_comma = str;
|
|
bool done;
|
|
do {
|
|
mpz_dig_t *d = dig + ilen;
|
|
mpz_dbl_dig_t a = 0;
|
|
|
|
// compute next remainder
|
|
while (--d >= dig) {
|
|
a = (a << DIG_SIZE) | *d;
|
|
*d = a / base;
|
|
a %= base;
|
|
}
|
|
|
|
// convert to character
|
|
a += '0';
|
|
if (a > '9') {
|
|
a += base_char - '9' - 1;
|
|
}
|
|
*s++ = a;
|
|
|
|
// check if number is zero
|
|
done = true;
|
|
for (d = dig; d < dig + ilen; ++d) {
|
|
if (*d != 0) {
|
|
done = false;
|
|
break;
|
|
}
|
|
}
|
|
if (comma && (s - last_comma) == 3) {
|
|
*s++ = comma;
|
|
last_comma = s;
|
|
}
|
|
}
|
|
while (!done);
|
|
|
|
// free the copy of the digits array
|
|
m_del(mpz_dig_t, dig, ilen);
|
|
|
|
if (prefix) {
|
|
const char *p = &prefix[strlen(prefix)];
|
|
while (p > prefix) {
|
|
*s++ = *--p;
|
|
}
|
|
}
|
|
if (i->neg != 0) {
|
|
*s++ = '-';
|
|
}
|
|
|
|
// reverse string
|
|
for (char *u = str, *v = s - 1; u < v; ++u, --v) {
|
|
char temp = *u;
|
|
*u = *v;
|
|
*v = temp;
|
|
}
|
|
|
|
*s = '\0'; // null termination
|
|
|
|
return s - str;
|
|
}
|
|
|
|
#endif // MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_MPZ
|