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