'c to golang conversion, why my code can not work?

I have a problem these days,I need conversion c code to golang. I tried to convert.It doesn't work.

The id should be sub-database and sub-table to db_0 and table_64; id : 3ca042611c8411e3b068abcd0189890a

c code:

uint64_t __fastcall LiveHash(const char *key, int len, int left, int right)
{
  int v4; // ecx
  unsigned __int8 *v5; // r8
  int v6; // er9
  int v7; // eax
  int v8; // edx
  int v9; // ecx
  unsigned int v10; // eax
  int v11; // eax
  unsigned int v12; // eax
  int v14; // eax
  unsigned int v15; // eax

  if ( len <= 0 || !key )
    return 0LL;
  v4 = len;
  v5 = (unsigned __int8 *)key;
  if ( len >> 2 > 0 )
  {
    v6 = len >> 2;
    do
    {
      v7 = v5[3];
      v8 = v4 + *v5 + (v5[1] << 8);
      v9 = v5[2];
      v5 += 4;
      v10 = (v8 << 16) ^ v8 ^ ((v9 + (v7 << 8)) << 11);
      --v6;
      v4 = v10 + (v10 >> 11);
    }
    while ( v6 );
    v5 = (unsigned __int8 *)&key[4 * ((len >> 2) - 1) + 4];
  }
  v11 = len & 3;
  switch ( v11 )
  {
    case 2:
      v4 = ((v4 + *v5 + (v5[1] << 8)) ^ ((v4 + *v5 + (v5[1] << 8)) << 11))
         + (((v4 + *v5 + (v5[1] << 8)) ^ ((v4 + *v5 + ((unsigned int)v5[1] << 8)) << 11)) >> 17);
      break;
    case 3:
      v14 = v4 + *v5 + (v5[1] << 8);
      v15 = v14 ^ ((char)v5[2] << 18) ^ (v14 << 16);
      v4 = v15 + (v15 >> 11);
      break;
    case 1:
      v4 = ((v4 + (char)*v5) ^ ((v4 + (char)*v5) << 10))
         + (((v4 + (char)*v5) ^ (unsigned int)((v4 + (char)*v5) << 10)) >> 1);
      break;
  }
  v12 = ((v4 ^ (unsigned int)(8 * v4)) >> 5) + (v4 ^ 8 * v4);
  return (((v12 ^ 4 * v12) >> 15) + (v12 ^ 4 * v12)) ^ ((((v12 ^ 4 * v12) >> 15) + (v12 ^ 4 * v12)) << 10);
}

my go code:

func hash(guid string)int{
    v5 := make([]int, len(guid))
    for i := range guid{
        v5[i]= int(guid[i])
    }
    fmt.Println(v5)
    var v7,v8,v9, v10 int
    v4 := len(guid)
    if v4 >> 2 > 0{
        v6 := v4>>2
        for i:=v6;i>0;i--{
            v7 = v5[3]
            v8 = v4+v5[0]+(v5[1]<<8)
            v9 = v5[2]
            v10 = (v8<<16) ^ v8 ^((v9+(v7<<8))<<11)
            v6--
            v4 = v10+(v10>>11)
        }
        start := 4*((v4>>2)-1)+4
        if start < 0{
            start = 0
        }
        v5 = v5[start:]
    }
    v11:= v4&3
    switch v11 {
    case 2:
        v4 = ((v4+v5[0]+(v5[1]<<8))^((v4+v5[0]+(v5[1]<<8))<<11))+
            (((v4+v5[0]+(v5[1]<<8)) ^ ((v4+v5[0]+(v5[1]<<8))<<11))>> 17)
    case 3:
        v14:=v4+v5[0]+(v5[1]<<8)
        v15 := v14^(v5[2]<<18)^(v14<<16)
        v4 = v15+(v15>>11)
    case 1:
        v4 = ((v4+v5[0])^((v4+v5[0])<<10))+(((v4+v5[0])^((v4+v5[0]) << 10))>>1)
    }
    v12 := ((v4^(8*v4))>>5)+(v4^8*v4)
    return (((v12^4*v12)>>15)+(v12^4*v12))^((((v12^4*v12)>>15)+(v12^4*12))<< 10)
}

my function run result is 2782879514833785705, I do not know how sub-database and sub-table to db_0 and table_64;

cgo


Solution 1:[1]

this is my herrible code, haha

func hash(guid string)uint32{
    v5 := make([]uint8, len(guid))
    for i := range guid{
        v5[i]= guid[i]
    }
    var v7,v8,v9 int32
    var v10 uint32
    v4 := int32(len(guid))
    if v4 >> 2 > 0{
        v6 := v4>>2
        for i:=v6;i>0;i--{
            v7 = int32(v5[3])
            v8 = v4+int32(v5[0])+(int32(v5[1])<<8)
            v9 = int32(v5[2])
            v5 = v5[4:]
            v10 = uint32((v8<<16) ^ v8 ^((v9+(v7<<8))<<11))
            v6--
            v4 = int32(v10+(v10 >> 11))
        }
    }
    v12 :=  int32(int64(uint32(v4^int32(uint32(8*v4)))>>5)+int64(v4^8*v4))
    v13 := uint32(v12)
    v14 := int32((v13^4*v13)>>15)
    v15 := v12^4*v12
    return uint32((v14+v15)^((v14+v15)<< 10))
}

Solution 2:[2]

I have some code about math calc, work normal in python. it can work 100000000 times. but in go it can give me right answer 100000 only, 100000000 times will stop at a high CPU useage, and never give me answer.

66811 root 20 0 703688 6808 1200 S 100.7 0.3 3:52.52 mathtest

I am thought about that ,this is a memory problem, but in fact, CPU problem. I do not know how to fix.

same problem with this? golang math can not finished with my code, but python is ok

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mark_x
Solution 2