From patchwork Fri Nov 11 15:42:23 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Greenhalgh X-Patchwork-Id: 81868 Delivered-To: patch@linaro.org Received: by 10.140.97.165 with SMTP id m34csp1330992qge; Fri, 11 Nov 2016 07:43:40 -0800 (PST) X-Received: by 10.99.48.68 with SMTP id w65mr5754455pgw.68.1478879020921; Fri, 11 Nov 2016 07:43:40 -0800 (PST) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id p4si10736443pgn.253.2016.11.11.07.43.40 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 11 Nov 2016 07:43:40 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-return-441134-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org; spf=pass (google.com: domain of gcc-patches-return-441134-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-441134-patch=linaro.org@gcc.gnu.org DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-type; q=dns; s=default; b=HG6O3Nowazbjv5Vz nJK46ERYFKcUs9m7qObTaNd5vvXGaGG3F/SRwIhMndu1hZBhIXyjxs9Re10Cm1YT hPT+LUwlZ/M03TuGjumRzDbD8z0PkAP//Ni9tFlg2ctyIOiabMnR/V/aMB/XKoag c52oqXD6EvtE5rz19CPg6PJz5u4= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-type; s=default; bh=CEW5Jd8ssOMTfCYjWigP3Q ppMC8=; b=l4+UCOK5MRi5tv9t8QlUp7v+FVNeQAw+WoFzbVBVYfplcKGw3hlvZ6 S6nAvXWQXHXDjSjwBxAsnJK8TBRkJvVyEyupVbN9b8Eiz4AmhFL/erygh9uaMWEW WuBnqcJGmPby7JkDu3cYqcRJNIVXZI8h8rwVL/Hdt6kchjMl62Zus= Received: (qmail 53757 invoked by alias); 11 Nov 2016 15:43:14 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 53064 invoked by uid 89); 11 Nov 2016 15:43:13 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.8 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 spammy=decimal, sk:matthew, 22, 40, 2240 X-HELO: EUR02-AM5-obe.outbound.protection.outlook.com Received: from mail-eopbgr00076.outbound.protection.outlook.com (HELO EUR02-AM5-obe.outbound.protection.outlook.com) (40.107.0.76) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 11 Nov 2016 15:43:03 +0000 Received: from AM3PR08CA0081.eurprd08.prod.outlook.com (10.163.23.177) by VI1PR0801MB1790.eurprd08.prod.outlook.com (10.168.67.143) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.707.6; Fri, 11 Nov 2016 15:42:59 +0000 Received: from DB3FFO11FD001.protection.gbl (2a01:111:f400:7e04::177) by AM3PR08CA0081.outlook.office365.com (2a01:111:e400:8854::49) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.1.707.6 via Frontend Transport; Fri, 11 Nov 2016 15:42:59 +0000 Authentication-Results: spf=pass (sender IP is 217.140.96.140) smtp.mailfrom=arm.com; codesourcery.com; dkim=none (message not signed) header.d=none; codesourcery.com; dmarc=bestguesspass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 217.140.96.140 as permitted sender) receiver=protection.outlook.com; client-ip=217.140.96.140; helo=nebula.arm.com; Received: from nebula.arm.com (217.140.96.140) by DB3FFO11FD001.mail.protection.outlook.com (10.47.216.90) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA_P384) id 15.1.707.3 via Frontend Transport; Fri, 11 Nov 2016 15:42:59 +0000 X-IncomingTopHeaderMarker: OriginalChecksum:; UpperCasedChecksum:; SizeAsReceived:896; Count:13 Received: from e107456-lin.cambridge.arm.com (10.1.2.79) by mail.arm.com (10.1.106.66) with Microsoft SMTP Server id 14.3.294.0; Fri, 11 Nov 2016 15:42:33 +0000 From: James Greenhalgh To: CC: , , , , Subject: [Patch 14/17] [libgcc, ARM] Generalise float-to-half conversion function. Date: Fri, 11 Nov 2016 15:42:23 +0000 Message-ID: <1478878946-22725-1-git-send-email-james.greenhalgh@arm.com> In-Reply-To: <1478878647-22547-1-git-send-email-james.greenhalgh@arm.com> References: <1478878647-22547-1-git-send-email-james.greenhalgh@arm.com> MIME-Version: 1.0 X-IncomingHeaderCount: 13 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-HT: Tenant X-Forefront-Antispam-Report: CIP:217.140.96.140; IPV:CAL; SCL:-1; CTRY:GB; EFV:NLI; SFV:NSPM; SFS:(10009020)(6009001)(7916002)(2980300002)(438002)(199003)(189002)(377424004)(50986999)(512874002)(246002)(104016004)(6916009)(8676002)(110136003)(2950100002)(356003)(6666003)(5890100001)(4610100001)(7846002)(626004)(5660300001)(305945005)(568964002)(50226002)(8936002)(230783001)(84326002)(2906002)(4326007)(87936001)(189998001)(106466001)(33646002)(77096005)(26826002)(2351001)(2476003)(92566002)(586003)(86362001)(76176999)(36756003); DIR:OUT; SFP:1101; SCL:1; SRVR:VI1PR0801MB1790; H:nebula.arm.com; FPR:; SPF:Pass; PTR:fw-tnat.cambridge.arm.com; A:1; MX:1; LANG:en; X-Microsoft-Exchange-Diagnostics: 1; DB3FFO11FD001; 1:G9/qndO5L1Ixrzcmtqcyi1WIQmYDdLW9q1FnIMtaIXk0jd2Igd3LBSXLctKwxfUubYplZG+oYwA4V1s/tV8Dt6klYp8cYM4co8TQEm5ypX95YaCQ+q007ZbqCdeab41Lt29864HUWwumD5/YvxVOmX5Y3GRwNwL14utVVUOA635dMEGNX9/3YpBPOLCy6LpRTnNgIkcATJJRff+xvNJq70E5Bas/tVfv1g7eep569HFAoK5zxQilvXnAq7OhrC8qiepnQ1AzDLZdiGJ+LANS+XyXsTrztYrol0SpTxAfJesD6IHD75mnPnE9P6r2p795zl0Zrdoa48C49aQ8JQcviuNtGkSrKOfZCV9KVRAI7sfepvPI+xT65o6Gi+cDmLjQCTNbtWuJur4k7uTsmTzleysuTGTnpCcRYkTSgGXL8miierh3ScYRiQqLiZVMzaacEwFw2RWAV3IgDzDYNwylSAxiI2Ai7haZrbTFhGFRPi9Ts8vjBK4jPmjH0jmKp4TLap2PTCHGMHzRdB+1ti+YMTeU0OlKFBorWCUOhSDWkwQyX5r/G3NHPE2KJ+VKgKd8z5e1mlv/ewc8Ay0D6n+ily8t6Wx2wkFVrNbQ++G/5ks= X-Microsoft-Exchange-Diagnostics: 1; VI1PR0801MB1790; 2:kKwL+Vy63wSpR8mKdkl2qOUqnkcvUDauAs5Ceqtv6rvvd9KTqHlDOPBX75kKL3S2dWHHylQ9MkCopqDl7WBQfh/tkn9bT3kbj64cVE7Wbs/oE0ky6if5dPXmJunfH2lxwXopBBswvqXTli+jUUJKFa0CdcDTfVJY+jKJPcQcyFA=; 3:QXJE+nl1I/PabGHTKQjWPsowKK0zlWhBFcnSLOR639Glf81y9iu5g3msukccpFfcwM8atOKdbt9CHWagF6gPYqLRMi3fsh6XEKGyYNlBjB+6KjWcdM45yy/td9J0wpyS2+uW/e/q6SEQUxgizWlpMigE/YOgpqIg/RTJRLqbH8cn74K5d954WuPDh+m4cW4oxd99MwN7TG0CsHZRa1jQEs+5p2NWjOX8XPlwYSh0P+lCrQpPiCGb3kNnshWJlbAVufd3XYouyssgUYoQBQoc240EMJbJ2V51Q9JJ7UJj234= X-MS-Office365-Filtering-Correlation-Id: 208a27bc-e436-41bf-bb8e-08d40a496a1f X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001)(8251501002); SRVR:VI1PR0801MB1790; X-Microsoft-Exchange-Diagnostics: 1; VI1PR0801MB1790; 25:zEBroEJoADg6DDJDwVRfARr/2vvBxJhm7hur0nxnqntzLRUhOducI/mZsmAQqDVTJhJxFHdmGvUKSeTpgVhjV/edyD1t9sQfK5b0wWqbEeLApKtiA15UO4x64lvd6Gi4mkjwvmeFS5M7UWKXm65p0HMdJCizRw6QtSFEty+RKFJdoyN6Lk1B1dhxWHackaE0ean0aaMnmaMEwx2vpP5Mx+iXhmUNzyzfCEySmib7WhDI69ZS/jz4NN+O7bNNlv1PG0c27cSIfG/m1abPdogrD7nsuxo9wzD2FdX4Xpfqo6PcIjtjNO5Qm1eGpWk1sPG2FoMW9ioFUp1J+Mp63BpR0PqGhfCqiQqzPaoCj/tN1pMbwGtTl/cwzToI0xq5ovyV+Es9vf/KucWgDC+vLPLKCAX4NyYC6cX6SHQ3nmyxPDggQZgvEKzgt9QO1V8lh4KzkOv/ZWGydADjT03pbtpDmJNkMxw3J1/6b/bvKjxtk5hIMUGZWQMAJlfbMWQ4xe66UuEuMZA2zKtEDsj87DpJPo9EQBDZdv7CWdTqsSt/QIcYK88mr1Ug/UGW84T+M94NJMHP92S2SmVUhvXTLWi2aOSCpd0iReQTktADI1aRzsDN8wL0pHTjgVRSWE9sluLkZ03T/5zGWNe6XQAkd+VlUJDIFJfUum1Qajbe9N0SRf0h4e5cNkTrWJJM3oSjVkri/ER1/Raj84JhCJfX1ybh7ZZWunPfuW6qdGp9Gw7upQuGHFniCff7j4zHzJfRcejhubFoeoT4mrn80CN8C2GaC0bTunMrN9W9ZxXRoD4ZHEeThY5lOxuAp3A/LhF8WAHTBVxWdlYHCIr4o7hhxMzJgevwT/WGVDLHSsMhszN1O5C1uxS8GIgQOCJTFa719YrKcq9gfzwdvwbrKvdLDe5X8gEVwMVBnGPVITIMkWi0t1lWDMicRnbS8g/L+n8m/MK2 X-Microsoft-Exchange-Diagnostics: 1; VI1PR0801MB1790; 31:VcA8AgauRGofVolpDGHtLnfuCla5wTbjWNAQ0jhoY7hB9vs4eBjwML8Cc5k1ixp9Cc4+uZqCqwlBHnWw9l/rJQGwhDE2OxwPKgGonAgw8MCRpmLpUl3TqqG5xrD2RdKPtQaWLQOrs8yDtxy9fKt99kPtUh9jGlnpCYUBhyViOgCEa97INYedp1DchLSwatteD3AHa1fIgCcxGRXpLM5x4lRixLaF0OUxyeaAo3WOMLzcYWfjotaH88e9WCm6a47h45l50RdD0dOTakfFUp3Rx/hoy5ytJSSzkVE48BwlSg78c3iD6HrN42DwQoNsbIgWDF+n7QjnQuMvslJs2DWAxg==; 20:zFM3gmaYL0iL0yuvWoRelaqkJ2a2UyfY/HQM9T/W5OYQfFkp7ZjD81+JOmA8AIOS3jZAbL3wzcJKZAporFxnC66wNVcg51IFB5cSRkeg5oKSUvZnY6OtdrlucY7QArTs7p/s73gLEjl0JHwhijrRgyr0q5aUBqNUzV5od5mg4YWCrfaemU6QoDrNMZ3LT16e/mwrgX9OnuK0+Dojwd5y7ekc6EVe0LtZhRidf0Xviy77TNGdzP82Rcl1N8OFnW/c NoDisclaimer: True X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(180628864354917); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(102415395)(6040176)(601004)(2401047)(5005006)(13024025)(8121501046)(13013025)(13020025)(13023025)(10201501046)(3002001)(6055026); SRVR:VI1PR0801MB1790; BCL:0; PCL:0; RULEID:; SRVR:VI1PR0801MB1790; X-Microsoft-Exchange-Diagnostics: 1; VI1PR0801MB1790; 4:5C8uyf1Fitg+PrMFWR/k1mSjF/rs594MHDoLKX3krzia6G6PDV1OfISKzPL3DvX+oyDv+yYjtGPU/Hlo+IG30XVaxSuqjl9K+J4gd3ns8q2PG7Z2tr6af+A5w5wPTvFJnYW49C4cIPDYKX/dPBDiXiMaTskLzBukWhQjeQWs5N9xIGQEoXHqM3NPaZXLa/TGuGVM3OHAtvJ92iuH2ph76t3bRSW8U0idfvac+Or287fLzouwQdTe2DxNdhKxQbxKsgLbswCJgpRJlB9jh58H/Rr01v21+Km3n/fbOnNgBqxapgMLtpNcH9OVjLYdrH0dkGb4c8lJJ09fvpscDX/RY9U2MSI+hKy5S6RADf8o9SgSloEAZYsduSoN3MWhLfZJb+P0nKQu+JTucuHS/OosKhjlgYrWso5I1INby/Hrg0vocGufWW+O4TwDkaHnHdudCUt2lX4y4e/yf4U3QMb4IdsqkjkIqrSTACe9PE3cME9fnA6u2l59QPhJ3TiRHrMD4/KWPtdhqYJzZgrJ/NjdmWGRp3Hg4H+blYzg4xmCAcXO1wffbn3z3RdN/kzeeyWo X-Forefront-PRVS: 012349AD1C X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; VI1PR0801MB1790; 23:kzbXD01zbwktc64/3mflaGhv82GEQVdJmPdfn6U?= =?us-ascii?Q?xJOWa/v3pqxZQQH5sbX0aV5m//F7wRgLPJr59y+tlbXy2wQgmcZhQ08iddWx?= =?us-ascii?Q?iLo9J6dFMytBGrCWfWJTB5BApjJdd61S+taH1526O4B6v31PC/zsCKRO1GCd?= =?us-ascii?Q?Bep3ccYwSyxwl8PUpmJnKSe0FDJZ/T+Q+Pa5YDYmB0kw/kq67FmaT9dkAk9S?= =?us-ascii?Q?ytqMGvQLX8yJK3fAUQ705P8IIjeHl7yNmZ+PKXHNcOMonlpFGSK1QtefIUVD?= =?us-ascii?Q?IFGzmGIYTt+hXD7c78I4y2lQGXyndOgA2chkaub8ZETVBQUvN8DSkzpP2fc9?= =?us-ascii?Q?JwEo2jyIzaeDQEqog+iYtY0ZlvFkO5nDG2yt+Pc6oByszxwJXlDdcwmSEW4/?= =?us-ascii?Q?ZM+h09zi8KHCHmnM9QFXyUE6KWqTw0lmq8xezD6TEbTvRB29HZixEkkIDuUt?= =?us-ascii?Q?7mxpL7KpOPfxi8dvLvwubl4O2inBgwLxH40ES7gYQ3Hm1V7wGhYSdcqmzpv3?= =?us-ascii?Q?R1l5kWK0UCsUPilr/kP738MYSLL8ZX0RGzddx6junS2quuh2GtUF7/nQ9Pg4?= =?us-ascii?Q?AfH4TdxrtPROHD1rdq2cV8JvsVAaclRTNsrTsotBjk9SX1Po7UI2NH5hqRjA?= =?us-ascii?Q?miHh2FHhvVVjlAi0j+Mh68RewPL4I+wItQHbYrlMkb5rHVQ45O0ywzR1Gx49?= =?us-ascii?Q?oUwdAC+WLH/UnbTb1bMbFRH940eu3WMrRvh6ui4d1e/9TloJpPzyT9N53uIZ?= =?us-ascii?Q?ABRBpudLGMOOCIUu1dj7z61x8a3+k2v7An9TLUAgFf0Ojpbm+yXAxJ3HDG48?= =?us-ascii?Q?RPDu5h8CrU6a5NfsRYJfW4aHclJIFC1hj7NvlN7rlUqeTu1BrTaQkrp9i6Q7?= =?us-ascii?Q?rkJpm5D47W9/2p9kZnu7uPDK/JlEauUCGX6+45rzI5/Qcjjf9Re1S7RjPT/n?= =?us-ascii?Q?skBAgd28rKwz2ZXQS39GYfuD+OdDN/xbkUp4BxbL0dLskSqKevjxZh02vi7Y?= =?us-ascii?Q?lsHee/EgEpfgq4Rkdka7DzEPQhRlpNPT8rb17tugWUTqs5TNxZLHvr6sKNkX?= =?us-ascii?Q?AUwKZpYt/H84wnIbWuckraKGVN/t4?= X-Microsoft-Exchange-Diagnostics: 1; VI1PR0801MB1790; 6://7zkgt1qBG5tlYJsxdvZvW4lKRQMxCZe9Po3epKpLNoqLxeMy6hiIgX1Z63oA+qQI4JAlUQWmHNn4rU+53R9IxFTCFutHCK+YEkrmnnUqB7YNZlT8G6NZu9WKAgauk6Q7bdTKzHsuhtavpjzEWi0ASwnRKW8+mYc/IPZzVg3XNaiuqext1KyGJd0ORujZcE4cO8a5OGhxSPER4WB/8mrddwsj/QX7oTARaou2YLCg0CzrFlEwWuwiCm5serlTyjY6MsfWtBCMvl75q4DD3J9HpwEsOJAGAvkyKKfw/7/pq7lsTLbt8FKjuJTXXy1kMxzoFbLZNMldDr/0roFZfhAroKmYef3vbew7RgMNH39p8=; 5:JxpVL+iflljBaPUZlLo118yaBzsT2Hf3AFcxNFjsDosn8oKxVlAmBK6Mjlrj7B5NILb0+aY5xwgdezw+uX56MsS6EfaCoN28Mtvq2mM+uFpE3zzeWHkmwgPenpXdJyB/fS2VhVp+xmiOxwsWqu0F2nEpgKkwiywUxDCvHf8WjPs=; 24:ZuyI1E0XkDyx117kEDfi5ZtuA2HCZvqkUPsEduTqGDG9/wKLTZ71FuJOT/zapH67jsbaTEaA2owDoglIolqUYR5yfJ5nLHRjXX4q5MDO/yw= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; VI1PR0801MB1790; 7:bKzyLuKfsQRYUyoUt3FEOkkaX3NPG6ke9RvZrE/2Crge07OJerEB8x7NaviW2YIDh0f7XZw0FRTsrgC50tZwQbEzhkkRryiEVw2Og4t2zfGKBAJN4jUQgiMeofDXbmsVBtJqR2RPTK/B7HWsQ6CeNM9ZJByuCavR7mkgU3LYtiE9bQLa45KUugYpR4aHePKgoBloseMl61cv1b4303mHur2cZ4UaFawB8VuEhyOR4GzinFEwcN4OFuVEx5kd15eLVpSBfLxxeT/gV9qrHnLKcWh7dVwGPGu9+MokI42ImV1dUlV8cIW6jolqmSNUbEorP4wRj1iicqKmRXsPh/2DXIX3//0zrgTOfe/NI2NAU1U= X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Nov 2016 15:42:59.2941 (UTC) X-MS-Exchange-CrossTenant-Id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=f34e5979-57d9-4aaa-ad4d-b122a662184d; Ip=[217.140.96.140]; Helo=[nebula.arm.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR0801MB1790 X-IsSubscribed: yes Hi, I'm adapting this patch from work started by Matthew Wahab. Conversions from double precision floats to the ARM __fp16 are required to round only once. A conversion function for double to __fp16 to support this on soft-fp targets. This and the following patch add this conversion function by reusing the exising float to __fp16 function config/arm/fp16.c:__gnu_f2h_internal. This patch generalizes __gnu_f2h_internal by adding a specification of the source format and reworking the code to make use of it. Initially, only the binary32 format is supported. A previous version of this patch had a bug handling rounding, the update in this patch should be sufficient to fix the bug, replacing: > else > mask = 0x00001fff; With: mask = (point - 1) >> 10; I've tested that fix throwing semi-random bit-patterns at the conversion function to confirm that the software implementation now matches the hardware behaviour for this routine. Additionally, bootstrapped again, and cross-tested with no issues. OK? Thanks, James ---- libgcc/ 2016-11-09 James Greenhalgh Matthew Wahab * config/arm/fp16.c (struct format): New. (binary32): New. (__gnu_float2h_internal): New. Body moved from __gnu_f2h_internal and generalize. (_gnu_f2h_internal): Move body to function __gnu_float2h_internal. Call it with binary32. diff --git a/libgcc/config/arm/fp16.c b/libgcc/config/arm/fp16.c index 39c863c..ba89796 100644 --- a/libgcc/config/arm/fp16.c +++ b/libgcc/config/arm/fp16.c @@ -22,40 +22,74 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see . */ +struct format +{ + /* Number of bits. */ + unsigned long long size; + /* Exponent bias. */ + unsigned long long bias; + /* Exponent width in bits. */ + unsigned long long exponent; + /* Significand precision in explicitly stored bits. */ + unsigned long long significand; +}; + +static const struct format +binary32 = +{ + 32, /* size. */ + 127, /* bias. */ + 8, /* exponent. */ + 23 /* significand. */ +}; + static inline unsigned short -__gnu_f2h_internal(unsigned int a, int ieee) +__gnu_float2h_internal (const struct format* fmt, + unsigned long long a, int ieee) { - unsigned short sign = (a >> 16) & 0x8000; - int aexp = (a >> 23) & 0xff; - unsigned int mantissa = a & 0x007fffff; - unsigned int mask; - unsigned int increment; + unsigned long long point = 1ULL << fmt->significand;; + unsigned short sign = (a >> (fmt->size - 16)) & 0x8000; + int aexp; + unsigned long long mantissa; + unsigned long long mask; + unsigned long long increment; + + /* Get the exponent and mantissa encodings. */ + mantissa = a & (point - 1); + + mask = (1 << fmt->exponent) - 1; + aexp = (a >> fmt->significand) & mask; - if (aexp == 0xff) + /* Infinity, NaN and alternative format special case. */ + if (((unsigned int) aexp) == mask) { if (!ieee) return sign; if (mantissa == 0) return sign | 0x7c00; /* Infinity. */ /* Remaining cases are NaNs. Convert SNaN to QNaN. */ - return sign | 0x7e00 | (mantissa >> 13); + return sign | 0x7e00 | (mantissa >> (fmt->significand - 10)); } + /* Zero. */ if (aexp == 0 && mantissa == 0) return sign; - aexp -= 127; + /* Construct the exponent and mantissa. */ + aexp -= fmt->bias; + + /* Decimal point is immediately after the significand. */ + mantissa |= point; - /* Decimal point between bits 22 and 23. */ - mantissa |= 0x00800000; if (aexp < -14) { - mask = 0x00ffffff; + mask = point | (point - 1); + /* Minimum exponent for half-precision is 2^-24. */ if (aexp >= -25) mask >>= 25 + aexp; } else - mask = 0x00001fff; + mask = (point - 1) >> 10; /* Round. */ if (mantissa & mask) @@ -64,8 +98,8 @@ __gnu_f2h_internal(unsigned int a, int ieee) if ((mantissa & mask) == increment) increment = mantissa & (increment << 1); mantissa += increment; - if (mantissa >= 0x01000000) - { + if (mantissa >= (point << 1)) + { mantissa >>= 1; aexp++; } @@ -93,7 +127,13 @@ __gnu_f2h_internal(unsigned int a, int ieee) /* We leave the leading 1 in the mantissa, and subtract one from the exponent bias to compensate. */ - return sign | (((aexp + 14) << 10) + (mantissa >> 13)); + return sign | (((aexp + 14) << 10) + (mantissa >> (fmt->significand - 10))); +} + +static inline unsigned short +__gnu_f2h_internal (unsigned int a, int ieee) +{ + return __gnu_float2h_internal (&binary32, (unsigned long long) a, ieee); } unsigned int