use gategen::boolvar::*;
use gategen::gatesim::*;
use gategen::intvar::*;
use gatenative::{cpu_build_exec::*, *};
fn mul_add_circuit() -> Circuit<u32> {
call32(|| {
let a = U16Var32::var();
let b = U16Var32::var();
let c = U16Var32::var();
let ra = &a * &b + &c;
let rb = &c * &a + &b;
let rc = &b * &c + &a;
let out = ra.concat(rb).concat(rc);
out.to_translated_circuit(a.concat(b).concat(c).iter())
})
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let param_a = 0x4da0u16;
let param_b = 0x8725u16;
let param_c = 0xe052u16;
let circuit = mul_add_circuit();
let mut builder = CPUBuilder::new(None);
builder.transform_helpers();
builder.add_with_config(
"mul_add",
circuit,
CodeConfig::new()
.pop_input_code(Some(
r##"{
// Get paramareters from input
const uint32_t* params = (const uint32_t*)buffer;
const uint32_t param_a = params[0];
const uint32_t param_b = params[1];
const uint32_t param_c = params[2];
size_t i;
uint32_t temp[TYPE_LEN];
// generate values of function: (x xor param_a) + (x and param_b) + (x or param_c).
for (i = 0; i < TYPE_LEN; i++) {
const uint32_t id = idx*TYPE_LEN + i;
temp[i] = ((id^param_a) + (id¶m_b) + (id|param_c)) & 0xffff;
}
INPUT_TRANSFORM_B16(i32, i33, i34, i35, i36, i37, i38, i39,
i40, i41, i42, i43, i44, i45, i46, i47, temp);
}"##,
))
.pop_from_buffer(Some(&(32..48).collect::<Vec<_>>()))
.pop_input_len(Some(4))
.aggr_output_code(Some(
r##"{
size_t i;
uint32_t* buffer_u32 = (uint32_t*)buffer;
uint32_t out_table[TYPE_LEN];
OUTPUT_TRANSFORM_B16(out_table, o32, o33, o34, o35, o36, o37, o38, o39,
o40, o41, o42, o43, o44, o45, o46, o47);
for (i = 0; i < TYPE_LEN; i++)
__sync_fetch_and_add(&(buffer_u32[3]), out_table[i]);
}"##,
))
.aggr_only_to_buffer(Some(&(32..48).collect::<Vec<_>>()))
.aggr_output_len(Some(4)),
);
let mut execs = builder.build()?;
let mut it = execs[0].input_transformer(64, &((0..16).chain(32..48).collect::<Vec<_>>()))?;
let mut ot = execs[0].output_transformer(64, &((0..16).chain(32..48).collect::<Vec<_>>()))?;
let input = execs[0].new_data_from_vec(
(0..16384u32)
.map(|x| [(x + 3) & 0xffff, (x + 1489u32) & 0xffff])
.flatten()
.collect::<Vec<_>>(),
);
let input = it.transform(&input)?;
let mut buffer =
execs[0].new_data_from_slice(&[param_a.into(), param_b.into(), param_c.into(), 0u32]);
let output = execs[0].execute_buffer(&input, 0, &mut buffer)?;
let output = ot.transform(&output)?;
let output = output.release();
let buffer = buffer.release();
for (i, v) in output.chunks(2).into_iter().enumerate() {
println!("{}: {} {}", i, v[0], v[1]);
}
println!("Result: {}", buffer[3]);
Ok(())
}