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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
/*
** Copyright (C) 2018 Martin Brain
**
** See the file LICENSE for licensing information.
*/
/*
** compare.h
**
** Martin Brain
** martin.brain@cs.ox.ac.uk
** 25/08/14
**
** Comparison between floating-point numbers
**
*/
#include "symfpu/core/unpackedFloat.h"
#include "symfpu/core/ite.h"
#ifndef SYMFPU_COMPARE
#define SYMFPU_COMPARE
namespace symfpu {
// SMT-LIB equality
template <class t>
typename t::prop smtlibEqual (const typename t::fpt &format,
const unpackedFloat<t> &left,
const unpackedFloat<t> &right) {
typedef typename t::prop prop;
PRECONDITION(left.valid(format));
PRECONDITION(right.valid(format));
// Relies on a number of properties of the unpacked format
// particularly the use of default exponents, significands and signs
prop flagsEqual((left.getNaN() == right.getNaN()) &&
(left.getInf() == right.getInf()) &&
(left.getZero() == right.getZero()) &&
(left.getSign() == right.getSign()));
prop flagsAndExponent(flagsEqual && left.getExponent() == right.getExponent());
// Avoid comparing (and thus instantiating) the significand unless necessary
probabilityAnnotation<t,prop>(flagsAndExponent, UNLIKELY);
prop res(ITE(flagsAndExponent,
left.getSignificand() == right.getSignificand(),
prop(false)));
return res;
}
// IEEE-754 Equality (not actually an equivalence relation but ...)
template <class t>
typename t::prop ieee754Equal (const typename t::fpt &format,
const unpackedFloat<t> &left,
const unpackedFloat<t> &right) {
typedef typename t::prop prop;
PRECONDITION(left.valid(format));
PRECONDITION(right.valid(format));
prop neitherNan(!left.getNaN() && !right.getNaN()); // All comparison with NaN are false
prop bothZero(left.getZero() && right.getZero()); // Both zeros are equal
prop neitherZero(!left.getZero() && !right.getZero());
prop flagsAndExponent(neitherNan &&
(bothZero || (neitherZero &&
(left.getInf() == right.getInf() &&
left.getSign() == right.getSign() &&
left.getExponent() == right.getExponent()))));
// Avoid comparing (and thus instantiating) the significand unless necessary
probabilityAnnotation<t,prop>(flagsAndExponent, UNLIKELY);
prop res(ITE(flagsAndExponent, left.getSignificand() == right.getSignificand(), prop(false)));
return res;
}
// Share the common comparison code between functions
// equality == true if the equal case returns true
// IEEE-754 semantics for ordering with NaN
// (i.e. unordered with everything, not even equal to itself)
template <class t>
typename t::prop ordering (const typename t::fpt &format,
const unpackedFloat<t> &left,
const unpackedFloat<t> &right,
const typename t::prop equality) {
typedef typename t::prop prop;
PRECONDITION(left.valid(format));
PRECONDITION(right.valid(format));
// All comparison with NaN are false
prop neitherNaN(!left.getNaN() && !right.getNaN());
// Either is an infinity (wrong in the case of NaN but will be corrected)
prop infCase( (left.isNegativeInf() && ITE(equality, prop(true), !right.isNegativeInf()) ) ||
(right.isPositiveInf() && ITE(equality, prop(true), !left.isPositiveInf()) ) ||
(ITE(equality, left.getInf() && right.getInf() && left.getSign() == right.getSign(), prop(false))) );
// Either is a zero (wrong in the case of NaN but will be corrected)
prop zeroCase( ( left.getZero() && !right.getZero() && !right.getSign()) ||
(right.getZero() && !left.getZero() && left.getSign()) ||
(ITE(equality, left.getZero() && right.getZero(), prop(false))) );
// Normal and subnormal case
prop normalOrSubnormal(!left.getNaN() && !right.getNaN() &&
!left.getInf() && !right.getInf() &&
!left.getZero() && !right.getZero());
prop negativeLessThanPositive(normalOrSubnormal && left.getSign() && !right.getSign());
prop exponentNeeded(normalOrSubnormal && left.getSign() == right.getSign());
probabilityAnnotation<t>(exponentNeeded, UNLIKELY);
prop positiveCase(!left.getSign() && !right.getSign() &&
left.getExponent() < right.getExponent());
prop negativeCase( left.getSign() && right.getSign() &&
left.getExponent() > right.getExponent());
prop exponentEqual(left.getExponent() == right.getExponent());
prop significandNeeded(exponentNeeded && exponentEqual);
probabilityAnnotation<t>(significandNeeded, VERYUNLIKELY);
prop positiveExEqCase(!left.getSign() && !right.getSign() &&
left.getSignificand() < right.getSignificand());
prop negativeExEqCase( left.getSign() && right.getSign() &&
left.getSignificand() > right.getSignificand());
prop positiveExEqCaseEq(!left.getSign() && !right.getSign() &&
left.getSignificand() <= right.getSignificand());
prop negativeExEqCaseEq( left.getSign() && right.getSign() &&
left.getSignificand() >= right.getSignificand());
return ITE(!normalOrSubnormal,
neitherNaN && (infCase || zeroCase),
ITE(!exponentNeeded,
negativeLessThanPositive,
ITE(!significandNeeded,
positiveCase || negativeCase,
ITE(equality,
positiveExEqCaseEq || negativeExEqCaseEq,
positiveExEqCase || negativeExEqCase))));
}
template <class t>
typename t::prop lessThan (const typename t::fpt &format,
const unpackedFloat<t> &left,
const unpackedFloat<t> &right) {
PRECONDITION(left.valid(format));
PRECONDITION(right.valid(format));
typedef typename t::prop prop;
return ordering(format, left, right, prop(false));
}
template <class t>
typename t::prop lessThanOrEqual (const typename t::fpt &format,
const unpackedFloat<t> &left,
const unpackedFloat<t> &right) {
PRECONDITION(left.valid(format));
PRECONDITION(right.valid(format));
typedef typename t::prop prop;
return ordering(format, left, right, prop(true));
}
// Note that IEEE-754 says that max(+0,-0) = +/-0 and max(-0,+0) = +/- 0
template <class t>
unpackedFloat<t> max (const typename t::fpt &format,
const unpackedFloat<t> &left,
const unpackedFloat<t> &right,
const typename t::prop &zeroCase) {
return ITE(left.getNaN() || ordering(format, left, right, zeroCase),
right,
left);
}
// Note that IEEE-754 says that min(+0,-0) = +/-0 and min(-0,+0) = +/- 0
// this will always return the left one.
template <class t>
unpackedFloat<t> min (const typename t::fpt &format,
const unpackedFloat<t> &left,
const unpackedFloat<t> &right,
const typename t::prop &zeroCase) {
return ITE(right.getNaN() || ordering(format, left, right, zeroCase),
left,
right);
}
template <class t>
typename t::prop originalLessThan (const typename t::fpt &format,
const unpackedFloat<t> &left,
const unpackedFloat<t> &right) {
typedef typename t::prop prop;
PRECONDITION(left.valid(format));
PRECONDITION(right.valid(format));
// Optimisation : merge < and ==
// All comparison with NaN are false
prop neitherNan(!left.getNaN() && !right.getNaN());
// Infinities are bigger than everything but themself
prop eitherInf(left.getInf() || right.getInf());
prop infCase(( left.isNegativeInf() && !right.isNegativeInf()) ||
(!left.isPositiveInf() && right.isPositiveInf()));
// Both zero are equal
prop eitherZero(left.getZero() || right.getZero());
prop zeroCase(( left.getZero() && !right.getZero() && !right.getSign()) ||
(!left.getZero() && left.getSign() && right.getZero()));
// Normal and subnormal
prop negativeLessThanPositive(left.getSign() && !right.getSign()); // - < +
prop positiveCase(!left.getSign() && !right.getSign() &&
((left.getExponent() < right.getExponent()) ||
(left.getExponent() == right.getExponent() &&
left.getSignificand() < right.getSignificand())));
prop negativeCase(left.getSign() && right.getSign() &&
((left.getExponent() > right.getExponent()) ||
(left.getExponent() == right.getExponent() &&
left.getSignificand() > right.getSignificand())));
return neitherNan &&
ITE(eitherInf,
infCase,
ITE(eitherZero,
zeroCase,
negativeLessThanPositive || positiveCase || negativeCase));
}
// Optimised combination of the two
template <class t>
typename t::prop originalLessThanOrEqual (const typename t::fpt &format,
const unpackedFloat<t> &left,
const unpackedFloat<t> &right) {
typedef typename t::prop prop;
PRECONDITION(left.valid(format));
PRECONDITION(right.valid(format));
// Optimisation : merge < and ==
// All comparison with NaN are false
prop neitherNan(!left.getNaN() && !right.getNaN());
// Infinities are bigger than everything but themself
prop eitherInf(left.getInf() || right.getInf());
prop infCase( (left.getInf() && right.getInf() && left.getSign() == right.getSign()) ||
left.isNegativeInf() ||
right.isPositiveInf());
// Both zero are equal
prop eitherZero(left.getZero() || right.getZero());
prop zeroCase((left.getZero() && right.getZero()) ||
( left.getZero() && !right.getSign()) ||
( left.getSign() && right.getZero()));
// Normal and subnormal
prop negativeLessThanPositive(left.getSign() && !right.getSign()); // - < +
prop positiveCase(!left.getSign() && !right.getSign() &&
((left.getExponent() < right.getExponent()) ||
(left.getExponent() == right.getExponent() &&
left.getSignificand() <= right.getSignificand())));
prop negativeCase(left.getSign() && right.getSign() &&
((left.getExponent() > right.getExponent()) ||
(left.getExponent() == right.getExponent() &&
left.getSignificand() >= right.getSignificand())));
return neitherNan &&
ITE(eitherInf,
infCase,
ITE(eitherZero,
zeroCase,
negativeLessThanPositive || positiveCase || negativeCase));
}
}
#endif
|