Debian Codegraph @

src/c/predicate.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * predicate.d - predicates
6 *
7 * Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya
8 * Copyright (c) 1990 Giuseppe Attardi
9 * Copyright (c) 2001 Juan Jose Garcia Ripoll
10 *
11 * See file 'LICENSE' for the copyright details.
12 *
13 */
14
15 #include <string.h>
16 #define ECL_INCLUDE_MATH_H
17 #include <ecl/ecl.h>
18 #define ECL_DEFINE_AET_SIZE
19 #include <ecl/internal.h>
20
21 cl_object
22 cl_identity(cl_object x)
23 {
24 @(return x);
25 }
26
27 cl_object
28 cl_null(cl_object x)
29 {
30 @(return (Null(x) ? ECL_T : ECL_NIL));
31 }
32
33 cl_object
34 cl_symbolp(cl_object x)
35 {
36 @(return (ECL_SYMBOLP(x) ? ECL_T : ECL_NIL));
37 }
38
39 cl_object
40 cl_atom(cl_object x)
41 {
42 @(return (ECL_ATOM(x) ? ECL_T : ECL_NIL));
43 }
44
45 cl_object
46 cl_consp(cl_object x)
47 {
48 @(return (CONSP(x) ? ECL_T : ECL_NIL));
49 }
50
51 cl_object
52 cl_listp(cl_object x)
53 {
54 @(return ((Null(x) || CONSP(x)) ? ECL_T : ECL_NIL));
55 }
56
57 cl_object
58 cl_numberp(cl_object x)
59 {
60 cl_type t = ecl_t_of(x);
61 @(return (ECL_NUMBER_TYPE_P(t) ? ECL_T : ECL_NIL));
62 }
63
64 /* Used in compiled code */
65 bool ecl_numberp(cl_object x)
66 {
67 cl_type t = ecl_t_of(x);
68 return ECL_NUMBER_TYPE_P(t);
69 }
70
71 cl_object
72 cl_integerp(cl_object x)
73 {
74 cl_type t = ecl_t_of(x);
75 @(return ((t == t_fixnum || t == t_bignum) ? ECL_T : ECL_NIL));
76 }
77
78 cl_object
79 cl_rationalp(cl_object x)
80 {
81 cl_type t = ecl_t_of(x);
82 @(return ((t == t_fixnum || t == t_bignum || t == t_ratio) ? ECL_T : ECL_NIL));
83 }
84
85 cl_object
86 cl_floatp(cl_object x)
87 {
88 @(return (floatp(x)? ECL_T : ECL_NIL));
89 }
90
91 bool
92 floatp(cl_object x)
93 {
94 cl_type t = ecl_t_of(x);
95 return (t == t_singlefloat) || (t == t_doublefloat) || (t == t_longfloat);
96 }
97
98 cl_object
99 cl_realp(cl_object x)
100 {
101 cl_type t = ecl_t_of(x);
102 @(return (ECL_REAL_TYPE_P(t) ? ECL_T : ECL_NIL));
103 }
104
105 bool
106 ecl_realp(cl_object x)
107 {
108 cl_type t = ecl_t_of(x);
109 return ECL_REAL_TYPE_P(t);
110 }
111
112 cl_object
113 cl_complexp(cl_object x)
114 {
115 @(return (ECL_COMPLEXP(x) ? ECL_T : ECL_NIL));
116 }
117
118 cl_object
119 cl_characterp(cl_object x)
120 {
121 @(return (ECL_CHARACTERP(x) ? ECL_T : ECL_NIL));
122 }
123
124 #ifdef ECL_UNICODE
125 cl_object
126 si_base_char_p(cl_object c)
127 {
128 @(return ((ECL_CHARACTERP(c) && ECL_BASE_CHAR_P(c))? ECL_T : ECL_NIL));
129 }
130 #endif
131
132 bool
133 ecl_stringp(cl_object x)
134 {
135 cl_type t = ecl_t_of(x);
136 #ifdef ECL_UNICODE
137 return t == t_base_string || t == t_string;
138 #else
139 return t == t_base_string;
140 #endif
141 }
142
143 cl_object
144 cl_stringp(cl_object x)
145 {
146 @(return (ECL_STRINGP(x)? ECL_T : ECL_NIL));
147 }
148
149 cl_object
150 cl_bit_vector_p(cl_object x)
151 {
152 @(return (ECL_BIT_VECTOR_P(x) ? ECL_T : ECL_NIL));
153 }
154
155 cl_object
156 cl_vectorp(cl_object x)
157 {
158 @(return (ECL_VECTORP(x) ? ECL_T : ECL_NIL));
159 }
160
161 cl_object
162 cl_simple_string_p(cl_object x)
163 {
164 @(return ((ECL_STRINGP(x) &&
165 !ECL_ADJUSTABLE_ARRAY_P(x) &&
166 !ECL_ARRAY_HAS_FILL_POINTER_P(x) &&
167 Null(CAR(x->base_string.displaced))) ? ECL_T : ECL_NIL));
168 }
169
170 #ifdef ECL_UNICODE
171 cl_object
172 si_base_string_p(cl_object x)
173 {
174 @(return (ECL_BASE_STRING_P(x) ? ECL_T : ECL_NIL));
175 }
176 #endif
177
178 cl_object
179 cl_simple_bit_vector_p(cl_object x)
180 {
181 @(return ((ECL_BIT_VECTOR_P(x) &&
182 !ECL_ADJUSTABLE_ARRAY_P(x) &&
183 !ECL_ARRAY_HAS_FILL_POINTER_P(x) &&
184 Null(CAR(x->vector.displaced))) ? ECL_T : ECL_NIL));
185 }
186
187 cl_object
188 cl_simple_vector_p(cl_object x)
189 {
190 cl_type t = ecl_t_of(x);
191 @(return ((t == t_vector &&
192 !ECL_ADJUSTABLE_ARRAY_P(x) &&
193 !ECL_ARRAY_HAS_FILL_POINTER_P(x) &&
194 Null(CAR(x->vector.displaced)) &&
195 (cl_elttype)x->vector.elttype == ecl_aet_object) ? ECL_T : ECL_NIL));
196 }
197
198 cl_object
199 cl_arrayp(cl_object x)
200 {
201 @(return (ECL_ARRAYP(x) ? ECL_T : ECL_NIL));
202 }
203
204 cl_object
205 cl_packagep(cl_object x)
206 {
207 @(return (ECL_PACKAGEP(x) ? ECL_T : ECL_NIL));
208 }
209
210 cl_object
211 cl_functionp(cl_object x)
212 {
213 cl_type t;
214 cl_object output;
215
216 t = ecl_t_of(x);
217 if (t == t_bytecodes || t == t_bclosure || t == t_cfun
218 || t == t_cfunfixed || t == t_cclosure
219 || (t == t_instance && x->instance.isgf))
220 output = ECL_T;
221 else
222 output = ECL_NIL;
223 @(return output);
224 }
225
226 cl_object
227 cl_compiled_function_p(cl_object x)
228 {
229 cl_type t = ecl_t_of(x);
230 @(return ((t == t_bytecodes || t == t_bclosure || t == t_cfun
231 || t == t_cfunfixed || t == t_cclosure) ? ECL_T : ECL_NIL))
232 }
233
234 cl_object
235 cl_eq(cl_object x, cl_object y)
236 {
237 @(return ((x == y) ? ECL_T : ECL_NIL));
238 }
239
240 /*
241 * EQL-comparison of floats. If we are using signed zeros and NaNs,
242 * numeric comparison of floating points is not equivalent to bit-wise
243 * equality. In particular every two NaNs always give false
244 * (= #1=(/ 0.0 0.0) #1#) => NIL
245 * and signed zeros always compare equal
246 * (= 0 -0.0) => T
247 * which is not the same as what EQL should return
248 * (EQL #1=(/ 0.0 0.0) #1#) => T
249 * (EQL 0 -0.0) => NIL
250 *
251 * Furthermore, we can not use bit comparisons because in some platforms
252 * long double has unused bits that makes two long floats be = but not eql.
253 */
254 #if !defined(ECL_SIGNED_ZERO) && !defined(ECL_IEEE_FP)
255 #define FLOAT_EQL(name, type) \
256 static bool name(type a, type b) { return a == b; }
257 #else
258 #define FLOAT_EQL(name, type) \
259 static bool name(type a, type b) { \
260 if (a == b) return signbit(a) == signbit(b); \
261 if (isnan(a) || isnan(b)) return isnan(a) && isnan(b); \
262 return 0; \
263 }
264 #endif
265
266 FLOAT_EQL(float_eql, float);
267 FLOAT_EQL(double_eql, double);
268 FLOAT_EQL(long_double_eql, long double);
269 #undef FLOAT_EQL
270
271 bool
272 ecl_eql(cl_object x, cl_object y)
273 {
274 if (x == y)
275 return TRUE;
276 if (ECL_IMMEDIATE(x) || ECL_IMMEDIATE(y))
277 return FALSE;
278 if (x->d.t != y->d.t)
279 return FALSE;
280 switch (x->d.t) {
281 case t_bignum:
282 return (_ecl_big_compare(x, y) == 0);
283 case t_ratio:
284 return (ecl_eql(x->ratio.num, y->ratio.num) &&
285 ecl_eql(x->ratio.den, y->ratio.den));
286 case t_singlefloat:
287 return float_eql(ecl_single_float(x), ecl_single_float(y));
288 case t_longfloat:
289 return long_double_eql(ecl_long_float(x), ecl_long_float(y));
290 case t_doublefloat:
291 return double_eql(ecl_double_float(x), ecl_double_float(y));
292 case t_complex:
293 return (ecl_eql(x->gencomplex.real, y->gencomplex.real) &&
294 ecl_eql(x->gencomplex.imag, y->gencomplex.imag));
295 #ifdef ECL_COMPLEX_FLOAT
296 case t_csfloat:
297 return (float_eql(crealf(ecl_csfloat(x)), crealf(ecl_csfloat(y))) &&
298 float_eql(cimagf(ecl_csfloat(x)), cimagf(ecl_csfloat(y))));
299 case t_cdfloat:
300 return (double_eql(creal(ecl_cdfloat(x)), creal(ecl_cdfloat(y))) &&
301 double_eql(cimag(ecl_cdfloat(x)), cimag(ecl_cdfloat(y))));
302 case t_clfloat:
303 return (long_double_eql(creall(ecl_clfloat(x)), creall(ecl_clfloat(y))) &&
304 long_double_eql(cimagl(ecl_clfloat(x)), cimagl(ecl_clfloat(y))));
305 #endif
306 #ifdef ECL_SSE2
307 case t_sse_pack:
308 return !memcmp(x->sse.data.b8, y->sse.data.b8, 16);
309 #endif
310 default:
311 return FALSE;
312 }
313 }
314
315 cl_object
316 cl_eql(cl_object x, cl_object y)
317 {
318 @(return (ecl_eql(x, y) ? ECL_T : ECL_NIL));
319 }
320
321 bool
322 ecl_equal(cl_object x, cl_object y)
323 {
324 cl_type tx, ty;
325 BEGIN:
326 if (x==y)
327 return(TRUE);
328 tx = ecl_t_of(x);
329 ty = ecl_t_of(y);
330 switch (tx) {
331 case t_list:
332 if (Null(x) || Null(y)) {
333 /* If X is NIL, then X and Y must be EQ */
334 return FALSE;
335 }
336 if (tx != ty || !ecl_equal(CAR(x), CAR(y)))
337 return FALSE;
338 x = CDR(x);
339 y = CDR(y);
340 goto BEGIN;
341 case t_symbol:
342 case t_vector:
343 case t_array:
344 case t_fixnum:
345 return FALSE;
346 case t_bignum:
347 return (tx == ty) && (_ecl_big_compare(x,y) == 0);
348 case t_ratio:
349 return (tx == ty) && ecl_eql(x->ratio.num, y->ratio.num) &&
350 ecl_eql(x->ratio.den, y->ratio.den);
351 case t_singlefloat: {
352 if (tx != ty) return 0;
353 return float_eql(ecl_single_float(x), ecl_single_float(y));
354 }
355 case t_doublefloat: {
356 if (tx != ty) return 0;
357 return double_eql(ecl_double_float(x), ecl_double_float(y));
358 }
359 case t_longfloat: {
360 if (tx != ty) return 0;
361 return long_double_eql(ecl_long_float(x), ecl_long_float(y));
362 }
363 case t_complex:
364 return (tx == ty) && ecl_eql(x->gencomplex.real, y->gencomplex.real) &&
365 ecl_eql(x->gencomplex.imag, y->gencomplex.imag);
366 #ifdef ECL_COMPLEX_FLOAT
367 case t_csfloat:
368 if (tx != ty) return 0;
369 return (float_eql(crealf(ecl_csfloat(x)), crealf(ecl_csfloat(y))) &&
370 float_eql(cimagf(ecl_csfloat(x)), cimagf(ecl_csfloat(y))));
371 case t_cdfloat:
372 if (tx != ty) return 0;
373 return (double_eql(creal(ecl_cdfloat(x)), creal(ecl_cdfloat(y))) &&
374 double_eql(cimag(ecl_cdfloat(x)), cimag(ecl_cdfloat(y))));
375 case t_clfloat:
376 if (tx != ty) return 0;
377 return (long_double_eql(creall(ecl_clfloat(x)), creall(ecl_clfloat(y))) &&
378 long_double_eql(cimagl(ecl_clfloat(x)), cimagl(ecl_clfloat(y))));
379 #endif
380 case t_character:
381 return (tx == ty) && (ECL_CHAR_CODE(x) == ECL_CHAR_CODE(y));
382 case t_base_string:
383 #ifdef ECL_UNICODE
384 case t_string:
385 if (ty != t_base_string && ty != t_string)
386 return FALSE;
387 #else
388 if (ty != t_base_string)
389 return FALSE;
390 #endif
391 return ecl_string_eq(x, y);
392 case t_bitvector: {
393 cl_index i, ox, oy;
394 if (ty != tx)
395 return FALSE;
396 if (x->vector.fillp != y->vector.fillp)
397 return(FALSE);
398 ox = x->vector.offset;
399 oy = y->vector.offset;
400 for (i = 0; i < x->vector.fillp; i++)
401 if(((x->vector.self.bit[(i+ox)/8] << (i+ox)%8) & 0200)
402 != ((y->vector.self.bit[(i+oy)/8] << (i+oy)%8) & 0200))
403 return(FALSE);
404 return(TRUE);
405 }
406 case t_pathname:
407 return ty == tx &&
408 ecl_equal(x->pathname.host, y->pathname.host) &&
409 ecl_equal(x->pathname.device, y->pathname.device) &&
410 ecl_equal(x->pathname.directory, y->pathname.directory) &&
411 ecl_equal(x->pathname.name, y->pathname.name) &&
412 ecl_equal(x->pathname.type, y->pathname.type) &&
413 ecl_equal(x->pathname.version, y->pathname.version);
414 case t_foreign:
415 return (tx == ty) && (x->foreign.data == y->foreign.data);
416 default:
417 return FALSE;
418 }
419 }
420
421 cl_object
422 cl_equal(cl_object x, cl_object y)
423 {
424 @(return (ecl_equal(x, y) ? ECL_T : ECL_NIL));
425 }
426
427 bool
428 ecl_equalp(cl_object x, cl_object y)
429 {
430 cl_type tx, ty;
431 cl_index j;
432 BEGIN:
433 if (x == y)
434 return TRUE;
435 tx = ecl_t_of(x);
436 ty = ecl_t_of(y);
437
438 switch (tx) {
439 case t_fixnum:
440 case t_bignum:
441 case t_ratio:
442 case t_singlefloat:
443 case t_doublefloat:
444 case t_longfloat:
445 case t_complex:
446 #ifdef ECL_COMPLEX_FLOAT
447 case t_csfloat:
448 case t_cdfloat:
449 case t_clfloat:
450 #endif
451 return ECL_NUMBER_TYPE_P(ty) && ecl_number_equalp(x, y);
452 case t_vector:
453 case t_base_string:
454 case t_bitvector:
455 #ifdef ECL_UNICODE
456 case t_string:
457 if (ty != t_vector && ty != t_base_string && ty != t_bitvector
458 && ty != t_string)
459 return FALSE;
460 #else
461 if (ty != t_vector && ty != t_base_string && ty != t_bitvector)
462 return FALSE;
463 #endif
464 j = x->vector.fillp;
465 if (j != y->vector.fillp)
466 return FALSE;
467 goto ARRAY;
468 case t_array:
469 if (ty != t_array || x->array.rank != y->array.rank)
470 return FALSE;
471 if (x->array.rank > 1) {
472 cl_index i = 0;
473 for (i = 0; i < x->array.rank; i++)
474 if (x->array.dims[i] != y->array.dims[i])
475 return(FALSE);
476 }
477 if (x->array.dim != y->array.dim)
478 return(FALSE);
479 j=x->array.dim;
480 ARRAY: {
481 cl_index i;
482 cl_elttype etx = x->array.elttype;
483 cl_elttype ety = y->array.elttype;
484 if (etx == ety
485 && (etx == ecl_aet_b8 || etx == ecl_aet_i8
486 || etx == ecl_aet_b16 || etx == ecl_aet_i16
487 || etx == ecl_aet_b32 || etx == ecl_aet_i32
488 || etx == ecl_aet_b64 || etx == ecl_aet_i64
489 || etx == ecl_aet_fix || etx == ecl_aet_index)) {
490 return memcmp(x->array.self.t, y->array.self.t, j * ecl_aet_size[etx]) == 0;
491 }
492
493 #define AET_FLOAT_EQUALP(t1, t2) \
494 case ecl_aet_##t2: \
495 for (i = 0; i < j; i++) \
496 if (x->array.self.t1[i] != y->array.self.t2[i]) \
497 return(FALSE); \
498 return(TRUE);
499
500 #ifdef ECL_COMPLEX_FLOAT
501 #define AET_FLOAT_EQUALP_CF(t1, cf) AET_FLOAT_EQUALP(t1, cf)
502 #else
503 #define AET_FLOAT_EQUALP_CF(t1, cf)
504 #endif
505
506 #define AET_FLOAT_SWITCH(t1) \
507 case ecl_aet_##t1: \
508 switch(ety) { \
509 AET_FLOAT_EQUALP(t1, sf); \
510 AET_FLOAT_EQUALP(t1, df); \
511 AET_FLOAT_EQUALP(t1, lf); \
512 AET_FLOAT_EQUALP_CF(t1, csf); \
513 AET_FLOAT_EQUALP_CF(t1, cdf); \
514 AET_FLOAT_EQUALP_CF(t1, clf); \
515 default: \
516 break; \
517 }
518
519 switch (etx) {
520 AET_FLOAT_SWITCH(sf);
521 AET_FLOAT_SWITCH(df);
522 AET_FLOAT_SWITCH(lf);
523 #ifdef ECL_COMPLEX_FLOAT
524 AET_FLOAT_SWITCH(csf);
525 AET_FLOAT_SWITCH(cdf);
526 AET_FLOAT_SWITCH(clf);
527 #endif
528 default:
529 break;
530 }
531 #undef AET_FLOAT_EQUALP
532 #undef AET_FLOAT_SWITCH
533 #undef AET_FLOAT_EQUALP_CF
534
535 for (i = 0; i < j; i++)
536 if (!ecl_equalp(ecl_aref_unsafe(x, i), ecl_aref_unsafe(y, i)))
537 return(FALSE);
538 return(TRUE);
539 }
540 case t_character:
541 return (ty == tx) && ecl_char_equal(x, y);
542 case t_list:
543 if ((tx != ty) || Null(x) || Null(y)) {
544 /* X is NIL but it is not EQ to Y */
545 return FALSE;
546 }
547 if (!ecl_equalp(CAR(x), CAR(y)))
548 return(FALSE);
549 x = CDR(x);
550 y = CDR(y);
551 goto BEGIN;
552 case t_instance: {
553 cl_index i;
554 if ((ty != tx) || (ECL_CLASS_OF(x) != ECL_CLASS_OF(y)) || si_structurep(x) == ECL_NIL)
555 return(FALSE);
556 for (i = 0; i < x->instance.length; i++)
557 if (!ecl_equalp(x->instance.slots[i], y->instance.slots[i]))
558 return(FALSE);
559 return(TRUE);
560 }
561 case t_pathname:
562 return (tx == ty) && ecl_equal(x, y);
563 case t_hashtable: {
564 if (tx != ty ||
565 x->hash.entries != y->hash.entries ||
566 x->hash.test != y->hash.test)
567 return(FALSE);
568 {
569 cl_env_ptr env = ecl_process_env();
570 cl_object iterator = si_hash_table_iterator(x);
571 do {
572 cl_object ndx = _ecl_funcall1(iterator);
573 if (Null(ndx)) {
574 return TRUE;
575 } else {
576 cl_object key = env->values[1];
577 cl_object x_value = env->values[2];
578 cl_object y_value = ecl_gethash_safe(key, y, OBJNULL);
579 if (y_value == OBJNULL || !ecl_equalp(x_value, y_value))
580 return FALSE;
581 }
582 } while (1);
583 }
584 }
585 case t_random:
586 return (tx == ty) && ecl_equalp(x->random.value, y->random.value);
587 default:
588 return ecl_eql(x,y);
589 }
590 }
591
592 cl_object
593 cl_equalp(cl_object x, cl_object y)
594 {
595 @(return (ecl_equalp(x, y) ? ECL_T : ECL_NIL));
596 }
597
598 cl_object
599 si_fixnump(cl_object x)
600 {
601 @(return (ECL_FIXNUMP(x) ? ECL_T : ECL_NIL));
602 }