Back to index

lightning-sunbird  0.9+nobinonly
mpi_hp.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 /* $Id: mpi_hp.c,v 1.5 2004/04/27 23:04:36 gerv%gerv.net Exp $ */
00037 
00038 /* This file contains routines that perform vector multiplication.  */
00039 
00040 #include "mpi-priv.h"
00041 #include <unistd.h>
00042 
00043 #include <stddef.h>
00044 /* #include <sys/systeminfo.h> */
00045 #include <strings.h>
00046 
00047 extern void multacc512( 
00048    int             length,        /* doublewords in multiplicand vector. */
00049    const mp_digit *scalaraddr,    /* Address of scalar. */
00050    const mp_digit *multiplicand,  /* The multiplicand vector. */
00051    mp_digit *      result);       /* Where to accumulate the result. */
00052 
00053 extern void maxpy_little(
00054    int             length,        /* doublewords in multiplicand vector. */
00055    const mp_digit *scalaraddr,    /* Address of scalar. */
00056    const mp_digit *multiplicand,  /* The multiplicand vector. */
00057    mp_digit *      result);       /* Where to accumulate the result. */
00058 
00059 extern void add_diag_little(
00060    int            length,       /* doublewords in input vector. */
00061    const mp_digit *root,         /* The vector to square. */
00062    mp_digit *      result);      /* Where to accumulate the result. */
00063 
00064 void 
00065 s_mpv_sqr_add_prop(const mp_digit *pa, mp_size a_len, mp_digit *ps)
00066 {
00067     add_diag_little(a_len, pa, ps);
00068 }
00069 
00070 #define MAX_STACK_DIGITS 258
00071 #define MULTACC512_LEN   (512 / MP_DIGIT_BIT)
00072 #define HP_MPY_ADD_FN    (a_len == MULTACC512_LEN ? multacc512 : maxpy_little)
00073 
00074 /* c = a * b */
00075 void 
00076 s_mpv_mul_d(const mp_digit *a, mp_size a_len, mp_digit b, mp_digit *c)
00077 {
00078     mp_digit x[MAX_STACK_DIGITS];
00079     mp_digit *px = x;
00080     size_t   xSize = 0;
00081 
00082     if (a == c) {
00083        if (a_len > MAX_STACK_DIGITS) {
00084            xSize = sizeof(mp_digit) * (a_len + 2);
00085            px = malloc(xSize);
00086            if (!px)
00087               return;
00088        }
00089        memcpy(px, a, a_len * sizeof(*a));
00090        a = px;
00091     }
00092     s_mp_setz(c, a_len + 1);
00093     HP_MPY_ADD_FN(a_len, &b, a, c);
00094     if (px != x && px) {
00095        memset(px, 0, xSize);
00096        free(px);
00097     }
00098 }
00099 
00100 /* c += a * b, where a is a_len words long. */
00101 void     
00102 s_mpv_mul_d_add(const mp_digit *a, mp_size a_len, mp_digit b, mp_digit *c)
00103 {
00104     c[a_len] = 0;    /* so carry propagation stops here. */
00105     HP_MPY_ADD_FN(a_len, &b, a, c);
00106 }
00107 
00108 /* c += a * b, where a is y words long. */
00109 void     
00110 s_mpv_mul_d_add_prop(const mp_digit *a, mp_size a_len, mp_digit b, 
00111                       mp_digit *c)
00112 {
00113     HP_MPY_ADD_FN(a_len, &b, a, c);
00114 }
00115