981 Reimplement as library system call
This commit is contained in:
@@ -310,6 +310,7 @@ pub(crate) enum SystemClauseType {
|
||||
SetSTOWithErrorAsUnify,
|
||||
HomeDirectory,
|
||||
DebugHook,
|
||||
PopCount
|
||||
}
|
||||
|
||||
impl SystemClauseType {
|
||||
@@ -603,6 +604,7 @@ impl SystemClauseType {
|
||||
clause_name!("$set_sto_with_error_as_unify")
|
||||
}
|
||||
&SystemClauseType::DebugHook => clause_name!("$debug_hook"),
|
||||
&SystemClauseType::PopCount => clause_name!("$popcount"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -854,6 +856,7 @@ impl SystemClauseType {
|
||||
("$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,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
:- 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]).
|
||||
@@ -121,3 +121,7 @@ rational_numerator_denominator(R, N, D) :-
|
||||
append(Ns, [' ', r, d, i, v, ' '|Ds], Cs),
|
||||
number_chars(N, Ns),
|
||||
number_chars(D, Ds).
|
||||
|
||||
popcount(X, N) :-
|
||||
integer(X),
|
||||
'$popcount'(X, N).
|
||||
|
||||
@@ -5539,6 +5539,19 @@ impl MachineState {
|
||||
&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)
|
||||
|
||||
Reference in New Issue
Block a user