Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SybaseDecimal.cs « Mono.Data.SybaseTypes « Mono.Data.SybaseClient « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 62eb4a40e832a38b253c2f49bea776139c517e7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
//
// Mono.Data.SybaseTypes.SybaseDecimal
//
// Author:
//   Tim Coleman <tim@timcoleman.com>
//
// Based on System.Data.SqlTypes.SqlDecimal
//
// (C) Ximian, Inc. 2002-2003
// (C) Copyright Tim Coleman, 2002
//

using Mono.Data.Tds.Protocol;
using System;
using System.Data.SqlTypes;
using System.Globalization;
using System.Text;

namespace Mono.Data.SybaseTypes {
	public struct SybaseDecimal : INullable, IComparable
	{

		#region Fields

		int[] value;
		byte precision;
		byte scale;
		bool positive;

		bool notNull;

		// borrowed from System.Decimal
		const int SCALE_SHIFT = 16;
		const int SIGN_SHIFT = 31;
		const int RESERVED_SS32_BITS = 0x7F00FFFF;
		const ulong LIT_GUINT64_HIGHBIT = 0x8000000000000000;
		const ulong LIT_GUINT32_HIGHBIT = 0x80000000;
		const byte DECIMAL_MAX_INTFACTORS = 9;
		static uint [] constantsDecadeInt32Factors = new uint [10]
			{
				1u, 10u, 100u, 1000u, 10000u, 100000u, 1000000u,
				10000000u, 100000000u, 1000000000u
			};

		public static readonly byte MaxPrecision = 38;
		public static readonly byte MaxScale = 38;

		public static readonly SybaseDecimal MaxValue = new SybaseDecimal (MaxPrecision, (byte)0, true, (int)716002642, Int32.MaxValue, (int)1518778966, (int)1262177448);
		public static readonly SybaseDecimal MinValue = new SybaseDecimal (MaxPrecision, (byte)0, false, (int)716002642, Int32.MaxValue, (int)1518778966, (int)1262177448);
		public static readonly SybaseDecimal Null;

		#endregion

		#region Constructors

		public SybaseDecimal (decimal value) 
		{
			int[] binData = Decimal.GetBits (value);

			this.scale = (byte)(binData[3] >> SCALE_SHIFT);
			if (this.scale > MaxScale || (this.scale & RESERVED_SS32_BITS) != 0)
				throw new ArgumentException(Locale.GetText ("Invalid scale"));

			this.value = new int[4];
			this.value[0] = binData[0];
			this.value[1] = binData[1];
			this.value[2] = binData[2];
			this.value[3] = 0;
			notNull = true;

			positive = (value >= 0);
			precision = GetPrecision (value);
		}

		public SybaseDecimal (double value) : this ((decimal)value) { }
		public SybaseDecimal (int value) : this ((decimal)value) { }
		public SybaseDecimal (long value) : this ((decimal)value) { }

		public SybaseDecimal (byte bPrecision, byte bScale, bool fPositive, int[] bits) : this (bPrecision, bScale, fPositive, bits[0], bits[1], bits[2], bits[3]) { }

		public SybaseDecimal (byte bPrecision, byte bScale, bool fPositive, int data1, int data2, int data3, int data4) 
		{
			this.precision = bPrecision;
			this.scale = bScale;
			this.positive = fPositive;
			this.value = new int[4];
			this.value[0] = data1;
			this.value[1] = data2;
			this.value[2] = data3;
			this.value[3] = data4;
			notNull = true;

			if (precision < scale)
				throw new ArgumentException ("Invalid scale");
			if (this.ToDouble () > (System.Math.Pow (10, 38) -1) || this.ToDouble () < -(System.Math.Pow (10, 38)))
				throw new SybaseTypeException ("Can't convert to SybaseDecimal.");
		}

		#endregion

		#region Properties

		public byte[] BinData {
			get { 
				byte[] b = new byte [value.Length * 4];
				int j = 0;
				for (int i = 0; i < value.Length; i += 1) {
					b [j++] = (byte) (0xff & value [i]);
					b [j++] = (byte) (0xff & value [i] >> 8);
					b [j++] = (byte) (0xff & value [i] >> 16);
					b [j++] = (byte) (0xff & value [i] >> 24);
				}
				return b;
			}
		}

		public int[] Data { 
			get { 
				if (this.IsNull)
					throw new SybaseNullValueException ();
				else
					return (value);
			}
		}

		public bool IsNull { 
			get { return !notNull; }
		}

		public bool IsPositive { 
			get { return positive; }
		}

		public byte Precision { 
			get { return precision; }
		}

		public byte Scale { 
			get { return scale; }
		}

