| 1 |
|
| #define PERL_NO_GET_CONTEXT // we'll define thread context if necessary (faster) |
| 2 |
|
| #include "EXTERN.h" // globals/constant import locations |
| 3 |
|
| #include "perl.h" // Perl symbols, structures and constants definition |
| 4 |
|
| #include "XSUB.h" // xsubpp functions and macros |
| 5 |
|
| #include <math.h> |
| 6 |
|
| |
| 7 |
|
| #ifndef OpSIBLING |
| 8 |
|
| # define OpSIBLING(o) ((o)->op_sibling) |
| 9 |
|
| #endif |
| 10 |
|
| #ifndef OpMORESIB_set |
| 11 |
|
| # define OpMORESIB_set(o, sib) ((o)->op_sibling = (sib)) |
| 12 |
|
| #endif |
| 13 |
|
| #ifndef OpLASTSIB_set |
| 14 |
|
| # define OpLASTSIB_set(o, parent) ((o)->op_sibling = NULL) |
| 15 |
|
| #endif |
| 16 |
|
| |
| 17 |
|
| #if PERL_VERSION >= 14 |
| 18 |
|
| static XOP entropy_xop; |
| 19 |
|
| #endif |
| 20 |
|
| |
| 21 |
7 |
| static int makehist(const unsigned char *str, int *hist, int len) { |
| 22 |
|
| int chars[256]; |
| 23 |
7 |
| int histlen = 0, i; |
| 24 |
1799 |
▶ | for (i = 0; i < 256; i++) chars[i] = -1; |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 256; i++) chars[i] = -1; |
1792 |
7 |
1799 |
|
| 25 |
100 |
▶ | for (i = 0; i < len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < len; i++) { |
93 |
7 |
100 |
|
| 26 |
93 |
| int c = (int)str[i]; |
| 27 |
93 |
▶ | if (chars[c] == -1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (chars[c] == -1) { |
69 |
24 |
93 |
|
| 28 |
69 |
| chars[c] = histlen; |
| 29 |
69 |
| histlen++; |
| 30 |
|
| } |
| 31 |
93 |
| hist[chars[c]]++; |
| 32 |
|
| } |
| 33 |
7 |
| return histlen; |
| 34 |
|
| } |
| 35 |
|
| |
| 36 |
8 |
| static double entropy(const char *str) { |
| 37 |
8 |
| int len = strlen(str); |
| 38 |
8 |
| int hist[256] = {0}; /* Max 256 unique chars, stack allocated */ |
| 39 |
|
| int histlen, i; |
| 40 |
8 |
| double out = 0.0; |
| 41 |
8 |
▶ | if (len == 0) return 0.0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len == 0) return 0.0; |
1 |
7 |
8 |
|
| 42 |
7 |
| histlen = makehist((const unsigned char *)str, hist, len); |
| 43 |
76 |
▶ | for (i = 0; i < histlen; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < histlen; i++) { |
69 |
7 |
76 |
|
| 44 |
69 |
| double p = (double)hist[i] / len; |
| 45 |
69 |
| out -= p * log2(p); |
| 46 |
|
| } |
| 47 |
7 |
| return out; |
| 48 |
|
| } |
| 49 |
|
| |
| 50 |
|
| #if PERL_VERSION >= 14 |
| 51 |
|
| |
| 52 |
7 |
| static OP* pp_entropy(pTHX) { |
| 53 |
7 |
| dSP; |
| 54 |
7 |
| SV *sv = TOPs; |
| 55 |
|
| STRLEN len; |
| 56 |
7 |
| const char *str = SvPV(sv, len); |
| 57 |
7 |
| double result = entropy(str); |
| 58 |
7 |
| POPs; |
| 59 |
7 |
| mPUSHn(result); |
| 60 |
7 |
| RETURN; |
| 61 |
|
| } |
| 62 |
|
| |
| 63 |
10 |
| static OP* entropy_call_checker(pTHX_ OP *entersubop, GV *namegv, SV *ckobj) { |
| 64 |
|
| OP *pushop, *argop, *nextop, *newop; |
| 65 |
|
| PERL_UNUSED_ARG(namegv); |
| 66 |
|
| PERL_UNUSED_ARG(ckobj); |
| 67 |
10 |
| pushop = cLISTOPx(entersubop)->op_first; |
| 68 |
10 |
▶ | if (!pushop) return entersubop; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pushop) return entersubop; |
0 |
10 |
10 |
|
| 69 |
10 |
▶ | if (pushop->op_type == OP_NULL && cLISTOPx(pushop)->op_first) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (pushop->op_type == OP_NULL && cLISTOPx(pushop)->op_first) { |
10 |
0 |
10 |
| if (pushop->op_type == OP_NULL && cLISTOPx(pushop)->op_first) { |
10 |
0 |
10 |
|
| 70 |
10 |
| pushop = cLISTOPx(pushop)->op_first; |
| 71 |
|
| } |
| 72 |
10 |
▶ | argop = OpSIBLING(pushop); |
Branch: 1 branch
| Branch | True | False | Total |
| argop = OpSIBLING(pushop); |
10 |
0 |
10 |
|
| 73 |
10 |
▶ | if (!argop) return entersubop; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!argop) return entersubop; |
0 |
10 |
10 |
|
| 74 |
10 |
▶ | switch (argop->op_type) { |
Branch: 1 branch
| Branch | True | False | Total |
| switch (argop->op_type) { |
7 |
3 |
10 |
|
| 75 |
7 |
| case OP_CONST: |
| 76 |
|
| case OP_PADSV: |
| 77 |
|
| case OP_RV2SV: |
| 78 |
|
| case OP_AELEM: |
| 79 |
|
| case OP_HELEM: |
| 80 |
|
| case OP_AELEMFAST: |
| 81 |
|
| #if PERL_VERSION >= 16 |
| 82 |
|
| case OP_AELEMFAST_LEX: |
| 83 |
|
| #endif |
| 84 |
7 |
| break; |
| 85 |
3 |
| default: |
| 86 |
3 |
| return entersubop; |
| 87 |
|
| } |
| 88 |
7 |
▶ | nextop = OpSIBLING(argop); |
Branch: 1 branch
| Branch | True | False | Total |
| nextop = OpSIBLING(argop); |
7 |
0 |
7 |
|
| 89 |
7 |
▶ | if (!nextop) return entersubop; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nextop) return entersubop; |
0 |
7 |
7 |
|
| 90 |
7 |
▶ | if (OpSIBLING(nextop)) return entersubop; |
Branch: 2 branches
| Branch | True | False | Total |
| if (OpSIBLING(nextop)) return entersubop; |
0 |
7 |
7 |
| if (OpSIBLING(nextop)) return entersubop; |
0 |
0 |
0 |
|
| 91 |
7 |
| OpMORESIB_set(pushop, nextop); |
| 92 |
7 |
| OpLASTSIB_set(argop, NULL); |
| 93 |
7 |
| newop = newUNOP(OP_NULL, 0, argop); |
| 94 |
7 |
| newop->op_type = OP_CUSTOM; |
| 95 |
7 |
| newop->op_ppaddr = pp_entropy; |
| 96 |
7 |
| op_free(entersubop); |
| 97 |
7 |
| return newop; |
| 98 |
|
| } |
| 99 |
|
| |
| 100 |
|
| #endif |
| 101 |
|
| |
| 102 |
|
| MODULE = Shannon::Entropy::XS PACKAGE = Shannon::Entropy::XS |
| 103 |
|
| PROTOTYPES: ENABLE |
| 104 |
|
| |
| 105 |
|
| double |
| 106 |
|
| entropy(string) |
| 107 |
|
| SV *string |
| 108 |
|
| CODE: |
| 109 |
|
| STRLEN len; |
| 110 |
1 |
| const char *str = SvPV(string, len); |
| 111 |
1 |
| RETVAL = entropy(str); |
| 112 |
|
| OUTPUT: |
| 113 |
|
| RETVAL |
| 114 |
|
| |
| 115 |
|
| BOOT: |
| 116 |
|
| #if PERL_VERSION >= 14 |
| 117 |
|
| { |
| 118 |
|
| CV *entropy_cv; |
| 119 |
2 |
| XopENTRY_set(&entropy_xop, xop_name, "entropy"); |
| 120 |
2 |
| XopENTRY_set(&entropy_xop, xop_desc, "Shannon entropy calculation"); |
| 121 |
2 |
| XopENTRY_set(&entropy_xop, xop_class, OA_UNOP); |
| 122 |
2 |
| Perl_custom_op_register(aTHX_ pp_entropy, &entropy_xop); |
| 123 |
2 |
| entropy_cv = get_cv("Shannon::Entropy::XS::entropy", 0); |
| 124 |
2 |
▶ | if (entropy_cv) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (entropy_cv) { |
2 |
0 |
2 |
|
| 125 |
2 |
| cv_set_call_checker(entropy_cv, entropy_call_checker, (SV *)entropy_cv); |
| 126 |
|
| } |
| 127 |
|
| } |
| 128 |
|
| #endif |