]> Repositorios git - scryer-prolog.git/commitdiff
981 Reimplement as library system call
authorRegan-Koopmans <[email protected]>
Thu, 17 Jun 2021 10:10:25 +0000 (12:10 +0200)
committerRegan-Koopmans <[email protected]>
Thu, 17 Jun 2021 10:10:25 +0000 (12:10 +0200)
src/clause_types.rs
src/lib/arithmetic.pl
src/machine/system_calls.rs

index d45fdf5a27e69714d220cd4d745751bd8326e7b6..e6559417d2c572c5e1a9159644e1bedaaf4926e1 100644 (file)
@@ -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,
         }
     }
index f0d2b58ec28fff9bc2e696b4055671a26f06ccaa..3d3a6625577d971e1e5b054a6aec15997e5f47e7 100644 (file)
@@ -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).
index 29c4fbffb02d1a1922eecd5ca8b664c7f5ce9209..ade423a16540f7dfd21bd86d7eab9390d3cf343f 100644 (file)
@@ -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)