-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtablegen.py
executable file
·69 lines (58 loc) · 1.71 KB
/
tablegen.py
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
#! /usr/bin/env python3
# -*- coding:utf-8 -*-
"""
This file is part of fftautocorr
Copyright (C) 2020 CareF
author: CareF
Licensed under a 3-clause BSD style license - see LICENSE.md
The script generates a table for the optimized padded length for FFT.
Running this script is part of pre-compilation. See Makefile for details.
"""
import os
import numpy as np
def tablegen(intmax, ps=(2, 3, 5)):
"""
Generate a composite number table < intmax with prime composition in [ps]
"""
logL = np.log2(intmax)
allp = 1
for p in ps:
compp = p**(np.arange(logL/np.log2(p), dtype=np.int))
allp = np.outer(allp, compp).reshape(-1)
allp = allp[allp < intmax]
return np.sort(allp)
def print_as_c_header(filename, array):
contents = """/*
* This file is generated by {this}
*/
#ifndef {filename}
#define {filename}
#include <stdlib.h>
static int find_factor(size_t n) {{
const static int factortable[] = {{
{numbers}
}};
int lo=0, hi=sizeof(factortable)/sizeof(factortable[0]), mid;
while(lo < hi) {{
mid = (lo + hi) / 2;
if(factortable[mid] < n)
lo = mid+1;
else
hi = mid;
/* factortable[lo:] >= n; factortable[:lo] < n */
}}
if(lo == sizeof(factortable)/sizeof(factortable[0]))
return -1;
return factortable[lo];
}}
#endif
""".format(filename=filename.replace('.', '_').upper(),
this=os.path.basename(__file__),
numbers=', '.join([str(n) for n in array]))
with open(filename, 'w') as f:
f.write(contents)
if __name__ == "__main__":
import sys
filename = sys.argv[1]
print("Generating %s" % filename)
print_as_c_header(filename, tablegen(2**31-1))