SetSTOWithErrorAsUnify,
HomeDirectory,
DebugHook,
+ PopCount
}
impl SystemClauseType {
clause_name!("$set_sto_with_error_as_unify")
}
&SystemClauseType::DebugHook => clause_name!("$debug_hook"),
+ &SystemClauseType::PopCount => clause_name!("$popcount"),
}
}
("$set_sto_with_error_as_unify", 0) => Some(SystemClauseType::SetSTOWithErrorAsUnify),
("$home_directory", 1) => Some(SystemClauseType::HomeDirectory),
("$debug_hook", 0) => Some(SystemClauseType::DebugHook),
+ ("$popcount", 2) => Some(SystemClauseType::PopCount),
_ => None,
}
}
:- module(arithmetic, [expmod/4, lsb/2, msb/2, number_to_rational/2,
- number_to_rational/3,
+ number_to_rational/3, popcount/2,
rational_numerator_denominator/3]).
:- use_module(library(charsio), [write_term_to_chars/3]).
append(Ns, [' ', r, d, i, v, ' '|Ds], Cs),
number_chars(N, Ns),
number_chars(D, Ds).
+
+popcount(X, N) :-
+ integer(X),
+ '$popcount'(X, N).
&SystemClauseType::DebugHook => {
self.fail = false;
}
+ &SystemClauseType::PopCount => {
+ let number = self.store(self.deref(self[temp_v!(1)]));
+ let count = match Number::try_from((number, &self.heap)) {
+ Ok(Number::Fixnum(n)) => Integer::from(n.count_ones()),
+ Ok(Number::Integer(n)) => Integer::from((&*n).count_ones().unwrap()),
+ _ => {
+ unreachable!()
+ }
+ };
+
+ let pop_count = self.heap.to_unifiable(HeapCellValue::Integer(Rc::new(count)));
+ (self.unify_fn)(self, self[temp_v!(2)], pop_count);
+ }
};
return_from_clause!(self.last_call, self)