DUDS
Distributed Update of Data from Something
ExtendedUnit.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the DUDS project. It is subject to the BSD-style
3  * license terms in the LICENSE file found in the top-level directory of this
4  * distribution and at https://github.com/jjackowski/duds/blob/master/LICENSE.
5  * No part of DUDS, including this file, may be copied, modified, propagated,
6  * or distributed except according to the terms contained in the LICENSE file.
7  *
8  * Copyright (C) 2017 Jeff Jackowski
9  */
11 
12 namespace duds { namespace data {
13 
14 void ExtendedUnit::offset(float o) {
15  const std::uint32_t &oi = *(std::uint32_t*)(&o);
16  int temp = ((oi >> 23) & 0xFF) - 127;
17  if ((temp > 62) || (temp < -63)) {
19  }
20  exp = temp;
21  sign = oi >= (1 << 31);
22  mant = (oi & 0x7FFFFF) >> 3;
23 }
24 
25 float ExtendedUnit::offsetf() const {
26  std::uint32_t oo = (mant << 3) | ((exp + 127) << 23) | (sign << 31);
27  return *((float*)(&oo));
28 }
29 
30 void ExtendedUnit::offset(double o) {
31  const std::uint64_t &oi = *(std::uint64_t*)(&o);
32  int temp = ((oi >> 52) & 0x7FF) - 1023;
33  if ((temp > 62) || (temp < -63)) {
35  }
36  exp = temp;
37  sign = oi >= ((std::uint64_t)1 << 63);
38  mant = (oi & (std::uint64_t)0xFFFFFFFFFFFFF) >> 32;
39 }
40 
41 double ExtendedUnit::offset() const {
42  std::uint64_t oo = ((std::uint64_t)mant << 32) |
43  ((std::uint64_t)(exp + 1023) << 52) | ((std::uint64_t)sign << 63);
44  return *((double*)(&oo));
45 }
46 
48  // return the result; may fail with exception
49  return ExtendedUnit(unit * U, scaloff);
50 }
51 
53  // return the result; may fail with exception
54  return ExtendedUnit(unit / U, scaloff);
55 }
56 
58  // a temporary
59  Unit n(unit * U); // may throw
60  // keep the new value
61  unit = n.value();
62  return *this;
63 }
64 
66  // a temporary
67  Unit n(unit / U); // may throw
68  // keep the new value
69  unit = n.value();
70  return *this;
71 }
72 
74  unit = U;
75  scaloff = 0;
76  return *this;
77 }
78 
79 } }
80 
Indicates that a value is beyond the range allowed by the Unit or ExtendedUnit class.
Definition: Unit.hpp:35
ExtendedUnit()=default
Makes an uninitialized ExtendedUnit.
ExtendedUnit & operator*=(const Unit &U)
Combines two units into a new unit.
int sign
The sign flag.
ExtendedUnit & operator=(const Unit &U)
Makes this extended unit equivalent to the given Unit object.
const ExtendedUnit operator*(const Unit &U) const
Combines two units into a new unit.
std::int32_t value() const
Returns the internal exponent fields.
Definition: Unit.hpp:200
unsigned int mant
The mantissa, save for the impled leading bit.
const ExtendedUnit operator/(const Unit &U) const
Combines two units into a new unit.
Represents an SI unit, either base or derived.
Definition: Unit.hpp:178
ExtendedUnit & operator/=(const Unit &U)
Combines two units into a new unit.
boost::error_info< struct Info_unitexp, int > BadUnitExponent
The out-of-range exponent.
Definition: Unit.hpp:40
redo: Use floats for scale and offset.
float offsetf() const
Query the offset as a float.
int exp
The exponent as a signed integer rather than unsigned with bias.
double offset() const
Query the offset as a double.
#define DUDS_THROW_EXCEPTION(x)
Works like BOOST_THROW_EXCEPTION, but includes a stack trace if DUDS_ERRORS_VERBOSE is defined...
Definition: Errors.hpp:48
std::uint32_t scaloff
A scaling and offset value packed into four bytes.