ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/XS.xs
(Generate patch)

Comparing JSON-XS/XS.xs (file contents):
Revision 1.74 by root, Wed Mar 19 15:17:53 2008 UTC vs.
Revision 1.75 by root, Wed Mar 19 15:37:54 2008 UTC

4 4
5#include <assert.h> 5#include <assert.h>
6#include <string.h> 6#include <string.h>
7#include <stdlib.h> 7#include <stdlib.h>
8#include <stdio.h> 8#include <stdio.h>
9#include <limits.h>
9#include <float.h> 10#include <float.h>
10 11
11#if defined(__BORLANDC__) || defined(_MSC_VER) 12#if defined(__BORLANDC__) || defined(_MSC_VER)
12# define snprintf _snprintf // C compilers have this in stdio.h 13# define snprintf _snprintf // C compilers have this in stdio.h
13#endif 14#endif
15// some old perls do not have this, try to make it work, no 16// some old perls do not have this, try to make it work, no
16// guarentees, though. if it breaks, you get to keep the pieces. 17// guarentees, though. if it breaks, you get to keep the pieces.
17#ifndef UTF8_MAXBYTES 18#ifndef UTF8_MAXBYTES
18# define UTF8_MAXBYTES 13 19# define UTF8_MAXBYTES 13
19#endif 20#endif
21
22#define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 2)
20 23
21#define F_ASCII 0x00000001UL 24#define F_ASCII 0x00000001UL
22#define F_LATIN1 0x00000002UL 25#define F_LATIN1 0x00000002UL
23#define F_UTF8 0x00000004UL 26#define F_UTF8 0x00000004UL
24#define F_INDENT 0x00000008UL 27#define F_INDENT 0x00000008UL
648 else if (SvIOKp (sv)) 651 else if (SvIOKp (sv))
649 { 652 {
650 // we assume we can always read an IV as a UV and vice versa 653 // we assume we can always read an IV as a UV and vice versa
651 // we assume two's complement 654 // we assume two's complement
652 // we assume no aliasing issues in the union 655 // we assume no aliasing issues in the union
653 if (SvIsUV (sv) ? SvUVX (sv) > 59000 656 if (SvIsUV (sv) ? SvUVX (sv) <= 59000
654 : SvIVX (sv) > 59000 || SvIVX (sv) < -59000) 657 : SvIVX (sv) <= 59000 && SvIVX (sv) >= -59000)
655 {
656 // large integer, use the (rather slow) snprintf way.
657 need (enc, sizeof (UV) * 5 / 2 + 1); // CHAR_BIT is at least 8
658 enc->cur +=
659 SvIsUV(sv)
660 ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv))
661 : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv));
662 }
663 else
664 { 658 {
665 // optimise the "small number case" 659 // optimise the "small number case"
666 // code will likely be branchless and use only a single multiplication 660 // code will likely be branchless and use only a single multiplication
667 // works for numbers up to 59074 661 // works for numbers up to 59074
668 I32 i = SvIVX (sv); 662 I32 i = SvIVX (sv);
679 673
680 // now output digit by digit, each time masking out the integer part 674 // now output digit by digit, each time masking out the integer part
681 // and multiplying by 5 while moving the decimal point one to the right, 675 // and multiplying by 5 while moving the decimal point one to the right,
682 // resulting in a net multiplication by 10. 676 // resulting in a net multiplication by 10.
683 // we always write the digit to memory but conditionally increment 677 // we always write the digit to memory but conditionally increment
684 // the pointer, to ease the usage of conditional move instructions. 678 // the pointer, to enable the use of conditional move instructions.
685 digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; 679 digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffffUL) * 5;
686 digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; 680 digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffffUL) * 5;
687 digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; 681 digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffffUL) * 5;
688 digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; 682 digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffffUL) * 5;
689 digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' 683 digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0'
684 }
685 else
686 {
687 // large integer, use the (rather slow) snprintf way.
688 need (enc, IVUV_MAXCHARS);
689 enc->cur +=
690 SvIsUV(sv)
691 ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv))
692 : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv));
690 } 693 }
691 } 694 }
692 else if (SvROK (sv)) 695 else if (SvROK (sv))
693 encode_rv (enc, SvRV (sv)); 696 encode_rv (enc, SvRV (sv));
694 else if (!SvOK (sv)) 697 else if (!SvOK (sv))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines