Skip to content

Instantly share code, notes, and snippets.

@intsuc
Last active July 13, 2024 12:52
Show Gist options
  • Save intsuc/f564238d3ee55c51192e5f66008ddce3 to your computer and use it in GitHub Desktop.
Save intsuc/f564238d3ee55c51192e5f66008ddce3 to your computer and use it in GitHub Desktop.
Get the high 32 bits of a LongTag
function high32:init
data modify storage _ a set value -1145395493543608220L
# (0xf01abcde00000064)
function high32:get {source: "storage _ a"}
# Function high32:get returned -266683170
# (0xf01abcde)
data modify storage _ a set value 5001117283045605276L
# (0x456789abffffff9c)
function high32:get {source: "storage _ a"}
# Function high32:get returned 1164413355
# (0x456789ab)
$data modify storage high32: source set from $(source)
data modify storage high32: wrapper[0] set from storage high32: source
execute store result score #low high32 run data get storage high32: wrapper[0] 1
execute store result score #high_pos high32 run data get storage high32: source 0.00000000023283064365386963
execute store result score #high_neg high32 run data get storage high32: source -0.00000000023283064365386963
scoreboard players operation #high high32 = #high_pos high32
scoreboard players operation #high high32 > #high_neg high32
execute if score #high high32 matches 0..2097151 run return run scoreboard players get #high_pos high32
execute if score #high high32 matches 2097152..4194303 if score #low high32 matches -1 run return run scoreboard players remove #high_pos high32 1
execute if score #high high32 matches 4194304..8388607 if score #low high32 matches -2..-1 run return run scoreboard players remove #high_pos high32 1
execute if score #high high32 matches 8388608..16777215 if score #low high32 matches -4..-1 run return run scoreboard players remove #high_pos high32 1
execute if score #high high32 matches 16777216..33554431 if score #low high32 matches -8..-1 run return run scoreboard players remove #high_pos high32 1
execute if score #high high32 matches 33554432..67108863 if score #low high32 matches -16..-1 run return run scoreboard players remove #high_pos high32 1
execute if score #high high32 matches 67108864..134217727 if score #low high32 matches -32..-1 run return run scoreboard players remove #high_pos high32 1
execute if score #high high32 matches 134217728..268435455 if score #low high32 matches -64..-1 run return run scoreboard players remove #high_pos high32 1
execute if score #high high32 matches 268435456..536870911 if score #low high32 matches -128..-1 run return run scoreboard players remove #high_pos high32 1
execute if score #high high32 matches 536870912..1073741823 if score #low high32 matches -256..-1 run return run scoreboard players remove #high_pos high32 1
execute if score #high high32 matches 1073741824..2147483646 if score #low high32 matches -512..-1 run return run scoreboard players remove #high_pos high32 1
execute if score #high high32 matches 2147483647.. if score #high_neg high32 matches -2147483647.. if score #low high32 matches -512..-1 run return run scoreboard players remove #high_pos high32 1
return run scoreboard players get #high_pos high32
data modify storage high32: wrapper set value [I;0]
scoreboard objectives add high32 dummy
#[rustfmt::skip]
#[allow(overflowing_literals)]
fn get(x: i64) -> i32 {
fn floor(x: i64, scale: f64) -> i32 {
let x = (x as f64) * scale;
let y = x as i32;
if x < (y as f64) {
y - 1
} else {
y
}
}
let low = x as i32;
let high_pos = floor(x, 2.3283064365386963e-10);
let high_neg = floor(x, -2.3283064365386963e-10);
let high = high_pos.max(high_neg);
high_pos - match (high, low) {
(0b00000000000000000000000000000000..=0b00000000000111111111111111111111, _) => 0,
(0b00000000001000000000000000000000..=0b00000000001111111111111111111111, 0b11111111111111111111111111111111..=0b11111111111111111111111111111111) => 1,
(0b00000000010000000000000000000000..=0b00000000011111111111111111111111, 0b11111111111111111111111111111110..=0b11111111111111111111111111111111) => 1,
(0b00000000100000000000000000000000..=0b00000000111111111111111111111111, 0b11111111111111111111111111111100..=0b11111111111111111111111111111111) => 1,
(0b00000001000000000000000000000000..=0b00000001111111111111111111111111, 0b11111111111111111111111111111000..=0b11111111111111111111111111111111) => 1,
(0b00000010000000000000000000000000..=0b00000011111111111111111111111111, 0b11111111111111111111111111110000..=0b11111111111111111111111111111111) => 1,
(0b00000100000000000000000000000000..=0b00000111111111111111111111111111, 0b11111111111111111111111111100000..=0b11111111111111111111111111111111) => 1,
(0b00001000000000000000000000000000..=0b00001111111111111111111111111111, 0b11111111111111111111111111000000..=0b11111111111111111111111111111111) => 1,
(0b00010000000000000000000000000000..=0b00011111111111111111111111111111, 0b11111111111111111111111110000000..=0b11111111111111111111111111111111) => 1,
(0b00100000000000000000000000000000..=0b00111111111111111111111111111111, 0b11111111111111111111111100000000..=0b11111111111111111111111111111111) => 1,
(0b01000000000000000000000000000000..=0b01111111111111111111111111111110, 0b11111111111111111111111000000000..=0b11111111111111111111111111111111) => 1,
(0b01111111111111111111111111111111..=0b01111111111111111111111111111111, 0b11111111111111111111111000000000..=0b11111111111111111111111111111111) => if high_neg == i32::MIN { 0 } else { 1 },
_ => 0,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment