summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--generate_table.c76
1 files changed, 67 insertions, 9 deletions
diff --git a/generate_table.c b/generate_table.c
index c7a9d0c..9fd05bc 100644
--- a/generate_table.c
+++ b/generate_table.c
@@ -6,6 +6,59 @@
6// s( 1) n(-4) off( 0) x = ( x << 4 ) 6// s( 1) n(-4) off( 0) x = ( x << 4 )
7// s( 1) n( 8) off(-1) x = - x + ( x << 8 ) 7// s( 1) n( 8) off(-1) x = - x + ( x << 8 )
8 8
9typedef enum { use_2pn, use_1_2pn, use_2pm_2pn, use_1_2pm_2pn } tr_flag;
10typedef struct {
11 double factor;
12 int n;
13 int m;
14 int nsign;
15 tr_flag flag;
16} table_record;
17
18static int g_records;
19static table_record g_tr[8192];
20
21static int compare_record( const void *a, const void *b ) {
22 if( ((table_record*)a)->factor > ((table_record*)b)->factor )
23 return 1;
24 if( ((table_record*)a)->factor < ((table_record*)b)->factor )
25 return -1;
26 return 0;
27}
28
29static void add_record( double v, int n, int nsign, int m, tr_flag flag )
30{
31 g_tr[g_records].factor = v;
32 g_tr[g_records].n = n;
33 g_tr[g_records].m = m;
34 g_tr[g_records].nsign = nsign;
35 g_tr[g_records].flag = flag;
36 ++g_records;
37}
38
39static void dump_record( int record )
40{
41 if( record >= g_records ) return; // should warn?
42 double factor = g_tr[record].factor;
43 int n = g_tr[record].n;
44 int m = g_tr[record].m;
45 int nsign = g_tr[record].nsign;
46 tr_flag flag = g_tr[record].flag;
47
48 switch( flag ) {
49 case use_2pn:
50 printf( "%0+25.21lf x = x %s %2d\n", factor, n>=0?"<<":">>", (int)abs(n) );
51 break;
52 case use_1_2pn:
53 printf( "%0+25.21lf x%s= x %s %2d\n", factor, nsign==-1?"-":"+", n>=0?"<<":">>", (int)abs(n) );
54 break;
55 case use_2pm_2pn:
56 case use_1_2pm_2pn:
57 printf( "%0+25.21lf x%s= ( x %s %2d ) %s ( x %s %2d )\n", factor, flag==use_2pm_2pn?" ":"-", m>=0?"<<":">>", (int)abs(m), nsign==-1?"-":"+", n>=0?"<<":">>", (int)abs(n));
58 break;
59 }
60}
61
9int main() { 62int main() {
10 int n, m, nsign; 63 int n, m, nsign;
11 64
@@ -14,21 +67,26 @@ int main() {
14 for ( nsign=-1; nsign<=1; nsign+=2 ) 67 for ( nsign=-1; nsign<=1; nsign+=2 )
15 { 68 {
16 // The one term only case 69 // The one term only case
17 double v = (double)nsign * pow( 2.f, (double)n ); 70 double v = (double)nsign * pow( 2., (double)n );
18 if( v > 0.f ) 71 if( v > 0.0 )
19 printf( "%0+25.21lf %08X x = %s ( x %s %2d )%s\n", log(v), abs((int)(67108864.f*log(v))), nsign==-1?"-":" ", n>=0?"<<":">>", (int)abs(n), " !RECOMMENDED!" ); 72 add_record( log(v), n, nsign, m, use_2pn );
20 73
21 // Loop over second term 74 // Loop over second term
22 for (m=-31; m<=31; ++m ) 75 for (m=-31; m<=31; ++m )
23 { 76 {
24 double v = pow( 2.f, (double)m ) + (double)nsign * pow( 2.f, (double)n ); 77 double v = pow( 2.0, (double)m ) + (double)nsign * pow( 2., (double)n );
25 if( v > 0.f ) { 78 if( v <= 0.0 )
26 printf( "%0+25.21lf %08X x = ( x %s %2d ) %s ( x %s %2d )%s\n", log(v), abs((int)(67108864.f*log(v))), m>=0?"<<":">>", (int)abs(m), nsign==-1?"-":"+", n>=0?"<<":">>", (int)abs(n), n*m?"":" !RECOMMENDED!" ); 79 continue;
27 if( v < 1.f ) 80 add_record( log(v), n, nsign, m, m ? use_2pm_2pn : use_1_2pn );
28 printf( "%0+25.21lf %08X x-= ( x %s %2d ) %s ( x %s %2d )\n", log(1.0f - v), abs((int)(67108864.f*log(1.0f - v))), m>=0?"<<":">>", (int)abs(m), nsign==-1?"-":"+", n>=0?"<<":">>", (int)abs(n) ); 81 if( v < 1.0 )
29 } 82 add_record( log(1.0 - v), n, nsign, m, use_1_2pm_2pn );
30 } 83 }
31 } 84 }
32 85
86 qsort( g_tr, g_records, sizeof(*g_tr), compare_record );
87
88 for (n=0; n<g_records; ++n )
89 dump_record(n);
90
33 return 0; 91 return 0;
34} 92}