Skip to content

Commit

Permalink
Use explicit conversion between integer types in examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
zygoloid committed Jan 11, 2025
1 parent 752d09e commit 2b3f1d1
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 71 deletions.
26 changes: 2 additions & 24 deletions examples/advent2024/day5_common.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,9 @@ library "day5_common";

import library "io_utils";

// fn LeftShift(a: Core.UInt(100), b: i32) -> Core.UInt(100) = "int.left_shift";
fn LeftShift_u100(a: Core.UInt(100), b: Core.UInt(100)) -> Core.UInt(100) = "int.left_shift";

// TODO: Add a builtin that can do this!
fn Promote(a: i32) -> Core.UInt(100) {
var result: Core.UInt(100) = 0;
var bit_i32: i32 = 1;
var bit_u100: Core.UInt(100) = 1;
while (bit_i32 != 0) {
if (a & bit_i32 != 0) {
result = result | bit_u100;
}
bit_i32 <<= 1;
// TODO: bit_u100 <<= 1;
bit_u100 = bit_u100 << 1;
}
return result;
}

fn LeftShift(a: Core.UInt(100), b: i32) -> Core.UInt(100) {
return LeftShift_u100(a, Promote(b));
}

fn PageMask(page: i32) -> Core.UInt(100) {
return LeftShift(1 as Core.UInt(100), page);
// TODO: return (1 as Core.UInt(100)) << page;
return (1 as Core.UInt(100)) << (page as Core.UInt(100));
}

class Rules {
Expand Down
18 changes: 1 addition & 17 deletions examples/advent2024/day9_common.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,6 @@ library "day9_common";
import Core library "io";
import library "io_utils";

// TODO: Add a builtin that can do this!
fn Promote(a: i32) -> i64 {
var result: i64 = 0;
var bit_i32: i32 = 1;
var bit_i64: i64 = 1;
while (bit_i32 != 0) {
if (a & bit_i32 != 0) {
result = result | bit_i64;
}
bit_i32 <<= 1;
// TODO: bit_i64 <<= 1;
bit_i64 = bit_i64 << 1;
}
return result;
}

class SectorList {
fn Read() -> SectorList {
returned var me: SectorList;
Expand Down Expand Up @@ -125,7 +109,7 @@ class SectorList {
var i: i32 = 0;
while (i < self.size) {
if (self.data[i] != -1) {
total = total + Promote(i * self.data[i]);
total = total + ((i * self.data[i]) as i64);
}
++i;
}
Expand Down
32 changes: 2 additions & 30 deletions examples/advent2024/io_utils.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,7 @@ fn ReadInt64(p: i64*) -> bool {
}
// TODO: Check for overflow.
*p = *p * 10;
if (c == 0x31) {
*p = *p + 1;
} else if (c == 0x32) {
*p = *p + 2;
} else if (c == 0x33) {
*p = *p + 3;
} else if (c == 0x34) {
*p = *p + 4;
} else if (c == 0x35) {
*p = *p + 5;
} else if (c == 0x36) {
*p = *p + 6;
} else if (c == 0x37) {
*p = *p + 7;
} else if (c == 0x38) {
*p = *p + 8;
} else if (c == 0x39) {
*p = *p + 9;
}
*p = *p + ((c - 0x30) as i64);
read_any_digits = true;
}
return read_any_digits;
Expand All @@ -89,17 +71,7 @@ fn PrintInt64NoNewline(n_val: i64) {
}
while (pow10 != 0) {
let d: i64 = n / pow10;
// TODO: Core.PrintChar(0x30 + (d as i32));
if (d == 0) { Core.PrintChar(0x30); }
else if (d == 1) { Core.PrintChar(0x31); }
else if (d == 2) { Core.PrintChar(0x32); }
else if (d == 3) { Core.PrintChar(0x33); }
else if (d == 4) { Core.PrintChar(0x34); }
else if (d == 5) { Core.PrintChar(0x35); }
else if (d == 6) { Core.PrintChar(0x36); }
else if (d == 7) { Core.PrintChar(0x37); }
else if (d == 8) { Core.PrintChar(0x38); }
else if (d == 9) { Core.PrintChar(0x39); }
Core.PrintChar((d + 0x30) as i32);
n = n % pow10;
pow10 = pow10 / 10;
}
Expand Down

0 comments on commit 2b3f1d1

Please sign in to comment.