18c2ecf20Sopenharmony_ci/* libgcc1 routines for 68000 w/o floating-point hardware. 28c2ecf20Sopenharmony_ci Copyright (C) 1994, 1996, 1997, 1998 Free Software Foundation, Inc. 38c2ecf20Sopenharmony_ci 48c2ecf20Sopenharmony_ciThis file is part of GNU CC. 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ciGNU CC is free software; you can redistribute it and/or modify it 78c2ecf20Sopenharmony_ciunder the terms of the GNU General Public License as published by the 88c2ecf20Sopenharmony_ciFree Software Foundation; either version 2, or (at your option) any 98c2ecf20Sopenharmony_cilater version. 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ciIn addition to the permissions in the GNU General Public License, the 128c2ecf20Sopenharmony_ciFree Software Foundation gives you unlimited permission to link the 138c2ecf20Sopenharmony_cicompiled version of this file with other programs, and to distribute 148c2ecf20Sopenharmony_cithose programs without any restriction coming from the use of this 158c2ecf20Sopenharmony_cifile. (The General Public License restrictions do apply in other 168c2ecf20Sopenharmony_cirespects; for example, they cover modification of the file, and 178c2ecf20Sopenharmony_cidistribution when not linked into another program.) 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ciThis file is distributed in the hope that it will be useful, but 208c2ecf20Sopenharmony_ciWITHOUT ANY WARRANTY; without even the implied warranty of 218c2ecf20Sopenharmony_ciMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 228c2ecf20Sopenharmony_ciGeneral Public License for more details. */ 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci/* As a special exception, if you link this library with files 258c2ecf20Sopenharmony_ci compiled with GCC to produce an executable, this does not cause 268c2ecf20Sopenharmony_ci the resulting executable to be covered by the GNU General Public License. 278c2ecf20Sopenharmony_ci This exception does not however invalidate any other reasons why 288c2ecf20Sopenharmony_ci the executable file might be covered by the GNU General Public License. */ 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci/* Use this one for any 680x0; assumes no floating point hardware. 318c2ecf20Sopenharmony_ci The trailing " '" appearing on some lines is for ANSI preprocessors. Yuk. 328c2ecf20Sopenharmony_ci Some of this code comes from MINIX, via the folks at ericsson. 338c2ecf20Sopenharmony_ci D. V. Henkel-Wallace (gumby@cygnus.com) Fete Bastille, 1992 348c2ecf20Sopenharmony_ci*/ 358c2ecf20Sopenharmony_ci#include <asm/export.h> 368c2ecf20Sopenharmony_ci/* These are predefined by new versions of GNU cpp. */ 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci#ifndef __USER_LABEL_PREFIX__ 398c2ecf20Sopenharmony_ci#define __USER_LABEL_PREFIX__ _ 408c2ecf20Sopenharmony_ci#endif 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci#ifndef __REGISTER_PREFIX__ 438c2ecf20Sopenharmony_ci#define __REGISTER_PREFIX__ 448c2ecf20Sopenharmony_ci#endif 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci#ifndef __IMMEDIATE_PREFIX__ 478c2ecf20Sopenharmony_ci#define __IMMEDIATE_PREFIX__ # 488c2ecf20Sopenharmony_ci#endif 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci/* ANSI concatenation macros. */ 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci#define CONCAT1(a, b) CONCAT2(a, b) 538c2ecf20Sopenharmony_ci#define CONCAT2(a, b) a ## b 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci/* Use the right prefix for global labels. */ 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci#define SYM(x) CONCAT1 (__USER_LABEL_PREFIX__, x) 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ci/* Use the right prefix for registers. */ 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci#define REG(x) CONCAT1 (__REGISTER_PREFIX__, x) 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci/* Use the right prefix for immediate values. */ 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci#define IMM(x) CONCAT1 (__IMMEDIATE_PREFIX__, x) 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci#define d0 REG (d0) 688c2ecf20Sopenharmony_ci#define d1 REG (d1) 698c2ecf20Sopenharmony_ci#define d2 REG (d2) 708c2ecf20Sopenharmony_ci#define d3 REG (d3) 718c2ecf20Sopenharmony_ci#define d4 REG (d4) 728c2ecf20Sopenharmony_ci#define d5 REG (d5) 738c2ecf20Sopenharmony_ci#define d6 REG (d6) 748c2ecf20Sopenharmony_ci#define d7 REG (d7) 758c2ecf20Sopenharmony_ci#define a0 REG (a0) 768c2ecf20Sopenharmony_ci#define a1 REG (a1) 778c2ecf20Sopenharmony_ci#define a2 REG (a2) 788c2ecf20Sopenharmony_ci#define a3 REG (a3) 798c2ecf20Sopenharmony_ci#define a4 REG (a4) 808c2ecf20Sopenharmony_ci#define a5 REG (a5) 818c2ecf20Sopenharmony_ci#define a6 REG (a6) 828c2ecf20Sopenharmony_ci#define fp REG (fp) 838c2ecf20Sopenharmony_ci#define sp REG (sp) 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci .text 868c2ecf20Sopenharmony_ci .proc 878c2ecf20Sopenharmony_ci .globl SYM (__udivsi3) 888c2ecf20Sopenharmony_ciSYM (__udivsi3): 898c2ecf20Sopenharmony_ci#if !(defined(__mcf5200__) || defined(__mcoldfire__)) 908c2ecf20Sopenharmony_ci movel d2, sp@- 918c2ecf20Sopenharmony_ci movel sp@(12), d1 /* d1 = divisor */ 928c2ecf20Sopenharmony_ci movel sp@(8), d0 /* d0 = dividend */ 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci cmpl IMM (0x10000), d1 /* divisor >= 2 ^ 16 ? */ 958c2ecf20Sopenharmony_ci jcc L3 /* then try next algorithm */ 968c2ecf20Sopenharmony_ci movel d0, d2 978c2ecf20Sopenharmony_ci clrw d2 988c2ecf20Sopenharmony_ci swap d2 998c2ecf20Sopenharmony_ci divu d1, d2 /* high quotient in lower word */ 1008c2ecf20Sopenharmony_ci movew d2, d0 /* save high quotient */ 1018c2ecf20Sopenharmony_ci swap d0 1028c2ecf20Sopenharmony_ci movew sp@(10), d2 /* get low dividend + high rest */ 1038c2ecf20Sopenharmony_ci divu d1, d2 /* low quotient */ 1048c2ecf20Sopenharmony_ci movew d2, d0 1058c2ecf20Sopenharmony_ci jra L6 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ciL3: movel d1, d2 /* use d2 as divisor backup */ 1088c2ecf20Sopenharmony_ciL4: lsrl IMM (1), d1 /* shift divisor */ 1098c2ecf20Sopenharmony_ci lsrl IMM (1), d0 /* shift dividend */ 1108c2ecf20Sopenharmony_ci cmpl IMM (0x10000), d1 /* still divisor >= 2 ^ 16 ? */ 1118c2ecf20Sopenharmony_ci jcc L4 1128c2ecf20Sopenharmony_ci divu d1, d0 /* now we have 16 bit divisor */ 1138c2ecf20Sopenharmony_ci andl IMM (0xffff), d0 /* mask out divisor, ignore remainder */ 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci/* Multiply the 16 bit tentative quotient with the 32 bit divisor. Because of 1168c2ecf20Sopenharmony_ci the operand ranges, this might give a 33 bit product. If this product is 1178c2ecf20Sopenharmony_ci greater than the dividend, the tentative quotient was too large. */ 1188c2ecf20Sopenharmony_ci movel d2, d1 1198c2ecf20Sopenharmony_ci mulu d0, d1 /* low part, 32 bits */ 1208c2ecf20Sopenharmony_ci swap d2 1218c2ecf20Sopenharmony_ci mulu d0, d2 /* high part, at most 17 bits */ 1228c2ecf20Sopenharmony_ci swap d2 /* align high part with low part */ 1238c2ecf20Sopenharmony_ci tstw d2 /* high part 17 bits? */ 1248c2ecf20Sopenharmony_ci jne L5 /* if 17 bits, quotient was too large */ 1258c2ecf20Sopenharmony_ci addl d2, d1 /* add parts */ 1268c2ecf20Sopenharmony_ci jcs L5 /* if sum is 33 bits, quotient was too large */ 1278c2ecf20Sopenharmony_ci cmpl sp@(8), d1 /* compare the sum with the dividend */ 1288c2ecf20Sopenharmony_ci jls L6 /* if sum > dividend, quotient was too large */ 1298c2ecf20Sopenharmony_ciL5: subql IMM (1), d0 /* adjust quotient */ 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ciL6: movel sp@+, d2 1328c2ecf20Sopenharmony_ci rts 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci#else /* __mcf5200__ || __mcoldfire__ */ 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci/* Coldfire implementation of non-restoring division algorithm from 1378c2ecf20Sopenharmony_ci Hennessy & Patterson, Appendix A. */ 1388c2ecf20Sopenharmony_ci link a6,IMM (-12) 1398c2ecf20Sopenharmony_ci moveml d2-d4,sp@ 1408c2ecf20Sopenharmony_ci movel a6@(8),d0 1418c2ecf20Sopenharmony_ci movel a6@(12),d1 1428c2ecf20Sopenharmony_ci clrl d2 | clear p 1438c2ecf20Sopenharmony_ci moveq IMM (31),d4 1448c2ecf20Sopenharmony_ciL1: addl d0,d0 | shift reg pair (p,a) one bit left 1458c2ecf20Sopenharmony_ci addxl d2,d2 1468c2ecf20Sopenharmony_ci movl d2,d3 | subtract b from p, store in tmp. 1478c2ecf20Sopenharmony_ci subl d1,d3 1488c2ecf20Sopenharmony_ci jcs L2 | if no carry, 1498c2ecf20Sopenharmony_ci bset IMM (0),d0 | set the low order bit of a to 1, 1508c2ecf20Sopenharmony_ci movl d3,d2 | and store tmp in p. 1518c2ecf20Sopenharmony_ciL2: subql IMM (1),d4 1528c2ecf20Sopenharmony_ci jcc L1 1538c2ecf20Sopenharmony_ci moveml sp@,d2-d4 | restore data registers 1548c2ecf20Sopenharmony_ci unlk a6 | and return 1558c2ecf20Sopenharmony_ci rts 1568c2ecf20Sopenharmony_ci#endif /* __mcf5200__ || __mcoldfire__ */ 1578c2ecf20Sopenharmony_ci EXPORT_SYMBOL(__udivsi3) 158