/usr/include/fflas-ffpack/field/modular-balanced-int64.h is in fflas-ffpack-common 1.6.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 | /* -*- mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
// vim:sts=8:sw=8:ts=8:noet:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s
/* Copyright (C) 2010 LinBox
* Adapted by B Boyer <brice.boyer@imag.fr>
* (from other modular-balanced* files)
*
*
* ========LICENCE========
* This file is part of the library FFLAS-FFPACK.
*
* FFLAS-FFPACK is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* ========LICENCE========
*
*/
/*! @file field/modular-balanced-int64.h
* @ingroup field
* @brief Balanced representation of <code>Z/mZ</code> over \c int64_t .
*/
#ifndef __FFLASFFPACK_modular_balanced_int64_H
#define __FFLASFFPACK_modular_balanced_int64_H
#include <math.h>
#include "fflas-ffpack/field/modular-randiter.h"
#include "fflas-ffpack/field/nonzero-randiter.h"
#include "fflas-ffpack/utils/debug.h"
#ifndef LINBOX_MAX_INT64
#ifdef __x86_64__
#define LINBOX_MAX_INT64 INT64_MAX
#else
#define LINBOX_MAX_INT64 INT64_MAX
#endif
#endif
// todo INT64_MAX
namespace FFPACK
{
/// \ingroup field
template <>
class ModularBalanced<int64_t> {
protected:
int64_t modulus;
int64_t half_mod;
int64_t mhalf_mod;
double modulusinv;
public:
typedef int64_t Element;
typedef ModularBalancedRandIter<int64_t> RandIter;
const Element one ;
const Element zero ;
const Element mOne ;
static const bool balanced = true ;
//default modular field,taking 65521 as default modulus
ModularBalanced () :
modulus(65521)
,one(1),zero(0),mOne(-1)
{
modulusinv = 1/(double)65521;
half_mod = (65521 >> 1);
mhalf_mod = half_mod-65520;
}
ModularBalanced (int64_t value, int exp = 1) :
modulus(value)
,one(1),zero(0),mOne(-1)
{
half_mod = (modulus >> 1);
mhalf_mod = half_mod-modulus+1;
modulusinv = 1 / ((double) value);
#ifdef DEBUG
if(exp != 1) throw Failure(__func__,__FILE__,__LINE__,"exponent must be 1");
if(value <= 1) throw Failure(__func__,__FILE__,__LINE__,"modulus must be > 1");
if(value > getMaxModulus() ) throw Failure(__func__,__FILE__,__LINE__,"modulus is too big");
if( ! (value % 2) ) throw Failure(__func__,__FILE__,__LINE__,"modulus must be odd");
#endif
}
ModularBalanced (const ModularBalanced<int64_t>& mf) :
modulus(mf.modulus),
half_mod(mf.half_mod),
mhalf_mod(mf.mhalf_mod),
modulusinv(mf.modulusinv)
,one(mf.one),zero(mf.zero),mOne(mf.mOne)
{ }
ModularBalanced<Element> & assign(const ModularBalanced<Element> &F)
{
modulus = F.modulus;
half_mod = F.half_mod;
mhalf_mod = F.mhalf_mod;
// lmodulus = F.lmodulus;
modulusinv = F.modulusinv;
F.assign(const_cast<Element&>(one),F.one);
F.assign(const_cast<Element&>(zero),F.zero);
F.assign(const_cast<Element&>(mOne),F.mOne);
return *this;
}
#if 1
const ModularBalanced &operator=(const ModularBalanced<int64_t> &F)
{
modulus = F.modulus;
half_mod = F.half_mod;
mhalf_mod = F.mhalf_mod;
// lmodulus = F.lmodulus;
modulusinv = F.modulusinv;
F.assign(const_cast<Element&>(one),F.one);
F.assign(const_cast<Element&>(zero),F.zero);
F.assign(const_cast<Element&>(mOne),F.mOne);
return *this;
}
#endif
uint64_t characteristic () const
{
return (uint64_t)modulus;
}
size_t cardinality () const
{
return (size_t) modulus;
}
double & convert(double &x, const Element &y) const
{
return x = (double) y;
}
float & convert(float &x, const Element &y) const
{
return x = (float) y;
}
std::ostream &write (std::ostream &os) const
{
return os << "balanced int64_t mod " << modulus;
}
std::istream &read (std::istream &is)
{
is >> modulus;
half_mod = modulus/2;
mhalf_mod = half_mod-modulus+1;
modulusinv = 1 /((double) modulus );
#ifdef DEBUG
if(modulus <= 1) throw Failure(__func__,__FILE__,__LINE__,"modulus must be > 1");
if(modulus > getMaxModulus() ) throw Failure(__func__,__FILE__,__LINE__,"modulus is too big");
if( ! (modulus % 2) ) throw Failure(__func__,__FILE__,__LINE__,"modulus must be oddd");
#endif
return is;
}
std::ostream &write (std::ostream &os, const Element &x) const
{
return os << x;
}
std::istream &read (std::istream &is, Element &x) const
{
int64_t tmp;
is >> tmp;
init(x,tmp);
return is;
}
template<class Element1>
Element &init (Element & x, const Element1 &y) const
{
x = y % modulus;
if ( x < mhalf_mod )
x += modulus;
else if (x > half_mod )
x -= modulus;
return x;
}
Element &init (Element &x, const double &y) const
{
x = (Element) fmod(y,(double)modulus);
if (x < mhalf_mod)
x += modulus;
else if (x > half_mod)
x -= modulus;
return x;
}
Element &init (Element &x, const size_t &y) const
{
x = (Element)y % Element(modulus);
if (x < mhalf_mod)
x += modulus;
else if (x > half_mod)
x -= modulus;
return x;
}
inline Element& init(Element& x, int y =0) const
{
x = y % modulus;
if ( x < mhalf_mod )
x += modulus;
else if (x > half_mod )
x -= modulus;
return x;
}
inline Element& init(Element& x, long y) const
{
x = y % modulus;
if ( x < mhalf_mod ) x += modulus;
else if ( x > half_mod ) x -= modulus;
return x;
}
inline Element& assign(Element& x, const Element& y) const
{
return x = y;
}
inline bool areEqual (const Element &x, const Element &y) const
{
return x == y;
}
inline bool isZero (const Element &x) const
{
return x == 0;
}
inline bool isOne (const Element &x) const
{
return x == 1;
}
inline Element &add (Element &x, const Element &y, const Element &z) const
{
x = y + z;
if ( x > half_mod ) x -= modulus;
else if ( x < mhalf_mod ) x += modulus;
return x;
}
inline Element &sub (Element &x, const Element &y, const Element &z) const
{
x = y - z;
if (x > half_mod) x -= modulus;
else if (x < mhalf_mod) x += modulus;
return x;
}
inline Element &mul (Element &x, const Element &y, const Element &z) const
{
int64_t q;
q = (int64_t) ((((double) y) * ((double) z)) * modulusinv); // q could be off by (+/-) 1
x = (int64_t) (y*z - q*modulus);
if (x > half_mod)
x -= modulus;
else if (x < mhalf_mod)
x += modulus;
return x;
}
inline Element &div (Element &x, const Element &y, const Element &z) const
{
Element temp;
inv (temp, z);
return mul (x, y, temp);
}
inline Element &neg (Element &x, const Element &y) const
{
return x = -y;
}
inline Element &inv (Element &x, const Element &y) const
{
int64_t d, t;
XGCD(d, x, t, y, modulus);
#ifdef DEBUG
if (d != 1)
throw Failure(__func__,__FILE__,__LINE__,"InvMod: inverse undefined");
#endif
if (x > half_mod)
x -= modulus;
else if (x < mhalf_mod)
x += modulus;
return x;
}
inline Element &axpy (Element &r,
const Element &a,
const Element &x,
const Element &y) const
{
int64_t q;
q = (int64_t) (((((double) a) * ((double) x)) + (double)y) * modulusinv); // q could be off by (+/-) 1
r = (int64_t) (a * x + y - q*modulus);
if (r > half_mod)
r -= modulus;
else if (r < mhalf_mod)
r += modulus;
return r;
}
inline Element &addin (Element &x, const Element &y) const
{
x += y;
if ( x > half_mod ) x -= modulus;
else if (x < mhalf_mod) x += modulus;
return x;
}
inline Element &subin (Element &x, const Element &y) const
{
x -= y;
if (x > half_mod)
x -= modulus;
else if (x < mhalf_mod)
x += modulus;
return x;
}
inline Element &mulin (Element &x, const Element &y) const
{
return mul(x,x,y);
}
inline Element &divin (Element &x, const Element &y) const
{
return div(x,x,y);
}
inline Element &negin (Element &x) const
{
return x = -x;
}
inline Element &invin (Element &x) const
{
return inv (x, x);
}
inline Element &axpyin (Element &r, const Element &a, const Element &x) const
{
int64_t q;
q = (int64_t) (((((double) a)*((double) x)) + (double)r) * modulusinv); // q could be off by (+/-) 1
r = (int64_t) (a * x + r - q*modulus);
if (r > half_mod)
r -= modulus;
else if (r < mhalf_mod)
r += modulus;
return r;
}
static inline int64_t getMaxModulus()
{
#if 1
#ifdef __x86_64__
return 4611686018427387904L; // 2^62
// return 8589934591L;
#else
return 4611686018427387904LL; // 2^62
// return 8589934591LL;
#endif
#endif
// return 1 << 31;
}
private:
inline static void XGCD(int64_t& d, int64_t& s, int64_t& t, int64_t a, int64_t b)
{
int64_t u, v, u0, v0, u1, v1, u2, v2, q, r;
int64_t aneg = 0, bneg = 0;
if (a < 0) {
#ifdef DEBUG
if (a < -LINBOX_MAX_INT64) throw Failure(__func__,__FILE__,__LINE__,"XGCD: integer overflow");
#endif
a = -a;
aneg = 1;
}
if (b < 0) {
#ifdef DEBUG
if (b < -LINBOX_MAX_INT64) throw Failure(__func__,__FILE__,__LINE__,"XGCD: integer overflow");
#endif
b = -b;
bneg = 1;
}
u1 = 1; v1 = 0;
u2 = 0; v2 = 1;
u = a; v = b;
while (v != 0) {
q = u / v;
r = u % v;
u = v;
v = r;
u0 = u2;
v0 = v2;
u2 = u1 - q*u2;
v2 = v1- q*v2;
u1 = u0;
v1 = v0;
}
if (aneg)
u1 = -u1;
if (bneg)
v1 = -v1;
d = u;
s = u1;
t = v1;
}
};
}
#undef LINBOX_MAX_INT64
#include "field-general.h"
#endif //__FFLASFFPACK_modular_balanced_int64_H
|