		public decimal Value { 
			get { 
				if (this.IsNull) 
					throw new SybaseNullValueException ();
				if (this.value[3] > 0)
					throw new OverflowException ();
				return new decimal (value[0], value[1], value[2], !positive, scale);
			}
		}

		#endregion

		#region Methods

		public static SybaseDecimal Abs (SybaseDecimal n)
		{
			return new SybaseDecimal (n.Precision, n.Scale, true, n.BinData [0], n.BinData [1], n.BinData [2], n.BinData [3]);
		}

		public static SybaseDecimal Add (SybaseDecimal x, SybaseDecimal y)
		{
			return (x + y);
		}

		public static SybaseDecimal AdjustScale (SybaseDecimal n, int digits, bool fRound)
		{
			byte prec = n.Precision;
			if (n.IsNull)
				throw new SybaseNullValueException ();
			if (digits > 0)
				prec = (byte) (prec + digits);
			if (fRound)
				n = Round (n, digits + n.Scale);
			return new SybaseDecimal (prec, (byte) (n.Scale + digits), n.IsPositive, n.Data);
		}

		public static SybaseDecimal Ceiling (SybaseDecimal n)
		{
			return AdjustScale (n, -(n.Scale), true);
		}

		public int CompareTo (object value)
		{
			if (value == null)
				return 1;
			else if (!(value is SybaseDecimal))
				throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SybaseTypes.SybaseDecimal"));
			else if (((SybaseDecimal)value).IsNull)
				return 1;
			else
				return this.Value.CompareTo (((SybaseDecimal)value).Value);
		}

		public static SybaseDecimal ConvertToPrecScale (SybaseDecimal n, int precision, int scale)
		{
			return new SybaseDecimal ((byte) precision, (byte) scale, n.IsPositive, n.Data);
		}

		public static SybaseDecimal Divide (SybaseDecimal x, SybaseDecimal y)
		{
			return (x / y);
		}

		public override bool Equals (object value)
		{
			if (!(value is SybaseDecimal))
				return false;
			else if (this.IsNull && ((SybaseDecimal) value).IsNull)
				return true;
			else if (((SybaseDecimal) value).IsNull)
				return false;
			else
				return (bool) (this == (SybaseDecimal)value);
		}

		public static SybaseBoolean Equals (SybaseDecimal x, SybaseDecimal y)
		{
			return (x == y);
		}

		public static SybaseDecimal Floor (SybaseDecimal n)
		{
			return AdjustScale (n, -(n.Scale), false);
		}

		internal static SybaseDecimal FromTdsBigDecimal (TdsBigDecimal x)
		{
			if (x == null)
				return Null;
			else
				return new SybaseDecimal (x.Precision, x.Scale, !x.IsNegative, x.Data);
                }

		public override int GetHashCode ()
		{
			int result = 10;
			result = 91 * result + this.Data [0];
			result = 91 * result + this.Data [1];
			result = 91 * result + this.Data [2];
			result = 91 * result + this.Data [3];
			result = 91 * result + (int) this.Scale;
			result = 91 * result + (int) this.Precision;

			return result;
		}

		public static SybaseBoolean GreaterThan (SybaseDecimal x, SybaseDecimal y)
		{
			return (x > y);
		}

		public static SybaseBoolean GreaterThanOrEqual (SybaseDecimal x, SybaseDecimal y)
		{
			return (x >= y);
		}

		public static SybaseBoolean LessThan (SybaseDecimal x, SybaseDecimal y)
		{
			return (x < y);
		}

		public static SybaseBoolean LessThanOrEqual (SybaseDecimal x, SybaseDecimal y)
		{
			return (x <= y);
		}

		public static SybaseDecimal Multiply (SybaseDecimal x, SybaseDecimal y)
		{
			return (x * y);
		}

		public static SybaseBoolean NotEquals (SybaseDecimal x, SybaseDecimal y)
		{
			return (x != y);
		}

		public static SybaseDecimal Parse (string s)
		{
			if (s == null)
				throw new ArgumentNullException ();
			else
				return SybaseDouble.Parse (s).ToSybaseDecimal ();
		}

		public static SybaseDecimal Power (SybaseDecimal n, double exp)
		{
			if (n.IsNull)
				return SybaseDecimal.Null;
			return new SybaseDecimal (System.Math.Pow (n.ToDouble (), exp));
		}

		public static SybaseDecimal Round (SybaseDecimal n, int position)
		{
			if (n.IsNull)
				throw new SybaseNullValueException ();
			SybaseDecimal result = new SybaseDecimal (System.Math.Round ((double) (n.ToDouble () * System.Math.Pow (10, position))));
			result = result / new SybaseDecimal (System.Math.Pow (10, position));
			return result;
		}

