#ifdef vax # # General Image Processing System # Peter G. Ford # Center for Space Research # Copyright 1984 Massachusetts Institute of Technology # # Module: icvtf.c Creation Date: 04/28/84 # Version: 2.1 Update: 11/14/86 # Function: Convert an IBM binary array to a VAX binary array. # Called from Fortran or C routines. # Syntax: ierr = icvti(iibm, nitem, iarr); # ierr = icvtf(fibm, nitem, farr); # ierr = icvtd(dibm, nitem, darr); # ierr: number of conversion errors (see note 3) # iibm: input array of 4-byte IBM integers # iarr: output array of 4-byte VAX integers # fibm: input array of 4-byte IBM floats # farr: output array of 4-byte VAX F-format floats # dibm: input array of 8-byte IBM doubles (REAL*8) # darr: output array of 8-byte VAX doubles (D-format) # nitem: number of items to convert # # Notes: [1] It is assumed that the IBM input fields have # NOT already been byte-swapped. # [2] Unnormalized IBM floats are interpreted correctly. # [3] While there are no specific bit patterns that represent # illegal IBM floating point fields, the IBM exponent # range is larger than the VAX. Fields that would # cause overflow or underflow when converting are # set to the hexadecimal values described below. The # value returned by icvtf or icvtd tells the calling # program the number of such fix-ups that were made. # icvti always returns a zero value. # Routine Condition Output Value # icvtf underflow 0x00000100 # icvtf overflow 0xffffffff # icvtd underflow 0x0000010000000000 # icvtd overflow 0xffffffffffffffff # # Entry Points: .globl _icvti # Convert IBM INTEGER*4 to Fortran integer*4 .globl _icvti_ # Convert IBM INTEGER*4 to C long int .globl _icvtf # Convert IBM REAL*4 to Fortran real*4 .globl _icvtf_ # Convert IBM REAL*4 to C float .globl _icvtd # Convert IBM REAL*8 to Fortran real*8 .globl _icvtd_ # Convert IBM REAL*8 to C double (long float) .data .asciz "@(#)icvtf.c GIPS 2.1 (MIT/CSR) 11/14/86" .text .align 1 _icvti_:.word 0xe00 # define register mask movl *8(ap),r10 # load integer count jbr 1f # branch to common code .align 1 _icvti: .word 0xe00 # define register mask movl 8(ap),r10 # load integer count 1: movl 12(ap),r9 # point to VAX longs (output) movl 4(ap),r11 # point to IBM integer*4's (input) movl 12(ap),r9 # point to VAX longs (output) 2: sobgeq r10,3f # loop over integer count clrl r0 # clear return code ret # done 3: movb (r11)+,r0 # load the first byte ashl $8,r0,r0 # shift left movb (r11)+,r0 # load the second byte ashl $8,r0,r0 # shift left movb (r11)+,r0 # load the third byte ashl $8,r0,r0 # shift left movb (r11)+,r0 # load the fourth byte movl r0,(r9)+ # store the answer jbr 2b # loop again .align 1 _icvtf_:.word 0xfc0 # define register mask movl *8(ap),r10 # load float count jbr 1f # branch to common code .align 1 _icvtf: .word 0xfc0 # define register mask movl 8(ap),r10 # load float count 1: movl 4(ap),r11 # point to IBM floats (input) movl 12(ap),r9 # point to VAX floats (output) clrl r0 # clear return register 2: sobgeq r10,3f # loop over floats count ret # done 3: movzbl (r11)+,r8 # load the IBM sign and exponent bicl3 $0x80,r8,r7 # extract the IBM exponent ashl $2,r7,r7 # convert exponent to binary subl2 $127,r7 # re-bias to VAX specs movb (r11)+,r6 # load first mantissa byte ashl $8,r6,r6 # shift left movb (r11)+,r6 # load second mantissa byte ashl $8,r6,r6 # shift left movb (r11)+,r6 # load third mantissa byte ashl $8,r6,r6 # shift mantissa into sign bit jeql 5f # if zero, so is the float! 4: decl r7 # decrement the exponent jleq 6f # branch if underflow rotl $1,r6,r6 # rotate the mantissa jlbc r6,4b # ... until leading bit drops off cmpl $255,r7 # test again jlss 7f # branch if overflow bicl2 $1,r6 # clear the hidden bit bisl2 r7,r6 # insert the binary exponent rotl $7,r6,r6 # put mantissa in proper place jbc $7,r8,5f # branch unless float is negative bisl2 $0x8000,r6 # negate our float 5: movl r6,(r9)+ # store the answer jbr 2b # loop over specified floats 6: movl $0x00000100,r6 # insert the smallest VAX float jbr 8f # continue 7: movl $0xffffffff,r6 # insert the largest VAX float 8: incl r0 # increment error count jbr 5b # continue .align 1 _icvtd_:.word 0xfe0 # define register mask movl *8(ap),r10 # load doubles count jbr 1f # branch to common code .align 1 _icvtd: .word 0xfe0 # define register mask movl 8(ap),r10 # load doubles count 1: movl 4(ap),r11 # point to IBM doubles (input) movl 12(ap),r9 # point to VAX doubles (output) clrl r0 # clear return register 2: sobgeq r10,3f # loop over doubles count ret # done 3: movzbl (r11)+,r8 # load the IBM sign and exponent bicl3 $0x80,r8,r7 # extract the IBM exponent ashl $2,r7,r7 # convert exponent to binary subl2 $127,r7 # re-bias to VAX specs movb (r11)+,r5 # load first mantissa byte ashl $8,r5,r5 # shift left movb (r11)+,r5 # load second mantissa byte ashl $8,r5,r5 # shift left movb (r11)+,r5 # load third mantissa byte movb (r11)+,r6 # load fourth mantissa byte ashl $8,r6,r6 # shift left movb (r11)+,r6 # load fifth mantissa byte ashl $8,r6,r6 # shift left movb (r11)+,r6 # load sixth mantissa byte ashl $8,r6,r6 # shift left movb (r11)+,r6 # load seventh and last mantissa byte ashl $8,r5,r5 # shift upper mantissa into sign bit jneq 4f # branch if non-zero tstl r6 # test the lower mantissa jeql 6f # if zero, so is the double! 4: rotl $1,r5,r5 # rotate msb to lsb decl r7 # decrement the exponent jleq 7f # branch if underflow jlbs r5,5f # get out of loop if set rotl $1,r6,r6 # rotate lower mantissa jlbc r6,4b # branch if bit not set bisl2 $0x100,r5 # insert the bit in the upper mantissa bicl2 $1,r6 # clear it from the lower mantissa jbr 4b # continue the shift loop 5: cmpl $255,r7 # test the exponent jlss 8f # branch if overflow bicl2 $1,r5 # clear the hidden bit bisl2 r7,r5 # insert the binary exponent rotl $7,r5,r5 # put mantissa in proper place rotl $16,r6,r6 # rotate lower mantissa too jbc $7,r8,6f # branch unless float is negative bisl2 $0x8000,r5 # negate our double 6: movq r5,(r9)+ # store the answer jbr 2b # loop over specified doubles 7: movl $0x00000100,r5 # insert the smallest VAX double clrl r6 # clear lower mantissa jbr 9f # continue 8: movl $0xffffffff,r5 # insert the largest VAX double movl r5,r6 # copy to lower mantissa 9: incl r0 # increment error count jbr 6b # continue #else #ifndef lint static char *sccsid = "@(#)icvtf.c GIPS 2.1 (MIT/CSR) 11/14/86"; #endif static int errors = -1; /* init flag & error count */ static double table[256] = { 0.0 }; /* powers of 2 */ static init() { register i = 127; register double *d1 = &table[128]; register double *d2; for (*(d2 = d1) = 1.0; --i > 0; d1++) *--d2 = 1.0/(d1[1] = 2 * *d1); d1[1] = *d1; d2[-1] = *d2; } static double dconv(buf, sw) register unsigned char *buf; register sw; { register unsigned char *b = buf; register unsigned long fract = 0; register exp = 4; register shft; double f; static short shift[] = {-1,3,2,2,1,1,1,1,0,0,0,0,0,0,0,0}; while (--exp >= 0) fract = (fract << 8) | *b++; shft = shift[(fract >> 20) & 0xf]; if (shft < 0) { if (fract) errors++; return 0; } exp = ((int)((fract >> 24) & 0177)-64)*4+128-shft; fract = (fract << shft) & 0xffffff; if (exp <= 1) if (exp == 1) fract >>= 1; else { exp = 1; fract = 0x800000; errors++; } else if (exp >= 255) if (exp == 255) fract <<= 1; else { exp = 255; fract = 0x7fffff; errors++; } f = fract * table[104]; if (sw) { for (fract = 0, sw = 4; --sw >= 0; ) fract = (fract << 8) | *b++ ; if (fract & 0x80000000) { fract &= ~0x80000000; f += table[104-1+shft]; } f += fract * table[104-32+shft]; } return f * ((*buf & 0x80) ? -table[exp] : table[exp]); } icvtf(fibm, nitem, farr) register char *fibm; register int nitem; register float *farr; { if (errors < 0) init(); for (errors = 0; --nitem >= 0; fibm += 4) *farr++ = (float) dconv((unsigned char *)fibm, 0); return errors; } icvtd(dibm, nitem, darr) register char *dibm; register int nitem; register double *darr; { if (errors < 0) init(); for (errors = 0; --nitem >= 0; dibm += 8) *darr++ = dconv((unsigned char *)dibm, 1); return errors; } icvti(iibm, nitem, iarr) register char *iibm; register int nitem; register int *iarr; { register i; register n; for ( ; --nitem >= 0; *iarr++ = n) for (n = 0, i = 4; --i >= 0; ) n = (n << 8) | (*iibm++ & 0377); return 0; } icvtf_(fibm, pts, farr) char *fibm; int *pts; float *farr; { return icvtf(fibm, *pts, farr); } icvtd_(dibm, pts, darr) char *dibm; int *pts; double *darr; { return icvtd(dibm, *pts, darr); } icvti_(iibm, pts, iarr) char *iibm; int *pts; int *iarr; { return icvti(iibm, *pts, iarr); } #endif