lbn68000.c
上传用户:zbbssh
上传日期:2007-01-08
资源大小:196k
文件大小:14k
源码类别:

CA认证

开发平台:

C/C++

  1. /*
  2.  * lbn68000.c - 16-bit bignum primitives for the 68000 (or 68010) processors.
  3.  *
  4.  * Copyright (c) 1995  Colin Plumb.  All rights reserved.
  5.  * For licensing and other legal details, see the file legal.c.
  6.  *
  7.  * This was written for Metrowerks C, and while it should be reasonably
  8.  * portable, NOTE that Metrowerks lets a callee trash a0, a1, d0, d1, and d2.
  9.  * Some 680x0 compilers make d2 callee-save, so instructions to save it
  10.  * will have to be added.
  11.  * 
  12.  * This code supports 16 or 32-bit ints, based on UINT_MAX.
  13.  * Regardless of UINT_MAX, only bignums up to 64K words (1 million bits)
  14.  * are supported.  (68k hackers will recognize this as a consequence of
  15.  * using dbra.)
  16.  *
  17.  * These primitives use little-endian word order.
  18.  * (The order of bytes within words is irrelevant to this issue.)
  19.  */
  20. #include <limits.h>
  21. #include "lbn.h"        /* Should include lbn68000.h */
  22. /*
  23.  * The Metrowerks C compiler (1.2.2) produces bad 68k code for the
  24.  * following input, which happens to be the inner loop of lbnSub1,
  25.  * so a few less than critical routines have been recoded in assembly
  26.  * to avoid the bug.  (Optimizer on or off does not matter.)
  27.  * 
  28.  * unsigned
  29.  * decrement(unsigned *num, unsigned len)
  30.  * {
  31.  *      do {
  32.  *              if ((*num++)-- != 0)
  33.  *                      return 0;
  34.  *      } while (--len);
  35.  *      return 1;
  36.  * }
  37.  */
  38. asm BNWORD16
  39. lbnSub1_16(BNWORD16 *num, unsigned len, BNWORD16 borrow)
  40. {
  41.         movea.l 4(sp),a0        /* num */
  42. #if UINT_MAX == 0xffff
  43.         move.w  10(sp),d0       /* borrow */
  44. #else
  45.         move.w  12(sp),d0       /* borrow */
  46. #endif
  47.         sub.w   d0,(a0)+
  48.         bcc             done
  49. #if UINT_MAX == 0xffff
  50.         move.w  8(sp),d0        /* len */
  51. #else
  52.         move.w  10(sp),d0       /* len */
  53. #endif
  54.         subq.w  #2,d0
  55.         bcs             done
  56. loop:
  57.         subq.w  #1,(a0)+
  58.         dbcc    d0,loop
  59. done:
  60.         moveq.l #0,d0
  61.         addx.w  d0,d0
  62.         rts
  63. }
  64. asm BNWORD16
  65. lbnAdd1_16(BNWORD16 *num, unsigned len, BNWORD16 carry)
  66. {
  67.         movea.l 4(sp),a0        /* num */
  68. #if UINT_MAX == 0xffff
  69.         move.w  10(sp),d0       /* carry */
  70. #else
  71.         move.w  12(sp),d0       /* carry */
  72. #endif
  73.         add.w   d0,(a0)+
  74.         bcc             done
  75. #if UINT_MAX == 0xffff
  76.         move.w  8(sp),d0        /* len */
  77. #else
  78.         move.w  10(sp),d0       /* len */
  79. #endif
  80.         subq.w  #2,d0
  81.         bcs             done
  82. loop:
  83.         addq.w  #1,(a0)+
  84.         dbcc    d0,loop
  85. done:
  86.         moveq.l #0,d0
  87.         addx.w  d0,d0
  88.         rts
  89. }
  90. asm void
  91. lbnMulN1_16(BNWORD16 *out, BNWORD16 const *in, unsigned len, BNWORD16 k)
  92. {
  93.         move.w  d3,-(sp)        /* 2 bytes of stack frame */
  94.         move.l  2+4(sp),a1      /* out */
  95.         move.l  2+8(sp),a0      /* in */
  96. #if UINT_MAX == 0xffff
  97.         move.w  2+12(sp),d3     /* len */
  98.         move.w  2+14(sp),d2     /* k */
  99. #else
  100.         move.w  2+14(sp),d3     /* len (low 16 bits) */
  101.         move.w  2+16(sp),d2     /* k */
  102. #endif
  103.         move.w  (a0)+,d1        /* First multiply */
  104.         mulu.w  d2,d1
  105.         move.w  d1,(a1)+
  106.         clr.w   d1
  107.         swap    d1
  108.         subq.w  #1,d3           /* Setup for loop unrolling */
  109.         lsr.w   #1,d3
  110.         bcs.s   m16_even
  111.         beq.s   m16_short
  112.         
  113.         subq.w  #1,d3           /* Set up software pipeline properly */
  114.         move.l  d1,d0
  115.         
  116. m16_loop:
  117.         move.w  (a0)+,d1
  118.         mulu.w  d2,d1
  119.         add.l   d0,d1
  120.         move.w  d1,(a1)+
  121.         clr.w d1
  122.         swap d1
  123. m16_even:
  124.         move.w  (a0)+,d0
  125.         mulu.w  d2,d0
  126.         add.l   d1,d0
  127.         move.w  d0,(a1)+
  128.         clr.w   d0
  129.         swap    d0
  130.         dbra    d3,m16_loop
  131.         
  132.         move.w  d0,(a1)
  133.         move.w  (sp)+,d3
  134.         rts
  135. m16_short:
  136.         move.w  d1,(a1)
  137.         move.w  (sp)+,d3
  138.         rts
  139. }
  140. asm BNWORD16
  141. lbnMulAdd1_16(BNWORD16 *out, BNWORD16 const *in, unsigned len, BNWORD16 k)
  142. {
  143.         move.w  d4,-(sp) 
  144.         clr.w   d4
  145.         move.w  d3,-(sp)        /* 4 bytes of stack frame */
  146.         move.l  4+4(sp),a1      /* out */
  147.         move.l  4+8(sp),a0      /* in */
  148. #if UINT_MAX == 0xffff
  149.         move.w  4+12(sp),d3     /* len */
  150.         move.w  4+14(sp),d2     /* k */
  151. #else
  152.         move.w  4+14(sp),d3     /* len (low 16 bits) */
  153.         move.w  4+16(sp),d2     /* k */
  154. #endif
  155.         move.w  (a0)+,d1        /* First multiply */
  156.         mulu.w  d2,d1
  157.         add.w   d1,(a1)+
  158.         clr.w   d1
  159.         swap    d1
  160.         addx.w  d4,d1
  161.         subq.w  #1,d3           /* Setup for loop unrolling */
  162.         lsr.w   #1,d3
  163.         bcs.s   ma16_even
  164.         beq.s   ma16_short
  165.         
  166.         subq.w  #1,d3           /* Set up software pipeline properly */
  167.         move.l  d1,d0
  168.         
  169. ma16_loop:
  170.         move.w  (a0)+,d1
  171.         mulu.w  d2,d1
  172.         add.l   d0,d1
  173.         add.w   d1,(a1)+
  174.         clr.w   d1
  175.         swap    d1
  176.         addx.w  d4,d1
  177. ma16_even:
  178.         move.w  (a0)+,d0
  179.         mulu.w  d2,d0
  180.         add.l   d1,d0
  181.         add.w   d0,(a1)+
  182.         clr.w   d0
  183.         swap    d0
  184.         addx.w  d4,d0
  185.         dbra    d3,ma16_loop
  186.         
  187.         move.w  (sp)+,d3
  188.         move.w  (sp)+,d4
  189.         rts
  190. ma16_short:
  191.         move.w  (sp)+,d3
  192.         move.l  d1,d0   
  193.         move.w  (sp)+,d4
  194.         rts
  195. }
  196. asm BNWORD16
  197. lbnMulSub1_16(BNWORD16 *out, BNWORD16 const *in, unsigned len, BNWORD16 k)
  198. {
  199.         move.w  d4,-(sp) 
  200.         clr.w   d4
  201.         move.w  d3,-(sp)        /* 4 bytes of stack frame */
  202.         move.l  4+4(sp),a1      /* out */
  203.         move.l  4+8(sp),a0      /* in */
  204. #if UINT_MAX == 0xffff
  205.         move.w  4+12(sp),d3     /* len */
  206.         move.w  4+14(sp),d2     /* k */
  207. #else
  208.         move.w  4+14(sp),d3     /* len (low 16 bits) */
  209.         move.w  4+16(sp),d2     /* k */
  210. #endif
  211.         move.w  (a0)+,d1        /* First multiply */
  212.         mulu.w  d2,d1
  213.         sub.w   d1,(a1)+
  214.         clr.w   d1
  215.         swap    d1
  216.         addx.w  d4,d1
  217.         subq.w  #1,d3           /* Setup for loop unrolling */
  218.         lsr.w   #1,d3
  219.         bcs.s   ms16_even
  220.         beq.s   ms16_short
  221.         
  222.         subq.w  #1,d3           /* Set up software pipeline properly */
  223.         move.l  d1,d0
  224.         
  225. ms16_loop:
  226.         move.w  (a0)+,d1
  227.         mulu.w  d2,d1
  228.         add.l   d0,d1
  229.         sub.w   d1,(a1)+
  230.         clr.w   d1
  231.         swap    d1
  232.         addx.w  d4,d1
  233. ms16_even:
  234.         move.w  (a0)+,d0
  235.         mulu.w  d2,d0
  236.         add.l   d1,d0
  237.         sub.w   d0,(a1)+
  238.         clr.w   d0
  239.         swap    d0
  240.         addx.w  d4,d0
  241.         dbra    d3,ms16_loop
  242.         
  243.         move.w  (sp)+,d3
  244.         move.w  (sp)+,d4
  245.         rts
  246. ms16_short:
  247.         move.w  (sp)+,d3
  248.         move.l  d1,d0   
  249.         move.w  (sp)+,d4
  250.         rts
  251. }
  252. /* The generic long/short divide doesn't know that nh < d */
  253. asm BNWORD16
  254. lbnDiv21_16(BNWORD16 *q, BNWORD16 nh, BNWORD16 nl, BNWORD16 d)
  255. {
  256.         move.l  8(sp),d0 /* nh *and* nl */
  257.         divu.w 12(sp),d0
  258.         move.l 4(sp),a0
  259.         move.w d0,(a0)
  260.         clr.w d0
  261.         swap d0
  262.         rts
  263. }
  264. asm unsigned
  265. lbnModQ_16(BNWORD16 const *n, unsigned len, BNWORD16 d)
  266. {
  267.         move.l  4(sp),a0        /* n */
  268.         moveq.l #0,d1
  269. #if UINT_MAX == 0xffff
  270.         move.w  8(sp),d1        /* len */
  271.         move.w  10(sp),d2       /* d */
  272. #else
  273.         move.w  10(sp),d1       /* len (low 16 bits) */
  274.         move.w  12(sp),d2       /* d */
  275. #endif
  276. add.l d1,a0
  277. add.l d1,a0 /* n += len */
  278. moveq.l #0,d0
  279.         subq.w  #1,d1
  280. mq16_loop:
  281.         move.w  -(a0),d0 /* Assemble remainder and new word */
  282.         divu.w  d2,d0         /* Put remainder in high half of d0 */
  283.         dbra    d1,mq16_loop    
  284.                         
  285. mq16_done:
  286.         clr.w   d0
  287.         swap    d0
  288.         rts
  289. }
  290. /*
  291.  * Detect if this is a 32-bit processor (68020+ *or* CPU32).
  292.  * Both the 68020+ and CPU32 processors (which have 32x32->64-bit
  293.  * multiply, what the 32-bit math library wants) support scaled indexed
  294.  * addressing.  The 68000 and 68010 ignore the scale selection
  295.  * bits, treating it as *1 all the time.  So a 32-bit processor
  296.  * will evaluate -2(a0,a0.w*2) as 1+1*2-2 = 1.
  297.  * A 16-bit processor will compute 1+1-2 = 0.
  298.  *
  299.  * Thus, the return value will indicate whether the chip this is
  300.  * running on supports 32x32->64-bit multiply (mulu.l).
  301.  */
  302. asm int
  303. is68020(void)
  304. {
  305.         machine 68020
  306.         lea     1,a0
  307. #if 0
  308.         lea     -2(a0,a0.w*2),a0 /* Metrowerks won't assemble this, arrgh */
  309. #else
  310.         dc.w    0x41f0, 0x82fe
  311. #endif
  312.         move.l a0,d0
  313.         rts
  314. }
  315. /*
  316.  * Since I had to hand-assemble that fancy addressing mode, I had to study
  317.  * up on 680x0 addressing modes.
  318.  * A summary of 680x0 addressing modes.
  319.  * A 68000 effective address specifies an operand on an instruction, which
  320.  * may be a register or in memory.  It is made up of a 3-bit mode and a
  321.  * 3-bit register specifier.  The meanings of the various modes are:
  322.  *
  323.  * 000 reg - Dn, n specified by "reg"
  324.  * 001 reg - An, n specified by "reg"
  325.  * 010 reg - (An)
  326.  * 011 reg - (An)+
  327.  * 100 reg - -(An)
  328.  * 101 reg - d16(An), one 16-bit displacement word follows, sign-extended
  329.  * 110 reg - Fancy addressing mode off of An, see extension word below
  330.  * 111 000 - abs.W, one 16-bit signed absolute address follows
  331.  * 111 001 - abs.L, one 32-bit absolute address follows
  332.  * 111 010 - d16(PC), one 16-bit displacemnt word follows, sign-extended
  333.  * 111 011 - Fancy addressing mode off of PC, see extension word below
  334.  * 111 100 - #immediate, followed by 16 or 32 bits of immediate value
  335.  * 111 101 - unused, reserved
  336.  * 111 110 - unused, reserved
  337.  * 111 111 - unused, reserved
  338.  *
  339.  * Memory references are to data space, except that PC-relative references
  340.  * are to program space, and are read-only.
  341.  *
  342.  * Fancy addressing modes are followed by a 16-bit extension word, and come
  343.  * in "brief" and "full" forms.
  344.  * The "brief" form looks like this.  Bit 8 is 0 to indicate this form:
  345.  *
  346.  * 1   1   1   1   1   1   1  
  347.  * 6   5   4   3   2   1   0   9   8   7   6   5   4   3   2   1   0
  348.  * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
  349.  * |A/D|  register |L/W| scale | 0 |   8-bit signed displacement   |
  350.  * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
  351.  *
  352.  * The basic effective address specifies a 32-bit base register - A0 through
  353.  * A7 or PC (the address of the following instruction).
  354.  * The A/D and register fields specify an index register.  A/D is 1 for
  355.  * address registers, and 0 for data registers.  L/W specifies the length
  356.  * of the index register, 1 for 32 bits, and 0 for 16 bits (sign-extended).
  357.  * The scale field is a left shift amount (0 to 3 bits) to apply to the
  358.  * sign-extended index register.  The final address is d8(An,Rn.X*SCALE),
  359.  * also written (d8,An,Rn.X*SCALE).  X is "W" or "L", SCALE is 1, 2, 4 or 8.
  360.  * "*1" may be omitted, as may a d8 of 0.
  361.  *
  362.  * The 68000 supports this form, but only with a scale field of 0.
  363.  * It does NOT (says the MC68030 User's Manual MC68030UM/AD, section 2.7)
  364.  * decode the scale field and the following format bit.  They are treated
  365.  * as 0.
  366.  * I recall (I don't have the data book handy) that the CPU32 processor
  367.  * core used in the 683xx series processors supports variable scales,
  368.  * but only the brief extension word form.  I suspect it decodes the
  369.  * format bit and traps if it is not zero, but I don't recall.
  370.  *
  371.  * The "full" form (680x0, x >= 2 processors only) looks like this: 
  372.  *
  373.  * 1   1   1   1   1   1   1  
  374.  * 6   5   4   3   2   1   0   9   8   7   6   5   4   3   2   1   0
  375.  * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
  376.  * |A/D|  register |L/W| scale | 1 | BS| IS|BD size| 0 | P |OD size|
  377.  * +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
  378.  *
  379.  * The first 8 bits are interpreted the same way as in the brief form,
  380.  * except that bit 8 is set to 1 to indicate the full form.
  381.  * BS, Base Suppress, if set, causes a value of 0 to be used in place of
  382.  * the base register value.  If this is set, the base register
  383.  * specified is irrelevant, except that if it is the PC, the fetch is
  384.  * still done from program space.  The specifier "ZPC" can be used in
  385.  * place of "PC" in the effective address mnemonic to represent this
  386.  * case.
  387.  * IS, Index Suppress, if set, causes a value of 0 to be used in place
  388.  * of the scaled index register. In this case, the first 7 bits of the
  389.  * extension word are irrelevant.
  390.  * BD size specifies the base displacement size.  A value of 00
  391.  * in this field is illegal, while 01, 10 and 11 indicate that the
  392.  * extension word is followed by 0, 1 or 2 16-bit words of base displacement
  393.  * (zero, sign-extended to 32 bits, and most-significant word first,
  394.  * respectively) to add to the base register value.
  395.  * Bit 3 is unused.
  396.  * The P bit is the pre/post indexing bit, and only applies if an outer
  397.  * displacement is used.  This is explained later.
  398.  * OD size specifies the size of an outer displacement.  In the simple
  399.  * case, this field is set to 00 and the effective address is
  400.  * (disp,An,Rn.X*SCALE) or (disp,PC,Rn.X*SCALE).
  401.  * In this case the P bit must be 0.  Any of those compnents may be
  402.  * suppressed, with a BD size of 01, the BS bit, or the IS bit.
  403.  * If the OD size is not 00, it encodes an outer displacement in the same
  404.  * manner as the BD size, and 0, 1 or 2 16-bit words of outer displacement
  405.  * follow the base displacement in the instruction stream.  In this case,
  406.  * this is a double-indirect addressing mode.  The base, base displacement,
  407.  * and possibly the index, specify a 32-bit memory word which holds a value
  408.  * which is fetched, and the outer displacement and possibly the index are
  409.  * added to produce the address of the operand.
  410.  * If the P bit is 0, this is pre-indexed, and the index value is added
  411.  * before the fetch of the indirect word, producing an effective address
  412.  * of ([disp,An,Rn.X*SCALE],disp).  If the P bit is 1, the post-indexed case,
  413.  * the memory word is fectched from base+base displacement, then the index
  414.  * and outer displacement are added to compute the address of the operand.
  415.  * This effective address is written ([disp,An],Rn.X*SCALE,disp).
  416.  * (In both cases, "An" may also be "PC" or "ZPC".)
  417.  * Any of the components may be omitted.  If the index is omitted (using the
  418.  * IS bit), the P bit is irrelevant, but must be written as 0.
  419.  * Thus, legal combinations of IS, P and OD size are:
  420.  * 0 0 00 - (disp,An,Rn.X*SCALE), also written disp(An,Rn.X*SCALE)
  421.  * 0 0 01 - ([disp,An,Rn.X*SCALE])
  422.  * 0 0 10 - ([disp,An,Rn.X*SCALE],d16)
  423.  * 0 0 11 - ([disp,An,Rn.X*SCALE],d32)
  424.  * 0 1 01 - ([disp,An],Rn.X*SCALE)
  425.  * 0 1 10 - ([disp,An],Rn.X*SCALE,d16)
  426.  * 0 1 11 - ([disp,An],Rn.X*SCALE,d32)
  427.  * 1 0 00 - (disp,An), also written disp(An)
  428.  * 1 0 01 - ([disp,An])
  429.  * 1 0 10 - ([disp,An],d16)
  430.  * 1 0 11 - ([disp,An],d32)
  431.  */ 
  432. /* 45678901234567890123456789012345678901234567890123456789012345678901234567 */