		public static SybaseInt32 Sign (SybaseDecimal n)
		{
			SybaseInt32 result = 0;
			if (n >= new SybaseDecimal (0))
				result = 1;
			else
				result = -1;
			return result;
		}

		public static SybaseDecimal Subtract (SybaseDecimal x, SybaseDecimal y)
		{
			return (x - y);
		}

		private static byte GetPrecision (decimal value)
		{
			string str = value.ToString ();
			byte result = 0;
			foreach (char c in str) 
				if (c >= '0' && c <= '9')
					result ++;
			return result;
		}

		public double ToDouble ()
		{
			// FIXME: This is the wrong way to do this
			double d = (uint) this.Data [0];
			d += ((uint) this.Data [1]) * System.Math.Pow (2, 32);
			d += ((uint) this.Data [2]) * System.Math.Pow (2, 64);
			d += ((uint) this.Data [3]) * System.Math.Pow (2, 96);
			d /= System.Math.Pow (10, scale);
			return d;
		}

		public SybaseBoolean ToSybaseBoolean ()
		{
			return ((SybaseBoolean)this);
		}
		
		public SybaseByte ToSybaseByte ()
		{
			return ((SybaseByte)this);
		}

		public SybaseDouble ToSybaseDouble ()
		{
			return ((SybaseDouble)this);
		}

		public SybaseInt16 ToSybaseInt16 ()
		{
			return ((SybaseInt16)this);
		}

		public SybaseInt32 ToSybaseInt32 ()
		{
			return ((SybaseInt32)this);
		}

		public SybaseInt64 ToSybaseInt64 ()
		{
			return ((SybaseInt64)this);
		}

		public SybaseMoney ToSybaseMoney ()
		{
			return ((SybaseMoney)this);
		}

		public SybaseSingle ToSybaseSingle ()
		{
			return ((SybaseSingle)this);
		}

		public SybaseString ToSybaseString ()
		{
			return ((SybaseString)this);
		}

		public override string ToString ()
		{
			if (this.IsNull)
				return String.Empty;

			// convert int [4] -> ulong [2]
			ulong lo = (uint) this.Data [0] + (ulong) ((ulong) this.Data [1] << 32);
			ulong hi = (uint) this.Data [2] + (ulong) ((ulong) this.Data [3] << 32);

			uint rest = 0;
			StringBuilder result = new StringBuilder ();
			for (int i = 0; lo != 0 || hi != 0; i += 1) {
				Div128By32 (ref hi, ref lo, 10, ref rest);
				result.Insert (0, rest.ToString ());
			}
			while (result.Length < Precision)
				result.Append ("0");
			while (result.Length > Precision)
				result.Remove (result.Length - 1, 1);
			if (Scale > 0)
				result.Insert (result.Length - Scale, ".");
			return result.ToString ();
		}

		// from decimal.c
		private static int Div128By32 (ref ulong hi, ref ulong lo, uint divider)
		{
			uint t = 0;
			return Div128By32 (ref hi, ref lo, divider, ref t);
		}

		// from decimal.c
		private static int Div128By32 (ref ulong hi, ref ulong lo, uint divider, ref uint rest)
		{
			ulong a = 0;
			ulong b = 0;
			ulong c = 0;

			a = (uint) (hi >> 32);
			b = a / divider;
			a -= b * divider;
			a <<= 32;
			a |= (uint) hi;
			c = a / divider;
			a -= c * divider;
			a <<= 32;
			hi = b << 32 | (uint) c;

			a = (uint) (lo >> 32);
			b = a / divider;
			a -= b * divider;
			a <<= 32;
			a |= (uint) lo;
			c = a / divider;
			a -= c * divider;
			a <<= 32;
			lo = b << 32 | (uint) c;

			rest = (uint) a;
			a <<= 1;

			return (a > divider || (a == divider && (c & 1) == 1)) ? 1 : 0;
		}

		[MonoTODO ("Find out what is the right way to set scale and precision")]
		private static SybaseDecimal DecimalDiv (SybaseDecimal x, SybaseDecimal y)
		{
			ulong lo = 0;
			ulong hi = 0;
			int sc = 0; // scale
			int texp = 0;
			byte prec = 0;

			prec = x.Precision >= y.Precision ? x.Precision : y.Precision;
			DecimalDivSub (ref x, ref y, ref lo, ref hi, ref texp);

			sc = x.Scale - y.Scale;

			Rescale128 (ref lo, ref hi, ref sc, texp, 0, 38, 1);

			uint r = 0;
			while (prec < sc) {
				Div128By32 (ref hi, ref lo, 10, ref r);
				sc -= 1;
			}

			if (r >= 5)
				lo += 1;
		
			while ((((double) hi) * System.Math.Pow (2, 64) + lo) - System.Math.Pow (10, prec) > 0)
				prec += 1;

			while ((prec + sc) > MaxScale) {
				Div128By32 (ref hi, ref lo, 10, ref r);
				sc -= 1;
				if (r >= 5)
					lo += 1;
			}

			int resultLo = (int) lo;
			int resultMi = (int) (lo >> 32);
			int resultMi2 = (int) hi;
			int resultHi = (int) (hi >> 32);

			return new SybaseDecimal (prec, (byte) sc, true, resultLo, resultMi, resultMi2, resultHi);
		}

