summaryrefslogtreecommitdiffstats
path: root/3rdparty/openpgm-svn-r1135/pgm/galois_generator.pl
blob: b3f531ddea0b6d325fec995d659bfc9c3dd45098 (plain) (blame)
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
#!/usr/bin/perl
#
# Galois field table generator.
#
# Copyright (c) 2006-2010 Miru Limited.
#
# This library 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

use strict;

my $GF_ELEMENT_BYTES = 1;
my $GF_ELEMENT_BITS = 8 * $GF_ELEMENT_BYTES;
my $GF_NO_ELEMENTS = 1 << $GF_ELEMENT_BITS;
my $GF_MAX = $GF_NO_ELEMENTS - 1;

my $GF_GENERATOR = 0x11d;

my @gflog;
my @gfantilog;

my $j = 1;

for (my $i = 0; $i < $GF_MAX; $i++)
{
	$gflog[ $j ] = $i;
	$gfantilog[ $i ] = $j;

	$j <<= 1;
	if ($j & $GF_NO_ELEMENTS) {
		$j ^= $GF_GENERATOR;
	}
}

$gflog[ 0 ] = $GF_MAX;
$gfantilog[ $GF_MAX ] = 0;

print<<MOO;
/* vim:ts=8:sts=8:sw=4:noai:noexpandtab
 *
 * Galois field tables
 *
 * Copyright (c) 2006-2010 Miru Limited.
 *
 * This library 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <impl/framework.h>


/* globals */

const pgm_gf8_t pgm_gflog[PGM_GF_NO_ELEMENTS] =
{
MOO

# print out y = log₂(x) table
for (my $i = 0; $i < $GF_NO_ELEMENTS; $i++)
{
	print "\t" if ($i % 8 == 0);
	print sprintf("0x%2.2x", $gflog[ $i ]);
	print ',' unless ($i == $GF_MAX);
	print ( (($i % 8) == 7) ? "\n" : ' ' );
}

print<<MOO;
};

const pgm_gf8_t pgm_gfantilog[PGM_GF_NO_ELEMENTS] =
{
MOO

# print out y = antilog₂(x) table, aka pow2(x), 2^^x
for (my $i = 0; $i < $GF_NO_ELEMENTS; $i++)
{
	print "\t" if ($i % 8 == 0);
	print sprintf("0x%2.2x", $gfantilog[ $i ]);
	print ',' unless ($i == $GF_MAX);
	print ( (($i % 8) == 7) ? "\n" : ' ' );
}

print<<MOO;
};

#ifdef CONFIG_GALOIS_MUL_LUT
const pgm_gf8_t pgm_gftable[PGM_GF_NO_ELEMENTS * PGM_GF_NO_ELEMENTS] =
{
MOO

sub gfmul {
	my($a, $b) = @_;
	return 0 if ($a == 0 || $b == 0);
	my $sum = $gflog[ $a ] + $gflog[ $b ];
	return ($sum >= $GF_MAX) ? $gfantilog[ $sum - $GF_MAX ] : $gfantilog[ $sum ];
}

# print out multiplication table z = x • y
for (my $i = 0; $i < $GF_NO_ELEMENTS; $i++)
{
	for (my $j = 0; $j < $GF_NO_ELEMENTS; $j++)
	{
		print "\t" if ($j % 8 == 0);
		print sprintf("0x%2.2x", gfmul( $i, $j ));
		print ',' unless ($i == $GF_MAX && $j == $GF_MAX);
		print ( (($j % 8) == 7) ? "\n" : ' ' );
	}
}

print<<MOO;
};
#endif

/* eof */
MOO

# eof