| // Copyright 2009 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| package runtime |
| #include "runtime.h" |
| #include "type.h" |
| #include "../../cmd/ld/textflag.h" |
| |
| bool runtime·use_aeshash; |
| |
| void |
| runtime·memequal(bool *eq, uintptr s, void *a, void *b) |
| { |
| if(a == b) { |
| *eq = 1; |
| return; |
| } |
| *eq = runtime·memeq(a, b, s); |
| } |
| |
| void |
| runtime·memprint(uintptr s, void *a) |
| { |
| uint64 v; |
| |
| v = 0xbadb00b; |
| switch(s) { |
| case 1: |
| v = *(uint8*)a; |
| break; |
| case 2: |
| v = *(uint16*)a; |
| break; |
| case 4: |
| v = *(uint32*)a; |
| break; |
| case 8: |
| v = *(uint64*)a; |
| break; |
| } |
| runtime·printint_c(v); |
| } |
| |
| void |
| runtime·memcopy(uintptr s, void *a, void *b) |
| { |
| if(b == nil) { |
| runtime·memclr(a, s); |
| return; |
| } |
| runtime·memmove(a, b, s); |
| } |
| |
| void |
| runtime·memequal0(bool *eq, uintptr s, void *a, void *b) |
| { |
| USED(s); |
| USED(a); |
| USED(b); |
| *eq = true; |
| } |
| |
| void |
| runtime·memcopy0(uintptr s, void *a, void *b) |
| { |
| USED(s); |
| USED(a); |
| USED(b); |
| } |
| |
| void |
| runtime·memequal8(bool *eq, uintptr s, void *a, void *b) |
| { |
| USED(s); |
| *eq = *(uint8*)a == *(uint8*)b; |
| } |
| |
| void |
| runtime·memcopy8(uintptr s, void *a, void *b) |
| { |
| USED(s); |
| if(b == nil) { |
| *(uint8*)a = 0; |
| return; |
| } |
| *(uint8*)a = *(uint8*)b; |
| } |
| |
| void |
| runtime·memequal16(bool *eq, uintptr s, void *a, void *b) |
| { |
| USED(s); |
| *eq = *(uint16*)a == *(uint16*)b; |
| } |
| |
| void |
| runtime·memcopy16(uintptr s, void *a, void *b) |
| { |
| USED(s); |
| if(b == nil) { |
| *(uint16*)a = 0; |
| return; |
| } |
| *(uint16*)a = *(uint16*)b; |
| } |
| |
| void |
| runtime·memequal32(bool *eq, uintptr s, void *a, void *b) |
| { |
| USED(s); |
| *eq = *(uint32*)a == *(uint32*)b; |
| } |
| |
| void |
| runtime·memcopy32(uintptr s, void *a, void *b) |
| { |
| USED(s); |
| if(b == nil) { |
| *(uint32*)a = 0; |
| return; |
| } |
| *(uint32*)a = *(uint32*)b; |
| } |
| |
| void |
| runtime·memequal64(bool *eq, uintptr s, void *a, void *b) |
| { |
| USED(s); |
| *eq = *(uint64*)a == *(uint64*)b; |
| } |
| |
| void |
| runtime·memcopy64(uintptr s, void *a, void *b) |
| { |
| USED(s); |
| if(b == nil) { |
| *(uint64*)a = 0; |
| return; |
| } |
| *(uint64*)a = *(uint64*)b; |
| } |
| |
| void |
| runtime·memequal128(bool *eq, uintptr s, void *a, void *b) |
| { |
| USED(s); |
| *eq = ((uint64*)a)[0] == ((uint64*)b)[0] && ((uint64*)a)[1] == ((uint64*)b)[1]; |
| } |
| |
| void |
| runtime·memcopy128(uintptr s, void *a, void *b) |
| { |
| USED(s); |
| if(b == nil) { |
| ((uint64*)a)[0] = 0; |
| ((uint64*)a)[1] = 0; |
| return; |
| } |
| ((uint64*)a)[0] = ((uint64*)b)[0]; |
| ((uint64*)a)[1] = ((uint64*)b)[1]; |
| } |
| |
| void |
| runtime·f32equal(bool *eq, uintptr s, void *a, void *b) |
| { |
| USED(s); |
| *eq = *(float32*)a == *(float32*)b; |
| } |
| |
| void |
| runtime·f64equal(bool *eq, uintptr s, void *a, void *b) |
| { |
| USED(s); |
| *eq = *(float64*)a == *(float64*)b; |
| } |
| |
| void |
| runtime·c64equal(bool *eq, uintptr s, void *a, void *b) |
| { |
| Complex64 *ca, *cb; |
| |
| USED(s); |
| ca = a; |
| cb = b; |
| *eq = ca->real == cb->real && ca->imag == cb->imag; |
| } |
| |
| void |
| runtime·c128equal(bool *eq, uintptr s, void *a, void *b) |
| { |
| Complex128 *ca, *cb; |
| |
| USED(s); |
| ca = a; |
| cb = b; |
| *eq = ca->real == cb->real && ca->imag == cb->imag; |
| } |
| |
| void |
| runtime·algslicecopy(uintptr s, void *a, void *b) |
| { |
| USED(s); |
| if(b == nil) { |
| ((Slice*)a)->array = 0; |
| ((Slice*)a)->len = 0; |
| ((Slice*)a)->cap = 0; |
| return; |
| } |
| ((Slice*)a)->array = ((Slice*)b)->array; |
| ((Slice*)a)->len = ((Slice*)b)->len; |
| ((Slice*)a)->cap = ((Slice*)b)->cap; |
| } |
| |
| void |
| runtime·strequal(bool *eq, uintptr s, void *a, void *b) |
| { |
| intgo alen; |
| byte *s1, *s2; |
| |
| USED(s); |
| alen = ((String*)a)->len; |
| if(alen != ((String*)b)->len) { |
| *eq = false; |
| return; |
| } |
| s1 = ((String*)a)->str; |
| s2 = ((String*)b)->str; |
| if(s1 == s2) { |
| *eq = true; |
| return; |
| } |
| *eq = runtime·memeq(s1, s2, alen); |
| } |
| |
| void |
| runtime·strprint(uintptr s, void *a) |
| { |
| USED(s); |
| runtime·printstring_c(*(String*)a); |
| } |
| |
| void |
| runtime·strcopy(uintptr s, void *a, void *b) |
| { |
| USED(s); |
| if(b == nil) { |
| ((String*)a)->str = 0; |
| ((String*)a)->len = 0; |
| return; |
| } |
| ((String*)a)->str = ((String*)b)->str; |
| ((String*)a)->len = ((String*)b)->len; |
| } |
| |
| void |
| runtime·interprint(uintptr s, void *a) |
| { |
| USED(s); |
| runtime·printiface_c(*(Iface*)a); |
| } |
| |
| void |
| runtime·interequal(bool *eq, uintptr s, void *a, void *b) |
| { |
| USED(s); |
| *eq = runtime·ifaceeq_c(*(Iface*)a, *(Iface*)b); |
| } |
| |
| void |
| runtime·intercopy(uintptr s, void *a, void *b) |
| { |
| USED(s); |
| if(b == nil) { |
| ((Iface*)a)->tab = 0; |
| ((Iface*)a)->data = 0; |
| return; |
| } |
| ((Iface*)a)->tab = ((Iface*)b)->tab; |
| ((Iface*)a)->data = ((Iface*)b)->data; |
| } |
| |
| void |
| runtime·nilinterprint(uintptr s, void *a) |
| { |
| USED(s); |
| runtime·printeface_c(*(Eface*)a); |
| } |
| |
| void |
| runtime·nilinterequal(bool *eq, uintptr s, void *a, void *b) |
| { |
| USED(s); |
| *eq = runtime·efaceeq_c(*(Eface*)a, *(Eface*)b); |
| } |
| |
| void |
| runtime·nilintercopy(uintptr s, void *a, void *b) |
| { |
| USED(s); |
| if(b == nil) { |
| ((Eface*)a)->type = 0; |
| ((Eface*)a)->data = 0; |
| return; |
| } |
| ((Eface*)a)->type = ((Eface*)b)->type; |
| ((Eface*)a)->data = ((Eface*)b)->data; |
| } |
| |
| extern uintptr runtime·nohashcode; |
| |
| void |
| runtime·noequal(bool *eq, uintptr s, void *a, void *b) |
| { |
| USED(s); |
| USED(a); |
| USED(b); |
| USED(eq); |
| runtime·panicstring("comparing uncomparable types"); |
| } |
| |
| static FuncVal memhashfunc = {(void*)runtime·memhash}; |
| static FuncVal nohashfunc = {(void*)runtime·nohash}; |
| static FuncVal strhashfunc = {(void*)runtime·strhash}; |
| static FuncVal interhashfunc = {(void*)runtime·interhash}; |
| static FuncVal nilinterhashfunc = {(void*)runtime·nilinterhash}; |
| static FuncVal f32hashfunc = {(void*)runtime·f32hash}; |
| static FuncVal f64hashfunc = {(void*)runtime·f64hash}; |
| static FuncVal c64hashfunc = {(void*)runtime·c64hash}; |
| static FuncVal c128hashfunc = {(void*)runtime·c128hash}; |
| |
| static FuncVal aeshashfunc = {(void*)runtime·aeshash}; |
| static FuncVal aeshash32func = {(void*)runtime·aeshash32}; |
| static FuncVal aeshash64func = {(void*)runtime·aeshash64}; |
| static FuncVal aeshashstrfunc = {(void*)runtime·aeshashstr}; |
| |
| Alg |
| runtime·algarray[] = |
| { |
| [AMEM] { &memhashfunc, runtime·memequal, runtime·memprint, runtime·memcopy }, |
| [ANOEQ] { &nohashfunc, runtime·noequal, runtime·memprint, runtime·memcopy }, |
| [ASTRING] { &strhashfunc, runtime·strequal, runtime·strprint, runtime·strcopy }, |
| [AINTER] { &interhashfunc, runtime·interequal, runtime·interprint, runtime·intercopy }, |
| [ANILINTER] { &nilinterhashfunc, runtime·nilinterequal, runtime·nilinterprint, runtime·nilintercopy }, |
| [ASLICE] { &nohashfunc, runtime·noequal, runtime·memprint, runtime·algslicecopy }, |
| [AFLOAT32] { &f32hashfunc, runtime·f32equal, runtime·memprint, runtime·memcopy }, |
| [AFLOAT64] { &f64hashfunc, runtime·f64equal, runtime·memprint, runtime·memcopy }, |
| [ACPLX64] { &c64hashfunc, runtime·c64equal, runtime·memprint, runtime·memcopy }, |
| [ACPLX128] { &c128hashfunc, runtime·c128equal, runtime·memprint, runtime·memcopy }, |
| [AMEM0] { &memhashfunc, runtime·memequal0, runtime·memprint, runtime·memcopy0 }, |
| [AMEM8] { &memhashfunc, runtime·memequal8, runtime·memprint, runtime·memcopy8 }, |
| [AMEM16] { &memhashfunc, runtime·memequal16, runtime·memprint, runtime·memcopy16 }, |
| [AMEM32] { &memhashfunc, runtime·memequal32, runtime·memprint, runtime·memcopy32 }, |
| [AMEM64] { &memhashfunc, runtime·memequal64, runtime·memprint, runtime·memcopy64 }, |
| [AMEM128] { &memhashfunc, runtime·memequal128, runtime·memprint, runtime·memcopy128 }, |
| [ANOEQ0] { &nohashfunc, runtime·noequal, runtime·memprint, runtime·memcopy0 }, |
| [ANOEQ8] { &nohashfunc, runtime·noequal, runtime·memprint, runtime·memcopy8 }, |
| [ANOEQ16] { &nohashfunc, runtime·noequal, runtime·memprint, runtime·memcopy16 }, |
| [ANOEQ32] { &nohashfunc, runtime·noequal, runtime·memprint, runtime·memcopy32 }, |
| [ANOEQ64] { &nohashfunc, runtime·noequal, runtime·memprint, runtime·memcopy64 }, |
| [ANOEQ128] { &nohashfunc, runtime·noequal, runtime·memprint, runtime·memcopy128 }, |
| }; |
| |
| // Runtime helpers. |
| |
| // used in asm_{386,amd64}.s |
| #pragma dataflag NOPTR |
| byte runtime·aeskeysched[HashRandomBytes]; |
| |
| void |
| runtime·hashinit(void) |
| { |
| runtime·nohashcode = (uintptr)runtime·nohash; |
| if(NaCl) |
| return; |
| |
| // Install aes hash algorithm if we have the instructions we need |
| if((runtime·cpuid_ecx & (1 << 25)) != 0 && // aes (aesenc) |
| (runtime·cpuid_ecx & (1 << 9)) != 0 && // sse3 (pshufb) |
| (runtime·cpuid_ecx & (1 << 19)) != 0) { // sse4.1 (pinsr{d,q}) |
| byte *rnd; |
| int32 n; |
| runtime·use_aeshash = true; |
| runtime·algarray[AMEM].hash = &aeshashfunc; |
| runtime·algarray[AMEM8].hash = &aeshashfunc; |
| runtime·algarray[AMEM16].hash = &aeshashfunc; |
| runtime·algarray[AMEM32].hash = &aeshash32func; |
| runtime·algarray[AMEM64].hash = &aeshash64func; |
| runtime·algarray[AMEM128].hash = &aeshashfunc; |
| runtime·algarray[ASTRING].hash = &aeshashstrfunc; |
| // Initialize with random data so hash collisions will be hard to engineer. |
| runtime·get_random_data(&rnd, &n); |
| if(n > HashRandomBytes) |
| n = HashRandomBytes; |
| runtime·memmove(runtime·aeskeysched, rnd, n); |
| if(n < HashRandomBytes) { |
| // Not very random, but better than nothing. |
| int64 t = runtime·nanotime(); |
| while (n < HashRandomBytes) { |
| runtime·aeskeysched[n++] = (int8)(t >> (8 * (n % 8))); |
| } |
| } |
| } |
| } |
| |
| // func equal(t *Type, x T, y T) (ret bool) |
| #pragma textflag NOSPLIT |
| void |
| runtime·equal(Type *t, ...) |
| { |
| byte *x, *y; |
| bool *ret; |
| |
| x = (byte*)ROUND((uintptr)(&t+1), t->align); |
| y = x + t->size; |
| ret = (bool*)ROUND((uintptr)(y+t->size), Structrnd); |
| t->alg->equal(ret, t->size, x, y); |
| } |
| |
| // Testing adapter for memclr |
| func memclrBytes(s Slice) { |
| runtime·memclr(s.array, s.len); |
| } |