		// From decimal.c
		private static void Rescale128 (ref ulong clo, ref ulong chi, ref int scale, int texp, int minScale, int maxScale, int roundFlag) 
		{
			uint factor = 0;
			uint overhang = 0;
			int sc = 0;
			int i = 0;
			int roundBit = 0;

			sc = scale;
			if (texp > 0) {
				// reduce exp
				while (texp > 0 && sc <= maxScale) {
					overhang = (uint) (chi >> 64);
					while (texp > 0 && (((clo & 1) == 0) || overhang > 0)) {
						if (--texp == 0)
							roundBit = (int) (clo & 1);
						RShift128 (ref clo, ref chi);
						overhang = (uint) (chi >> 32);
					}

					if (texp > DECIMAL_MAX_INTFACTORS)
						i = DECIMAL_MAX_INTFACTORS;
					else
						i = texp;
					
					if (sc + i > maxScale) 
						i = maxScale - sc;
					if (i == 0)
						break;
					
					texp -= i;
					sc += i;

					// 10^i/2^i=5^i
					factor = constantsDecadeInt32Factors [i] >> i;
					Mult128By32 (ref clo, ref chi, factor, 0);
				}

				while (texp > 0) {
					if (--texp == 0)
						roundBit = (int) (clo & 1);
					RShift128 (ref clo, ref chi);

				}
			}

			while (sc > maxScale) {
				i = scale - maxScale;
				if (i > DECIMAL_MAX_INTFACTORS)
					i = DECIMAL_MAX_INTFACTORS;
				sc -= i;
				roundBit = Div128By32 (ref clo, ref chi, constantsDecadeInt32Factors [i]);
			}

			while (sc < minScale) {
				if (roundFlag == 0)
					roundBit = 0;
				i = minScale - sc;
				if (i > DECIMAL_MAX_INTFACTORS)
					i = DECIMAL_MAX_INTFACTORS;
				sc += i;
				Mult128By32 (ref clo, ref chi, constantsDecadeInt32Factors [i], roundBit);
				roundBit = 0;
			}
			scale = sc;
			Normalize128 (ref clo, ref chi, ref sc, roundFlag, roundBit);
		}

		// from decimal.c
		private static void Normalize128 (ref ulong clo, ref ulong chi, ref int scale, int roundFlag, int roundBit)
		{
			if ((roundFlag != 0) && (roundBit != 0))
				RoundUp128 (ref clo, ref chi);
		}

		// from decimal.c
		private static void RoundUp128 (ref ulong lo, ref ulong hi)
		{
			if ((++lo) == 0)
				++hi;
		}

		// from decimal.c
		private static void DecimalDivSub (ref SybaseDecimal x, ref SybaseDecimal y, ref ulong clo, ref ulong chi, ref int exp)
		{
			ulong xlo, xmi, xhi;
			ulong tlo = 0;
			ulong tmi = 0;
			ulong thi = 0;
			uint ylo = 0;
			uint ymi = 0;
			uint ymi2 = 0;
			uint yhi = 0;
			int ashift = 0;
			int bshift = 0;
			int extraBit = 0;

			xhi = (ulong) ((ulong) x.Data [3] << 32) | (ulong) x.Data [2];
			xmi = (ulong) ((ulong) x.Data [1] << 32) | (ulong) x.Data [0];
			xlo = (uint) 0;
			ylo = (uint) y.Data [0];
			ymi = (uint) y.Data [1];
			ymi2 = (uint) y.Data [2];
			yhi = (uint) y.Data [3];

			if (ylo == 0 && ymi == 0 && ymi2 == 0 && yhi == 0)
				throw new DivideByZeroException ();
			if (xmi == 0 && xhi == 0) {
				clo = chi = 0;
				return;
			}

			// enlarge dividend to get maximal precision
			for (ashift = 0; (xhi & LIT_GUINT64_HIGHBIT) == 0; ++ashift) 
				LShift128 (ref xmi, ref xhi);

			// ensure that divisor is at least 2^95
			for (bshift = 0; (yhi & LIT_GUINT32_HIGHBIT) == 0; ++bshift) 
				LShift128 (ref ylo, ref ymi, ref ymi2, ref yhi);

			thi = ((ulong) yhi) << 32 | (ulong) ymi2;
			tmi = ((ulong) ymi) << 32 | (ulong) ylo;
			tlo = 0;

			if (xhi > thi || (xhi == thi && xmi >= tmi)) {
				Sub192 (xlo, xmi, xhi, tlo, tmi, thi, ref xlo, ref xmi, ref xhi);
				extraBit = 1;
			} else {
				extraBit = 0;
			}

			Div192By128To128 (xlo, xmi, xhi, ylo, ymi, ymi2, yhi, ref clo, ref chi);

			exp = 128 + ashift - bshift;

			if (extraBit != 0) {
				RShift128 (ref clo, ref chi);
				chi += LIT_GUINT64_HIGHBIT;
				exp -= 1;
			}

			// try loss free right shift
			while (exp > 0 && (clo & 1) == 0) {
				RShift128 (ref clo, ref chi);
				exp -= 1;
			}
		}

