Ruby: internal/compar.h Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

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