From f859751c6a612205740698d67e083efe5cc4ee5e Mon Sep 17 00:00:00 2001 From: William Carroll Date: Mon, 5 Sep 2022 22:18:43 -0700 Subject: feat(tvix/eval): Support builtins.bitXor Bitwise "exclusive-or" on integers. Change-Id: I90a0a15afb3a58662d70e82ea14e48b877476e04 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6550 Autosubmit: wpcarro Reviewed-by: wpcarro Reviewed-by: tazjin Tested-by: BuildkiteCI --- tvix/eval/src/builtins/mod.rs | 7 +++++++ tvix/eval/src/tests/tvix_tests/eval-okay-builtins-bitxor.exp | 1 + tvix/eval/src/tests/tvix_tests/eval-okay-builtins-bitxor.nix | 12 ++++++++++++ 3 files changed, 20 insertions(+) create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-builtins-bitxor.exp create mode 100644 tvix/eval/src/tests/tvix_tests/eval-okay-builtins-bitxor.nix diff --git a/tvix/eval/src/builtins/mod.rs b/tvix/eval/src/builtins/mod.rs index ebbc5e336c21..6098bba1e48f 100644 --- a/tvix/eval/src/builtins/mod.rs +++ b/tvix/eval/src/builtins/mod.rs @@ -115,6 +115,13 @@ fn pure_builtins() -> Vec { }) }) }), + Builtin::new("bitXor", 2, |args, vm| { + force!(vm, &args[0], x, { + force!(vm, &args[1], y, { + Ok(Value::Integer(x.as_int()? ^ y.as_int()?)) + }) + }) + }), Builtin::new("catAttrs", 2, |mut args, _| { let list = args.pop().unwrap().to_list()?; let key = args.pop().unwrap().to_str()?; diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-bitxor.exp b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-bitxor.exp new file mode 100644 index 000000000000..457157d45970 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-bitxor.exp @@ -0,0 +1 @@ +[ 0 1 1 0 8 8 0 0 0 0 ] diff --git a/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-bitxor.nix b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-bitxor.nix new file mode 100644 index 000000000000..80e363fb0748 --- /dev/null +++ b/tvix/eval/src/tests/tvix_tests/eval-okay-builtins-bitxor.nix @@ -0,0 +1,12 @@ +[ + (builtins.bitXor 0 0) + (builtins.bitXor 1 0) + (builtins.bitXor 0 1) + (builtins.bitXor 1 1) + (builtins.bitXor 8 0) + (builtins.bitXor 0 8) + (builtins.bitXor 8 8) + (builtins.bitXor 8 (builtins.add 4 4)) + (builtins.bitXor (builtins.add 4 4) 8) + (builtins.bitXor (builtins.add 4 4) (builtins.add 4 4)) +] -- cgit 1.4.1