		// From decimal.c
		private static void RShift192 (ref ulong lo, ref ulong mi, ref ulong hi)
		{
			lo >>= 1;
			if ((mi & 1) != 0)
				lo |= LIT_GUINT64_HIGHBIT;
			
			mi >>= 1;
			if ((hi & 1) != 0)
				mi |= LIT_GUINT64_HIGHBIT;
	
			hi >>= 1;
		}

		// From decimal.c
		private static void RShift128 (ref ulong lo, ref ulong hi)
		{
			lo >>= 1;
			if ((hi & 1) != 0)
				lo |= LIT_GUINT64_HIGHBIT;
			hi >>= 1;
		}

		// From decimal.c
		private static void LShift128 (ref ulong lo, ref ulong hi)
		{
			hi <<= 1;

			if ((lo & LIT_GUINT64_HIGHBIT) != 0)
				hi += 1;
			
			lo <<= 1;
		}

		// From decimal.c
		private static void LShift128 (ref uint lo, ref uint mi, ref uint mi2, ref uint hi)
		{
			hi <<= 1;
			if ((mi2 & LIT_GUINT32_HIGHBIT) != 0)
				hi += 1;

			mi2 <<= 1;
			if ((mi & LIT_GUINT32_HIGHBIT) != 0)
				mi2 += 1;

			mi <<= 1;
			if ((lo & LIT_GUINT32_HIGHBIT) != 0)
				mi += 1;

			lo <<= 1;
		}

		// From decimal.c
		private static void Div192By128To128 (ulong xlo, ulong xmi, ulong xhi, uint ylo, uint ymi, uint ymi2, uint yhi, ref ulong clo, ref ulong chi)
		{
			ulong rlo, rmi, rhi; // remainders
			uint h, c;

			rlo = xlo;
			rmi = xmi;
			rhi = xhi;

			h = Div192By128To32WithRest (ref rlo, ref rmi, ref rhi, ylo, ymi, ymi2, yhi);

			// mid 32 bit
			rhi = (rhi << 32) | (rmi >> 32);
			rmi = (rmi << 32) | (rlo >> 32);
			rlo <<= 32;

			chi = (((ulong)h) << 32) | Div192By128To32WithRest (ref rlo, ref rmi, ref rhi, ylo, ymi, ymi2, yhi);

			// low 32 bit
			rhi = (rhi << 32) | (rmi >> 32);
			rmi = (rmi << 32) | (rlo >> 32);
			rlo <<= 32;

			h = Div192By128To32WithRest (ref rlo, ref rmi, ref rhi, ylo, ymi, ymi2, yhi);

			// estimate lowest 32 bit (two last bits may be wrong)
			if (rhi >= yhi)
				c =  0xFFFFFFFF;
			else {
				rhi <<= 32;
				c = (uint)(rhi / yhi);
			}

			clo = (((ulong)h) << 32) | c;
		}

		// From decimal.c
		private static uint Div192By128To32WithRest (ref ulong xlo, ref ulong xmi, ref ulong xhi, uint ylo, uint ymi, uint ymi2, uint yhi)
		{
			ulong rlo, rmi, rhi; // remainder
			ulong tlo = 0;
			ulong thi = 0;
			uint c;

			rlo = xlo;
			rmi = xmi;
			rhi = xhi;

			if (rhi >= (((ulong)yhi << 32)))
				c = 0xFFFFFFFF;
			else
				c = (uint) (rhi / yhi);

			Mult128By32To128 (ylo, ymi, ymi2, yhi, c, ref tlo, ref thi);
			Sub192 (rlo, rmi, rhi, 0, tlo, thi, ref rlo, ref rmi, ref rhi);

			while (((long)rhi) < 0) {
				c--;
				Add192 (rlo, rmi, rhi, 0, (((ulong)ymi) << 32) | ylo, yhi | ymi2, ref rlo, ref rmi, ref rhi);
			}
			xlo = rlo;
			xmi = rmi;
			xhi = rhi;

			return c;
		}

