# HG changeset patch # User Michael Pavone # Date 1428127626 25200 # Node ID 2a0463c46913b8c038a1f1d2b6a49df1b5cf64fc # Parent 79a14e41b79ae69a6c9ca936983984ff95ba9702 Add hash method to float types and make hash always return a 32-bit int diff -r 79a14e41b79a -r 2a0463c46913 cbackend.js --- a/cbackend.js Mon Mar 30 19:12:51 2015 -0700 +++ b/cbackend.js Fri Apr 03 23:07:06 2015 -0700 @@ -822,12 +822,25 @@ 'return ' + toplevel.moduleVar('true') + ';' ] }); - intObj.addMessage('hash', { - vars: {}, - lines: [ - 'return &(self->header);' - ] - }); + if (bits == 32 && !unsigned) { + intObj.addMessage('hash', { + vars: {}, + lines: [ + 'return &(self->header);' + ] + }); + } else { + intObj.addMessage('hash', { + vars: { + int32ret: 'obj_int32 *' + }, + lines: [ + 'int32ret = make_object(&obj_int32_meta, NULL, 0);', + bits < 64 ? 'int32ret->num = self->num;' : 'int32ret->num = self->num ^ (self->num >> 32);', + 'return &(int32ret->header);' + ] + }); + } intObj.addMessage('signed?', { vars: {}, lines: [ @@ -901,6 +914,20 @@ 'return &(str->header);' ] }); + + floatObj.addMessage('hash', { + vars: { + intptr: 'int32_t *', + int32ret: 'obj_int32 *' + }, + lines: [ + 'intptr = (int32_t *)&self->num;', + 'int32ret = make_object(&obj_int32_meta, NULL, 0);', + 'int32ret->num = *intptr' + (size == 64 ? ' ^ intptr[1];' : ';'), + 'return &(int32ret->header);' + ] + }); + floatObj.addMessage('f' + bits, { vars: {}, lines: [ diff -r 79a14e41b79a -r 2a0463c46913 samples/hash.tp --- a/samples/hash.tp Mon Mar 30 19:12:51 2015 -0700 +++ b/samples/hash.tp Fri Apr 03 23:07:06 2015 -0700 @@ -6,5 +6,8 @@ print: "hash of 1 is " . (1 hash) . "\n" print: "hash of 2 is " . (2 hash) . "\n" print: "hash of 3 is " . (3 hash) . "\n" + print: "hash of 1u32 is " . (1u32 hash) . "\n" + print: "hash of 2.0 is " . (2.0 hash) . "\n" + print: "hash of 3f32 is " . (3.0f32 hash) . "\n" } }