summaryrefslogtreecommitdiff
path: root/numpy/random/src/dsfmt/dSFMT-calc-jump.hpp
blob: b960826be43cf02f58966dddd0be6bbb04ef4eeb (plain)
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
#pragma once
#ifndef DSFMT_CALC_JUMP_HPP
#define DSFMT_CALC_JUMP_HPP
/**
 * @file dSFMT-calc-jump.hpp
 *
 * @brief functions for calculating jump polynomial.
 *
 * @author Mutsuo Saito (Hiroshima University)
 * @author Makoto Matsumoto (The University of Tokyo)
 *
 * Copyright (C) 2012 Mutsuo Saito, Makoto Matsumoto,
 * Hiroshima University and The University of Tokyo.
 * All rights reserved.
 *
 * The 3-clause BSD License is applied to this software, see
 * LICENSE.txt
 */
#include <iostream>
#include <iomanip>
#include <sstream>
#include <NTL/GF2X.h>

namespace dsfmt {
/**
 * converts polynomial to string for convenient use in C language.
 * @param x output string
 * @param polynomial input polynomial
 */
    static inline void polytostring(std::string& x, NTL::GF2X& polynomial)
    {
	using namespace NTL;
	using namespace std;

	long degree = deg(polynomial);
	int buff;
	stringstream ss;
	for (int i = 0; i <= degree; i+=4) {
	    buff = 0;
	    for (int j = 0; j < 4; j++) {
		if (IsOne(coeff(polynomial, i + j))) {
		    buff |= 1 << j;
		} else {
		    buff &= (0x0f ^ (1 << j));
		}
	    }
	    ss << hex << buff;
	}
	ss << flush;
	x = ss.str();
    }

/**
 * converts string to polynomial
 * @param str string
 * @param poly output polynomial
 */
    static inline void stringtopoly(NTL::GF2X& poly, std::string& str)
    {
	using namespace NTL;
	using namespace std;

	stringstream ss(str);
	char c;
	long p = 0;
	clear(poly);
	while(ss) {
	    ss >> c;
	    if (!ss) {
		break;
	    }
	    if (c >= 'a') {
		c = c - 'a' + 10;
	    } else {
		c = c - '0';
	    }
	    for (int j = 0; j < 4; j++) {
		if (c & (1 << j)) {
		    SetCoeff(poly, p, 1);
		} else {
		    SetCoeff(poly, p, 0);
		}
		p++;
	    }
	}
    }

/**
 * calculate the jump polynomial.
 * SFMT generates 4 32-bit integers from one internal state.
 * @param jump_str output string which represents jump polynomial.
 * @param step jump step of internal state
 * @param characteristic polynomial
 */
    static inline void calc_jump(std::string& jump_str,
				 NTL::ZZ& step,
				 NTL::GF2X& characteristic)
    {
	using namespace NTL;
	using namespace std;
	GF2X jump;
	PowerXMod(jump, step, characteristic);
	polytostring(jump_str, jump);
    }
}
#endif