File: create-pc-tables.pl

package info (click to toggle)
libmath-prime-util-perl 0.73-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,796 kB
  • sloc: perl: 24,676; ansic: 11,471; makefile: 26; python: 24
file content (80 lines) | stat: -rwxr-xr-x 2,347 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env perl
use strict;
use warnings;
use ntheory ":all";
use v5.20;

my $s = 0;
if(0) {
  $s+= make_table(1,    15,   0,   9);
  $s+= make_table(2,    30,   9,  39);
  $s+= make_table(3,    60,  39,  63);
  $s+= make_table(4,   300,  60, 120);
  $s+= make_table(5, 30000, 120,3000);
}
if(0) {
  $s+= make_table(1,    15,   0,   9);
  $s+= make_table(2,    30,   9,  39);
  $s+= make_table(3,    60,  39,  63);
  $s+= make_table(4,    60,  63,  90);
  $s+= make_table(5, 15000,  90,3000);
}
if(0) {
  $s+= make_table(1,     5,   0,   1.5);
  $s+= make_table(2,    15,   1.5, 12);
  $s+= make_table(3,    30,  12,  39);
  $s+= make_table(4,    30,  39,  66);
  $s+= make_table(5,    60,  66,  90);
  $s+= make_table(6, 30000,  90,3000);
}
if(1) {
  #                      k    M       M
  $s+= make_table(0,     3,   0,      0.30);
  $s+= make_table(1,     6,   0.30,   3.0 );
  $s+= make_table(2,    15,   3.0,   15   );
  $s+= make_table(3,    30,  15,     42   );
  $s+= make_table(4,    30,  42,     69   );
  $s+= make_table(5,    60,  69,     90   );
  $s+= make_table(6, 30000,  90,   3000   );
}
say "/* $s bytes */";


sub make_table {
  my($name, $stepk, $start, $stop) = @_;
  my $step = 1000 * $stepk;
  $start *= 1_000_000;
  $stop  *= 1_000_000;

  die "start must be less than stop" unless $start < $stop;
  die "start must be divisible by step" unless ($start % $step) == 0;
  die "stop  must be divisible by step" unless ($stop  % $step) == 0;
  my $s = $start / $step;
  my $pc = prime_count($start);
  my $nsteps = ($stop - $start) / $step;

  if ($start == 0) {
    $s = 0;
    $pc = prime_count(5);
  }

  my @c;
  {
    my($npc,$spc) = ($pc);
    @c = map { ($spc,$npc) = ($npc, prime_count(($s+$_)*$step)); $npc-$spc; } 1 .. $nsteps;
  }
  my $min = vecmin(@c);
  @c = map { $_-$min } @c;
  my $max = vecmax(@c);

  say "#define NSTEP_STEP_$name   $step";
  say "#define NSTEP_START_$name  $start";
  say "#define NSTEP_COUNT_$name  $pc";
  say "#define NSTEP_BASE_$name   $min";
  my $type = ($max <= 255) ? "char" : ($max <= 65535) ? "short" : "int";
  say "static const unsigned $type step_counts_${name}[] =";
  say "{",join(",",@c),"};";
  say "#define NSTEP_NUM_$name  (sizeof(step_counts_$name)/sizeof(step_counts_${name}[0]))";
  say "";
  return scalar(@c) * (($max <= 255) ? 1 : ($max <= 65535) ? 2 : 4);
}