1 package frost.core
6
7 uses frost.unsafe.Pointer
8
9
12 class UInt64 : Value, HashKey<UInt64>, Comparable<UInt64>, Formattable {
13 @private
14 class Bits : ListView<Bit> {
15 def value:UInt64
16 init(value:UInt64) {
17 self.value := value
18 }
19
20
21 @override
22 function [](index:Int):Bit {
23 return value && (1 << index) != 0
24 }
25
26
29 @override
30 function get_count():Int {
31 return 64
32 }
33
34 @override
35 function get_iterator():Iterator<Bit> {
36 return org.frostlang.frost.IntBitIterator(value, 1 << (64->builtin_uint64 - 1))
37 }
38 }
39
42 constant MIN:UInt64 := 0
43
44
47 constant MAX:UInt64 := 18446744073709551615
48
49 @package
50 def value:builtin_uint64
51
52
53 @implicit
54 init(value:builtin_uint64) {
55 self.value := value
56 }
57
58
59 @implicit
60 @priority(-6)
61 init(value:UInt8) {
62 self.value := value.value->builtin_uint64
63 }
64
65
66 @implicit
67 @priority(-5)
68 init(value:UInt16) {
69 self.value := value.value->builtin_uint64
70 }
71
72
73 @implicit
74 @priority(-4)
75 init(value:UInt32) {
76 self.value := value.value->builtin_uint64
77 }
78
79
80 @implicit
81 @priority(-4)
82 init(value:UInt) {
83 self.value := value.value->builtin_uint64
84 }
85
86
87 @priority(20)
88 function +(other:UInt64):UInt64 {
89 return value + other.value
90 }
91
92
93 @priority(20)
94 function +&(other:UInt64):UInt64 {
95 return value +& other.value
96 }
97
98
99 @priority(20)
100 function -(other:UInt64):UInt64 {
101 return value - other.value
102 }
103
104
105 @priority(20)
106 function -&(other:UInt64):UInt64 {
107 return value -& other.value
108 }
109
110
111 function -():UInt64 {
112 return UInt64(-value)
113 }
114
115
116 @priority(20)
117 function *(other:UInt64):UInt64 {
118 return value * other.value
119 }
120
121
122 @priority(20)
123 function *&(other:UInt64):UInt64 {
124 return value *& other.value
125 }
126
127
128 @priority(20)
129 function //(other:UInt64):UInt64 {
130 return value // other.value
131 }
132
133
134 @priority(20)
135 function //&(other:UInt64):UInt64 {
136 return value //& other.value
137 }
138
139
140 @priority(20)
141 function %(other:UInt64):UInt64 {
142 return value % other.value
143 }
144
145
146 @priority(7)
147 function /(other:Int8):Real64 {
148 return value->builtin_float64 / other.value->builtin_float64
149 }
150
151
152 @priority(9)
153 function /(other:Int16):Real64 {
154 return value->builtin_float64 / other.value->builtin_float64
155 }
156
157
158 @priority(11)
159 function /(other:Int32):Real64 {
160 return value->builtin_float64 / other.value->builtin_float64
161 }
162
163
164 @priority(13)
165 function /(other:Int64):Real64 {
166 return value->builtin_float64 / other.value->builtin_float64
167 }
168
169
170 @priority(6)
171 function /(other:UInt8):Real64 {
172 return value->builtin_float64 / other.value->builtin_float64
173 }
174
175
176 @priority(8)
177 function /(other:UInt16):Real64 {
178 return value->builtin_float64 / other.value->builtin_float64
179 }
180
181
182 @priority(10)
183 function /(other:UInt32):Real64 {
184 return value->builtin_float64 / other.value->builtin_float64
185 }
186
187
188 @priority(20)
189 function /(other:UInt64):Real64 {
190 return value->builtin_float64 / other.value->builtin_float64
191 }
192
193
194 @priority(16)
195 function /(other:Real32):Real64 {
196 return value->builtin_float64 / other.value->builtin_float64
197 }
198
199
200 @priority(17)
201 function /(other:Real64):Real64 {
202 return value->builtin_float64 / other.value
203 }
204
205
206 function !!():UInt64 {
207 return UInt64(!!value)
208 }
209
210
211 @priority(13)
212 function &&(other:Int64):Int64 {
213 return value->builtin_int64 && other.value
214 }
215
216
217 @priority(20)
218 function &&(other:UInt64):UInt64 {
219 return value && other.value
220 }
221
222
223 @priority(13)
224 function ||(other:Int64):Int64 {
225 return value->builtin_int64 || other.value
226 }
227
228
229 @priority(20)
230 function ||(other:UInt64):UInt64 {
231 return value || other.value
232 }
233
234
235 @priority(13)
236 function ~~(other:Int64):Int64 {
237 return value->builtin_int64 ~~ other.value
238 }
239
240
241 @priority(20)
242 function ~~(other:UInt64):UInt64 {
243 return value ~~ other.value
244 }
245
246
247 @priority(20)
248 function <<(other:UInt64):UInt64 {
249 return value << other.value
250 }
251
252
253 @priority(20)
254 function <<&(other:UInt64):UInt64 {
255 return value <<& other.value
256 }
257
258
259 @priority(20)
260 function >>(other:UInt64):UInt64 {
261 return value >> other.value
262 }
263
264
265 @priority(6)
266 function =(other:UInt8):Bit {
267 return value = other.value->builtin_uint64
268 }
269
270
271 @priority(8)
272 function =(other:UInt16):Bit {
273 return value = other.value->builtin_uint64
274 }
275
276
277 @priority(10)
278 function =(other:UInt32):Bit {
279 return value = other.value->builtin_uint64
280 }
281
282
283 @override
284 @priority(20)
285 function =(other:UInt64):Bit {
286 return value = other.value
287 }
288
289
290 @priority(14)
291 function =(other:UInt):Bit {
292 return value = other.value->builtin_uint64
293 }
294
295
296 @priority(6)
297 function !=(other:UInt8):Bit {
298 return value != other.value->builtin_uint64
299 }
300
301
302 @priority(8)
303 function !=(other:UInt16):Bit {
304 return value != other.value->builtin_uint64
305 }
306
307
308 @priority(10)
309 function !=(other:UInt32):Bit {
310 return value != other.value->builtin_uint64
311 }
312
313
314 @override
315 @priority(20)
316 function !=(other:UInt64):Bit {
317 return value != other.value
318 }
319
320
321 @priority(14)
322 function !=(other:UInt):Bit {
323 return value != other.value->builtin_uint64
324 }
325
326
327 @priority(6)
328 function <(other:UInt8):Bit {
329 return value < other.value->< other.value->builtin_uint64
330 }
331
332
333 @priority(8)
334 function <(other:UInt16):Bit {
335 return value < other.value->< other.value->builtin_uint64
336 }
337
338
339 @priority(10)
340 function <(other:UInt32):Bit {
341 return value < other.value->< other.value->builtin_uint64
342 }
343
344
345 @override
346 @priority(20)
347 function <(other:UInt64):Bit {
348 return value < other.value
349 }< other.value
350 }
351
352
353 @priority(14)
354 function <(other:UInt):Bit {
355 return value < other.value->< other.value->builtin_uint64
356 }
357
358
359 @priority(6)
360 function >(other:UInt8):Bit {
361 return value > other.value->builtin_uint64
362 }
363
364
365 @priority(8)
366 function >(other:UInt16):Bit {
367 return value > other.value->builtin_uint64
368 }
369
370
371 @priority(10)
372 function >(other:UInt32):Bit {
373 return value > other.value->builtin_uint64
374 }
375
376
377 @override
378 @priority(20)
379 function >(other:UInt64):Bit {
380 return value > other.value
381 }
382
383
384 @priority(14)
385 function >(other:UInt):Bit {
386 return value > other.value->builtin_uint64
387 }
388
389
390 @priority(6)
391 function >=(other:UInt8):Bit {
392 return value >= other.value->builtin_uint64
393 }
394
395
396 @priority(8)
397 function >=(other:UInt16):Bit {
398 return value >= other.value->builtin_uint64
399 }
400
401
402 @priority(10)
403 function >=(other:UInt32):Bit {
404 return value >= other.value->builtin_uint64
405 }
406
407
408 @override
409 @priority(20)
410 function >=(other:UInt64):Bit {
411 return value >= other.value
412 }
413
414
415 @priority(14)
416 function >=(other:UInt):Bit {
417 return value >= other.value->builtin_uint64
418 }
419
420
421 @priority(6)
422 function <=(other:UInt8):Bit {
423 return value <= other.value->builtin_uint64
424 }
425
426
427 @priority(8)
428 function <=(other:UInt16):Bit {
429 return value <= other.value->builtin_uint64
430 }
431
432
433 @priority(10)
434 function <=(other:UInt32):Bit {
435 return value <= other.value->builtin_uint64
436 }
437
438
439 @override
440 @priority(20)
441 function <=(other:UInt64):Bit {
442 return value <= other.value
443 }
444
445
446 @priority(14)
447 function <=(other:UInt):Bit {
448 return value <= other.value->builtin_uint64
449 }
450
451 @class
452 @pre(range.max - range.min < Int64.MAX.asUInt64)< Int64.MAX.asUInt64)
453 function [](range:Range<UInt64>):ListView<UInt64> {
454 return org.frostlang.frost.UInt64List(SteppedRange<UInt64, UInt64>(range.min, range.max, 1, range.inclusive))
455 }
456
457 @class
458 @pre(org.frostlang.frost.UInt64List.computeCount(range) > 0)
459 function [](range:SteppedRange<UInt64, UInt64>):ListView<UInt64> {
460 return org.frostlang.frost.UInt64List(range)
461 }
462
465 property bits:ListView<Bit>
466 function get_bits():ListView<Bit> { return Bits(self) }
467
468
469 property sqrt:Real64
470 function get_sqrt():Real64 {
471 return toReal64.sqrt
472 }
473
474
475
476
477
478
479
480 @priority(6)
481 function min(other:UInt8):UInt64 {
482 if value < other.value->< other.value->builtin_uint64 {
483 return value
484 }
485 return other.value->builtin_uint64
486 }
487
488
489 @priority(8)
490 function min(other:UInt16):UInt64 {
491 if value < other.value->< other.value->builtin_uint64 {
492 return value
493 }
494 return other.value->builtin_uint64
495 }
496
497
498 @priority(10)
499 function min(other:UInt32):UInt64 {
500 if value < other.value->< other.value->builtin_uint64 {
501 return value
502 }
503 return other.value->builtin_uint64
504 }
505
506
507 @priority(20)
508 function min(other:UInt64):UInt64 {
509 if value < other.value {< other.value {
510 return value
511 }
512 return other.value
513 }
514
515
516 @priority(14)
517 function min(other:UInt):UInt64 {
518 if value < other.value->< other.value->builtin_uint64 {
519 return value
520 }
521 return other.value->builtin_uint64
522 }
523
524
525
526
527
528
529
530 @priority(6)
531 function max(other:UInt8):UInt64 {
532 if value > other.value->builtin_uint64 {
533 return value
534 }
535 return other.value->builtin_uint64
536 }
537
538
539 @priority(8)
540 function max(other:UInt16):UInt64 {
541 if value > other.value->builtin_uint64 {
542 return value
543 }
544 return other.value->builtin_uint64
545 }
546
547
548 @priority(10)
549 function max(other:UInt32):UInt64 {
550 if value > other.value->builtin_uint64 {
551 return value
552 }
553 return other.value->builtin_uint64
554 }
555
556
557 @priority(20)
558 function max(other:UInt64):UInt64 {
559 if value > other.value {
560 return value
561 }
562 return other.value
563 }
564
565
566 @priority(14)
567 function max(other:UInt):UInt64 {
568 if value > other.value->builtin_uint64 {
569 return value
570 }
571 return other.value->builtin_uint64
572 }
573
574 @override
575 function get_hash():Int {
576 return Int(value->builtin_int)
577 }
578
579
580 property bitCount:UInt64
581 @external(frostUInt64_get_bitCount)
582 function get_bitCount():UInt64
583
584
585 @class
586 @pre(radix >= 2 & radix <= 36)
587 function parse(str:String, radix:Int):UInt64? {
588 def result := Frost.parse(str, radix)
589 if result == null {
590 return null
591 }
592 return result
593 }
594
595
599 property asInt:Int
600 function get_asInt():Int {
601 assert self <= Int.MAX.toUInt64, "UInt64(\{self}) cannot be safely converted to Int"
602 return Int(value->builtin_int)
603 }
604
605
609 property toInt:Int
610 function get_toInt():Int {
611 return Int(value->builtin_int)
612 }
613
614
618 property asInt8:Int8
619 function get_asInt8():Int8 {
620 assert self <= Int8.MAX.toUInt64, "UInt64(\{self}) cannot be safely converted to Int8"
621 return Int8(value->builtin_int8)
622 }
623
624
628 property toInt8:Int8
629 function get_toInt8():Int8 {
630 return Int8(value->builtin_int8)
631 }
632
633
637 property asInt16:Int16
638 function get_asInt16():Int16 {
639 assert self <= Int16.MAX.toUInt64, "UInt64(\{self}) cannot be safely converted to Int16"
640 return Int16(value->builtin_int16)
641 }
642
643
647 property toInt16:Int16
648 function get_toInt16():Int16 {
649 return Int16(value->builtin_int16)
650 }
651
652
656 property asInt32:Int32
657 function get_asInt32():Int32 {
658 assert self <= Int32.MAX.toUInt64, "UInt64(\{self}) cannot be safely converted to Int32"
659 return Int32(value->builtin_int32)
660 }
661
662
666 property toInt32:Int32
667 function get_toInt32():Int32 {
668 return Int32(value->builtin_int32)
669 }
670
671
675 property asInt64:Int64
676 function get_asInt64():Int64 {
677 assert self <= Int64.MAX.toUInt64, "UInt64(\{self}) cannot be safely converted to Int64"
678 return Int64(value->builtin_int64)
679 }
680
681
685 property toInt64:Int64
686 function get_toInt64():Int64 {
687 return Int64(value->builtin_int64)
688 }
689
690
694 property asUInt:UInt
695 function get_asUInt():UInt {
696 assert self <= UInt.MAX.toUInt64, "UInt64(\{self}) cannot be safely converted to UInt"
697 return UInt(value->builtin_uint)
698 }
699
700
704 property toUInt:UInt
705 function get_toUInt():UInt {
706 return UInt(value->builtin_uint)
707 }
708
709
713 property asUInt8:UInt8
714 function get_asUInt8():UInt8 {
715 assert self <= UInt8.MAX.toUInt64, "UInt64(\{self}) cannot be safely converted to UInt8"
716 return UInt8(value->builtin_uint8)
717 }
718
719
723 property toUInt8:UInt8
724 function get_toUInt8():UInt8 {
725 return UInt8(value->builtin_uint8)
726 }
727
728
732 property asUInt16:UInt16
733 function get_asUInt16():UInt16 {
734 assert self <= UInt16.MAX.toUInt64, "UInt64(\{self}) cannot be safely converted to UInt16"
735 return UInt16(value->builtin_uint16)
736 }
737
738
742 property toUInt16:UInt16
743 function get_toUInt16():UInt16 {
744 return UInt16(value->builtin_uint16)
745 }
746
747
751 property asUInt32:UInt32
752 function get_asUInt32():UInt32 {
753 assert self <= UInt32.MAX.toUInt64, "UInt64(\{self}) cannot be safely converted to UInt32"
754 return UInt32(value->builtin_uint32)
755 }
756
757
761 property toUInt32:UInt32
762 function get_toUInt32():UInt32 {
763 return UInt32(value->builtin_uint32)
764 }
765
766
769 property asReal32:Real32
770 function get_asReal32():Real32 {
771 return Real32(value->builtin_float32)
772 }
773
774
777 property toReal32:Real32
778 function get_toReal32():Real32 {
779 return Real32(value->builtin_float32)
780 }
781
782
785 property asReal64:Real64
786 function get_asReal64():Real64 {
787 return Real64(value->builtin_float64)
788 }
789
790
793 property toReal64:Real64
794 function get_toReal64():Real64 {
795 return Real64(value->builtin_float64)
796 }
797
798
799 @override
800 function get_toString():String {
801 constant max := 20
802 def chars := Pointer<Char8>.alloc(max)
803 var index := max - 1
804 var value := self
805 if value >= 0 {
806 do {
807 chars[index] := Char8((value % 10 + 48).asUInt8)
808 value := (value // 10)
809 index -= 1
810 }
811 while value > 0
812 index += 1
813 }
814 else {
815 do {
816 chars[index] := Char8((48 - value % 10).asUInt8)
817 value := (value // 10)
818 index -= 1
819 }
820 while value < 0< 0
821 chars[index] := "-"
822 }
823 def size := max - index
824 for i in 0 .. size {
825 chars[i] := chars[i + index]
826 }
827 return String(chars, size)
828 }
829
830
831 @override
832 function format(fmt:String):String {
833 return Frost.format(false, self, 0xFFFFFFFFFFFFFFFF, fmt)
834 }
835 }
836