ObjFW  Diff

Differences From Artifact [f96cb2d4be]:

To Artifact [eeab93bc59]:


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
1222
1223
1224
1225
1226
1227








1228
1229
1230
1231
1232
1233
1234
1235
1236
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







+
+














-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-

+



+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+


-
-
+
-

-
-
-
+
-
-
-
-
-
+
+
+
-
-
+
-
-
-
-
+
+
-
-
+
+


-
+

-
+


-
+

-
-
-
+
-
-
-
-
-
+
+
+
-
-
-


-
+

-
+


-
-
-
-
-
-
-
-
-
-
-
+

-
+


-
+

-
+


-
+

-
+


-
+

-
+


-
+

-
+


-
+

-
+


-
-
-
-
-
-
-
-
-
-
-
+

-
+

-
-
-
-
-
+
-
-
-
-
-
-
-
+

-
+


-
-
-
-
-
-
+

-
+


-
+

-
+


-
+

-
+


-
+

-
+







-
+



-
-
+
+




-
+



-
+
-
-
+
-
-
+
-
-
-
-
-
-
-
-


-
-
-
-
-
-
-

-
-
-
-
+



-
+
-
-
+
-
-
+
-
-
-
-
-
-
-
-



-
+



-
-
+
+




-
+



-
-
+
+




-
+



-
+
-
-
+
-
-
+
-
-
-
-
-
-
-
-

-
-
-
-
-
-
+
-
-

-
-
-
-
+



-
-
+
+




-
+



-
-
-
+
-
-
-
+
-
-
+
-
-
-
-
-



-
-
-
-
-
-
-
-
-
-
-
+



-
+
-
-
+
-
-
+
-
-
-
-
-
-
-
-



-
+



-
-
+
+




-
+



-
-
+
+




-
+



-
+
-
-
+
-
-
+
-
-
-
-
-
-
-
-



-
+



-
-
+
+




-
+



-
-
-
+
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-



-
-
-
-
-
-
-
-
-
-
-
+



-
-
+
+




-
+



-
-
+
+












-
+





-
+
-
-
-
-
-
-
+
+
+
+
+


-
+
-
-
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
-
-
-
+
+
-
-
-
-
-
+
+
+













-
-
-
-
-
+
+
+
+
+
-
-
-
+
+
+
+
-
-
-
+
+
+
-
-
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
+
+
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
+
+





-
+
-

-
-
-
-
-
-
+
+
-
-
-
-
-
-
+
+
-
-
-
-
-
+
-
-
-
-
-
-
+
-
-
-
-
-
-
+
-
-
-
-
-
-
+
+
-
-
-
-
-

-
-
-
-
+
-
-
+
-
-
-
-
-
-
-
+
+
-
-
-
-
-
-
+
+
-
-
-
+
-
-
+
-
-
-
-
-
-
+
-
-
-
-
-
-
+
+
-
-
-
-
-

-
+
-
-
-
-
-
+
-
-
-
-
-
-
-
+
+
-
-
-
-
-
-
+
+
-
-
-
-
-
-
-
+
+
+
-
-
-
-
+
+
-
-
-
-
-

-
-
-
-
-
-
+
-
-
-
-
-
-
+
-
-
-
-
-
-
+
-
-
-
-
-
-
+
-
-
-
-
-

-
-
-
-
+
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-


-
+

-
+


-
+
-
-
-
-
-
+
+
-
-


-
+

-
+



-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
+
-
-
-


-
-
-
-
-
+
-

-
-
-
+
-
-
-


-
-
-
-
-
+
-

-
-
-
+
-
-
-


-
-
-
-
-
+
-

-
-
-
+
-
-
-


-
-
-
-
-
-
-
-
-
-
-
-
-
-
-


-
-
-
-
+
-
-














-
-
+











-
-
-
+
+

-
+











-
-
+









-
+
-
-
-
+
+








-
-
+
+












-


-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-


-
+









-
-
+
+

-
-
-
+
+
+

-
-
-
+
-
-
+
-
-
-
-
-


















-
-
+
-
-
-
+
-
-
-
+
-
-
-
-
-
-
-
-
-
-
+
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
+
-
-
-
-
-
-
-
-
-
+
+
-
-

-
-
-
-
-
-
-
+
+
+
-





-
+



-
+
-

-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-

-
+



-
-
-
-
+
+
+
+
+
+
+
+

-







 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#define OF_NUMBER_M

#include "config.h"

#include <math.h>

#import "OFNumber.h"
#import "OFString.h"
#import "OFXMLElement.h"
#import "OFXMLAttribute.h"
#import "OFData.h"

#import "OFInvalidArgumentException.h"
#import "OFInvalidFormatException.h"
#import "OFOutOfRangeException.h"

#define RETURN_AS(t)							\
	switch (_type) {						\
	case OF_NUMBER_TYPE_BOOL:					\
		return (t)_value.bool_;					\
	case OF_NUMBER_TYPE_CHAR:					\
		return (t)_value.sChar;					\
	case OF_NUMBER_TYPE_SHORT:					\
		return (t)_value.sShort;				\
	case OF_NUMBER_TYPE_INT:					\
		return (t)_value.sInt;					\
	case OF_NUMBER_TYPE_LONG:					\
		return (t)_value.sLong;					\
	case OF_NUMBER_TYPE_LONGLONG:					\
		return (t)_value.sLongLong;				\
	case OF_NUMBER_TYPE_UCHAR:					\
		return (t)_value.uChar;					\
	case OF_NUMBER_TYPE_USHORT:					\
		return (t)_value.uShort;				\
	case OF_NUMBER_TYPE_UINT:					\
		return (t)_value.uInt;					\
	case OF_NUMBER_TYPE_ULONG:					\
		return (t)_value.uLong;					\
	case OF_NUMBER_TYPE_ULONGLONG:					\
		return (t)_value.uLongLong;				\
	case OF_NUMBER_TYPE_INT8:					\
		return (t)_value.int8;					\
	case OF_NUMBER_TYPE_INT16:					\
		return (t)_value.int16;					\
	case OF_NUMBER_TYPE_INT32:					\
		return (t)_value.int32;					\
	case OF_NUMBER_TYPE_INT64:					\
		return (t)_value.int64;					\
	case OF_NUMBER_TYPE_UINT8:					\
		return (t)_value.uInt8;					\
	case OF_NUMBER_TYPE_UINT16:					\
		return (t)_value.uInt16;				\
	case OF_NUMBER_TYPE_UINT32:					\
		return (t)_value.uInt32;				\
	case OF_NUMBER_TYPE_UINT64:					\
		return (t)_value.uInt64;				\
	case OF_NUMBER_TYPE_SIZE:					\
		return (t)_value.size;					\
	case OF_NUMBER_TYPE_SSIZE:					\
		return (t)_value.sSize;					\
	case OF_NUMBER_TYPE_INTMAX:					\
		return (t)_value.intMax;				\
	case OF_NUMBER_TYPE_UINTMAX:					\
		return (t)_value.uIntMax;				\
	case OF_NUMBER_TYPE_PTRDIFF:					\
		return (t)_value.ptrDiff;				\
	case OF_NUMBER_TYPE_INTPTR:					\
		return (t)_value.intPtr;				\
	case OF_NUMBER_TYPE_UINTPTR:					\
		return (t)_value.uIntPtr;				\
	case OF_NUMBER_TYPE_FLOAT:					\
		return (t)_value.float_;				\
	case OF_NUMBER_TYPE_DOUBLE:					\
		return (t)_value.double_;				\
	default:							\
		@throw [OFInvalidFormatException exception];		\
	}

@interface OFNumber ()
+ (instancetype)of_alloc;
- (OFString *)of_JSONRepresentationWithOptions: (int)options
					 depth: (size_t)depth;
@end

@interface OFNumberPlaceholder: OFNumber
@end

@interface OFNumberSingleton: OFNumber
@end

#ifdef OF_OBJFW_RUNTIME
enum {
	TAG_CHAR,
	TAG_SHORT,
	TAG_INT,
	TAG_LONG,
	TAG_LONG_LONG,
	TAG_UNSIGNED_CHAR,
	TAG_UNSIGNED_SHORT,
	TAG_UNSIGNED_INT,
	TAG_UNSIGNED_LONG,
	TAG_UNSIGNED_LONG_LONG,
};
# define TAG_BITS 4
# define TAG_MASK 0xF

