git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

gbdk-lib/libc/_mullong.c

      1 /*-------------------------------------------------------------------------
      2    _mullong.c - routine for multiplication of 32 bit (unsigned) long
      3 
      4    Copyright (C) 1999, Sandeep Dutta . sandeep.dutta@usa.net
      5    Copyright (C) 1999, Jean Louis VERN jlvern@writeme.com
      6 
      7    This library is free software; you can redistribute it and/or modify it
      8    under the terms of the GNU General Public License as published by the
      9    Free Software Foundation; either version 2, or (at your option) any
     10    later version.
     11 
     12    This library is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this library; see the file COPYING. If not, write to the
     19    Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
     20    MA 02110-1301, USA.
     21 
     22    As a special exception, if you link this library with other files,
     23    some of which are compiled with SDCC, to produce an executable,
     24    this library does not by itself cause the resulting executable to
     25    be covered by the GNU General Public License. This exception does
     26    not however invalidate any other reasons why the executable file
     27    might be covered by the GNU General Public License.
     28 -------------------------------------------------------------------------*/
     29 
     30 
     31 struct some_struct {
     32 	short a ;
     33 	char b;
     34 	long c ;};
     35 union bil {
     36         struct {unsigned char b0,b1,b2,b3 ;} b;
     37         struct {unsigned short lo,hi ;} i;
     38         unsigned long l;
     39         struct { unsigned char b0; unsigned short i12; unsigned char b3;} bi;
     40 } ;
     41 
     42 #define bcast(x) ((union bil *)&(x))
     43 
     44 /*
     45                      3   2   1   0
     46        X             3   2   1   0
     47        ----------------------------
     48                    0.3 0.2 0.1 0.0
     49                1.3 1.2 1.1 1.0
     50            2.3 2.2 2.1 2.0
     51        3.3 3.2 3.1 3.0
     52        ----------------------------
     53                   |3.3|1.3|0.2|0.0|   A
     54                     |2.3|0.3|0.1|     B
     55                     |3.2|1.2|1.0|     C
     56                       |2.2|1.1|       D
     57                       |3.1|2.0|       E
     58                         |2.1|         F
     59                         |3.0|         G
     60                           |-------> only this side 32 x 32 -> 32
     61 */
     62 
     63 /* 32x32->32 multiplication to be used
     64    if 16x16->16 is faster than three 8x8->16.
     65    2009, by M.Bodrato ( http://bodrato.it/ )
     66 
     67    z80 and sm83 don't have any hardware multiplication.
     68    r2k and r3k have 16x16 hardware multiplication.
     69  */
     70 long _mullong (long a, long b)
     71 {
     72   unsigned short i12;
     73 
     74   bcast(a)->i.hi *= bcast(b)->i.lo;
     75   bcast(a)->i.hi += bcast(b)->i.hi * bcast(a)->i.lo;
     76 
     77   /* only (a->i.lo * b->i.lo) 16x16->32 to do. asm? */
     78   bcast(a)->i.hi += bcast(a)->b.b1 * bcast(b)->b.b1;
     79 
     80   i12 = bcast(b)->b.b0 * bcast(a)->b.b1;
     81   bcast(b)->bi.i12 = bcast(a)->b.b0 * bcast(b)->b.b1;
     82 
     83   /* add up the two partial result, store carry in b3 */
     84   bcast(b)->b.b3 = ((bcast(b)->bi.i12 += i12) < i12);
     85 
     86   bcast(a)->i.lo  = bcast(a)->b.b0 * bcast(b)->b.b0;
     87 
     88   bcast(b)->bi.b0 = 0;
     89 
     90   return a + b;
     91 }

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.