• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

realm / realm-core / github_pull_request_281750

30 Oct 2023 03:37PM UTC coverage: 90.528% (-1.0%) from 91.571%
github_pull_request_281750

Pull #6073

Evergreen

jedelbo
Log free space and history sizes when opening file
Pull Request #6073: Merge next-major

95488 of 175952 branches covered (0.0%)

8973 of 12277 new or added lines in 149 files covered. (73.09%)

622 existing lines in 51 files now uncovered.

233503 of 257934 relevant lines covered (90.53%)

6533720.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

63.53
/src/realm/decimal128.cpp
1
/*************************************************************************
2
 *
3
 * Copyright 2019 Realm Inc.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 **************************************************************************/
18
#include <realm/decimal128.hpp>
19

20
#include <realm/string_data.hpp>
21
#include <realm/util/to_string.hpp>
22

23
#include <external/IntelRDFPMathLib20U2/LIBRARY/src/bid_conf.h>
24
#include <external/IntelRDFPMathLib20U2/LIBRARY/src/bid_functions.h>
25
#include <cstring>
26
#include <stdexcept>
27

28
namespace realm {
29

30
namespace {
31

32
Decimal128 to_decimal128(const BID_UINT128& val)
33
{
6,012✔
34
    Decimal128 tmp;
6,012✔
35
    memcpy(tmp.raw(), &val, sizeof(BID_UINT128));
6,012✔
36
    return tmp;
6,012✔
37
}
6,012✔
38

39
BID_UINT128 to_BID_UINT128(const Decimal128& val)
40
{
4,846,464✔
41
    BID_UINT128 ret;
4,846,464✔
42
    memcpy(&ret, val.raw(), sizeof(BID_UINT128));
4,846,464✔
43
    return ret;
4,846,464✔
44
}
4,846,464✔
45

46
} // namespace
47

48
Decimal128::Decimal128() noexcept
49
    : Decimal128(0)
50
{
1,661,355✔
51
}
1,661,355✔
52

53
/**
54
 * The following function is effectively 'binary64_to_bid128' from the Intel library.
55
 * All macros are expanded, so it is perhaps not that readable. The reason for
56
 * including it here is to avoid including 'bid_binarydecimal.c' in its entirety
57
 * as this would add around 2Mb of data to the binary.
58
 */
59

60
namespace {
61

62
typedef struct BID_ALIGN(16) {
63
    BID_UINT64 w[6];
64
} BID_UINT384;
65
typedef struct BID_ALIGN(16) {
66
    BID_UINT64 w[8];
67
} BID_UINT512;
68

69
static const BID_UINT128 bid_coefflimits_bid128[] = {{{4003012203950112768ull, 542101086242752ull}},
70
                                                     {{8179300070273843200ull, 108420217248550ull}},
71
                                                     {{1635860014054768640ull, 21684043449710ull}},
72
                                                     {{327172002810953728ull, 4336808689942ull}},
73
                                                     {{7444132030046011392ull, 867361737988ull}},
74
                                                     {{12556872850234933248ull, 173472347597ull}},
75
                                                     {{9890072199530807296ull, 34694469519ull}},
76
                                                     {{16735409698873802752ull, 6938893903ull}},
77
                                                     {{14415128384000491520ull, 1387778780ull}},
78
                                                     {{2883025676800098304ull, 277555756ull}},
79
                                                     {{4265953950101929984ull, 55511151ull}},
80
                                                     {{4542539604762296320ull, 11102230ull}},
81
                                                     {{908507920952459264ull, 2220446ull}},
82
                                                     {{3871050398932402176ull, 444089ull}},
83
                                                     {{15531605338754121728ull, 88817ull}},
84
                                                     {{10485018697234644992ull, 17763ull}},
85
                                                     {{13165050183672659968ull, 3552ull}},
86
                                                     {{10011707666218352640ull, 710ull}},
87
                                                     {{2002341533243670528ull, 142ull}},
88
                                                     {{7779165936132554752ull, 28ull}},
89
                                                     {{12623879631452241920ull, 5ull}},
90
                                                     {{2524775926290448384ull, 1ull}},
91
                                                     {{4194304000000000000ull, 0ull}},
92
                                                     {{838860800000000000ull, 0ull}},
93
                                                     {{167772160000000000ull, 0ull}},
94
                                                     {{33554432000000000ull, 0ull}},
95
                                                     {{6710886400000000ull, 0ull}},
96
                                                     {{1342177280000000ull, 0ull}},
97
                                                     {{268435456000000ull, 0ull}},
98
                                                     {{53687091200000ull, 0ull}},
99
                                                     {{10737418240000ull, 0ull}},
100
                                                     {{2147483648000ull, 0ull}},
101
                                                     {{429496729600ull, 0ull}},
102
                                                     {{85899345920ull, 0ull}},
103
                                                     {{17179869184ull, 0ull}},
104
                                                     {{3435973836ull, 0ull}},
105
                                                     {{687194767ull, 0ull}},
106
                                                     {{137438953ull, 0ull}},
107
                                                     {{27487790ull, 0ull}},
108
                                                     {{5497558ull, 0ull}},
109
                                                     {{1099511ull, 0ull}},
110
                                                     {{219902ull, 0ull}},
111
                                                     {{43980ull, 0ull}},
112
                                                     {{8796ull, 0ull}},
113
                                                     {{1759ull, 0ull}},
114
                                                     {{351ull, 0ull}},
115
                                                     {{70ull, 0ull}},
116
                                                     {{14ull, 0ull}},
117
                                                     {{2ull, 0ull}}};
118

119
static const BID_UINT128 bid_roundbound_128[] = {
120
    {{0ull, (1ull << 63)}},      // BID_ROUNDING_TO_NEAREST | positive | even
121
    {{~0ull, (1ull << 63) - 1}}, // BID_ROUNDING_TO_NEAREST | positive | odd
122
    {{0ull, (1ull << 63)}},      // BID_ROUNDING_TO_NEAREST | negative | even
123
    {{~0ull, (1ull << 63) - 1}}, // BID_ROUNDING_TO_NEAREST | negative | odd
124

125
    {{~0ull, ~0ull}}, // BID_ROUNDING_DOWN       | positive | even
126
    {{~0ull, ~0ull}}, // BID_ROUNDING_DOWN       | positive | odd
127
    {{0ull, 0ull}},   // BID_ROUNDING_DOWN       | negative | even
128
    {{0ull, 0ull}},   // BID_ROUNDING_DOWN       | negative | odd
129

130
    {{0ull, 0ull}},   // BID_ROUNDING_UP         | positive | even
131
    {{0ull, 0ull}},   // BID_ROUNDING_UP         | positive | odd
132
    {{~0ull, ~0ull}}, // BID_ROUNDING_UP         | negative | even
133
    {{~0ull, ~0ull}}, // BID_ROUNDING_UP         | negative | odd
134

135
    {{~0ull, ~0ull}}, // BID_ROUNDING_TO_ZERO    | positive | even
136
    {{~0ull, ~0ull}}, // BID_ROUNDING_TO_ZERO    | positive | odd
137
    {{~0ull, ~0ull}}, // BID_ROUNDING_TO_ZERO    | negative | even
138
    {{~0ull, ~0ull}}, // BID_ROUNDING_TO_ZERO    | negative | odd
139

140
    {{~0ull, (1ull << 63) - 1}}, // BID_ROUNDING_TIES_AWAY  | positive | even
141
    {{~0ull, (1ull << 63) - 1}}, // BID_ROUNDING_TIES_AWAY  | positive | odd
142
    {{~0ull, (1ull << 63) - 1}}, // BID_ROUNDING_TIES_AWAY  | negative | even
143
    {{~0ull, (1ull << 63) - 1}}  // BID_ROUNDING_TIES_AWAY  | negative | odd
144
};
145

146
// Table of powers of 5
147

148

149
static const BID_UINT128 bid_power_five[] = {{{1ull, 0ull}},
150
                                             {{5ull, 0ull}},
151
                                             {{25ull, 0ull}},
152
                                             {{125ull, 0ull}},
153
                                             {{625ull, 0ull}},
154
                                             {{3125ull, 0ull}},
155
                                             {{15625ull, 0ull}},
156
                                             {{78125ull, 0ull}},
157
                                             {{390625ull, 0ull}},
158
                                             {{1953125ull, 0ull}},
159
                                             {{9765625ull, 0ull}},
160
                                             {{48828125ull, 0ull}},
161
                                             {{244140625ull, 0ull}},
162
                                             {{1220703125ull, 0ull}},
163
                                             {{6103515625ull, 0ull}},
164
                                             {{30517578125ull, 0ull}},
165
                                             {{152587890625ull, 0ull}},
166
                                             {{762939453125ull, 0ull}},
167
                                             {{3814697265625ull, 0ull}},
168
                                             {{19073486328125ull, 0ull}},
169
                                             {{95367431640625ull, 0ull}},
170
                                             {{476837158203125ull, 0ull}},
171
                                             {{2384185791015625ull, 0ull}},
172
                                             {{11920928955078125ull, 0ull}},
173
                                             {{59604644775390625ull, 0ull}},
174
                                             {{298023223876953125ull, 0ull}},
175
                                             {{1490116119384765625ull, 0ull}},
176
                                             {{7450580596923828125ull, 0ull}},
177
                                             {{359414837200037393ull, 2ull}},
178
                                             {{1797074186000186965ull, 10ull}},
179
                                             {{8985370930000934825ull, 50ull}},
180
                                             {{8033366502585570893ull, 252ull}},
181
                                             {{3273344365508751233ull, 1262ull}},
182
                                             {{16366721827543756165ull, 6310ull}},
183
                                             {{8046632842880574361ull, 31554ull}},
184
                                             {{3339676066983768573ull, 157772ull}},
185
                                             {{16698380334918842865ull, 788860ull}},
186
                                             {{9704925379756007861ull, 3944304ull}},
187
                                             {{11631138751360936073ull, 19721522ull}},
188
                                             {{2815461535676025517ull, 98607613ull}},
189
                                             {{14077307678380127585ull, 493038065ull}},
190
                                             {{15046306170771983077ull, 2465190328ull}},
191
                                             {{1444554559021708921ull, 12325951644ull}},
192
                                             {{7222772795108544605ull, 61629758220ull}},
193
                                             {{17667119901833171409ull, 308148791101ull}},
194
                                             {{14548623214327650581ull, 1540743955509ull}},
195
                                             {{17402883850509598057ull, 7703719777548ull}},
196
                                             {{13227442957709783821ull, 38518598887744ull}},
197
                                             {{10796982567420264257ull, 192592994438723ull}}};
198

199
// These are the different, bipartite, tables for conversion to bid128
200
// Using the same approach, the tables become extremely large
201
// And things are more amenable here since there's never overflow/underflow
202

203
static const BID_UINT256 bid_outertable_sig[] = {
204
    {{16710528681477587410ull, 1427578414467097172ull, 17470362193306814444ull, 17633471421292828081ull}},
205
    {{15880413049339289368ull, 3169162604521042544ull, 12421848348224877000ull, 10175591536883283100ull}},
206
    {{728324709502741634ull, 5487234822932806241ull, 14277366029702694882ull, 11743877385420605756ull}},
207
    {{5270439690693945016ull, 5335305964155802506ull, 4731239033579481048ull, 13553871098685738146ull}},
208
    {{15770926697301461842ull, 17478494563481727979ull, 12172666691698088779ull, 15642825255298684824ull}},
209
    {{6706015564063194464ull, 9524484409513358023ull, 3584925281718916951ull, 18053733887991431306ull}},
210
    {{11970480524618434228ull, 11405570099769256704ull, 15462553542164535233ull, 10418108684938663938ull}},
211
    {{6786207772287397676ull, 2319456072422691258ull, 3306628541457879036ull, 12023771840725819358ull}},
212
    {{11981052113010165492ull, 3504057943690712651ull, 1876153621163772099ull, 13876903538819465956ull}},
213
    {{9393164661428669080ull, 12786250932199773041ull, 1469280998340568779ull, 16015644206874417279ull}},
214
    {{16924685242153318850ull, 18017830257179898541ull, 8443357802200517361ull, 9242006282008467740ull}},
215
    {{10964968671057563176ull, 5039440430669711539ull, 5426243050445487622ull, 10666405798403203685ull}},
216
    {{10955754838860298353ull, 7697974614691938479ull, 5802604364043796934ull, 12310337083160321132ull}},
217
    {{2020816881108765590ull, 11827301330378587775ull, 11428107909474365520ull, 14207634883319258514ull}},
218
    {{17088069107880998350ull, 4283872614129133981ull, 3596834484036483711ull, 16397348635874181367ull}},
219
    {{17878879927905357932ull, 16765016545576715295ull, 16689816215723394984ull, 9462273083962776199ull}},
220
    {{13121733289080687293ull, 18283685101712419716ull, 16276586284347626380ull, 10920620632484725600ull}},
221
    {{17814811358648632259ull, 13245640156276305425ull, 16363965810173909683ull, 12603732099085151178ull}},
222
    {{4756697993914874888ull, 11508234184157253656ull, 5137266535116401279ull, 14546248621894116172ull}},
223
    {{5318236458935323174ull, 6543830884414701181ull, 6453355338781772809ull, 16788150311867950084ull}},
224
    {{6485710970464102310ull, 9658720758000782538ull, 1405691438411884535ull, 9687789553853107178ull}},
225
    {{16668567668910748869ull, 7353216905500064137ull, 16398637311140236340ull, 11180894225541718927ull}},
226
    {{10250898639443956700ull, 17209112682100433509ull, 10404161081088486903ull, 12904119664018836844ull}},
227
    {{8190593687966138954ull, 9395575747272723417ull, 5270639644724875979ull, 14892932617404296676ull}},
228
    {{16096765186944088526ull, 5812137315202163815ull, 13827109944906121794ull, 17188266051577202911ull}},
229
    {{13125058493821651226ull, 13878096157524874998ull, 7819283672493662452ull, 9918680808189048078ull}},
230
    {{10784977039313888136ull, 7095114120404217728ull, 5980679097159643429ull, 11447370977331402726ull}},
231
    {{18074025829186132275ull, 1141984379626550674ull, 7557580538320593620ull, 13211666432945230258ull}},
232
    {{884127375074722974ull, 5630658839879210216ull, 8888788495242174599ull, 15247879210087606793ull}},
233
    {{14794677677148287412ull, 15991859528909753139ull, 2255166953101703543ull, 17597917839164816062ull}},
234
    {{11781503818372409883ull, 16487377189598053250ull, 1614766483381505408ull, 10155074945409931597ull}},
235
    {{13901203812957478350ull, 17671725616207330354ull, 9774501520532043416ull, 11720198729122693309ull}},
236
    {{2841277750318224700ull, 62614824260888948ull, 7875289095414864909ull, 13526543032773672749ull}},
237
    {{4177215723684349918ull, 5549883551398310595ull, 6548711429670794128ull, 15611285324269742443ull}},
238
    {{10113135653152274419ull, 1238174514434849746ull, 15010187426055142985ull, 18017332949391848572ull}},
239
    {{15332868447221136909ull, 16659234357027498643ull, 8156814090647504084ull, 10397103116953834012ull}},
240
    {{15245187402216469644ull, 12129929088655149192ull, 9861651730211963150ull, 11999528845718521943ull}},
241
    {{11169863271521019024ull, 11690833164181629132ull, 18055231442152805128ull, 13848924157002783033ull}},
242
    {{5681139181384005971ull, 16315598095635316730ull, 12429006944274865118ull, 15983352577617880224ull}},
243
    {{0ull, 0ull, 0ull, 9223372036854775808ull}},
244
    {{847738094735128551ull, 159020156881263929ull, 14298703881791668535ull, 10644899600020376799ull}},
245
    {{12571812103339493257ull, 9592188640606484874ull, 15977522551232326327ull, 12285516299433008781ull}},
246
    {{11255846670375652269ull, 16219642565822741785ull, 1164180458167399492ull, 14178988662640388631ull}},
247
    {{4768530159026621925ull, 11269558331910606010ull, 14728279675391465720ull, 16364287392998134214ull}},
248
    {{10435171899994305314ull, 3358688235984080491ull, 10873005112892106269ull, 9443194724678278428ull}},
249
    {{9001934648837042518ull, 12742858465034581069ull, 7821978264675184102ull, 10898601872067700364ull}},
250
    {{17621267265258286727ull, 4697230115438671198ull, 9730745556445007669ull, 12578319756070083561ull}},
251
    {{15489206033570711069ull, 5939008219696634639ull, 7281543418588385486ull, 14516919669433371671ull}},
252
    {{5582382164278045428ull, 1021128504191590019ull, 15859662269683349667ull, 16754301112998936544ull}},
253
    {{13306060077138970688ull, 17077419079040409017ull, 602300193611639289ull, 9668256495766433483ull}},
254
    {{16144900726383728979ull, 6332437060439625781ull, 3394061071468721991ull, 11158350687084940805ull}},
255
    {{841527738022137013ull, 8576187517129556015ull, 4780478900157118356ull, 12878101662951253988ull}},
256
    {{6209518431268106959ull, 6563228687006195825ull, 8680557599339190037ull, 14862904661462481806ull}},
257
    {{13777918056850427098ull, 13980713634323581067ull, 3260730320187665275ull, 17153610117183879308ull}},
258
    {{14026398967643035423ull, 16044002814696042637ull, 13563648246219923183ull, 9898682214361989196ull}},
259
    {{8580849201736980837ull, 7652064075251385167ull, 12055336643618066002ull, 11424290153682525668ull}},
260
    {{7703450277136593061ull, 30939122015382097ull, 1733744904199768989ull, 13185028339041359606ull}},
261
    {{16645858086931268484ull, 268706294728574543ull, 4562366333509913804ull, 15217135591158481007ull}},
262
    {{1535817262145462730ull, 16249574698204674087ull, 1726174694286833848ull, 17562435942139069664ull}},
263
    {{1327779613951528273ull, 12607890553358950732ull, 6773080245737622022ull, 10134599720625107110ull}},
264
    {{10146669700226523625ull, 6816618733538609533ull, 17338427607494047961ull, 11696567815043613180ull}},
265
    {{1218646606393179524ull, 8053984438814192242ull, 5512554737593155320ull, 13499270067222312908ull}},
266
    {{8529816360522570005ull, 2898610325645650649ull, 12799329154556421864ull, 15579808985797328396ull}},
267
    {{8976626101186384018ull, 17306366585957786234ull, 18289272351796647404ull, 17981005404381600394ull}},
268
    {{13259258789938866699ull, 3853966764738768487ull, 3962898110873089456ull, 10376139901559067117ull}},
269
    {{899097863387258947ull, 18205835716688941338ull, 5828614502416977816ull, 11975334730781032005ull}},
270
    {{6805696657844643720ull, 11269663690239600300ull, 15713752492130876427ull, 13821001188766021149ull}},
271
    {{1390669429605106863ull, 9874674503958832077ull, 10784451562526769943ull, 15951126056533488631ull}},
272
    {{5704986635434998471ull, 13359511205918707297ull, 13484363347568202582ull, 18409550726197325520ull}},
273
    {{8031416311578790746ull, 15203770801091158414ull, 10108131133879485063ull, 10623436763626360685ull}},
274
    {{14540970352057967818ull, 10823414366732926995ull, 16152175176069010361ull, 12260745560745135745ull}},
275
    {{15950896424073439808ull, 7311065895593189991ull, 14504991862333000338ull, 14150400200058902426ull}},
276
    {{5978819348613013533ull, 5596367464999577452ull, 9804643584580705193ull, 16331292810031855499ull}},
277
    {{8586457898440847299ull, 6018330550275346810ull, 7956755163056284140ull, 9424154832238877876ull}},
278
    {{1114429888821394320ull, 1760611426277998851ull, 9839409379426382903ull, 10876627507095459665ull}},
279
    {{7554605751361075608ull, 6504275622057553570ull, 9148491728899045148ull, 12552958650829068784ull}},
280
    {{9208049516541304162ull, 15518058123431536615ull, 17563500997894963674ull, 14487649851631658771ull}},
281
    {{1484107481346855224ull, 16657394431011607502ull, 12009304572091620947ull, 16720520162760224108ull}},
282
    {{14325586681310127114ull, 11250726580617083666ull, 2403442209646777766ull, 9648762821313776241ull}},
283
    {{14963893077912294692ull, 3450059817568720983ull, 15313875826588494017ull, 11135852602159508258ull}}};
284

285
static const BID_UINT256 bid_innertable_sig[] = {
286
    {{1014026100135492416ull, 3035406636157676337ull, 4549648098962661924ull, 12141680576410806693ull}},
287
    {{5879218643596753424ull, 3794258295197095421ull, 10298746142130715309ull, 15177100720513508366ull}},
288
    {{5980354661461664842ull, 4677254443711878590ull, 1825030320404309164ull, 9485687950320942729ull}},
289
    {{16698815363681856860ull, 5846568054639848237ull, 6892973918932774359ull, 11857109937901178411ull}},
290
    {{7038461149320157363ull, 2696524049872422393ull, 4004531380238580045ull, 14821387422376473014ull}},
291
    {{15928253264393568112ull, 3991170540383957947ull, 16337890167931276240ull, 9263367138985295633ull}},
292
    {{15298630562064572236ull, 4988963175479947434ull, 6587304654631931588ull, 11579208923731619542ull}},
293
    {{9899916165725939487ull, 6236203969349934293ull, 17457502855144690293ull, 14474011154664524427ull}},
294
    {{16986581225584812263ull, 12406940980114805770ull, 17210192550503474962ull, 18092513943330655534ull}},
295
    {{15228299284417895569ull, 12366024130999141510ull, 6144684325637283947ull, 11307821214581659709ull}},
296
    {{9812002068667593653ull, 10845844145321538984ull, 12292541425473992838ull, 14134776518227074636ull}},
297
    {{12265002585834492066ull, 4333933144797147922ull, 15365676781842491048ull, 17668470647783843295ull}},
298
    {{12277312634573945445ull, 2708708215498217451ull, 16521077016292638761ull, 11042794154864902059ull}},
299
    {{10734954774790043902ull, 7997571287800159718ull, 16039660251938410547ull, 13803492693581127574ull}},
300
    {{4195321431632779070ull, 5385278091322811744ull, 10826203278068237376ull, 17254365866976409468ull}},
301
    {{2622075894770486919ull, 3365798807076757340ull, 15989749085647424168ull, 10783978666860255917ull}},
302
    {{3277594868463108648ull, 4207248508845946675ull, 6152128301777116498ull, 13479973333575319897ull}},
303
    {{17932051640861049522ull, 14482432672912209151ull, 12301846395648783526ull, 16849966666969149871ull}},
304
    {{18125061303179237808ull, 4439834402142742815ull, 14606183024921571560ull, 10531229166855718669ull}},
305
    {{18044640610546659355ull, 5549793002678428519ull, 4422670725869800738ull, 13164036458569648337ull}},
306
    {{17944114744755936290ull, 16160613290202811457ull, 10140024425764638826ull, 16455045573212060421ull}},
307
    {{4297542687831378326ull, 14712069324804145065ull, 8643358275316593218ull, 10284403483257537763ull}},
308
    {{9983614378216610811ull, 9166714619150405523ull, 6192511825718353619ull, 12855504354071922204ull}},
309
    {{7867831954343375609ull, 6846707255510619000ull, 7740639782147942024ull, 16069380442589902755ull}},
310
    {{4917394971464609756ull, 4279192034694136875ull, 2532056854628769813ull, 10043362776618689222ull}},
311
    {{1535057695903374291ull, 9960676061795058998ull, 12388443105140738074ull, 12554203470773361527ull}},
312
    {{11142194156733993672ull, 3227473040389047939ull, 10873867862998534689ull, 15692754338466701909ull}},
313
    {{4658028338745052093ull, 13546385696311624722ull, 9102010423587778132ull, 9807971461541688693ull}},
314
    {{15045907460286090924ull, 16932982120389530902ull, 15989199047912110569ull, 12259964326927110866ull}},
315
    {{9584012288502837847ull, 7331169595204749916ull, 10763126773035362404ull, 15324955408658888583ull}},
316
    {{15213379717169049462ull, 13805353033857744505ull, 13644483260788183358ull, 9578097130411805364ull}},
317
    {{5181666591179148116ull, 8033319255467404824ull, 17055604075985229198ull, 11972621413014756705ull}},
318
    {{6477083238973935145ull, 818277032479480222ull, 7484447039699372786ull, 14965776766268445882ull}},
319
    {{17883235079640873178ull, 5123109163727063042ull, 9289465418239495895ull, 9353610478917778676ull}},
320
    {{13130671812696315664ull, 1792200436231440899ull, 11611831772799369869ull, 11692013098647223345ull}},
321
    {{11801653747443006676ull, 6851936563716689028ull, 679731660717048624ull, 14615016373309029182ull}},
322
    {{14752067184303758345ull, 8564920704645861285ull, 10073036612751086588ull, 18268770466636286477ull}},
323
    {{11525884999403542918ull, 14576447477258439111ull, 8601490892183123069ull, 11417981541647679048ull}},
324
    {{9795670230827040743ull, 4385501291290885177ull, 10751863615228903837ull, 14272476927059598810ull}},
325
    {{16856273806961188833ull, 10093562632540994375ull, 4216457482181353988ull, 17840596158824498513ull}},
326
    {{17452700156991824877ull, 15531848682192897292ull, 14164500972431816002ull, 11150372599265311570ull}},
327
    {{3369131122530229480ull, 10191438815886345808ull, 8482254178684994195ull, 13937965749081639463ull}},
328
    {{4211413903162786849ull, 8127612501430544356ull, 5991131704928854840ull, 17422457186352049329ull}},
329
    {{11855505726331517589ull, 5079757813394090222ull, 15273672361649004035ull, 10889035741470030830ull}},
330
    {{5596010121059621178ull, 1738011248315224874ull, 9868718415206479236ull, 13611294676837538538ull}},
331
    {{16218384688179302281ull, 2172514060394031092ull, 3112525982153323237ull, 17014118346046923173ull}},
332
    {{913118393257288118ull, 3663664296959963385ull, 4251171748059520975ull, 10633823966279326983ull}},
333
    {{5753084009998998051ull, 18414638426482117943ull, 702278666647013314ull, 13292279957849158729ull}},
334
    {{2579668994071359659ull, 13794925996247871621ull, 5489534351736154547ull, 16615349947311448411ull}},
335
    {{3918136130508293739ull, 6315985738441225811ull, 1125115960621402640ull, 10384593717069655257ull}},
336
    {{285984144707979270ull, 7894982173051532264ull, 6018080969204141204ull, 12980742146337069071ull}},
337
    {{357480180884974087ull, 9868727716314415330ull, 2910915193077788601ull, 16225927682921336339ull}},
338
    {{4835111131480496709ull, 17697169868764979341ull, 17960223060169475539ull, 10141204801825835211ull}},
339
    {{10655574932778008790ull, 17509776317528836272ull, 17838592806784456520ull, 12676506002282294014ull}},
340
    {{13319468665972510987ull, 3440476323201493724ull, 13074868971625794843ull, 15845632502852867518ull}},
341
    {{17548039953087595175ull, 18291198766496791241ull, 3560107088838733872ull, 9903520314283042199ull}},
342
    {{8099991886077330257ull, 4417254384411437436ull, 18285191916330581053ull, 12379400392853802748ull}},
343
    {{10124989857596662821ull, 10133253998941684699ull, 4409745821703674700ull, 15474250491067253436ull}},
344
    {{4022275651784220311ull, 15556655786193328745ull, 11979463175419572495ull, 9671406556917033397ull}},
345
    {{9639530583157663293ull, 14834133714314273027ull, 1139270913992301907ull, 12089258196146291747ull}},
346
    {{7437727210519691212ull, 13930981124465453380ull, 15259146697772541096ull, 15111572745182864683ull}},
347
    {{13871951543429582816ull, 8706863202790908362ull, 7231123676894144233ull, 9444732965739290427ull}},
348
    {{8116567392432202712ull, 15495265021916023357ull, 4427218577690292387ull, 11805916207174113034ull}},
349
    {{14757395258967641293ull, 14757395258967641292ull, 14757395258967641292ull, 14757395258967641292ull}},
350
    {{0ull, 0ull, 0ull, 9223372036854775808ull}},
351
    {{0ull, 0ull, 0ull, 11529215046068469760ull}},
352
    {{0ull, 0ull, 0ull, 14411518807585587200ull}},
353
    {{0ull, 0ull, 0ull, 18014398509481984000ull}},
354
    {{0ull, 0ull, 0ull, 11258999068426240000ull}},
355
    {{0ull, 0ull, 0ull, 14073748835532800000ull}},
356
    {{0ull, 0ull, 0ull, 17592186044416000000ull}},
357
    {{0ull, 0ull, 0ull, 10995116277760000000ull}},
358
    {{0ull, 0ull, 0ull, 13743895347200000000ull}},
359
    {{0ull, 0ull, 0ull, 17179869184000000000ull}},
360
    {{0ull, 0ull, 0ull, 10737418240000000000ull}},
361
    {{0ull, 0ull, 0ull, 13421772800000000000ull}},
362
    {{0ull, 0ull, 0ull, 16777216000000000000ull}},
363
    {{0ull, 0ull, 0ull, 10485760000000000000ull}},
364
    {{0ull, 0ull, 0ull, 13107200000000000000ull}},
365
    {{0ull, 0ull, 0ull, 16384000000000000000ull}},
366
    {{0ull, 0ull, 0ull, 10240000000000000000ull}},
367
    {{0ull, 0ull, 0ull, 12800000000000000000ull}},
368
    {{0ull, 0ull, 0ull, 16000000000000000000ull}},
369
    {{0ull, 0ull, 0ull, 10000000000000000000ull}},
370
    {{0ull, 0ull, 0ull, 12500000000000000000ull}},
371
    {{0ull, 0ull, 0ull, 15625000000000000000ull}},
372
    {{0ull, 0ull, 0ull, 9765625000000000000ull}},
373
    {{0ull, 0ull, 0ull, 12207031250000000000ull}},
374
    {{0ull, 0ull, 0ull, 15258789062500000000ull}},
375
    {{0ull, 0ull, 0ull, 9536743164062500000ull}},
376
    {{0ull, 0ull, 0ull, 11920928955078125000ull}},
377
    {{0ull, 0ull, 0ull, 14901161193847656250ull}},
378
    {{0ull, 0ull, 4611686018427387904ull, 9313225746154785156ull}},
379
    {{0ull, 0ull, 5764607523034234880ull, 11641532182693481445ull}},
380
    {{0ull, 0ull, 11817445422220181504ull, 14551915228366851806ull}},
381
    {{0ull, 0ull, 5548434740920451072ull, 18189894035458564758ull}},
382
    {{0ull, 0ull, 17302829768357445632ull, 11368683772161602973ull}},
383
    {{0ull, 0ull, 7793479155164643328ull, 14210854715202003717ull}},
384
    {{0ull, 0ull, 14353534962383192064ull, 17763568394002504646ull}},
385
    {{0ull, 0ull, 4359273333062107136ull, 11102230246251565404ull}},
386
    {{0ull, 0ull, 5449091666327633920ull, 13877787807814456755ull}},
387
    {{0ull, 0ull, 2199678564482154496ull, 17347234759768070944ull}},
388
    {{0ull, 0ull, 1374799102801346560ull, 10842021724855044340ull}},
389
    {{0ull, 0ull, 1718498878501683200ull, 13552527156068805425ull}},
390
    {{0ull, 0ull, 6759809616554491904ull, 16940658945086006781ull}},
391
    {{0ull, 0ull, 6530724019560251392ull, 10587911840678754238ull}},
392
    {{0ull, 0ull, 17386777061305090048ull, 13234889800848442797ull}},
393
    {{0ull, 0ull, 7898413271349198848ull, 16543612251060553497ull}},
394
    {{0ull, 0ull, 16465723340661719040ull, 10339757656912845935ull}},
395
    {{0ull, 0ull, 15970468157399760896ull, 12924697071141057419ull}},
396
    {{0ull, 0ull, 15351399178322313216ull, 16155871338926321774ull}},
397
    {{0ull, 0ull, 4982938468024057856ull, 10097419586828951109ull}},
398
    {{0ull, 0ull, 10840359103457460224ull, 12621774483536188886ull}},
399
    {{0ull, 0ull, 4327076842467049472ull, 15777218104420236108ull}},
400
    {{0ull, 0ull, 11927795063396681728ull, 9860761315262647567ull}},
401
    {{0ull, 0ull, 10298057810818464256ull, 12325951644078309459ull}},
402
    {{0ull, 0ull, 8260886245095692416ull, 15407439555097886824ull}},
403
    {{0ull, 0ull, 5163053903184807760ull, 9629649721936179265ull}},
404
    {{0ull, 0ull, 11065503397408397604ull, 12037062152420224081ull}},
405
    {{0ull, 0ull, 18443565265187884909ull, 15046327690525280101ull}},
406
    {{0ull, 2305843009213693952ull, 13833071299956122020ull, 9403954806578300063ull}},
407
    {{0ull, 2882303761517117440ull, 12679653106517764621ull, 11754943508222875079ull}},
408
    {{0ull, 8214565720323784704ull, 11237880364719817872ull, 14693679385278593849ull}},
409
    {{0ull, 10268207150404730880ull, 212292400617608628ull, 18367099231598242312ull}},
410
    {{0ull, 15641001505857732608ull, 132682750386005392ull, 11479437019748901445ull}},
411
    {{0ull, 1104507808612614144ull, 4777539456409894645ull, 14349296274686126806ull}},
412
    {{0ull, 5992320779193155584ull, 15195296357367144114ull, 17936620343357658507ull}},
413
    {{0ull, 8356886505423110144ull, 7191217214140771119ull, 11210387714598536567ull}}};
414

415
static const int bid_outertable_exp[] = {
416
    -16839, -16413, -15988, -15563, -15138, -14713, -14287, -13862, -13437, -13012, -12586, -12161, -11736, -11311,
417
    -10886, -10460, -10035, -9610,  -9185,  -8760,  -8334,  -7909,  -7484,  -7059,  -6634,  -6208,  -5783,  -5358,
418
    -4933,  -4508,  -4082,  -3657,  -3232,  -2807,  -2382,  -1956,  -1531,  -1106,  -681,   -255,   170,    595,
419
    1020,   1445,   1871,   2296,   2721,   3146,   3571,   3997,   4422,   4847,   5272,   5697,   6123,   6548,
420
    6973,   7398,   7823,   8249,   8674,   9099,   9524,   9949,   10375,  10800,  11225,  11650,  12075,  12501,
421
    12926,  13351,  13776,  14202,  14627,  15052,  15477,  15902,  16328,  16753,
422
};
423

424
static const int bid_innertable_exp[] = {
425
    -468, -465, -461, -458, -455, -451, -448, -445, -442, -438, -435, -432, -428, -425, -422, -418, -415, -412, -408,
426
    -405, -402, -398, -395, -392, -388, -385, -382, -378, -375, -372, -368, -365, -362, -358, -355, -352, -349, -345,
427
    -342, -339, -335, -332, -329, -325, -322, -319, -315, -312, -309, -305, -302, -299, -295, -292, -289, -285, -282,
428
    -279, -275, -272, -269, -265, -262, -259, -255, -252, -249, -246, -242, -239, -236, -232, -229, -226, -222, -219,
429
    -216, -212, -209, -206, -202, -199, -196, -192, -189, -186, -182, -179, -176, -172, -169, -166, -162, -159, -156,
430
    -153, -149, -146, -143, -139, -136, -133, -129, -126, -123, -119, -116, -113, -109, -106, -103, -99,  -96,  -93,
431
    -89,  -86,  -83,  -79,  -76,  -73,  -69,  -66,  -63,  -60,  -56,  -53,  -50,  -46,
432
};
433

434
void realm_binary64_to_bid128(BID_UINT128* pres, double* px, _IDEC_flags* pfpsf)
435
{
26,139✔
436
    double x = *px;
26,139✔
437
    BID_UINT128 c;
26,139✔
438
    BID_UINT64 c_prov_hi, c_prov_lo;
26,139✔
439
    BID_UINT256 r;
26,139✔
440
    BID_UINT384 z;
26,139✔
441

13,179✔
442
    // Unpack the input
13,179✔
443
    int e, s, t, e_out, e_plus, e_hi, e_lo, f;
26,139✔
444
    {
26,139✔
445
        union {
26,139✔
446
            BID_UINT64 i;
26,139✔
447
            double f;
26,139✔
448
        } x_in;
26,139✔
449
        x_in.f = x;
26,139✔
450
        c.w[1] = x_in.i;
26,139✔
451
        e = (c.w[1] >> 52) & ((1ull << 11) - 1);
26,139✔
452
        s = c.w[1] >> 63;
26,139✔
453
        c.w[1] = c.w[1] & ((1ull << 52) - 1);
26,139✔
454
        if (e == 0) {
26,139✔
455
            int l;
720✔
456
            if (c.w[1] == 0) {
720✔
457
                BID_UINT128 x_out;
720✔
458
                x_out.w[0] = 0;
720✔
459
                x_out.w[1] = ((BID_UINT64)(s) << 63) + ((BID_UINT64)(6176) << 49) + (0);
720✔
460
                *pres = x_out;
720✔
461
                return;
720✔
462
            };
360✔
463
            l = (((c.w[1]) == 0)
×
464
                     ? 64
×
465
                     : (((((c.w[1]) & 0xFFFFFFFF00000000ull) <= ((c.w[1]) & ~0xFFFFFFFF00000000ull)) ? 32 : 0) +
×
466
                        ((((c.w[1]) & 0xFFFF0000FFFF0000ull) <= ((c.w[1]) & ~0xFFFF0000FFFF0000ull)) ? 16 : 0) +
×
467
                        ((((c.w[1]) & 0xFF00FF00FF00FF00ull) <= ((c.w[1]) & ~0xFF00FF00FF00FF00ull)) ? 8 : 0) +
×
468
                        ((((c.w[1]) & 0xF0F0F0F0F0F0F0F0ull) <= ((c.w[1]) & ~0xF0F0F0F0F0F0F0F0ull)) ? 4 : 0) +
×
469
                        ((((c.w[1]) & 0xCCCCCCCCCCCCCCCCull) <= ((c.w[1]) & ~0xCCCCCCCCCCCCCCCCull)) ? 2 : 0) +
×
470
                        ((((c.w[1]) & 0xAAAAAAAAAAAAAAAAull) <= ((c.w[1]) & ~0xAAAAAAAAAAAAAAAAull)) ? 1 : 0))) -
×
471
                (64 - 53);
×
472
            c.w[1] = c.w[1] << l;
×
473
            e = -(l + 1074);
×
474
            t = 0;
×
475
            *(pfpsf) |= 0x02;
×
476
        }
×
477
        else if (e == ((1ull << 11) - 1)) {
25,419✔
478
            if (c.w[1] == 0) {
×
479
                BID_UINT128 x_out;
×
480
                x_out.w[0] = 0;
×
481
                x_out.w[1] = ((BID_UINT64)(s) << 63) + ((BID_UINT64)((0xF << 10)) << 49) + (0);
×
482
                *pres = x_out;
×
483
                return;
×
484
            };
×
485
            if ((c.w[1] & (1ull << 51)) == 0)
×
486
                *(pfpsf) |= 0x01;
×
487
            {
×
488
                if ((((54210108624275ull) < (((((unsigned long long)c.w[1]) << 13) >> 18))) ||
×
489
                     (((54210108624275ull) == (((((unsigned long long)c.w[1]) << 13) >> 18))) &&
×
490
                      ((4089650035136921599ull) <
×
491
                       (((0ull >> 18) + ((((unsigned long long)c.w[1]) << 13) << 46))))))) {
×
492
                    BID_UINT128 x_out;
×
493
                    x_out.w[0] = 0ull;
×
494
                    x_out.w[1] = ((BID_UINT64)(s) << 63) + ((BID_UINT64)((0x1F << 9)) << 49) + (0ull);
×
495
                    *pres = x_out;
×
496
                    return;
×
497
                }
×
498
                else {
×
499
                    BID_UINT128 x_out;
×
500
                    x_out.w[0] = ((0ull >> 18) + ((((unsigned long long)c.w[1]) << 13) << 46));
×
501
                    x_out.w[1] = ((BID_UINT64)(s) << 63) + ((BID_UINT64)((0x1F << 9)) << 49) +
×
502
                                 (((((unsigned long long)c.w[1]) << 13) >> 18));
×
503
                    *pres = x_out;
×
504
                    return;
×
505
                }
×
506
            }
25,419✔
507
        }
25,419✔
508
        else {
25,419✔
509
            c.w[1] += 1ull << 52;
25,419✔
510
            t = (((c.w[1]) == 0) ? 64
12,819✔
511
                                 : (((((c.w[1]) & -(c.w[1])) & ~0xFFFFFFFF00000000ull) ? 0 : 32) +
25,419✔
512
                                    ((((c.w[1]) & -(c.w[1])) & ~0xFFFF0000FFFF0000ull) ? 0 : 16) +
20,568✔
513
                                    ((((c.w[1]) & -(c.w[1])) & ~0xFF00FF00FF00FF00ull) ? 0 : 8) +
21,453✔
514
                                    ((((c.w[1]) & -(c.w[1])) & ~0xF0F0F0F0F0F0F0F0ull) ? 0 : 4) +
20,271✔
515
                                    ((((c.w[1]) & -(c.w[1])) & ~0xCCCCCCCCCCCCCCCCull) ? 0 : 2) +
20,787✔
516
                                    ((((c.w[1]) & -(c.w[1])) & ~0xAAAAAAAAAAAAAAAAull) ? 0 : 1)));
19,422✔
517
            e -= 1075;
25,419✔
518
        }
25,419✔
519
    };
25,779✔
520

12,819✔
521
    // Now -1126<=e<=971 (971 for max normal, -1074 for min normal, -1126 for min denormal)
12,819✔
522

12,819✔
523
    // Shift up to the top: like a pure quad coefficient with a shift of 15.
12,819✔
524
    // In our case, this is 2^{113-53+15} times the core, so unpack at the
12,819✔
525
    // high end shifted by 11.
12,819✔
526

12,819✔
527
    c.w[0] = 0;
25,419✔
528
    c.w[1] = c.w[1] << 11;
25,419✔
529

12,819✔
530
    t += (113 - 53);
25,419✔
531
    e -= (113 - 53); // Now e belongs [-1186;911].
25,419✔
532

12,819✔
533
    // (We never need to check for overflow: this format is the biggest of all!)
12,819✔
534

12,819✔
535
    // Now filter out all the exact cases where we need to specially force
12,819✔
536
    // the exponent to 0. We can let through inexact cases and those where the
12,819✔
537
    // main path will do the right thing anyway, e.g. integers outside coeff range.
12,819✔
538
    //
12,819✔
539
    // First check that e <= 0, because if e > 0, the input must be >= 2^113,
12,819✔
540
    // which is too large for the coefficient of any target decimal format.
12,819✔
541
    // We write a = -(e + t)
12,819✔
542
    //
12,819✔
543
    // (1) If e + t >= 0 <=> a <= 0 the input is an integer; treat it specially
12,819✔
544
    //     iff it fits in the coefficient range. Shift c' = c >> -e, and
12,819✔
545
    //     compare with the coefficient range; if it's in range then c' is
12,819✔
546
    //     our coefficient, exponent is 0. Otherwise we pass through.
12,819✔
547
    //
12,819✔
548
    // (2) If a > 0 then we have a non-integer input. The special case would
12,819✔
549
    //     arise as c' / 2^a where c' = c >> t, i.e. 10^-a * (5^a c'). Now
12,819✔
550
    //     if a > 48 we can immediately forget this, since 5^49 > 10^34.
12,819✔
551
    //     Otherwise we determine whether we're in range by a table based on
12,819✔
552
    //     a, and if so get the multiplier also from a table based on a.
12,819✔
553
    //
12,819✔
554
    // Note that when we shift, we need to take into account the fact that
12,819✔
555
    // c is already 15 places to the left in preparation for the reciprocal
12,819✔
556
    // multiplication; thus we add 15 to all the shift counts
12,819✔
557

12,819✔
558
    if (e <= 0) {
25,419✔
559
        BID_UINT128 cint;
25,419✔
560
        int a = -(e + t);
25,419✔
561
        cint.w[1] = c.w[1], cint.w[0] = c.w[0];
25,419✔
562
        if (a <= 0) {
25,419✔
563
            (((15 - e) == 0)
9,459✔
564
             ? cint.w[1] = cint.w[1],
4,728✔
565
             cint.w[0] = cint.w[0]
×
566
             : (((15 - e) >= 64) ? cint.w[0] = cint.w[1] >> ((15 - e) - 64), cint.w[1] = 0
9,459✔
567
                                 : ((cint.w[0]) = ((cint.w[1]) << (64 - (15 - e))) + ((cint.w[0]) >> (15 - e)),
4,728✔
568
                                    (cint.w[1]) = (cint.w[1]) >> (15 - e))));
×
569
            if ((((cint.w[1]) < (542101086242752ull)) ||
9,459✔
570
                 (((cint.w[1]) == (542101086242752ull)) && ((cint.w[0]) < (4003012203950112768ull))))) {
9,459!
571
                BID_UINT128 x_out;
9,459✔
572
                x_out.w[0] = cint.w[0];
9,459✔
573
                x_out.w[1] = ((BID_UINT64)(s) << 63) + ((BID_UINT64)(6176) << 49) + (cint.w[1]);
9,459✔
574
                *pres = x_out;
9,459✔
575
                return;
9,459✔
576
            };
4,728✔
577
        }
×
578
        else if (a <= 48) {
15,960✔
579
            BID_UINT128 pow5 = bid_coefflimits_bid128[a];
8,160✔
580
            (((15 + t) == 0)
8,160✔
581
             ? cint.w[1] = cint.w[1],
4,239✔
582
             cint.w[0] = cint.w[0]
×
583
             : (((15 + t) >= 64) ? cint.w[0] = cint.w[1] >> ((15 + t) - 64), cint.w[1] = 0
8,160✔
584
                                 : ((cint.w[0]) = ((cint.w[1]) << (64 - (15 + t))) + ((cint.w[0]) >> (15 + t)),
4,239✔
585
                                    (cint.w[1]) = (cint.w[1]) >> (15 + t))));
×
586
            if ((((cint.w[1]) < (pow5.w[1])) || (((cint.w[1]) == (pow5.w[1])) && ((cint.w[0]) <= (pow5.w[0]))))) {
8,160✔
587
                BID_UINT128 cc;
8,106✔
588
                cc.w[1] = cint.w[1];
8,106✔
589
                cc.w[0] = cint.w[0];
8,106✔
590
                pow5 = bid_power_five[a];
8,106✔
591
                {
8,106✔
592
                    BID_UINT128 ALBL;
8,106✔
593
                    BID_UINT64 QM64;
8,106✔
594
                    {
8,106✔
595
                        BID_UINT64 CXH, CXL, CYH, CYL, PL, PH, PM, PM2;
8,106✔
596
                        CXH = ((cc).w[0]) >> 32;
8,106✔
597
                        CXL = (BID_UINT32)((cc).w[0]);
8,106✔
598
                        CYH = ((pow5).w[0]) >> 32;
8,106✔
599
                        CYL = (BID_UINT32)((pow5).w[0]);
8,106✔
600
                        PM = CXH * CYL;
8,106✔
601
                        PH = CXH * CYH;
8,106✔
602
                        PL = CXL * CYL;
8,106✔
603
                        PM2 = CXL * CYH;
8,106✔
604
                        PH += (PM >> 32);
8,106✔
605
                        PM = (BID_UINT64)((BID_UINT32)PM) + PM2 + (PL >> 32);
8,106✔
606
                        (ALBL).w[1] = PH + (PM >> 32);
8,106✔
607
                        (ALBL).w[0] = (PM << 32) + (BID_UINT32)PL;
8,106✔
608
                    };
8,106✔
609
                    QM64 = (pow5).w[0] * (cc).w[1] + (cc).w[0] * (pow5).w[1];
8,106✔
610
                    (cc).w[0] = ALBL.w[0];
8,106✔
611
                    (cc).w[1] = QM64 + ALBL.w[1];
8,106✔
612
                };
8,106✔
613
                {
8,106✔
614
                    BID_UINT128 x_out;
8,106✔
615
                    x_out.w[0] = cc.w[0];
8,106✔
616
                    x_out.w[1] = ((BID_UINT64)(s) << 63) + ((BID_UINT64)(6176 - a) << 49) + (cc.w[1]);
8,106✔
617
                    *pres = x_out;
8,106✔
618
                    return;
8,106✔
619
                };
4,212✔
620
            }
×
621
        }
8,160✔
622
    }
25,419✔
623

12,819✔
624
    // Input exponent can stretch between the maximal and minimal
12,819✔
625
    // exponents (remembering we force normalization): -16607 <= e <= 16271
12,819✔
626

12,819✔
627
    // Compute the estimated decimal exponent e_out; the provisional exponent
12,819✔
628
    // will be either "e_out" or "e_out-1" depending on later significand check
12,819✔
629
    // NB: this is the *biased* exponent
12,819✔
630
    e_plus = e + 42152;
16,794✔
631
    e_out = (((19728 * e_plus) + ((19779 * e_plus) >> 16)) >> 16) - 6512;
7,854✔
632

3,879✔
633
    // Set up pointers into the bipartite table
3,879✔
634
    e_hi = 11232 - e_out;
7,854✔
635
    e_lo = e_hi & 127;
7,854✔
636
    e_hi = e_hi >> 7;
7,854✔
637

3,879✔
638
    // Look up the inner entry first
3,879✔
639
    r = bid_innertable_sig[e_lo], f = bid_innertable_exp[e_lo];
7,854✔
640

3,879✔
641
    // If we need the other entry, multiply significands and add exponents
3,879✔
642
    if (e_hi != 39) {
7,854✔
643
        BID_UINT256 s_prime = bid_outertable_sig[e_hi];
×
644
        BID_UINT512 t_prime;
×
645
        f = f + 256 + bid_outertable_exp[e_hi];
×
646
        {
×
647
            BID_UINT512 P0, P1, P2, P3;
×
648
            BID_UINT64 CY;
×
649
            {
×
650
                BID_UINT128 lP0, lP1, lP2, lP3;
×
651
                BID_UINT64 lC;
×
652
                {
×
653
                    BID_UINT64 CXH, CXL, CYH, CYL, PL, PH, PM, PM2;
×
654
                    CXH = ((r).w[0]) >> 32;
×
655
                    CXL = (BID_UINT32)((r).w[0]);
×
656
                    CYH = ((s_prime).w[0]) >> 32;
×
657
                    CYL = (BID_UINT32)((s_prime).w[0]);
×
658
                    PM = CXH * CYL;
×
659
                    PH = CXH * CYH;
×
660
                    PL = CXL * CYL;
×
661
                    PM2 = CXL * CYH;
×
662
                    PH += (PM >> 32);
×
663
                    PM = (BID_UINT64)((BID_UINT32)PM) + PM2 + (PL >> 32);
×
664
                    (lP0).w[1] = PH + (PM >> 32);
×
665
                    (lP0).w[0] = (PM << 32) + (BID_UINT32)PL;
×
666
                };
×
667
                {
×
668
                    BID_UINT64 CXH, CXL, CYH, CYL, PL, PH, PM, PM2;
×
669
                    CXH = ((r).w[0]) >> 32;
×
670
                    CXL = (BID_UINT32)((r).w[0]);
×
671
                    CYH = ((s_prime).w[1]) >> 32;
×
672
                    CYL = (BID_UINT32)((s_prime).w[1]);
×
673
                    PM = CXH * CYL;
×
674
                    PH = CXH * CYH;
×
675
                    PL = CXL * CYL;
×
676
                    PM2 = CXL * CYH;
×
677
                    PH += (PM >> 32);
×
678
                    PM = (BID_UINT64)((BID_UINT32)PM) + PM2 + (PL >> 32);
×
679
                    (lP1).w[1] = PH + (PM >> 32);
×
680
                    (lP1).w[0] = (PM << 32) + (BID_UINT32)PL;
×
681
                };
×
682
                {
×
683
                    BID_UINT64 CXH, CXL, CYH, CYL, PL, PH, PM, PM2;
×
684
                    CXH = ((r).w[0]) >> 32;
×
685
                    CXL = (BID_UINT32)((r).w[0]);
×
686
                    CYH = ((s_prime).w[2]) >> 32;
×
687
                    CYL = (BID_UINT32)((s_prime).w[2]);
×
688
                    PM = CXH * CYL;
×
689
                    PH = CXH * CYH;
×
690
                    PL = CXL * CYL;
×
691
                    PM2 = CXL * CYH;
×
692
                    PH += (PM >> 32);
×
693
                    PM = (BID_UINT64)((BID_UINT32)PM) + PM2 + (PL >> 32);
×
694
                    (lP2).w[1] = PH + (PM >> 32);
×
695
                    (lP2).w[0] = (PM << 32) + (BID_UINT32)PL;
×
696
                };
×
697
                {
×
698
                    BID_UINT64 CXH, CXL, CYH, CYL, PL, PH, PM, PM2;
×
699
                    CXH = ((r).w[0]) >> 32;
×
700
                    CXL = (BID_UINT32)((r).w[0]);
×
701
                    CYH = ((s_prime).w[3]) >> 32;
×
702
                    CYL = (BID_UINT32)((s_prime).w[3]);
×
703
                    PM = CXH * CYL;
×
704
                    PH = CXH * CYH;
×
705
                    PL = CXL * CYL;
×
706
                    PM2 = CXL * CYH;
×
707
                    PH += (PM >> 32);
×
708
                    PM = (BID_UINT64)((BID_UINT32)PM) + PM2 + (PL >> 32);
×
709
                    (lP3).w[1] = PH + (PM >> 32);
×
710
                    (lP3).w[0] = (PM << 32) + (BID_UINT32)PL;
×
711
                };
×
712
                (P0).w[0] = lP0.w[0];
×
713
                {
×
714
                    BID_UINT64 X1 = lP1.w[0];
×
715
                    (P0).w[1] = lP1.w[0] + lP0.w[1];
×
716
                    lC = ((P0).w[1] < X1) ? 1 : 0;
×
717
                };
×
718
                {
×
719
                    BID_UINT64 X1;
×
720
                    X1 = lP2.w[0] + lC;
×
721
                    (P0).w[2] = X1 + lP1.w[1];
×
722
                    lC = (((P0).w[2] < X1) || (X1 < lC)) ? 1 : 0;
×
723
                };
×
724
                {
×
725
                    BID_UINT64 X1;
×
726
                    X1 = lP3.w[0] + lC;
×
727
                    (P0).w[3] = X1 + lP2.w[1];
×
728
                    lC = (((P0).w[3] < X1) || (X1 < lC)) ? 1 : 0;
×
729
                };
×
730
                (P0).w[4] = lP3.w[1] + lC;
×
731
            };
×
732
            {
×
733
                BID_UINT128 lP0, lP1, lP2, lP3;
×
734
                BID_UINT64 lC;
×
735
                {
×
736
                    BID_UINT64 CXH, CXL, CYH, CYL, PL, PH, PM, PM2;
×
737
                    CXH = ((r).w[1]) >> 32;
×
738
                    CXL = (BID_UINT32)((r).w[1]);
×
739
                    CYH = ((s_prime).w[0]) >> 32;
×
740
                    CYL = (BID_UINT32)((s_prime).w[0]);
×
741
                    PM = CXH * CYL;
×
742
                    PH = CXH * CYH;
×
743
                    PL = CXL * CYL;
×
744
                    PM2 = CXL * CYH;
×
745
                    PH += (PM >> 32);
×
746
                    PM = (BID_UINT64)((BID_UINT32)PM) + PM2 + (PL >> 32);
×
747
                    (lP0).w[1] = PH + (PM >> 32);
×
748
                    (lP0).w[0] = (PM << 32) + (BID_UINT32)PL;
×
749
                };
×
750
                {
×
751
                    BID_UINT64 CXH, CXL, CYH, CYL, PL, PH, PM, PM2;
×
752
                    CXH = ((r).w[1]) >> 32;
×
753
                    CXL = (BID_UINT32)((r).w[1]);
×
754
                    CYH = ((s_prime).w[1]) >> 32;
×
755
                    CYL = (BID_UINT32)((s_prime).w[1]);
×
756
                    PM = CXH * CYL;
×
757
                    PH = CXH * CYH;
×
758
                    PL = CXL * CYL;
×
759
                    PM2 = CXL * CYH;
×
760
                    PH += (PM >> 32);
×
761
                    PM = (BID_UINT64)((BID_UINT32)PM) + PM2 + (PL >> 32);
×
762
                    (lP1).w[1] = PH + (PM >> 32);
×
763
                    (lP1).w[0] = (PM << 32) + (BID_UINT32)PL;
×
764
                };
×
765
                {
×
766
                    BID_UINT64 CXH, CXL, CYH, CYL, PL, PH, PM, PM2;
×
767
                    CXH = ((r).w[1]) >> 32;
×
768
                    CXL = (BID_UINT32)((r).w[1]);
×
769
                    CYH = ((s_prime).w[2]) >> 32;
×
770
                    CYL = (BID_UINT32)((s_prime).w[2]);
×
771
                    PM = CXH * CYL;
×
772
                    PH = CXH * CYH;
×
773
                    PL = CXL * CYL;
×
774
                    PM2 = CXL * CYH;
×
775
                    PH += (PM >> 32);
×
776
                    PM = (BID_UINT64)((BID_UINT32)PM) + PM2 + (PL >> 32);
×
777
                    (lP2).w[1] = PH + (PM >> 32);
×
778
                    (lP2).w[0] = (PM << 32) + (BID_UINT32)PL;
×
779
                };
×
780
                {
×
781
                    BID_UINT64 CXH, CXL, CYH, CYL, PL, PH, PM, PM2;
×
782
                    CXH = ((r).w[1]) >> 32;
×
783
                    CXL = (BID_UINT32)((r).w[1]);
×
784
                    CYH = ((s_prime).w[3]) >> 32;
×
785
                    CYL = (BID_UINT32)((s_prime).w[3]);
×
786
                    PM = CXH * CYL;
×
787
                    PH = CXH * CYH;
×
788
                    PL = CXL * CYL;
×
789
                    PM2 = CXL * CYH;
×
790
                    PH += (PM >> 32);
×
791
                    PM = (BID_UINT64)((BID_UINT32)PM) + PM2 + (PL >> 32);
×
792
                    (lP3).w[1] = PH + (PM >> 32);
×
793
                    (lP3).w[0] = (PM << 32) + (BID_UINT32)PL;
×
794
                };
×
795
                (P1).w[0] = lP0.w[0];
×
796
                {
×
797
                    BID_UINT64 X1 = lP1.w[0];
×
798
                    (P1).w[1] = lP1.w[0] + lP0.w[1];
×
799
                    lC = ((P1).w[1] < X1) ? 1 : 0;
×
800
                };
×
801
                {
×
802
                    BID_UINT64 X1;
×
803
                    X1 = lP2.w[0] + lC;
×
804
                    (P1).w[2] = X1 + lP1.w[1];
×
805
                    lC = (((P1).w[2] < X1) || (X1 < lC)) ? 1 : 0;
×
806
                };
×
807
                {
×
808
                    BID_UINT64 X1;
×
809
                    X1 = lP3.w[0] + lC;
×
810
                    (P1).w[3] = X1 + lP2.w[1];
×
811
                    lC = (((P1).w[3] < X1) || (X1 < lC)) ? 1 : 0;
×
812
                };
×
813
                (P1).w[4] = lP3.w[1] + lC;
×
814
            };
×
815
            {
×
816
                BID_UINT128 lP0, lP1, lP2, lP3;
×
817
                BID_UINT64 lC;
×
818
                {
×
819
                    BID_UINT64 CXH, CXL, CYH, CYL, PL, PH, PM, PM2;
×
820
                    CXH = ((r).w[2]) >> 32;
×
821
                    CXL = (BID_UINT32)((r).w[2]);
×
822
                    CYH = ((s_prime).w[0]) >> 32;
×
823
                    CYL = (BID_UINT32)((s_prime).w[0]);
×
824
                    PM = CXH * CYL;
×
825
                    PH = CXH * CYH;
×
826
                    PL = CXL * CYL;
×
827
                    PM2 = CXL * CYH;
×
828
                    PH += (PM >> 32);
×
829
                    PM = (BID_UINT64)((BID_UINT32)PM) + PM2 + (PL >> 32);
×
830
                    (lP0).w[1] = PH + (PM >> 32);
×
831
                    (lP0).w[0] = (PM << 32) + (BID_UINT32)PL;
×
832
                };
×
833
                {
×
834
                    BID_UINT64 CXH, CXL, CYH, CYL, PL, PH, PM, PM2;
×
835
                    CXH = ((r).w[2]) >> 32;
×
836
                    CXL = (BID_UINT32)((r).w[2]);
×
837
                    CYH = ((s_prime).w[1]) >> 32;
×
838
                    CYL = (BID_UINT32)((s_prime).w[1]);
×
839
                    PM = CXH * CYL;
×
840
                    PH = CXH * CYH;
×
841
                    PL = CXL * CYL;
×
842
                    PM2 = CXL * CYH;
×
843
                    PH += (PM >> 32);
×
844
                    PM = (BID_UINT64)((BID_UINT32)PM) + PM2 + (PL >> 32);
×
845
                    (lP1).w[1] = PH + (PM >> 32);
×
846
                    (lP1).w[0] = (PM << 32) + (BID_UINT32)PL;
×
847
                };
×
848
                {
×
849
                    BID_UINT64 CXH, CXL, CYH, CYL, PL, PH, PM, PM2;
×
850
                    CXH = ((r).w[2]) >> 32;
×
851
                    CXL = (BID_UINT32)((r).w[2]);
×
852
                    CYH = ((s_prime).w[2]) >> 32;
×
853
                    CYL = (BID_UINT32)((s_prime).w[2]);
×
854
                    PM = CXH * CYL;
×
855
                    PH = CXH * CYH;
×
856
                    PL = CXL * CYL;
×
857
                    PM2 = CXL * CYH;
×
858
                    PH += (PM >> 32);
×
859
                    PM = (BID_UINT64)((BID_UINT32)PM) + PM2 + (PL >> 32);
×
860
                    (lP2).w[1] = PH + (PM >> 32);
×
861
                    (lP2).w[0] = (PM << 32) + (BID_UINT32)PL;
×
862
                };
×
863
                {
×
864
                    BID_UINT64 CXH, CXL, CYH, CYL, PL, PH, PM, PM2;
×
865
                    CXH = ((r).w[2]) >> 32;
×
866
                    CXL = (BID_UINT32)((r).w[2]);
×
867
                    CYH = ((s_prime).w[3]) >> 32;
×
868
                    CYL = (BID_UINT32)((s_prime).w[3]);
×
869
                    PM = CXH * CYL;
×
870
                    PH = CXH * CYH;
×
871
                    PL = CXL * CYL;
×
872
                    PM2 = CXL * CYH;
×
873
                    PH += (PM >> 32);
×
874
                    PM = (BID_UINT64)((BID_UINT32)PM) + PM2 + (PL >> 32);
×
875
                    (lP3).w[1] = PH + (PM >> 32);
×
876
                    (lP3).w[0] = (PM << 32) + (BID_UINT32)PL;
×
877
                };
×
878
                (P2).w[0] = lP0.w[0];
×
879
                {
×
880
                    BID_UINT64 X1 = lP1.w[0];
×
881
                    (P2).w[1] = lP1.w[0] + lP0.w[1];
×
882
                    lC = ((P2).w[1] < X1) ? 1 : 0;
×
883
                };
×
884
                {
×
885
                    BID_UINT64 X1;
×
886
                    X1 = lP2.w[0] + lC;
×
887
                    (P2).w[2] = X1 + lP1.w[1];
×
888
                    lC = (((P2).w[2] < X1) || (X1 < lC)) ? 1 : 0;
×
889
                };
×
890
                {
×
891
                    BID_UINT64 X1;
×
892
                    X1 = lP3.w[0] + lC;
×
893
                    (P2).w[3] = X1 + lP2.w[1];
×
894
                    lC = (((P2).w[3] < X1) || (X1 < lC)) ? 1 : 0;
×
895
                };
×
896
                (P2).w[4] = lP3.w[1] + lC;
×
897
            };
×
898
            {
×
899
                BID_UINT128 lP0, lP1, lP2, lP3;
×
900
                BID_UINT64 lC;
×
901
                {
×
902
                    BID_UINT64 CXH, CXL, CYH, CYL, PL, PH, PM, PM2;
×
903
                    CXH = ((r).w[3]) >> 32;
×
904
                    CXL = (BID_UINT32)((r).w[3]);
×
905
                    CYH = ((s_prime).w[0]) >> 32;
×
906
                    CYL = (BID_UINT32)((s_prime).w[0]);
×
907
                    PM = CXH * CYL;
×
908
                    PH = CXH * CYH;
×
909
                    PL = CXL * CYL;
×
910
                    PM2 = CXL * CYH;
×
911
                    PH += (PM >> 32);
×
912
                    PM = (BID_UINT64)((BID_UINT32)PM) + PM2 + (PL >> 32);
×
913
                    (lP0).w[1] = PH + (PM >> 32);
×
914
                    (lP0).w[0] = (PM << 32) + (BID_UINT32)PL;
×
915
                };
×
916
                {
×
917
                    BID_UINT64 CXH, CXL, CYH, CYL, PL, PH, PM, PM2;
×
918
                    CXH = ((r).w[3]) >> 32;
×
919
                    CXL = (BID_UINT32)((r).w[3]);
×
920
                    CYH = ((s_prime).w[1]) >> 32;
×
921
                    CYL = (BID_UINT32)((s_prime).w[1]);
×
922
                    PM = CXH * CYL;
×
923
                    PH = CXH * CYH;
×
924
                    PL = CXL * CYL;
×
925
                    PM2 = CXL * CYH;
×
926
                    PH += (PM >> 32);
×
927
                    PM = (BID_UINT64)((BID_UINT32)PM) + PM2 + (PL >> 32);
×
928
                    (lP1).w[1] = PH + (PM >> 32);
×
929
                    (lP1).w[0] = (PM << 32) + (BID_UINT32)PL;
×
930
                };
×
931
                {
×
932
                    BID_UINT64 CXH, CXL, CYH, CYL, PL, PH, PM, PM2;
×
933
                    CXH = ((r).w[3]) >> 32;
×
934
                    CXL = (BID_UINT32)((r).w[3]);
×
935
                    CYH = ((s_prime).w[2]) >> 32;
×
936
                    CYL = (BID_UINT32)((s_prime).w[2]);
×
937
                    PM = CXH * CYL;
×
938
                    PH = CXH * CYH;
×
939
                    PL = CXL * CYL;
×
940
                    PM2 = CXL * CYH;
×
941
                    PH += (PM >> 32);
×
942
                    PM = (BID_UINT64)((BID_UINT32)PM) + PM2 + (PL >> 32);
×
943
                    (lP2).w[1] = PH + (PM >> 32);
×
944
                    (lP2).w[0] = (PM << 32) + (BID_UINT32)PL;
×
945
                };
×
946
                {
×
947
                    BID_UINT64 CXH, CXL, CYH, CYL, PL, PH, PM, PM2;
×
948
                    CXH = ((r).w[3]) >> 32;
×
949
                    CXL = (BID_UINT32)((r).w[3]);
×
950
                    CYH = ((s_prime).w[3]) >> 32;
×
951
                    CYL = (BID_UINT32)((s_prime).w[3]);
×
952
                    PM = CXH * CYL;
×
953
                    PH = CXH * CYH;
×
954
                    PL = CXL * CYL;
×
955
                    PM2 = CXL * CYH;
×
956
                    PH += (PM >> 32);
×
957
                    PM = (BID_UINT64)((BID_UINT32)PM) + PM2 + (PL >> 32);
×
958
                    (lP3).w[1] = PH + (PM >> 32);
×
959
                    (lP3).w[0] = (PM << 32) + (BID_UINT32)PL;
×
960
                };
×
961
                (P3).w[0] = lP0.w[0];
×
962
                {
×
963
                    BID_UINT64 X1 = lP1.w[0];
×
964
                    (P3).w[1] = lP1.w[0] + lP0.w[1];
×
965
                    lC = ((P3).w[1] < X1) ? 1 : 0;
×
966
                };
×
967
                {
×
968
                    BID_UINT64 X1;
×
969
                    X1 = lP2.w[0] + lC;
×
970
                    (P3).w[2] = X1 + lP1.w[1];
×
971
                    lC = (((P3).w[2] < X1) || (X1 < lC)) ? 1 : 0;
×
972
                };
×
973
                {
×
974
                    BID_UINT64 X1;
×
975
                    X1 = lP3.w[0] + lC;
×
976
                    (P3).w[3] = X1 + lP2.w[1];
×
977
                    lC = (((P3).w[3] < X1) || (X1 < lC)) ? 1 : 0;
×
978
                };
×
979
                (P3).w[4] = lP3.w[1] + lC;
×
980
            };
×
981
            (t_prime).w[0] = P0.w[0];
×
982
            {
×
983
                BID_UINT64 X1 = P1.w[0];
×
984
                (t_prime).w[1] = P1.w[0] + P0.w[1];
×
985
                CY = ((t_prime).w[1] < X1) ? 1 : 0;
×
986
            };
×
987
            {
×
988
                BID_UINT64 X1;
×
989
                X1 = P1.w[1] + CY;
×
990
                (t_prime).w[2] = X1 + P0.w[2];
×
991
                CY = (((t_prime).w[2] < X1) || (X1 < CY)) ? 1 : 0;
×
992
            };
×
993
            {
×
994
                BID_UINT64 X1;
×
995
                X1 = P1.w[2] + CY;
×
996
                (t_prime).w[3] = X1 + P0.w[3];
×
997
                CY = (((t_prime).w[3] < X1) || (X1 < CY)) ? 1 : 0;
×
998
            };
×
999
            {
×
1000
                BID_UINT64 X1;
×
1001
                X1 = P1.w[3] + CY;
×
1002
                (t_prime).w[4] = X1 + P0.w[4];
×
1003
                CY = (((t_prime).w[4] < X1) || (X1 < CY)) ? 1 : 0;
×
1004
            };
×
1005
            (t_prime).w[5] = P1.w[4] + CY;
×
1006
            {
×
1007
                BID_UINT64 X1 = P2.w[0];
×
1008
                (t_prime).w[2] = P2.w[0] + (t_prime).w[2];
×
1009
                CY = ((t_prime).w[2] < X1) ? 1 : 0;
×
1010
            };
×
1011
            {
×
1012
                BID_UINT64 X1;
×
1013
                X1 = P2.w[1] + CY;
×
1014
                (t_prime).w[3] = X1 + (t_prime).w[3];
×
1015
                CY = (((t_prime).w[3] < X1) || (X1 < CY)) ? 1 : 0;
×
1016
            };
×
1017
            {
×
1018
                BID_UINT64 X1;
×
1019
                X1 = P2.w[2] + CY;
×
1020
                (t_prime).w[4] = X1 + (t_prime).w[4];
×
1021
                CY = (((t_prime).w[4] < X1) || (X1 < CY)) ? 1 : 0;
×
1022
            };
×
1023
            {
×
1024
                BID_UINT64 X1;
×
1025
                X1 = P2.w[3] + CY;
×
1026
                (t_prime).w[5] = X1 + (t_prime).w[5];
×
1027
                CY = (((t_prime).w[5] < X1) || (X1 < CY)) ? 1 : 0;
×
1028
            };
×
1029
            (t_prime).w[6] = P2.w[4] + CY;
×
1030
            {
×
1031
                BID_UINT64 X1 = P3.w[0];
×
1032
                (t_prime).w[3] = P3.w[0] + (t_prime).w[3];
×
1033
                CY = ((t_prime).w[3] < X1) ? 1 : 0;
×
1034
            };
×
1035
            {
×
1036
                BID_UINT64 X1;
×
1037
                X1 = P3.w[1] + CY;
×
1038
                (t_prime).w[4] = X1 + (t_prime).w[4];
×
1039
                CY = (((t_prime).w[4] < X1) || (X1 < CY)) ? 1 : 0;
×
1040
            };
×
1041
            {
×
1042
                BID_UINT64 X1;
×
1043
                X1 = P3.w[2] + CY;
×
1044
                (t_prime).w[5] = X1 + (t_prime).w[5];
×
1045
                CY = (((t_prime).w[5] < X1) || (X1 < CY)) ? 1 : 0;
×
1046
            };
×
1047
            {
×
1048
                BID_UINT64 X1;
×
1049
                X1 = P3.w[3] + CY;
×
1050
                (t_prime).w[6] = X1 + (t_prime).w[6];
×
1051
                CY = (((t_prime).w[6] < X1) || (X1 < CY)) ? 1 : 0;
×
1052
            };
×
1053
            (t_prime).w[7] = P3.w[4] + CY;
×
1054
        };
×
1055
        r.w[0] = t_prime.w[4] + 1, r.w[1] = t_prime.w[5], r.w[2] = t_prime.w[6], r.w[3] = t_prime.w[7];
×
1056
    }
×
1057

3,879✔
1058
    {
7,854✔
1059
        BID_UINT512 P0, P1;
7,854✔
1060
        BID_UINT64 CY;
7,854✔
1061
        {
7,854✔
1062
            BID_UINT128 lP0, lP1, lP2, lP3;
7,854✔
1063
            BID_UINT64 lC;
7,854✔
1064
            {
7,854✔
1065
                BID_UINT64 CXH, CXL, CYH, CYL, PL, PH, PM, PM2;
7,854✔
1066
                CXH = ((c).w[0]) >> 32;
7,854✔
1067
                CXL = (BID_UINT32)((c).w[0]);
7,854✔
1068
                CYH = ((r).w[0]) >> 32;
7,854✔
1069
                CYL = (BID_UINT32)((r).w[0]);
7,854✔
1070
                PM = CXH * CYL;
7,854✔
1071
                PH = CXH * CYH;
7,854✔
1072
                PL = CXL * CYL;
7,854✔
1073
                PM2 = CXL * CYH;
7,854✔
1074
                PH += (PM >> 32);
7,854✔
1075
                PM = (BID_UINT64)((BID_UINT32)PM) + PM2 + (PL >> 32);
7,854✔
1076
                (lP0).w[1] = PH + (PM >> 32);
7,854✔
1077
                (lP0).w[0] = (PM << 32) + (BID_UINT32)PL;
7,854✔
1078
            };
7,854✔
1079
            {
7,854✔
1080
                BID_UINT64 CXH, CXL, CYH, CYL, PL, PH, PM, PM2;
7,854✔
1081
                CXH = ((c).w[0]) >> 32;
7,854✔
1082
                CXL = (BID_UINT32)((c).w[0]);
7,854✔
1083
                CYH = ((r).w[1]) >> 32;
7,854✔
1084
                CYL = (BID_UINT32)((r).w[1]);
7,854✔
1085
                PM = CXH * CYL;
7,854✔
1086
                PH = CXH * CYH;
7,854✔
1087
                PL = CXL * CYL;
7,854✔
1088
                PM2 = CXL * CYH;
7,854✔
1089
                PH += (PM >> 32);
7,854✔
1090
                PM = (BID_UINT64)((BID_UINT32)PM) + PM2 + (PL >> 32);
7,854✔
1091
                (lP1).w[1] = PH + (PM >> 32);
7,854✔
1092
                (lP1).w[0] = (PM << 32) + (BID_UINT32)PL;
7,854✔
1093
            };
7,854✔
1094
            {
7,854✔
1095
                BID_UINT64 CXH, CXL, CYH, CYL, PL, PH, PM, PM2;
7,854✔
1096
                CXH = ((c).w[0]) >> 32;
7,854✔
1097
                CXL = (BID_UINT32)((c).w[0]);
7,854✔
1098
                CYH = ((r).w[2]) >> 32;
7,854✔
1099
                CYL = (BID_UINT32)((r).w[2]);
7,854✔
1100
                PM = CXH * CYL;
7,854✔
1101
                PH = CXH * CYH;
7,854✔
1102
                PL = CXL * CYL;
7,854✔
1103
                PM2 = CXL * CYH;
7,854✔
1104
                PH += (PM >> 32);
7,854✔
1105
                PM = (BID_UINT64)((BID_UINT32)PM) + PM2 + (PL >> 32);
7,854✔
1106
                (lP2).w[1] = PH + (PM >> 32);
7,854✔
1107
                (lP2).w[0] = (PM << 32) + (BID_UINT32)PL;
7,854✔
1108
            };
7,854✔
1109
            {
7,854✔
1110
                BID_UINT64 CXH, CXL, CYH, CYL, PL, PH, PM, PM2;
7,854✔
1111
                CXH = ((c).w[0]) >> 32;
7,854✔
1112
                CXL = (BID_UINT32)((c).w[0]);
7,854✔
1113
                CYH = ((r).w[3]) >> 32;
7,854✔
1114
                CYL = (BID_UINT32)((r).w[3]);
7,854✔
1115
                PM = CXH * CYL;
7,854✔
1116
                PH = CXH * CYH;
7,854✔
1117
                PL = CXL * CYL;
7,854✔
1118
                PM2 = CXL * CYH;
7,854✔
1119
                PH += (PM >> 32);
7,854✔
1120
                PM = (BID_UINT64)((BID_UINT32)PM) + PM2 + (PL >> 32);
7,854✔
1121
                (lP3).w[1] = PH + (PM >> 32);
7,854✔
1122
                (lP3).w[0] = (PM << 32) + (BID_UINT32)PL;
7,854✔
1123
            };
7,854✔
1124
            (P0).w[0] = lP0.w[0];
7,854✔
1125
            {
7,854✔
1126
                BID_UINT64 X1 = lP1.w[0];
7,854✔
1127
                (P0).w[1] = lP1.w[0] + lP0.w[1];
7,854✔
1128
                lC = ((P0).w[1] < X1) ? 1 : 0;
7,854✔
1129
            };
7,854✔
1130
            {
7,854✔
1131
                BID_UINT64 X1;
7,854✔
1132
                X1 = lP2.w[0] + lC;
7,854✔
1133
                (P0).w[2] = X1 + lP1.w[1];
7,854✔
1134
                lC = (((P0).w[2] < X1) || (X1 < lC)) ? 1 : 0;
7,854✔
1135
            };
7,854✔
1136
            {
7,854✔
1137
                BID_UINT64 X1;
7,854✔
1138
                X1 = lP3.w[0] + lC;
7,854✔
1139
                (P0).w[3] = X1 + lP2.w[1];
7,854✔
1140
                lC = (((P0).w[3] < X1) || (X1 < lC)) ? 1 : 0;
7,854✔
1141
            };
7,854✔
1142
            (P0).w[4] = lP3.w[1] + lC;
7,854✔
1143
        };
7,854✔
1144
        {
7,854✔
1145
            BID_UINT128 lP0, lP1, lP2, lP3;
7,854✔
1146
            BID_UINT64 lC;
7,854✔
1147
            {
7,854✔
1148
                BID_UINT64 CXH, CXL, CYH, CYL, PL, PH, PM, PM2;
7,854✔
1149
                CXH = ((c).w[1]) >> 32;
7,854✔
1150
                CXL = (BID_UINT32)((c).w[1]);
7,854✔
1151
                CYH = ((r).w[0]) >> 32;
7,854✔
1152
                CYL = (BID_UINT32)((r).w[0]);
7,854✔
1153
                PM = CXH * CYL;
7,854✔
1154
                PH = CXH * CYH;
7,854✔
1155
                PL = CXL * CYL;
7,854✔
1156
                PM2 = CXL * CYH;
7,854✔
1157
                PH += (PM >> 32);
7,854✔
1158
                PM = (BID_UINT64)((BID_UINT32)PM) + PM2 + (PL >> 32);
7,854✔
1159
                (lP0).w[1] = PH + (PM >> 32);
7,854✔
1160
                (lP0).w[0] = (PM << 32) + (BID_UINT32)PL;
7,854✔
1161
            };
7,854✔
1162
            {
7,854✔
1163
                BID_UINT64 CXH, CXL, CYH, CYL, PL, PH, PM, PM2;
7,854✔
1164
                CXH = ((c).w[1]) >> 32;
7,854✔
1165
                CXL = (BID_UINT32)((c).w[1]);
7,854✔
1166
                CYH = ((r).w[1]) >> 32;
7,854✔
1167
                CYL = (BID_UINT32)((r).w[1]);
7,854✔
1168
                PM = CXH * CYL;
7,854✔
1169
                PH = CXH * CYH;
7,854✔
1170
                PL = CXL * CYL;
7,854✔
1171
                PM2 = CXL * CYH;
7,854✔
1172
                PH += (PM >> 32);
7,854✔
1173
                PM = (BID_UINT64)((BID_UINT32)PM) + PM2 + (PL >> 32);
7,854✔
1174
                (lP1).w[1] = PH + (PM >> 32);
7,854✔
1175
                (lP1).w[0] = (PM << 32) + (BID_UINT32)PL;
7,854✔
1176
            };
7,854✔
1177
            {
7,854✔
1178
                BID_UINT64 CXH, CXL, CYH, CYL, PL, PH, PM, PM2;
7,854✔
1179
                CXH = ((c).w[1]) >> 32;
7,854✔
1180
                CXL = (BID_UINT32)((c).w[1]);
7,854✔
1181
                CYH = ((r).w[2]) >> 32;
7,854✔
1182
                CYL = (BID_UINT32)((r).w[2]);
7,854✔
1183
                PM = CXH * CYL;
7,854✔
1184
                PH = CXH * CYH;
7,854✔
1185
                PL = CXL * CYL;
7,854✔
1186
                PM2 = CXL * CYH;
7,854✔
1187
                PH += (PM >> 32);
7,854✔
1188
                PM = (BID_UINT64)((BID_UINT32)PM) + PM2 + (PL >> 32);
7,854✔
1189
                (lP2).w[1] = PH + (PM >> 32);
7,854✔
1190
                (lP2).w[0] = (PM << 32) + (BID_UINT32)PL;
7,854✔
1191
            };
7,854✔
1192
            {
7,854✔
1193
                BID_UINT64 CXH, CXL, CYH, CYL, PL, PH, PM, PM2;
7,854✔
1194
                CXH = ((c).w[1]) >> 32;
7,854✔
1195
                CXL = (BID_UINT32)((c).w[1]);
7,854✔
1196
                CYH = ((r).w[3]) >> 32;
7,854✔
1197
                CYL = (BID_UINT32)((r).w[3]);
7,854✔
1198
                PM = CXH * CYL;
7,854✔
1199
                PH = CXH * CYH;
7,854✔
1200
                PL = CXL * CYL;
7,854✔
1201
                PM2 = CXL * CYH;
7,854✔
1202
                PH += (PM >> 32);
7,854✔
1203
                PM = (BID_UINT64)((BID_UINT32)PM) + PM2 + (PL >> 32);
7,854✔
1204
                (lP3).w[1] = PH + (PM >> 32);
7,854✔
1205
                (lP3).w[0] = (PM << 32) + (BID_UINT32)PL;
7,854✔
1206
            };
7,854✔
1207
            (P1).w[0] = lP0.w[0];
7,854✔
1208
            {
7,854✔
1209
                BID_UINT64 X1 = lP1.w[0];
7,854✔
1210
                (P1).w[1] = lP1.w[0] + lP0.w[1];
7,854✔
1211
                lC = ((P1).w[1] < X1) ? 1 : 0;
7,854✔
1212
            };
7,854✔
1213
            {
7,854✔
1214
                BID_UINT64 X1;
7,854✔
1215
                X1 = lP2.w[0] + lC;
7,854✔
1216
                (P1).w[2] = X1 + lP1.w[1];
7,854✔
1217
                lC = (((P1).w[2] < X1) || (X1 < lC)) ? 1 : 0;
7,854✔
1218
            };
7,854✔
1219
            {
7,854✔
1220
                BID_UINT64 X1;
7,854✔
1221
                X1 = lP3.w[0] + lC;
7,854✔
1222
                (P1).w[3] = X1 + lP2.w[1];
7,854✔
1223
                lC = (((P1).w[3] < X1) || (X1 < lC)) ? 1 : 0;
7,854✔
1224
            };
7,854✔
1225
            (P1).w[4] = lP3.w[1] + lC;
7,854✔
1226
        };
7,854✔
1227
        (z).w[0] = P0.w[0];
7,854✔
1228
        {
7,854✔
1229
            BID_UINT64 X1 = P1.w[0];
7,854✔
1230
            (z).w[1] = P1.w[0] + P0.w[1];
7,854✔
1231
            CY = ((z).w[1] < X1) ? 1 : 0;
7,854✔
1232
        };
7,854✔
1233
        {
7,854✔
1234
            BID_UINT64 X1;
7,854✔
1235
            X1 = P1.w[1] + CY;
7,854✔
1236
            (z).w[2] = X1 + P0.w[2];
7,854✔
1237
            CY = (((z).w[2] < X1) || (X1 < CY)) ? 1 : 0;
7,854✔
1238
        };
7,854✔
1239
        {
7,854✔
1240
            BID_UINT64 X1;
7,854✔
1241
            X1 = P1.w[2] + CY;
7,854✔
1242
            (z).w[3] = X1 + P0.w[3];
7,854✔
1243
            CY = (((z).w[3] < X1) || (X1 < CY)) ? 1 : 0;
7,854✔
1244
        };
7,854✔
1245
        {
7,854✔
1246
            BID_UINT64 X1;
7,854✔
1247
            X1 = P1.w[3] + CY;
7,854✔
1248
            (z).w[4] = X1 + P0.w[4];
7,854✔
1249
            CY = (((z).w[4] < X1) || (X1 < CY)) ? 1 : 0;
7,854✔
1250
        };
7,854✔
1251
        (z).w[5] = P1.w[4] + CY;
7,854✔
1252
    };
7,854✔
1253

3,879✔
1254
    // Make adjustive shift, ignoring the lower 128 bits
3,879✔
1255
    e = -(241 + e + f);
7,854✔
1256
    ((z.w[0]) = ((z.w[1]) << (64 - (e))) + ((z.w[0]) >> (e)), (z.w[1]) = ((z.w[2]) << (64 - (e))) + ((z.w[1]) >> (e)),
7,854✔
1257
     (z.w[2]) = ((z.w[3]) << (64 - (e))) + ((z.w[2]) >> (e)), (z.w[3]) = ((z.w[4]) << (64 - (e))) + ((z.w[3]) >> (e)),
7,854✔
1258
     (z.w[4]) = ((z.w[5]) << (64 - (e))) + ((z.w[4]) >> (e)), (z.w[5]) = (z.w[5]) >> (e));
7,854✔
1259

3,879✔
1260
    // Now test against 10^33 and so decide on adjustment
3,879✔
1261
    // I feel there ought to be a smarter way of doing the multiplication
3,879✔
1262
    if ((((z.w[5]) < (54210108624275ull)) ||
7,854✔
1263
         (((z.w[5]) == (54210108624275ull)) && ((z.w[4]) < (4089650035136921600ull))))) {
7,830✔
1264
        {
60✔
1265
            unsigned long long c0, c1, c2, c3, c4, c5;
60✔
1266
            {
60✔
1267
                unsigned long long s3 = (z.w[0]) + ((z.w[0]) >> 2);
60✔
1268
                (c0) = ((s3 < (unsigned long long)(z.w[0])) << 3) + (s3 >> 61);
60✔
1269
                s3 = (s3 << 3) + ((z.w[0] & 3) << 1);
60✔
1270
                (z.w[0]) = s3 + (0ull);
60✔
1271
                if ((unsigned long long)(z.w[0]) < s3)
60✔
1272
                    ++(c0);
×
1273
            };
60✔
1274
            {
60✔
1275
                unsigned long long s3 = (z.w[1]) + ((z.w[1]) >> 2);
60✔
1276
                (c1) = ((s3 < (unsigned long long)(z.w[1])) << 3) + (s3 >> 61);
60✔
1277
                s3 = (s3 << 3) + ((z.w[1] & 3) << 1);
60✔
1278
                (z.w[1]) = s3 + (c0);
60✔
1279
                if ((unsigned long long)(z.w[1]) < s3)
60✔
1280
                    ++(c1);
×
1281
            };
60✔
1282
            {
60✔
1283
                unsigned long long s3 = (z.w[2]) + ((z.w[2]) >> 2);
60✔
1284
                (c2) = ((s3 < (unsigned long long)(z.w[2])) << 3) + (s3 >> 61);
60✔
1285
                s3 = (s3 << 3) + ((z.w[2] & 3) << 1);
60✔
1286
                (z.w[2]) = s3 + (c1);
60✔
1287
                if ((unsigned long long)(z.w[2]) < s3)
60✔
1288
                    ++(c2);
×
1289
            };
60✔
1290
            {
60✔
1291
                unsigned long long s3 = (z.w[3]) + ((z.w[3]) >> 2);
60✔
1292
                (c3) = ((s3 < (unsigned long long)(z.w[3])) << 3) + (s3 >> 61);
60✔
1293
                s3 = (s3 << 3) + ((z.w[3] & 3) << 1);
60✔
1294
                (z.w[3]) = s3 + (c2);
60✔
1295
                if ((unsigned long long)(z.w[3]) < s3)
60✔
1296
                    ++(c3);
×
1297
            };
60✔
1298
            {
60✔
1299
                unsigned long long s3 = (z.w[4]) + ((z.w[4]) >> 2);
60✔
1300
                (c4) = ((s3 < (unsigned long long)(z.w[4])) << 3) + (s3 >> 61);
60✔
1301
                s3 = (s3 << 3) + ((z.w[4] & 3) << 1);
60✔
1302
                (z.w[4]) = s3 + (c3);
60✔
1303
                if ((unsigned long long)(z.w[4]) < s3)
60✔
1304
                    ++(c4);
×
1305
            };
60✔
1306
            {
60✔
1307
                unsigned long long s3 = (z.w[5]) + ((z.w[5]) >> 2);
60✔
1308
                (c5) = ((s3 < (unsigned long long)(z.w[5])) << 3) + (s3 >> 61);
60✔
1309
                s3 = (s3 << 3) + ((z.w[5] & 3) << 1);
60✔
1310
                (z.w[5]) = s3 + (c4);
60✔
1311
                if ((unsigned long long)(z.w[5]) < s3)
60✔
1312
                    ++(c5);
×
1313
            };
60✔
1314
        };
60✔
1315
        e_out = e_out - 1;
60✔
1316
    }
60✔
1317

3,879✔
1318
    // Set up provisional results
3,879✔
1319
    c_prov_hi = z.w[5];
7,854✔
1320
    c_prov_lo = z.w[4];
7,854✔
1321

3,879✔
1322
    // Round using round-sticky words
3,879✔
1323
    // If we spill over into the next decade, correct
3,879✔
1324
    if ((((bid_roundbound_128[(__bid_IDEC_glbround << 2) + ((s & 1) << 1) + (c_prov_lo & 1)].w[1]) < (z.w[3])) ||
7,854✔
1325
         (((bid_roundbound_128[(__bid_IDEC_glbround << 2) + ((s & 1) << 1) + (c_prov_lo & 1)].w[1]) == (z.w[3])) &&
7,713✔
1326
          ((bid_roundbound_128[(__bid_IDEC_glbround << 2) + ((s & 1) << 1) + (c_prov_lo & 1)].w[0]) < (z.w[2]))))
3,738!
1327

141✔
1328

141✔
1329
    ) {
282✔
1330
        c_prov_lo = c_prov_lo + 1;
282✔
1331
        if (c_prov_lo == 0)
282✔
1332
            c_prov_hi = c_prov_hi + 1;
×
1333
        else if ((c_prov_lo == 4003012203950112768ull) && (c_prov_hi == 542101086242752ull)) {
282!
1334
            c_prov_hi = 54210108624275ull;
×
1335
            c_prov_lo = 4089650035136921600ull;
×
1336
            e_out = e_out + 1;
×
1337
        }
×
1338
    }
282✔
1339

3,879✔
1340
    // Don't need to check overflow or underflow; however set inexact flag
3,879✔
1341
    if ((z.w[3] != 0) || (z.w[2] != 0))
7,854!
1342
        *(pfpsf) |= 0x20;
7,854✔
1343

3,879✔
1344
    // Package up the result
3,879✔
1345
    {
7,854✔
1346
        BID_UINT128 x_out;
7,854✔
1347
        x_out.w[0] = c_prov_lo;
7,854✔
1348
        x_out.w[1] = ((BID_UINT64)(s) << 63) + ((BID_UINT64)(e_out) << 49) + (c_prov_hi);
7,854✔
1349
        *pres = x_out;
7,854✔
1350
        return;
7,854✔
1351
    };
12,819✔
1352
}
×
1353

1354
} // namespace
1355

1356
/*******************************************************************************************************************/
1357

1358
Decimal128::Decimal128(double val, RoundTo rounding_precision) noexcept
1359
{
26,139✔
1360
    // The logic below is ported from MongoDB Decimal128 implementation
13,179✔
1361
    uint64_t largest_coeff = (rounding_precision == RoundTo::Digits7) ? 9999999 : 999999999999999;
20,847✔
1362
    unsigned flags = 0;
26,139✔
1363
    BID_UINT128 converted_value;
26,139✔
1364
    realm_binary64_to_bid128(&converted_value, &val, &flags);
26,139✔
1365
    memcpy(this, &converted_value, sizeof(*this));
26,139✔
1366

13,179✔
1367
    // If the precision is already less than 15 decimals, or val is infinity or NaN, there's no need to quantize
13,179✔
1368
    if ((get_coefficient_low() <= largest_coeff && get_coefficient_high() == 0) || std::isinf(val) ||
26,139✔
1369
        std::isnan(val)) {
20,247✔
1370
        return;
11,781✔
1371
    }
11,781✔
1372

7,290✔
1373
    // Get the base2 exponent from val.
7,290✔
1374
    int base2Exp;
14,358✔
1375
    frexp(val, &base2Exp);
14,358✔
1376

7,290✔
1377
    // As frexp normalizes val between 0.5 and 1.0 rather than 1.0 and 2.0, adjust.
7,290✔
1378
    base2Exp--;
14,358✔
1379

7,290✔
1380

7,290✔
1381
    // We will use base10Exp = base2Exp * 30103 / (100*1000) as lowerbound (using integer division).
7,290✔
1382
    //
7,290✔
1383
    // This formula is derived from the following, with base2Exp the binary exponent of val:
7,290✔
1384
    //   (1) 10**(base2Exp * log10(2)) == 2**base2Exp
7,290✔
1385
    //   (2) 0.30103 closely approximates log10(2)
7,290✔
1386
    //
7,290✔
1387
    // Exhaustive testing using Python shows :
7,290✔
1388
    //     { base2Exp * 30103 / (100 * 1000) == math.floor(math.log10(2**base2Exp))
7,290✔
1389
    //       for base2Exp in xrange(-1074, 1023) } == { True }
7,290✔
1390
    int base10Exp = (base2Exp * 30103) / (100 * 1000);
14,358✔
1391

7,290✔
1392
    // As integer division truncates, rather than rounds down (as in Python), adjust accordingly.
7,290✔
1393
    if (base2Exp < 0)
14,358✔
1394
        base10Exp--;
36✔
1395

7,290✔
1396
    int adjust = (rounding_precision == RoundTo::Digits7) ? 6 : 14;
11,268✔
1397
    BID_UINT128 q{1, BID_UINT64(base10Exp - adjust + DECIMAL_EXPONENT_BIAS_128) << DECIMAL_COEFF_HIGH_BITS};
14,358✔
1398
    BID_UINT128 quantizedResult;
14,358✔
1399
    bid128_quantize(&quantizedResult, &converted_value, &q, &flags);
14,358✔
1400
    memcpy(this, &quantizedResult, sizeof(*this));
14,358✔
1401

7,290✔
1402
    // Check if the quantization was done correctly: m_valuem_value stores exactly 15
7,290✔
1403
    // decimal digits of precision (15 digits can fit into the low 64 bits of the decimal)
7,290✔
1404
    if (get_coefficient_low() > largest_coeff) {
14,358✔
1405
        // If we didn't precisely get 15 digits of precision, the original base 10 exponent
78✔
1406
        // guess was 1 off, so quantize once more with base10Exp + 1
78✔
1407
        adjust--;
156✔
1408
        BID_UINT128 q1{1, (BID_UINT64(base10Exp) - adjust + DECIMAL_EXPONENT_BIAS_128) << DECIMAL_COEFF_HIGH_BITS};
156✔
1409
        bid128_quantize(&quantizedResult, &converted_value, &q1, &flags);
156✔
1410
        memcpy(this, &quantizedResult, sizeof(*this));
156✔
1411
    }
156✔
1412
}
14,358✔
1413

1414
Decimal128::Decimal128(int val) noexcept
1415
    : Decimal128(static_cast<int64_t>(val))
1416
{
1,846,473✔
1417
}
1,846,473✔
1418

1419
Decimal128::Decimal128(int64_t val) noexcept
1420
{
1,884,822✔
1421
    constexpr uint64_t expon = uint64_t(DECIMAL_EXPONENT_BIAS_128) << DECIMAL_COEFF_HIGH_BITS;
1,884,822✔
1422
    if (val < 0) {
1,884,822✔
1423
        m_value.w[1] = expon | MASK_SIGN;
71,979✔
1424
        m_value.w[0] = val == std::numeric_limits<int64_t>::lowest() ? val : ~val + 1;
71,970✔
1425
    }
71,979✔
1426
    else {
1,812,843✔
1427
        m_value.w[1] = expon;
1,812,843✔
1428
        m_value.w[0] = val;
1,812,843✔
1429
    }
1,812,843✔
1430
}
1,884,822✔
1431

1432
Decimal128::Decimal128(uint64_t val) noexcept
1433
{
5,568✔
1434
    BID_UINT64 tmp(val);
5,568✔
1435
    BID_UINT128 expanded;
5,568✔
1436
    bid128_from_uint64(&expanded, &tmp);
5,568✔
1437
    memcpy(this, &expanded, sizeof(*this));
5,568✔
1438
}
5,568✔
1439

1440
Decimal128::Decimal128(Bid32 val) noexcept
1441
{
559,101✔
1442
    if (val.u == DECIMAL_NULL_32) {
559,101✔
1443
        m_value = DECIMAL_NULL_128;
19,272✔
1444
        return;
19,272✔
1445
    }
19,272✔
1446
    unsigned flags = 0;
539,829✔
1447
    BID_UINT32 x(val.u);
539,829✔
1448
    BID_UINT128 tmp;
539,829✔
1449
    bid32_to_bid128(&tmp, &x, &flags);
539,829✔
1450
    memcpy(this, &tmp, sizeof(*this));
539,829✔
1451
}
539,829✔
1452

1453
Decimal128::Decimal128(Bid64 val) noexcept
1454
{
858✔
1455
    if (val.w == DECIMAL_NULL_64) {
858✔
1456
        m_value = DECIMAL_NULL_128;
24✔
1457
        return;
24✔
1458
    }
24✔
1459
    unsigned flags = 0;
834✔
1460
    BID_UINT64 x(val.w);
834✔
1461
    BID_UINT128 tmp;
834✔
1462
    bid64_to_bid128(&tmp, &x, &flags);
834✔
1463
    memcpy(this, &tmp, sizeof(*this));
834✔
1464
}
834✔
1465

1466
Decimal128::Decimal128(Bid128 coefficient, int exponent, bool sign) noexcept
1467
{
432✔
1468
    uint64_t sign_x = sign ? MASK_SIGN : 0;
432✔
1469
    m_value = coefficient;
432✔
1470
    uint64_t tmp = exponent + DECIMAL_EXPONENT_BIAS_128;
432✔
1471
    m_value.w[1] |= (sign_x | (tmp << DECIMAL_COEFF_HIGH_BITS));
432✔
1472
}
432✔
1473

1474
Decimal128::Decimal128(StringData init) noexcept
1475
{
17,334✔
1476
    unsigned flags = 0;
17,334✔
1477
    BID_UINT128 tmp;
17,334✔
1478
    bid128_from_string(&tmp, const_cast<char*>(init.data()), &flags);
17,334✔
1479
    memcpy(this, &tmp, sizeof(*this));
17,334✔
1480
}
17,334✔
1481

1482
Decimal128::Decimal128(null) noexcept
1483
{
76,590✔
1484
    m_value.w[0] = 0xaa;
76,590✔
1485
    m_value.w[1] = 0x7c00000000000000;
76,590✔
1486
}
76,590✔
1487

1488
Decimal128 Decimal128::nan(const char* init) noexcept
1489
{
1,812✔
1490
    Bid128 val;
1,812✔
1491
    val.w[0] = strtol(init, nullptr, 10);
1,812✔
1492
    val.w[1] = 0x7c00000000000000ull;
1,812✔
1493
    return Decimal128(val);
1,812✔
1494
}
1,812✔
1495

1496
bool Decimal128::is_null() const noexcept
1497
{
5,335,866✔
1498
    return m_value.w[0] == 0xaa && m_value.w[1] == 0x7c00000000000000;
5,335,866✔
1499
}
5,335,866✔
1500

1501
bool Decimal128::is_nan() const noexcept
1502
{
2,242,782✔
1503
    return (m_value.w[1] & 0x7c00000000000000ull) == 0x7c00000000000000ull;
2,242,782✔
1504
}
2,242,782✔
1505

1506
bool Decimal128::to_int(int64_t& i) const noexcept
1507
{
72✔
1508
    BID_SINT64 res;
72✔
1509
    unsigned flags = 0;
72✔
1510
    BID_UINT128 x = to_BID_UINT128(*this);
72✔
1511
    bid128_to_int64_int(&res, &x, &flags);
72✔
1512
    if (flags == 0) {
72✔
1513
        i = res;
60✔
1514
        return true;
60✔
1515
    }
60✔
1516
    return false;
12✔
1517
}
12✔
1518

1519

1520
bool Decimal128::operator==(const Decimal128& rhs) const noexcept
1521
{
329,001✔
1522
    if (is_null() && rhs.is_null()) {
329,001✔
1523
        return true;
780✔
1524
    }
780✔
1525
    unsigned flags = 0;
328,221✔
1526
    int ret;
328,221✔
1527
    BID_UINT128 l = to_BID_UINT128(*this);
328,221✔
1528
    BID_UINT128 r = to_BID_UINT128(rhs);
328,221✔
1529
    bid128_quiet_equal(&ret, &l, &r, &flags);
328,221✔
1530
    if (ret) {
328,221✔
1531
        return true;
292,434✔
1532
    }
292,434✔
1533
    bool lhs_is_nan = is_nan();
35,787✔
1534
    bool rhs_is_nan = rhs.is_nan();
35,787✔
1535
    if (lhs_is_nan && rhs_is_nan) {
35,787✔
1536
        return m_value.w[1] == rhs.m_value.w[1] && m_value.w[0] == rhs.m_value.w[0];
6✔
1537
    }
6✔
1538
    return 0;
35,781✔
1539
}
35,781✔
1540

1541
bool Decimal128::operator!=(const Decimal128& rhs) const noexcept
1542
{
3,168✔
1543
    return !(*this == rhs);
3,168✔
1544
}
3,168✔
1545

1546
int Decimal128::compare(const Decimal128& rhs) const noexcept
1547
{
1,915,062✔
1548
    unsigned flags = 0;
1,915,062✔
1549
    int ret;
1,915,062✔
1550
    BID_UINT128 l = to_BID_UINT128(*this);
1,915,062✔
1551
    BID_UINT128 r = to_BID_UINT128(rhs);
1,915,062✔
1552
    bid128_quiet_less(&ret, &l, &r, &flags);
1,915,062✔
1553
    if (ret)
1,915,062✔
1554
        return -1;
868,521✔
1555
    bid128_quiet_greater(&ret, &l, &r, &flags);
1,046,541✔
1556
    if (ret)
1,046,541✔
1557
        return 1;
901,704✔
1558

72,780✔
1559
    // Either equal or one or more is NaN
72,780✔
1560
    bool lhs_is_nan = is_nan();
144,837✔
1561
    bool rhs_is_nan = rhs.is_nan();
144,837✔
1562
    if (!lhs_is_nan && !rhs_is_nan) {
144,837✔
1563
        // Neither is NaN
55,533✔
1564
        return 0;
110,631✔
1565
    }
110,631✔
1566
    if (lhs_is_nan && rhs_is_nan) {
34,206✔
1567
        // We should have stable sorting of NaN
9,753✔
1568
        if (m_value.w[1] == rhs.m_value.w[1]) {
19,542✔
1569
            return m_value.w[0] == rhs.m_value.w[0] ? 0 : m_value.w[0] < rhs.m_value.w[0] ? -1 : 1;
14,958✔
1570
        }
19,542✔
1571
        else {
×
1572
            return m_value.w[1] < rhs.m_value.w[1] ? -1 : 1;
×
1573
        }
×
1574
    }
14,664✔
1575
    // nan vs non-nan should always order nan first
7,494✔
1576
    return lhs_is_nan ? -1 : 1;
14,664✔
1577
}
14,664✔
1578

1579
bool Decimal128::operator<(const Decimal128& rhs) const noexcept
1580
{
747,537✔
1581
    return compare(rhs) < 0;
747,537✔
1582
}
747,537✔
1583

1584
bool Decimal128::operator>(const Decimal128& rhs) const noexcept
1585
{
698,601✔
1586
    return compare(rhs) > 0;
698,601✔
1587
}
698,601✔
1588

1589
bool Decimal128::operator<=(const Decimal128& rhs) const noexcept
1590
{
1,338✔
1591
    return compare(rhs) <= 0;
1,338✔
1592
}
1,338✔
1593

1594
bool Decimal128::operator>=(const Decimal128& rhs) const noexcept
1595
{
828✔
1596
    return compare(rhs) >= 0;
828✔
1597
}
828✔
1598

1599
static Decimal128 do_multiply(BID_UINT128 x, BID_UINT128 mul) noexcept
1600
{
48✔
1601
    unsigned flags = 0;
48✔
1602
    BID_UINT128 res;
48✔
1603
    bid128_mul(&res, &x, &mul, &flags);
48✔
1604
    return to_decimal128(res);
48✔
1605
}
48✔
1606

1607
Decimal128 Decimal128::operator*(int64_t mul) const noexcept
1608
{
12✔
1609
    BID_UINT128 x = to_BID_UINT128(*this);
12✔
1610
    BID_UINT128 y = to_BID_UINT128(Decimal128(mul));
12✔
1611
    return do_multiply(x, y);
12✔
1612
}
12✔
1613

1614
Decimal128 Decimal128::operator*(size_t mul) const noexcept
1615
{
6✔
1616
    Decimal128 tmp_mul(static_cast<uint64_t>(mul));
6✔
1617
    return do_multiply(to_BID_UINT128(*this), to_BID_UINT128(tmp_mul));
6✔
1618
}
6✔
1619

1620
Decimal128 Decimal128::operator*(int mul) const noexcept
1621
{
12✔
1622
    BID_UINT128 x = to_BID_UINT128(*this);
12✔
1623
    BID_UINT128 y = to_BID_UINT128(Decimal128(mul));
12✔
1624
    return do_multiply(x, y);
12✔
1625
}
12✔
1626

1627
Decimal128 Decimal128::operator*(Decimal128 mul) const noexcept
1628
{
18✔
1629
    BID_UINT128 x = to_BID_UINT128(*this);
18✔
1630
    BID_UINT128 y = to_BID_UINT128(mul);
18✔
1631
    return do_multiply(x, y);
18✔
1632
}
18✔
1633

1634
static Decimal128 do_divide(BID_UINT128 x, BID_UINT128 div)
1635
{
5,964✔
1636
    unsigned flags = 0;
5,964✔
1637
    BID_UINT128 res;
5,964✔
1638
    bid128_div(&res, &x, &div, &flags);
5,964✔
1639
    return to_decimal128(res);
5,964✔
1640
}
5,964✔
1641

1642
Decimal128 Decimal128::operator/(int64_t div) const noexcept
1643
{
24✔
1644
    BID_UINT128 x = to_BID_UINT128(*this);
24✔
1645
    BID_UINT128 y = to_BID_UINT128(Decimal128(div));
24✔
1646
    return do_divide(x, y);
24✔
1647
}
24✔
1648

1649
Decimal128 Decimal128::operator/(size_t div) const noexcept
1650
{
5,550✔
1651
    Decimal128 tmp_div(static_cast<uint64_t>(div));
5,550✔
1652
    return do_divide(to_BID_UINT128(*this), to_BID_UINT128(tmp_div));
5,550✔
1653
}
5,550✔
1654

1655
Decimal128 Decimal128::operator/(int div) const noexcept
1656
{
24✔
1657
    BID_UINT128 x = to_BID_UINT128(*this);
24✔
1658
    BID_UINT128 y = to_BID_UINT128(Decimal128(div));
24✔
1659
    return do_divide(x, y);
24✔
1660
}
24✔
1661

1662
Decimal128 Decimal128::operator/(Decimal128 div) const noexcept
1663
{
366✔
1664
    BID_UINT128 x = to_BID_UINT128(*this);
366✔
1665
    BID_UINT128 y = to_BID_UINT128(div);
366✔
1666
    return do_divide(x, y);
366✔
1667
}
366✔
1668

1669
Decimal128& Decimal128::operator+=(Decimal128 rhs) noexcept
1670
{
79,854✔
1671
    unsigned flags = 0;
79,854✔
1672
    BID_UINT128 x = to_BID_UINT128(*this);
79,854✔
1673
    BID_UINT128 y = to_BID_UINT128(rhs);
79,854✔
1674

39,927✔
1675
    BID_UINT128 res;
79,854✔
1676
    bid128_add(&res, &x, &y, &flags);
79,854✔
1677
    memcpy(this, &res, sizeof(Decimal128));
79,854✔
1678
    return *this;
79,854✔
1679
}
79,854✔
1680

1681
Decimal128& Decimal128::operator-=(Decimal128 rhs) noexcept
1682
{
24✔
1683
    unsigned flags = 0;
24✔
1684
    BID_UINT128 x = to_BID_UINT128(*this);
24✔
1685
    BID_UINT128 y = to_BID_UINT128(rhs);
24✔
1686

12✔
1687
    BID_UINT128 res;
24✔
1688
    bid128_sub(&res, &x, &y, &flags);
24✔
1689
    memcpy(this, &res, sizeof(Decimal128));
24✔
1690
    return *this;
24✔
1691
}
24✔
1692

1693
bool Decimal128::is_valid_str(StringData str) noexcept
1694
{
×
1695
    unsigned flags = 0;
×
1696
    BID_UINT128 tmp;
×
1697
    bid128_from_string(&tmp, const_cast<char*>(str.data()), &flags);
×
1698

1699
    return (tmp.w[1] & 0x7c00000000000000ull) != 0x7c00000000000000ull;
×
1700
}
×
1701

1702
std::string Decimal128::to_string() const noexcept
1703
{
3,378✔
1704
    if (is_null()) {
3,378✔
1705
        return "NULL";
6✔
1706
    }
6✔
1707

1,686✔
1708
    Bid128 coefficient;
3,372✔
1709
    int exponen;
3,372✔
1710
    bool sign;
3,372✔
1711
    unpack(coefficient, exponen, sign);
3,372✔
1712
    if (coefficient.w[1]) {
3,372✔
1713
        char buffer[64];
6✔
1714
        unsigned flags = 0;
6✔
1715
        BID_UINT128 x;
6✔
1716
        memcpy(&x, this, sizeof(Decimal128));
6✔
1717
        bid128_to_string(buffer, &x, &flags);
6✔
1718
        return std::string(buffer);
6✔
1719
    }
6✔
1720

1,683✔
1721
    // The significant is stored in w[0] only. We can get a nicer printout by using this
1,683✔
1722
    // algorithm here.
1,683✔
1723
    std::string ret;
3,366✔
1724
    if (sign)
3,366✔
1725
        ret = "-";
312✔
1726

1,683✔
1727
    // check for NaN or Infinity
1,683✔
1728
    if ((m_value.w[1] & 0x7800000000000000ull) == 0x7800000000000000ull) {
3,366✔
1729
        if ((m_value.w[1] & 0x7c00000000000000ull) == 0x7c00000000000000ull) { // x is NAN
72✔
1730
            ret += "NaN";
24✔
1731
        }
24✔
1732
        else {
48✔
1733
            ret += "Inf";
48✔
1734
        }
48✔
1735
        return ret;
72✔
1736
    }
72✔
1737

1,647✔
1738
    auto digits = util::to_string(coefficient.w[0]);
3,294✔
1739
    size_t digits_before = digits.length();
3,294✔
1740
    while (digits_before > 1 && exponen != 0) {
11,634✔
1741
        digits_before--;
8,340✔
1742
        exponen++;
8,340✔
1743
    }
8,340✔
1744
    ret += digits.substr(0, digits_before);
3,294✔
1745
    if (digits_before < digits.length()) {
3,294✔
1746
        // There are also digits after the decimal sign
1,020✔
1747
        ret += '.';
2,040✔
1748
        ret += digits.substr(digits_before);
2,040✔
1749
    }
2,040✔
1750
    if (exponen != 0) {
3,294✔
1751
        ret += 'E';
1,608✔
1752
        ret += util::to_string(exponen);
1,608✔
1753
    }
1,608✔
1754

1,647✔
1755
    return ret;
3,294✔
1756
}
3,294✔
1757

1758
auto Decimal128::to_bid32() const noexcept -> std::optional<Bid32>
1759
{
224,379✔
1760
    if (is_null()) {
224,379✔
1761
        return DECIMAL_NULL_32;
36,702✔
1762
    }
36,702✔
1763
    unsigned flags = 0;
187,677✔
1764
    BID_UINT32 buffer;
187,677✔
1765
    BID_UINT128 tmp = to_BID_UINT128(*this);
187,677✔
1766
    bid128_to_bid32(&buffer, &tmp, &flags);
187,677✔
1767
    if (flags & ~BID_INEXACT_EXCEPTION)
187,677✔
1768
        return {};
93,840✔
1769
    return Bid32(buffer);
187,677✔
1770
}
187,677✔
1771

1772
auto Decimal128::to_bid64() const noexcept -> std::optional<Bid64>
1773
{
396✔
1774
    if (is_null()) {
396✔
1775
        return DECIMAL_NULL_64;
18✔
1776
    }
18✔
1777
    unsigned flags = 0;
378✔
1778
    BID_UINT64 buffer;
378✔
1779
    BID_UINT128 tmp = to_BID_UINT128(*this);
378✔
1780
    bid128_to_bid64(&buffer, &tmp, &flags);
378✔
1781
    if (flags & ~BID_INEXACT_EXCEPTION)
378✔
1782
        return {};
189✔
1783
    return Bid64(buffer);
378✔
1784
}
378✔
1785

1786
void Decimal128::unpack(Bid128& coefficient, int& exponent, bool& sign) const noexcept
1787
{
239,601✔
1788
    sign = (m_value.w[1] & MASK_SIGN) != 0;
239,601✔
1789
    int64_t exp = m_value.w[1] & MASK_EXP;
239,601✔
1790
    exp >>= DECIMAL_COEFF_HIGH_BITS;
239,601✔
1791
    exponent = int(exp) - DECIMAL_EXPONENT_BIAS_128;
239,601✔
1792
    coefficient.w[0] = get_coefficient_low();
239,601✔
1793
    coefficient.w[1] = get_coefficient_high();
239,601✔
1794
}
239,601✔
1795

1796
bool operator==(Decimal128::Bid32 lhs, Decimal128::Bid32 rhs) noexcept
1797
{
480✔
1798
    static constexpr int DECIMAL_COEFF_BITS_32 = 23;
480✔
1799
    static constexpr int DECIMAL_EXP_BITS_32 = 8;
480✔
1800
    static constexpr unsigned MASK_COEFF_32 = (1 << DECIMAL_COEFF_BITS_32) - 1;
480✔
1801
    static constexpr unsigned MASK_EXP_32 = ((1 << DECIMAL_EXP_BITS_32) - 1) << DECIMAL_COEFF_BITS_32;
480✔
1802
    static constexpr unsigned MASK_SIGN_32 = 1 << (DECIMAL_COEFF_BITS_32 + DECIMAL_EXP_BITS_32);
480✔
1803

240✔
1804
    uint32_t x = lhs.u;
480✔
1805
    uint32_t y = rhs.u;
480✔
1806

240✔
1807
    if (x == y) {
480✔
1808
        return true;
216✔
1809
    }
216✔
1810

132✔
1811
    unsigned sig_x = (x & MASK_COEFF_32);
264✔
1812
    unsigned sig_y = (y & MASK_COEFF_32);
264✔
1813
    bool x_is_zero = (sig_x == 0);
264✔
1814
    bool y_is_zero = (sig_y == 0);
264✔
1815

132✔
1816
    if (x_is_zero && y_is_zero) {
264!
NEW
1817
        return true;
×
NEW
1818
    }
×
1819
    else if ((x_is_zero && !y_is_zero) || (!x_is_zero && y_is_zero)) {
264!
NEW
1820
        return false;
×
NEW
1821
    }
×
1822

132✔
1823
    // Check if sign differs
132✔
1824
    if ((x ^ y) & MASK_SIGN_32) {
264✔
1825
        return false;
18✔
1826
    }
18✔
1827

123✔
1828
    int exp_x = (x & MASK_EXP_32) >> DECIMAL_COEFF_BITS_32;
246✔
1829
    int exp_y = (y & MASK_EXP_32) >> DECIMAL_COEFF_BITS_32;
246✔
1830

123✔
1831
    // Make exp_y biggest
123✔
1832
    if (exp_x > exp_y) {
246✔
1833
        std::swap(exp_x, exp_y);
96✔
1834
        std::swap(sig_x, sig_y);
96✔
1835
    }
96✔
1836
    if (exp_y - exp_x > 6) {
246✔
1837
        return false;
216✔
1838
    }
216✔
1839
    for (int lcv = 0; lcv < (exp_y - exp_x); lcv++) {
54✔
1840
        sig_y = sig_y * 10;
24✔
1841
        if (sig_y > 9999999) {
24✔
NEW
1842
            return false;
×
NEW
1843
        }
×
1844
    }
24✔
1845
    return (sig_y == sig_x);
30✔
1846
}
30✔
1847

1848
} // namespace realm
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc