12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455open!Import(* Bits API built using lists on ints. Allows any bit precision and is simple, but
slow. *)moduleGates=struct(* msb first *)typet=intlist[@@derivingcompare]letequal=[%compare.equal:t]letempty=[]letis_empty=List.is_emptyletwidthx=List.lengthxletof_constant=Constant.to_bit_listletto_constant=Constant.of_bit_listletconcat_msbl=List.concatlletselectshl=letrecselbi=matchbwith|[]->[]|hd::tl->ifi>hthen[]elseifi>=lthenhd::seltl(i+1)elseseltl(i+1)inList.rev(sel(List.revs)0);;let(&:)=List.map2_exn~f:(land)let(|:)=List.map2_exn~f:(lor)let(^:)=List.map2_exn~f:(lxor)let(~:)=List.map~f:(funx->ifx=1then0else1)letrecto_stringb=matchbwith|[]->""|h::t->(ifh=1then"1"else"0")^to_stringt;;letto_bstr=to_stringletsexp_of_ts=[%sexp(to_bstrs:string)]let(--)a_=aendmodulePrimitives=structincludeComb.Make_primitives(Gates)(* About 30% faster than the generic implementation, and common enough to care. *)letmuxselvals=letlen=List.lengthvalsinletidx=to_constantsel|>Constant.to_int64|>Int64.to_int_truncinList.nth_exnvals(ifidx>=lenthenlen-1elseidx);;endincludeComb.Make(Primitives)