Path: blob/main/Modules/_decimal/libmpdec/basearith.h
12 views
/*1* Copyright (c) 2008-2020 Stefan Krah. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/262728#ifndef LIBMPDEC_BASEARITH_H_29#define LIBMPDEC_BASEARITH_H_303132#include "mpdecimal.h"33#include "typearith.h"343536/* Internal header file: all symbols have local scope in the DSO */37MPD_PRAGMA(MPD_HIDE_SYMBOLS_START)383940mpd_uint_t _mpd_baseadd(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v,41mpd_size_t m, mpd_size_t n);42void _mpd_baseaddto(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n);43mpd_uint_t _mpd_shortadd(mpd_uint_t *w, mpd_size_t m, mpd_uint_t v);44mpd_uint_t _mpd_shortadd_b(mpd_uint_t *w, mpd_size_t m, mpd_uint_t v,45mpd_uint_t b);46mpd_uint_t _mpd_baseincr(mpd_uint_t *u, mpd_size_t n);47void _mpd_basesub(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v,48mpd_size_t m, mpd_size_t n);49void _mpd_basesubfrom(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n);50void _mpd_basemul(mpd_uint_t *w, const mpd_uint_t *u, const mpd_uint_t *v,51mpd_size_t m, mpd_size_t n);52void _mpd_shortmul(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n,53mpd_uint_t v);54mpd_uint_t _mpd_shortmul_c(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n,55mpd_uint_t v);56mpd_uint_t _mpd_shortmul_b(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n,57mpd_uint_t v, mpd_uint_t b);58mpd_uint_t _mpd_shortdiv(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n,59mpd_uint_t v);60mpd_uint_t _mpd_shortdiv_b(mpd_uint_t *w, const mpd_uint_t *u, mpd_size_t n,61mpd_uint_t v, mpd_uint_t b);62int _mpd_basedivmod(mpd_uint_t *q, mpd_uint_t *r, const mpd_uint_t *uconst,63const mpd_uint_t *vconst, mpd_size_t nplusm, mpd_size_t n);64void _mpd_baseshiftl(mpd_uint_t *dest, mpd_uint_t *src, mpd_size_t n,65mpd_size_t m, mpd_size_t shift);66mpd_uint_t _mpd_baseshiftr(mpd_uint_t *dest, mpd_uint_t *src, mpd_size_t slen,67mpd_size_t shift);68697071#ifdef CONFIG_6472extern const mpd_uint_t mprime_rdx;7374/*75* Algorithm from: Division by Invariant Integers using Multiplication,76* T. Granlund and P. L. Montgomery, Proceedings of the SIGPLAN '9477* Conference on Programming Language Design and Implementation.78*79* http://gmplib.org/~tege/divcnst-pldi94.pdf80*81* Variables from the paper and their translations (See section 8):82*83* N := 6484* d := MPD_RADIX85* l := 6486* m' := floor((2**(64+64) - 1)/MPD_RADIX) - 2**6487*88* Since N-l == 0:89*90* dnorm := d91* n2 := hi92* n10 := lo93*94* ACL2 proof: mpd-div-words-r-correct95*/96static inline void97_mpd_div_words_r(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo)98{99mpd_uint_t n_adj, h, l, t;100mpd_uint_t n1_neg;101102/* n1_neg = if lo >= 2**63 then MPD_UINT_MAX else 0 */103n1_neg = (lo & (1ULL<<63)) ? MPD_UINT_MAX : 0;104/* n_adj = if lo >= 2**63 then lo+MPD_RADIX else lo */105n_adj = lo + (n1_neg & MPD_RADIX);106107/* (h, l) = if lo >= 2**63 then m'*(hi+1) else m'*hi */108_mpd_mul_words(&h, &l, mprime_rdx, hi-n1_neg);109l = l + n_adj;110if (l < n_adj) h++;111t = h + hi;112/* At this point t == qest, with q == qest or q == qest+1:113* 1) 0 <= 2**64*hi + lo - qest*MPD_RADIX < 2*MPD_RADIX114*/115116/* t = 2**64-1 - qest = 2**64 - (qest+1) */117t = MPD_UINT_MAX - t;118119/* (h, l) = 2**64*MPD_RADIX - (qest+1)*MPD_RADIX */120_mpd_mul_words(&h, &l, t, MPD_RADIX);121l = l + lo;122if (l < lo) h++;123h += hi;124h -= MPD_RADIX;125/* (h, l) = 2**64*hi + lo - (qest+1)*MPD_RADIX (mod 2**128)126* Case q == qest+1:127* a) h == 0, l == r128* b) q := h - t == qest+1129* c) r := l130* Case q == qest:131* a) h == MPD_UINT_MAX, l == 2**64-(MPD_RADIX-r)132* b) q := h - t == qest133* c) r := l + MPD_RADIX = r134*/135136*q = (h - t);137*r = l + (MPD_RADIX & h);138}139#else140static inline void141_mpd_div_words_r(mpd_uint_t *q, mpd_uint_t *r, mpd_uint_t hi, mpd_uint_t lo)142{143_mpd_div_words(q, r, hi, lo, MPD_RADIX);144}145#endif146147148/* Multiply two single base MPD_RADIX words, store result in array w[2]. */149static inline void150_mpd_singlemul(mpd_uint_t w[2], mpd_uint_t u, mpd_uint_t v)151{152mpd_uint_t hi, lo;153154_mpd_mul_words(&hi, &lo, u, v);155_mpd_div_words_r(&w[1], &w[0], hi, lo);156}157158/* Multiply u (len 2) and v (len m, 1 <= m <= 2). */159static inline void160_mpd_mul_2_le2(mpd_uint_t w[4], mpd_uint_t u[2], mpd_uint_t v[2], mpd_ssize_t m)161{162mpd_uint_t hi, lo;163164_mpd_mul_words(&hi, &lo, u[0], v[0]);165_mpd_div_words_r(&w[1], &w[0], hi, lo);166167_mpd_mul_words(&hi, &lo, u[1], v[0]);168lo = w[1] + lo;169if (lo < w[1]) hi++;170_mpd_div_words_r(&w[2], &w[1], hi, lo);171if (m == 1) return;172173_mpd_mul_words(&hi, &lo, u[0], v[1]);174lo = w[1] + lo;175if (lo < w[1]) hi++;176_mpd_div_words_r(&w[3], &w[1], hi, lo);177178_mpd_mul_words(&hi, &lo, u[1], v[1]);179lo = w[2] + lo;180if (lo < w[2]) hi++;181lo = w[3] + lo;182if (lo < w[3]) hi++;183_mpd_div_words_r(&w[3], &w[2], hi, lo);184}185186187/*188* Test if all words from data[len-1] to data[0] are zero. If len is 0, nothing189* is tested and the coefficient is regarded as "all zero".190*/191static inline int192_mpd_isallzero(const mpd_uint_t *data, mpd_ssize_t len)193{194while (--len >= 0) {195if (data[len] != 0) return 0;196}197return 1;198}199200/*201* Test if all full words from data[len-1] to data[0] are MPD_RADIX-1202* (all nines). Return true if len == 0.203*/204static inline int205_mpd_isallnine(const mpd_uint_t *data, mpd_ssize_t len)206{207while (--len >= 0) {208if (data[len] != MPD_RADIX-1) return 0;209}210return 1;211}212213214MPD_PRAGMA(MPD_HIDE_SYMBOLS_END) /* restore previous scope rules */215216217#endif /* LIBMPDEC_BASEARITH_H_ */218219220