-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathopencl_numeric.h
1237 lines (961 loc) · 25.1 KB
/
opencl_numeric.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* opencl_numeric.h
*
* Collection of numeric functions for OpenCL devices
* --
* Copyright 2011-2014 (C) KaiGai Kohei <[email protected]>
* Copyright 2014 (C) The PG-Strom Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#ifndef OPENCL_NUMERIC_H
#define OPENCL_NUMERIC_H
#ifdef OPENCL_DEVICE_CODE
#define CL_CHAR_BIT 8
/* PostgreSQL numeric data type */
#if 0
#define PG_DEC_DIGITS 1
#define PG_NBASE 10
typedef cl_char NumericDigit;
#endif
#if 0
#define PG_DEC_DIGITS 2
#define PG_NBASE 100
typedef cl_char NumericDigit;
#endif
#if 1
#define PG_DEC_DIGITS 4
#define PG_NBASE 10000
typedef cl_short NumericDigit;
#endif
#define PG_MAX_DIGITS 18 /* Max digits of 57 bit mantissa. */
#define PG_MAX_DATA ((PG_MAX_DIGITS + PG_DEC_DIGITS - 1) / \
PG_DEC_DIGITS)
struct NumericShort
{
cl_ushort n_header; /* Sign + display scale + weight */
NumericDigit n_data[PG_MAX_DATA]; /* Digits */
};
struct NumericLong
{
cl_ushort n_sign_dscale; /* Sign + display scale */
cl_short n_weight; /* Weight of 1st digit */
NumericDigit n_data[PG_MAX_DATA]; /* Digits */
};
union NumericChoice
{
cl_ushort n_header; /* Header word */
struct NumericLong n_long; /* Long form (4-byte header) */
struct NumericShort n_short; /* Short form (2-byte header) */
};
// struct NumericData
// {
// int32 vl_len_; /* varlena header (do not touch directly!) */
// union NumericChoice choice; /* choice of format */
// };
#define NUMERIC_SIGN_MASK 0xC000
#define NUMERIC_POS 0x0000
#define NUMERIC_NEG 0x4000
#define NUMERIC_SHORT 0x8000
#define NUMERIC_NAN 0xC000
#define NUMERIC_FLAGBITS(n) ((n)->n_header & NUMERIC_SIGN_MASK)
#define NUMERIC_IS_NAN(n) (NUMERIC_FLAGBITS(n) == NUMERIC_NAN)
#define NUMERIC_IS_SHORT(n) (NUMERIC_FLAGBITS(n) == NUMERIC_SHORT)
#define NUMERIC_SHORT_SIGN_MASK 0x2000
#define NUMERIC_SHORT_DSCALE_MASK 0x1F80
#define NUMERIC_SHORT_DSCALE_SHIFT 7
#define NUMERIC_SHORT_DSCALE_MAX (NUMERIC_SHORT_DSCALE_MASK >> \
NUMERIC_SHORT_DSCALE_SHIFT)
#define NUMERIC_SHORT_WEIGHT_SIGN_MASK 0x0040
#define NUMERIC_SHORT_WEIGHT_MASK 0x003F
#define NUMERIC_SHORT_WEIGHT_MAX NUMERIC_SHORT_WEIGHT_MASK
#define NUMERIC_SHORT_WEIGHT_MIN (-(NUMERIC_SHORT_WEIGHT_MASK+1))
#define NUMERIC_DSCALE_MASK 0x3FFF
#define NUMERIC_DIGITS(n) (NUMERIC_IS_SHORT(n) ? (n)->n_short.n_data : (n)->n_long.n_data)
#define NUMERIC_SIGN(n) (NUMERIC_IS_SHORT(n) ? (((n)->n_short.n_header & NUMERIC_SHORT_SIGN_MASK) ? NUMERIC_NEG : NUMERIC_POS) : NUMERIC_FLAGBITS(n))
#define NUMERIC_DSCALE(n) (NUMERIC_IS_SHORT(n) ? ((n)->n_short.n_header & NUMERIC_SHORT_DSCALE_MASK) >> NUMERIC_SHORT_DSCALE_SHIFT : ((n)->n_long.n_sign_dscale & NUMERIC_DSCALE_MASK))
#define NUMERIC_WEIGHT(n) (NUMERIC_IS_SHORT(n) ? (((n)->n_short.n_header & NUMERIC_SHORT_WEIGHT_SIGN_MASK ? ~NUMERIC_SHORT_WEIGHT_MASK : 0) | ((n)->n_short.n_header & NUMERIC_SHORT_WEIGHT_MASK)) : ((n)->n_long.n_weight))
/* IEEE 754 FORMAT */
#if 0
#define PG_FLOAT_SIGN_POS 31
#define PG_FLOAT_SIGN_BITS 1
#define PG_FLOAT_EXPO_POS 23
#define PG_FLOAT_EXPO_BITS 8
#define PG_FLOAT_MANT_POS 0
#define PG_FLOAT_MANT_BITS 23
#define PG_DOUBLE_SIGN_POS 63
#define PG_DOUBLE_SIGN_BITS 1
#define PG_DOUBLE_EXPO_POS 52
#define PG_DOUBLE_EXPO_BITS 11
#define PG_DOUBLE_MANT_POS 0
#define PG_DOUBLE_MANT_BITS 52
#endif
/*
* PG-Strom internal representation of NUMERIC data type
*
* Even though the nature of NUMERIC data type is variable-length and error-
* less mathmatical operation, we assume most of numeric usage can be hosted
* within 64bit variable. A small number anomaly can be calculated by CPU,
* so we focus on the major portion of use-cases.
* Internal data format of numeric is 64-bit integer that is separated to
* (1) 6bit exponents based on 10, (2) 1bit sign bit, and (3) 57bit mantissa.
* Function that can handle NUMERIC data type will set StromError_CpuReCheck,
* if it detects overflow during calculation.
*/
typedef struct {
cl_ulong value;
bool isnull;
} pg_numeric_t;
#endif /* OPENCL_DEVICE_CODE */
#define PG_NUMERIC_EXPONENT_BITS 6
#define PG_NUMERIC_EXPONENT_POS 58
#define PG_NUMERIC_EXPONENT_MASK (((0x1UL << (PG_NUMERIC_EXPONENT_BITS)) - 1) << (PG_NUMERIC_EXPONENT_POS))
#define PG_NUMERIC_EXPONENT_MAX ((1 << ((PG_NUMERIC_EXPONENT_BITS) - 1)) - 1)
#define PG_NUMERIC_EXPONENT_MIN (0 - (1 << ((PG_NUMERIC_EXPONENT_BITS) - 1)))
#define PG_NUMERIC_SIGN_BITS 1
#define PG_NUMERIC_SIGN_POS 57
#define PG_NUMERIC_SIGN_MASK (((0x1UL << (PG_NUMERIC_SIGN_BITS)) - 1) << (PG_NUMERIC_SIGN_POS))
#define PG_NUMERIC_MANTISSA_BITS 57
#define PG_NUMERIC_MANTISSA_POS 0
#define PG_NUMERIC_MANTISSA_MASK (((0x1UL << (PG_NUMERIC_MANTISSA_BITS)) - 1) << (PG_NUMERIC_MANTISSA_POS))
#define PG_NUMERIC_MANTISSA_MAX ((0x1UL << (PG_NUMERIC_MANTISSA_BITS)) - 1)
#define PG_NUMERIC_EXPONENT(num) ((cl_long)(num) >> 58)
#define PG_NUMERIC_SIGN(num) (((num) & PG_NUMERIC_SIGN_MASK) != 0)
#define PG_NUMERIC_MANTISSA(num) ((num) & PG_NUMERIC_MANTISSA_MASK)
#define PG_NUMERIC_SET(expo,sign,mant) \
((cl_ulong)((cl_long)(expo) << 58) | \
((sign) != 0 ? PG_NUMERIC_SIGN_MASK : 0UL) | \
((mant) & PG_NUMERIC_MANTISSA_MASK))
#ifdef OPENCL_DEVICE_CODE
static pg_numeric_t
pg_numeric_from_varlena(__private int *errcode, __global varlena *vl_val)
{
pg_numeric_t result;
union NumericChoice numData;
if (vl_val == NULL)
{
result.isnull = true;
result.value = 0;
return result;
}
__global cl_char *pSrc = VARDATA_ANY(vl_val);
cl_int len = VARSIZE_ANY_EXHDR(vl_val);
if (sizeof(numData) < len) {
// Numeric data is too large.
// PG-Strom numeric type support 18 characters.
result.isnull = true;
result.value = 0;
*errcode = StromError_CpuReCheck;
return result;
}
// Once data copy to private memory for alignment.
// memcpy(&numData, pSrc, len);
{
// OpenCL memcpy does not support private memory.
__private cl_char *dst = (__private cl_char *) &numData;
__global cl_char *src = (__global cl_char *) pSrc;
for(int i=0; i<len; i++) {
dst[i] = src[i];
}
}
// Convert PG-Strom numeric type from PostgreSQL numeric type.
{
int sign = NUMERIC_SIGN(&numData);
int expo;
cl_ulong mant;
int weight = NUMERIC_WEIGHT(&numData);
// int dscale = NUMERIC_DSCALE(&numData);
NumericDigit *digits = NUMERIC_DIGITS(&numData);
int offset = (unsigned long)digits - (unsigned long)&numData;
int ndigits = (len - offset) / sizeof(NumericDigit);
int i, base;
cl_ulong mantLast;
// Numeric value is 0, if ndigits is 0.
if (ndigits == 0) {
result.isnull = false;
result.value = PG_NUMERIC_SET(0, 0, 0);
return result;
}
// Generate exponent.
expo = (weight - (ndigits - 1)) * PG_DEC_DIGITS;
// Generate mantissa.
mant = 0;
for (i=0; i<ndigits-1; i++) {
mant = mant * PG_NBASE + digits[i];
}
base = PG_NBASE;
mantLast = digits[i];
for (i=0; i<PG_DEC_DIGITS; i++) {
if (mantLast % 10 == 0) {
expo ++;
base /= 10;
mantLast /= 10;
} else {
break;
}
}
// overflow check
if ((mant * base) / base != mant) {
result.isnull = true;
result.value = 0;
*errcode = StromError_CpuReCheck;
return result;
}
mant = mant * base + mantLast;
// Normalize
while (mant % 10 == 0 && expo < PG_NUMERIC_EXPONENT_MAX) {
mant /= 10;
expo ++;
}
if (PG_NUMERIC_EXPONENT_MAX < expo) {
// Exponent is overflow.
int expoDiff = expo - PG_NUMERIC_EXPONENT_MAX;
int i;
ulong mag;
if (PG_MAX_DIGITS <= expoDiff) {
// magnify is overflow
result.isnull = true;
result.value = 0;
*errcode = StromError_CpuReCheck;
return result;
}
for (i=0, mag=1; i < expoDiff; i++) {
mag *= 10;
}
if ((mant * mag) / mag != mant) {
result.isnull = true;
result.value = 0;
*errcode = StromError_CpuReCheck;
return result;
}
expo -= expoDiff;
mant *= mag;
}
// Error check
if (expo < PG_NUMERIC_EXPONENT_MIN || PG_NUMERIC_EXPONENT_MAX < expo ||
(mant & ~PG_NUMERIC_MANTISSA_MASK)) {
result.isnull = true;
result.value = 0;
*errcode = StromError_CpuReCheck;
return result;
}
// Set value to PG_Strom numeric type
result.isnull = false;
result.value = PG_NUMERIC_SET(expo, sign, mant);
}
return result;
}
/*
* pg_numeric_vref
*
* It contains special case handling due to internal numeric format.
* If kds intends to have varlena format (PostgreSQL compatible), it tries
* to reference varlena variable. Otherwise, in case when attlen > 0, it
* tries to fetch fixed-length variable.
*/
static pg_numeric_t
pg_numeric_vref(__global kern_data_store *kds,
__global kern_data_store *ktoast,
__private int *errcode,
cl_uint colidx,
cl_uint rowidx)
{
__global void *addr = kern_get_datum(kds,ktoast,colidx,rowidx);
pg_numeric_t result;
if (!addr)
result.isnull = true;
else if (kds->colmeta[colidx].attlen < 0)
result = pg_numeric_from_varlena(errcode, (__global varlena *)addr);
else
{
result.isnull = false;
result.value = *((__global cl_ulong *) addr);
}
return result;
}
/* pg_numeric_vstore() is same as template */
STROMCL_SIMPLE_VARSTORE_TEMPLATE(numeric, cl_ulong)
static pg_numeric_t
pg_numeric_param(__global kern_parambuf *kparams,
__private int *errcode,
cl_uint param_id)
{
__global varlena *vl_val;
pg_numeric_t result;
if (param_id < kparams->nparams &&
kparams->poffset[param_id] > 0)
{
vl_val = (__global varlena *)
((__global char *)kparams + kparams->poffset[param_id]);
/* only uncompressed & inline datum */
if (VARATT_IS_4B_U(vl_val) || VARATT_IS_1B(vl_val))
return pg_numeric_from_varlena(errcode, vl_val);
STROM_SET_ERROR(errcode, StromError_CpuReCheck);
}
result.isnull = true;
return result;
}
static pg_bool_t
pgfn_numeric_isnull(__private int *errcode,
pg_numeric_t arg)
{
pg_bool_t result;
result.isnull = false;
result.value = arg.isnull;
return result;
}
static pg_bool_t
pgfn_numeric_isnotnull(__private int *errcode,
pg_numeric_t arg)
{
pg_bool_t result;
result.isnull = false;
result.value = !arg.isnull;
return result;
}
/* to avoid conflicts with auto-generated data type */
#define PG_NUMERIC_TYPE_DEFINED
/*
* Numeric format translation functions
* ----------------------------------------------------------------
*/
static pg_int8_t
numeric_to_integer(__private int *errcode, pg_numeric_t arg, cl_int size)
{
pg_int8_t v;
if (arg.isnull == true) {
v.isnull = true;
v.value = 0;
return v;
}
int expo = PG_NUMERIC_EXPONENT(arg.value);
int sign = PG_NUMERIC_SIGN(arg.value);
cl_ulong mant = PG_NUMERIC_MANTISSA(arg.value);
if (mant == 0) {
v.isnull = false;
v.value = 0;
}
int exp = abs(expo);
long mag = 1;
for(int i=0; i<exp; i++) {
if((mag * 10) < mag) {
v.isnull = true;
v.value = 0;
return v;
}
mag *= 10;
}
if (expo < 0) {
// Round off if exponent is minus.
mant = (mant + mag/2) / mag;
} else {
// Overflow check
if ((mant * mag) / mag != mant) {
v.isnull = true;
v.value = 0;
*errcode = StromError_CpuReCheck;
return v;
}
mant *= mag;
}
// Overflow check
int nbits = size * CL_CHAR_BIT;
cl_ulong max_val = (1UL << (nbits - 1)) - 1;
cl_ulong abs_min_val = (1UL << (nbits - 1));
if((sign == 0 && max_val < mant) || (sign != 0 && abs_min_val < mant)) {
v.isnull = true;
v.value = 0;
*errcode = StromError_CpuReCheck;
return v;
}
v.isnull = false;
v.value = (sign == 0) ? mant : (-mant);
return v;
}
static pg_float8_t
numeric_to_float(__private int *errcode, pg_numeric_t arg)
{
pg_float8_t v;
if (arg.isnull == true) {
v.isnull = true;
v.value = 0;
return v;
}
int expo = PG_NUMERIC_EXPONENT(arg.value);
int sign = PG_NUMERIC_SIGN(arg.value);
ulong mant = PG_NUMERIC_MANTISSA(arg.value);
if (mant == 0) {
v.isnull = false;
v.value = PG_NUMERIC_SET(0, 0, 0);
return v;
}
double fvalue = (double)mant * exp10((double)expo);
if (isinf(fvalue) || isnan(fvalue)) {
v.isnull = true;
v.value = 0;
*errcode = StromError_CpuReCheck;
return v;
}
v.isnull = false;
v.value = (sign == 0) ? fvalue : (-fvalue);
return v;
}
static pg_int2_t
pgfn_numeric_int2(__private int *errcode, pg_numeric_t arg)
{
pg_int2_t v;
pg_int8_t tmp = numeric_to_integer(errcode, arg, sizeof(v.value));
v.isnull = tmp.isnull;
v.value = tmp.value;
return v;
}
static pg_int4_t
pgfn_numeric_int4(__private int *errcode, pg_numeric_t arg)
{
pg_int4_t v;
pg_int8_t tmp = numeric_to_integer(errcode, arg, sizeof(v.value));
v.isnull = tmp.isnull;
v.value = tmp.value;
return v;
}
static pg_int8_t
pgfn_numeric_int8(__private int *errcode, pg_numeric_t arg)
{
pg_int8_t v;
return numeric_to_integer(errcode, arg, sizeof(v.value));
}
static pg_float4_t
pgfn_numeric_float4(__private int *errcode, pg_numeric_t arg)
{
pg_float8_t tmp = numeric_to_float(errcode, arg);
pg_float4_t v = { (cl_float)tmp.value, tmp.isnull };
if (v.isnull == false && isinf(v.value)) {
v.isnull = true;
v.value = 0;
*errcode = StromError_CpuReCheck;
}
return v;
}
static pg_float8_t
pgfn_numeric_float8(__private int *errcode, pg_numeric_t arg)
{
return numeric_to_float(errcode, arg);
}
static pg_numeric_t
integer_to_numeric(__private int *errcode, pg_int8_t arg, cl_int size)
{
pg_numeric_t v;
int sign;
int expo;
cl_ulong mant;
if (arg.isnull) {
v.isnull = true;
v.value = 0;
return v;
}
if (arg.value == 0) {
v.isnull = false;
v.value = PG_NUMERIC_SET(0, 0, 0);
return v;
}
if (0 <= arg.value) {
sign = 0;
mant = arg.value;
} else {
sign = 1;
mant = -arg.value;
}
expo = 0;
// Normalize
while (mant % 10 == 0 && expo < PG_NUMERIC_EXPONENT_MAX) {
mant /= 10;
expo ++;
}
if(PG_NUMERIC_MANTISSA_BITS < size * CL_CHAR_BIT - 1) {
// Error check
if (mant & ~PG_NUMERIC_MANTISSA_MASK) {
v.isnull = true;
v.value = 0;
*errcode = StromError_CpuReCheck;
return v;
}
}
v.isnull = false;
v.value = PG_NUMERIC_SET(expo, sign, mant);
return v;
}
static pg_numeric_t
float_to_numeric(__private int *errcode, pg_float8_t arg, int dig)
{
pg_numeric_t v;
if (arg.isnull) {
v.isnull = true;
v.value = 0;
return v;
}
if (isnan(arg.value) || isinf(arg.value)) {
v.isnull = true;
v.value = 0;
*errcode = StromError_CpuReCheck;
return v;
}
if (arg.value == 0.0) {
v.isnull = false;
v.value = PG_NUMERIC_SET(0, 0, 0);
return v;
}
int sign;
double fval;
if (0 <= arg.value) {
sign = 0;
fval = arg.value;
} else {
sign = 1;
fval = -arg.value;
}
int fexpo = ceil(log10(fval)) + 1;
double fmant = fval * (double)exp10((double)(dig - fexpo));
if(isinf(fmant)) {
v.isnull = true;
v.value = 0;
*errcode = StromError_CpuReCheck;
return v;
}
int expo = fexpo - dig;
ulong mant;
double thrMax = exp10((double)(dig));
while(thrMax < fmant) {
fmant /= 10;
expo ++;
}
double thrMin = thrMax / 10;
while(fmant < thrMin) {
fmant *= 10;
expo --;
}
mant = fmant + 0.5;
// normalize
while (mant % 10 == 0 && expo < PG_NUMERIC_EXPONENT_MAX) {
mant /= 10;
expo ++;
}
if (PG_NUMERIC_EXPONENT_MAX < expo) {
// Exponent is overflow.
int expoDiff = expo - PG_NUMERIC_EXPONENT_MAX;
int i;
ulong mag;
if (PG_MAX_DIGITS <= expoDiff) {
// magnify is overflow
v.isnull = true;
v.value = 0;
*errcode = StromError_CpuReCheck;
return v;
}
for (i=0, mag=1; i < expoDiff; i++) {
mag *= 10;
}
if ((mant * mag) / mag != mant) {
v.isnull = true;
v.value = 0;
*errcode = StromError_CpuReCheck;
return v;
}
expo -= expoDiff;
mant *= mag;
}
// Error check
if (expo < PG_NUMERIC_EXPONENT_MIN || PG_NUMERIC_EXPONENT_MAX < expo ||
(mant & ~PG_NUMERIC_MANTISSA_MASK)) {
v.isnull = true;
v.value = 0;
*errcode = StromError_CpuReCheck;
return v;
}
v.isnull = false;
v.value = PG_NUMERIC_SET(expo, sign, mant);
return v;
}
static pg_numeric_t
pgfn_int2_numeric(__private int *errcode, pg_int2_t arg)
{
pg_int8_t tmp = { arg.value, arg.isnull };
return integer_to_numeric(errcode, tmp, sizeof(arg.value));
}
static pg_numeric_t
pgfn_int4_numeric(__private int *errcode, pg_int4_t arg)
{
pg_int8_t tmp = { arg.value, arg.isnull };
return integer_to_numeric(errcode, tmp, sizeof(arg.value));
}
static pg_numeric_t
pgfn_int8_numeric(__private int *errcode, pg_int8_t arg)
{
return integer_to_numeric(errcode, arg, sizeof(arg.value));
}
static pg_numeric_t
pgfn_float4_numeric(__private int *errcode, pg_float4_t arg)
{
pg_float8_t tmp = { (cl_double)arg.value, arg.isnull };
return float_to_numeric(errcode, tmp, FLT_DIG);
}
static pg_numeric_t
pgfn_float8_numeric(__private int *errcode, pg_float8_t arg)
{
return float_to_numeric(errcode, arg, DBL_DIG);
}
/*
* Numeric operator functions
* ----------------------------------------------------------------
*/
static pg_numeric_t
pgfn_numeric_uplus(__private int *errcode, pg_numeric_t arg)
{
/* return the value as-is */
return arg;
}
static pg_numeric_t
pgfn_numeric_uminus(__private int *errcode, pg_numeric_t arg)
{
/* reverse the sign bit */
arg.value ^= PG_NUMERIC_SIGN_MASK;
return arg;
}
static pg_numeric_t
pgfn_numeric_abs(__private int *errcode, pg_numeric_t arg)
{
/* clear the sign bit */
arg.value &= ~PG_NUMERIC_SIGN_MASK;
return arg;
}
static pg_numeric_t
pgfn_numeric_add(__private int *errcode,
pg_numeric_t arg1, pg_numeric_t arg2)
{
pg_numeric_t v;
if (arg1.isnull || arg2.isnull) {
v.isnull = true;
v.value = 0;
return v;
}
int expo1 = PG_NUMERIC_EXPONENT(arg1.value);
int sign1 = PG_NUMERIC_SIGN(arg1.value);
cl_ulong mant1 = PG_NUMERIC_MANTISSA(arg1.value);
int expo2 = PG_NUMERIC_EXPONENT(arg2.value);
int sign2 = PG_NUMERIC_SIGN(arg2.value);
cl_ulong mant2 = PG_NUMERIC_MANTISSA(arg2.value);
// Change the number of digits
if (expo1 != expo2) {
int expoDiff = abs(expo1 - expo2);
cl_ulong value = (expo1 < expo2) ? (mant2) : (mant1);
ulong mag;
int i;
if (PG_MAX_DIGITS <= expoDiff) {
// magnify is overflow
v.isnull = true;
v.value = 0;
*errcode = StromError_CpuReCheck;
return v;
}
mag = 1;
for (i=0; i < expoDiff; i++) {
mag *= 10;
}
// Overflow check
if ((value * mag) / mag != value) {
v.isnull = true;
v.value = 0;
*errcode = StromError_CpuReCheck;
return v;
}
if (expo1 < expo2) {
mant2 = value * mag;
expo2 = expo1;
} else {
mant1 = value * mag;
expo1 = expo2;
}
}
// Add mantissa
if (sign1 != sign2) {
if (mant1 < mant2) {
sign1 = sign2;
mant1 = mant2 - mant1;
} else {
mant1 -= mant2;
}
} else {
if ((mant1 + mant2) < mant1) {
// Overflow
v.isnull = true;
v.value = 0;
*errcode = StromError_CpuReCheck;
return v;
}
mant1 += mant2;
}
// Set 0 if mantissa is 0
if(mant1 == 0UL) {
v.isnull = false;
v.value = PG_NUMERIC_SET(0, 0, 0);
return v;
}
// Normalize
while(mant1 % 10 == 0 && expo1 < PG_NUMERIC_EXPONENT_MAX) {
mant1 /= 10;
expo1 ++;
}
// Error check
if (expo1 < PG_NUMERIC_EXPONENT_MIN || PG_NUMERIC_EXPONENT_MAX < expo1 ||
(mant1 & ~PG_NUMERIC_MANTISSA_MASK)) {
v.isnull = true;
v.value = 0;
*errcode = StromError_CpuReCheck;
return v;
}
// Set
v.isnull = false;
v.value = PG_NUMERIC_SET(expo1, sign1, mant1);
return v;
}
static pg_numeric_t
pgfn_numeric_sub(__private int *errcode,
pg_numeric_t arg1, pg_numeric_t arg2)
{
pg_numeric_t arg = pgfn_numeric_uminus(errcode, arg2);
return pgfn_numeric_add(errcode, arg1, arg);
}
static pg_numeric_t
pgfn_numeric_mul(__private int *errcode,
pg_numeric_t arg1, pg_numeric_t arg2)
{
pg_numeric_t v;
if (arg1.isnull || arg2.isnull) {
v.isnull = true;
v.value = 0;
return v;
}
int expo1 = PG_NUMERIC_EXPONENT(arg1.value);
int sign1 = PG_NUMERIC_SIGN(arg1.value);
cl_ulong mant1 = PG_NUMERIC_MANTISSA(arg1.value);
int expo2 = PG_NUMERIC_EXPONENT(arg2.value);
int sign2 = PG_NUMERIC_SIGN(arg2.value);
cl_ulong mant2 = PG_NUMERIC_MANTISSA(arg2.value);
// Set 0, if mantissa is 0.
if (mant1 == 0UL || mant2 == 0UL) {
v.isnull = false;
v.value = PG_NUMERIC_SET(0, 0, 0);
return v;
}
// Calculate exponential
expo1 += expo2;
// Calculate sign
sign1 ^= sign2;
// Calculate mantissa
if ((mant1 * mant2) / mant2 != mant1) {
v.isnull = true;
v.value = 0;
*errcode = StromError_CpuReCheck;
return v;
}
mant1 *= mant2;
// Normalize
while (mant1 % 10 == 0 && expo1 < PG_NUMERIC_EXPONENT_MAX) {
mant1 /= 10;
expo1 ++;
}
if (PG_NUMERIC_EXPONENT_MAX < expo1) {
// Exponent is overflow.
int expoDiff = expo1 - PG_NUMERIC_EXPONENT_MAX;
ulong mag;
int i;
if (PG_MAX_DIGITS <= expoDiff) {
// magnify is overflow
v.isnull = true;
v.value = 0;
*errcode = StromError_CpuReCheck;
return v;
}
for (i=0, mag=1; i < expoDiff; i++) {
mag *= 10;
}