(UNO)8ビット機 信号処理用の1/Xの表で遊ぶ(固定小数点8,8)(8ビット/8ビット=8ビット)(割り算)


x かころぐをみよ
x 注意、精度は、悪い

目的
8ビットの割り算を高速に行う


結果

![o_coq342.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/840352/587acf07-8e9c-b893-94db-070028683b8e.jpeg)


プログラム

```c++


// 1/xのテーブルを使った8ビット単位の割り算　正の数のみ
//DIV_UC_UNO1

// 8ビット / 8ビット の　正の数の割り算
int DIV_UC(int x, int y) {

  static unsigned char uu[] = {
    255, 255, 128, 85, 64, 51, 42, 36,
    32, 28, 25, 23, 21, 19, 18, 17,
    16, 15, 14, 13, 12, 12, 11, 11,
    10, 10, 9, 9, 9, 8, 8, 8,
    8, 7, 7, 7, 7, 6, 6, 6,
    6, 6, 6, 5, 5, 5, 5, 5,
    5, 5, 5, 5, 4, 4, 4, 4,
    4, 4, 4, 4, 4, 4, 4, 4,
    4, 3, 3, 3, 3, 3, 3, 3,
    3, 3, 3, 3, 3, 3, 3, 3,
    3, 3, 3, 3, 3, 3, 2, 2,
    2, 2, 2, 2, 2, 2, 2, 2,
    2, 2, 2, 2, 2, 2, 2, 2,
    2, 2, 2, 2, 2, 2, 2, 2,
    2, 2, 2, 2, 2, 2, 2, 2,
    2, 2, 2, 2, 2, 2, 2, 2,
    2, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1,
    1, 1, 1, 1, 1, 1, 1, 1
    , 1
  };

  //範囲エラー処理
  if (x > 256 | x < 0 | y > 256 | y < 0 ) {
    return (0);
  }//endif

  //例外処理と高速化の為 x/1とx/0は、xは、そのまま
  //256を超えたら255
  if ( y == 0 | y == 1 ) {
    if ( x > 255 ) {
      return (255);
    } else {
      return (x);
    }
  }//endif

  //範囲エラーを防ぐ為と高速化の為にx/2は1シフト
  if ( y == 2 ) {
    return (x >> 1);
  }//endif

  // 1/yのメイン処理 例 64/4=16 64*64/256=16
  return ((x * uu[y]) >> 8);

}//DIV_UC


//初期化
void setup() {
  // put your setup code here, to run once:

  //シリアルポートの初期化
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);

}//setup


//メインループ
void loop() {
  // put your main code here, to run repeatedly:

  /*
  Serial.println("debug");
  //範囲エラーのテスト
  Serial.print("257/2="); Serial.println(DIV_UC(257, 2));
  Serial.print("128/257="); Serial.println(DIV_UC(128, 257));
  Serial.print("-10/2="); Serial.println(DIV_UC((-10), 2));
  Serial.print("128/-10="); Serial.println(DIV_UC(128, (-10)));

  // n/1とn/0のテスト
  Serial.print("128/1="); Serial.println(DIV_UC(128, 1));
  Serial.print("128/0="); Serial.println(DIV_UC(128, 0));
  Serial.print("256/1="); Serial.println(DIV_UC(256, 1));
  Serial.print("256/0="); Serial.println(DIV_UC(256, 0));

  // n/2のテスト
  Serial.print("128/2="); Serial.println(DIV_UC(128, 2));
  */

  //通常ケース
  Serial.print("64/4="); Serial.println(DIV_UC(64, 4));

  //3秒待つ
  delay(3000);

}//loop


```



------------------------

おまけ
------------------------



```c




unsigned char uu[]={
255,255,128,85,64,51,42,36,
32,28,25,23,21,19,18,17,
16,15,14,13,12,12,11,11,
10,10,9,9,9,8,8,8,
8,7,7,7,7,6,6,6,
6,6,6,5,5,5,5,5,
5,5,5,5,4,4,4,4,
4,4,4,4,4,4,4,4,
4,3,3,3,3,3,3,3,
3,3,3,3,3,3,3,3,
3,3,3,3,3,3,2,2,
2,2,2,2,2,2,2,2,
2,2,2,2,2,2,2,2,
2,2,2,2,2,2,2,2,
2,2,2,2,2,2,2,2,
2,2,2,2,2,2,2,2,
2,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1 
,1
};


#include <stdio.h>
int main(void){
    // Your code here!
    int s,m;

    for(int i=0;i<256;i++){
        s = i;
        if(s==0){s=1;};
        
        m = (256 * uu[s])>>8;
        
        printf(" (256/%d=%d)  [256/%d=%d] \n",
                      i,  m,      i, 256/s );
        
    }

}


```

```


 (256/0=255)  [256/0=256] 
 (256/1=255)  [256/1=256] 
 (256/2=128)  [256/2=128] 
 (256/3=85)  [256/3=85] 
 (256/4=64)  [256/4=64] 
 (256/5=51)  [256/5=51] 
 (256/6=42)  [256/6=42] 
 (256/7=36)  [256/7=36] 
 (256/8=32)  [256/8=32] 
 (256/9=28)  [256/9=28] 
 (256/10=25)  [256/10=25] 
 (256/11=23)  [256/11=23] 
 (256/12=21)  [256/12=21] 
 (256/13=19)  [256/13=19] 
 (256/14=18)  [256/14=18] 
 (256/15=17)  [256/15=17] 
 (256/16=16)  [256/16=16] 
 (256/17=15)  [256/17=15] 
 (256/18=14)  [256/18=14] 
 (256/19=13)  [256/19=13] 
 (256/20=12)  [256/20=12] 
 (256/21=12)  [256/21=12] 
 (256/22=11)  [256/22=11] 
 (256/23=11)  [256/23=11] 
 (256/24=10)  [256/24=10] 
 (256/25=10)  [256/25=10] 
 (256/26=9)  [256/26=9] 
 (256/27=9)  [256/27=9] 
 (256/28=9)  [256/28=9] 
 (256/29=8)  [256/29=8] 
 (256/30=8)  [256/30=8] 
 (256/31=8)  [256/31=8] 
 (256/32=8)  [256/32=8] 
 (256/33=7)  [256/33=7] 
 (256/34=7)  [256/34=7] 
 (256/35=7)  [256/35=7] 
 (256/36=7)  [256/36=7] 
 (256/37=6)  [256/37=6] 
 (256/38=6)  [256/38=6] 
 (256/39=6)  [256/39=6] 
 (256/40=6)  [256/40=6] 
 (256/41=6)  [256/41=6] 
 (256/42=6)  [256/42=6] 
 (256/43=5)  [256/43=5] 
 (256/44=5)  [256/44=5] 
 (256/45=5)  [256/45=5] 
 (256/46=5)  [256/46=5] 
 (256/47=5)  [256/47=5] 
 (256/48=5)  [256/48=5] 
 (256/49=5)  [256/49=5] 
 (256/50=5)  [256/50=5] 
 (256/51=5)  [256/51=5] 
 (256/52=4)  [256/52=4] 
 (256/53=4)  [256/53=4] 
 (256/54=4)  [256/54=4] 
 (256/55=4)  [256/55=4] 
 (256/56=4)  [256/56=4] 
 (256/57=4)  [256/57=4] 
 (256/58=4)  [256/58=4] 
 (256/59=4)  [256/59=4] 
 (256/60=4)  [256/60=4] 
 (256/61=4)  [256/61=4] 
 (256/62=4)  [256/62=4] 
 (256/63=4)  [256/63=4] 
 (256/64=4)  [256/64=4] 
 (256/65=3)  [256/65=3] 
 (256/66=3)  [256/66=3] 
 (256/67=3)  [256/67=3] 
 (256/68=3)  [256/68=3] 
 (256/69=3)  [256/69=3] 
 (256/70=3)  [256/70=3] 
 (256/71=3)  [256/71=3] 
 (256/72=3)  [256/72=3] 
 (256/73=3)  [256/73=3] 
 (256/74=3)  [256/74=3] 
 (256/75=3)  [256/75=3] 
 (256/76=3)  [256/76=3] 
 (256/77=3)  [256/77=3] 
 (256/78=3)  [256/78=3] 
 (256/79=3)  [256/79=3] 
 (256/80=3)  [256/80=3] 
 (256/81=3)  [256/81=3] 
 (256/82=3)  [256/82=3] 
 (256/83=3)  [256/83=3] 
 (256/84=3)  [256/84=3] 
 (256/85=3)  [256/85=3] 
 (256/86=2)  [256/86=2] 
 (256/87=2)  [256/87=2] 
 (256/88=2)  [256/88=2] 
 (256/89=2)  [256/89=2] 
 (256/90=2)  [256/90=2] 
 (256/91=2)  [256/91=2] 
 (256/92=2)  [256/92=2] 
 (256/93=2)  [256/93=2] 
 (256/94=2)  [256/94=2] 
 (256/95=2)  [256/95=2] 
 (256/96=2)  [256/96=2] 
 (256/97=2)  [256/97=2] 
 (256/98=2)  [256/98=2] 
 (256/99=2)  [256/99=2] 
 (256/100=2)  [256/100=2] 
 (256/101=2)  [256/101=2] 
 (256/102=2)  [256/102=2] 
 (256/103=2)  [256/103=2] 
 (256/104=2)  [256/104=2] 
 (256/105=2)  [256/105=2] 
 (256/106=2)  [256/106=2] 
 (256/107=2)  [256/107=2] 
 (256/108=2)  [256/108=2] 
 (256/109=2)  [256/109=2] 
 (256/110=2)  [256/110=2] 
 (256/111=2)  [256/111=2] 
 (256/112=2)  [256/112=2] 
 (256/113=2)  [256/113=2] 
 (256/114=2)  [256/114=2] 
 (256/115=2)  [256/115=2] 
 (256/116=2)  [256/116=2] 
 (256/117=2)  [256/117=2] 
 (256/118=2)  [256/118=2] 
 (256/119=2)  [256/119=2] 
 (256/120=2)  [256/120=2] 
 (256/121=2)  [256/121=2] 
 (256/122=2)  [256/122=2] 
 (256/123=2)  [256/123=2] 
 (256/124=2)  [256/124=2] 
 (256/125=2)  [256/125=2] 
 (256/126=2)  [256/126=2] 
 (256/127=2)  [256/127=2] 
 (256/128=2)  [256/128=2] 
 (256/129=1)  [256/129=1] 
 (256/130=1)  [256/130=1] 
 (256/131=1)  [256/131=1] 
 (256/132=1)  [256/132=1] 
 (256/133=1)  [256/133=1] 
 (256/134=1)  [256/134=1] 
 (256/135=1)  [256/135=1] 
 (256/136=1)  [256/136=1] 
 (256/137=1)  [256/137=1] 
 (256/138=1)  [256/138=1] 
 (256/139=1)  [256/139=1] 
 (256/140=1)  [256/140=1] 
 (256/141=1)  [256/141=1] 
 (256/142=1)  [256/142=1] 
 (256/143=1)  [256/143=1] 
 (256/144=1)  [256/144=1] 
 (256/145=1)  [256/145=1] 
 (256/146=1)  [256/146=1] 
 (256/147=1)  [256/147=1] 
 (256/148=1)  [256/148=1] 
 (256/149=1)  [256/149=1] 
 (256/150=1)  [256/150=1] 
 (256/151=1)  [256/151=1] 
 (256/152=1)  [256/152=1] 
 (256/153=1)  [256/153=1] 
 (256/154=1)  [256/154=1] 
 (256/155=1)  [256/155=1] 
 (256/156=1)  [256/156=1] 
 (256/157=1)  [256/157=1] 
 (256/158=1)  [256/158=1] 
 (256/159=1)  [256/159=1] 
 (256/160=1)  [256/160=1] 
 (256/161=1)  [256/161=1] 
 (256/162=1)  [256/162=1] 
 (256/163=1)  [256/163=1] 
 (256/164=1)  [256/164=1] 
 (256/165=1)  [256/165=1] 
 (256/166=1)  [256/166=1] 
 (256/167=1)  [256/167=1] 
 (256/168=1)  [256/168=1] 
 (256/169=1)  [256/169=1] 
 (256/170=1)  [256/170=1] 
 (256/171=1)  [256/171=1] 
 (256/172=1)  [256/172=1] 
 (256/173=1)  [256/173=1] 
 (256/174=1)  [256/174=1] 
 (256/175=1)  [256/175=1] 
 (256/176=1)  [256/176=1] 
 (256/177=1)  [256/177=1] 
 (256/178=1)  [256/178=1] 
 (256/179=1)  [256/179=1] 
 (256/180=1)  [256/180=1] 
 (256/181=1)  [256/181=1] 
 (256/182=1)  [256/182=1] 
 (256/183=1)  [256/183=1] 
 (256/184=1)  [256/184=1] 
 (256/185=1)  [256/185=1] 
 (256/186=1)  [256/186=1] 
 (256/187=1)  [256/187=1] 
 (256/188=1)  [256/188=1] 
 (256/189=1)  [256/189=1] 
 (256/190=1)  [256/190=1] 
 (256/191=1)  [256/191=1] 
 (256/192=1)  [256/192=1] 
 (256/193=1)  [256/193=1] 
 (256/194=1)  [256/194=1] 
 (256/195=1)  [256/195=1] 
 (256/196=1)  [256/196=1] 
 (256/197=1)  [256/197=1] 
 (256/198=1)  [256/198=1] 
 (256/199=1)  [256/199=1] 
 (256/200=1)  [256/200=1] 
 (256/201=1)  [256/201=1] 
 (256/202=1)  [256/202=1] 
 (256/203=1)  [256/203=1] 
 (256/204=1)  [256/204=1] 
 (256/205=1)  [256/205=1] 
 (256/206=1)  [256/206=1] 
 (256/207=1)  [256/207=1] 
 (256/208=1)  [256/208=1] 
 (256/209=1)  [256/209=1] 
 (256/210=1)  [256/210=1] 
 (256/211=1)  [256/211=1] 
 (256/212=1)  [256/212=1] 
 (256/213=1)  [256/213=1] 
 (256/214=1)  [256/214=1] 
 (256/215=1)  [256/215=1] 
 (256/216=1)  [256/216=1] 
 (256/217=1)  [256/217=1] 
 (256/218=1)  [256/218=1] 
 (256/219=1)  [256/219=1] 
 (256/220=1)  [256/220=1] 
 (256/221=1)  [256/221=1] 
 (256/222=1)  [256/222=1] 
 (256/223=1)  [256/223=1] 
 (256/224=1)  [256/224=1] 
 (256/225=1)  [256/225=1] 
 (256/226=1)  [256/226=1] 
 (256/227=1)  [256/227=1] 
 (256/228=1)  [256/228=1] 
 (256/229=1)  [256/229=1] 
 (256/230=1)  [256/230=1] 
 (256/231=1)  [256/231=1] 
 (256/232=1)  [256/232=1] 
 (256/233=1)  [256/233=1] 
 (256/234=1)  [256/234=1] 
 (256/235=1)  [256/235=1] 
 (256/236=1)  [256/236=1] 
 (256/237=1)  [256/237=1] 
 (256/238=1)  [256/238=1] 
 (256/239=1)  [256/239=1] 
 (256/240=1)  [256/240=1] 
 (256/241=1)  [256/241=1] 
 (256/242=1)  [256/242=1] 
 (256/243=1)  [256/243=1] 
 (256/244=1)  [256/244=1] 
 (256/245=1)  [256/245=1] 
 (256/246=1)  [256/246=1] 
 (256/247=1)  [256/247=1] 
 (256/248=1)  [256/248=1] 
 (256/249=1)  [256/249=1] 
 (256/250=1)  [256/250=1] 
 (256/251=1)  [256/251=1] 
 (256/252=1)  [256/252=1] 
 (256/253=1)  [256/253=1] 
 (256/254=1)  [256/254=1] 
 (256/255=1)  [256/255=1] 

```