| 1 |
|
| #define PERL_NO_GET_CONTEXT |
| 2 |
|
| #include "EXTERN.h" |
| 3 |
|
| #include "perl.h" |
| 4 |
|
| #include "XSUB.h" |
| 5 |
|
| #include <stdlib.h> |
| 6 |
|
| #include <string.h> |
| 7 |
|
| |
| 8 |
|
| static double PRECISION = 0.01; |
| 9 |
|
| static double OFFSET = 0.5555555; |
| 10 |
124 |
| static int precision_places(double prec) { |
| 11 |
124 |
| int places = 0; |
| 12 |
124 |
| double p = prec; |
| 13 |
248 |
▶ | while (p < 1.0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < 1.0) { |
124 |
124 |
248 |
|
| 14 |
124 |
| p *= 10.0; |
| 15 |
124 |
| places++; |
| 16 |
124 |
▶ | if (places > 18) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (places > 18) break; |
0 |
124 |
124 |
|
| 17 |
|
| } |
| 18 |
124 |
| return places; |
| 19 |
|
| } |
| 20 |
|
| |
| 21 |
124 |
| static SV *sn_to_sv(double val) { |
| 22 |
|
| dTHX; |
| 23 |
124 |
| int places = precision_places(PRECISION); |
| 24 |
124 |
| double scaled = val / PRECISION; |
| 25 |
124 |
| double delta = 1e-12; |
| 26 |
|
| double rounded_units; |
| 27 |
124 |
▶ | if (scaled >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (scaled >= 0) { |
123 |
1 |
124 |
|
| 28 |
123 |
| rounded_units = floor(scaled + 0.5 + delta); |
| 29 |
|
| } else { |
| 30 |
1 |
| rounded_units = ceil(scaled - 0.5 - delta); |
| 31 |
|
| } |
| 32 |
124 |
| double rounded = rounded_units * PRECISION; |
| 33 |
|
| |
| 34 |
|
| char fmt[16]; |
| 35 |
124 |
| snprintf(fmt, sizeof(fmt), "%%.%df", places); |
| 36 |
124 |
| char *num = Perl_form(aTHX_ fmt, rounded); |
| 37 |
124 |
| size_t len = strlen(num); |
| 38 |
124 |
| char *dot = strchr(num, '.'); |
| 39 |
124 |
▶ | if (dot) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dot) { |
83 |
41 |
124 |
|
| 40 |
168 |
▶ | while (len > 1 && num[len - 1] == '0') { |
Branch: 2 branches
| Branch | True | False | Total |
| while (len > 1 && num[len - 1] == '0') { |
168 |
0 |
168 |
| while (len > 1 && num[len - 1] == '0') { |
85 |
83 |
168 |
|
| 41 |
85 |
| num[--len] = '\0'; |
| 42 |
|
| } |
| 43 |
83 |
▶ | if (len > 0 && num[len - 1] == '.') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (len > 0 && num[len - 1] == '.') { |
83 |
0 |
83 |
| if (len > 0 && num[len - 1] == '.') { |
54 |
29 |
83 |
|
| 44 |
54 |
| num[--len] = '\0'; |
| 45 |
|
| } |
| 46 |
|
| } |
| 47 |
124 |
| double val_out = strtod(num, NULL); |
| 48 |
124 |
▶ | if (val_out == 0.0 && val != 0.0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (val_out == 0.0 && val != 0.0) { |
6 |
118 |
124 |
| if (val_out == 0.0 && val != 0.0) { |
0 |
6 |
6 |
|
| 49 |
0 |
| return newSVnv(val); |
| 50 |
|
| } |
| 51 |
124 |
| return newSVpv(num, 0); |
| 52 |
|
| } |
| 53 |
|
| |
| 54 |
154 |
| static SV * new (double num) { |
| 55 |
|
| dTHX; |
| 56 |
154 |
| SV *sv = newSVnv(num); |
| 57 |
154 |
| return sv_bless(newRV_noinc(sv), gv_stashsv(newSVpv("smallnum::XS", 12), 0)); |
| 58 |
|
| } |
| 59 |
|
| |
| 60 |
161 |
| static double _sref (SV * n) { |
| 61 |
|
| dTHX; |
| 62 |
|
| double num; |
| 63 |
161 |
▶ | if (SvROK(n)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(n)) { |
157 |
4 |
161 |
|
| 64 |
157 |
| num = SvNV(SvRV(n)); |
| 65 |
|
| } else { |
| 66 |
4 |
| num = SvNV(n); |
| 67 |
|
| } |
| 68 |
161 |
| return num; |
| 69 |
|
| } |
| 70 |
|
| |
| 71 |
|
| MODULE = smallnum::XS PACKAGE = smallnum::XS |
| 72 |
|
| PROTOTYPES: ENABLE |
| 73 |
|
| FALLBACK: TRUE |
| 74 |
|
| |
| 75 |
|
| SV * _smallnum (...) |
| 76 |
|
| CODE: |
| 77 |
134 |
| double num = SvNV(ST(0)); |
| 78 |
134 |
| RETVAL = new(num); |
| 79 |
|
| OUTPUT: |
| 80 |
|
| RETVAL |
| 81 |
|
| |
| 82 |
|
| SV * _set_precision (num) |
| 83 |
|
| SV * num |
| 84 |
|
| CODE: |
| 85 |
2 |
| PRECISION = SvNV(num); |
| 86 |
2 |
| RETVAL = sn_to_sv(PRECISION); |
| 87 |
|
| OUTPUT: |
| 88 |
|
| RETVAL |
| 89 |
|
| |
| 90 |
|
| SV * _set_offset (num) |
| 91 |
|
| SV * num |
| 92 |
|
| CODE: |
| 93 |
1 |
| OFFSET = SvNV(num); |
| 94 |
1 |
| RETVAL = sn_to_sv(OFFSET); |
| 95 |
|
| OUTPUT: |
| 96 |
|
| RETVAL |
| 97 |
|
| |
| 98 |
|
| SV * _num (...) |
| 99 |
|
| OVERLOAD: \"\" |
| 100 |
|
| CODE: |
| 101 |
121 |
| double num = _sref(ST(0)); |
| 102 |
121 |
| double out = num >= 0 |
| 103 |
120 |
| ? PRECISION * floor((num + (OFFSET * PRECISION)) / PRECISION) |
| 104 |
121 |
▶ | : PRECISION * ceil((num - OFFSET * PRECISION) / PRECISION); |
Branch: 1 branch
| Branch | True | False | Total |
| : PRECISION * ceil((num - OFFSET * PRECISION) / PRECISION); |
120 |
1 |
121 |
|
| 105 |
121 |
| RETVAL = sn_to_sv(out); |
| 106 |
|
| OUTPUT: |
| 107 |
|
| RETVAL |
| 108 |
|
| |
| 109 |
|
| SV * _divide (...) |
| 110 |
|
| OVERLOAD: / |
| 111 |
|
| CODE: |
| 112 |
13 |
| double num1 = _sref(ST(0)); |
| 113 |
13 |
| double num2 = _sref(ST(1)); |
| 114 |
12 |
▶ | double out = (num1 && num2) |
Branch: 1 branch
| Branch | True | False | Total |
| double out = (num1 && num2) |
11 |
1 |
12 |
|
| 115 |
7 |
▶ | ? (SvOK(ST(2)) && SvIV(ST(2)) > 0 |
Branch: 1 branch
| Branch | True | False | Total |
| ? (SvOK(ST(2)) && SvIV(ST(2)) > 0 |
3 |
4 |
7 |
|
| 116 |
|
| ? num2 / num1 |
| 117 |
18 |
▶ | : num1 / num2) |
Branch: 1 branch
| Branch | True | False | Total |
| : num1 / num2) |
7 |
4 |
11 |
|
| 118 |
25 |
▶ | : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| : 0; |
12 |
1 |
13 |
|
| 119 |
13 |
| RETVAL = new(out); |
| 120 |
|
| OUTPUT: |
| 121 |
|
| RETVAL |
| 122 |
|
| |
| 123 |
|
| SV * _multiply (...) |
| 124 |
|
| OVERLOAD: * |
| 125 |
|
| CODE: |
| 126 |
7 |
| double num1 = _sref(ST(0)); |
| 127 |
7 |
| double num2 = _sref(ST(1)); |
| 128 |
7 |
| double out = num2 * num1; |
| 129 |
7 |
| RETVAL = new(out); |
| 130 |
|
| OUTPUT: |
| 131 |
|
| RETVAL |