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