@interface OFTaggedPointerNumber: OFNumberSingleton
@end
#endif

static struct {
	Class isa;
} placeholder;

#define SINGLETON(var, sel, val)				\
	static OFNumberSingleton *var;				\
								\
	static void						\
	var##Init(void)						\
	{							\
		var = [[OFNumberSingleton alloc] sel val];	\
	}
SINGLETON(falseNumber, initWithBool:, false)
SINGLETON(trueNumber, initWithBool:, true)
SINGLETON(charZeroNumber, initWithChar:, 0)
SINGLETON(shortZeroNumber, initWithShort:, 0)
SINGLETON(intZeroNumber, initWithInt:, 0)
SINGLETON(longZeroNumber, initWithLong:, 0)
SINGLETON(longLongZeroNumber, initWithLongLong:, 0)
SINGLETON(unsignedCharZeroNumber, initWithUnsignedChar:, 0)
SINGLETON(unsignedShortZeroNumber, initWithUnsignedShort:, 0)
SINGLETON(unsignedIntZeroNumber, initWithUnsignedInt:, 0)
SINGLETON(unsignedLongZeroNumber, initWithUnsignedLong:, 0)
SINGLETON(unsignedLongLongZeroNumber, initWithUnsignedLongLong:, 0)
SINGLETON(floatZeroNumber, initWithFloat:, 0)
SINGLETON(doubleZeroNumber, initWithDouble:, 0)
#undef SINGLETON

#ifdef OF_OBJFW_RUNTIME
static int numberTag;
#endif

static bool
isUnsigned(OFNumber *number)
{
	switch (*number.objCType) {
	case 'B':
		return true;
	case 'C':
		return true;
	case 'S':
		return true;
	case 'I':
		return true;
	case 'L':
		return true;
	case 'Q':
		return true;
	default:
		return false;
	}
}

static bool
isSigned(OFNumber *number)
{
	switch (*number.objCType) {
	case 'c':
		return true;
	case 's':
		return true;
	case 'i':
		return true;
	case 'l':
		return true;
	case 'q':
		return true;
	default:
		return false;
	}
}

static bool
isFloat(OFNumber *number)
{
	switch (*number.objCType) {
	case 'f':
		return true;
	case 'd':
		return true;
	default:
		return false;
	}
}

@implementation OFNumberPlaceholder
- (instancetype)initWithBool: (bool)value
{
	if (value) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, trueNumberInit);
		return (id)trueNumber;
	} else {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, falseNumberInit);
		return (id)falseNumber;
	}
}

#ifdef __clang__
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare"
#endif
- (instancetype)initWithChar: (signed char)value
{
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, charZeroNumberInit);
		return (id)charZeroNumber;
#ifdef OF_OBJFW_RUNTIME
	} else if ((unsigned char)value <= (UINTPTR_MAX >> TAG_BITS)) {
		id ret = objc_createTaggedPointer(numberTag,
		    ((uintptr_t)(unsigned char)value << TAG_BITS) | TAG_CHAR);

		if (ret != nil)
			return ret;
#endif
	}

	return (id)[[OFNumber of_alloc] initWithChar: value];
}

- (instancetype)initWithShort: (short)value
{
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, shortZeroNumberInit);
		return (id)shortZeroNumber;
#ifdef OF_OBJFW_RUNTIME
	} else if ((unsigned short)value <= (UINTPTR_MAX >> TAG_BITS)) {
		id ret = objc_createTaggedPointer(numberTag,
		    ((uintptr_t)(unsigned short)value << TAG_BITS) | TAG_SHORT);

		if (ret != nil)
			return ret;
#endif
	}

	return (id)[[OFNumber of_alloc] initWithShort: value];
}

- (instancetype)initWithInt: (int)value
{
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, intZeroNumberInit);
		return (id)intZeroNumber;
#ifdef OF_OBJFW_RUNTIME
	} else if ((unsigned int)value <= (UINTPTR_MAX >> TAG_BITS)) {
		id ret = objc_createTaggedPointer(numberTag,
		    ((uintptr_t)(unsigned int)value << TAG_BITS) | TAG_INT);

		if (ret != nil)
			return ret;
#endif
	}

	return (id)[[OFNumber of_alloc] initWithInt: value];
}

- (instancetype)initWithLong: (long)value
{
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, longZeroNumberInit);
		return (id)longZeroNumber;
#ifdef OF_OBJFW_RUNTIME
	} else if ((unsigned long)value <= (UINTPTR_MAX >> TAG_BITS)) {
		id ret = objc_createTaggedPointer(numberTag,
		    ((uintptr_t)(unsigned long)value << TAG_BITS) | TAG_LONG);

		if (ret != nil)
			return ret;
#endif
	}

	return (id)[[OFNumber of_alloc] initWithLong: value];
}

- (instancetype)initWithLongLong: (long long)value
{
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, longLongZeroNumberInit);
		return (id)longLongZeroNumber;
#ifdef OF_OBJFW_RUNTIME
	} else if ((unsigned long long)value <= (UINTPTR_MAX >> TAG_BITS)) {
		id ret = objc_createTaggedPointer(numberTag,
		    ((uintptr_t)(unsigned long long)value << TAG_BITS) |
		    TAG_LONG_LONG);

		if (ret != nil)
			return ret;
#endif
	}

	return (id)[[OFNumber of_alloc] initWithLongLong: value];
}

- (instancetype)initWithUnsignedChar: (unsigned char)value
{
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, unsignedCharZeroNumberInit);
		return (id)unsignedCharZeroNumber;
#ifdef OF_OBJFW_RUNTIME
	} else if (value <= (UINTPTR_MAX >> TAG_BITS)) {
		id ret = objc_createTaggedPointer(numberTag,
		    ((uintptr_t)value << TAG_BITS) | TAG_UNSIGNED_CHAR);

		if (ret != nil)
			return ret;
#endif
	}

	return (id)[[OFNumber of_alloc] initWithUnsignedChar: value];
}

- (instancetype)initWithUnsignedShort: (unsigned short)value
{
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, unsignedShortZeroNumberInit);
		return (id)unsignedShortZeroNumber;
#ifdef OF_OBJFW_RUNTIME
	} else if (value <= (UINTPTR_MAX >> TAG_BITS)) {
		id ret = objc_createTaggedPointer(numberTag,
		    ((uintptr_t)value << TAG_BITS) | TAG_UNSIGNED_SHORT);

		if (ret != nil)
			return ret;
#endif
	}

	return (id)[[OFNumber of_alloc] initWithUnsignedShort: value];
}

- (instancetype)initWithUnsignedInt: (unsigned int)value
{
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, unsignedIntZeroNumberInit);
		return (id)unsignedIntZeroNumber;
#ifdef OF_OBJFW_RUNTIME
	} else if (value <= (UINTPTR_MAX >> TAG_BITS)) {
		id ret = objc_createTaggedPointer(numberTag,
		    ((uintptr_t)value << TAG_BITS) | TAG_UNSIGNED_INT);

		if (ret != nil)
			return ret;
#endif
	}

	return (id)[[OFNumber of_alloc] initWithUnsignedInt: value];
}

- (instancetype)initWithUnsignedLong: (unsigned long)value
{
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, unsignedLongZeroNumberInit);
		return (id)unsignedLongZeroNumber;
#ifdef OF_OBJFW_RUNTIME
	} else if (value <= (UINTPTR_MAX >> TAG_BITS)) {
		id ret = objc_createTaggedPointer(numberTag,
		    ((uintptr_t)value << TAG_BITS) | TAG_UNSIGNED_LONG);

		if (ret != nil)
			return ret;
#endif
	}

	return (id)[[OFNumber of_alloc] initWithUnsignedLong: value];
}

- (instancetype)initWithUnsignedLongLong: (unsigned long long)value
{
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, unsignedLongLongZeroNumberInit);
		return (id)unsignedLongLongZeroNumber;
#ifdef OF_OBJFW_RUNTIME
	} else if (value <= (UINTPTR_MAX >> TAG_BITS)) {
		id ret = objc_createTaggedPointer(numberTag,
		    ((uintptr_t)value << TAG_BITS) | TAG_UNSIGNED_LONG_LONG);

		if (ret != nil)
			return ret;
#endif
	}

	return (id)[[OFNumber of_alloc] initWithUnsignedLongLong: value];
}

