Changeset 6e969c in git


Ignore:
Timestamp:
Apr 23, 2018, 6:10:26 PM (6 years ago)
Author:
Hans Schoenemann <hannes@…>
Branches:
(u'fieker-DuVal', '117eb8c30fc9e991c4decca4832b1d19036c4c65')(u'spielwiese', 'b4f17ed1d25f93d46dbe29e4b499baecc2fd51bb')
Children:
2a26e5cb4fa3b3a9830c9062444f5d6051880a5f
Parents:
eb6e1d58eb9834a39221581ba56d2e26f61499cc
Message:
opt: Z/p arithm. for slimgb
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • kernel/GBEngine/tgb_internal.h

    reb6e1d r6e969c  
    7171
    7272#define npInit n_Init
    73 #define npNeg n_InpNeg
    74 #define npInvers n_Invers
    75 #define npMult n_Mult
    76 #define npIsOne n_IsOne
    77 #define npIsZero n_IsZero
     73#define npNeg npNegM
     74#define npInvers npInversM
     75#define npMult npMultM
     76//#define npIsOne n_IsOne
     77#define npIsZero npIsZeroM
    7878
    7979#else
  • libpolys/coeffs/modulop.cc

    reb6e1d r6e969c  
    141141}
    142142
    143 BOOLEAN npIsOne (number a, const coeffs r)
    144 {
    145   n_Test(a, r);
    146 
    147   return 1 == (long)a;
    148 }
    149 
    150143BOOLEAN npIsMOne (number a, const coeffs r)
    151144{
     
    153146
    154147  return ((r->npPminus1M == (long)a) &&(1L!=(long)a))/*for char 2*/;
    155 }
    156 
    157 #ifdef USE_NTL_XGCD
    158 
    159 //ifdef HAVE_NTL // in ntl.a
    160 //extern void XGCD(long& d, long& s, long& t, long a, long b);
    161 #include <NTL/ZZ.h>
    162 #ifdef NTL_CLIENT
    163 NTL_CLIENT
    164 #endif
    165 
    166 #endif
    167 
    168 static inline long InvMod(long a, const coeffs R)
    169 {
    170    long s, t;
    171 
    172 #ifdef USE_NTL_XGCD
    173    long d;
    174    XGCD(d, s, t, a, R->ch);
    175    assume (d == 1);
    176 #else
    177    long  u, v, u0, v0, u1, v1, u2, v2, q, r;
    178 
    179    assume(a>0);
    180    u1=1; u2=0;
    181    u = a; v = R->ch;
    182 
    183    while (v != 0)
    184    {
    185       q = u / v;
    186       //r = u % v;
    187       r = u - q*v;
    188       u = v;
    189       v = r;
    190       u0 = u2;
    191       u2 = u1 - q*u2;
    192       u1 = u0;
    193    }
    194 
    195    assume(u==1);
    196    s = u1;
    197 #endif
    198 #ifdef HAVE_GENERIC_ADD
    199    if (s < 0)
    200       return s + R->ch;
    201    else
    202       return s;
    203 #else
    204   #if SIZEOF_LONG == 8
    205   s += (s >> 63) & R->ch;
    206   #else
    207   s += (s >> 31) & R->ch;
    208   #endif
    209   return s;
    210 #endif
    211 }
    212 
    213 static inline number npInversM (number c, const coeffs r)
    214 {
    215   n_Test(c, r);
    216 #ifndef HAVE_GENERIC_MULT
    217   #ifndef HAVE_INVTABLE
    218   number d = (number)(long)r->npExpTable[r->npPminus1M - r->npLogTable[(long)c]];
    219   #else
    220   long inv=(long)r->npInvTable[(long)c];
    221   if (inv==0)
    222   {
    223     inv = (long)r->npExpTable[r->npPminus1M - r->npLogTable[(long)c]];
    224     r->npInvTable[(long)c]=inv;
    225   }
    226   number d = (number)inv;
    227   #endif
    228 #else
    229   #ifdef HAVE_INVTABLE
    230   long inv=(long)r->npInvTable[(long)c];
    231   if (inv==0)
    232   {
    233     inv=InvMod((long)c,r);
    234     r->npInvTable[(long)c]=inv;
    235   }
    236   #else
    237   long  inv=InvMod((long)c,r);
    238   #endif
    239   number d = (number)inv;
    240 #endif
    241   n_Test(d, r);
    242   return d;
    243148}
    244149
     
    826731static inline number nvInversM (number c, const coeffs r)
    827732{
    828   long inv=InvMod((long)c,r);
     733  long inv=npInvMod((long)c,r);
    829734  return (number)inv;
    830735}
  • libpolys/coeffs/modulop.h

    reb6e1d r6e969c  
    2929#define NV_MAX_PRIME 32749
    3030#define FACTORY_MAX_PRIME 536870909
     31
     32#ifdef USE_NTL_XGCD
     33//ifdef HAVE_NTL // in ntl.a
     34//extern void XGCD(long& d, long& s, long& t, long a, long b);
     35#include <NTL/ZZ.h>
     36#ifdef NTL_CLIENT
     37NTL_CLIENT
     38#endif
     39#endif
    3140
    3241struct n_Procs_s; typedef struct  n_Procs_s  *coeffs;
     
    173182  return 0 == (long)a;
    174183}
    175 
    176 // inline number npMultM(number a, number b, int npPrimeM)
    177 // {
    178 //   return (number)(((long)a*(long)b) % npPrimeM);
    179 // }
     184static inline BOOLEAN npIsOne (number a, const coeffs)
     185{
     186  return 1 == (long)a;
     187}
     188
     189static inline long npInvMod(long a, const coeffs R)
     190{
     191   long s, t;
     192
     193#ifdef USE_NTL_XGCD
     194   long d;
     195   XGCD(d, s, t, a, R->ch);
     196   assume (d == 1);
     197#else
     198   long  u, v, u0, v0, u1, v1, u2, v2, q, r;
     199
     200   assume(a>0);
     201   u1=1; u2=0;
     202   u = a; v = R->ch;
     203
     204   while (v != 0)
     205   {
     206      q = u / v;
     207      //r = u % v;
     208      r = u - q*v;
     209      u = v;
     210      v = r;
     211      u0 = u2;
     212      u2 = u1 - q*u2;
     213      u1 = u0;
     214   }
     215
     216   assume(u==1);
     217   s = u1;
     218#endif
     219#ifdef HAVE_GENERIC_ADD
     220   if (s < 0)
     221      return s + R->ch;
     222   else
     223      return s;
     224#else
     225  #if SIZEOF_LONG == 8
     226  s += (s >> 63) & R->ch;
     227  #else
     228  s += (s >> 31) & R->ch;
     229  #endif
     230  return s;
     231#endif
     232}
     233
     234static inline number npInversM (number c, const coeffs r)
     235{
     236  n_Test(c, r);
     237#ifndef HAVE_GENERIC_MULT
     238  #ifndef HAVE_INVTABLE
     239  number d = (number)(long)r->npExpTable[r->npPminus1M - r->npLogTable[(long)c]];
     240  #else
     241  long inv=(long)r->npInvTable[(long)c];
     242  if (inv==0)
     243  {
     244    inv = (long)r->npExpTable[r->npPminus1M - r->npLogTable[(long)c]];
     245    r->npInvTable[(long)c]=inv;
     246  }
     247  number d = (number)inv;
     248  #endif
     249#else
     250  #ifdef HAVE_INVTABLE
     251  long inv=(long)r->npInvTable[(long)c];
     252  if (inv==0)
     253  {
     254    inv=npInvMod((long)c,r);
     255    r->npInvTable[(long)c]=inv;
     256  }
     257  #else
     258  long  inv=npInvMod((long)c,r);
     259  #endif
     260  number d = (number)inv;
     261#endif
     262  n_Test(d, r);
     263  return d;
     264}
     265
    180266
    181267// The folloing is reused inside gnumpc.cc, gnumpfl.cc and longrat.cc
Note: See TracChangeset for help on using the changeset viewer.