CMS 3D CMS Logo

liblogintpack.h
Go to the documentation of this file.
1 #ifndef liblogintpack_h
2 #define liblogintpack_h
3 
4 #include <cmath>
5 #include <cstdint>
6 
7 namespace logintpack
8 {
10  // note that abs(unpack(smallestNegative)) == unpack(1), i.e. there
11  // is no "x" such that "unpack(x) == -unpack(0)"
13  inline int16_t pack16logCeil(double x,double lmin, double lmax, uint16_t base=32768)
14  {
15  if(base>32768) base=32768;
16  const double l = std::log(std::abs(x));
17  const double centered = (l-lmin)/(lmax-lmin)*base;
18  int16_t r=std::ceil(centered);
19  if(centered >= base-1) r=base-1;
20  if(centered < 0) r=0;
21  if(x<0) r = r==0 ? -1 : -r;
22  return r;
23  }
24 
25 
26  inline int16_t pack16log(double x,double lmin, double lmax, uint16_t base=32768)
27  {
28  if(base>32768) base=32768;
29  const float delta=(log(1.+exp((lmax-lmin)/base))-log(2.))*base/(lmax-lmin);
30  const double l = std::log(std::abs(x));
31  const double centered = (l-lmin)/(lmax-lmin)*base;
32  int16_t r=std::floor(centered);
33  if(centered-r>delta) r+=1;
34  if(centered >= base-1) r=base-1;
35  if(centered < 0) r=0;
36  if(x<0) r = r==0 ? -1 : -r;
37  return r;
38  }
39 
42  inline int16_t pack16logClosed(double x,double lmin, double lmax, uint16_t base=32768)
43  {
44  if(base>32768) base=32768;
45  const double l = std::log(std::abs(x));
46  const double centered = (l-lmin)/(lmax-lmin)*(base-1);
47  int16_t r=round(centered);
48  if(centered >= base-1) r=base-1;
49  if(centered < 0) r=0;
50  if(x<0) r = r==0 ? -1 : -r;
51  return r;
52  }
53 
54 
55  inline double unpack16log(int16_t i,double lmin, double lmax, uint16_t base=32768)
56  {
57  if(base>32768) base=32768;
58  const double basef=base;
59  const double l=lmin+std::abs(i)/basef*(lmax-lmin);
60  const double val=std::exp(l);
61  if(i<0) return -val; else return val;
62  }
63 
65  inline double unpack16logClosed(int16_t i,double lmin, double lmax, uint16_t base=32768)
66  {
67  if(base>32768) base=32768;
68  const double basef=base-1;
69  double l=lmin+std::abs(i)/basef*(lmax-lmin);
70  if (std::abs(i) == base-1) l = lmax;
71  const double val=std::exp(l);
72  if(i<0) return -val; else return val;
73  }
74 
75 
76 
77  inline
78  int8_t pack8logCeil(double x,double lmin, double lmax, uint8_t base=128)
79  {
80  if(base>128) base=128;
81  const double l = std::log(std::abs(x));
82  const double centered = (l-lmin)/(lmax-lmin)*base;
83  int8_t r=std::ceil(centered);
84  if(centered >= base-1) r=base-1;
85  if(centered < 0) r=0;
86  if(x<0) r = r==0 ? -1 : -r;
87  return r;
88  }
89 
90  inline
91  int8_t pack8log(double x,double lmin, double lmax, uint8_t base=128)
92  {
93  if(base>128) base=128;
94  const double l = std::log(std::abs(x));
95  const double centered = (l-lmin)/(lmax-lmin)*base;
96  int8_t r=centered;
97  if(centered >= base-1) r=base-1;
98  if(centered < 0) r=0;
99  if(x<0) r = r==0 ? -1 : -r;
100  return r;
101  }
102 
105  inline
106  int8_t pack8logClosed(double x,double lmin, double lmax, uint8_t base=128)
107  {
108  if(base>128) base=128;
109  const double l = std::log(std::abs(x));
110  const double centered = (l-lmin)/(lmax-lmin)*(base-1);
111  int8_t r=round(centered);
112  if(centered >= base-1) r=base-1;
113  if(centered < 0) r=0;
114  if(x<0) r = r==0 ? -1 : -r;
115  return r;
116  }
117 
118  inline
119  double unpack8log(int8_t i,double lmin, double lmax, uint8_t base=128)
120  {
121  if(base>128) base=128;
122  const double basef=base;
123  const double l=lmin+std::abs(i)/basef*(lmax-lmin);
124  const double val=std::exp(l);
125  if(i<0) return -val; else return val;
126  }
127 
129  inline
130  double unpack8logClosed(int8_t i,double lmin, double lmax, uint8_t base=128)
131  {
132  if(base>128) base=128;
133  const double basef=base-1;
134  double l=lmin+std::abs(i)/basef*(lmax-lmin);
135  if (std::abs(i) == base-1) l = lmax;
136  const double val=std::exp(l);
137  if(i<0) return -val; else return val;
138  }
139 
140 }
141 #endif
dbl * delta
Definition: mlp_gen.cc:36
double unpack8log(int8_t i, double lmin, double lmax, uint8_t base=128)
int8_t pack8log(double x, double lmin, double lmax, uint8_t base=128)
Definition: liblogintpack.h:91
double unpack16log(int16_t i, double lmin, double lmax, uint16_t base=32768)
Definition: liblogintpack.h:55
constexpr int8_t smallestNegative
Definition: liblogintpack.h:12
#define constexpr
int8_t pack8logCeil(double x, double lmin, double lmax, uint8_t base=128)
Definition: liblogintpack.h:78
double unpack16logClosed(int16_t i, double lmin, double lmax, uint16_t base=32768)
reverse of pack8logClosed
Definition: liblogintpack.h:65
int8_t pack8logClosed(double x, double lmin, double lmax, uint8_t base=128)
int16_t pack16logClosed(double x, double lmin, double lmax, uint16_t base=32768)
Definition: liblogintpack.h:42
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
base
Make Sure CMSSW is Setup ##.
constexpr int8_t smallestPositive
Definition: liblogintpack.h:9
double unpack8logClosed(int8_t i, double lmin, double lmax, uint8_t base=128)
reverse of pack8logClosed
int16_t pack16log(double x, double lmin, double lmax, uint16_t base=32768)
Definition: liblogintpack.h:26
int16_t pack16logCeil(double x, double lmin, double lmax, uint16_t base=32768)
Definition: liblogintpack.h:13