- (instancetype)initWithFloat: (float)value
{
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, floatZeroNumberInit);
		return (id)floatZeroNumber;
	}

	return (id)[[OFNumber of_alloc] initWithFloat: value];
}

- (instancetype)initWithDouble: (double)value
{
	if (value == 0) {
		static of_once_t once = OF_ONCE_INIT;
		of_once(&once, doubleZeroNumberInit);
		return (id)doubleZeroNumber;
	}

	return (id)[[OFNumber of_alloc] initWithDouble: value];
}

- (instancetype)initWithSerialization: (OFXMLElement *)element
{
	return (id)[[OFNumber of_alloc] initWithSerialization: element];
}
#ifdef __clang__
# pragma clang diagnostic pop
#endif
@end

@implementation OFNumberSingleton
- (instancetype)autorelease
{
	return self;
}

- (instancetype)retain
{
	return self;
}

- (void)release
{
}

- (unsigned int)retainCount
{
	return OF_RETAIN_COUNT_MAX;
}
@end

#ifdef OF_OBJFW_RUNTIME
@implementation OFTaggedPointerNumber
- (const char *)objCType
{
	uintptr_t value = object_getTaggedPointerValue(self);

	switch (value & TAG_MASK) {
	case TAG_CHAR:
		return @encode(signed char);
	case TAG_SHORT:
		return @encode(short);
	case TAG_INT:
		return @encode(int);
	case TAG_LONG:
		return @encode(long);
	case TAG_LONG_LONG:
		return @encode(long long);
	case TAG_UNSIGNED_CHAR:
		return @encode(unsigned char);
	case TAG_UNSIGNED_SHORT:
		return @encode(unsigned short);
	case TAG_UNSIGNED_INT:
		return @encode(unsigned int);
	case TAG_UNSIGNED_LONG:
		return @encode(unsigned long);
	case TAG_UNSIGNED_LONG_LONG:
		return @encode(unsigned long long);
	default:
		@throw [OFInvalidArgumentException exception];
	}
}

# define RETURN_VALUE							   \
	uintptr_t value = object_getTaggedPointerValue(self);		   \
									   \
	switch (value & TAG_MASK) {					   \
	case TAG_CHAR:							   \
		return (signed char)(unsigned char)(value >> TAG_BITS);	   \
	case TAG_SHORT:							   \
		return (short)(unsigned short)(value >> TAG_BITS);	   \
	case TAG_INT:							   \
		return (int)(unsigned int)(value >> TAG_BITS);		   \
	case TAG_LONG:							   \
		return (long)(unsigned long)(value >> TAG_BITS);	   \
	case TAG_LONG_LONG:						   \
		return (long long)(unsigned long long)(value >> TAG_BITS); \
	case TAG_UNSIGNED_CHAR:						   \
		return (unsigned char)(value >> TAG_BITS);		   \
	case TAG_UNSIGNED_SHORT:					   \
		return (unsigned short)(value >> TAG_BITS);		   \
	case TAG_UNSIGNED_INT:						   \
		return (unsigned int)(value >> TAG_BITS);		   \
	case TAG_UNSIGNED_LONG:						   \
		return (unsigned long)(value >> TAG_BITS);		   \
	case TAG_UNSIGNED_LONG_LONG:					   \
		return (unsigned long long)(value >> TAG_BITS);		   \
	default:							   \
		@throw [OFInvalidArgumentException exception];		   \
	}
- (long long)longLongValue
{
	RETURN_VALUE
}

- (unsigned long long)unsignedLongLongValue
{
	RETURN_VALUE
}

- (double)doubleValue
{
	RETURN_VALUE
}
@end
# undef RETURN_VALUE
#endif

@implementation OFNumber
@synthesize type = _type;

+ (void)initialize
+ (instancetype)numberWithBool: (bool)bool_
{
	return [[[self alloc] initWithBool: bool_] autorelease];
}

	if (self != [OFNumber class])
+ (instancetype)numberWithChar: (signed char)sChar
{
	return [[[self alloc] initWithChar: sChar] autorelease];
}

		return;

	placeholder.isa = [OFNumberPlaceholder class];
+ (instancetype)numberWithShort: (signed short)sShort
{

	return [[[self alloc] initWithShort: sShort] autorelease];
}

+ (instancetype)numberWithInt: (signed int)sInt
#ifdef OF_OBJFW_RUNTIME
	numberTag =
{
	return [[[self alloc] initWithInt: sInt] autorelease];
	    objc_registerTaggedPointerClass([OFTaggedPointerNumber class]);
#endif
}

+ (instancetype)numberWithLong: (signed long)sLong
+ (instancetype)of_alloc
{
	return [[[self alloc] initWithLong: sLong] autorelease];
	return [super alloc];
}

+ (instancetype)numberWithLongLong: (signed long long)sLongLong
+ (instancetype)alloc
{
	return [[[self alloc] initWithLongLong: sLongLong] autorelease];
}

	if (self == [OFNumber class])
+ (instancetype)numberWithUnsignedChar: (unsigned char)uChar
{
	return [[[self alloc] initWithUnsignedChar: uChar] autorelease];
}

		return (id)&placeholder;

	return [super alloc];
+ (instancetype)numberWithUnsignedShort: (unsigned short)uShort
{
	return [[[self alloc] initWithUnsignedShort: uShort] autorelease];
}

+ (instancetype)numberWithUnsignedInt: (unsigned int)uInt
+ (instancetype)numberWithBool: (bool)value
{
	return [[[self alloc] initWithUnsignedInt: uInt] autorelease];
	return [[[self alloc] initWithBool: value] autorelease];
}

+ (instancetype)numberWithUnsignedLong: (unsigned long)uLong
{
	return [[[self alloc] initWithUnsignedLong: uLong] autorelease];
}

+ (instancetype)numberWithUnsignedLongLong: (unsigned long long)uLongLong
{
	return [[[self alloc] initWithUnsignedLongLong: uLongLong] autorelease];
}

+ (instancetype)numberWithInt8: (int8_t)int8
+ (instancetype)numberWithChar: (signed char)value
{
	return [[[self alloc] initWithInt8: int8] autorelease];
	return [[[self alloc] initWithChar: value] autorelease];
}

+ (instancetype)numberWithInt16: (int16_t)int16
+ (instancetype)numberWithShort: (short)value
{
	return [[[self alloc] initWithInt16: int16] autorelease];
	return [[[self alloc] initWithShort: value] autorelease];
}

+ (instancetype)numberWithInt32: (int32_t)int32
+ (instancetype)numberWithInt: (int)value
{
	return [[[self alloc] initWithInt32: int32] autorelease];
	return [[[self alloc] initWithInt: value] autorelease];
}

+ (instancetype)numberWithInt64: (int64_t)int64
+ (instancetype)numberWithLong: (long)value
{
	return [[[self alloc] initWithInt64: int64] autorelease];
	return [[[self alloc] initWithLong: value] autorelease];
}

+ (instancetype)numberWithUInt8: (uint8_t)uInt8
+ (instancetype)numberWithLongLong: (long long)value
{
	return [[[self alloc] initWithUInt8: uInt8] autorelease];
	return [[[self alloc] initWithLongLong: value] autorelease];
}

+ (instancetype)numberWithUInt16: (uint16_t)uInt16
+ (instancetype)numberWithUnsignedChar: (unsigned char)value
{
	return [[[self alloc] initWithUInt16: uInt16] autorelease];
	return [[[self alloc] initWithUnsignedChar: value] autorelease];
}

+ (instancetype)numberWithUInt32: (uint32_t)uInt32
{
	return [[[self alloc] initWithUInt32: uInt32] autorelease];
}

+ (instancetype)numberWithUInt64: (uint64_t)uInt64
{
	return [[[self alloc] initWithUInt64: uInt64] autorelease];
}

+ (instancetype)numberWithSize: (size_t)size
+ (instancetype)numberWithUnsignedShort: (unsigned short)value
{
	return [[[self alloc] initWithSize: size] autorelease];
	return [[[self alloc] initWithUnsignedShort: value] autorelease];
}

+ (instancetype)numberWithSSize: (ssize_t)sSize
{
	return [[[self alloc] initWithSSize: sSize] autorelease];
}


+ (instancetype)numberWithIntMax: (intmax_t)intMax
{
	return [[[self alloc] initWithIntMax: intMax] autorelease];
}