		// From decimal.c
		private static void Mult192By32 (ref ulong clo, ref ulong cmi, ref ulong chi, ulong factor, int roundBit)
		{
			ulong a = 0;
			uint h0 = 0;
			uint h1 = 0;

			a = ((ulong)(uint)clo) * factor;

			if (roundBit != 0)
				a += factor / 2;

			h0 = (uint)a;
			a >>= 32;
			a += (clo >> 32) * factor;
			h1 = (uint)a;

			clo = ((ulong)h1) << 32 | h0;

			a >>= 32;
			a += ((ulong)(uint)cmi) * factor;
			h0 = (uint)a;

			a >>= 32;
			a += (cmi >> 32) * factor;
			h1 = (uint)a;

			cmi = ((ulong)h1) << 32 | h0;
			a >>= 32;
			a += ((ulong)(uint)chi) * factor;
			h0 = (uint)a;

			a >>= 32;
			a += (chi >> 32) * factor;
			h1 = (uint)a;
			chi = ((ulong)h1) << 32 | h0;
		}

		// From decimal.c
		private static void Mult128By32 (ref ulong clo, ref ulong chi, uint factor, int roundBit)
		{
			ulong a = 0;
			uint h0 = 0;
			uint h1 = 0;

			a = ((ulong)(uint)clo) * factor;

			if (roundBit != 0)
				a += factor / 2;

			h0 = (uint)a;

			a >>= 32;
			a += (clo >> 32) * factor;
			h1 = (uint)a;

			clo = ((ulong)h1) << 32 | h0;

			a >>= 32;
			a += ((ulong)(uint)chi) * factor;
			h0 = (uint)a;

			a >>= 32;
			a += (chi >> 32) * factor;
			h1 = (uint)a;

			chi = ((ulong)h1) << 32 | h0;
		}

		// From decimal.c
		private static void Mult128By32To128 (uint xlo, uint xmi, uint xmi2, uint xhi, uint factor, ref ulong clo, ref ulong chi)
		{
			ulong a;
			uint h0, h1, h2;

			a = ((ulong)xlo) * factor;
			h0 = (uint)a;

			a >>= 32;
			a += ((ulong)xmi) * factor;
			h1 = (uint)a;

			a >>= 32;
			a += ((ulong)xmi2) * factor;
			h2 = (uint)a;

			a >>= 32;
			a += ((ulong)xhi) * factor;

			clo = ((ulong)h1) << 32 | h0;
			chi = a | h2;
		}

		// From decimal.c
		private static void Add192 (ulong xlo, ulong xmi, ulong xhi, ulong ylo, ulong ymi, ulong yhi, ref ulong clo, ref ulong cmi, ref ulong chi)
		{
			xlo += ylo;
			if (xlo < ylo) {
				xmi++;
				if (xmi == 0)
					xhi++;
			}

			xmi += ymi;

			if (xmi < ymi)
				xmi++;

			xhi += yhi;
			clo = xlo;
			cmi = xmi;
			chi = xhi;
		}

		// From decimal.c
		private static void Sub192 (ulong xlo, ulong xmi, ulong xhi, ulong ylo, ulong ymi, ulong yhi, ref ulong lo, ref ulong mi, ref ulong hi)
		{
			ulong clo = 0;
			ulong cmi = 0;
			ulong chi = 0;

			clo = xlo - ylo;
			cmi = xmi - ymi;
			chi = xhi - yhi;

			if (xlo < ylo) {
				if (cmi == 0)
					chi--;
				cmi--;
			}

			if (xmi < ymi)
				chi--;

			lo = clo;
			mi = cmi;
			hi = chi;
		}

		public static SybaseDecimal Truncate (SybaseDecimal n, int position)
		{
			return new SybaseDecimal ((byte) n.Precision, (byte) position, n.IsPositive, n.Data);
		}

