summaryrefslogtreecommitdiff
path: root/legacy/embryo
diff options
context:
space:
mode:
authorCarsten Haitzler <raster@rasterman.com>2012-06-14 08:46:58 +0000
committerCarsten Haitzler <raster@rasterman.com>2012-06-14 08:46:58 +0000
commit2eb4dede2f1c96335dfa4a184ae9f73955994fa2 (patch)
tree0140d2f696a73ec092829a3a23c5672aa7c174aa /legacy/embryo
parent9d8a7e0095b0bda049d8ba0ce31bdd4a7814e48b (diff)
fix divide by 0 possible errors in embryo fp support
SVN revision: 72112
Diffstat (limited to 'legacy/embryo')
-rw-r--r--legacy/embryo/ChangeLog6
-rw-r--r--legacy/embryo/src/lib/embryo_float.c27
2 files changed, 29 insertions, 4 deletions
diff --git a/legacy/embryo/ChangeLog b/legacy/embryo/ChangeLog
index 51b3d50f62..97da875c1a 100644
--- a/legacy/embryo/ChangeLog
+++ b/legacy/embryo/ChangeLog
@@ -35,3 +35,9 @@
352012-04-26 Carsten Haitzler (The Rasterman) 352012-04-26 Carsten Haitzler (The Rasterman)
36 36
37 1.2.0 release 37 1.2.0 release
38
392012-06-14 Carsten Haitzler (The Rasterman)
40
41 * Fix divide by 0 possibilities in the fp support so no FPE is
42 produced (bad).
43
diff --git a/legacy/embryo/src/lib/embryo_float.c b/legacy/embryo/src/lib/embryo_float.c
index 6efa2ae82d..ffaa87dab6 100644
--- a/legacy/embryo/src/lib/embryo_float.c
+++ b/legacy/embryo/src/lib/embryo_float.c
@@ -48,6 +48,9 @@
48#include "embryo_private.h" 48#include "embryo_private.h"
49 49
50#define PI 3.1415926535897932384626433832795f 50#define PI 3.1415926535897932384626433832795f
51#ifndef MAXFLOAT
52#define MAXFLOAT 3.40282347e+38f
53#endif
51 54
52/* internally useful calls */ 55/* internally useful calls */
53 56
@@ -114,10 +117,21 @@ _embryo_fp_div(Embryo_Program *ep __UNUSED__, Embryo_Cell *params)
114{ 117{
115 /* params[1] = float dividend (top) */ 118 /* params[1] = float dividend (top) */
116 /* params[2] = float divisor (bottom) */ 119 /* params[2] = float divisor (bottom) */
117 float f; 120 float f, ff;
118 121
119 if (params[0] != (2 * sizeof(Embryo_Cell))) return 0; 122 if (params[0] != (2 * sizeof(Embryo_Cell))) return 0;
120 f = EMBRYO_CELL_TO_FLOAT(params[1]) / EMBRYO_CELL_TO_FLOAT(params[2]); 123 f = EMBRYO_CELL_TO_FLOAT(params[1]);
124 ff = EMBRYO_CELL_TO_FLOAT(params[2]);
125 if (ff == 0.0)
126 {
127 if (f == 0.0)
128 return EMBRYO_FLOAT_TO_CELL(0.0f);
129 else if (f < 0.0)
130 return EMBRYO_FLOAT_TO_CELL(-MAXFLOAT);
131 else
132 return EMBRYO_FLOAT_TO_CELL(MAXFLOAT);
133 }
134 f = f / ff;
121 return EMBRYO_FLOAT_TO_CELL(f); 135 return EMBRYO_FLOAT_TO_CELL(f);
122} 136}
123 137
@@ -238,7 +252,7 @@ _embryo_fp_log(Embryo_Program *ep, Embryo_Cell *params)
238{ 252{
239 /* params[1] = float operand 1 (value) */ 253 /* params[1] = float operand 1 (value) */
240 /* params[2] = float operand 2 (base) */ 254 /* params[2] = float operand 2 (base) */
241 float f, ff; 255 float f, ff, tf;
242 256
243 if (params[0] != (2 * sizeof(Embryo_Cell))) return 0; 257 if (params[0] != (2 * sizeof(Embryo_Cell))) return 0;
244 f = EMBRYO_CELL_TO_FLOAT(params[1]); 258 f = EMBRYO_CELL_TO_FLOAT(params[1]);
@@ -250,7 +264,12 @@ _embryo_fp_log(Embryo_Program *ep, Embryo_Cell *params)
250 } 264 }
251 if (ff == 10.0) f = log10f(f); 265 if (ff == 10.0) f = log10f(f);
252 else if (ff == 2.0) f = log2f(f); 266 else if (ff == 2.0) f = log2f(f);
253 else f = (logf(f) / logf(ff)); 267 else
268 {
269 tf = logf(ff);
270 if (tf == 0.0) f = 0.0;
271 else f = (logf(f) / tf);
272 }
254 return EMBRYO_FLOAT_TO_CELL(f); 273 return EMBRYO_FLOAT_TO_CELL(f);
255} 274}
256 275