+ (instancetype)numberWithUIntMax: (uintmax_t)uIntMax
+ (instancetype)numberWithUnsignedInt: (unsigned int)value
{
	return [[[self alloc] initWithUIntMax: uIntMax] autorelease];
	return [[[self alloc] initWithUnsignedInt: value] autorelease];
}

+ (instancetype)numberWithPtrDiff: (ptrdiff_t)ptrDiff
{
	return [[[self alloc] initWithPtrDiff: ptrDiff] autorelease];
}

+ (instancetype)numberWithIntPtr: (intptr_t)intPtr
+ (instancetype)numberWithUnsignedLong: (unsigned long)value
{
	return [[[self alloc] initWithIntPtr: intPtr] autorelease];
	return [[[self alloc] initWithUnsignedLong: value] autorelease];
}

+ (instancetype)numberWithUIntPtr: (uintptr_t)uIntPtr
+ (instancetype)numberWithUnsignedLongLong: (unsigned long long)value
{
	return [[[self alloc] initWithUIntPtr: uIntPtr] autorelease];
	return [[[self alloc] initWithUnsignedLongLong: value] autorelease];
}

+ (instancetype)numberWithFloat: (float)float_
+ (instancetype)numberWithFloat: (float)value
{
	return [[[self alloc] initWithFloat: float_] autorelease];
	return [[[self alloc] initWithFloat: value] autorelease];
}

+ (instancetype)numberWithDouble: (double)double_
+ (instancetype)numberWithDouble: (double)value
{
	return [[[self alloc] initWithDouble: double_] autorelease];
	return [[[self alloc] initWithDouble: value] autorelease];
}

- (instancetype)init
{
	OF_INVALID_INIT_METHOD
}

- (instancetype)initWithBool: (bool)bool_
- (instancetype)initWithBool: (bool)value
{
	self = [super init];

	_value.bool_ = bool_;
	_type = OF_NUMBER_TYPE_BOOL;
	_value.unsigned_ = value;
	_typeEncoding = @encode(bool);

	return self;
}

- (instancetype)initWithChar: (signed char)sChar
- (instancetype)initWithChar: (signed char)value
{
	self = [super init];

	_value.sChar = sChar;
	_value.signed_ = value;
	_type = OF_NUMBER_TYPE_CHAR;

	_typeEncoding = @encode(signed char);
	return self;
}


- (instancetype)initWithShort: (signed short)sShort
{
	self = [super init];

	_value.sShort = sShort;
	_type = OF_NUMBER_TYPE_SHORT;

	return self;
}

- (instancetype)initWithInt: (signed int)sInt
{
	self = [super init];

	_value.sInt = sInt;
	_type = OF_NUMBER_TYPE_INT;

	return self;
}

- (instancetype)initWithLong: (signed long)sLong
- (instancetype)initWithShort: (short)value
{
	self = [super init];

	_value.sLong = sLong;
	_value.signed_ = value;
	_type = OF_NUMBER_TYPE_LONG;

	_typeEncoding = @encode(short);
	return self;
}


- (instancetype)initWithLongLong: (signed long long)sLongLong
{
	self = [super init];

	_value.sLongLong = sLongLong;
	_type = OF_NUMBER_TYPE_LONGLONG;

	return self;
}

- (instancetype)initWithUnsignedChar: (unsigned char)uChar
- (instancetype)initWithInt: (int)value
{
	self = [super init];

	_value.uChar = uChar;
	_type = OF_NUMBER_TYPE_UCHAR;
	_value.signed_ = value;
	_typeEncoding = @encode(int);

	return self;
}

- (instancetype)initWithUnsignedShort: (unsigned short)uShort
- (instancetype)initWithLong: (long)value
{
	self = [super init];

	_value.uShort = uShort;
	_type = OF_NUMBER_TYPE_USHORT;
	_value.signed_ = value;
	_typeEncoding = @encode(long);

	return self;
}

- (instancetype)initWithUnsignedInt: (unsigned int)uInt
- (instancetype)initWithLongLong: (long long)value
{
	self = [super init];

	_value.uInt = uInt;
	_value.signed_ = value;
	_type = OF_NUMBER_TYPE_UINT;

	_typeEncoding = @encode(long long);
	return self;
}


- (instancetype)initWithUnsignedLong: (unsigned long)uLong
{
	self = [super init];

	_value.uLong = uLong;
	_type = OF_NUMBER_TYPE_ULONG;

	return self;
}

- (instancetype)initWithUnsignedLongLong: (unsigned long long)uLongLong
{
	self = [super init];

}
	_value.uLongLong = uLongLong;
	_type = OF_NUMBER_TYPE_ULONGLONG;

	return self;
}

- (instancetype)initWithInt8: (int8_t)int8
- (instancetype)initWithUnsignedChar: (unsigned char)value
{
	self = [super init];

	_value.int8 = int8;
	_type = OF_NUMBER_TYPE_INT8;
	_value.unsigned_ = value;
	_typeEncoding = @encode(unsigned long);

	return self;
}

- (instancetype)initWithInt16: (int16_t)int16
- (instancetype)initWithUnsignedShort: (unsigned short)value
{
	self = [super init];

	_value.int16 = int16;
	_type = OF_NUMBER_TYPE_INT16;

	_value.unsigned_ = value;
	return self;
}

	_typeEncoding = @encode(unsigned short);
- (instancetype)initWithInt32: (int32_t)int32
{

	self = [super init];

	_value.int32 = int32;
	_type = OF_NUMBER_TYPE_INT32;

	return self;
}

- (instancetype)initWithInt64: (int64_t)int64
{
	self = [super init];

	_value.int64 = int64;
	_type = OF_NUMBER_TYPE_INT64;

	return self;
}

- (instancetype)initWithUInt8: (uint8_t)uInt8
- (instancetype)initWithUnsignedInt: (unsigned int)value
{
	self = [super init];

	_value.uInt8 = uInt8;
	_value.unsigned_ = value;
	_type = OF_NUMBER_TYPE_UINT8;

	_typeEncoding = @encode(unsigned int);
	return self;
}


- (instancetype)initWithUInt16: (uint16_t)uInt16
{
	self = [super init];

	_value.uInt16 = uInt16;
	_type = OF_NUMBER_TYPE_UINT16;

	return self;
}

- (instancetype)initWithUInt32: (uint32_t)uInt32
- (instancetype)initWithUnsignedLong: (unsigned long)value
{
	self = [super init];

	_value.uInt32 = uInt32;
	_type = OF_NUMBER_TYPE_UINT32;
	_value.unsigned_ = value;
	_typeEncoding = @encode(unsigned long);

	return self;
}

- (instancetype)initWithUInt64: (uint64_t)uInt64
- (instancetype)initWithUnsignedLongLong: (unsigned long long)value
{
	self = [super init];

	_value.uInt64 = uInt64;
	_type = OF_NUMBER_TYPE_UINT64;
	_value.unsigned_ = value;
	_typeEncoding = @encode(unsigned long long);

	return self;
}

- (instancetype)initWithSize: (size_t)size
- (instancetype)initWithPtrDiff: (ptrdiff_t)value
{
	self = [super init];

	_value.size = size;
	_value.signed_ = value;
	_type = OF_NUMBER_TYPE_SIZE;

	_typeEncoding = @encode(ptrdiff_t);
	return self;
}


- (instancetype)initWithSSize: (ssize_t)sSize
{
	self = [super init];

	_value.sSize = sSize;
	_type = OF_NUMBER_TYPE_SSIZE;

	return self;
}

- (instancetype)initWithIntMax: (intmax_t)intMax
- (instancetype)initWithIntPtr: (intptr_t)value
{
	self = [super init];

	_value.intMax = intMax;
	_type = OF_NUMBER_TYPE_INTMAX;
	_value.signed_ = value;
	_typeEncoding = @encode(intptr_t);

	return self;
}

- (instancetype)initWithUIntMax: (uintmax_t)uIntMax
- (instancetype)initWithUIntPtr: (uintptr_t)value
{
	self = [super init];

	_value.uIntMax = uIntMax;
	_type = OF_NUMBER_TYPE_UINTMAX;

	_value.unsigned_ = value;
	return self;
}

	_typeEncoding = @encode(uintptr_t);
- (instancetype)initWithPtrDiff: (ptrdiff_t)ptrDiff
{
	self = [super init];

	_value.ptrDiff = ptrDiff;
	_type = OF_NUMBER_TYPE_PTRDIFF;

	return self;
}