		public static SybaseDecimal operator + (SybaseDecimal x, SybaseDecimal y)
		{
			// if one of them is negative, perform subtraction
			if (x.IsPositive && !y.IsPositive) return x - y;
			if (y.IsPositive && !x.IsPositive) return y - x;
		
			// adjust the scale to the smaller of the two beforehand
			if (x.Scale > y.Scale)
				x = SybaseDecimal.AdjustScale(x, y.Scale - x.Scale, true);
			else if (y.Scale > x.Scale)
				y = SybaseDecimal.AdjustScale(y, x.Scale - y.Scale, true);

			// set the precision to the greater of the two
			byte resultPrecision;
			if (x.Precision > y.Precision)
				resultPrecision = x.Precision;
			else
				resultPrecision = y.Precision;
				
			int[] xData = x.Data;
			int[] yData = y.Data;
			int[] resultBits = new int[4];

			ulong res; 
			ulong carry = 0;

			// add one at a time, and carry the results over to the next
			for (int i = 0; i < 4; i +=1)
			{
				carry = 0;
				res = (ulong)(xData[i]) + (ulong)(yData[i]) + carry;
				if (res > Int32.MaxValue)
				{
					carry = res - Int32.MaxValue;
					res = Int32.MaxValue;
				}
				resultBits [i] = (int)res;
			}

			// if we have carry left, then throw an exception
			if (carry > 0)
				throw new OverflowException ();
			else
				return new SybaseDecimal (resultPrecision, x.Scale, x.IsPositive, resultBits);
		}

		public static SybaseDecimal operator / (SybaseDecimal x, SybaseDecimal y)
		{
			return DecimalDiv (x, y);
		}

		public static SybaseBoolean operator == (SybaseDecimal x, SybaseDecimal y)
		{
			if (x.IsNull || y.IsNull) 
				return SybaseBoolean.Null;

			if (x.Scale > y.Scale)
				x = SybaseDecimal.AdjustScale(x, y.Scale - x.Scale, true);
			else if (y.Scale > x.Scale)
				y = SybaseDecimal.AdjustScale(y, x.Scale - y.Scale, true);

			for (int i = 0; i < 4; i += 1)
			{
				if (x.Data[i] != y.Data[i])
					return new SybaseBoolean (false);
			}
			return new SybaseBoolean (true);
		}

		public static SybaseBoolean operator > (SybaseDecimal x, SybaseDecimal y)
		{
			if (x.IsNull || y.IsNull) 
				return SybaseBoolean.Null;

			if (x.Scale > y.Scale)
				x = SybaseDecimal.AdjustScale(x, y.Scale - x.Scale, true);
			else if (y.Scale > x.Scale)
				y = SybaseDecimal.AdjustScale(y, x.Scale - y.Scale, true);

			for (int i = 3; i >= 0; i -= 1)
			{
				if (x.Data[i] == 0 && y.Data[i] == 0) 
					continue;
				else
					return new SybaseBoolean (x.Data[i] > y.Data[i]);
			}
			return new SybaseBoolean (false);
		}

		public static SybaseBoolean operator >= (SybaseDecimal x, SybaseDecimal y)
		{
			if (x.IsNull || y.IsNull) 
				return SybaseBoolean.Null;

			if (x.Scale > y.Scale)
				x = SybaseDecimal.AdjustScale(x, y.Scale - x.Scale, true);
			else if (y.Scale > x.Scale)
				y = SybaseDecimal.AdjustScale(y, x.Scale - y.Scale, true);

			for (int i = 3; i >= 0; i -= 1)
			{
				if (x.Data[i] == 0 && y.Data[i] == 0) 
					continue;
				else
					return new SybaseBoolean (x.Data[i] >= y.Data[i]);
			}
			return new SybaseBoolean (true);
		}

		public static SybaseBoolean operator != (SybaseDecimal x, SybaseDecimal y)
		{
			if (x.IsNull || y.IsNull) 
				return SybaseBoolean.Null;

			if (x.Scale > y.Scale)
				x = SybaseDecimal.AdjustScale(x, y.Scale - x.Scale, true);
			else if (y.Scale > x.Scale)
				y = SybaseDecimal.AdjustScale(y, x.Scale - y.Scale, true);

			for (int i = 0; i < 4; i += 1)
			{
				if (x.Data[i] != y.Data[i])
					return new SybaseBoolean (true);
			}
			return new SybaseBoolean (false);
		}

		public static SybaseBoolean operator < (SybaseDecimal x, SybaseDecimal y)
		{
			if (x.IsNull || y.IsNull) 
				return SybaseBoolean.Null;

			if (x.Scale > y.Scale)
				x = SybaseDecimal.AdjustScale(x, y.Scale - x.Scale, true);
			else if (y.Scale > x.Scale)
				y = SybaseDecimal.AdjustScale(y, x.Scale - y.Scale, true);

			for (int i = 3; i >= 0; i -= 1)
			{
				if (x.Data[i] == 0 && y.Data[i] == 0) 
					continue;

				return new SybaseBoolean (x.Data[i] < y.Data[i]);
			}
			return new SybaseBoolean (false);
		}

