Ruby  3.1.4p223 (2023-03-30 revision HEAD)
compar.h
1 #ifndef INTERNAL_COMPAR_H /*-*-C-*-vi:se ft=c:*/
2 #define INTERNAL_COMPAR_H
11 #include "internal/vm.h" /* for rb_method_basic_definition_p */
12 
13 #define STRING_P(s) (RB_TYPE_P((s), T_STRING) && CLASS_OF(s) == rb_cString)
14 
15 enum {
16  cmp_opt_Integer,
17  cmp_opt_String,
18  cmp_opt_Float,
19  cmp_optimizable_count
20 };
21 
22 struct cmp_opt_data {
23  unsigned int opt_methods;
24  unsigned int opt_inited;
25 };
26 
27 #define NEW_CMP_OPT_MEMO(type, value) \
28  NEW_PARTIAL_MEMO_FOR(type, value, cmp_opt)
29 #define CMP_OPTIMIZABLE_BIT(type) (1U << TOKEN_PASTE(cmp_opt_,type))
30 #define CMP_OPTIMIZABLE(data, type) \
31  (((data).opt_inited & CMP_OPTIMIZABLE_BIT(type)) ? \
32  ((data).opt_methods & CMP_OPTIMIZABLE_BIT(type)) : \
33  (((data).opt_inited |= CMP_OPTIMIZABLE_BIT(type)), \
34  rb_method_basic_definition_p(TOKEN_PASTE(rb_c,type), id_cmp) && \
35  ((data).opt_methods |= CMP_OPTIMIZABLE_BIT(type))))
36 
37 #define OPTIMIZED_CMP(a, b, data) \
38  ((FIXNUM_P(a) && FIXNUM_P(b) && CMP_OPTIMIZABLE(data, Integer)) ? \
39  (((long)a > (long)b) ? 1 : ((long)a < (long)b) ? -1 : 0) : \
40  (STRING_P(a) && STRING_P(b) && CMP_OPTIMIZABLE(data, String)) ? \
41  rb_str_cmp(a, b) : \
42  (RB_FLOAT_TYPE_P(a) && RB_FLOAT_TYPE_P(b) && CMP_OPTIMIZABLE(data, Float)) ? \
43  rb_float_cmp(a, b) : \
44  rb_cmpint(rb_funcallv(a, id_cmp, 1, &b), a, b))
45 
46 /* compar.c */
47 VALUE rb_invcmp(VALUE, VALUE);
48 
49 #endif /* INTERNAL_COMPAR_H */
uintptr_t VALUE
Type that represents a Ruby object.
Definition: value.h:40