OpenCores
URL https://opencores.org/ocsvn/fp_log/fp_log/trunk

Subversion Repositories fp_log

[/] [fp_log/] [trunk/] [DP-ICSILog/] [DP-ICSILog.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 NikosAl
/*******************************************************************************************
2
 *
3
 * Double-Precision ICSILog algorithm implementation
4
 * written by Nikolaos Alchiotis and Alexandros Stamatakis
5
 *
6
 * The Exelixis Lab
7
 * Bioinformatics Unit (I12)
8
 * Department of Computer Science
9
 * Technical University of Munich
10
 *
11
 * Emails: alachiot@cs.tum.edu, stamatak@cs.tum.edu
12
 * WWW:   http://wwwkramer.in.tum.de/exelixis/
13
 *
14
 * This code is made available under GNU GPL
15
 *
16
 *
17
*******************************************************************************************/
18
 
19
#include <stdio.h>
20
#include <stdlib.h>
21
#include <math.h>
22
 
23
/********************************************/
24
// Union to Access the bits
25
 
26
typedef union
27
{
28
  double value;
29
 
30
  struct
31
  {
32
    unsigned int rght_p;
33
    unsigned int lft_p;
34
  }
35
    parts;
36
}
37
  ieee_double_shape_type;
38
 
39
 
40
/********************************************/
41
 
42
//Constant Value
43
double con_val;
44
 
45
// Variables used for error checking
46
double value_minus_inf,
47
       value_nan,
48
       value_inf,
49
       value_inf_inf;
50
 
51
// Prec shows how many MSBs of the mantissa field will be used to index the mantissa LUT
52
unsigned int prec = 12;
53
 
54
// Lut_entries is the number of entries of the mantissa LUT
55
int lut_entries;//=pow(2,prec);
56
 
57
//ShiftBits is used by the main function to perform the shifting operation
58
int shiftBits;//= 12 + 20 - prec;
59
 
60
 
61
/********************************************/
62
 
63
// Function to initialize the mantissa LUT . It is the same one that the official ICSILog uses.
64
 
65
static void DP_ICSILog_INIT(const unsigned precision, double* const pTable)
66
{
67
  /*
68
     step along table elements and x-axis positions
69
     (start with extra half increment, so the steps intersect at their midpoints.)
70
  */
71
 
72
  con_val = log(2);
73
  value_minus_inf = -1.0 / 0.0;
74
  value_nan = 0.0 / 0.0;
75
  value_inf = pow(10, 308);
76
  value_inf_inf = value_inf + value_inf;
77
 
78
 
79
  double oneToTwo = 1.0f + (1.0f / (double)( 1 << (precision + 1)));
80
  int i;
81
 
82
  for(i = 0;  i < (1 << precision);  ++i )
83
    {
84
      // make y-axis value for table element
85
      pTable[i] = logf(oneToTwo);
86
 
87
      oneToTwo += 1.0f / (double)(1 << precision);
88
    }
89
}
90
 
91
/********************************************/
92
// The DP_myICSILog main function.
93
 
94
static inline double DP_myICSILog(const double input , const int num_of_bits, register double * const t)
95
{
96
  register double result;
97
 
98
  if(input < 0.0)
99
    result = value_nan;
100
  else
101
    {
102
      if(input == 0.0)
103
        result = value_minus_inf;
104
      else
105
        {
106
          if(input > value_inf)
107
            result = value_inf_inf;
108
          else
109
            {
110
              ieee_double_shape_type model_input;
111
              model_input.value = input;
112
 
113
              register unsigned int left = model_input.parts.lft_p;
114
 
115
              register double m1 = ((left << 1) >> 21) - 1023.0;
116
              register int index = (left << 12) >> num_of_bits;
117
 
118
              result = (con_val * m1  + t[index]);
119
            }
120
        }
121
    }
122
  return result;
123
}
124
 
125
 
126
int main()
127
{
128
 
129
  double
130
    value = 123456789,
131
    *DP_myICSILog_LUT,
132
    result;
133
 
134
  /*********************************************************************/
135
  // How to Initialize the function
136
  prec = 12;
137
 
138
  lut_entries = pow(2,prec);
139
 
140
  shiftBits = 12 + 20 - prec;
141
 
142
  DP_myICSILog_LUT = (double*)malloc(sizeof(double) * lut_entries);
143
 
144
  DP_ICSILog_INIT(prec, DP_myICSILog_LUT);
145
 
146
  /********************************************************************/
147
 
148
 
149
  /********************************************************************/
150
 
151
  // How to Call the function
152
  result = DP_myICSILog(value, shiftBits, DP_myICSILog_LUT);
153
 
154
  printf("\n\nDP_myICSILog(%f) = %f \n",value, result);
155
  printf("\nLog(%f) = %f \n\n\n", value, log(value));
156
  return 0;
157
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.