- (instancetype)initWithIntPtr: (intptr_t)intPtr
{

	self = [super init];

	_value.intPtr = intPtr;
	_type = OF_NUMBER_TYPE_INTPTR;

	return self;
}

- (instancetype)initWithUIntPtr: (uintptr_t)uIntPtr
{
	self = [super init];

	_value.uIntPtr = uIntPtr;
	_type = OF_NUMBER_TYPE_UINTPTR;

	return self;
}

- (instancetype)initWithFloat: (float)float_
- (instancetype)initWithFloat: (float)value
{
	self = [super init];

	_value.float_ = float_;
	_type = OF_NUMBER_TYPE_FLOAT;
	_value.float_ = value;
	_typeEncoding = @encode(float);

	return self;
}

- (instancetype)initWithDouble: (double)double_
- (instancetype)initWithDouble: (double)value
{
	self = [super init];

	_value.double_ = double_;
	_type = OF_NUMBER_TYPE_DOUBLE;
	_value.float_ = value;
	_typeEncoding = @encode(double);

	return self;
}

- (instancetype)initWithSerialization: (OFXMLElement *)element
{
	self = [super init];

	@try {
		void *pool = objc_autoreleasePoolPush();
		OFString *typeString;

		if (![element.name isEqual: self.className] ||
		if (![element.name isEqual: @"OFNumber"] ||
		    ![element.namespace isEqual: OF_SERIALIZATION_NS])
			@throw [OFInvalidArgumentException exception];

		typeString = [element attributeForName: @"type"].stringValue;

		if ([typeString isEqual: @"boolean"]) {
		if ([typeString isEqual: @"bool"]) {
			_type = OF_NUMBER_TYPE_BOOL;

			if ([[element stringValue] isEqual: @"true"])
				_value.bool_ = true;
			else if ([[element stringValue] isEqual: @"false"])
				_value.bool_ = false;
			OFString *stringValue = element.stringValue;
			if ([stringValue isEqual: @"true"])
				self = [self initWithBool: true];
			else if ([stringValue isEqual: @"false"])
				self = [self initWithBool: false];
			else
				@throw [OFInvalidArgumentException exception];
		} else if ([typeString isEqual: @"unsigned"]) {
		} else if ([typeString isEqual: @"float"]) {
			/*
			 * FIXME: This will fail if the value is bigger than
			unsigned long long value =
			 *	  INTMAX_MAX!
			 */
			_type = OF_NUMBER_TYPE_UINTMAX;
			_value.uIntMax = element.decimalValue;
		} else if ([typeString isEqual: @"signed"]) {
			_type = OF_NUMBER_TYPE_INTMAX;
			_value.intMax = element.decimalValue;
		} else if ([typeString isEqual: @"float"]) {
			    [element unsignedLongLongValueWithBase: 16];

			if (value > UINT64_MAX)
				@throw [OFOutOfRangeException exception];

			self = [self initWithDouble: OF_BSWAP_DOUBLE_IF_LE(
			    OF_INT_TO_DOUBLE_RAW(OF_BSWAP64_IF_LE(value)))];
		} else if ([typeString isEqual: @"signed"])
			_type = OF_NUMBER_TYPE_FLOAT;
			_value.float_ = OF_BSWAP_FLOAT_IF_LE(
			    OF_INT_TO_FLOAT_RAW(OF_BSWAP32_IF_LE(
			    (uint32_t)element.hexadecimalValue)));
		} else if ([typeString isEqual: @"double"]) {
			self = [self initWithLongLong: element.longLongValue];
		else if ([typeString isEqual: @"unsigned"])
			_type = OF_NUMBER_TYPE_DOUBLE;
			_value.double_ = OF_BSWAP_DOUBLE_IF_LE(
			    OF_INT_TO_DOUBLE_RAW(OF_BSWAP64_IF_LE(
			    (uint64_t)element.hexadecimalValue)));
		} else
			self = [self initWithUnsignedLongLong:
			    element.unsignedLongLongValue];
		else
			@throw [OFInvalidArgumentException exception];

		objc_autoreleasePoolPop(pool);
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (const char *)objCType
{
	switch (_type) {
	case OF_NUMBER_TYPE_BOOL:
		return @encode(bool);
	case OF_NUMBER_TYPE_CHAR:
		return @encode(signed char);
	return _typeEncoding;
}

- (void)getValue: (void *)value
	    size: (size_t)size
	case OF_NUMBER_TYPE_SHORT:
		return @encode(signed short);
	case OF_NUMBER_TYPE_INT:
{
	switch (*self.objCType) {
#define CASE(enc, type, property)					\
	case enc: {							\
		return @encode(signed int);
	case OF_NUMBER_TYPE_LONG:
		return @encode(signed long);
		type tmp = (type)self.property;				\
									\
		if (size != sizeof(type))				\
	case OF_NUMBER_TYPE_LONGLONG:
		return @encode(signed long long);
			@throw [OFOutOfRangeException exception];	\
	case OF_NUMBER_TYPE_UCHAR:
		return @encode(unsigned char);
									\
		memcpy(value, &tmp, size);				\
	case OF_NUMBER_TYPE_USHORT:
		return @encode(unsigned short);
	case OF_NUMBER_TYPE_UINT:
		return @encode(unsigned int);
	case OF_NUMBER_TYPE_ULONG:
		return @encode(unsigned long);
	case OF_NUMBER_TYPE_ULONGLONG:
		return @encode(unsigned long long);
	case OF_NUMBER_TYPE_INT8:
		return @encode(int8_t);
	case OF_NUMBER_TYPE_INT16:
		return @encode(int16_t);
	case OF_NUMBER_TYPE_INT32:
		return @encode(int32_t);
		break;							\
	case OF_NUMBER_TYPE_INT64:
		return @encode(int64_t);
	case OF_NUMBER_TYPE_UINT8:
		return @encode(uint8_t);
	}
	CASE('B', bool, unsignedLongLongValue)
	CASE('c', signed char, longLongValue)
	CASE('s', short, longLongValue)
	case OF_NUMBER_TYPE_UINT16:
		return @encode(uint16_t);
	case OF_NUMBER_TYPE_UINT32:
		return @encode(uint32_t);
	case OF_NUMBER_TYPE_UINT64:
		return @encode(uint64_t);
	case OF_NUMBER_TYPE_SIZE:
		return @encode(size_t);
	CASE('i', int, longLongValue)
	CASE('l', long, longLongValue)
	CASE('q', long long, longLongValue)
	CASE('C', unsigned char, unsignedLongLongValue)
	CASE('S', unsigned short, unsignedLongLongValue)
	CASE('I', unsigned int, unsignedLongLongValue)
	CASE('L', unsigned long, unsignedLongLongValue)
	CASE('Q', unsigned long long, unsignedLongLongValue)
	case OF_NUMBER_TYPE_SSIZE:
		return @encode(ssize_t);
	case OF_NUMBER_TYPE_INTMAX:
		return @encode(intmax_t);
	case OF_NUMBER_TYPE_UINTMAX:
		return @encode(uintmax_t);
	case OF_NUMBER_TYPE_PTRDIFF:
		return @encode(ptrdiff_t);
	case OF_NUMBER_TYPE_INTPTR:
		return @encode(intptr_t);
	case OF_NUMBER_TYPE_UINTPTR:
		return @encode(uintptr_t);
	case OF_NUMBER_TYPE_FLOAT:
		return @encode(float);
	CASE('f', float, doubleValue)
	case OF_NUMBER_TYPE_DOUBLE:
		return @encode(double);
	CASE('d', double, doubleValue)
#undef CASE
	default:
		@throw [OFInvalidFormatException exception];
	}
}

- (void)getValue: (void *)value
- (long long)longLongValue
	    size: (size_t)size
{
	switch (_type) {
	case OF_NUMBER_TYPE_BOOL:
		if (size != sizeof(bool))
			@throw [OFOutOfRangeException exception];

		memcpy(value, &_value.bool_, sizeof(bool));
	if (isFloat(self))
		return _value.float_;
		break;
	case OF_NUMBER_TYPE_CHAR:
		if (size != sizeof(signed char))
			@throw [OFOutOfRangeException exception];

		memcpy(value, &_value.sChar, sizeof(signed char));
	else if (isSigned(self))
		return _value.signed_;
		break;
	case OF_NUMBER_TYPE_SHORT:
		if (size != sizeof(signed short))
			@throw [OFOutOfRangeException exception];

	else if (isUnsigned(self))
		memcpy(value, &_value.sShort, sizeof(signed short));
		break;
	case OF_NUMBER_TYPE_INT:
		if (size != sizeof(signed int))
			@throw [OFOutOfRangeException exception];

		return _value.unsigned_;
		memcpy(value, &_value.sInt, sizeof(signed int));
		break;
	case OF_NUMBER_TYPE_LONG:
		if (size != sizeof(signed long))
			@throw [OFOutOfRangeException exception];

	else
		memcpy(value, &_value.sLong, sizeof(signed long));
		break;
	case OF_NUMBER_TYPE_LONGLONG:
		if (size != sizeof(signed long long))
			@throw [OFOutOfRangeException exception];

		@throw [OFInvalidFormatException exception];
}
		memcpy(value, &_value.sLongLong, sizeof(signed long long));
		break;
	case OF_NUMBER_TYPE_UCHAR:
		if (size != sizeof(unsigned char))
			@throw [OFOutOfRangeException exception];

		memcpy(value, &_value.uChar, sizeof(unsigned char));
		break;
	case OF_NUMBER_TYPE_USHORT:
		if (size != sizeof(unsigned short))
- (unsigned long long)unsignedLongLongValue
			@throw [OFOutOfRangeException exception];

{
		memcpy(value, &_value.uShort, sizeof(unsigned short));
		break;
	case OF_NUMBER_TYPE_UINT:
		if (size != sizeof(unsigned int))
			@throw [OFOutOfRangeException exception];

		memcpy(value, &_value.uInt, sizeof(unsigned int));
	if (isFloat(self))
		return _value.float_;
		break;
	case OF_NUMBER_TYPE_ULONG:
		if (size != sizeof(unsigned long))
			@throw [OFOutOfRangeException exception];

		memcpy(value, &_value.uLong, sizeof(unsigned long));
	else if (isSigned(self))
		return _value.signed_;
		break;
	case OF_NUMBER_TYPE_ULONGLONG:
		if (size != sizeof(unsigned long long))
	else if (isUnsigned(self))
			@throw [OFOutOfRangeException exception];

		return _value.unsigned_;
		memcpy(value, &_value.uLongLong, sizeof(unsigned long long));
		break;
	case OF_NUMBER_TYPE_INT8:
		if (size != sizeof(int8_t))
			@throw [OFOutOfRangeException exception];

	else
		memcpy(value, &_value.int8, sizeof(int8_t));
		break;
	case OF_NUMBER_TYPE_INT16:
		if (size != sizeof(int16_t))
			@throw [OFOutOfRangeException exception];

		@throw [OFInvalidFormatException exception];
}
		memcpy(value, &_value.int16, sizeof(int16_t));
		break;
	case OF_NUMBER_TYPE_INT32:
		if (size != sizeof(int32_t))
			@throw [OFOutOfRangeException exception];

		memcpy(value, &_value.int32, sizeof(int32_t));
- (double)doubleValue
		break;
	case OF_NUMBER_TYPE_INT64:
		if (size != sizeof(int64_t))
			@throw [OFOutOfRangeException exception];

{
		memcpy(value, &_value.int64, sizeof(int64_t));
		break;
	case OF_NUMBER_TYPE_UINT8:
		if (size != sizeof(uint8_t))
			@throw [OFOutOfRangeException exception];

		memcpy(value, &_value.uInt8, sizeof(uint8_t));
	if (isFloat(self))
		return _value.float_;
		break;
	case OF_NUMBER_TYPE_UINT16:
		if (size != sizeof(uint16_t))
			@throw [OFOutOfRangeException exception];

		memcpy(value, &_value.uInt16, sizeof(uint16_t));
	else if (isSigned(self))
		return _value.signed_;
		break;
	case OF_NUMBER_TYPE_UINT32:
		if (size != sizeof(uint32_t))
			@throw [OFOutOfRangeException exception];

		memcpy(value, &_value.uInt32, sizeof(uint32_t));
		break;
	else if (isUnsigned(self))
		return _value.unsigned_;
	else
	case OF_NUMBER_TYPE_UINT64:
		if (size != sizeof(uint64_t))
			@throw [OFOutOfRangeException exception];

		@throw [OFInvalidFormatException exception];
}
		memcpy(value, &_value.uInt64, sizeof(uint64_t));
		break;
	case OF_NUMBER_TYPE_SIZE:
		if (size != sizeof(size_t))
			@throw [OFOutOfRangeException exception];

		memcpy(value, &_value.size, sizeof(size_t));
		break;
	case OF_NUMBER_TYPE_SSIZE:
		if (size != sizeof(ssize_t))
			@throw [OFOutOfRangeException exception];

- (bool)boolValue
		memcpy(value, &_value.sSize, sizeof(ssize_t));
		break;
	case OF_NUMBER_TYPE_INTMAX:
		if (size != sizeof(intmax_t))
			@throw [OFOutOfRangeException exception];

{
		memcpy(value, &_value.intMax, sizeof(intmax_t));
		break;
	case OF_NUMBER_TYPE_UINTMAX:
		if (size != sizeof(uintmax_t))
			@throw [OFOutOfRangeException exception];

	return (bool)self.unsignedLongLongValue;
		memcpy(value, &_value.uIntMax, sizeof(uintmax_t));
		break;
	case OF_NUMBER_TYPE_PTRDIFF:
		if (size != sizeof(ptrdiff_t))
			@throw [OFOutOfRangeException exception];

}
		memcpy(value, &_value.ptrDiff, sizeof(ptrdiff_t));
		break;
	case OF_NUMBER_TYPE_INTPTR:
		if (size != sizeof(intptr_t))
			@throw [OFOutOfRangeException exception];

		memcpy(value, &_value.intPtr, sizeof(intptr_t));
		break;
	case OF_NUMBER_TYPE_UINTPTR:
		if (size != sizeof(uintptr_t))
- (signed char)charValue
			@throw [OFOutOfRangeException exception];

{
		memcpy(value, &_value.uIntPtr, sizeof(uintptr_t));
		break;
	case OF_NUMBER_TYPE_FLOAT:
		if (size != sizeof(float))
			@throw [OFOutOfRangeException exception];

		memcpy(value, &_value.float_, sizeof(float));
		break;
	case OF_NUMBER_TYPE_DOUBLE:
		if (size != sizeof(double))
			@throw [OFOutOfRangeException exception];

		memcpy(value, &_value.double_, sizeof(double));
		break;
	default:
		@throw [OFInvalidFormatException exception];
	}
}

- (bool)boolValue
{
	RETURN_AS(bool)
}

- (signed char)charValue
	return (signed char)self.longLongValue;
{
	RETURN_AS(signed char)
}

- (signed short)shortValue
- (short)shortValue
{
	RETURN_AS(signed short)
	return (short)self.longLongValue;
}

- (signed int)intValue
- (int)intValue
{
	RETURN_AS(signed int)
}

- (signed long)longValue
{
	return (int)self.longLongValue;
{
	RETURN_AS(signed long)
}

- (signed long long)longLongValue
- (long)longValue
{
	RETURN_AS(signed long long)
	return (long)self.longLongValue;
}

- (unsigned char)unsignedCharValue
{
	RETURN_AS(unsigned char)
}

- (unsigned short)unsignedShortValue
{
	RETURN_AS(unsigned short)
}

- (unsigned int)unsignedIntValue
{
	RETURN_AS(unsigned int)
}

- (unsigned long)unsignedLongValue
{
	RETURN_AS(unsigned long)
}

- (unsigned long long)unsignedLongLongValue
{
	RETURN_AS(unsigned long long)
}

- (int8_t)int8Value
{
	RETURN_AS(int8_t)
}

{
- (int16_t)int16Value
{
	RETURN_AS(int16_t)
}

	return (unsigned char)self.unsignedLongLongValue;
- (int32_t)int32Value
{
	RETURN_AS(int32_t)
}

- (int64_t)int64Value
{
	RETURN_AS(int64_t)
}

- (unsigned short)unsignedShortValue
- (uint8_t)uInt8Value
{
	RETURN_AS(uint8_t)
}

	return (unsigned short)self.unsignedLongLongValue;
- (uint16_t)uInt16Value
{
	RETURN_AS(uint16_t)
}

- (uint32_t)uInt32Value
{
	RETURN_AS(uint32_t)
}

- (unsigned int)unsignedIntValue
- (uint64_t)uInt64Value
{
	RETURN_AS(uint64_t)
}

	return (unsigned int)self.unsignedLongLongValue;
- (size_t)sizeValue
{
	RETURN_AS(size_t)
}

- (ssize_t)sSizeValue
{
	RETURN_AS(ssize_t)
}

- (unsigned long)unsignedLongValue
- (intmax_t)intMaxValue
{
	RETURN_AS(intmax_t)
}

	return (unsigned long)self.unsignedLongLongValue;
- (uintmax_t)uIntMaxValue
{
	RETURN_AS(uintmax_t)
}

- (ptrdiff_t)ptrDiffValue
{
	RETURN_AS(ptrdiff_t)
}

- (intptr_t)intPtrValue
{
	RETURN_AS(intptr_t)
}

- (uintptr_t)uIntPtrValue
{
	RETURN_AS(uintptr_t)
}

- (float)floatValue
{
	RETURN_AS(float)
}

- (double)doubleValue
	return (float)self.doubleValue;
{
	RETURN_AS(double)
}

- (bool)isEqual: (id)object
{
	OFNumber *number;

	if (object == self)
		return true;

	if (![object isKindOfClass: [OFNumber class]])
		return false;

	number = object;

	if (_type & OF_NUMBER_TYPE_FLOAT ||
	    number->_type & OF_NUMBER_TYPE_FLOAT) {
	if (isFloat(self) || isFloat(number)) {
		double value1 = number.doubleValue;
		double value2 = self.doubleValue;

		if (isnan(value1) && isnan(value2))
			return true;
		if (isnan(value1) || isnan(value2))
			return false;

		return (value1 == value2);
	}

	if (_type & OF_NUMBER_TYPE_SIGNED ||
	    number->_type & OF_NUMBER_TYPE_SIGNED)
		return (number.intMaxValue == self.intMaxValue);
	if (isSigned(self) || isSigned(number))
		return (number.longLongValue == self.longLongValue);

	return (number.uIntMaxValue == self.uIntMaxValue);
	return (number.unsignedLongLongValue == self.unsignedLongLongValue);
}

- (of_comparison_result_t)compare: (id <OFComparing>)object
{
	OFNumber *number;

	if (![(id)object isKindOfClass: [OFNumber class]])
		@throw [OFInvalidArgumentException exception];

	number = (OFNumber *)object;

	if (_type & OF_NUMBER_TYPE_FLOAT ||
	    number->_type & OF_NUMBER_TYPE_FLOAT) {
	if (isFloat(self) || isFloat(number)) {
		double double1 = self.doubleValue;
		double double2 = number.doubleValue;

		if (double1 > double2)
			return OF_ORDERED_DESCENDING;
		if (double1 < double2)
			return OF_ORDERED_ASCENDING;

		return OF_ORDERED_SAME;
	} else if (_type & OF_NUMBER_TYPE_SIGNED ||
	} else if (isSigned(self) || isSigned(number)) {
	    number->_type & OF_NUMBER_TYPE_SIGNED) {
		intmax_t int1 = self.intMaxValue;
		intmax_t int2 = number.intMaxValue;
		long long int1 = self.longLongValue;
		long long int2 = number.longLongValue;

		if (int1 > int2)
			return OF_ORDERED_DESCENDING;
		if (int1 < int2)
			return OF_ORDERED_ASCENDING;

		return OF_ORDERED_SAME;
	} else {
		uintmax_t uint1 = self.uIntMaxValue;
		uintmax_t uint2 = number.uIntMaxValue;
		unsigned long long uint1 = self.unsignedLongLongValue;
		unsigned long long uint2 = number.unsignedLongLongValue;

		if (uint1 > uint2)
			return OF_ORDERED_DESCENDING;
		if (uint1 < uint2)
			return OF_ORDERED_ASCENDING;

		return OF_ORDERED_SAME;
	}
}

- (uint32_t)hash
{
	of_number_type_t type = _type;
	uint32_t hash;

	/* Do we really need signed to represent this number? */
	if (type & OF_NUMBER_TYPE_SIGNED && self.intMaxValue >= 0)
		type &= ~OF_NUMBER_TYPE_SIGNED;

	/* Do we really need floating point to represent this number? */
	if (type & OF_NUMBER_TYPE_FLOAT) {
		double v = self.doubleValue;

		if (v < 0) {
			if (v == self.intMaxValue) {
				type &= ~OF_NUMBER_TYPE_FLOAT;
				type |= OF_NUMBER_TYPE_SIGNED;
			}
		} else {
			if (v == self.uIntMaxValue)
				type &= ~OF_NUMBER_TYPE_FLOAT;
		}
	}

	OF_HASH_INIT(hash);

	if (type & OF_NUMBER_TYPE_FLOAT) {
	if (isFloat(self)) {
		double d;

		if (isnan(self.doubleValue))
			return 0;

		d = OF_BSWAP_DOUBLE_IF_BE(self.doubleValue);

		for (uint_fast8_t i = 0; i < sizeof(double); i++)
			OF_HASH_ADD(hash, ((char *)&d)[i]);
	} else if (type & OF_NUMBER_TYPE_SIGNED) {
		intmax_t v = self.intMaxValue * -1;
	} else if (isSigned(self) || isUnsigned(self)) {
		unsigned long long value = self.unsignedLongLongValue;

		while (v != 0) {
			OF_HASH_ADD(hash, v & 0xFF);
			v >>= 8;
		while (value != 0) {
			OF_HASH_ADD(hash, value & 0xFF);
			value >>= 8;
		}

		OF_HASH_ADD(hash, 1);
	} else {
	} else
		uintmax_t v = self.uIntMaxValue;

		@throw [OFInvalidFormatException exception];
		while (v != 0) {
			OF_HASH_ADD(hash, v & 0xFF);
			v >>= 8;
		}
	}

	OF_HASH_FINALIZE(hash);

	return hash;
}

- (id)copy
{
	return [self retain];
}

- (OFString *)description
{
	return [self stringValue];
}

- (OFString *)stringValue
{
	OFMutableString *ret;

	if (*self.objCType == 'B')
	switch (_type) {
	case OF_NUMBER_TYPE_BOOL:
		return (_value.bool_ ? @"true" : @"false");
		return (self.boolValue ? @"true" : @"false");
	case OF_NUMBER_TYPE_UCHAR:
	case OF_NUMBER_TYPE_USHORT:
	case OF_NUMBER_TYPE_UINT:
	if (isFloat(self))
	case OF_NUMBER_TYPE_ULONG:
	case OF_NUMBER_TYPE_ULONGLONG:
	case OF_NUMBER_TYPE_UINT8:
	case OF_NUMBER_TYPE_UINT16:
	case OF_NUMBER_TYPE_UINT32:
	case OF_NUMBER_TYPE_UINT64:
	case OF_NUMBER_TYPE_SIZE:
	case OF_NUMBER_TYPE_UINTMAX:
	case OF_NUMBER_TYPE_UINTPTR:
		return [OFString stringWithFormat: @"%ju", self.uIntMaxValue];
		return [OFString stringWithFormat: @"%g", self.doubleValue];
	case OF_NUMBER_TYPE_CHAR:
	case OF_NUMBER_TYPE_SHORT:
	case OF_NUMBER_TYPE_INT:
	if (isSigned(self))
	case OF_NUMBER_TYPE_LONG:
	case OF_NUMBER_TYPE_LONGLONG:
	case OF_NUMBER_TYPE_INT8:
	case OF_NUMBER_TYPE_INT16:
	case OF_NUMBER_TYPE_INT32:
	case OF_NUMBER_TYPE_INT64:
	case OF_NUMBER_TYPE_SSIZE:
	case OF_NUMBER_TYPE_INTMAX:
	case OF_NUMBER_TYPE_PTRDIFF:
	case OF_NUMBER_TYPE_INTPTR:
		return [OFString stringWithFormat: @"%jd", self.intMaxValue];
		return [OFString stringWithFormat: @"%lld", self.longLongValue];
	case OF_NUMBER_TYPE_FLOAT:
		ret = [OFMutableString stringWithFormat: @"%g", _value.float_];

	if (isUnsigned(self))
		if (![ret containsString: @"."])
			[ret appendString: @".0"];

		[ret makeImmutable];

		return ret;
	case OF_NUMBER_TYPE_DOUBLE:
		ret = [OFMutableString stringWithFormat: @"%g", _value.double_];

		return [OFString stringWithFormat: @"%llu",
						   self.unsignedLongLongValue];
		if (![ret containsString: @"."])
			[ret appendString: @".0"];

		[ret makeImmutable];

		return ret;
	default:
		@throw [OFInvalidFormatException exception];
	}
}
	@throw [OFInvalidFormatException exception];
}


- (OFXMLElement *)XMLElementBySerializing
{
	void *pool = objc_autoreleasePoolPush();
	OFXMLElement *element;

	element = [OFXMLElement elementWithName: self.className
	element = [OFXMLElement elementWithName: @"OFNumber"
				      namespace: OF_SERIALIZATION_NS
				    stringValue: self.description];

	switch (_type) {
	if (*self.objCType == 'B')
	case OF_NUMBER_TYPE_BOOL:
		[element addAttributeWithName: @"type"
				  stringValue: @"boolean"];
		break;
	case OF_NUMBER_TYPE_UCHAR:
	case OF_NUMBER_TYPE_USHORT:
	case OF_NUMBER_TYPE_UINT:
	case OF_NUMBER_TYPE_ULONG:
	case OF_NUMBER_TYPE_ULONGLONG:
	case OF_NUMBER_TYPE_UINT8:
	case OF_NUMBER_TYPE_UINT16:
	case OF_NUMBER_TYPE_UINT32:
	case OF_NUMBER_TYPE_UINT64:
	case OF_NUMBER_TYPE_SIZE:
	case OF_NUMBER_TYPE_UINTMAX:
	case OF_NUMBER_TYPE_UINTPTR:
		[element addAttributeWithName: @"type"
				  stringValue: @"unsigned"];
				  stringValue: @"bool"];
		break;
	case OF_NUMBER_TYPE_CHAR:
	case OF_NUMBER_TYPE_SHORT:
	case OF_NUMBER_TYPE_INT:
	case OF_NUMBER_TYPE_LONG:
	case OF_NUMBER_TYPE_LONGLONG:
	case OF_NUMBER_TYPE_INT8:
	case OF_NUMBER_TYPE_INT16:
	case OF_NUMBER_TYPE_INT32:
	case OF_NUMBER_TYPE_INT64:
	case OF_NUMBER_TYPE_SSIZE:
	case OF_NUMBER_TYPE_INTMAX:
	case OF_NUMBER_TYPE_PTRDIFF:
	case OF_NUMBER_TYPE_INTPTR:
		[element addAttributeWithName: @"type"
				  stringValue: @"signed"];
		break;
	case OF_NUMBER_TYPE_FLOAT:
		[element addAttributeWithName: @"type"
				  stringValue: @"float"];
		element.stringValue = [OFString stringWithFormat: @"%08" PRIx32,
		    OF_BSWAP32_IF_LE(OF_FLOAT_TO_INT_RAW(OF_BSWAP_FLOAT_IF_LE(
		    _value.float_)))];

	else if (isFloat(self)) {
		break;
	case OF_NUMBER_TYPE_DOUBLE:
		[element addAttributeWithName: @"type"
				  stringValue: @"double"];
				  stringValue: @"float"];
		element.stringValue = [OFString
		    stringWithFormat: @"%016" PRIx64,
		    OF_BSWAP64_IF_LE(OF_DOUBLE_TO_INT_RAW(OF_BSWAP_DOUBLE_IF_LE(
		    _value.double_)))];

		break;
	default:
		    self.doubleValue)))];
	} else if (isSigned(self))
		[element addAttributeWithName: @"type"
				  stringValue: @"signed"];
	else if (isUnsigned(self))
		[element addAttributeWithName: @"type"
				  stringValue: @"unsigned"];
	else
		@throw [OFInvalidFormatException exception];
	}

	[element retain];

	objc_autoreleasePoolPop(pool);

	return [element autorelease];
}
1248
1249
1250
1251
1252
1253
1254
1255
1256


1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274

1275
1276
1277


1278
1279
1280
1281

1282
1283

1284
1285
1286
1287
1288
1289
1290
1291

1292
1293

1294
1295
1296
1297
1298
1299
1300
1301
1302


1303
1304
1305
1306
1307
1308
1309
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







-
-
+
+


















+

-
-
+
+



-
+

-
+







-
+

-
+







-
-
+
+







}

- (OFString *)of_JSONRepresentationWithOptions: (int)options
					 depth: (size_t)depth
{
	double doubleValue;

	if (_type == OF_NUMBER_TYPE_BOOL)
		return (_value.bool_ ? @"true" : @"false");
	if (*self.objCType == 'B')
		return (self.boolValue ? @"true" : @"false");

	doubleValue = self.doubleValue;
	if (isinf(doubleValue)) {
		if (options & OF_JSON_REPRESENTATION_JSON5) {
			if (doubleValue > 0)
				return @"Infinity";
			else
				return @"-Infinity";
		} else
			@throw [OFInvalidArgumentException exception];
	}

	return self.description;
}

- (OFData *)messagePackRepresentation
{
	OFMutableData *data;
	const char *typeEncoding = self.objCType;

	if (_type == OF_NUMBER_TYPE_BOOL) {
		uint8_t type = (_value.bool_ ? 0xC3 : 0xC2);
	if (*typeEncoding == 'B') {
		uint8_t type = (self.boolValue ? 0xC3 : 0xC2);

		data = [OFMutableData dataWithItems: &type
					      count: 1];
	} else if (_type == OF_NUMBER_TYPE_FLOAT) {
	} else if (*typeEncoding == 'f') {
		uint8_t type = 0xCA;
		float tmp = OF_BSWAP_FLOAT_IF_LE(_value.float_);
		float tmp = OF_BSWAP_FLOAT_IF_LE(self.floatValue);

		data = [OFMutableData dataWithItemSize: 1
					      capacity: 5];

		[data addItem: &type];
		[data addItems: &tmp
			 count: sizeof(tmp)];
	} else if (_type == OF_NUMBER_TYPE_DOUBLE) {
	} else if (*typeEncoding == 'd') {
		uint8_t type = 0xCB;
		double tmp = OF_BSWAP_DOUBLE_IF_LE(_value.double_);
		double tmp = OF_BSWAP_DOUBLE_IF_LE(self.doubleValue);

		data = [OFMutableData dataWithItemSize: 1
					      capacity: 9];

		[data addItem: &type];
		[data addItems: &tmp
			 count: sizeof(tmp)];
	} else if (_type & OF_NUMBER_TYPE_SIGNED) {
		intmax_t value = self.intMaxValue;
	} else if (isSigned(self)) {
		long long value = self.longLongValue;

		if (value >= -32 && value < 0) {
			uint8_t tmp = 0xE0 | ((uint8_t)(value - 32) & 0x1F);

			data = [OFMutableData dataWithItems: &tmp
						      count: 1];
		} else if (value >= INT8_MIN && value <= INT8_MAX) {
1343
1344
1345
1346
1347
1348
1349
1350
1351


1352
1353
1354
1355
1356
1357
1358
1187
1188
1189
1190
1191
1192
1193


1194
1195
1196
1197
1198
1199
1200
1201
1202







-
-
+
+







						      capacity: 9];

			[data addItem: &type];
			[data addItems: &tmp
				 count: sizeof(tmp)];
		} else
			@throw [OFOutOfRangeException exception];
	} else {
		uintmax_t value = self.uIntMaxValue;
	} else if (isUnsigned(self)) {
		unsigned long long value = self.unsignedLongLongValue;

		if (value <= 127) {
			uint8_t tmp = ((uint8_t)value & 0x7F);

			data = [OFMutableData dataWithItems: &tmp
						      count: 1];
		} else if (value <= UINT8_MAX) {
1392
1393
1394
1395
1396
1397
1398
1399


1400
1401
1402
1403
1404
1405
1236
1237
1238
1239
1240
1241
1242

1243
1244
1245
1246
1247
1248
1249
1250







-
+
+






						      capacity: 9];

			[data addItem: &type];
			[data addItems: &tmp
				 count: sizeof(tmp)];
		} else
			@throw [OFOutOfRangeException exception];
	}
	} else
		@throw [OFInvalidFormatException exception];

	[data makeImmutable];

	return data;
}
@end