about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2015-05-12 18:08:47 -0700
committerKartik K. Agaram <vc@akkartik.com>2015-05-12 18:08:47 -0700
commitfca0ebbe0cc01d37e47822d8a62ea062a845f33d (patch)
tree557ef7d0bd5d3ddc98ab47cf0273c99cc4a09630
parent98f3a94201df11501d417ac2e75a756ab54ac873 (diff)
downloadmu-fca0ebbe0cc01d37e47822d8a62ea062a845f33d.tar.gz
1360 - store doubles in memory
This is a far cleaner way to provide *some* floating-point support. We
can only represent signed integers up to 2^51 rather than 2^63. But in
exchange we don't have to worry about it elsewhere, and it's probably
faster than checking tag bits in every operation.

Hmm, yeah, surprised how easy this was. I think I'll give up on the
other approach.

I still don't have non-integer literals. But we won't bother with those
until we need them. `3.14159:literal` seems ugly.
-rw-r--r--010vm.cc4
-rw-r--r--020run.cc12
-rw-r--r--021arithmetic.cc17
-rw-r--r--024compare.cc2
-rw-r--r--031address.cc4
-rw-r--r--032array.cc6
-rw-r--r--036call_ingredient.cc2
-rw-r--r--037call_reply.cc2
-rw-r--r--043space.cc6
9 files changed, 31 insertions, 24 deletions
diff --git a/010vm.cc b/010vm.cc
index af60896a..95b1f3ad 100644
--- a/010vm.cc
+++ b/010vm.cc
@@ -57,7 +57,7 @@ struct property {
 
 :(before "End Globals")
 // Locations refer to a common 'memory'. Each location can store a number.
-map<index_t, long long int> Memory;
+map<index_t, double> Memory;
 :(before "End Setup")
 Memory.clear();
 
@@ -249,7 +249,7 @@ string slurp_until(istream& in, char delim) {
 }
 
 void dump_memory() {
-  for (map<index_t, long long int>::iterator p = Memory.begin(); p != Memory.end(); ++p) {
+  for (map<index_t, double>::iterator p = Memory.begin(); p != Memory.end(); ++p) {
     cout << p->first << ": " << p->second << '\n';
   }
 }
diff --git a/020run.cc b/020run.cc
index ba1e0048..1b7c2edc 100644
--- a/020run.cc
+++ b/020run.cc
@@ -68,13 +68,13 @@ void run_current_routine()
     // Read all ingredients from memory.
     // Each ingredient loads a vector of values rather than a single value; mu
     // permits operating on reagents spanning multiple locations.
-    vector<vector<long long int> > ingredients;
+    vector<vector<double> > ingredients;
     for (index_t i = 0; i < current_instruction().ingredients.size(); ++i) {
       trace("run") << "ingredient " << i << " is " << current_instruction().ingredients.at(i).name;
       ingredients.push_back(read_memory(current_instruction().ingredients.at(i)));
     }
     // Instructions below will write to 'products' or to 'instruction_counter'.
-    vector<vector<long long int> > products;
+    vector<vector<double> > products;
     index_t instruction_counter = current_step_index();
 //?     cout << "AAA: " << current_instruction().to_string() << '\n'; //? 1
     switch (current_instruction().operation) {
@@ -178,9 +178,9 @@ void run(string form) {
 
 //:: Reading from memory, writing to memory.
 
-vector<long long int> read_memory(reagent x) {
+vector<double> read_memory(reagent x) {
 //?   cout << "read_memory: " << x.to_string() << '\n'; //? 2
-  vector<long long int> result;
+  vector<double> result;
   if (isa_literal(x)) {
     result.push_back(x.value);
     return result;
@@ -188,14 +188,14 @@ vector<long long int> read_memory(reagent x) {
   index_t base = x.value;
   size_t size = size_of(x);
   for (index_t offset = 0; offset < size; ++offset) {
-    long long int val = Memory[base+offset];
+    double val = Memory[base+offset];
     trace("mem") << "location " << base+offset << " is " << val;
     result.push_back(val);
   }
   return result;
 }
 
-void write_memory(reagent x, vector<long long int> data) {
+void write_memory(reagent x, vector<double> data) {
   if (is_dummy(x)) return;
   index_t base = x.value;
   if (size_of(x) != data.size())
diff --git a/021arithmetic.cc b/021arithmetic.cc
index 307bcb18..0dedd02d 100644
--- a/021arithmetic.cc
+++ b/021arithmetic.cc
@@ -6,7 +6,7 @@ ADD,
 Recipe_number["add"] = ADD;
 :(before "End Primitive Recipe Implementations")
 case ADD: {
-  long long int result = 0;
+  double result = 0;
   for (index_t i = 0; i < ingredients.size(); ++i) {
     assert(ingredients.at(i).size() == 1);  // scalar
     result += ingredients.at(i).at(0);
@@ -53,7 +53,7 @@ Recipe_number["subtract"] = SUBTRACT;
 :(before "End Primitive Recipe Implementations")
 case SUBTRACT: {
   assert(ingredients.at(0).size() == 1);  // scalar
-  long long int result = ingredients.at(0).at(0);
+  double result = ingredients.at(0).at(0);
   for (index_t i = 1; i < ingredients.size(); ++i) {
     assert(ingredients.at(i).size() == 1);  // scalar
     result -= ingredients.at(i).at(0);
@@ -99,7 +99,7 @@ MULTIPLY,
 Recipe_number["multiply"] = MULTIPLY;
 :(before "End Primitive Recipe Implementations")
 case MULTIPLY: {
-  long long int result = 1;
+  double result = 1;
   for (index_t i = 0; i < ingredients.size(); ++i) {
     assert(ingredients.at(i).size() == 1);  // scalar
     result *= ingredients.at(i).at(0);
@@ -146,7 +146,7 @@ Recipe_number["divide"] = DIVIDE;
 :(before "End Primitive Recipe Implementations")
 case DIVIDE: {
   assert(ingredients.at(0).size() == 1);  // scalar
-  long long int result = ingredients.at(0).at(0);
+  double result = ingredients.at(0).at(0);
   for (index_t i = 1; i < ingredients.size(); ++i) {
     assert(ingredients.at(i).size() == 1);  // scalar
     result /= ingredients.at(i).at(0);
@@ -193,7 +193,7 @@ Recipe_number["divide-with-remainder"] = DIVIDE_WITH_REMAINDER;
 :(before "End Primitive Recipe Implementations")
 case DIVIDE_WITH_REMAINDER: {
   long long int quotient = ingredients.at(0).at(0) / ingredients.at(1).at(0);
-  long long int remainder = ingredients.at(0).at(0) % ingredients.at(1).at(0);
+  long long int remainder = static_cast<long long int>(ingredients.at(0).at(0)) % static_cast<long long int>(ingredients.at(1).at(0));
   products.resize(2);
   products.at(0).push_back(quotient);
   products.at(1).push_back(remainder);
@@ -227,3 +227,10 @@ recipe main [
 +mem: storing 2 in location 3
 +run: product 1 is 4
 +mem: storing 5 in location 4
+
+:(scenario divide_with_decimal_point)
+recipe main [
+  # todo: literal floats?
+  1:integer <- divide 5:literal, 2:literal
+]
++mem: storing 2.5 in location 1
diff --git a/024compare.cc b/024compare.cc
index 92d87e5c..af370834 100644
--- a/024compare.cc
+++ b/024compare.cc
@@ -6,7 +6,7 @@ EQUAL,
 Recipe_number["equal"] = EQUAL;
 :(before "End Primitive Recipe Implementations")
 case EQUAL: {
-  vector<long long int>& exemplar = ingredients.at(0);
+  vector<double>& exemplar = ingredients.at(0);
   bool result = true;
   for (index_t i = 1; i < ingredients.size(); ++i) {
     if (!equal(ingredients.at(i).begin(), ingredients.at(i).end(), exemplar.begin())) {
diff --git a/031address.cc b/031address.cc
index 58d93466..9eeb0f49 100644
--- a/031address.cc
+++ b/031address.cc
@@ -13,7 +13,7 @@ recipe main [
 +mem: location 2 is 34
 +mem: storing 34 in location 3
 
-:(before "index_t base = x.value" following "vector<long long int> read_memory(reagent x)")
+:(before "index_t base = x.value" following "vector<double> read_memory(reagent x)")
 x = canonize(x);
 
 //: similarly, write to addresses pointing at other locations using the
@@ -27,7 +27,7 @@ recipe main [
 +mem: location 1 is 2
 +mem: storing 34 in location 2
 
-:(before "index_t base = x.value" following "void write_memory(reagent x, vector<long long int> data)")
+:(before "index_t base = x.value" following "void write_memory(reagent x, vector<double> data)")
 x = canonize(x);
 
 :(code)
diff --git a/032array.cc b/032array.cc
index 67b03293..04234d3a 100644
--- a/032array.cc
+++ b/032array.cc
@@ -47,7 +47,7 @@ recipe main [
 +mem: storing 16 in location 9
 
 //: disable the size mismatch check since the destination array need not be initialized
-:(replace "if (size_of(x) != data.size())" following "void write_memory(reagent x, vector<long long int> data)")
+:(replace "if (size_of(x) != data.size())" following "void write_memory(reagent x, vector<double> data)")
 if (x.types.at(0) != Type_number["array"] && size_of(x) != data.size())
 :(after "size_t size_of(const reagent& r)")
   if (r.types.at(0) == Type_number["array"]) {
@@ -102,7 +102,7 @@ case INDEX: {
   assert(base.types.at(0) == Type_number["array"]);
   reagent offset = canonize(current_instruction().ingredients.at(1));
 //?   trace("run") << "ingredient 1 after canonize: " << offset.to_string(); //? 1
-  vector<long long int> offset_val(read_memory(offset));
+  vector<double> offset_val(read_memory(offset));
   vector<type_number> element_type = array_element(base.types);
 //?   trace("run") << "offset: " << offset_val.at(0); //? 1
 //?   trace("run") << "size of elements: " << size_of(element_type); //? 1
@@ -156,7 +156,7 @@ case INDEX_ADDRESS: {
   index_t base_address = base.value;
   assert(base.types.at(0) == Type_number["array"]);
   reagent offset = canonize(current_instruction().ingredients.at(1));
-  vector<long long int> offset_val(read_memory(offset));
+  vector<double> offset_val(read_memory(offset));
   vector<type_number> element_type = array_element(base.types);
   index_t result = base_address + 1 + offset_val.at(0)*size_of(element_type);
   products.resize(1);
diff --git a/036call_ingredient.cc b/036call_ingredient.cc
index 5c321ef9..fe3ef1ed 100644
--- a/036call_ingredient.cc
+++ b/036call_ingredient.cc
@@ -23,7 +23,7 @@ recipe f [
 +mem: storing 0 in location 12
 
 :(before "End call Fields")
-vector<vector<long long int> > ingredient_atoms;
+vector<vector<double> > ingredient_atoms;
 index_t next_ingredient_to_process;
 :(replace{} "call(recipe_number r)")
 call(recipe_number r) :running_recipe(r), running_step_index(0), next_ingredient_to_process(0) {}
diff --git a/037call_reply.cc b/037call_reply.cc
index 3de6d588..11d79e0c 100644
--- a/037call_reply.cc
+++ b/037call_reply.cc
@@ -76,7 +76,7 @@ recipe test1 [
 +warn: 'same-as-ingredient' result 2 must be location 1
 
 :(code)
-string to_string(const vector<long long int>& in) {
+string to_string(const vector<double>& in) {
   if (in.empty()) return "[]";
   ostringstream out;
   if (in.size() == 1) {
diff --git a/043space.cc b/043space.cc
index b0ee7f85..2a8ff4fc 100644
--- a/043space.cc
+++ b/043space.cc
@@ -107,7 +107,7 @@ index_t address(index_t offset, index_t base) {
   return base+1 + offset;
 }
 
-:(after "void write_memory(reagent x, vector<long long int> data)")
+:(after "void write_memory(reagent x, vector<double> data)")
   if (x.name == "default-space") {
     assert(data.size() == 1);
     Current_routine->calls.top().default_space = data.at(0);
@@ -122,9 +122,9 @@ recipe main [
 ]
 +mem: storing 10 in location 1
 
-:(after "vector<long long int> read_memory(reagent x)")
+:(after "vector<double> read_memory(reagent x)")
   if (x.name == "default-space") {
-    vector<long long int> result;
+    vector<double> result;
     result.push_back(Current_routine->calls.top().default_space);
     return result;
   }