Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#227 UNSIGNED-BYTE 64 type declaration problem

Unstable_git/CVS
closed
nobody
None
1
2013-01-11
2012-12-09
Mark Cox
No

Uncommenting the type declaration in the following function causes the function to longer work correctly.

(defun problem ()
  (let ((bits 0)
        (sign 1)
        (exponent 1026)
        (significand 1125899906842624))
     ; Recompile with this line uncommented and it fails.
     ; (declare (type (unsigned-byte 64) bits))
     (setf (ldb (byte  1 63) bits) sign
           (ldb (byte 11 52) bits) exponent
           (ldb (byte 52  0) bits) significand)
     bits))

 ;; Should return 13845191154443747328

The code above is extracted from the system IEEE-FLOATS, specifically ENCODE-FLOAT64.

ECL version: c1d78daf82 ( ./configure --prefix=/usr/local/ --enable-rpath )

CL-USER> *features*
(:SWANK :SERVE-EVENT :PROFILE :QUICKLISP :ASDF2 :ASDF :ASDF-UNICODE :DARWIN
 :FORMATTER :ECL-WEAK-HASH :LITTLE-ENDIAN :ECL-READ-WRITE-LOCK :LONG-LONG
 :UINT64-T :UINT32-T :UINT16-T :RELATIVE-PACKAGE-NAMES :LONG-FLOAT :UNICODE
 :DFFI :CLOS-STREAMS :CMU-FORMAT :ECL-PDE :DLOPEN :CLOS :THREADS :BOEHM-GC
 :ANSI-CL :COMMON-LISP :IEEE-FLOATING-POINT :PREFIXED-API :FFI :I686 :COMMON
 :ECL)

Discussion

  • Mark Cox
    Mark Cox
    2012-12-24

    A more succinct illustration of the problem is:

    (defun problem ()
      (let ((bits 9223372036854775808))
        (declare (type (unsigned-byte 64) bits))
        bits))
    
    ;; Returns 1141283328
    ;; Should return 9223372036854775808 or (dpb 1 (byte 1 63) 0)
    

    Settings bits 0 -> 60 of BITS works as expected. The issue is specific to bits 61 -> 63.

     
    Last edit: Mark Cox 2012-12-24
  • Mark Cox
    Mark Cox
    2012-12-28

    Further investigation of the problem reveals that the ECL generated
    code is not correctly handling BIGNUM values when SAFETY > 0 and DEBUG > 0.

    The output of the ECL compiler for the PROBLEM function is as follows:

    static cl_object L1problem()
    {
     cl_object T0;
     const cl_env_ptr cl_env_copy = ecl_process_env();
     cl_object value0;
     {
       /* !! omitted ECL compiler output !! */
    
       v1bits = ecl_fixnum(VV[0]);  // <--- VV[0] is a bignum value
       value0 = ecl_make_ulong(v1bits);
       cl_env_copy->nvalues = 1;
       return value0;
      }
     }
    }
    

    The bignum value at VV[0] is not suitable for the C macro ecl_fixnum.

    When SAFETY = 0 and DEBUG = 0 the ECL compiler generates correct code:

    static cl_object L1problem()
    {
     const cl_env_ptr cl_env_copy = ecl_process_env();
     cl_object value0;
    TTL:
     value0 = VV[0];
     cl_env_copy->nvalues = 1;
     return value0;
    }
    
     


Anonymous


Cancel   Add attachments