From patchwork Sun Aug 30 02:32:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 248721 Delivered-To: patch@linaro.org Received: by 2002:a92:5b9c:0:0:0:0:0 with SMTP id c28csp2299311ilg; Sat, 29 Aug 2020 19:34:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyIdyVhPj+gSAM+tjIAiJmfCHzRzjcvegKZMYg1IUsSqVqZRYLNC4uJNj1mrYyYfpOGKy+r X-Received: by 2002:a25:f304:: with SMTP id c4mr10402180ybs.209.1598754852691; Sat, 29 Aug 2020 19:34:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598754852; cv=none; d=google.com; s=arc-20160816; b=RVJm1pGXQEcXstFC9Mbtwdu8hNfwogt4AtzQygkZzR/6J2P/ycD6RSdIGW25QBrdCK 0Knn9tlzx8uBP9UlL8typTjWpw4IWB2wBQHHwyVvCg/60suEapNOaWWqNM4d+Ion/hy/ +YUVt3v4+pUMGGGNLE2A+nwYtHFmdIaeHeVenQ+1Fy5aKA2A4dx740rByCFTus9SCACu Cdql9dzbF8F4thOrh418AD3tqp8rwc2jqNVmbscN7NRBKmLfE2gfvbsbyS5uV9iidHUn lwxlD1uFQYC/zuLoU/Xzj/mhqGeTUkU25HLgERMuXBngkxXNbsu38kt3s4Q3/dxXrH3o tjig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=NO3P9ik3JgoBcB+gtLZEAETdlVtAEmQlYrb0l66oha4=; b=lqfpMUxXdiBeLATbA2RaJ8H/Aa5b5MQ3LJz44JdKohkh/9ZCTttqCvVMiCF45Qb3ro mA1aXZb8EfUxY74asVUYbtfyU0WHn2rt2bBHKJ/UvwYd6H3DtUf3Qr7BXQbWvX7m7cli pFI5ndPRvI2Dfoz9NJH/ur0FAfhGc4RvGYoc0KQr+QSmR+9dr8XLY+AsmRX/lZx5NyFs uV5R3D2QVTr8nFdGC2E6DwglE1yjnlIwDbHA2IwxHFC6d2qvNdRtqiAWDSNsbUE8SQwk 694uVlvinUYBeafjJVdnuGgXR383HNpUG+mwA6iZACAwIkxVrJo72DUEc+CzolNXvBuI Aajw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Eb1ZzKte; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id l14si5162829ybt.321.2020.08.29.19.34.12 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sat, 29 Aug 2020 19:34:12 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Eb1ZzKte; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:43046 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kCDAa-0005oP-4o for patch@linaro.org; Sat, 29 Aug 2020 22:34:12 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:52860) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kCD8i-0002lC-Ro for qemu-devel@nongnu.org; Sat, 29 Aug 2020 22:32:16 -0400 Received: from mail-pl1-x62a.google.com ([2607:f8b0:4864:20::62a]:39941) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kCD8g-0002Yn-DF for qemu-devel@nongnu.org; Sat, 29 Aug 2020 22:32:16 -0400 Received: by mail-pl1-x62a.google.com with SMTP id z15so1421843plo.7 for ; Sat, 29 Aug 2020 19:32:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=NO3P9ik3JgoBcB+gtLZEAETdlVtAEmQlYrb0l66oha4=; b=Eb1ZzKtevnMxJjzR6Cfxw7hOIFDI3QedSXz35giQKKD3U/5maYhqqwbs6n6BmhfKcj Qv0YAsgy22obJSPy3xXVm1fimCez1xAMRRHFb2TW9Ik6m0AxZXHf78X2TdgoQ4/Tf2ni lvPZe9MLQ1IX84TGwtVOmu0uqjkO+XN/oVrnsGaV1t4XUXlgX2DXrD70b83y6j6EymJJ ZPb1c8xCCrf/HZ8eJzYDE/9xJrI1hTaMqBd0/WvLoTPwoRokPh6vRd5VP8HduxIq+fNb gVeWZrPM+Ckr0TZI/fvBVYJDvSjJ54oHYH86gJwx0e3CZ4mjNs+/IRQVxsD/GWI2zXb5 UuMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=NO3P9ik3JgoBcB+gtLZEAETdlVtAEmQlYrb0l66oha4=; b=SD+HUg13EXE6W+T32PLhGQo9ll79BtCLtGbpPv0FUs8B1P6JfL6/ffmpFMvXRIjFjF X8++Pw+/5GwB3ROYbSYZaiFZSOpWfHFBBvl7u6BQNkw83bnrvNpbGS8UkKlBygXM/uph Sgr38YVa1z3ExH0Ky+CdpM1MZky1RQihuGaERcOUqux6i4pnW4tRNyJ6uZnYs49vdP8N Opus7cONarPUCWq/AbKb0p5YYX8uXqLN/559moIYEZjmnu9LvWYseBpQPmljU6NEP6eC p0cZwUgOyQUdBtgRnFaHIupPLFJNjT1ri3rBli0MZDHYsbc1LdGmFmX57qq9tbuZDa44 XKww== X-Gm-Message-State: AOAM531rNqGF/K6Ql7F+oKTAt4j1cTlzPFw7qGlX8fEG7Q7LmRtT7BJz tb89YbdT3rECaOu7i219rPg+dAVbplhkdg== X-Received: by 2002:a17:902:708b:: with SMTP id z11mr4352889plk.326.1598754732552; Sat, 29 Aug 2020 19:32:12 -0700 (PDT) Received: from localhost.localdomain ([71.212.141.89]) by smtp.gmail.com with ESMTPSA id l23sm3546269pgt.16.2020.08.29.19.32.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 29 Aug 2020 19:32:11 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PULL 5/7] softfloat: Define convert operations for bfloat16 Date: Sat, 29 Aug 2020 19:32:01 -0700 Message-Id: <20200830023203.612312-6-richard.henderson@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200830023203.612312-1-richard.henderson@linaro.org> References: <20200830023203.612312-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62a; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x62a.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peter.maydell@linaro.org, LIU Zhiwei Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" From: LIU Zhiwei Signed-off-by: LIU Zhiwei Reviewed-by: Richard Henderson Message-Id: <20200813071421.2509-3-zhiwei_liu@c-sky.com> [rth: Use FloatRoundMode for conversion functions.] Signed-off-by: Richard Henderson --- include/fpu/softfloat.h | 54 ++++++++++ fpu/softfloat.c | 223 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 277 insertions(+) -- 2.25.1 diff --git a/include/fpu/softfloat.h b/include/fpu/softfloat.h index 68b3cf488d..95f0789a92 100644 --- a/include/fpu/softfloat.h +++ b/include/fpu/softfloat.h @@ -348,6 +348,60 @@ static inline bool float16_unordered_quiet(float16 a, float16 b, #define float16_three make_float16(0x4200) #define float16_infinity make_float16(0x7c00) +/*---------------------------------------------------------------------------- +| Software bfloat16 conversion routines. +*----------------------------------------------------------------------------*/ + +bfloat16 bfloat16_round_to_int(bfloat16, float_status *status); +bfloat16 float32_to_bfloat16(float32, float_status *status); +float32 bfloat16_to_float32(bfloat16, float_status *status); +bfloat16 float64_to_bfloat16(float64 a, float_status *status); +float64 bfloat16_to_float64(bfloat16 a, float_status *status); + +int16_t bfloat16_to_int16_scalbn(bfloat16, FloatRoundMode, + int, float_status *status); +int32_t bfloat16_to_int32_scalbn(bfloat16, FloatRoundMode, + int, float_status *status); +int64_t bfloat16_to_int64_scalbn(bfloat16, FloatRoundMode, + int, float_status *status); + +int16_t bfloat16_to_int16(bfloat16, float_status *status); +int32_t bfloat16_to_int32(bfloat16, float_status *status); +int64_t bfloat16_to_int64(bfloat16, float_status *status); + +int16_t bfloat16_to_int16_round_to_zero(bfloat16, float_status *status); +int32_t bfloat16_to_int32_round_to_zero(bfloat16, float_status *status); +int64_t bfloat16_to_int64_round_to_zero(bfloat16, float_status *status); + +uint16_t bfloat16_to_uint16_scalbn(bfloat16 a, FloatRoundMode, + int, float_status *status); +uint32_t bfloat16_to_uint32_scalbn(bfloat16 a, FloatRoundMode, + int, float_status *status); +uint64_t bfloat16_to_uint64_scalbn(bfloat16 a, FloatRoundMode, + int, float_status *status); + +uint16_t bfloat16_to_uint16(bfloat16 a, float_status *status); +uint32_t bfloat16_to_uint32(bfloat16 a, float_status *status); +uint64_t bfloat16_to_uint64(bfloat16 a, float_status *status); + +uint16_t bfloat16_to_uint16_round_to_zero(bfloat16 a, float_status *status); +uint32_t bfloat16_to_uint32_round_to_zero(bfloat16 a, float_status *status); +uint64_t bfloat16_to_uint64_round_to_zero(bfloat16 a, float_status *status); + +bfloat16 int16_to_bfloat16_scalbn(int16_t a, int, float_status *status); +bfloat16 int32_to_bfloat16_scalbn(int32_t a, int, float_status *status); +bfloat16 int64_to_bfloat16_scalbn(int64_t a, int, float_status *status); +bfloat16 uint16_to_bfloat16_scalbn(uint16_t a, int, float_status *status); +bfloat16 uint32_to_bfloat16_scalbn(uint32_t a, int, float_status *status); +bfloat16 uint64_to_bfloat16_scalbn(uint64_t a, int, float_status *status); + +bfloat16 int16_to_bfloat16(int16_t a, float_status *status); +bfloat16 int32_to_bfloat16(int32_t a, float_status *status); +bfloat16 int64_to_bfloat16(int64_t a, float_status *status); +bfloat16 uint16_to_bfloat16(uint16_t a, float_status *status); +bfloat16 uint32_to_bfloat16(uint32_t a, float_status *status); +bfloat16 uint64_to_bfloat16(uint64_t a, float_status *status); + /*---------------------------------------------------------------------------- | Software bfloat16 operations. *----------------------------------------------------------------------------*/ diff --git a/fpu/softfloat.c b/fpu/softfloat.c index 77d81ab9b0..67cfa0fd82 100644 --- a/fpu/softfloat.c +++ b/fpu/softfloat.c @@ -2014,6 +2014,34 @@ float32 float64_to_float32(float64 a, float_status *s) return float32_round_pack_canonical(pr, s); } +float32 bfloat16_to_float32(bfloat16 a, float_status *s) +{ + FloatParts p = bfloat16_unpack_canonical(a, s); + FloatParts pr = float_to_float(p, &float32_params, s); + return float32_round_pack_canonical(pr, s); +} + +float64 bfloat16_to_float64(bfloat16 a, float_status *s) +{ + FloatParts p = bfloat16_unpack_canonical(a, s); + FloatParts pr = float_to_float(p, &float64_params, s); + return float64_round_pack_canonical(pr, s); +} + +bfloat16 float32_to_bfloat16(float32 a, float_status *s) +{ + FloatParts p = float32_unpack_canonical(a, s); + FloatParts pr = float_to_float(p, &bfloat16_params, s); + return bfloat16_round_pack_canonical(pr, s); +} + +bfloat16 float64_to_bfloat16(float64 a, float_status *s) +{ + FloatParts p = float64_unpack_canonical(a, s); + FloatParts pr = float_to_float(p, &bfloat16_params, s); + return bfloat16_round_pack_canonical(pr, s); +} + /* * Rounds the floating-point value `a' to an integer, and returns the * result as a floating-point value. The operation is performed @@ -2143,6 +2171,18 @@ float64 float64_round_to_int(float64 a, float_status *s) return float64_round_pack_canonical(pr, s); } +/* + * Rounds the bfloat16 value `a' to an integer, and returns the + * result as a bfloat16 value. + */ + +bfloat16 bfloat16_round_to_int(bfloat16 a, float_status *s) +{ + FloatParts pa = bfloat16_unpack_canonical(a, s); + FloatParts pr = round_to_int(pa, s->float_rounding_mode, 0, s); + return bfloat16_round_pack_canonical(pr, s); +} + /* * Returns the result of converting the floating-point value `a' to * the two's complement integer format. The conversion is performed @@ -2365,6 +2405,62 @@ int64_t float64_to_int64_round_to_zero(float64 a, float_status *s) return float64_to_int64_scalbn(a, float_round_to_zero, 0, s); } +/* + * Returns the result of converting the floating-point value `a' to + * the two's complement integer format. + */ + +int16_t bfloat16_to_int16_scalbn(bfloat16 a, FloatRoundMode rmode, int scale, + float_status *s) +{ + return round_to_int_and_pack(bfloat16_unpack_canonical(a, s), + rmode, scale, INT16_MIN, INT16_MAX, s); +} + +int32_t bfloat16_to_int32_scalbn(bfloat16 a, FloatRoundMode rmode, int scale, + float_status *s) +{ + return round_to_int_and_pack(bfloat16_unpack_canonical(a, s), + rmode, scale, INT32_MIN, INT32_MAX, s); +} + +int64_t bfloat16_to_int64_scalbn(bfloat16 a, FloatRoundMode rmode, int scale, + float_status *s) +{ + return round_to_int_and_pack(bfloat16_unpack_canonical(a, s), + rmode, scale, INT64_MIN, INT64_MAX, s); +} + +int16_t bfloat16_to_int16(bfloat16 a, float_status *s) +{ + return bfloat16_to_int16_scalbn(a, s->float_rounding_mode, 0, s); +} + +int32_t bfloat16_to_int32(bfloat16 a, float_status *s) +{ + return bfloat16_to_int32_scalbn(a, s->float_rounding_mode, 0, s); +} + +int64_t bfloat16_to_int64(bfloat16 a, float_status *s) +{ + return bfloat16_to_int64_scalbn(a, s->float_rounding_mode, 0, s); +} + +int16_t bfloat16_to_int16_round_to_zero(bfloat16 a, float_status *s) +{ + return bfloat16_to_int16_scalbn(a, float_round_to_zero, 0, s); +} + +int32_t bfloat16_to_int32_round_to_zero(bfloat16 a, float_status *s) +{ + return bfloat16_to_int32_scalbn(a, float_round_to_zero, 0, s); +} + +int64_t bfloat16_to_int64_round_to_zero(bfloat16 a, float_status *s) +{ + return bfloat16_to_int64_scalbn(a, float_round_to_zero, 0, s); +} + /* * Returns the result of converting the floating-point value `a' to * the unsigned integer format. The conversion is performed according @@ -2590,6 +2686,62 @@ uint64_t float64_to_uint64_round_to_zero(float64 a, float_status *s) return float64_to_uint64_scalbn(a, float_round_to_zero, 0, s); } +/* + * Returns the result of converting the bfloat16 value `a' to + * the unsigned integer format. + */ + +uint16_t bfloat16_to_uint16_scalbn(bfloat16 a, FloatRoundMode rmode, + int scale, float_status *s) +{ + return round_to_uint_and_pack(bfloat16_unpack_canonical(a, s), + rmode, scale, UINT16_MAX, s); +} + +uint32_t bfloat16_to_uint32_scalbn(bfloat16 a, FloatRoundMode rmode, + int scale, float_status *s) +{ + return round_to_uint_and_pack(bfloat16_unpack_canonical(a, s), + rmode, scale, UINT32_MAX, s); +} + +uint64_t bfloat16_to_uint64_scalbn(bfloat16 a, FloatRoundMode rmode, + int scale, float_status *s) +{ + return round_to_uint_and_pack(bfloat16_unpack_canonical(a, s), + rmode, scale, UINT64_MAX, s); +} + +uint16_t bfloat16_to_uint16(bfloat16 a, float_status *s) +{ + return bfloat16_to_uint16_scalbn(a, s->float_rounding_mode, 0, s); +} + +uint32_t bfloat16_to_uint32(bfloat16 a, float_status *s) +{ + return bfloat16_to_uint32_scalbn(a, s->float_rounding_mode, 0, s); +} + +uint64_t bfloat16_to_uint64(bfloat16 a, float_status *s) +{ + return bfloat16_to_uint64_scalbn(a, s->float_rounding_mode, 0, s); +} + +uint16_t bfloat16_to_uint16_round_to_zero(bfloat16 a, float_status *s) +{ + return bfloat16_to_uint16_scalbn(a, float_round_to_zero, 0, s); +} + +uint32_t bfloat16_to_uint32_round_to_zero(bfloat16 a, float_status *s) +{ + return bfloat16_to_uint32_scalbn(a, float_round_to_zero, 0, s); +} + +uint64_t bfloat16_to_uint64_round_to_zero(bfloat16 a, float_status *s) +{ + return bfloat16_to_uint64_scalbn(a, float_round_to_zero, 0, s); +} + /* * Integer to float conversions * @@ -2721,6 +2873,41 @@ float64 int16_to_float64(int16_t a, float_status *status) return int64_to_float64_scalbn(a, 0, status); } +/* + * Returns the result of converting the two's complement integer `a' + * to the bfloat16 format. + */ + +bfloat16 int64_to_bfloat16_scalbn(int64_t a, int scale, float_status *status) +{ + FloatParts pa = int_to_float(a, scale, status); + return bfloat16_round_pack_canonical(pa, status); +} + +bfloat16 int32_to_bfloat16_scalbn(int32_t a, int scale, float_status *status) +{ + return int64_to_bfloat16_scalbn(a, scale, status); +} + +bfloat16 int16_to_bfloat16_scalbn(int16_t a, int scale, float_status *status) +{ + return int64_to_bfloat16_scalbn(a, scale, status); +} + +bfloat16 int64_to_bfloat16(int64_t a, float_status *status) +{ + return int64_to_bfloat16_scalbn(a, 0, status); +} + +bfloat16 int32_to_bfloat16(int32_t a, float_status *status) +{ + return int64_to_bfloat16_scalbn(a, 0, status); +} + +bfloat16 int16_to_bfloat16(int16_t a, float_status *status) +{ + return int64_to_bfloat16_scalbn(a, 0, status); +} /* * Unsigned Integer to float conversions @@ -2851,6 +3038,42 @@ float64 uint16_to_float64(uint16_t a, float_status *status) return uint64_to_float64_scalbn(a, 0, status); } +/* + * Returns the result of converting the unsigned integer `a' to the + * bfloat16 format. + */ + +bfloat16 uint64_to_bfloat16_scalbn(uint64_t a, int scale, float_status *status) +{ + FloatParts pa = uint_to_float(a, scale, status); + return bfloat16_round_pack_canonical(pa, status); +} + +bfloat16 uint32_to_bfloat16_scalbn(uint32_t a, int scale, float_status *status) +{ + return uint64_to_bfloat16_scalbn(a, scale, status); +} + +bfloat16 uint16_to_bfloat16_scalbn(uint16_t a, int scale, float_status *status) +{ + return uint64_to_bfloat16_scalbn(a, scale, status); +} + +bfloat16 uint64_to_bfloat16(uint64_t a, float_status *status) +{ + return uint64_to_bfloat16_scalbn(a, 0, status); +} + +bfloat16 uint32_to_bfloat16(uint32_t a, float_status *status) +{ + return uint64_to_bfloat16_scalbn(a, 0, status); +} + +bfloat16 uint16_to_bfloat16(uint16_t a, float_status *status) +{ + return uint64_to_bfloat16_scalbn(a, 0, status); +} + /* Float Min/Max */ /* min() and max() functions. These can't be implemented as * 'compare and pick one input' because that would mishandle