Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/sun/security/ec/impl/mplogic.c
38918 views
/*1* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.2* Use is subject to license terms.3*4* This library is free software; you can redistribute it and/or5* modify it under the terms of the GNU Lesser General Public6* License as published by the Free Software Foundation; either7* version 2.1 of the License, or (at your option) any later version.8*9* This library is distributed in the hope that it will be useful,10* but WITHOUT ANY WARRANTY; without even the implied warranty of11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU12* Lesser General Public License for more details.13*14* You should have received a copy of the GNU Lesser General Public License15* along with this library; if not, write to the Free Software Foundation,16* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* *********************************************************************24*25* The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.26*27* The Initial Developer of the Original Code is28* Michael J. Fromberger.29* Portions created by the Initial Developer are Copyright (C) 199830* the Initial Developer. All Rights Reserved.31*32* Contributor(s):33*34*********************************************************************** */3536/* Bitwise logical operations on MPI values */3738#include "mpi-priv.h"39#include "mplogic.h"4041/* {{{ Lookup table for population count */4243static unsigned char bitc[] = {440, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,451, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,461, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,472, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,481, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,492, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,502, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,513, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,521, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,532, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,542, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,553, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,562, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,573, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,583, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,594, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 860};6162/* }}} */6364/*65mpl_rsh(a, b, d) - b = a >> d66mpl_lsh(a, b, d) - b = a << d67*/6869/* {{{ mpl_rsh(a, b, d) */7071mp_err mpl_rsh(const mp_int *a, mp_int *b, mp_digit d)72{73mp_err res;7475ARGCHK(a != NULL && b != NULL, MP_BADARG);7677if((res = mp_copy(a, b)) != MP_OKAY)78return res;7980s_mp_div_2d(b, d);8182return MP_OKAY;8384} /* end mpl_rsh() */8586/* }}} */8788/* {{{ mpl_lsh(a, b, d) */8990mp_err mpl_lsh(const mp_int *a, mp_int *b, mp_digit d)91{92mp_err res;9394ARGCHK(a != NULL && b != NULL, MP_BADARG);9596if((res = mp_copy(a, b)) != MP_OKAY)97return res;9899return s_mp_mul_2d(b, d);100101} /* end mpl_lsh() */102103/* }}} */104105/*------------------------------------------------------------------------*/106/*107mpl_set_bit108109Returns MP_OKAY or some error code.110Grows a if needed to set a bit to 1.111*/112mp_err mpl_set_bit(mp_int *a, mp_size bitNum, mp_size value)113{114mp_size ix;115mp_err rv;116mp_digit mask;117118ARGCHK(a != NULL, MP_BADARG);119120ix = bitNum / MP_DIGIT_BIT;121if (ix + 1 > MP_USED(a)) {122rv = s_mp_pad(a, ix + 1);123if (rv != MP_OKAY)124return rv;125}126127bitNum = bitNum % MP_DIGIT_BIT;128mask = (mp_digit)1 << bitNum;129if (value)130MP_DIGIT(a,ix) |= mask;131else132MP_DIGIT(a,ix) &= ~mask;133s_mp_clamp(a);134return MP_OKAY;135}136137/*138mpl_get_bit139140returns 0 or 1 or some (negative) error code.141*/142mp_err mpl_get_bit(const mp_int *a, mp_size bitNum)143{144mp_size bit, ix;145mp_err rv;146147ARGCHK(a != NULL, MP_BADARG);148149ix = bitNum / MP_DIGIT_BIT;150ARGCHK(ix <= MP_USED(a) - 1, MP_RANGE);151152bit = bitNum % MP_DIGIT_BIT;153rv = (mp_err)(MP_DIGIT(a, ix) >> bit) & 1;154return rv;155}156157/*158mpl_get_bits159- Extracts numBits bits from a, where the least significant extracted bit160is bit lsbNum. Returns a negative value if error occurs.161- Because sign bit is used to indicate error, maximum number of bits to162be returned is the lesser of (a) the number of bits in an mp_digit, or163(b) one less than the number of bits in an mp_err.164- lsbNum + numbits can be greater than the number of significant bits in165integer a, as long as bit lsbNum is in the high order digit of a.166*/167mp_err mpl_get_bits(const mp_int *a, mp_size lsbNum, mp_size numBits)168{169mp_size rshift = (lsbNum % MP_DIGIT_BIT);170mp_size lsWndx = (lsbNum / MP_DIGIT_BIT);171mp_digit * digit = MP_DIGITS(a) + lsWndx;172mp_digit mask = ((1 << numBits) - 1);173174ARGCHK(numBits < CHAR_BIT * sizeof mask, MP_BADARG);175ARGCHK(MP_HOWMANY(lsbNum, MP_DIGIT_BIT) <= MP_USED(a), MP_RANGE);176177if ((numBits + lsbNum % MP_DIGIT_BIT <= MP_DIGIT_BIT) ||178(lsWndx + 1 >= MP_USED(a))) {179mask &= (digit[0] >> rshift);180} else {181mask &= ((digit[0] >> rshift) | (digit[1] << (MP_DIGIT_BIT - rshift)));182}183return (mp_err)mask;184}185186/*187mpl_significant_bits188returns number of significnant bits in abs(a).189returns 1 if value is zero.190*/191mp_err mpl_significant_bits(const mp_int *a)192{193mp_err bits = 0;194int ix;195196ARGCHK(a != NULL, MP_BADARG);197198ix = MP_USED(a);199for (ix = MP_USED(a); ix > 0; ) {200mp_digit d;201d = MP_DIGIT(a, --ix);202if (d) {203while (d) {204++bits;205d >>= 1;206}207break;208}209}210bits += ix * MP_DIGIT_BIT;211if (!bits)212bits = 1;213return bits;214}215216/*------------------------------------------------------------------------*/217/* HERE THERE BE DRAGONS */218219220