summaryrefslogtreecommitdiff
path: root/tools/thirdparty/OpenFst/fst/lib/float-weight.h
blob: bec59c4a9d9413130c8c3d45bb4625a17bb8e3d0 (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// float-weight.h
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
// \file
// Float weight set and associated semiring operation definitions.
//

#ifndef FST_LIB_FLOAT_WEIGHT_H__
#define FST_LIB_FLOAT_WEIGHT_H__

#include <limits>

#include "fst/lib/weight.h"

namespace fst {

static const float kPosInfinity = numeric_limits<float>::infinity();
static const float kNegInfinity = -kPosInfinity;

// Single precision floating point weight base class
class FloatWeight {
 public:
  FloatWeight() {}

  FloatWeight(float f) : value_(f) {}

  FloatWeight(const FloatWeight &w) : value_(w.value_) {}

  FloatWeight &operator=(const FloatWeight &w) {
    value_ = w.value_;
    return *this;
  }

  istream &Read(istream &strm) {
    return ReadType(strm, &value_);
  }

  ostream &Write(ostream &strm) const {
    return WriteType(strm, value_);
  }

  ssize_t Hash() const {
    union {
      float f;
      ssize_t s;
    } u = { value_ };
    return u.s;
  }

  const float &Value() const { return value_; }

 protected:
  float value_;
};

inline bool operator==(const FloatWeight &w1, const FloatWeight &w2) {
  // Volatile qualifier thwarts over-aggressive compiler optimizations
  // that lead to problems esp. with NaturalLess().
  volatile float v1 = w1.Value();
  volatile float v2 = w2.Value();
  return v1 == v2;
}

inline bool operator!=(const FloatWeight &w1, const FloatWeight &w2) {
  return !(w1 == w2);
}

inline bool ApproxEqual(const FloatWeight &w1, const FloatWeight &w2,
                        float delta = kDelta) {
  return w1.Value() <= w2.Value() + delta && w2.Value() <= w1.Value() + delta;
}

inline ostream &operator<<(ostream &strm, const FloatWeight &w) {
  if (w.Value() == kPosInfinity)
    return strm << "Infinity";
  else if (w.Value() == kNegInfinity)
    return strm << "-Infinity";
  else if (w.Value() != w.Value())   // Fails for NaN
    return strm << "BadFloat";
  else
    return strm << w.Value();
}

inline istream &operator>>(istream &strm, FloatWeight &w) {
  string s;
  strm >> s;
  if (s == "Infinity") {
    w = FloatWeight(kPosInfinity);
  } else if (s == "-Infinity") {
    w = FloatWeight(kNegInfinity);
  } else {
    char *p;
    float f = strtod(s.c_str(), &p);
    if (p < s.c_str() + s.size())
      strm.clear(std::ios::badbit);
    else
      w = FloatWeight(f);
  }
  return strm;
}


// Tropical semiring: (min, +, inf, 0)
class TropicalWeight : public FloatWeight {
 public:
  typedef TropicalWeight ReverseWeight;

  TropicalWeight() : FloatWeight() {}

  TropicalWeight(float f) : FloatWeight(f) {}

  TropicalWeight(const TropicalWeight &w) : FloatWeight(w) {}

  static const TropicalWeight Zero() { return TropicalWeight(kPosInfinity); }

  static const TropicalWeight One() { return TropicalWeight(0.0F); }

  static const string &Type() {
    static const string type = "tropical";
    return type;
  }

  bool Member() const {
    // First part fails for IEEE NaN
    return Value() == Value() && Value() != kNegInfinity;
  }

  TropicalWeight Quantize(float delta = kDelta) const {
    return TropicalWeight(floor(Value()/delta + 0.5F) * delta);
  }

  TropicalWeight Reverse() const { return *this; }

  static uint64 Properties() {
    return kLeftSemiring | kRightSemiring | kCommutative |
      kPath | kIdempotent;
  }
};

inline TropicalWeight Plus(const TropicalWeight &w1,
                           const TropicalWeight &w2) {
  return w1.Value() < w2.Value() ? w1 : w2;
}

inline TropicalWeight Times(const TropicalWeight &w1,
                            const TropicalWeight &w2) {
  float f1 = w1.Value(), f2 = w2.Value();
  if (f1 == kPosInfinity)
    return w1;
  else if (f2 == kPosInfinity)
    return w2;
  else
    return TropicalWeight(f1 + f2);
}

inline TropicalWeight Divide(const TropicalWeight &w1,
                             const TropicalWeight &w2,
                             DivideType typ = DIVIDE_ANY) {
  float f1 = w1.Value(), f2 = w2.Value();
  if (f2 == kPosInfinity)
    return kNegInfinity;
  else if (f1 == kPosInfinity)
    return kPosInfinity;
  else
    return TropicalWeight(f1 - f2);
}


// Log semiring: (log(e^-x + e^y), +, inf, 0)
class LogWeight : public FloatWeight {
 public:
  typedef LogWeight ReverseWeight;

  LogWeight() : FloatWeight() {}

  LogWeight(float f) : FloatWeight(f) {}

  LogWeight(const LogWeight &w) : FloatWeight(w) {}

  static const LogWeight Zero() {   return LogWeight(kPosInfinity); }

  static const LogWeight One() { return LogWeight(0.0F); }

  static const string &Type() {
    static const string type = "log";
    return type;
  }

  bool Member() const {
    // First part fails for IEEE NaN
    return Value() == Value() && Value() != kNegInfinity;
  }

  LogWeight Quantize(float delta = kDelta) const {
    return LogWeight(floor(Value()/delta + 0.5F) * delta);
  }

  LogWeight Reverse() const { return *this; }

  static uint64 Properties() {
    return kLeftSemiring | kRightSemiring | kCommutative;
  }
};

inline double LogExp(double x) { return log(1.0F + exp(-x)); }

inline LogWeight Plus(const LogWeight &w1, const LogWeight &w2) {
  float f1 = w1.Value(), f2 = w2.Value();
  if (f1 == kPosInfinity)
    return w2;
  else if (f2 == kPosInfinity)
    return w1;
  else if (f1 > f2)
    return LogWeight(f2 - LogExp(f1 - f2));
  else
    return LogWeight(f1 - LogExp(f2 - f1));
}

inline LogWeight Times(const LogWeight &w1, const LogWeight &w2) {
  float f1 = w1.Value(), f2 = w2.Value();
  if (f1 == kPosInfinity)
    return w1;
  else if (f2 == kPosInfinity)
    return w2;
  else
    return LogWeight(f1 + f2);
}

inline LogWeight Divide(const LogWeight &w1,
                             const LogWeight &w2,
                             DivideType typ = DIVIDE_ANY) {
  float f1 = w1.Value(), f2 = w2.Value();
  if (f2 == kPosInfinity)
    return kNegInfinity;
  else if (f1 == kPosInfinity)
    return kPosInfinity;
  else
    return LogWeight(f1 - f2);
}

}  // namespace fst;

#endif  // FST_LIB_FLOAT_WEIGHT_H__