-
Notifications
You must be signed in to change notification settings - Fork 58
/
ulp.h
37 lines (30 loc) · 809 Bytes
/
ulp.h
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
// ulp.h - units in the last place
// Copyright (c) 2006-2011 KALX, LLC. All rights reserved. No warranty is made.
#pragma once
#include <cstdint>
namespace fms {
template<class T>
struct ulp_traits { };
template<>
struct ulp_traits<float> {
typedef int32_t integer;
static const integer x80_ = 0x80000000L;
};
template<>
struct ulp_traits<double> {
typedef int64_t integer;
static const integer x80_ = 0x8000000000000000LL;
};
template<class T>
inline typename ulp_traits<T>::integer ulp(T x, T y)
{
union { T d; ulp_traits<T>::integer l; } ix, iy;
ix.d = x;
iy.d = y;
if (ix.l < 0)
ix.l = ulp_traits<T>::x80_ - ix.l;
if (iy.l < 0)
iy.l = ulp_traits<T>::x80_ - iy.l;
return ix.l - iy.l;
}
}