[493c477] | 1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
[341696] | 2 | /* $Id$ */ |
---|
[2dd068] | 3 | |
---|
| 4 | #ifndef INCL_FFOPS_H |
---|
| 5 | #define INCL_FFOPS_H |
---|
| 6 | |
---|
[e4fe2b] | 7 | // #include "config.h" |
---|
[84250a6] | 8 | |
---|
[2dd068] | 9 | #include "cf_globals.h" |
---|
[27b3cf] | 10 | #ifdef HAVE_NTL |
---|
| 11 | #include <NTL/config.h> |
---|
| 12 | #endif |
---|
[2dd068] | 13 | |
---|
[e76d7a6] | 14 | /* define type of your compilers 64 bit integer type */ |
---|
| 15 | #ifndef INT64 |
---|
| 16 | #define INT64 long long int |
---|
| 17 | #endif |
---|
| 18 | |
---|
[2dd068] | 19 | extern int ff_prime; |
---|
| 20 | extern int ff_halfprime; |
---|
| 21 | extern short * ff_invtab; |
---|
[02af91] | 22 | extern bool ff_big; |
---|
[2dd068] | 23 | |
---|
| 24 | int ff_newinv ( const int ); |
---|
[02af91] | 25 | int ff_biginv ( const int ); |
---|
[2dd068] | 26 | void ff_setprime ( const int ); |
---|
| 27 | |
---|
| 28 | inline int ff_norm ( const int a ) |
---|
| 29 | { |
---|
[02af91] | 30 | int n = a % ff_prime; |
---|
[27b3cf] | 31 | #if defined(i386) || defined(NTL_AVOID_BRANCHING) |
---|
[806c18] | 32 | n += (n >> 31) & ff_prime; |
---|
[27b3cf] | 33 | return n; |
---|
| 34 | #else |
---|
[2a289e] | 35 | if (n < 0) n += ff_prime; |
---|
[27b3cf] | 36 | return n; |
---|
| 37 | #endif |
---|
[2dd068] | 38 | } |
---|
| 39 | |
---|
| 40 | inline int ff_symmetric( const int a ) |
---|
| 41 | { |
---|
| 42 | if ( cf_glob_switches.isOn( SW_SYMMETRIC_FF ) ) |
---|
[806c18] | 43 | return ( a > ff_halfprime ) ? a - ff_prime : a; |
---|
[2dd068] | 44 | else |
---|
[806c18] | 45 | return a; |
---|
[2dd068] | 46 | } |
---|
| 47 | |
---|
| 48 | inline int ff_longnorm ( const long a ) |
---|
| 49 | { |
---|
[02af91] | 50 | int n = (int)(a % (long)ff_prime); |
---|
[27b3cf] | 51 | #if defined(i386) || defined(NTL_AVOID_BRANCHING) |
---|
[806c18] | 52 | n += (n >> 31) & ff_prime; |
---|
[27b3cf] | 53 | return n; |
---|
| 54 | #else |
---|
[2a289e] | 55 | if (n < 0) n += ff_prime; |
---|
[27b3cf] | 56 | return n; |
---|
| 57 | #endif |
---|
[02af91] | 58 | } |
---|
| 59 | |
---|
[9c9e2a4] | 60 | inline int ff_bignorm ( const INT64 a ) |
---|
[02af91] | 61 | { |
---|
[9c9e2a4] | 62 | int n = (int)(a % (INT64)ff_prime); |
---|
[2a289e] | 63 | #if defined(i386) || defined(NTL_AVOID_BRANCHING) |
---|
[806c18] | 64 | n += (n >> 31) & ff_prime; |
---|
[2a289e] | 65 | return n; |
---|
| 66 | #else |
---|
| 67 | if (n < 0) n += ff_prime; |
---|
| 68 | return n; |
---|
| 69 | #endif |
---|
[2dd068] | 70 | } |
---|
| 71 | |
---|
| 72 | inline int ff_add ( const int a, const int b ) |
---|
| 73 | { |
---|
[79cf7d0] | 74 | //return ff_norm( a + b ); |
---|
[27b3cf] | 75 | #if defined(i386) || defined(NTL_AVOID_BRANCHING) |
---|
[09171bd] | 76 | int r=( a + b ); |
---|
| 77 | r -= ff_prime; |
---|
[806c18] | 78 | r += (r >> 31) & ff_prime; |
---|
[09171bd] | 79 | return r; |
---|
| 80 | #else |
---|
[79cf7d0] | 81 | int r=( a + b ); |
---|
| 82 | if (r >= ff_prime) r -= ff_prime; |
---|
| 83 | return r; |
---|
[09171bd] | 84 | #endif |
---|
[2dd068] | 85 | } |
---|
| 86 | |
---|
| 87 | inline int ff_sub ( const int a, const int b ) |
---|
| 88 | { |
---|
[79cf7d0] | 89 | //return ff_norm( a - b ); |
---|
[27b3cf] | 90 | #if defined(i386) || defined(NTL_AVOID_BRANCHING) |
---|
[09171bd] | 91 | int r=( a - b ); |
---|
[806c18] | 92 | r += (r >> 31) & ff_prime; |
---|
[09171bd] | 93 | return r; |
---|
| 94 | #else |
---|
[79cf7d0] | 95 | int r=( a - b ); |
---|
[2a289e] | 96 | if (r < 0) r += ff_prime; |
---|
[79cf7d0] | 97 | return r; |
---|
[09171bd] | 98 | #endif |
---|
[2dd068] | 99 | } |
---|
| 100 | |
---|
| 101 | inline int ff_neg ( const int a ) |
---|
| 102 | { |
---|
[79cf7d0] | 103 | //return ff_norm( -a ); |
---|
[2a289e] | 104 | // EXPERIMENT |
---|
| 105 | #if defined(i386) || defined(NTL_AVOID_BRANCHING) |
---|
| 106 | int r= -a; |
---|
[806c18] | 107 | r += (r >> 31) & ff_prime; |
---|
[2a289e] | 108 | return r; |
---|
| 109 | #else |
---|
[09171bd] | 110 | return ( a == 0 ? 0 : ff_prime-a ); |
---|
[2a289e] | 111 | #endif |
---|
[2dd068] | 112 | } |
---|
| 113 | |
---|
| 114 | inline int ff_mul ( const int a, const int b ) |
---|
| 115 | { |
---|
[02af91] | 116 | if ( ff_big ) |
---|
[806c18] | 117 | return ff_bignorm( (INT64)a * (INT64)b ); |
---|
[02af91] | 118 | else |
---|
[806c18] | 119 | return ff_longnorm ( (long)a * (long)b ); |
---|
[2dd068] | 120 | } |
---|
| 121 | |
---|
| 122 | inline int ff_inv ( const int a ) |
---|
| 123 | { |
---|
[02af91] | 124 | if ( ff_big ) |
---|
[806c18] | 125 | return ff_biginv( a ); |
---|
[02af91] | 126 | else { |
---|
[806c18] | 127 | register int b; |
---|
| 128 | if ( (b = (int)(ff_invtab[a])) ) |
---|
| 129 | return b; |
---|
| 130 | else |
---|
| 131 | return ff_newinv( a ); |
---|
[02af91] | 132 | } |
---|
[84250a6] | 133 | |
---|
[2dd068] | 134 | } |
---|
| 135 | |
---|
| 136 | inline int ff_div ( const int a, const int b ) |
---|
| 137 | { |
---|
[02af91] | 138 | return ff_mul( a, ff_inv( b ) ); |
---|
[2dd068] | 139 | } |
---|
| 140 | |
---|
[493c477] | 141 | #endif /* ! INCL_FFOPS_H */ |
---|