		public static SybaseBoolean operator <= (SybaseDecimal x, SybaseDecimal y)
		{
			if (x.IsNull || y.IsNull) 
				return SybaseBoolean.Null;

			if (x.Scale > y.Scale)
				x = SybaseDecimal.AdjustScale(x, y.Scale - x.Scale, true);
			else if (y.Scale > x.Scale)
				y = SybaseDecimal.AdjustScale(y, x.Scale - y.Scale, true);

			for (int i = 3; i >= 0; i -= 1)
			{
				if (x.Data[i] == 0 && y.Data[i] == 0) 
					continue;
				else
					return new SybaseBoolean (x.Data[i] <= y.Data[i]);
			}
			return new SybaseBoolean (true);
		}

		public static SybaseDecimal operator * (SybaseDecimal x, SybaseDecimal y)
		{
			// adjust the scale to the smaller of the two beforehand
			if (x.Scale > y.Scale)
				x = SybaseDecimal.AdjustScale(x, y.Scale - x.Scale, true);
			else if (y.Scale > x.Scale)
				y = SybaseDecimal.AdjustScale(y, x.Scale - y.Scale, true);

			// set the precision to the greater of the two
			byte resultPrecision;
			if (x.Precision > y.Precision)
				resultPrecision = x.Precision;
			else
				resultPrecision = y.Precision;
				
			int[] xData = x.Data;
			int[] yData = y.Data;
			int[] resultBits = new int[4];

			ulong res; 
			ulong carry = 0;

			// multiply one at a time, and carry the results over to the next
			for (int i = 0; i < 4; i +=1)
			{
				carry = 0;
				res = (ulong)(xData[i]) * (ulong)(yData[i]) + carry;
				if (res > Int32.MaxValue)
				{
					carry = res - Int32.MaxValue;
					res = Int32.MaxValue;
				}
				resultBits [i] = (int)res;
			}

			// if we have carry left, then throw an exception
			if (carry > 0)
				throw new OverflowException ();
			else
				return new SybaseDecimal (resultPrecision, x.Scale, (x.IsPositive == y.IsPositive), resultBits);
				
		}

		public static SybaseDecimal operator - (SybaseDecimal x, SybaseDecimal y)
		{
			if (x.IsPositive && !y.IsPositive) return x + y;
			if (!x.IsPositive && y.IsPositive) return -(x + y);
			if (!x.IsPositive && !y.IsPositive) return y - x;

			// otherwise, x is positive and y is positive
			bool resultPositive = (bool)(x > y);
			int[] yData = y.Data;

			for (int i = 0; i < 4; i += 1) yData[i] = -yData[i];

			SybaseDecimal yInverse = new SybaseDecimal (y.Precision, y.Scale, y.IsPositive, yData);

			if (resultPositive)
				return x + yInverse;
			else
				return -(x + yInverse);
		}

		public static SybaseDecimal operator - (SybaseDecimal n)
		{
			return new SybaseDecimal (n.Precision, n.Scale, !n.IsPositive, n.Data);
		}

		public static explicit operator SybaseDecimal (SybaseBoolean x)
		{
			if (x.IsNull) 
				return Null;
			else
				return new SybaseDecimal ((decimal)x.ByteValue);
		}

		public static explicit operator Decimal (SybaseDecimal n)
		{
			return n.Value;
		}

		public static explicit operator SybaseDecimal (SybaseDouble x)
		{
			if (x.IsNull) 
				return Null;
			else
				return new SybaseDecimal ((decimal)x.Value);
		}

		public static explicit operator SybaseDecimal (SybaseSingle x)
		{
			if (x.IsNull) 
				return Null;
			else
				return new SybaseDecimal ((decimal)x.Value);
		}

		[MonoTODO]
		public static explicit operator SybaseDecimal (SybaseString x)
		{
			throw new NotImplementedException ();
		}

		public static implicit operator SybaseDecimal (decimal x)
		{
			return new SybaseDecimal (x);
		}

		public static implicit operator SybaseDecimal (SybaseByte x)
		{
			if (x.IsNull) 
				return Null;
			else
				return new SybaseDecimal ((decimal)x.Value);
		}

		public static implicit operator SybaseDecimal (SybaseInt16 x)
		{
			if (x.IsNull) 
				return Null;
			else
				return new SybaseDecimal ((decimal)x.Value);
		}

		public static implicit operator SybaseDecimal (SybaseInt32 x)
		{
			if (x.IsNull) 
				return Null;
			else
				return new SybaseDecimal ((decimal)x.Value);
		}

		public static implicit operator SybaseDecimal (SybaseInt64 x)
		{
			if (x.IsNull) 
				return Null;
			else
				return new SybaseDecimal ((decimal)x.Value);
		}

		public static implicit operator SybaseDecimal (SybaseMoney x)
		{
			if (x.IsNull) 
				return Null;
			else
				return new SybaseDecimal ((decimal)x.Value);
		}

		#endregion
	}
}