File: GivaroTest.cpp

package info (click to toggle)
macaulay2 1.21%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 133,096 kB
  • sloc: cpp: 110,377; ansic: 16,306; javascript: 4,193; makefile: 3,821; sh: 3,580; lisp: 764; yacc: 590; xml: 177; python: 140; perl: 114; lex: 65; awk: 3
file content (197 lines) | stat: -rw-r--r-- 6,467 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright (c) 994-2009 by The Givaro group
// This file is part of Givaro.
// Givaro is governed by the CeCILL-B license under French law
// and abiding by the rules of distribution of free software.
// see the COPYRIGHT file for more details.
#include <iostream>
#include <givaro/modular.h>
#include <givaro/montgomery.h>
#include <givaro/gfq.h>
#include <givaro/gfqext.h>
using namespace Givaro;
template <class Field>
void TestField(const Field& F)
{
  std::cerr << "Within ";
  F.write(std::cerr);
  std::cerr << " : " << std::flush;
  typename Field::Element a, b, c, d;
  F.init(a, 7U);
  F.init(b, -29.3);
  F.init(c);       // empty constructor
  F.init(d);       // empty constructor
  F.add(c, a, b);  // c = a+b
  // Separate output writing
  F.write(std::cout, a) << " + " << std::flush;
  F.write(std::cout, b) << " = " << std::flush;
  F.write(std::cerr, c) << std::endl;
  F.mul(c, a, b);      // c = a*b
  F.axpy(d, a, b, c);  // d = a*b + c;
  // Writing all outputs in a single command line
  F.write(std::cerr << "Within ") << " : " << std::flush;
  F.write(F.write(F.write(F.write(std::cout, c) << " + ", a) << " * ", b)
              << " = ",
          d)
      << std::endl;
  {
    typename Field::Element e;
    F.init(e);
    F.assign(e, d);
    F.maxpy(e, a, b, d);  // e = d-a*b
    // Writing all outputs in a single command line
    F.write(std::cerr << "Within ") << " : " << std::flush;
    F.write(F.write(F.write(F.write(std::cout, d) << " - ", a) << " * ", b)
                << " = ",
            e)
        << std::endl;
  }
  {
    typename Field::Element e;
    F.init(e);
    F.assign(e, d);
    F.maxpyin(e, a, b);  // e = d - a*b;
    // Writing all outputs in a single command line
    F.write(std::cerr << "Within ") << " : " << std::flush;
    F.write(F.write(F.write(F.write(std::cout, d) << " - ", a) << " * ", b)
                << " = ",
            e)
        << std::endl;
  }
  {
    typename Field::Element e;
    F.init(e);
    F.assign(e, d);
    F.axmy(e, a, b, d);  // e = a*b -d;
    // Writing all outputs in a single command line
    F.write(std::cerr << "Within ") << " : " << std::flush;
    F.write(F.write(F.write(F.write(std::cout, a) << " * ", b) << " - ", d)
                << " = ",
            e)
        << std::endl;
  }
  {
    typename Field::Element e;
    F.init(e);
    F.assign(e, d);
    F.maxpyin(e, a, b);  // e = d - a*b;
    // Writing all outputs in a single command line
    F.write(std::cerr << "Within ") << " : " << std::flush;
    F.write(F.write(F.write(F.write(std::cout, d) << " - ", a) << " * ", b)
                << " = ",
            e)
        << std::endl;
  }
  // Four operations
  F.write(F.write(std::cout, a) << " += ", b) << " is ";
  F.write(std::cout, F.addin(a, b)) << "   ;   ";
  F.write(F.write(std::cout, a) << " -= ", b) << " is ";
  F.write(std::cout, F.subin(a, b)) << "   ;   ";
  F.write(F.write(std::cout, a) << " *= ", b) << " is ";
  F.write(std::cout, F.mulin(a, b)) << "   ;   ";
  F.write(F.write(std::cout, a) << " /= ", b) << " is ";
  F.write(std::cout, F.divin(a, b)) << std::endl;
  F.init(a, 22996);
  F.inv(b, a);
  F.write(F.write(std::cout << "1/", a) << " is ", b) << std::endl;
  F.mul(c, b, a);
  F.write(std::cout << "1 is ", c) << std::endl;
  F.init(a, 22996);
  F.init(b, 22996);
  F.write(std::cout << "1/", a) << " is ";
  F.invin(a);
  F.write(std::cout, a) << std::endl;
  F.mulin(a, b);
  F.write(std::cout << "1 is ", a) << std::endl;
  F.init(a, 37403);
  F.inv(b, a);
  F.write(F.write(std::cout << "1/", a) << " is ", b) << std::endl;
  F.mul(c, b, a);
  F.write(std::cout << "1 is ", c) << std::endl;
  F.init(a, 37403);
  F.init(b, 37403);
  F.write(std::cout << "1/", a) << " is ";
  F.invin(a);
  F.write(std::cout, a) << std::endl;
  F.mulin(a, b);
  F.write(std::cout << "1 is ", a) << std::endl;
}
extern "C" {
#include <sys/time.h>
#include <sys/resource.h>
}
int main(int argc, char** argv)
{
  // modulo 13 over 16 bits
  Modular<int16_t> C13(13);
  TestField(C13);
  // modulo 13 over 32 bits
  Modular<int32_t> Z13(13);
  TestField(Z13);
  // modulo 13 over unsigned 32 bits
  Modular<uint32_t> U13(13);
  TestField(U13);
#ifdef __USE_Givaro_SIXTYFOUR__
  // modulo 13 over 64 bits
  Modular<int64_t> LL13(13U);
  TestField(LL13);
#endif
  // modulo 13 fully tabulated
  Modular<Log16> L13(13);
  TestField(L13);
  // modulo 13 over 32 bits with Montgomery reduction
  Montgomery<int32_t> M13(13);
  TestField(M13);
  Montgomery<int32_t> M3(39989);
  TestField(M3);
  // modulo 13 with primitive root representation
  GFqDom<int> GF13(13);
  TestField(GF13);
  // modulo 13 over arbitrary size
  Modular<Integer> IntZ13(13);
  TestField(IntZ13);
  // Zech log finite field with 5^4 elements
  GFqDom<int> GF625(5, 4);
  TestField(GF625);
#if 0 // Possibly related: https://github.com/cr-marcstevens/m4gb/issues/8
  // Zech log finite field with 256 elements
  // and prescribed irreducible polynomial
  std::vector<GFqDom<unsigned long long>::Residu_t> Irred(9);
  Irred[0] = 1;
  Irred[1] = 1;
  Irred[2] = 0;
  Irred[3] = 1;
  Irred[4] = 1;
  Irred[5] = 0;
  Irred[6] = 0;
  Irred[7] = 0;
  Irred[8] = 1;
  GFqDom<unsigned long long> F256(2, 8, Irred);
  TestField(F256);
#endif
  // Zech log finite field with 3^4 elements
  // Using the Q-adic Transform
  GFqExt<int32_t> GF81(3, 4);
  TestField(GF81);
  // Zech log finite field with 2Mb tables
  struct rusage tmp1;
  getrusage(RUSAGE_SELF, &tmp1);
  // user time
  double tim = (double)tmp1.ru_utime.tv_sec +
               ((double)tmp1.ru_utime.tv_usec) / (1000000.0);
  ;
  getrusage(RUSAGE_SELF, &tmp1);
  tim = (double)tmp1.ru_utime.tv_sec +
        ((double)tmp1.ru_utime.tv_usec) / (1000000.0) - tim;
  std::cerr << "Initialization took " << tim
            << " cpu seconds and : " << std::endl;
  std::cerr << tmp1.ru_maxrss << " maximum resident set size" << std::endl
            << tmp1.ru_ixrss << " integral shared memory size" << std::endl
            << tmp1.ru_idrss << " integral unshared data size" << std::endl
            << tmp1.ru_isrss << " integral unshared stack size" << std::endl
            << tmp1.ru_minflt << " page reclaims" << std::endl
            << tmp1.ru_majflt << " page faults" << std::endl
            << tmp1.ru_nswap << " swaps" << std::endl
            << tmp1.ru_inblock << " block input operations" << std::endl
            << tmp1.ru_oublock << " block output operations" << std::endl;
  return 0;
}