altos/scheme: Add ports. Split scheme code up.
[fw/altos] / src / scheme / ao_scheme_float.c
1 /*
2  * Copyright © 2017 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  */
14
15 #include "ao_scheme.h"
16 #include <math.h>
17
18 #ifdef AO_SCHEME_FEATURE_FLOAT
19
20 static void float_mark(void *addr)
21 {
22         (void) addr;
23 }
24
25 static int float_size(void *addr)
26 {
27         if (!addr)
28                 return 0;
29         return sizeof (struct ao_scheme_float);
30 }
31
32 static void float_move(void *addr)
33 {
34         (void) addr;
35 }
36
37 const struct ao_scheme_type ao_scheme_float_type = {
38         .mark = float_mark,
39         .size = float_size,
40         .move = float_move,
41         .name = "float",
42 };
43
44 #ifndef FLOAT_FORMAT
45 #define FLOAT_FORMAT "%g"
46 #endif
47
48 void
49 ao_scheme_float_write(FILE *out, ao_poly p, bool write)
50 {
51         struct ao_scheme_float *f = ao_scheme_poly_float(p);
52         float   v = f->value;
53
54         (void) write;
55         if (isnanf(v))
56                 fputs("+nan.0", out);
57         else if (isinff(v)) {
58                 if (v < 0)
59                         putc('-', out);
60                 else
61                         putc('+', out);
62                 fputs("inf.0", out);
63         } else
64                 fprintf(out, FLOAT_FORMAT, v);
65 }
66
67 float
68 ao_scheme_poly_number(ao_poly p)
69 {
70         switch (ao_scheme_poly_base_type(p)) {
71         case AO_SCHEME_INT:
72                 return ao_scheme_poly_int(p);
73         case AO_SCHEME_BIGINT:
74                 return ao_scheme_poly_bigint(p)->value;
75         case AO_SCHEME_OTHER:
76                 switch (ao_scheme_other_type(ao_scheme_poly_other(p))) {
77                 case AO_SCHEME_FLOAT:
78                         return ao_scheme_poly_float(p)->value;
79                 }
80         }
81         return NAN;
82 }
83
84 ao_poly
85 ao_scheme_float_get(float value)
86 {
87         struct ao_scheme_float  *f;
88
89         f = ao_scheme_alloc(sizeof (struct ao_scheme_float));
90         f->type = AO_SCHEME_FLOAT;
91         f->value = value;
92         return ao_scheme_float_poly(f);
93 }
94
95 ao_poly
96 ao_scheme_do_inexactp(struct ao_scheme_cons *cons)
97 {
98         ao_poly val;
99
100         if (!ao_scheme_parse_args(_ao_scheme_atom_inexact3f, cons,
101                                   AO_SCHEME_POLY, &val,
102                                   AO_SCHEME_ARG_END))
103                 return AO_SCHEME_NIL;
104         if (ao_scheme_poly_type(val) == AO_SCHEME_FLOAT)
105                 return _ao_scheme_bool_true;
106         return _ao_scheme_bool_false;
107 }
108
109 ao_poly
110 ao_scheme_do_finitep(struct ao_scheme_cons *cons)
111 {
112         ao_poly val;
113         float   f;
114
115         if (!ao_scheme_parse_args(_ao_scheme_atom_inexact3f, cons,
116                                   AO_SCHEME_POLY, &val,
117                                   AO_SCHEME_ARG_END))
118                 return AO_SCHEME_NIL;
119         switch (ao_scheme_poly_type(val)) {
120         case AO_SCHEME_INT:
121         case AO_SCHEME_BIGINT:
122                 return _ao_scheme_bool_true;
123         case AO_SCHEME_FLOAT:
124                 f = ao_scheme_poly_float(val)->value;
125                 if (!isnan(f) && !isinf(f))
126                         return _ao_scheme_bool_true;
127         }
128         return _ao_scheme_bool_false;
129 }
130
131 ao_poly
132 ao_scheme_do_infinitep(struct ao_scheme_cons *cons)
133 {
134         ao_poly val;
135         float   f;
136
137         if (!ao_scheme_parse_args(_ao_scheme_atom_inexact3f, cons,
138                                   AO_SCHEME_POLY, &val,
139                                   AO_SCHEME_ARG_END))
140                 return AO_SCHEME_NIL;
141         switch (ao_scheme_poly_type(val)) {
142         case AO_SCHEME_FLOAT:
143                 f = ao_scheme_poly_float(val)->value;
144                 if (isinf(f))
145                         return _ao_scheme_bool_true;
146         }
147         return _ao_scheme_bool_false;
148 }
149
150 ao_poly
151 ao_scheme_do_sqrt(struct ao_scheme_cons *cons)
152 {
153         float   f;
154
155         if (!ao_scheme_parse_args(_ao_scheme_atom_sqrt, cons,
156                                   AO_SCHEME_FLOAT, &f,
157                                   AO_SCHEME_ARG_END))
158                 return AO_SCHEME_NIL;
159         return ao_scheme_float_get(sqrtf(f));
160 }
161 #endif