~ubuntu-branches/ubuntu/natty/lightning-extension/natty-security

« back to all changes in this revision

Viewing changes to mozilla/mfbt/FloatingPoint.h

  • Committer: Package Import Robot
  • Author(s): Chris Coulson, Chris Coulson, Ben Collins
  • Date: 2012-07-16 14:19:14 UTC
  • mfrom: (1.1.21)
  • Revision ID: package-import@ubuntu.com-20120716141914-mynrjxhu00lsv01h
Tags: 1.6+build1-0ubuntu0.11.04.2
* New upstream stable release (CALENDAR_1_6_BUILD1) (LP: #1024564)

[ Chris Coulson <chris.coulson@canonical.com> ]
* Fix LP: #995054 - Ensure the /usr/lib/thunderbird/extensions symlink
  exists on upgrade, which may not be the case when upgrading from a really
  old lightning version
  - add debian/lightning-extension.postinst

[ Ben Collins <bcollins@ubuntu.com> ]
* Fix LP: #1025387 - FTBFS: powerpc build fails
  - add debian/patches/fix-dtoa-build-on-ppc.patch
  - update debian/patches/series

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
2
/* This Source Code Form is subject to the terms of the Mozilla Public
 
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 
4
 * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
5
 
 
6
/* Various predicates and operations on IEEE-754 floating point types. */
 
7
 
 
8
#ifndef mozilla_FloatingPoint_h_
 
9
#define mozilla_FloatingPoint_h_
 
10
 
 
11
#include "mozilla/Assertions.h"
 
12
#include "mozilla/Attributes.h"
 
13
#include "mozilla/StandardInteger.h"
 
14
 
 
15
/*
 
16
 * It's reasonable to ask why we have this header at all.  Don't isnan,
 
17
 * copysign, the built-in comparison operators, and the like solve these
 
18
 * problems?  Unfortunately, they don't.  We've found that various compilers
 
19
 * (MSVC, MSVC when compiling with PGO, and GCC on OS X, at least) miscompile
 
20
 * the standard methods in various situations, so we can't use them.  Some of
 
21
 * these compilers even have problems compiling seemingly reasonable bitwise
 
22
 * algorithms!  But with some care we've found algorithms that seem to not
 
23
 * trigger those compiler bugs.
 
24
 *
 
25
 * For the aforementioned reasons, be very wary of making changes to any of
 
26
 * these algorithms.  If you must make changes, keep a careful eye out for
 
27
 * compiler bustage, particularly PGO-specific bustage.
 
28
 *
 
29
 * Some users require that this file be C-compatible.  Unfortunately, this means
 
30
 * no mozilla namespace to contain everything, no detail namespace clarifying
 
31
 * MozDoublePun to be an internal data structure, and so on.
 
32
 */
 
33
 
 
34
/*
 
35
 * These implementations all assume |double| is a 64-bit double format number
 
36
 * type, compatible with the IEEE-754 standard.  C/C++ don't require this to be
 
37
 * the case.  But we required this in implementations of these algorithms that
 
38
 * preceded this header, so we shouldn't break anything if we continue doing so.
 
39
 */
 
40
MOZ_STATIC_ASSERT(sizeof(double) == sizeof(uint64_t), "double must be 64 bits");
 
41
 
 
42
/*
 
43
 * Constant expressions in C can't refer to consts, unfortunately, so #define
 
44
 * these rather than use |const uint64_t|.
 
45
 */
 
46
#define MOZ_DOUBLE_SIGN_BIT          0x8000000000000000ULL
 
47
#define MOZ_DOUBLE_EXPONENT_BITS     0x7ff0000000000000ULL
 
48
#define MOZ_DOUBLE_SIGNIFICAND_BITS  0x000fffffffffffffULL
 
49
 
 
50
#define MOZ_DOUBLE_EXPONENT_BIAS   1023
 
51
#define MOZ_DOUBLE_EXPONENT_SHIFT  52
 
52
 
 
53
MOZ_STATIC_ASSERT((MOZ_DOUBLE_SIGN_BIT & MOZ_DOUBLE_EXPONENT_BITS) == 0,
 
54
                  "sign bit doesn't overlap exponent bits");
 
55
MOZ_STATIC_ASSERT((MOZ_DOUBLE_SIGN_BIT & MOZ_DOUBLE_SIGNIFICAND_BITS) == 0,
 
56
                  "sign bit doesn't overlap significand bits");
 
57
MOZ_STATIC_ASSERT((MOZ_DOUBLE_EXPONENT_BITS & MOZ_DOUBLE_SIGNIFICAND_BITS) == 0,
 
58
                  "exponent bits don't overlap significand bits");
 
59
 
 
60
MOZ_STATIC_ASSERT((MOZ_DOUBLE_SIGN_BIT | MOZ_DOUBLE_EXPONENT_BITS | MOZ_DOUBLE_SIGNIFICAND_BITS)
 
61
                  == ~(uint64_t)0,
 
62
                  "all bits accounted for");
 
63
 
 
64
#ifdef __cplusplus
 
65
extern "C" {
 
66
#endif
 
67
 
 
68
/*
 
69
 * This union is NOT a public data structure, and it is not to be used outside
 
70
 * this file!
 
71
 */
 
72
union MozDoublePun {
 
73
    /*
 
74
     * Every way to pun the bits of a double introduces an additional layer of
 
75
     * complexity, across a multitude of platforms, architectures, and ABIs.
 
76
     * Use *only* uint64_t to reduce complexity.  Don't add new punning here
 
77
     * without discussion!
 
78
     */
 
79
    uint64_t u;
 
80
    double d;
 
81
};
 
82
 
 
83
/** Determines whether a double is NaN. */
 
84
static MOZ_ALWAYS_INLINE int
 
85
MOZ_DOUBLE_IS_NaN(double d)
 
86
{
 
87
  union MozDoublePun pun;
 
88
  pun.d = d;
 
89
 
 
90
  /*
 
91
   * A double is NaN if all exponent bits are 1 and the significand contains at
 
92
   * least one non-zero bit.
 
93
   */
 
94
  return (pun.u & MOZ_DOUBLE_EXPONENT_BITS) == MOZ_DOUBLE_EXPONENT_BITS &&
 
95
         (pun.u & MOZ_DOUBLE_SIGNIFICAND_BITS) != 0;
 
96
}
 
97
 
 
98
/** Determines whether a double is +Infinity or -Infinity. */
 
99
static MOZ_ALWAYS_INLINE int
 
100
MOZ_DOUBLE_IS_INFINITE(double d)
 
101
{
 
102
  union MozDoublePun pun;
 
103
  pun.d = d;
 
104
 
 
105
  /* Infinities have all exponent bits set to 1 and an all-0 significand. */
 
106
  return (pun.u & ~MOZ_DOUBLE_SIGN_BIT) == MOZ_DOUBLE_EXPONENT_BITS;
 
107
}
 
108
 
 
109
/** Determines whether a double is not NaN or infinite. */
 
110
static MOZ_ALWAYS_INLINE int
 
111
MOZ_DOUBLE_IS_FINITE(double d)
 
112
{
 
113
  union MozDoublePun pun;
 
114
  pun.d = d;
 
115
 
 
116
  /*
 
117
   * NaN and Infinities are the only non-finite doubles, and both have all
 
118
   * exponent bits set to 1.
 
119
   */
 
120
  return (pun.u & MOZ_DOUBLE_EXPONENT_BITS) != MOZ_DOUBLE_EXPONENT_BITS;
 
121
}
 
122
 
 
123
/**
 
124
 * Determines whether a double is negative.  It is an error to call this method
 
125
 * on a double which is NaN.
 
126
 */
 
127
static MOZ_ALWAYS_INLINE int
 
128
MOZ_DOUBLE_IS_NEGATIVE(double d)
 
129
{
 
130
  union MozDoublePun pun;
 
131
  pun.d = d;
 
132
 
 
133
  MOZ_ASSERT(!MOZ_DOUBLE_IS_NaN(d), "NaN does not have a sign");
 
134
 
 
135
  /* The sign bit is set if the double is negative. */
 
136
  return (pun.u & MOZ_DOUBLE_SIGN_BIT) != 0;
 
137
}
 
138
 
 
139
/** Determines whether a double represents -0. */
 
140
static MOZ_ALWAYS_INLINE int
 
141
MOZ_DOUBLE_IS_NEGATIVE_ZERO(double d)
 
142
{
 
143
  union MozDoublePun pun;
 
144
  pun.d = d;
 
145
 
 
146
  /* Only the sign bit is set if the double is -0. */
 
147
  return pun.u == MOZ_DOUBLE_SIGN_BIT;
 
148
}
 
149
 
 
150
/** Returns the exponent portion of the double. */
 
151
static MOZ_ALWAYS_INLINE int_fast16_t
 
152
MOZ_DOUBLE_EXPONENT(double d)
 
153
{
 
154
  union MozDoublePun pun;
 
155
  pun.d = d;
 
156
 
 
157
  /*
 
158
   * The exponent component of a double is an unsigned number, biased from its
 
159
   * actual value.  Subtract the bias to retrieve the actual exponent.
 
160
   */
 
161
  return (int_fast16_t)((pun.u & MOZ_DOUBLE_EXPONENT_BITS) >> MOZ_DOUBLE_EXPONENT_SHIFT) -
 
162
                        MOZ_DOUBLE_EXPONENT_BIAS;
 
163
}
 
164
 
 
165
/** Returns +Infinity. */
 
166
static MOZ_ALWAYS_INLINE double
 
167
MOZ_DOUBLE_POSITIVE_INFINITY()
 
168
{
 
169
  union MozDoublePun pun;
 
170
 
 
171
  /*
 
172
   * Positive infinity has all exponent bits set, sign bit set to 0, and no
 
173
   * significand.
 
174
   */
 
175
  pun.u = MOZ_DOUBLE_EXPONENT_BITS;
 
176
  return pun.d;
 
177
}
 
178
 
 
179
/** Returns -Infinity. */
 
180
static MOZ_ALWAYS_INLINE double
 
181
MOZ_DOUBLE_NEGATIVE_INFINITY()
 
182
{
 
183
  union MozDoublePun pun;
 
184
 
 
185
  /*
 
186
   * Negative infinity has all exponent bits set, sign bit set to 1, and no
 
187
   * significand.
 
188
   */
 
189
  pun.u = MOZ_DOUBLE_SIGN_BIT | MOZ_DOUBLE_EXPONENT_BITS;
 
190
  return pun.d;
 
191
}
 
192
 
 
193
/** Constructs a NaN value with the specified sign bit and significand bits. */
 
194
static MOZ_ALWAYS_INLINE double
 
195
MOZ_DOUBLE_SPECIFIC_NaN(int signbit, uint64_t significand)
 
196
{
 
197
  union MozDoublePun pun;
 
198
 
 
199
  MOZ_ASSERT(signbit == 0 || signbit == 1);
 
200
  MOZ_ASSERT((significand & ~MOZ_DOUBLE_SIGNIFICAND_BITS) == 0);
 
201
  MOZ_ASSERT(significand & MOZ_DOUBLE_SIGNIFICAND_BITS);
 
202
 
 
203
  pun.u = (signbit ? MOZ_DOUBLE_SIGN_BIT : 0) |
 
204
          MOZ_DOUBLE_EXPONENT_BITS |
 
205
          significand;
 
206
  MOZ_ASSERT(MOZ_DOUBLE_IS_NaN(pun.d));
 
207
  return pun.d;
 
208
}
 
209
 
 
210
/**
 
211
 * Computes a NaN value.  Do not use this method if you depend upon a particular
 
212
 * NaN value being returned.
 
213
 */
 
214
static MOZ_ALWAYS_INLINE double
 
215
MOZ_DOUBLE_NaN()
 
216
{
 
217
  return MOZ_DOUBLE_SPECIFIC_NaN(0, 0xfffffffffffffULL);
 
218
}
 
219
 
 
220
/** Computes the smallest non-zero positive double value. */
 
221
static MOZ_ALWAYS_INLINE double
 
222
MOZ_DOUBLE_MIN_VALUE()
 
223
{
 
224
  union MozDoublePun pun;
 
225
  pun.u = 1;
 
226
  return pun.d;
 
227
}
 
228
 
 
229
/** Computes a 32-bit hash of the given double. */
 
230
static MOZ_ALWAYS_INLINE uint32_t
 
231
MOZ_HASH_DOUBLE(double d)
 
232
{
 
233
  union MozDoublePun pun;
 
234
  pun.d = d;
 
235
 
 
236
  return ((uint32_t)(pun.u >> 32)) ^ ((uint32_t)(pun.u));
 
237
}
 
238
 
 
239
static MOZ_ALWAYS_INLINE int
 
240
MOZ_DOUBLE_IS_INT32(double d, int32_t* i)
 
241
{
 
242
  /*
 
243
   * XXX Casting a double that doesn't truncate to int32_t, to int32_t, induces
 
244
   *     undefined behavior.  We should definitely fix this (bug 744965), but as
 
245
   *     apparently it "works" in practice, it's not a pressing concern now.
 
246
   */
 
247
  return !MOZ_DOUBLE_IS_NEGATIVE_ZERO(d) && d == (*i = (int32_t)d);
 
248
}
 
249
 
 
250
#ifdef __cplusplus
 
251
} /* extern "C" */
 
252
#endif
 
253
 
 
254
#endif  /* mozilla_FloatingPoint_h_ */