| 584 | } |
| 585 | |
| 586 | void ecall_tanh(float *input,int N,float *output){ |
| 587 | float *x = (float*)malloc(sizeof(float)*N); |
| 588 | memset(x, 0, sizeof(float)*N); |
| 589 | for(int j=0;j<N;j++){ |
| 590 | x[j] = 0.0f; |
| 591 | float sum = input[insert*N+j]; |
| 592 | float c = 0.0f; |
| 593 | for(int i=0;i<Nt;i++){ |
| 594 | float y = input[indexs[i]*N+j] - c; |
| 595 | float t = sum + y; |
| 596 | c = (t - sum) - y; |
| 597 | sum = t; |
| 598 | } |
| 599 | x[j] = sum; |
| 600 | } |
| 601 | |
| 602 | for (int j = 0; j < N; j++) { |
| 603 | float exp_pos = expf(x[j]); |
| 604 | float exp_neg = expf(-x[j]); |
| 605 | if(exp_pos == INFINITY || exp_neg == INFINITY){ |
| 606 | x[j] = x[j] > 0 ? 1.0f : -1.0f; |
| 607 | } |
| 608 | else{ |
| 609 | x[j]= (exp_pos-exp_neg)/(exp_pos+exp_neg); |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | float mean = 0.0f, std, mean_sqr = 0.0f; |
| 614 | float c_mean = 0.0f, c_mean_sqr = 0.0f; |
| 615 | |
| 616 | for (int j = 0; j < N; j++) { |
| 617 | float y = (x[j]/static_cast<float>(N)) - c_mean; |
| 618 | float t = mean + y; |
| 619 | c_mean = (t - mean) - y; |
| 620 | mean = t; |
| 621 | |
| 622 | float y2 = (x[j]*x[j]/static_cast<float>(N)) - c_mean_sqr; |
| 623 | float t2 = mean_sqr + y2; |
| 624 | c_mean_sqr = (t2 - mean_sqr) - y2; |
| 625 | mean_sqr = t2; |
| 626 | } |
| 627 | std = sqrtf(mean_sqr - mean * mean)/static_cast<float>(Nt); |
| 628 | mean = mean/static_cast<float>(Nt); |
| 629 | for(int i=0;i<N*(Ne-1);i++){ |
| 630 | output[i] =(x[i%N] - gaussrand(mean, std))/static_cast<float>(Nt+1); |
| 631 | } |
| 632 | for(int j=N*(Ne-1);j<N*Ne;j++){ |
| 633 | output[j] = 0.0f; |
| 634 | float sum = 0.0f; |
| 635 | float c = 0.0f; |
| 636 | for(int i=0;i<Nt;i++){ |
| 637 | float y = -1.0f * output[indexs[i]*N+(j-N*(Ne-1))] - c; |
| 638 | float t = sum + y; |
| 639 | c = (t - sum) - y; |
| 640 | sum = t; |
| 641 | } |
| 642 | output[j] =x[j-N*(Ne-1)] + sum; |
| 643 | } |