summaryrefslogtreecommitdiff
path: root/src/palrt/convert.h
blob: 8e3a9d490370196ef5967733be88bca3f18185b2 (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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//

//
// ===========================================================================
// File: convert.h
// 
// ===========================================================================

/***
*Purpose:
*  Common header (shared by convert.cpp and decimal.cpp) for numeric
*  conversions and other math stuff.
*
*Revision History:
*
*  
*
*Implementation Notes:
*
*****************************************************************************/

#ifndef _CONVERT_H_ /* { */
#define _CONVERT_H_

//***********************************************************************
//
// Structures
//

typedef union{
    struct {
#if BIGENDIAN
      ULONG sign:1;
      ULONG exp:11;
      ULONG mantHi:20;
      ULONG mantLo;
#else // BIGENDIAN
      ULONG mantLo;
      ULONG mantHi:20;
      ULONG exp:11;
      ULONG sign:1;
#endif
    } u;
    double dbl;
} DBLSTRUCT;

// Intializer for a DBLSTRUCT
#if BIGENDIAN
#define DEFDS(Lo, Hi, exp, sign) { {sign, exp, Hi, Lo } }
#else
#define DEFDS(Lo, Hi, exp, sign) { {Lo, Hi, exp, sign} }
#endif


typedef struct {
#if BIGENDIAN
    ULONG sign:1;
    ULONG exp:8;
    ULONG mant:23;
#else
    ULONG mant:23;
    ULONG exp:8;
    ULONG sign:1;
#endif
} SNGSTRUCT;



typedef union {
    DWORDLONG int64;
    struct {
#ifdef BIGENDIAN
        ULONG Hi;
        ULONG Lo;
#else
        ULONG Lo;
        ULONG Hi;
#endif
    } u;
} SPLIT64;



//***********************************************************************
//
// Constants
//

static const ULONG ulTenToTenDiv4 = 2500000000U;
static const ULONG ulTenToNine    = 1000000000U;

//***********************************************************************
//
// Inlines for Decimal
//


#ifndef UInt32x32To64
#define UInt32x32To64(a, b) ((DWORDLONG)((DWORD)(a)) * (DWORDLONG)((DWORD)(b)))
#endif

#define Div64by32(num, den) ((ULONG)((DWORDLONG)(num) / (ULONG)(den)))
#define Mod64by32(num, den) ((ULONG)((DWORDLONG)(num) % (ULONG)(den)))

inline DWORDLONG DivMod32by32(ULONG num, ULONG den)
{
    SPLIT64  sdl;

    sdl.u.Lo = num / den;
    sdl.u.Hi = num % den;
    return sdl.int64;
}

inline DWORDLONG DivMod64by32(DWORDLONG num, ULONG den)
{
    SPLIT64  sdl;

    sdl.u.Lo = Div64by32(num, den);
    sdl.u.Hi = Mod64by32(num, den);
    return sdl.int64;
}

#endif /* } _CONVERT_H_ */