Home | History | Annotate | Line # | Download | only in rl78
lib2shift.c revision 1.3.4.2
      1 /* Shift functions for the GCC support library for the Renesas RL78 processors.
      2    Copyright (C) 2011-2017 Free Software Foundation, Inc.
      3    Contributed by Red Hat.
      4 
      5    This file is part of GCC.
      6 
      7    GCC is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3, or (at your option)
     10    any later version.
     11 
     12    GCC 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    Under Section 7 of GPL version 3, you are granted additional
     18    permissions described in the GCC Runtime Library Exception, version
     19    3.1, as published by the Free Software Foundation.
     20 
     21    You should have received a copy of the GNU General Public License and
     22    a copy of the GCC Runtime Library Exception along with this program;
     23    see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     24    <http://www.gnu.org/licenses/>.  */
     25 
     26 typedef          int  sint32_type   __attribute__ ((mode (SI)));
     28 typedef unsigned int  uint32_type   __attribute__ ((mode (SI)));
     29 typedef          int  sint16_type   __attribute__ ((mode (HI)));
     30 typedef unsigned int  uint16_type   __attribute__ ((mode (HI)));
     31 
     32 uint32_type __ashlsi3 (uint32_type in, char bit);
     33 sint32_type __ashrsi3 (sint32_type in, char bit);
     34 int __clrsbhi2 (sint16_type x);
     35 extern int __clrsbsi2 (sint32_type x);
     36 
     37 typedef struct
     38 {
     39   union
     40   {
     41     uint32_type u;
     42     uint16_type h[2];
     43   } u;
     44 } dd;
     45 
     46 uint32_type
     47 __ashlsi3 (uint32_type in, char bit)
     48 {
     49   uint16_type h, l;
     50   dd d;
     51 
     52   if (bit > 32)
     53     return 0;
     54   if (bit < 0)
     55     return in;
     56 
     57   d.u.u = in;
     58   h = d.u.h[1];
     59   l = d.u.h[0];
     60 
     61   if (bit > 15)
     62     {
     63       h = l;
     64       l = 0;
     65       bit -= 16;
     66     }
     67 
     68   while (bit)
     69     {
     70       h = (h << 1) | (l >> 15);
     71       l <<= 1;
     72       bit --;
     73     }
     74 
     75   d.u.h[1] = h;
     76   d.u.h[0] = l;
     77   return d.u.u;
     78 }
     79 
     80 sint32_type
     81 __ashrsi3 (sint32_type in, char bit)
     82 {
     83   sint16_type h;
     84   uint16_type l;
     85   dd d;
     86 
     87   if (bit > 32)
     88     return 0;
     89   if (bit < 0)
     90     return in;
     91 
     92   d.u.u = in;
     93   h = d.u.h[1];
     94   l = d.u.h[0];
     95 
     96   while (bit)
     97     {
     98       l = (h << 15) | (l >> 1);
     99       h >>= 1;
    100       bit --;
    101     }
    102 
    103   d.u.h[1] = h;
    104   d.u.h[0] = l;
    105   return d.u.u;
    106 }
    107 
    108 int
    109 __clrsbhi2 (sint16_type x)
    110 {
    111   if (x == 0)
    112     return 15;
    113   return __clrsbsi2 ((sint32_type) x) - 16;
    114 }
    115