Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Math::BigInt] as_bin() can fail if IVSIZE == 4 && NVSIZE == 16 #18533

Closed
sisyphus opened this issue Feb 6, 2021 · 1 comment
Closed

[Math::BigInt] as_bin() can fail if IVSIZE == 4 && NVSIZE == 16 #18533

sisyphus opened this issue Feb 6, 2021 · 1 comment

Comments

@sisyphus
Copy link
Contributor

sisyphus commented Feb 6, 2021

First raised as pjacklam/p5-Math-BigInt#1 , which was later referenced at https://rt.cpan.org/Public/Bug/Display.html?id=134045

No responses yet received, so I'll raise it here.

Module:
Math::BigInt::Calc
Afflicts both Math::BigInt and Math::BigFloat

Description

Initially I thought this was specific to Win32 and the __float128 nvtype.
Now I believe (unverified) that the problem will afflict every perl where IVSIZE==4 && NVSIZE==16.
(Note: AFAIK, the 80-bit extended precision long double is NOT subject to this issue, but will only ever report NVSIZE==16 when IVSIZE != 4.)
That this issue has never come up before is testament to the rarity of such a perl configuration.
There are a number of FAIL reports (all from Cygwin) for this - eg:
https://www.cpantesters.org/cpan/report/aebbf2eb-6cf4-1014-b776-34ea06c191b3
And I can reproduce the issue on 32-bit builds on native MS Windows when ivsize is 4 and nvtype is __float128.

My thoughts about how to deal with it have changed as I've learned a bit more about the problem, though I still don't know what the troublesome code is doing. (I tried unsuccessfully to obtain some info about it from Daniel Pfeiffer who, apparently, wrote that code.)

AFAIK, the bug impacts ONLY upon integer values whose bitsize is in the range 33 to 53 (inclusive), and on 54-bit integer values whose decimal representation does not exceed 16 digits.
Attached is a simplistic patch (diff.txt) that hard codes values that work for this configuration into Calc.pm.
Given that we're dealing with a very rare configuration, I'm thinking that such a "permanent bandaid" might be all that's really needed.

I guess I'll probably need to present a new PR over at https://github.com/pjacklam/p5-Math-BigInt, but I'd like to get some feedback on whether this patch is acceptable before I do that.

Steps to Reproduce

C:\>perl -MMath::BigInt -e "$x = Math::BigInt->new('0b'. ('101' x 12)); print $x->as_bin();"
0b11111111111111111111111111111111

Expected behavior

That one-liner should output:
0b101101101101101101101101101101101101
Perl configuration

Summary of my perl5 (revision 5 version 33 subversion 6) configuration:
   
  Platform:
    osname=MSWin32
    osvers=6.1.7601
    archname=MSWin32-x86-multi-thread-quadmath
    uname=''
    config_args='undef'
    hint=recommended
    useposix=true
    d_sigaction=undef
    useithreads=define
    usemultiplicity=define
    use64bitint=undef
    use64bitall=undef
    uselongdouble=undef
    usemymalloc=n
    default_inc_excludes_dot=define
  Compiler:
    cc='gcc'
    ccflags =' -DWIN32 -fdiagnostics-color=never -DPERL_TEXTMODE_SCRIPTS -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -DUSE_PERLIO -D__USE_MINGW_ANSI_STDIO -fno-strict-aliasing -mms-bitfields'
    optimize='-s -O2'
    cppflags='-DWIN32'
    ccversion=''
    gccversion='10.2.0'
    gccosandvers=''
    intsize=4
    longsize=4
    ptrsize=4
    doublesize=8
    byteorder=1234
    doublekind=3
    d_longlong=define
    longlongsize=8
    d_longdbl=define
    longdblsize=12
    longdblkind=3
    ivtype='long'
    ivsize=4
    nvtype='__float128'
    nvsize=16
    Off_t='long long'
    lseeksize=8
    alignbytes=8
    prototype=define
  Linker and Libraries:
    ld='g++'
    ldflags ='-s -L"C:\perl-5.33.6-1020\lib\MSWin32-x86-multi-thread-quadmath\CORE" -L"C:\_32\winlibs-gcc-1020\mingw32\lib" -L"C:\_32\winlibs-gcc-1020\mingw32\i686-w64-mingw32\lib" -L"C:\_32\winlibs-gcc-1020\mingw32\lib\gcc\i686-w64-mingw32\10.2.0"'
    libpth=C:\_32\winlibs-gcc-1020\mingw32\lib C:\_32\winlibs-gcc-1020\mingw32\i686-w64-mingw32\lib C:\_32\winlibs-gcc-1020\mingw32\lib\gcc\i686-w64-mingw32\10.2.0 C:\_32\msys_1020\1.0\local\lib
    libs= -lmoldname -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -lnetapi32 -luuid -lws2_32 -lmpr -lwinmm -lversion -lodbc32 -lodbccp32 -lcomctl32 -lquadmath
    perllibs= -lmoldname -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -lnetapi32 -luuid -lws2_32 -lmpr -lwinmm -lversion -lodbc32 -lodbccp32 -lcomctl32 -lquadmath
    libc=
    so=dll
    useshrplib=true
    libperl=libperl533.a
    gnulibc_version=''
  Dynamic Linking:
    dlsrc=dl_win32.xs
    dlext=dll
    d_dlsymun=undef
    ccdlflags=' '
    cccdlflags=' '
    lddlflags='-mdll -s -L"C:\perl-5.33.6-1020\lib\MSWin32-x86-multi-thread-quadmath\CORE" -L"C:\_32\winlibs-gcc-1020\mingw32\lib" -L"C:\_32\winlibs-gcc-1020\mingw32\i686-w64-mingw32\lib" -L"C:\_32\winlibs-gcc-1020\mingw32\lib\gcc\i686-w64-mingw32\10.2.0"'


Characteristics of this binary (from libperl): 
  Compile-time options:
    HAS_TIMES
    HAVE_INTERP_INTERN
    MULTIPLICITY
    PERLIO_LAYERS
    PERL_COPY_ON_WRITE
    PERL_DONT_CREATE_GVSV
    PERL_IMPLICIT_CONTEXT
    PERL_IMPLICIT_SYS
    PERL_MALLOC_WRAP
    PERL_OP_PARENT
    PERL_PRESERVE_IVUV
    USE_ITHREADS
    USE_LARGE_FILES
    USE_LOCALE
    USE_LOCALE_COLLATE
    USE_LOCALE_CTYPE
    USE_LOCALE_NUMERIC
    USE_LOCALE_TIME
    USE_PERLIO
    USE_PERL_ATOF
    USE_QUADMATH
  Built under MSWin32
  Compiled at Jan 21 2021 13:33:50
  @INC:
    C:/perl-5.33.6-1020/site/lib/MSWin32-x86-multi-thread-quadmath
    C:/perl-5.33.6-1020/site/lib
    C:/perl-5.33.6-1020/lib/MSWin32-x86-multi-thread-quadmath
    C:/perl-5.33.6-1020/lib

diff.txt

@sisyphus
Copy link
Contributor Author

sisyphus commented Jul 4, 2021

@sisyphus sisyphus closed this as completed Jul 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant